Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions transformer_lens/components/abstract_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,11 @@ def _apply_qk_norm(
Returns:
Normalized tensor with same shape as input
"""
# Reshape from [batch, pos, head_index, d_head] to [batch * pos * head_index, d_head]
d_head = x.shape[-1]
x_normed = norm_module(x.reshape(-1, d_head))
return x_normed.reshape(x.shape)
# Reshape from [batch, pos, head_index, d_head] to [batch * pos, head_index, d_head]
# so it matches RMSNorm's expected 3D input shape (batch, pos, length)
batch, pos, n_heads, d_head = x.shape
x_normed = norm_module(x.reshape(batch * pos, n_heads, d_head))
return x_normed.reshape(batch, pos, n_heads, d_head)

def calculate_qkv_matrices(
self,
Expand Down
5 changes: 3 additions & 2 deletions transformer_lens/components/grouped_query_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ def _apply_qk_norm(
Returns:
Normalized tensor with same shape as input
"""
# Reshape from [batch, pos, head_index, d_head] to [batch * pos * head_index, d_head]
# Reshape from [batch, pos, head_index, d_head] to [batch * pos, head_index, d_head]
# so it matches RMSNorm's expected 3D input shape (batch, pos, length)
batch, pos, n_heads, d_head = x.shape
x_reshaped = x.reshape(-1, d_head)
x_reshaped = x.reshape(batch * pos, n_heads, d_head)
x_normed = norm_module(x_reshaped)
return x_normed.reshape(batch, pos, n_heads, d_head)
Loading