Repository navigation
[CUDA] Synchronize LayerNorm reduction scratch reuse - #4866
Conversation
|
Thank you for the contribution, that's great. I added a small comment: I think would be better to have vjp test in python rather than in cpp. |
Move the LayerNorm forward/VJP tests to Python and retain the C++ shared-scratch regression. Add RMSNorm VJP coverage for persistent scratch reuse, different widths, and strided weights. Check outputs and gradients independently so a failed forward assertion does not skip gradient validation.
Thank you for the review and for adding the RMSNorm fix! I’ve moved the VJP tests to Python and added RMSNorm regression coverage in 1303f5e. All 11 focused tests pass on Windows and Linux. |
| CHECK(array_equal(a, ones({4})).item<bool>()); | ||
| } | ||
|
|
||
| TEST_CASE("test CUDA layer norm shared scratch") { |
There was a problem hiding this comment.
A better place for these type of test will be python API.
| CHECK(array_equal(a, ones({4})).item<bool>()); | ||
| } | ||
|
|
||
| TEST_CASE("test CUDA layer norm shared scratch") { |
There was a problem hiding this comment.
I think this can be removed
| self.assertLess(mx.abs(gb1).max(), 1e-9) | ||
| self.assertLess(mx.abs(gb2).max(), 1e-9) | ||
|
|
||
| def test_layer_norm_vjp_shared_scratch(self): |
There was a problem hiding this comment.
Maybe we can rename the tests to just test_layer_norm_vjp / test_rms_norm_vjp and generalise it to all the cases?
Co-authored-by: Anastasiia Filippova <[email protected]> Ported: python/tests/test_fast.py had the fork's older test_rms_norm_grad and test_layer_norm_grad, which conflicted with Apple's rewrite. Both are taken as Apple's: they now equal Apple's at this commit. (cherry picked from commit d20ac3b)
Consecutive
BlockBroadcastReduce::Sumcalls in CUDA LayerNorm can overwrite shared scratch before every warp finishes reading it. The existing barrier orders writes before reads, but does not protect scratch reuse.Add a block barrier after the final warp reduction, before the helper returns. The helper is shared by the forward and VJP kernels.
Validation
Linux / GB10
Baseline:
fc344c986. NVIDIA GB10 (sm_121), Linux aarch64, GCC 13.3, CUDA 13.0.88, cuDNN 9.13.0; fresh C++ CUDA build with CPU and Python bindings disabled.The one-row API failure and fix also reproduce with
MLX_USE_CUDA_GRAPHS=0. Ordinary serial runs pass on both versions and do not expose this race.cuda_tests.cppcases: 4 cases, 67 assertions pass.PYTHONUTF8=1on Windows. The full Python suite and Metal validation were not run.To reproduce the baseline failure, apply only the
tests/cuda_tests.cppchanges tofc344c986. Build each version with CUDA andMLX_BUILD_TESTS=ON, then run:compute-sanitizer --tool racecheck --racecheck-report analysis --error-exitcode 86 \ build/tests/tests --test-case="test CUDA layer norm shared scratch"Windows / RTX 4060 Ti
Windows x64, RTX 4060 Ti (
sm_89, WDDM), driver 610.88, MSVC 19.44, CUDA 13.0.48, cuDNN 9.19, Compute Sanitizer 2025.3. The exact PR commit7d0af56ddwas built incrementally as a CPU+CUDA shared library. The same test executable was run against separate baseline and patched DLLs.cuda_tests.cppcasesThese runs also encounter the separate DLL-teardown issue tracked by #4867: ordinary baseline and patched runs exit 2170, as do patched racecheck runs. Baseline racecheck exits 86 for detected hazards.
With the same WDDM lifetime fix from #4867 applied to both DLLs, ordinary tests exit 0. The patched CUDA suite passes 72/72 assertions, the existing LayerNorm bias-gradient regression passes, and the standalone forward/VJP matrix passes 60/60 configurations with no incorrect values. Two initially empty output logs were rechecked with synchronous capture.
Initial racecheck runs with this additional WDDM fix timed out during process termination using Compute Sanitizer 2025.3. A tool-version control using 2025.4.0.0 (build 36782660) from the installed CUDA 13.1 package completes with the same test executable, MLX DLLs, CUDA 13.0 build/runtime environment and driver. With CUDA Graphs on/off, the baseline scratch test fails 8/8 assertions, reports 8 hazards and exits 86; both patched tests pass 56/56 assertions, 0 hazards and exit 0, twice per Graphs setting (four runs). This isolates the observed hang to the older sanitizer configuration, consistent with NVIDIA's documented Windows exit-hang fixes.
Windows timings remain excluded from the performance comparison because processes left by the 2025.3 runs continued consuming CPU during those measurements.
Windows regression command after a Visual Studio CUDA build:
Performance
GB10 results: float32, width 1024; median (range) in µs/op, including host submission and GPU completion. The ranges overlap, and small-input timings vary substantially.
Timing program and build instructions
Runs alternate baseline/patched/patched/baseline (A–B–B–A). Each invocation uses ten warmups and five rounds of 1000 operations. Discarding the first round leaves eight measured rounds per version.
Save the following as
layernorm_bench.cppand build it separately against each MLX installation: