Skip to content

fix(rocm): write the layer_norm VJP bias gradient - #2635

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-2632-rocm-layer-norm-vjp-bias-grad
Oct 26, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-2632-rocm-layer-norm-vjp-bias-grad

Conversation

@inureyes

Copy link
Copy Markdown
Member

LayerNormVJP::eval_gpu in the ROCm overlay allocated gb and never wrote it (a TODO stood where the reduction belonged), so the bias gradient of fast::layer_norm was uninitialized memory: zeros in a fresh allocation, a stale buffer otherwise. The existing past-the-cap VJP test checked only wrt 0 and 1.

Fix

  • layer_norm.hip: before the VJP kernel is enqueued, col_reduce sums the cotangent over every row into gb with a ContiguousStridedReduce plan over n_rows x axis_size, as mlx/backend/cuda/layer_norm.cu does; gb is marked as a kernel input when g was donated into gx or gw_temp. The reduction has to run first because the kernel overwrites a donated g. Unlike CUDA, g is 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 how ColReduceArgs collapses the non-reduced axes.
  • fast_layer_norm_vjp_for_test takes a nullable weight (*const MlxArray, as fast_scaled_dot_product_attention does for mask); without one, wrt 1 is an error.
  • LOCAL_FIXES.md item 64. No core file changes, so CORE_RESIDUAL.diff is 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_host now checks wrt = 2 at 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 zeroed gb passed. On main: got [0, 0, 0, 0], expected [8388608, ...].

The whole rocm_hip_grid_clamp binary passes (22 passed, 7 ignored), 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 #2632

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 inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority platform:linux Linux (CUDA / packaging) specific labels Oct 26, 2026
@inureyes inureyes added status:done Completed and removed status:review Under review labels Oct 26, 2026
@inureyes
inureyes marked this pull request as ready for review October 26, 2026 14:27
@inureyes
inureyes merged commit 0ee8f03 into main Oct 26, 2026
29 checks passed
@inureyes
inureyes deleted the fix/issue-2632-rocm-layer-norm-vjp-bias-grad branch 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
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

platform:linux Linux (CUDA / packaging) specific priority:high High priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: Write the layer_norm VJP bias gradient on ROCm

1 participant