Skip to content

fix(rocm): match MLX in eager complex64 and bool casts - #2623

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-2616-rocm-complex-bool-cast
Oct 26, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-2616-rocm-complex-bool-cast

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Eager astype between complex64 and bool on ROCm now matches MLX CUDA/CPU and the fused compiled path. CastOp<hipFloatComplex, bool> returns x.x != 0 || x.y != 0 (was &&) and CastOp<bool, hipFloatComplex> returns 1+0i for true (was 1+1i). Recorded as LOCAL_FIXES item 60, citing mlx/backend/cuda/device/cast_op.cuh:22-39.

Probe graphs 12 (complex64 input to bool) and 13 (bool input to complex64, packed as real + 10 * imag) feed an eagerly built input so the eager mode runs a standalone cast kernel; the compiled modes run the fused one.

Test plan

  • Before the fix on gfx1151: eager_and_compiled_complex64_to_bool_follow_mlx failed with left: [0.0, 0.0, 0.0] right: [1.0, 1.0, 0.0], and eager_and_compiled_bool_to_complex64_follow_mlx failed with compiled [1.0, 0.0, 1.0] != eager [11.0, 0.0, 11.0] (8 passed, 2 failed).
  • After the fix on gfx1151, under scripts/rocm_gpu_guard.sh --hold: compiled_dtype_cast_tests 10 passed, 0 failed.
  • make verify-llama-compat verify-env-var-docs verify-kernel-port-dispatch verify-kernel-dtype-keys verify-rocm-overlay, cargo fmt check, and cargo clippy --profile test-fast --features rocm -p mlxcel-core --lib --tests -- -D warnings clean.

Closes #2616

The eager ROCm CastOp for complex64 -> bool used `&&`, so 1+0i and 0+1i cast to false, and bool -> complex64 returned 1+1i for true. MLX CUDA (cast_op.cuh) and the CPU complex64_t::operator bool use `||` and 1+0i, and the fused path already follows them since item 59, so eager and compiled results disagreed.

copy.hpp now returns `x.x != 0 || x.y != 0` and (1, 0). Recorded as LOCAL_FIXES item 60. New probe graphs 12 and 13 run each cast as a standalone eager astype and in both compiled modes; the two new tests failed on gfx1151 before the fix and pass after.

Closes #2616
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:low Low priority 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 09:24
@inureyes
inureyes merged commit 47bf32b into main Oct 26, 2026
28 checks passed
@inureyes
inureyes deleted the fix/issue-2616-rocm-complex-bool-cast branch October 26, 2026 09:24
inureyes added a commit that referenced this pull request Oct 26, 2026
## 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, so `gather_qmm` promoted 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 standalone `hipLaunchKernel` probe (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, although `hipDeviceProp_t::maxGridSize` and `rocminfo` report 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_kernel` and `gather_qmv_tiled_6bit_kernel` (`qmv_tiled_kernel.hip`) compute the output index in `int64_t`; the opt-in 4/8-bit tiled kernel also widens `rhs_idx * N * w_stride` and `lhs_idx * M * K`.
- `gemv_gather_impl` (`gemms/gemv.hip`) offsets its output by `int64_t(indices_idx) * rows`.
- The dead `quantized/qmv_kernel.hip` is dropped, and the stale comment in `qdequant.hpp` now names its real user. README backend count is 110.
- `LOCAL_FIXES.md` item 61 (60 was taken by #2623). No core file changed, so `CORE_RESIDUAL.diff` is untouched.
- New `tests/rocm_hip_grid_clamp/gather_qmv.rs` module: 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

- [x] `rocm_hip_grid_clamp`, all 13 fast tests pass under `rocm_gpu_guard.sh --hold`; after the review fix and the rebase onto main, the `gather` filter (5 fast tests) passes again
- [x] The ignored >2^31 test passes with the fix (5.8 s). Negative control: with `gather_qmv_kernel`'s 32-bit index restored, it fails with `hipErrorIllegalAddress` in `gather_qmv_kernel<float, float, 4, 64, true>` (dispatch grid `[256, 256, 32769]`)
- [x] mlxcel-core lib `gather` (41) and `quantized` (42) filters, and `rocm_gather_qmm_expert_batched` (2), pass
- [x] `make verify-llama-compat verify-env-var-docs verify-kernel-port-dispatch verify-kernel-dtype-keys verify-rocm-overlay` and `rocm_overlay.py drift` pass
- [x] `cargo clippy --profile test-fast --features rocm -p mlxcel -p mlxcel-core --lib --tests -- -D warnings` is clean

The 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_batched` and `gemv_batched_inline` in `gemms/gemv.hip` still form `batch_idx * rows` in `int` (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
@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

priority:low Low 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(rocm): eager complex64 <-> bool casts disagree with MLX and the fused path

1 participant