Add rope_pairwise config flag to optimize YaRN RoPE - #4627
Conversation
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
3bd0253 to
05da824
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
3b47905 to
61a5e35
Compare
|
🤖 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. |
|
🤖 I'm sorry @NuojCheng, but I was unable to process your request. Please see the logs for more details. |
1 similar comment
|
🤖 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
61a5e35 to
fc5a91e
Compare
|
🤖 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. |
|
🤖 I'm sorry @NuojCheng, but I was unable to process your request. Please see the logs for more details. |
NuojCheng
left a comment
There was a problem hiding this comment.
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
|
🤖 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. |
There was a problem hiding this comment.
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_pairwiserequiringrope_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 |
There was a problem hiding this comment.
| # 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) |
There was a problem hiding this comment.
| sign = jnp.asarray([-1.0, 1.0], dtype=jnp.float32) | |
| sign = jnp.array([-1.0, 1.0], dtype=cos.dtype) |
| ) | ||
|
|
||
| @model_validator(mode="after") | ||
| def validate_rope_pairwise(self) -> "Rope": |
There was a problem hiding this comment.
| 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.") |
There was a problem hiding this comment.
| raise ValueError("rope_pairwise=True requires rope_interleave=True.") | |
| raise ValueError("pairwise=True requires interleave=True.") |
RissyRan
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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]]]]) |
There was a problem hiding this comment.
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,
)
Description
Add
rope_pairwiseconfig flag to optimize YaRN RoPE computation inYarnRotaryEmbedding.When enabled (
rope_pairwise: true), it uses a pairwise interleaved implementation of RoPE inYarnRotaryEmbeddingwhich keeps the rank-5 pair tensor intact, avoiding some data formatting during MLA.Tests
Added
test_pairwise_calltoYarnRotaryEmbeddingTestintests/unit/embeddings_test.py.Added
test_pairwise_requires_interleavevalidation test.https://xmanager.corp.google.com/experiments/271579010
Before: https://xprof.corp.google.com/trace_viewer/chengnuojin-11765148423170897263?view_start=1400.245&view_end=1420.311
After: https://xprof.corp.google.com/trace_viewer/dandragona-1224952786495301116?view_start=2691.354&view_end=2701.054
After: https://xprof.corp.google.com/trace_viewer/dandragona-8379284804318708435?view_start=109105.685&view_end=109109.290 (isolation mircrobench)
Checklist