Skip to content

fix(rocm): guard empty and 64-bit sizes in norm VJP, gemv, GatherQMM - #2636

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-2633-rocm-empty-and-64bit-guards
Oct 26, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-2633-rocm-empty-and-64bit-guards

Conversation

@inureyes

Copy link
Copy Markdown
Member

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 fix(rocm): write the layer_norm VJP bias gradient #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

Three ROCm overlay launch paths narrowed 64-bit sizes to int or divided before an empty-output check.

- rms_norm and layer_norm: zero rows launched row_grid(0), an invalid configuration. Forward and VJP now return early; the VJPs fill dw and db with zeros. The VJPs size gw_temp and the reduction plans with 32-bit dimensions, so past INT_MAX rows they now throw a named error before copying any input instead of wrapping and faulting in col_reduce_looped.
- gemv: the row offset in gemv_impl and the batch output offset in both batched kernels are 64-bit, as #2624 did for the gather kernels.
- GatherQMM: the empty-output return now precedes B = out.size() / M / N, which raised SIGFPE for M == 0 or N == 0 in every mode.

tests/rocm_hip_grid_clamp/size_guards.rs covers each case; all six tests fail or crash on main and pass with the fix on gfx1151 (the gemv and row-count tests are ignored for memory). LOCAL_FIXES.md item 65.

Closes #2633
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium 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:48
@inureyes
inureyes merged commit 5610f8f into main Oct 26, 2026
28 checks passed
@inureyes
inureyes deleted the fix/issue-2633-rocm-empty-and-64bit-guards branch October 26, 2026 14:48
@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:medium Medium 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: Guard empty and 64-bit sizes in ROCm norm VJP, gemv, GatherQMM

1 participant