Skip to content

[Perf][Compile] Add local compilation for cos/sin RoPE - #4985

Merged
acisseJZhong merged 3 commits into
mainfrom
compile_rope
Oct 1, 2026
Merged

acisseJZhong merged 3 commits into
mainfrom
compile_rope

Conversation

@acisseJZhong

@acisseJZhong acisseJZhong commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Compile CosSinRoPE.forward as the default cos_sin_rope local compile region.
  • Cover Qwen3.5/Qwen3.8 MRoPE through its inherited CosSinRoPE.forward.
  • Keep ComplexRoPE eager because Inductor does not efficiently lower complex operators.
  • Keep both Helion overrides because the faster cos/sin implementation depends on shape and Helion is substantially faster for complex RoPE.

This PR is stacked on #4931.

Performance

One NVIDIA H100 with BF16 inputs. Forward+backward includes query and key gradients. Each result is the median of five trials with 20 warmup iterations and 100 timed iterations per trial. Compilation time is excluded.

Shape is [T, Hq, Hkv, D], where T is the rank-local token count after packing and Hq/Hkv are rank-local heads after TP. A speedup above 1.0x means local compile is faster.

CosSinRoPE

The exact Helion cos/sin override applies to these models.

Workload Shape Eager Helion Local compile Speedup vs eager Speedup vs Helion
Qwen3 0.6B RL TP2 [4096, 8, 4, 128] 409.27 us 472.09 us 250.63 us 1.63x 1.88x
Qwen3 8B SFT TP1 [40960, 32, 8, 128] 12.92 ms 854.86 us 901.50 us 14.33x 0.95x
Qwen3 14B TP1 [163840, 40, 8, 128] 61.26 ms 3.95 ms 4.34 ms 14.12x 0.91x
Qwen3 30B-A3B RL TP2 [4096, 16, 2, 128] 634.79 us 503.01 us 295.81 us 2.15x 1.70x
Qwen3 32B TP1 [81920, 64, 8, 128] 45.93 ms 2.95 ms 3.21 ms 14.33x 0.92x
GPT-OSS 20B RL TP2 [4096, 32, 4, 64] 632.31 us 458.05 us 265.19 us 2.38x 1.73x

Local compile is faster than eager for all tested shapes and faster than Helion for three of six. Helion remains 1.05x, 1.10x, and 1.09x faster for the Qwen3 8B, 14B, and 32B shapes, respectively.

MRoPE

MRoPE inherits the locally compiled CosSinRoPE.forward and specializes cache construction for three-axis positions. The Helion override targets exactly CosSinRoPE.Config, so it does not replace MRoPE.Config.

Workload Positions Shape Eager Local compile Speedup vs eager
Qwen3.5 9B RL TP2 text [T] [2048, 8, 2, 64] 413.46 us 180.22 us 2.29x
Qwen3.5 9B TP2 three-axis [T, 3] [2048, 8, 2, 64] 670.32 us 226.58 us 2.96x
Qwen3.5 27B RL TP2 text [T] [65536, 12, 2, 64] 3.78 ms 299.47 us 12.62x
Qwen3.5 27B TP2 three-axis [T, 3] [65536, 12, 2, 64] 4.02 ms 368.85 us 10.89x
Qwen3.8 2.4T-A95B TP4 text [T] [262144, 16, 1, 64] 17.74 ms 1.35 ms 13.12x
Qwen3.8 27B TP4 three-axis [T, 3] [1048576, 6, 1, 64] 32.59 ms 4.27 ms 7.63x

Local compile is faster than eager for all tested MRoPE text and three-axis shapes.

Complex RoPE exclusion

DeepSeek V3 recipes use four packed 16K sequences per DP rank, so T = 65536. The 16B model has 16 query heads; the 236B and 671B models have 128. All use one MLA RoPE key head and D = 64.

Workload Shape Eager Helion Local compile Speedup vs eager Speedup vs Helion
DeepSeek V3 16B [65536, 16, 1, 64] 1.50 ms 345.38 us 1.40 ms 1.07x 0.25x
DeepSeek V3 236B/671B [65536, 128, 1, 64] 10.47 ms 2.17 ms 9.94 ms 1.05x 0.22x

Inductor warns that complex-operator code generation is unsupported. Exploratory local compile is only 1.05-1.07x faster than eager, while Helion is 4.06-4.57x faster on the DeepSeek V3 shapes. Therefore this PR leaves ComplexRoPE eager and retains the Helion override.

These are isolated operator measurements, not end-to-end training results.

Correctness

  • Cos/sin RoPE and Qwen3.5/Qwen3.8 MRoPE compiled forward and backward are bitwise identical to eager.
  • Cos/sin RoPE compiled forward and backward is batch invariant.

Tests

pytest -q tests/unit_tests/cpu/test_compile_config.py tests/unit_tests/cpu/test_offset_rmsnorm.py tests/unit_tests/cpu/test_rope.py tests/unit_tests/gpu/test_offset_rmsnorm.py tests/unit_tests/gpu/test_swiglu.py tests/unit_tests/gpu/test_rope_compile.py tests/unit_tests/gpu/test_helion_rope.py
63 passed, 5 subtests passed

pyrefly check <changed files> --summarize-errors
No errors found

@acisseJZhong
acisseJZhong added this pull request to stack #4896 October 1, 2026 00:28
@pytorch-bot pytorch-bot Bot added the ciflow/rl label Oct 1, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 1, 2026
@acisseJZhong
acisseJZhong force-pushed the compile_rope branch 2 times, most recently from 10e25f5 to 9009cf1 Compare October 1, 2026 01:08
@acisseJZhong acisseJZhong changed the title [Perf][Compile] Add local compilation for cos/sin RoPE [Perf][Compile] Add local compilation for RoPE Oct 1, 2026
@acisseJZhong
acisseJZhong force-pushed the compile_rope branch 2 times, most recently from 3849a5c to c126d60 Compare October 1, 2026 03:31
@acisseJZhong acisseJZhong changed the title [Perf][Compile] Add local compilation for RoPE [Perf][Compile] Add local compilation for cos/sin RoPE Oct 1, 2026
@acisseJZhong
acisseJZhong marked this pull request as ready for review October 1, 2026 03:34
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 1, 2026
Comment on lines 47 to +51
def qwen35_local_compile_config() -> LocalCompileConfig:
"""Return the default local compile regions for Qwen3.5 models."""
return LocalCompileConfig(regions=["gated_rmsnorm", "loss", "offset_rmsnorm"])
return LocalCompileConfig(
regions=["gated_rmsnorm", "loss", "cos_sin_rope", "offset_rmsnorm"]
)

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.

noob question: why do we need this custom fn? And why only for qwen 3.5?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

because offset_rmsnorm is sth only qwen3.5 uses, so the common import won't cover this.

@pytorch-bot

pytorch-bot Bot commented Oct 1, 2026

Copy link
Copy Markdown

Warning: Unknown label ciflow/rocm-mi300.
Currently recognized labels are

  • ciflow/b200
  • ciflow/docker
  • ciflow/fake-pg
  • ciflow/h100.8
  • ciflow/real-pg
  • ciflow/rl
  • ciflow/rocm

Please add the new label to .github/pytorch-probot.yml

@acisseJZhong
acisseJZhong merged commit 6ea2321 into main Oct 1, 2026
acisseJZhong added a commit that referenced this pull request Oct 2, 2026
## Summary

- Add function-scoped local compilation for the Kimi K3 SiTUGLU
activation.
- Enable the situglu local-compile target by default.
- Cover dense execution and the grouped-expert call path that passes
offsets.
- Stack on #4985.

## Performance

Steady-state BF16 operator latency on one H100 with Kimi K3 beta=4 and
linear_beta=25. Compilation time is excluded. Results are the average of
two independent run medians; each run used five measurements after
warmup.

Shapes use the released Kimi K3 hidden dimensions with representative
TP8/EP8 layouts. Routed-expert rows assume balanced top-k=16 routing
across EP8.

| Path | Rank-local shape | Eager fwd | Local compile fwd | Speedup |
Eager fwd+bwd | Local compile fwd+bwd | Speedup |
| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: |
| Dense, 4K tokens, TP8 | [512, 4224] | 79.65 us | 13.07 us | 6.09x |
189.29 us | 76.17 us | 2.49x |
| Dense, 32K tokens, TP8 | [4096, 4224] | 850.16 us | 54.15 us | 15.70x
| 2.074 ms | 137.29 us | 15.11x |
| Shared experts, 4K tokens, TP8 | [512, 768] | 43.63 us | 7.45 us |
5.85x | 188.14 us | 76.03 us | 2.47x |
| Shared experts, 32K tokens, TP8 | [4096, 768] | 120.50 us | 16.40 us |
7.35x | 295.94 us | 77.56 us | 3.82x |
| Routed experts, 4K tokens, EP8 | [8192, 3072] | 1.208 ms | 86.69 us |
13.93x | 2.943 ms | 205.78 us | 14.30x |
| Routed experts, 32K tokens, EP8 | [65536, 3072] | 9.204 ms | 621.20 us
| 14.82x | 22.555 ms | 1.561 ms | 14.45x |

Local compile is faster for every tested shape. These isolated operator
results do not claim the same end-to-end training speedup. The routed
path still evaluates the full padded buffer because SiTUGLU does not
consume offsets.

## Testing

- pytest -q tests/unit_tests/cpu/test_compile_config.py
tests/unit_tests/cpu/test_feed_forward.py
tests/unit_tests/cpu/test_moe.py (32 passed, 9 subtests passed)
- pytest -q tests/unit_tests/gpu/test_situglu.py (3 passed)
- Verified compiled forward and backward emit Triton code.
- Verified forward, gate gradient, and up gradient match the eager
reference.
- Verified compiled forward and both input gradients are bitwise
batch-invariant.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/b200 Trigger B200 CI ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/h100.8 Trigger H100.8 CI ciflow/real-pg Run the full 8-GPU Real PG integration suite ciflow/rl ciflow/rocm ciflow/rocm-mi300 CLA Signed This label is managed by the Meta Open Source bot. module: rocm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants