Skip to content

fix(ops): pass one shift for each axis in roll(_:shift:axes:) - #50

Merged
Gajesh2007 merged 2 commits into
mainfrom
fix/roll-multiple-axes
Oct 2, 2026
Merged

Gajesh2007 merged 2 commits into
mainfrom
fix/roll-multiple-axes

Conversation

@davidtai

@davidtai davidtai commented Oct 1, 2026 •

Copy link
Copy Markdown

Proposed changes

Defect

Source/MLX/Ops.swift:2595, roll(_:shift:axes:) passes [shift] (one value) with all the axes to mlx_roll_axes. The C++ roll(a, Shape shift, axes) needs one shift value for each axis, so with two or more axes MLX raises [roll] At least one shift value per axis is required.

PR #41 found this with OpsCoverageTests.testRollMultipleAxes (Fork Tests run 36806791209 recorded it as an expected failure).

Reference behavior

Python mx.roll(a, 1, axis=(0, 1)) calls the C++ overload roll(a, int shift, axes), which repeats the shift for each axis. For a = [[0, 1, 2], [3, 4, 5]] the result is [[5, 3, 4], [2, 0, 1]].

Fix

Repeat the shift once for each axis before the call.

Test

Tests/MLXTests/RollMultipleAxesTests.swift adds testRollMultipleAxes. It checks the two-axis roll above and a one-axis roll. It is the same test as OpsCoverageTests.testRollMultipleAxes in #41, without the XCTExpectFailure wrapper.

Merge note

This PR fixes the defect that #41 records in OpsCoverageTests.testRollMultipleAxes. After #41 has removed its wrapper and merged, the duplicate test in this PR can be deleted.

Merge order for the fix and recording PRs

  1. Merge the fix PRs first: fix(dtype): use float32 finfo for complex64 and fix bfloat16 subnormal #44, then fix(nn): keep the float16 causal mask finite #39, fix(nn): use the normalized sum as post-norm decoder cross-attention queries #40, fix(linalg): pass upper on in the free triInv function #43, fix(nn): make the deprecated SoftMax use the last axis #47, fix(ops): give convolve .same the right length for an even kernel #48, fix(ops): reduce over all axes in median(_:keepDims:) #49, fix(ops): pass one shift for each axis in roll(_:shift:axes:) #50 and fix(ops): default nanToNum infinities to the largest finite value #51 (see step 4 for why fix(dtype): use float32 finfo for complex64 and fix bfloat16 subnormal #44 goes before fix(nn): keep the float16 causal mask finite #39).
  2. Then, for each recording PR (test: cover Transformer and the recurrent layers #37, test: cover the ops free functions, operators and methods #41, test: cover linalg, random, factory, dtype, parameter types and ArrayAt #42, test: cover MLXNN losses, activations, quantized layers and upsample #45): on its branch, remove its XCTExpectFailure wrappers (its PR body has a checklist with file and line), and in test: cover Transformer and the recurrent layers #37 change testCausalMaskValues from -1e9 to the dtype minimum. Do this BEFORE the recording PR merges. XCTest reports an unexpected pass inside XCTExpectFailure as a failure, so with this order main does not go red between merges.
  3. Two pairs of fix PRs add their fork.yaml section at the same line: fix(nn): keep the float16 causal mask finite #39 with fix(nn): make the deprecated SoftMax use the last axis #47, and fix(nn): use the normalized sum as post-norm decoder cross-attention queries #40 with fix(ops): default nanToNum infinities to the largest finite value #51. Git reports a conflict for the second PR of each pair. After the first PR of a pair merges, merge main into the branch of the second PR (no rebase), then merge it.
  4. Merge fix(dtype): use float32 finfo for complex64 and fix bfloat16 subnormal #44 before fix(nn): keep the float16 causal mask finite #39. Until fix(dtype): use float32 finfo for complex64 and fix bfloat16 subnormal #44 merges, DType.complex64.finfo.min is the float64 minimum. In complex64 that value is -inf, so a complex64 mask from fix(nn): keep the float16 causal mask finite #39 is NaN at allowed positions (0 * -inf).

Upstream status

Upstream ml-explore/mlx-swift main already has the fixes of #44, #48, #49 and #51. It still has the defects that #39, #40, #43, #47 and #50 fix.

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes (ran swift format format --in-place --configuration .swift-format on the changed files)
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed) (fork.yaml; check_forkdiff.py passes)

🤖 Generated with Claude Code

roll(_:shift:axes:) passed one shift value for all the axes, so MLX raised an error when there were two or more axes. Repeat the shift for each axis, as Python mx.roll does for an integer shift. RollMultipleAxesTests covers it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Gajesh2007
Gajesh2007 merged commit 6b7df2b into main Oct 2, 2026
6 checks passed
@davidtai
davidtai deleted the fix/roll-multiple-axes branch October 4, 2026 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants