Skip to content

Fuse DSv3 sequence-wise loss forward/backward - #5259

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

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

Conversation

@ydwu4

@ydwu4 ydwu4 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Fuse expert frequencies, score normalization, raw-loss reduction and score
gradients. Preserve native loss injection, metrics, scaling and MoE layout.

Keep kernels, custom operators, autograd and override in one file, with
one CPU test file and one GPU test file. Compose with the router factory
without changing either module's ownership.

Recorded GB300 FP32 [4096,256], native -> CuTeDSL:
Fwd 49.19 -> 7.48 us; Bwd 32.45 -> 3.50 us; F+B 79.98 -> 10.20 us.
Bitwise checks pass; roofline acceptance remains open. Requires CuTeDSL 4.8+.

[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
ydwu4 added 2 commits October 9, 2026 22:37
[ghstack-poisoned]
[ghstack-poisoned]
@ydwu4 ydwu4 changed the title Fuse DSv3 sequence-wise load-balance loss forward/backward Fuse DSv3 sequence-wise loss forward/backward Oct 10, 2026
[ghstack-poisoned]
@acisseJZhong

Copy link
Copy Markdown
Contributor

Fwd 49.19 -> 7.48 us; Bwd 32.45 -> 3.50 us; F+B 79.98 -> 10.20 us.

was this speed up measured against local compile? this looks like huge improvement!

also could you provide numerics comparison?

@ydwu4

ydwu4 commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

It's against eager. yes, bitwise check passes see test_bitwise_forward_and_backward!

sanketpurandare added a commit that referenced this pull request Oct 20, 2026
## Agent details (AI-generated)

Adapted from Yidi Wu (@ydwu4), #5259, revision
25dafa9. Preserve original attribution and
license; this change includes integration and generalization, not just a port.

Keep provided-map/L1 and unbiased top-k objectives distinct. Preserve native
coefficient/token normalization, gradient injection, metric accumulation and
DP-local training ownership. Padding contributes no counts, score sums or
gradients; unbiased selection is independent of biased/group-limited routing.

General fused CUDA kernels support positive T/E and configured top-k, while
the aligned production geometry keeps the tuned CuTe launch. Derive scratch
and native CTA partitioning from shapes and device resources. Explicitly fold
block.y partials in native order, independent of Triton layout; preserve
unaligned score-base offsets and scalar clamp-min's strict boundary derivative.
Temporary normalized scores/maps and partial buffers are not autograd saved
state. Save only original scores, frequencies and an optional padding mask.
The general implementation does not fall back to eager ATen for shape/tails.

## Test Plan (AI-generated)

python -m pytest tests/unit_tests/cpu/test_fused_dsv3_router.py tests/unit_tests/gpu/test_fused_dsv3_router.py tests/unit_tests/cpu/test_fused_dsv3_seqwise_loss.py tests/unit_tests/gpu/test_fused_dsv3_seqwise_loss.py -q
pre-commit run --all-files

86 combined CPU/GPU tests passed, covering exact gradients/losses, signed and
tiny scores, clamp equality, all-padding rows, odd experts, native CTA-split
boundaries, offset1/2/3 storage, FakeTensor metadata and fullgraph/capture/replay.
The benchmark's post-hot seed14536 failure is preserved as an exact regression
test; fixing native reduction order removed its one-ULP raw-loss mismatch.
Strict relevant docs passed. All non-type lint hooks passed; only unchanged
baseline type findings remained.

Matched graph-replay F+B general cases beat the same native reference by
2.662x (T8193/E64/K6), 3.064x (T4097/E512/K8) and 2.517x (T33/E129/K8),
with exact reference checks and separate eager GPU trace collection. The
production path retains the same kernels; latest median20.933us versus
20.701us before overlaps the observed trial spread. A longer final matched
hot-path comparison and complete PP2/PP4/memory gates remain pending.
No distributed-throughput or full-model convergence claim follows here.
sanketpurandare added a commit that referenced this pull request Oct 23, 2026
## Agent details (AI-generated)

Adapted from Yidi Wu (@ydwu4), #5259, revision
25dafa9. Preserve original attribution and
license; this change includes integration and generalization, not just a port.

Keep provided-map/L1 and unbiased top-k objectives distinct. Preserve native
coefficient/token normalization, gradient injection, metric accumulation and
DP-local training ownership. Padding contributes no counts, score sums or
gradients; unbiased selection is independent of biased/group-limited routing.

General fused CUDA kernels support positive T/E and configured top-k, while
the aligned production geometry keeps the tuned CuTe launch. Derive scratch
and native CTA partitioning from shapes and device resources. Explicitly fold
block.y partials in native order, independent of Triton layout; preserve
unaligned score-base offsets and scalar clamp-min's strict boundary derivative.
Temporary normalized scores/maps and partial buffers are not autograd saved
state. Save only original scores, frequencies and an optional padding mask.
The general implementation does not fall back to eager ATen for shape/tails.

## Test Plan (AI-generated)

python -m pytest tests/unit_tests/cpu/test_fused_dsv3_router.py tests/unit_tests/gpu/test_fused_dsv3_router.py tests/unit_tests/cpu/test_fused_dsv3_seqwise_loss.py tests/unit_tests/gpu/test_fused_dsv3_seqwise_loss.py -q
pre-commit run --all-files

86 combined CPU/GPU tests passed, covering exact gradients/losses, signed and
tiny scores, clamp equality, all-padding rows, odd experts, native CTA-split
boundaries, offset1/2/3 storage, FakeTensor metadata and fullgraph/capture/replay.
The benchmark's post-hot seed14536 failure is preserved as an exact regression
test; fixing native reduction order removed its one-ULP raw-loss mismatch.
Strict relevant docs passed. All non-type lint hooks passed; only unchanged
baseline type findings remained.

Matched graph-replay F+B general cases beat the same native reference by
2.662x (T8193/E64/K6), 3.064x (T4097/E512/K8) and 2.517x (T33/E129/K8),
with exact reference checks and separate eager GPU trace collection. The
production path retains the same kernels; latest median20.933us versus
20.701us before overlaps the observed trial spread. A longer final matched
hot-path comparison and complete PP2/PP4/memory gates remain pending.
No distributed-throughput or full-model convergence claim follows here.

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.

2 participants