Repository navigation
Description
Summary
MLX's CUDA fast::rope kernel mishandles a broadcast input. When the array it rotates has a zero stride on an axis longer than one (the shape produced by tile or broadcast_to), every index along that axis receives the rotation computed for the first position instead of its own. This surfaces as a failing assertion in models::deepseek_v32::rope_tests::prefill_and_decode_cache_yarn_rotated_keys_past_the_original_context, which turns make verify-test-cuda red on main. The production cache is correct; the test's own reference is the victim, and so would be any future CUDA caller that rotates a broadcast view.
Background
The test entered with #3279 (a9860ece9, merged 2026-11-23), verified on ROCm gfx1151. It passes on Metal (nightly b41c07b73, run 45235504140) and on ROCm, and fails only on CUDA. No CI job runs the CUDA suite, so the failure appears only in a local gate run on an NVIDIA GB10 (sm_121). The report measures max diff 1.6620973, deterministic across threading and TF32 settings.
The root cause is in mlx/backend/cuda/rope.cu at the pinned MLX commit fc344c986c4169d99f76b5d95ae6b4a58a359a63 (set in src/lib/mlxcelverse/CMakeLists.txt:340). In RoPE::eval_gpu, any input of at most four dimensions is treated as layout_ok, so the output either donates the input buffer or allocates a new buffer that reuses the input's strides and data size. A broadcast input has a zero stride on the broadcast axis and a data size covering only one copy, so the rotated output collapses every position onto position 0's result. Metal (mlx/backend/metal/rope.cpp) and the ROCm overlay (overlays/rocm/mlx/backend/rocm/rope.hip) always allocate a dense output or copy, which is why they are unaffected.
Upstream rope.cu on ml-explore/mlx main differs from the pin only by an empty-input early return, so a pin bump does not fix it.
Proposed Solution
Add a CUDA source overlay at src/lib/mlxcelverse/overlays/cuda/mlx/backend/cuda/rope.cu: the pinned file with one change in RoPE::eval_gpu, so that an input with a zero stride on an axis longer than one is no longer layout_ok and instead takes the existing branch that copies it into a dense output before rotating. Every other input keeps the path it takes today. mlx_apply_source_overlays copies everything under overlays/cuda/mlx/backend/cuda/, so no CMake change is required; the backend-cuda overlay count goes from 34 to 35 files.
Rejected alternatives:
- Building the test reference from a dense copy. It greens the gate but leaves the kernel returning wrong values for the next caller.
- Forcing every RoPE input contiguous in the bridge. It adds a copy to the prefill path for the transposed views that attention rotates, which the kernel already handles correctly.
Out of scope: fixing this upstream in ml-explore/mlx, and overlapping layouts without a zero stride (as_strided windows), which nothing rotates.
Implementation Notes
- Kernel defect:
mlx/backend/cuda/rope.cu(at the pin),RoPE::eval_gpulines 281-303 (layout_okdecision and buffer setup) andrope_impllines 119 and 135 (output indexing bypos.y). - MLX pin:
src/lib/mlxcelverse/CMakeLists.txt:340(GIT_TAG fc344c986c4169d99f76b5d95ae6b4a58a359a63). - Test reference under repair:
src/models/deepseek_v32_rope_tests.rs:123,rotated_referencebuilds its rows withmlxcel_core::tile(row0, &[1, 1, len, 1]), which MLX returns as a broadcast view; the failing assertion is at line 168. - Overlay mechanism and precedent: the five overlay roots live under
src/lib/mlxcelverse/overlays/(core, cpu, cuda, metal, rocm); ROCm already carriesoverlays/rocm/mlx/backend/rocm/rope.hipwith the dense-output behavior to mirror. - Production exposure:
src/models/mistral4.rs:307,415broadcastsk_peonly after it has been rotated, so no production path rotates a broadcast array. Attention layouts rotate dense or non-zero-stride transposed views, which take the correct paths. - A PR is already open: fix(cuda): rotate broadcast RoPE inputs in a dense output #3443 ("fix(cuda): rotate broadcast RoPE inputs in a dense output").
Acceptance Criteria
-
models::deepseek_v32::rope_tests::prefill_and_decode_cache_yarn_rotated_keys_past_the_original_contextpasses on CUDA with the test source unchanged. - A new
mlxcel-coretest rotates broadcast inputs (along the sequence, head and batch axes, with the input held and with no other owner) and a strided view, for the base and for a frequency table, both conventions, full and partial dims, and compares against the rotation of a dense buffer of the same values, itself checked against a host f64 rotation. It fails on the pinned kernel and passes with the overlay. -
make verify-test-cudais green. - The DeepSeek V3 / V3.2, RoPE and MLA tests still pass, and real-checkpoint output is unchanged for inputs that are not broadcast.
Original Suggestion
Title: fix(cuda): MLX CUDA RoPE gives every row of a broadcast input the same rotation, failing make verify-test-cuda on main
Problem
make verify-test-cuda fails on main with one test:
---- models::deepseek_v32::rope_tests::prefill_and_decode_cache_yarn_rotated_keys_past_the_original_context stdout ----
thread '...' panicked at src/models/deepseek_v32_rope_tests.rs:168:9:
cached k_pe at positions 16..24 must be YaRN-rotated (max diff 1.6620973)
The test came in with #3279 (a9860ece9, merged 2026-11-23), which was verified on ROCm gfx1151. It passes on Metal: the nightly log of 2026-11-23 (b41c07b73, run 45235504140) has it as ok; that run's red was tests::family_order_is_exhaustive, which is #3368 and unrelated. No CI job runs the CUDA suite, so the failure shows only in a local gate run.
The cache the test inspects is correct. What is wrong is the test's reference, and it is wrong because MLX's CUDA fast::rope kernel mishandles a broadcast input: every index along the broadcast axis gets the same output element. This is an MLX backend defect that any CUDA caller rotating a tiled or broadcast_to array would hit, not a DeepSeek V3.2 one.
Reproduction
NVIDIA GB10 (DGX Spark, sm_121), driver 580.178.04, CUDA 13.0.88, Linux aarch64, rustc 1.97.1, main at 1c284f1f3, MLX pin fc344c986c4169d99f76b5d95ae6b4a58a359a63.
MLX_CUDA_ARCHITECTURES=121 cargo test --locked --profile test-fast --features cuda -p mlxcel --lib -- \
--test-threads=1 --exact \
models::deepseek_v32::rope_tests::prefill_and_decode_cache_yarn_rotated_keys_past_the_original_contextIt fails when run alone and is deterministic. The same test binary, three runs per setting:
| Setting | Result |
|---|---|
| no env, default test threads | failed, max diff 1.6620973 x3 |
no env, --test-threads=1 |
failed, max diff 1.6620973 x3 |
MLX_ENABLE_TF32=0 MLXCEL_SDPA_DETERMINISTIC=1 |
failed, max diff 1.6620973 x3 |
MLX_ENABLE_TF32=0 |
failed, max diff 1.6620973 x3 |
MLXCEL_SDPA_DETERMINISTIC=1 |
failed, max diff 1.6620973 x3 |
MLX_ENABLE_TF32=1 |
failed, max diff 1.6623757 x3 |
The other eight tests of models::deepseek_v32::rope_tests pass. The same message and number were in earlier gate runs on 251f08610 and 36129ebb3, on this host and on a second GB10.
With MLX_ENABLE_TF32=1 there is a second, much smaller difference behind this one, at the decode assertion (positions 24..25, 1.75e-4 against a 1e-4 tolerance): the decode step's row differs from the prefill's row 0 by that much when TF32 is on. It is not the defect described here, and the library test process pins MLX_ENABLE_TF32=0 when the variable is unset.
Without the model, on the same host (rotation of one 8-feature row repeated over 8 rows, positions 16..23, compared with a rotation computed on the host in f64; the same four layouts for the base and for a frequency table, traditional on and off):
| Input | Strides | Result on CUDA | Result on the CPU stream |
|---|---|---|---|
dense [1, 1, 8, 8] |
[64, 64, 8, 1] |
rows at positions 16..23, max error under 1e-6 | same |
tile(row, [1, 1, 8, 1]) |
[0, 0, 0, 1] |
every row is the rotation at position 16, max error 1.57 to 4.49 | rows at 16..23 |
broadcast_to(row, [1, 1, 8, 8]) |
[0, 0, 0, 1] |
every row is the rotation at position 16 | rows at 16..23 |
contiguous(tile(..)) |
[64, 64, 8, 1] |
rows at 16..23 | same |
last 8 of 12 features, [1, 8, 1, 8] transposed to [1, 1, 8, 8] (the layout the MLA forward rotates) |
[96, 8, 12, 1] |
rows at 16..23 | same |
Current behaviour
A diagnostic appended to the test file on unmodified main (prefill of 24 rows, then one decode step) prints:
k_pe: cache rows 0..25 vs host f64 YaRN rotation of row 0: worst row 1 diff 0.0000001
k_pe rows 16..24: tiled reference input strides [0, 0, 0, 1], dense copy strides [64, 64, 8, 1]
k_pe rows 16..24: |cache - reference(tiled)| 1.6620973 |cache - reference(dense)| 0.0000000
k_pe rows 16..24: reference(tiled) rows equal the host YaRN rotation at positions [16 16 16 16 16 16 16 16]
k_pe rows 24..25: |cache - reference(tiled)| 0.0000001 |cache - reference(dense)| 0.0000001
indexer key: cache rows 0..25 vs host f64 YaRN rotation of row 0: worst row 24 diff 0.0000000
indexer key rows 16..24: |cache - reference(tiled)| 0.0000000 |cache - reference(dense)| 0.0000000
indexer key rows 16..24: reference(tiled) rows equal the host YaRN rotation at positions [16 17 18 19 20 21 22 23]
So:
- Production is right. Every cached
k_peand indexer-key row, prefill and decode, equals row 0 rotated by the YaRN table at its own position (the tiny config's periods are[1, 400, 4000, 40000]), computed independently on the host. - The reference is wrong.
rotated_reference(src/models/deepseek_v32_rope_tests.rs:123) builds its rows withmlxcel_core::tile(row0, &[1, 1, len, 1]), which MLX returns as a broadcast view, and rotates that. On CUDA all eight reference rows come back as the rotation at position 16.1.6620973is the distance from the correct cache to that. With a dense copy of the same rows the reference matches the cache exactly. - Only this one assertion is affected. The indexer key rotates 8 of its 12 features, and a partial rotation takes the kernel's copy path, which is correct. The decode assertions tile one row, which is not a broadcast.
Cause
mlx/backend/cuda/rope.cu at the pin (fc344c986c4169d99f76b5d95ae6b4a58a359a63, src/lib/mlxcelverse/CMakeLists.txt:340), RoPE::eval_gpu, lines 281-303:
bool partial = dims_ < D;
bool layout_ok = ndim <= 4 || in.flags().row_contiguous;
if (layout_ok) {
if (in.is_donatable()) {
donated = true;
out.copy_shared_buffer(in);
} else if (!partial) {
out.set_data(
cu::malloc_async(in.data_size() * in.itemsize(), encoder),
in.data_size(),
in.strides(),
in.flags());
} else {
...copy_gpu(in, out, ctype, s);
}
} else {
donated = true;
copy_gpu(in, out, CopyType::General, s);
}For any input of at most four dimensions the output either is the input's buffer (donated) or is a new buffer with the input's data size and strides. A broadcast input has a zero stride on the broadcast axis and a data size that covers one copy of the data, so:
- Not donated, full rotation: the output has
stride[T] == 0.rope_implwritesout[2 * pos.x * out_strides[3] + pos.y * out_strides[2] + ...](line 135), so the threads for everypos.ywrite the same elements, and reading the output back returns one row for all of them. The rotation depends onpos.y(line 119), so one row cannot be right for all positions. - Donated: the kernel reads and writes the shared elements in place from several threads. An input broadcast along the head axis, where the non-donated path happens to be right because every head writes the same value, comes back wrong when nothing else owns it (measured: off by 3.73 on the first rotation checked). That this is the in-place branch is inferred from the held input passing and the unowned one failing; I did not instrument the kernel.
- Partial rotation that is not donated: copied to a dense output first, correct.
Metal's RoPE::eval_gpu (mlx/backend/metal/rope.cpp at the pin) always allocates a dense output or copies, and keeps separate input and output strides; the ROCm overlay (overlays/rocm/mlx/backend/rocm/rope.hip) does the same. That is why the test passes there.
Upstream: rope.cu on ml-explore/mlx main differs from the pin only by an early return for an empty input (checked 2026-11-26), so a pin bump does not fix it. gh search issues --repo ml-explore/mlx for "rope broadcast" and "rope cuda strides" returned nothing.
Exposure in production: I looked at every file that both rotates and calls tile, broadcast_to or repeat, and found no production path that rotates a broadcast array (src/models/mistral4.rs:307,415 broadcasts k_pe after rotating it). The attention layouts (dense, or a transposed and sliced view with non-zero strides) take the paths that are correct. That is a reading of call sites, not a proof; the known victim is the test reference above.
Proposed fix
Add a CUDA overlay src/lib/mlxcelverse/overlays/cuda/mlx/backend/cuda/rope.cu: the pin's file, Apple's header kept, the // Modified from upstream MLX fc344c98... mlx/backend/cuda/rope.cu line, and one change in RoPE::eval_gpu: an input with a zero stride on an axis longer than one is not layout_ok, so it takes the existing branch that copies it into a dense output and rotates there. Every other input takes the branch it takes today. mlx_apply_source_overlays copies everything under overlays/cuda/mlx/backend/cuda/, so no CMake change is needed; overlays/cuda/README.md goes from 34 to 35 files.
Rejected:
- Building the test's reference from a dense copy. It makes the gate green and leaves the kernel returning wrong values for the next caller.
- Forcing every RoPE input contiguous in the bridge. It would add a copy to the prefill path for the transposed views attention rotates, which the kernel handles correctly without one.
Out of scope: reporting or fixing this in ml-explore/mlx; layouts that overlap without a zero stride (as_strided windows), which nothing rotates.
Acceptance criteria
-
models::deepseek_v32::rope_tests::prefill_and_decode_cache_yarn_rotated_keys_past_the_original_contextpasses on CUDA with the test source unchanged. - A new
mlxcel-coretest rotates broadcast inputs (along the sequence, head and batch axes, with the input held and with no other owner) and a strided view, for the base and for a frequency table, both conventions, full and partial dims, and requires the rotation of a dense buffer of the same values, which is itself checked against a host f64 rotation. It fails on the pinned kernel and passes with the overlay. -
make verify-test-cudais green. - The DeepSeek V3 / V3.2, RoPE and MLA tests still pass, and real-checkpoint output is unchanged for inputs that are not broadcast.
Activity