Skip to content

Support batched input_positions in nnx.RoPE - #5659

Open
Bndrwe wants to merge 1 commit into
google:mainfrom
Bndrwe:fix-rope-batched-positions
Open

Bndrwe wants to merge 1 commit into
google:mainfrom
Bndrwe:fix-rope-batched-positions

Conversation

@Bndrwe

@Bndrwe Bndrwe commented Nov 21, 2026

Copy link
Copy Markdown

What does this PR do?

nnx.RoPE fails when input_positions has a batch dimension. RoPE._rotation_for builds the angle table with jnp.outer(input_positions, freqs), and jnp.outer flattens its inputs, so positions of shape (batch, seq_len) give a (batch * seq_len, dim // 2) table that can't broadcast against x:

TypeError: mul got incompatible shapes for broadcasting: (2, 5, 8), (1, 10, 8).

The docstrings say batched positions work ((..., seq_len) on RoPE.__call__, [batch_sizes..., length] on MultiHeadAttention.__call__), and left-padded or packed batches need them. The fix multiplies the positions against the frequencies with broadcasting, so the table comes out as (..., seq_len, dim // 2). 1-D positions behave as before.

This doesn't overlap with #5537, which fixes vmap axes in dot_product_attention_with_rope and leaves jnp.outer alone. With #5537 by itself, MultiHeadAttention still crashes on (batch, seq_len) positions. With both changes it works, and this patch applies cleanly on top of #5537.

I added TestRoPE.test_batched_input_positions, which checks batched RoPE against RoPE applied to each example separately, using three position rows (one left-padded). It fails on main and passes with the fix. pytest tests/nnx/nn/attention_test.py: 128 passed.

Checklist

  • This PR fixes a minor issue (e.g.: typo or small bug) or improves the docs (you can dismiss the other checks if that's the case).
  • This change is discussed in a Github issue/discussion (please add a link).
  • The documentation and docstrings adhere to the documentation guidelines.
  • This change includes necessary high-coverage tests. (No quality testing = no merge!)

RoPE._rotation_for used jnp.outer, which flattens its inputs, so
positions of shape (batch, seq_len) -- documented as (..., seq_len) --
produced a (batch * seq_len, dim // 2) table and failed to broadcast
against x. Broadcast the positions against the frequencies instead.
@google-cla

google-cla Bot commented Nov 21, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@Bndrwe

Bndrwe commented Nov 21, 2026

Copy link
Copy Markdown
Author

I've signed the Google CLA.

@Bndrwe

Bndrwe commented Nov 21, 2026

Copy link
Copy Markdown
Author

Linked my GitHub account to the CLA, so the check should pass now.

@Bndrwe Bndrwe closed this Nov 21, 2026
@Bndrwe Bndrwe reopened this Nov 21, 2026

This branch has not been deployed

No deployments
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