Skip to content

[mHC] Add opt-in deterministic projection backward - #1680

Open
WenzheWang wants to merge 1 commit into
linkedin:mainfrom
WenzheWang:wenzhe/mhc-projection-deterministic-backward
Open

WenzheWang wants to merge 1 commit into
linkedin:mainfrom
WenzheWang:wenzhe/mhc-projection-deterministic-backward

Conversation

@WenzheWang

@WenzheWang WenzheWang commented Nov 24, 2026 •

Copy link
Copy Markdown

Summary

mhc_mm_norm_bwd currently accumulates grad_phi with 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 nonzero out_grad_phi accumulation, output strides and return-time dtype conversion, and the existing grad_x calculation.

Related discussion: mHC projection backward in #1632.

I have personally reviewed the submitted changes and take responsibility for this contribution.

Details

  • Partition token blocks into at most 32 fixed groups, accumulate FP32 partials in a fixed order, then reduce groups with one writer per output element.
  • Bound the extra reduction scratch to 16 MiB independently of token count. When only one group fits, write directly without allocating partial scratch.
  • Compute projection partials before grad_x to preserve the supported out_grad_x=x case.
  • Add regression coverage for FP32/FP16/BF16 and mixed dtypes, tails, non-unit upstream gradients, supplied output buffers, repeated results, the direct fallback, and a public coefficient-backward optimizer update. A dropped-token negative control checks that the precision-aware oracle still rejects a missing gradient contribution.

The determinism guarantee covers the fixed-input projection grad_phi reduction on the same hardware/runtime. Other mHC reductions retain their existing behavior.

Testing Done

  • Hardware Type: NVIDIA A800 80GB PCIe
  • Runtime: PyTorch 2.14.0+cu130, Triton 3.8.0
  • Affected GPU suite: 52 passed, zero failures/errors/skips.

Reproduction command:

CUBLAS_WORKSPACE_CONFIG=:4096:8 python -m pytest -x -q \
  test/transformers/test_mhc_mm_norm_deterministic.py \
  test/transformers/test_mhc.py
52 passed
  • Fixed Ruff 0.14.11, full repository make checkstyle:
All checks passed!
507 files already formatted
All checks passed!
507 files left unchanged
  • run make test to ensure correctness
  • run make checkstyle to ensure code style
  • run make test-convergence to ensure convergence

The full-suite and model-convergence commands have now been run on 021fed76115c9e1b8953276800b6028193b9e2c6, using the runtime above:

Command Reported test cases Failures Errors Skipped
make test 6,756 55 0 1,090
make test-convergence 186 3 0 15

All 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 3596a627fc843882e1d24ae6dcf3a683bb4a1377 in 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 preallocated grad_x output.

N × K × M dtype Atomic median (ms) Ordered median (ms) Extra reduction scratch
4096 × 4096 × 24 FP32 0.6376 0.3046 12 MiB
4096 × 4096 × 24 FP16 0.5216 0.2517 12 MiB
4096 × 4096 × 24 BF16 0.5034 0.2145 12 MiB
32768 × 16384 × 288 BF16, direct fallback 156.9839 11.7191 0

For all four cases above, the old returned grad_phi differed 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=24 cases 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.

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
WenzheWang marked this pull request as ready for review November 24, 2026 07:20
@WenzheWang

Copy link
Copy Markdown
Author

@NotTruecCoder The validation for 021fed7 is complete: all 52 mHC-related tests passed, and all 58 failures from the full test and convergence commands reproduced on the unmodified base with matching failure messages. I have added the comparison to the PR description and kept both full-suite checkboxes unchecked.

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant