Skip to content

Add rope_pairwise config flag to optimize YaRN RoPE - #4627

Open
dandragona wants to merge 7 commits into
mainfrom
rope-pairwise-opt
Open

Add rope_pairwise config flag to optimize YaRN RoPE#4627
dandragona wants to merge 7 commits into
mainfrom
rope-pairwise-opt

Conversation

@dandragona

@dandragona dandragona commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

Description

Add rope_pairwise config flag to optimize YaRN RoPE computation in YarnRotaryEmbedding.

When enabled (rope_pairwise: true), it uses a pairwise interleaved implementation of RoPE in YarnRotaryEmbedding which keeps the rank-5 pair tensor intact, avoiding some data formatting during MLA.

Tests

Checklist

  • I have performed a self-review of my code.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests or unit tests.

@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@codecov

codecov Bot commented Jul 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@dandragona
dandragona force-pushed the rope-pairwise-opt branch 4 times, most recently from 3b47905 to 61a5e35 Compare July 28, 2026 16:20
@github-actions

Copy link
Copy Markdown
Contributor

🤖 Hi @NuojCheng, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions

Copy link
Copy Markdown
Contributor

🤖 I'm sorry @NuojCheng, but I was unable to process your request. Please see the logs for more details.

1 similar comment
@github-actions

Copy link
Copy Markdown
Contributor

🤖 I'm sorry @NuojCheng, but I was unable to process your request. Please see the logs for more details.

…f YarnRotaryEmbedding. This addresses the Codecov patch coverage warnings.

CHANGE_STEWARD=true
Add documentation for the new rope_pairwise optimization flag in the DeepSeek runner guide.

TAG=agy
CONV=a262e292-a0d4-49f9-ad26-f42e697a5979
CHANGE_STEWARD=true
The test was incorrectly expecting concatenated output for pairwise=True, but YarnRotaryEmbedding with pairwise=True returns interleaved output. Update expectation to match interleaved layout.

TAG=agy
CONV=a0af6c58-a54e-4ea7-8d04-fac29de8aa41
CHANGE_STEWARD=true
@github-actions

Copy link
Copy Markdown
Contributor

🤖 Hi @NuojCheng, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions

Copy link
Copy Markdown
Contributor

🤖 I'm sorry @NuojCheng, but I was unable to process your request. Please see the logs for more details.

Comment thread src/maxtext/configs/types.py Outdated
Comment thread tests/unit/embeddings_test.py

@NuojCheng NuojCheng left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a performance comparison, e.g. before/after, in the PR description?

@dandragona

Copy link
Copy Markdown
Collaborator Author

Could you add a performance comparison, e.g. before/after, in the PR description?

Done. Also updated for #4630

…ing in pairwise RoPE

Addresses GitHub review comment #discussion_r3684287727 by:
- Explicitly passing interleave=True in test_pairwise_call
- Adding test_pairwise_explicit_shard_mode_call for interleave=True, pairwise=True, shard_mode=EXPLICIT
- Annotating broadcasted cos/sin with 5D explicit sharding in rope_pairwise when shard_mode=EXPLICIT

CHANGE_STEWARD=true
CONV=fd71583d-3a4c-418b-a131-d086fa8e5c9d
CHANGE_STEWARD=true
CONV=fd71583d-3a4c-418b-a131-d086fa8e5c9d
Addresses GitHub review comment #discussion_r3684262252 by updating the return type annotation from "Rope" to "YarnRope".

CHANGE_STEWARD=true
CONV=70d61320-1190-44c5-a1c8-f64aadb81397
@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

🤖 Hi @RissyRan, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

## 📋 Review Summary

The Pull Request introduces the rope_pairwise configuration flag, which optimizes YaRN RoPE computations in YarnRotaryEmbedding by implementing a highly efficient, real-valued pairwise interleaved layout. This avoids complex number creation and redundant tensor reshaping, resulting in significant performance improvements. The overall design is sound, mathematically correct, and includes excellent unit test coverage for various edge cases.

🔍 General Feedback

  • Performance Optimization: Avoiding the creation of complex numbers and keeping the rank-5 pair tensor intact is a great performance enhancement for MLA computations.
  • Robust Verification: The unit tests comprehensively cover the new path, including checking validation limits (e.g., rope_pairwise requiring rope_interleave) and explicit sharding mode behaviors.
  • API Consistency: Suggesting a minor adjustment to the initialization error message to match the class parameters rather than configuration flags.

outputs = layer(inputs, position=position)
self.assertEqual(outputs.shape, (1, 2, 1, 4))

# Output with pairwise=True should match default interleaved YaRN RoPE

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 The comment states that the output with `pairwise=True` should match "default interleaved YaRN RoPE". However, the default implementation actually returns a concatenated layout (as tested in `test_basic_call`), whereas `pairwise=True` returns an interleaved layout. This is why the expected outputs have their second and third elements swapped. Clarifying this in the comment will help prevent future developer confusion.
Suggested change
# Output with pairwise=True should match default interleaved YaRN RoPE
# Output with pairwise=True matches default YaRN RoPE but with interleaved output layout [real0, imag0, real1, imag1]

cos = jnp.real(freqs)[..., jnp.newaxis]
sin = jnp.imag(freqs)[..., jnp.newaxis]
swapped = jnp.flip(pairs, axis=-1)
sign = jnp.asarray([-1.0, 1.0], dtype=jnp.float32)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 For constructing fixed literal arrays in JAX, `jnp.array` is more conventional and concise than `jnp.asarray`. Additionally, matching the dtype dynamically with `cos.dtype` is more robust and prevents hardcoded dtype mismatches if precision changes in the future.
Suggested change
sign = jnp.asarray([-1.0, 1.0], dtype=jnp.float32)
sign = jnp.array([-1.0, 1.0], dtype=cos.dtype)

Comment thread src/maxtext/configs/types.py Outdated
)

@model_validator(mode="after")
def validate_rope_pairwise(self) -> "Rope":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 The return type of `validate_rope_pairwise` is annotated as `"Rope"`, but it actually returns `self` (an instance of `YarnRope`). Since `YarnRope` and `Rope` are distinct configuration classes, this annotation is incorrect and can trigger static analysis/type checking errors.
Suggested change
def validate_rope_pairwise(self) -> "Rope":
def validate_rope_pairwise(self) -> "YarnRope":

self.pairwise = pairwise

if self.pairwise and not self.interleave:
raise ValueError("rope_pairwise=True requires rope_interleave=True.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 The parameters of the `YarnRotaryEmbedding` class are named `pairwise` and `interleave`. Raising an exception referencing the config flag names (`rope_pairwise` and `rope_interleave`) is confusing for developers using the Python API directly.
Suggested change
raise ValueError("rope_pairwise=True requires rope_interleave=True.")
raise ValueError("pairwise=True requires interleave=True.")

@RissyRan RissyRan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, minor comments. Thanks!

rope_interleave: true # RoPE with sin/cos interleaved vs concatenated
rope_truncate: true # Floor lower bound and ceil upper bound for correction range
rope_attention_scaling: false # Scale the rotary embedding output
rope_pairwise: false # Keep rank-5 pair tensor intact and return interleaved RoPE

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we make a comment to explain why is rank-5?

i.e. tensor shape with [Batch size, Sequence length, Attention heads, half_dim, 2]

self.assertEqual(outputs.shape, (1, 2, 1, 4))

# Output with pairwise=True should match default interleaved YaRN RoPE
expected = jnp.array([[[[1.0, 1.0, 1.0, 1.0]], [[-0.300781, 1.38281, 0.996094, 1.00781]]]])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we compare with default implementation if possible, i.e. with pairwise=False? Similar comments for other tests if applies.

embeddings.YarnRotaryEmbedding(
        embedding_dims=4,
        mesh=self.mesh,
        max_position_embeddings=16384,
        original_max_position_embeddings=4096,
        interleave=True,
        pairwise=False,
        rngs=self.rngs,
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants