Add fused DeepSeek-V4 partial interleaved RoPE - #1387
Open
vedavyas2727 wants to merge 2 commits into
Open
Conversation
Fuse DeepSeek V4's trailing interleaved rotary embedding with a stride-aware Triton kernel and conjugate backward. Enable the model patch, add correctness and convergence coverage, and record reproducible operator benchmarks. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Preserve float64 prefix and trig semantics, and enable the fused RoPE path in all DeepSeek V4 convergence tests with explicit patch assertions. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
vedavyas2727
force-pushed
the
vedavyas2727-deepseek-v4-partial-rope
branch
from
August 16, 2026 04:57
e78dbf2 to
8ad916a
Compare
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
Add a fused Triton implementation of DeepSeek-V4's partial interleaved rotary position embedding and enable it by default in
apply_liger_kernel_to_deepseek_v4.The kernel matches the Transformers 5.14.1 contract: it rotates only the trailing RoPE slice in FP32, preserves the leading non-rotary channels, accepts the supported
unsqueeze_dimlayouts and arbitrary positive strides, returns a fresh contiguous tensor, and uses the conjugate transform for backward without saving the input tensor. This also adds the functional/public APIs, monkey-patch and revert coverage, focused correctness tests, a reproducible benchmark script, benchmark data, and the README support-table entry.Related context: #623. This PR is scoped to DeepSeek-V4 and does not claim to close the broader DeepSeek-V3 request.
Details
DeepSeek-V4 uses adjacent interleaved rotary pairs in only the trailing portion of each head, so the existing Liger Llama-style RoPE kernel is not compatible. The new multi-row kernel handles the production transposed/non-contiguous
[B, H, S, D]layout directly and writes contiguous output. It is used for query, shared-KV, and inverse-rotated attention-output calls through the model module'sapply_rotary_pos_embglobal.No
nn.Modulewrapper is added because the upstream surface is a functional model helper. Gradients forcosandsinare intentionally not produced, matching their use as generated positional buffers in DeepSeek-V4.Testing Done
make testto ensure correctness (targeted operator, patch, and convergence tests run instead)Correctness coverage includes fp16/bf16/fp32/fp64 forward and backward, contiguous and production non-contiguous layouts,
unsqueeze_dim=1/2, broadcast and per-batch trigonometric tensors, strided trigonometric tensors, negative sine, odd shapes, full and partial rotary dimensions, functional exports, patch/revert behavior, and instance patching.Commands/results:
pytest test/transformers/test_deepseek_v4_rope.py test/transformers/test_monkey_patch.py -k deepseek_v4 -q: 24 passedpytest test/transformers/test_monkey_patch.py -q: 62 passedpytest test/convergence/fp32/test_mini_models.py -k mini_deepseek_v4 -xvs: 1 passedpytest test/convergence/fp32/test_mini_models_with_logits.py -k mini_deepseek_v4 -xvs: 1 passedpytest test/convergence/bf16/test_mini_models.py -k mini_deepseek_v4 -xvs: 1 passedpytest test/convergence/bf16/test_mini_models_with_logits.py -k mini_deepseek_v4 -xvs: 1 passedAll four convergence suites run with
rope=Truefor DeepSeek-V4 and assertmodeling_deepseek_v4.apply_rotary_pos_emb is liger_deepseek_v4_rotary_pos_embduring the Liger path.Benchmark methodology
B=1,H=64,D=512, rotary dimensionR=64, bf16, exact transposed/non-contiguous[B,H,S,D]inputs. Speed usestriton.testing.do_benchwith repository quantiles[0.5, 0.2, 0.8]; single calls use 25 ms warmup / 100 ms measurement and the aggregate workload uses 10 ms / 40 ms. Memory is the repository-standard absolute CUDA allocator peak over five runs. Providers are Transformers eager,torch.compile(fullgraph=True), and Liger. Raw results are committed tobenchmark/data/all_benchmark_data.csv.Single-call full forward+backward median latency:
At
T=4096, forward / backward / full medians are:Aggregate DeepSeek-V4-style Q (
H=64) + shared KV (H=1) + inverse output (H=64) full forward+backward:At
T=4096, aggregate full peak memory is 2321 / 2353 / 2065 MB for HF eager /torch.compile/ Liger respectively.These are operator and three-call attention-block workload measurements, not an end-to-end DeepSeek-V4 training benchmark; this PR does not claim an end-to-end model speedup.