Skip to content

Add Megatron-Core integration for RoPE - #1325

Draft
PrathyushaPolepalli wants to merge 4 commits into
linkedin:mainfrom
PrathyushaPolepalli:megatron-rope-integration
Draft

Add Megatron-Core integration for RoPE#1325
PrathyushaPolepalli wants to merge 4 commits into
linkedin:mainfrom
PrathyushaPolepalli:megatron-rope-integration

Conversation

@PrathyushaPolepalli

@PrathyushaPolepalli PrathyushaPolepalli commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Adds Liger's Triton RoPE to Megatron-Core

apply_liger_kernel_to_megatron(rope=True) reroutes Megatron's apply_rotary_pos_emb dispatcher — rebinding the symbol on every module that imported it by value (e.g. megatron.core.transformer.attention) — to Liger's kernel for the standard unfused bshd path. Fused (TE/Apex) RoPE, packed thd sequences, interleaved rotation, multi-latent attention, mscale scaling and per-batch/mRoPE freqs transparently fall back to the native implementation so numerics never silently change.

Megatron applies RoPE once per tensor (query, then key) while Liger's kernel is fused q/k; LigerMegatronRopeFunction reuses the existing kernel by passing a one-head throwaway k (negligible extra work), avoiding a second Triton kernel.

  • src/liger_kernel/megatron/rope.py: adapter + single-tensor autograd op
  • monkey_patch.py: rope flag + _patch_apply_rotary_pos_emb (rebind-everywhere)
  • init.py: exports
  • test/megatron/test_rope.py: correctness vs native bshd reference
  • test/megatron/test_monkey_patch.py: RoPE patch-mechanism tests
  • benchmark/scripts/benchmark_megatron_rope.py: liger vs torch vs megatron
  • examples/megatron/README.md: rope=True docs

Summary

Testing Done

  • Hardware Type:
  • run make test to ensure correctness
  • run make checkstyle to ensure code style
  • run make test-convergence to ensure convergence

PrathyushaPolepalli and others added 4 commits July 26, 2026 18:09
Adds Liger's Triton RoPE to Megatron-Core, following the same one-line
monkey-patch pattern as the RMSNorm (linkedin#1254) and cross-entropy (linkedin#1207,
linkedin#1260) integrations.

apply_liger_kernel_to_megatron(rope=True) reroutes Megatron's
apply_rotary_pos_emb dispatcher — rebinding the symbol on every module
that imported it by value (e.g. megatron.core.transformer.attention) —
to Liger's kernel for the standard unfused bshd path. Fused (TE/Apex)
RoPE, packed thd sequences, interleaved rotation, multi-latent attention,
mscale scaling and per-batch/mRoPE freqs transparently fall back to the
native implementation so numerics never silently change.

Megatron applies RoPE once per tensor (query, then key) while Liger's
kernel is fused q/k; LigerMegatronRopeFunction reuses the existing kernel
by passing a one-head throwaway k (negligible extra work), avoiding a
second Triton kernel.

- src/liger_kernel/megatron/rope.py: adapter + single-tensor autograd op
- monkey_patch.py: rope flag + _patch_apply_rotary_pos_emb (rebind-everywhere)
- __init__.py: exports
- test/megatron/test_rope.py: correctness vs native bshd reference
- test/megatron/test_monkey_patch.py: RoPE patch-mechanism tests
- benchmark/scripts/benchmark_megatron_rope.py: liger vs torch vs megatron
- examples/megatron/README.md: rope=True docs

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Accept and preserve Megatron's current mla_rotary_interleaved argument so
MLA calls fall back to the native implementation instead of failing at the
patched wrapper. Detect older Megatron signatures before forwarding the new
argument to keep the integration backward compatible.

Add regression coverage for both API generations and update the Megatron
high-level API documentation for RoPE and vocab-parallel cross-entropy.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use SBHD for Megatron's actual tensor order and BHSD for Liger's input order.
Keep the public bshd-suffixed adapter name because it mirrors Megatron's
upstream helper, and document that distinction explicitly.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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