Repository navigation
fix(rocm): write the layer_norm VJP bias gradient - #2635
Merged
Merged
Conversation
LayerNormVJP::eval_gpu on ROCm allocated the bias gradient and never wrote it, so d/db of fast::layer_norm was whatever the allocator returned (zeros or a stale buffer on gfx1151). It now sums the cotangent over every row with col_reduce before the VJP kernel is enqueued, as the CUDA backend does, since that kernel overwrites a donated cotangent. The cotangent is reduced through a [n_rows, axis_size] view so an N-D cotangent is summed over every leading axis. fast_layer_norm_vjp_for_test takes a nullable weight so the bias gradient can be tested without one. New tests compare d/db with host column sums for f32, f16 and bf16, with and without a weight, on both column-reduce kernels, and the 2^24 + 1 row test now checks d/db with a cotangent offset that keeps zeroed memory from passing. All three fail on main and pass with the fix on gfx1151. LOCAL_FIXES.md item 64. Closes #2632
inureyes
marked this pull request as ready for review
October 26, 2026 14:27
inureyes
added a commit
that referenced
this pull request
Oct 26, 2026
…2636) Three ROCm overlay launch paths narrowed 64-bit sizes to `int` or divided before an empty-output check. All three are fixed here with one test module, `tests/rocm_hip_grid_clamp/size_guards.rs`, and `LOCAL_FIXES.md` item 65. No core file changes. ## Fixes - **(a) Norms** (`layer_norm.hip`, `rms_norm.hip`): forward returns after allocating an empty output, and the VJPs return after allocating `gx`, with `gw` and `gb` filled with zeros through `fill_gpu` (the sum over no rows). The VJPs size `gw_temp`, the `gb` view from #2635 and the column-reduce plans with 32-bit dimensions (`Shape` is int32), so the wider type does not fit; past `INT_MAX` rows with a weight or bias gradient they now throw "rows exceed INT_MAX" before copying any input. - **(b) gemv** (`gemms/gemv.hip`): `mat + row * cols` in `gemv_impl` and `out + batch_idx * rows` in `gemv_batched` and `gemv_batched_inline` are 64-bit. - **(c) GatherQMM** (`quantized/qmm.hip`): `if (out.size() == 0) return;` moved above `B = out.size() / M / N`, after the `set_input_array`/`set_output_array` calls, for every mode. ## Tests (gfx1151, red on main, green with the fix) | Test | On main | | --- | --- | | `norms_of_zero_rows_return_empty_and_zero_gradients` (forward, VJP every `wrt`, with and without weight) | `hipLaunchKernel ... invalid configuration argument` | | `norm_vjp_past_int_max_rows_throws` (ignored; 2^31 + 2 rows) | memory fault in `col_reduce_looped` | | `gemv_past_i32_matrix_elements_matches_host` (ignored; 65537 x 32768 f16) | memory fault in `gemv_single` | | `batched_gemv_past_i32_output_elements_matches_host` (ignored; 65535 x 32800 output) | memory fault in `gemv_batched_inline` | | `gather_qmm_affine_with_empty_output_returns_empty` (M = 0, N = 0) | SIGFPE | | `gather_qmm_nvfp4_with_empty_output_returns_empty` (M = 0, N = 0) | SIGFPE | The ignored tests pass with `--ignored` (2.9 s together; the row-count test allocates nothing with the guard). With the fix, the full `rocm_hip_grid_clamp` binary (25 passed, 10 ignored), `rocm_nvfp4_quant` and `rocm_gather_qmm_expert_batched` pass. Clippy on `mlxcel` and `mlxcel-core` lib and tests is clean, and the contract checks (`verify-llama-compat`, `verify-env-var-docs`, `verify-kernel-port-dispatch`, `verify-rocm-overlay`, `verify-kernel-dtype-keys`) pass. Closes #2633
Closed
5 tasks
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.
LayerNormVJP::eval_gpuin the ROCm overlay allocatedgband never wrote it (a TODO stood where the reduction belonged), so the bias gradient offast::layer_normwas uninitialized memory: zeros in a fresh allocation, a stale buffer otherwise. The existing past-the-cap VJP test checked onlywrt0 and 1.Fix
layer_norm.hip: before the VJP kernel is enqueued,col_reducesums the cotangent over every row intogbwith aContiguousStridedReduceplan overn_rowsxaxis_size, asmlx/backend/cuda/layer_norm.cudoes;gbis marked as a kernel input whengwas donated intogxorgw_temp. The reduction has to run first because the kernel overwrites a donatedg. Unlike CUDA,gis reduced through a[n_rows, axis_size]view of its buffer, so a 3-D cotangent is summed over every leading axis by construction rather than by howColReduceArgscollapses the non-reduced axes.fast_layer_norm_vjp_for_testtakes a nullableweight(*const MlxArray, asfast_scaled_dot_product_attentiondoes formask); without one,wrt1 is an error.LOCAL_FIXES.mditem 64. No core file changes, soCORE_RESIDUAL.diffis untouched.Tests (gfx1151, red on main, green with the fix)
layer_norm_bias_grad::layer_norm_vjp_bias_grad_matches_host: f32, f16 and bf16 with weight and bias, on[24, 8](col_reduce_small) and[4, 37, 33](col_reduce_looped). On main:got 0.0, expected -2.375.layer_norm_bias_grad::layer_norm_vjp_bias_grad_without_weight_matches_host: the same without a weight. On main:got -2.6875, expected -2.375(stale buffer).row_grid::layer_norm_vjp_past_the_row_grid_cap_matches_hostnow checkswrt = 2at 2^24 + 1 rows. The cotangent gets a +0.5 offset, because the old one sums to about zero per 97-row cycle and a zeroedgbpassed. On main:got [0, 0, 0, 0], expected [8388608, ...].The whole
rocm_hip_grid_clampbinary passes (22 passed, 7 ignored), clippy onmlxcelandmlxcel-corelib and tests is clean, and the contract checks (verify-llama-compat,verify-env-var-docs,verify-kernel-port-dispatch,verify-rocm-overlay,verify-kernel-dtype-keys) pass.Closes #2632