Skip to content

Fix QK norm reshape to match RMSNorm's expected 3D input shape#1221

Open
brendanlong wants to merge 1 commit intoTransformerLensOrg:devfrom
brendanlong:brendanlong/gemma3-attention-shape
Open

Fix QK norm reshape to match RMSNorm's expected 3D input shape#1221
brendanlong wants to merge 1 commit intoTransformerLensOrg:devfrom
brendanlong:brendanlong/gemma3-attention-shape

Conversation

@brendanlong
Copy link
Copy Markdown
Contributor

Description

RMSNorm.forward() has a jaxtyping hint expecting a 3D tensor (batch, pos, length), but _apply_qk_norm was reshaping to 2D (batchposheads, d_head), causing a BeartypeCallHintParamViolation on Gemma 3 models. Reshape to 3D (batch*pos, heads, d_head) instead — normalization is identical since RMSNorm only operates on the last dimension.

This is a breaking change because it changes what gets passed to hooks. I'm unsure if we want to fix this or change the type hint, but this PR fixes the output to match the type hint.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

Note: I have new tests I'm working on in brendanlong#8 that failed due to this

RMSNorm.forward() has a jaxtyping hint expecting a 3D tensor (batch,
pos, length), but _apply_qk_norm was reshaping to 2D (batch*pos*heads,
d_head), causing a BeartypeCallHintParamViolation on Gemma 3 models.
Reshape to 3D (batch*pos, heads, d_head) instead — normalization is
identical since RMSNorm only operates on the last dimension.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jlarson4 jlarson4 changed the base branch from main to dev March 31, 2026 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant