Repository navigation
[mHC] Add opt-in deterministic projection backward - #1680
Open
WenzheWang wants to merge 1 commit into
Open
WenzheWang wants to merge 1 commit into
WenzheWang wants to merge 1 commit into
Conversation
Use bounded token groups and ordered FP32 reduction for grad_phi when PyTorch deterministic algorithms are enabled. Preserve the default atomic path, supplied output accumulation, and returned gradient dtypes. Cover tails, dtype and buffer contracts, repeatability, and optimizer use. Signed-off-by: Wenzhe Wang <w_wenzhe@163.com>
WenzheWang
marked this pull request as ready for review
November 24, 2026 07:20
Author
|
@NotTruecCoder The validation for Could you review the projection-backward changes when you have time? The GitHub workflow is still waiting for maintainer approval. |
This branch has not been deployed
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
mhc_mm_norm_bwdcurrently accumulatesgrad_phiwith atomic adds across token blocks. Fixed inputs can therefore produce different returned projection gradients between runs.This PR adds an ordered reduction selected by
torch.are_deterministic_algorithms_enabled(), while retaining the default atomic path. It preserves nonzeroout_grad_phiaccumulation, output strides and return-time dtype conversion, and the existinggrad_xcalculation.Related discussion: mHC projection backward in #1632.
I have personally reviewed the submitted changes and take responsibility for this contribution.
Details
grad_xto preserve the supportedout_grad_x=xcase.The determinism guarantee covers the fixed-input projection
grad_phireduction on the same hardware/runtime. Other mHC reductions retain their existing behavior.Testing Done
2.14.0+cu130, Triton3.8.0Reproduction command:
0.14.11, full repositorymake checkstyle:make testto ensure correctnessmake checkstyleto ensure code stylemake test-convergenceto ensure convergenceThe full-suite and model-convergence commands have now been run on
021fed76115c9e1b8953276800b6028193b9e2c6, using the runtime above:make testmake test-convergenceAll 52 mHC-related tests passed in the full run, including the 18 new deterministic-backward cases. Each of the 55 ordinary-test failures and 3 convergence failures was then rerun on the unmodified base
3596a627fc843882e1d24ae6dcf3a683bb4a1377in the same environment. All 58 reproduced with the same failure type and exact failure message. The ordinary-test failures also matched the assertion location.These comparisons isolate the observed failures from this patch in the tested environment; they do not make either full command pass. Both full-suite checkboxes therefore remain unchecked.
Determinism and timing
Each helper path had 4 warm-up calls and 30 timed samples in balanced atomic/ordered/ordered/atomic order. Inputs were fixed per shape/dtype; the helper seed was
20261124 + N + K + M. The timing includes the helper's actual allocations and launches, using a preallocatedgrad_xoutput.For all four cases above, the old returned
grad_phidiffered from the first run in all 29 subsequent runs; the ordered result differed in none. Supplied nonzero FP32 accumulation buffers were checked separately, and the ordered results were also repeatable.The smaller
N=65, K=257, M=24cases took 1.45–1.65× as long with the ordered path. At the integration level, coefficient forward+backward increased from 3.6966 to 4.0074 ms (+8.4%), and a two-layer BF16 mini-LM increased from 30.4584 to 31.1194 ms (+2.2%). These comparisons used the same weights/inputs and changed only the helper's selection policy. The mini-LM used B=1, T=128, HC=4, hidden size 256, 4 attention heads, and vocabulary size 1024; timings exclude optimizer updates.These shape-dependent costs are why this PR keeps the ordered path opt-in.