feat: optional torchembed fused Triton RoPE kernel (~3-4x training speedup)#2280
Open
py-ai-dev wants to merge 2 commits into
Open
feat: optional torchembed fused Triton RoPE kernel (~3-4x training speedup)#2280py-ai-dev wants to merge 2 commits into
py-ai-dev wants to merge 2 commits into
Conversation
…ope) Adds an optional `use_torchembed_rope: bool = False` field to `Config`. When enabled, `CausalSelfAttention` delegates the RoPE application to `torchembed.positional.RotaryEmbedding(use_fused=True)`, which runs a fused Triton kernel and delivers ~3–4x wall-clock speedup on CUDA GPUs (measured on NVIDIA GB10, bfloat16, batch=4, n_heads=32, d_qk=128). Changes ------- * litgpt/config.py – add `use_torchembed_rope` field (default False) * litgpt/model.py – lazy import of torchembed; create one `TorchembedRotaryEmbedding` per attention block when enabled; forward uses it during training (input_pos is None) and falls back to the existing apply_rope path during KV-cache inference * pyproject.toml – add `optional-dependencies.torchembed` group * benchmarks/bench_torchembed_rope.py – standalone timing script Compatibility ------------- * Default behaviour is unchanged (use_torchembed_rope=False) * Graceful ImportError if torchembed is not installed * Raises ValueError for incompatible flags (rope_interleave, rope_adjustments) * KV-cache inference falls back automatically to the standard path Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
py-ai-dev
requested review from
andyland,
k223kim,
lianakoleva and
t-vi
as code owners
July 6, 2026 11:34
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an opt-in
use_torchembed_rope: bool = Falseconfig flag that routes RoPE application throughtorchembed's fused Triton kernel instead of the plain-PyTorchapply_ropepath. All existing behaviour is unchanged by default.Benchmark
Hardware: NVIDIA GB10, bfloat16, batch=4, n_heads=32, d_qk=128
Reproduce with:
Changes
litgpt/config.pyuse_torchembed_rope: bool = Falsealongside existingrope_*fieldslitgpt/model.pyCausalSelfAttention.__init__creates oneTorchembedRotaryEmbeddingper block when enabled; forward uses it for the training path, falls back toapply_ropeduring KV-cache inferencepyproject.tomloptional-dependencies.torchembedgroup (torchembed>=0.3.1)benchmarks/bench_torchembed_rope.pyUsage
Install the extra dependency:
Compatibility / Safety
ImportErrorat model construction time if torchembed is not installed; never silently degrades.apply_ropepath wheninput_pos is not None, so generation is always correct.ValueErrorwith a descriptive message if combined withrope_interleave=Trueorrope_adjustments(YaRN / Llama3 scaling), which have different cos/sin layouts that the torchembed kernel does not match.Testing
🤖 Generated with Claude Code