Skip to content

Fuse DSv3 MTP cross-entropy forward/backward - #5261

Draft
ydwu4 wants to merge 4 commits into
gh/ydwu4/26/basefrom
gh/ydwu4/26/head
Draft

ydwu4 wants to merge 4 commits into
gh/ydwu4/26/basefrom
gh/ydwu4/26/head

Conversation

@ydwu4

@ydwu4 ydwu4 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Fuse log-softmax/NLL reduction and its gradient with compact saved state.
Preserve native MTP weighting, chunking, normalization and TP fallback.

Keep Gluon kernels, custom operators, autograd and override in one file,
with one CPU test file and one GPU test file.

Recorded GB300 local CE F+B: 3.734 -> 0.554 ms. Bitwise checks pass;
forward reaches 68.1% of the measured roof, so model dispatch stays off.

[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 10, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 10, 2026
@ydwu4
ydwu4 marked this pull request as draft October 10, 2026 04:03
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 10, 2026
[ghstack-poisoned]
ydwu4 added a commit that referenced this pull request Oct 10, 2026
Replace the raw CE callback through the existing recipe override pattern.
Keep native MTP weighting, per-depth normalization, chunking and TP fallback.
An explicit autograd Function saves BF16 logits plus compact row statistics;
two forward kernels and one backward kernel preserve native rounding.

Port the node 747954 handoff to the current Torch/Triton runtime, including
FakeTensor contracts, SPMD types, compilation guards and production-shape
bitwise tests. Keep model dispatch disabled until the roofline gate passes.

53 CE tests and 219 stack tests plus 24 subtests pass. Fresh local CE F+B
falls from 3.734 ms to 0.554 ms, but forward reaches only 68.1% of the
independently measured roof. Keep calibration and benchmarks external.


ghstack-source-id: f90ce6d
Pull-Request: #5261
[ghstack-poisoned]
ydwu4 added a commit that referenced this pull request Oct 10, 2026
Replace the raw CE callback through the existing recipe override pattern.
Keep native MTP weighting, per-depth normalization, chunking and TP fallback.
An explicit autograd Function saves BF16 logits plus compact row statistics;
two forward kernels and one backward kernel preserve native rounding.

Port the node 747954 handoff to the current Torch/Triton runtime, including
FakeTensor contracts, SPMD types, compilation guards and production-shape
bitwise tests. Keep model dispatch disabled until the roofline gate passes.

53 CE tests and 224 stack tests plus 30 subtests pass. Fresh local CE F+B
falls from 3.734 ms to 0.554 ms, but forward reaches only 68.1% of the
independently measured roof. Keep calibration and benchmarks external.


ghstack-source-id: 946298f
Pull-Request: #5261
ydwu4 added a commit that referenced this pull request Oct 10, 2026
Replace the raw CE callback through the existing recipe override pattern.
Keep native MTP weighting, per-depth normalization, chunking and TP fallback.
An explicit autograd Function saves BF16 logits plus compact row statistics;
two forward kernels and one backward kernel preserve native rounding.

Port the node 747954 handoff to the current Torch/Triton runtime, including
FakeTensor contracts, SPMD types, compilation guards and production-shape
bitwise tests. Keep model dispatch disabled until the roofline gate passes.

53 CE tests and 224 stack tests plus 30 subtests pass. Fresh local CE F+B
falls from 3.734 ms to 0.554 ms, but forward reaches only 68.1% of the
independently measured roof. Keep calibration and benchmarks external.


ghstack-source-id: 946298f
Pull-Request: #5261
[ghstack-poisoned]
@ydwu4 ydwu4 changed the title Add an experimental DeepSeek V3 MTP cross-entropy override Fuse DSv3 MTP cross-entropy forward/backward Oct 10, 2026
sanketpurandare added a commit that referenced this pull request Oct 20, 2026
## Agent details (AI-generated)

Adapt Yidi Wu's (@ydwu4) implementation from #5261 at
1acef59, preserving copyright/license and
credit for the original kernel design. Extend the override to positive token
and full-vocabulary sizes instead of restricting it to a benchmark shape.
Preserve native warp/register/global softmax reduction order, BF16 forward
and backward rounding, ignored labels, and device bounds assertions.

Only raw cross entropy is fused. Native MTP alignment, independent objective
denominators, coefficient/mean composition, and detached metrics remain
unchanged. Save two FP32 row statistics (32 KiB at 4096 tokens), not FP32
vocabulary activations. Retain proven 32-bit indexing when T*V < 2^31 and
widen larger addresses. Keep native TP/unsupported-layout behavior unless
fusion is required. Do not add owned outputs or a competing loss-result API.

Production kernels retain their existing hot instruction path. General shapes
use actual fused kernel variants, not eager fallback. Original-author branches
are untouched. GraphTrainer is outside scope.

## Test Plan (AI-generated)

python -m pytest tests/unit_tests/cpu/test_fused_dsv3_mtp_loss.py tests/unit_tests/gpu/test_fused_dsv3_mtp_loss.py -q

75 tests passed on the current local change, including byte-exact general and
production loss/gradient checks, metadata/error paths, invalid-label isolation,
compile and CUDA graph replay. Current-main stack rebase, final matched
performance/memory checks, distributed gates, lint/docs, and refreshed review
audit remain pending; these results are not a landing or convergence claim.
sanketpurandare added a commit that referenced this pull request Oct 23, 2026
## Agent details (AI-generated)

Adapt Yidi Wu's (@ydwu4) implementation from #5261 at
1acef59, preserving copyright/license and
credit for the original kernel design. Extend the override to positive token
and full-vocabulary sizes instead of restricting it to a benchmark shape.
Preserve native warp/register/global softmax reduction order, BF16 forward
and backward rounding, ignored labels, and device bounds assertions.

Only raw cross entropy is fused. Native MTP alignment, independent objective
denominators, coefficient/mean composition, and detached metrics remain
unchanged. Save two FP32 row statistics (32 KiB at 4096 tokens), not FP32
vocabulary activations. Retain proven 32-bit indexing when T*V < 2^31 and
widen larger addresses. Keep native TP/unsupported-layout behavior unless
fusion is required. Do not add owned outputs or a competing loss-result API.

Production kernels retain their existing hot instruction path. General shapes
use actual fused kernel variants, not eager fallback. Original-author branches
are untouched. GraphTrainer is outside scope.

## Test Plan (AI-generated)

python -m pytest tests/unit_tests/cpu/test_fused_dsv3_mtp_loss.py tests/unit_tests/gpu/test_fused_dsv3_mtp_loss.py -q

75 tests passed on the current local change, including byte-exact general and
production loss/gradient checks, metadata/error paths, invalid-label isolation,
compile and CUDA graph replay. Current-main stack rebase, final matched
performance/memory checks, distributed gates, lint/docs, and refreshed review
audit remain pending; these results are not a landing or convergence claim.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant