Skip to content

feat: optional torchembed fused Triton RoPE kernel (~3-4x training speedup)#2280

Open
py-ai-dev wants to merge 2 commits into
Lightning-AI:mainfrom
py-ai-dev:feat/torchembed-fused-rope
Open

feat: optional torchembed fused Triton RoPE kernel (~3-4x training speedup)#2280
py-ai-dev wants to merge 2 commits into
Lightning-AI:mainfrom
py-ai-dev:feat/torchembed-fused-rope

Conversation

@py-ai-dev

Copy link
Copy Markdown

Summary

Adds an opt-in use_torchembed_rope: bool = False config flag that routes RoPE application through torchembed's fused Triton kernel instead of the plain-PyTorch apply_rope path. All existing behaviour is unchanged by default.

Benchmark

Hardware: NVIDIA GB10, bfloat16, batch=4, n_heads=32, d_qk=128

seq_len litgpt (ms) torchembed (ms) speedup
512 0.41 0.11 3.73x
1024 0.79 0.21 3.76x
2048 1.56 0.41 3.80x
4096 3.10 0.80 3.88x
8192 6.21 1.55 4.01x

Reproduce with:

pip install torchembed
python benchmarks/bench_torchembed_rope.py

Changes

File What changed
litgpt/config.py Add use_torchembed_rope: bool = False alongside existing rope_* fields
litgpt/model.py Lazy import of torchembed; CausalSelfAttention.__init__ creates one TorchembedRotaryEmbedding per block when enabled; forward uses it for the training path, falls back to apply_rope during KV-cache inference
pyproject.toml New optional-dependencies.torchembed group (torchembed>=0.3.1)
benchmarks/bench_torchembed_rope.py Standalone timing script

Usage

from litgpt.config import Config
from litgpt.model import GPT

config = Config.from_name("Llama-3.2-1B", use_torchembed_rope=True)
model = GPT(config)

Install the extra dependency:

pip install "litgpt[torchembed]"
# or
pip install torchembed

Compatibility / Safety

  • Default off — existing training runs, checkpoints, and configs are unaffected.
  • Graceful fallback — raises a clear ImportError at model construction time if torchembed is not installed; never silently degrades.
  • KV-cache inference — automatically falls back to the standard apply_rope path when input_pos is not None, so generation is always correct.
  • Guarded flags — raises ValueError with a descriptive message if combined with rope_interleave=True or rope_adjustments (YaRN / Llama3 scaling), which have different cos/sin layouts that the torchembed kernel does not match.
  • No new required dependency — torchembed stays in an optional extras group.

Testing

# Smoke-test the forward pass
python -c "
import torch
from litgpt.config import Config
from litgpt.model import GPT

c = Config(vocab_size=256, n_layer=2, n_head=4, n_embd=128,
           use_torchembed_rope=True, block_size=64)
out = GPT(c)(torch.randint(0, 256, (2, 32)))
print('OK', out.shape)
"

🤖 Generated with Claude Code

…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>
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