perf: fuse mHC coefficient backward epilogue - #1402
Open
heiheiha798 wants to merge 3 commits into
Open
Conversation
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.
Addresses heiheiha798#8.
Engineering record: heiheiha798#9.
Summary
LigerMHCCoeffsFunction.backwardcurrently leaves Triton after the Sinkhorn backward and executes a fixed eager PyTorch coefficient-gradient epilogue before the fused MM/RMS backward. For the usualHC=4, that epilogue operates on only 24 columns but launches 29 pointwise/copy/reduction kernels on B300.This change replaces only that epilogue with:
grad_mix, and FP32 partialgrad_b/alpha gradients;grad_bcolumns and three scalar alpha gradients;benchmark/tools/that loads clean baseline/candidate worktrees in separate processes and alternates providers.The comparison helper intentionally lives outside
benchmark/scripts/benchmark_*.py, so the documentedmake run-benchmarkswildcard continues to contain only shared-framework benchmark scripts. Sinkhorn backward, MM/RMS backward, pre/post-residual kernels, forward behavior, public APIs, and the Ascend backend are unchanged. There is no autotune or architecture dispatch.Testing Done
make testto ensure correctnessmake checkstyleto ensure code stylemake test-convergenceto ensure convergenceAdditional recorded validation:
test/transformers/test_mhc.py,54 passed in 16.11s.HC={2,4,8}, odd/ragged token and channel counts,tmax={1,4,20}, and pre-only/post-only/residual-only non-unit upstream gradients. All six gradients are compared againstmhc_coeffs_refwith repository tolerances.40 passed.make checkstyleat PR HEAD: passed, 371 files formatted.benchmark/tools/compare_mhc_coeff_epilogue_commits.pyloaded baseline124fb8a2and candidate4302c8cfrom detached clean worktrees, alternated both providers for two rounds, and completed backward/full CUDA-event timing plus JSONL/readable summaries.The full repository test suite and convergence suite were not run, so their template boxes remain unchecked.
Commits and Environment
124fb8a2a443c1118f7caeff0203aea78424eb2b189bf55c4d53e8fbb5ff4e1ae96ae19431d3152b4302c8c4497983fc8990ac819e32656e4dbd3e79Job 4953 verified both detached worktrees, exact commits, import paths, index, and worktree cleanliness before GPU work. Each provider ran in a separate exact-source Python process in one B300 allocation. The two later commits add and then relocate only the comparison tool; kernel/test source remains the measured
189bf55content.Performance Method
Primary matrix: BF16,
B=4,HC=4,C={1024,2048,4096,8192},T={128,512,2048},tmax=20, direct coefficient backward and directly measured forward-plus-backward. Controls cover FP16,HC={2,8}, andtmax=8.For every comparison:
Selected job 4953 results (milliseconds,
p20 / median / p80):All 36 declared backward/full comparisons passed the no-regression gate. The minimum ratio was
1.065x; 22 primary comparisons improved by at least 10%.Profile Evidence
Nsight Compute profiled the same exact-source BF16
B=4, HC=4, C=4096, T=512, tmax=20backward:NCU timings are intrusive supporting evidence; the CUDA-event table above is the performance result.
Reproduction
Create clean detached worktrees, then run the tracked tool inside a B300 allocation:
The tool writes machine-readable per-round and summary JSONL to
--output, and writes a compact human-readable comparison table to stderr. FP16 and alternate-HC/tmax controls can be reproduced by changing the explicit CLI values.Risks and Limitations
grad_band alpha reductions use a deterministic two-stage order rather than eager PyTorch's reduction tree; tests intentionally use repository tolerances instead of claiming bit identity for those reductions.