Repository navigation
fix(rocm): widen gather QMV output offsets to 64 bits - #2624
Conversation
Follow-up to ml-explore/mlx#4738. gather_qmv_kernel, both tiled gather kernels and gemv_gather_impl still formed their output offset (and, in the opt-in 4/8-bit tiled kernel, the expert and activation offsets from uint32_t indices) in 32 bits, so an output past 2^31 elements wrapped. Each offset is now int64_t, the pattern of gather_qmv_warp_shared_kernel. The suspected 65535-block grid-z cap is ruled out on gfx1151 with ROCm 7.15: an unsorted gather_qmm at B = 65537 ran correctly through the wide, warp-shared (affine and mxfp4) and per-row kernels on the unmodified overlay, and a hipLaunchKernel probe ran grids of up to 16777216 blocks in z and 262144 in y although maxGridSize reports 65535. Launch geometry is unchanged; the B = 65537 tests stay as a guard, and an ignored test checks a gather_qmv_kernel output of 2^31 + 2^16 elements across element 2^31. The dead quantized/qmv_kernel.hip (built and included by nothing) is dropped. LOCAL_FIXES.md item 61 records the change. Closes #2620
The bf16 affine cases in tests/rocm_hip_grid_clamp/gather_qmv.rs quantized their weights from f32, so the affine scales and biases were f32. gather_qmm promotes the activations to the affine scales' dtype, so both bf16 affine cases ran the f32 per-row gather_qmv_kernel instead of gather_qmv_wide_kernel and gather_qmv_warp_shared_kernel, and the decisive B = 65537 case never reached the default bf16 4-bit kernel. Weights::new now quantizes from the activation dtype, and each fast case asserts that the output dtype equals the activation dtype, which fails if the dispatch falls back to f32. The ignored >2^31 test keeps f32. The five gather tests in rocm_hip_grid_clamp pass on gfx1151 under the GPU guard on the rebased branch, and clippy with -D warnings is clean. Refs #2620
Implementation Review SummaryIntent
Findings Addressed
Remaining Items
Verification
|
Item 61 and the grid-clamp test header now say that the ROCm 7.15 probe did not reproduce the y-grid refusal recorded in item 46 and that the item 46 caps stay. Item 61 also states that the gather cases assert the activation dtype, so the bf16 cases cannot fall back to the f32 kernel.
Security and performance reviewNo CRITICAL or HIGH findings, so nothing was changed. Every widened offset in Remaining (LOW, pre-existing, follow-up only):
All five contract targets pass, including |
…2627) ## Summary The ROCm per-row launchers (softmax, logsumexp, rms_norm plain/strided/VJP, layer_norm forward/VJP) put one block per row in grid x with an `int` `n_rows`. HIP refuses `gridDim.x * blockDim.x >= 2^32`, so 2^24 + 1 rows at 256 threads failed to launch. This is the ROCm side of ml-explore/mlx#4717. - Each launcher caps grid x at `UINT32_MAX / BLOCK_DIM` and spills into y (the `arg_reduce.hip` pattern of LOCAL_FIXES item 11). Kernels take `n_rows`, compute a 64-bit row from `blockIdx.x + blockIdx.y * gridDim.x`, and return past it before any barrier. Below the cap the grid is `dim3(n_rows)` as before. - The strided rms_norm kernel decomposes its row in 64 bits. - LOCAL_FIXES item 62 (61 was taken by #2624 while this was in flight). - Two test-only FFI entries, `fast_rms_norm_vjp_for_test` and `fast_layer_norm_vjp_for_test`, run the VJP primitives through `mlx::core::vjp`; the crate had no VJP entry point. ## Tests (gfx1151) New `row_grid` module in `tests/rocm_hip_grid_clamp.rs`: seven launches on 2^24 + 1 x 4 f32, every element compared on the GPU with an f64 host reference, plus `#[ignore]`d tests for the 512 and 1024-thread softmax arms (about 17 GiB each). Before the fix (overlay reverted, same tests): `0 passed; 7 failed; 2 ignored`, each with: ``` GPU launch failed: hipLaunchKernel( func, grid_dim, block_dim, params, smem_bytes, stream_) failed: invalid configuration argument ``` After: `7 passed; 0 failed; 2 ignored; finished in 7.12s`. The whole `rocm_hip_grid_clamp` file after rebasing onto #2624: `20 passed; 0 failed; 7 ignored`. A CPU-stream reference was tried first and dropped: about 5 minutes a test from host reads of device memory, and its f32 sum over 2^24 rows for the layer_norm weight gradient was 2.7e5 off the GPU value, which is within 1.7e3 of the f64 sum. The weight gradients use rtol 1e-3 for that reason; the per-row `dx` checks that every row ran. Existing tests, post-fix, under `rocm_gpu_guard.sh --hold`: mlxcel-core lib filters `softmax` (10), `rms_norm` (15), `layer_norm` (2), `fused_norm_parity` (10), all pass. `make verify-llama-compat verify-env-var-docs verify-kernel-port-dispatch verify-kernel-dtype-keys verify-rocm-overlay` and clippy (`-p mlxcel-core --lib --tests`, `-p mlxcel --test rocm_hip_grid_clamp`) are clean. Not run: the two ignored softmax arm tests (17 GiB on a 31 GiB shared host), `layer_norm_multiwave_mean_snapshot_is_stable` (needs the `mlxcel` lib test build). Closes #2619
Summary
Widens the remaining 32-bit offsets in the ROCm gather QMV paths to
int64_t(follow-up to ml-explore/mlx#4738) and settles the suspected grid-z launch limit: z cap ruled out on gfx1151, so launch geometry is unchanged.Grid-z result
The decisive test (unsorted
gather_qmm, bf16, affine 4-bit, group 64, E=128, M=1, N=K=64, B=65,537, every element against a host reference) passes with the launch geometry unchanged, as does the same shape through the bf16 8-bit warp-shared, bf16 mxfp4 and f32 per-row kernels. The first pre-fix run of the bf16 affine cases quantized the weights in f32, sogather_qmmpromoted them to the f32 per-row kernel; review fixed the weights to quantize in the activation dtype and each case now asserts the output dtype, and the corrected bf16 cases pass. The widening touches none of the bf16 kernels those cases reach, so they exercise the same batch-in-z launch the code had before. A standalonehipLaunchKernelprobe (ROCm 7.15, HIP runtime 71526333) ran grids of 65,537, 262,144 and 16,777,216 blocks in z and 65,537 and 262,144 in y, every block executing, althoughhipDeviceProp_t::maxGridSizeandrocminforeport 65535 for y and z. The limit that applies is 2^32 - 1 threads per dimension (LOCAL_FIXES item 11). The B = 65,537 tests stay as a regression guard.Changes
gather_qmv_kernel(qmm.hip),gather_qmv_tiled_kernelandgather_qmv_tiled_6bit_kernel(qmv_tiled_kernel.hip) compute the output index inint64_t; the opt-in 4/8-bit tiled kernel also widensrhs_idx * N * w_strideandlhs_idx * M * K.gemv_gather_impl(gemms/gemv.hip) offsets its output byint64_t(indices_idx) * rows.quantized/qmv_kernel.hipis dropped, and the stale comment inqdequant.hppnow names its real user. README backend count is 110.LOCAL_FIXES.mditem 61 (60 was taken by fix(rocm): match MLX in eager complex64 and bool casts #2623). No core file changed, soCORE_RESIDUAL.diffis untouched.tests/rocm_hip_grid_clamp/gather_qmv.rsmodule: the four B = 65,537 cases, plus an#[ignore]f32 test (N=65,536, K=64, B=32,769, 2^31 + 2^16 output elements) checking rows 0, 32,767 and 32,768 against the host.Test plan
rocm_hip_grid_clamp, all 13 fast tests pass underrocm_gpu_guard.sh --hold; after the review fix and the rebase onto main, thegatherfilter (5 fast tests) passes againgather_qmv_kernel's 32-bit index restored, it fails withhipErrorIllegalAddressingather_qmv_kernel<float, float, 4, 64, true>(dispatch grid[256, 256, 32769])gather(41) andquantized(42) filters, androcm_gather_qmm_expert_batched(2), passmake verify-llama-compat verify-env-var-docs verify-kernel-port-dispatch verify-kernel-dtype-keys verify-rocm-overlayandrocm_overlay.py driftpasscargo clippy --profile test-fast --features rocm -p mlxcel -p mlxcel-core --lib --tests -- -D warningsis cleanThe full fast set and the ignored test ran on the pre-rebase base (1cb18cc); the rebase onto main added #2622 and #2623, which do not touch these kernels. The overlay and contract checks were re-run after the rebase. The item 46 record says HIP refuses y grids past 65535 blocks; this runtime did not reproduce that, and item 61 and the test header now say so while leaving the item 46 caps in place.
Follow-ups not in scope:
gemv_batchedandgemv_batched_inlineingemms/gemv.hipstill formbatch_idx * rowsinint(not gather paths); no B = 65,537 case covers the bf16 6-bit tiled gather kernel; the tiled gather kernels clamp an out-of-range expert to 0 where the other gather kernels write zeros.Closes #2620