Skip to content

test: cover the ops free functions, operators and methods - #41

Merged
Gajesh2007 merged 7 commits into
mainfrom
test/ops-coverage
Oct 2, 2026
Merged

Gajesh2007 merged 7 commits into
mainfrom
test/ops-coverage

Conversation

@davidtai

@davidtai davidtai commented Oct 1, 2026 •

Copy link
Copy Markdown

Proposed changes

Line coverage on the base (run 36587565184) was low for the three op files:

File Lines Missed Cover
Source/MLX/Ops.swift 985 529 46.29%
Source/MLX/Ops+Array.swift 464 154 66.81%
Source/MLX/MLXArray+Ops.swift 695 183 73.67%

This PR adds tests only. It does not change library code.

New files:

  • Tests/MLXTests/OpsCoverageTests.swift (50 tests): the free functions in Ops.swift. This includes arithmetic, divmod, addMM, clip, nanToNum, the trigonometric and hyperbolic functions, erfInverse, the is* checks, comparison and logical functions, where/which, the shape functions (atLeast*D, padded, tiled, roll, tril/triu, flatten/unflatten, meshGrid), sort, partition and top, takeAlong/putAlong, median, std, trace, inner/outer/kron, the three tensordot forms, einsum, blockMaskedMM, gatherMM, segmentedMM, the window functions, softmax, the convolutions (1D, 2D, 3D, general, transposed) and convolve. The deprecated wrappers (addmm, softMax, gatherMatmul, quantizedMatmul, gatherQuantizedMatmul) are called too.
  • Tests/MLXTests/MLXArrayOpsCoverageTests.swift (26 tests): each operator in MLXArray+Ops.swift with every overload (array and array, array and scalar, scalar and array, and the compound assignments), and each free function in Ops+Array.swift together with its MLXArray method.

Each test uses a small array. It checks shape, dtype and values against values computed by hand or by a plain Swift loop (for example, the convolutions, kron, gatherMM and segmentedMM). Tolerances are used only where the math is not exact.

Defects found

Four tests find defects in the Swift wrappers. The failing assertions are wrapped in XCTExpectFailure, with a comment that names the defect. Each has its own fix PR, based on main:

  • Source/MLX/Ops.swift:1006, convolve(_:_:mode:) with .same and an even kernel: the right pad is max(0, padLeft / 2 - 1). Python MLX (python/src/ops.cpp) uses max(0, pad_l - 1). For a kernel of size 4 the output has one element less than max(len(a), len(v)). Test: testConvolveSameEvenKernel. Fix: fix(ops): give convolve .same the right length for an even kernel #48.
  • Source/MLX/Ops.swift:2032, median(_:keepDims:): it passes an empty axes list to mlx_median. MLX then raises [flatten] start_axis must be less than or equal to end_axis for every array with 1 or more dimensions. The doc comment and Python mx.median(a) reduce over all axes. Test: testMedianAllAxes. Fix: fix(ops): reduce over all axes in median(_:keepDims:) #49.
  • Source/MLX/Ops.swift:2595, roll(_:shift:axes:): it passes one shift value for all the axes. MLX raises [roll] At least one shift value per axis is required when there are two or more axes. Python mx.roll(a, 1, axis=(0, 1)) applies the shift to each axis. Test: testRollMultipleAxes. Fix: fix(ops): pass one shift for each axis in roll(_:shift:axes:) #50.
  • Source/MLX/Ops.swift:2102, nanToNum: posInf and negInf default to 0. The doc comment says that when they are not given, the largest finite value is used, as in Python MLX (posinf=None). Test: testNanToNumDefaults. Fix: fix(ops): default nanToNum infinities to the largest finite value #51.

The error tests use withError, so an MLX error becomes a test failure, not a crash. The fix PRs merge before this PR. Before this PR merges, remove the wrappers (see the checklist below).

Coverage after this PR (run 36810978852)

File Lines Missed Cover
Source/MLX/Ops.swift 985 28 97.16%
Source/MLX/Ops+Array.swift 464 0 100.00%
Source/MLX/MLXArray+Ops.swift 695 0 100.00%

The three files lose 838 missed lines in total.

fork.yaml: the Tests section text now names the new test files.

Before this PR merges: remove the wrappers

The fix PRs (#48, #49, #50 and #51) merge first. The wrappers stay on this branch until then, because the fixes are not on its base. After the fixes have merged, merge main into this branch (no rebase), do the steps below, wait for Fork Tests, and then merge this PR. XCTest reports an unexpected pass inside XCTExpectFailure as a failure, so this PR must not merge with the wrappers after the fixes are on main. Line numbers are at head 218abdc. In each step, remove the XCTExpectFailure(...) { line and its closing }, keep the assertions inside, and delete the "Defect" comment above it.

Base and merge order. This PR is based on ci/coverage-scope (#35). It merges after #35. It does not depend on #36, #37, #38 or the sibling coverage PRs. If a sibling test PR merges first, the only overlap is the text at the end of the Tests section in fork.yaml. Keep both lines.

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; no diff)
  • 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

davidtai and others added 3 commits September 29, 2026 09:06
The job summary now shows one table with Lines, Regions and Functions for
all of Source/MLX* and for MLX, MLXNN and MLXOptimizers, each against the
80% target. The target is reported only. The text under the table names
what is not measured: the Cmlx core and the targets that the test bundle
does not link.

The step reads each value from the TOTAL row by its column name and fails
when a value is missing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MLXFast, MLXFFT, MLXLinalg and MLXRandom are deprecated shims that forward
to APIs in Source/MLX. Those APIs are counted in the Source/MLX row, so the
text no longer reads as if FFT, linalg and random were unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add OpsCoverageTests (Source/MLX/Ops.swift) and MLXArrayOpsCoverageTests
(Source/MLX/Ops+Array.swift and Source/MLX/MLXArray+Ops.swift). Each test
calls the free function, the operator overloads with a scalar on each side
and the method forms on small arrays. It compares shape, dtype and values
with values computed by hand or by plain Swift loops.

Four tests find defects in the Swift wrappers. Their failing assertions are
wrapped in XCTExpectFailure:

- convolve .same with an even kernel of size 4 or more pads the right side
  with padLeft / 2 - 1, not padLeft - 1 (Ops.swift:1006).
- median(_:keepDims:) passes no axes, so MLX raises an error (Ops.swift:2032).
- roll(_:shift:axes:) passes one shift value for more than one axis, so MLX
  raises an error (Ops.swift:2595).
- nanToNum defaults posInf and negInf to 0, not to the largest finite
  value that its doc comment gives (Ops.swift:2102).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
XCTestCase has an add(_:) method, which hides the free function.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
davidtai and others added 2 commits September 30, 2026 22:32
The sum of squared deviations of the 8 values in testStd and testVariance
is 5.5, not 3.5. The GPU pow gives 8.999998 for 3 ** 2, so the pow checks
now use a relative tolerance.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Gajesh2007 Gajesh2007 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed test-only scope at head f95c382. Signed main integration preserves all fork.yaml topic entries, all four known-defect XCTExpectFailure markers, and the full original test blobs. No runtime Source changes or inherited CI regressions. New composed CI is pending; user explicitly authorizes proceeding without waiting.

@Gajesh2007
Gajesh2007 merged commit ff671f3 into main Oct 2, 2026
7 checks passed
@davidtai
davidtai deleted the test/ops-coverage branch October 4, 2026 22:00
jonathan308 added a commit that referenced this pull request Oct 26, 2026
The PR branch moved on GitHub after this refresh started: 3832c0d pins
Source/Cmlx/mlx to d11238f1 (Layr-Labs/mlx#41), the core that keeps
precompiled MLX builds on 32 x 32 steel tiles. Merging it makes this branch
a fast-forward of the published head. The merge is clean, and the gitlink
moves 2e16455f -> d11238f1. The forward merge of #56 and the restitch to the
merged #41 core (which contains d11238f1) follow.

Tests: run on the restitch commit that follows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jonathan308 added a commit that referenced this pull request Oct 26, 2026
…anch (#61)

Brings #56's forward merge of the restitched #54 and #56's own restitch
commit 3e1fc78 forward, so that this branch contains its base again.

Conflicts, resolved to this branch's side. The restitch commit that follows
sets this layer's pins and regenerates from them:
- Source/Cmlx/mlx (gitlink): kept d11238f1, this branch's #41 pin. #56 has
  its own core, 2b5253ad.
- fork.yaml, "Submodule pins" paragraph: kept this branch's paragraph.

Taken from #56 without conflict:
- Source/Cmlx/mlx-c moves 8a50797b -> 73720efb, the merge of mlx-c main into
  mlx-c#15. This layer pins the same C layer.
- The regenerated include-framework headers.

Tests: run on the restitch commit that follows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jonathan308 added a commit that referenced this pull request Oct 26, 2026
Restitches the nested pins after the merge of main 6959486:
- Source/Cmlx/mlx: d11238f1 -> ce21a37d. That is the merge of the refreshed
  mlx#35 into Layr-Labs/mlx#41, a fast-forward of the published d11238f1,
  plus the fix of the head-dim split NAX attention's run-time template.
  Without that fix, the JIT build that mlx-swift uses throws "Unable to
  build metal library from source" for head-dim 256 and 512 causal or masked
  prefill on NAX GPUs.
- Source/Cmlx/mlx-c: 73720efb (taken from #56), the merge of mlx-c main into
  mlx-c#15.

Regenerated as tools/update-mlx.sh does from those pins:
- The mirrored Source/Cmlx/include headers, the embedded JIT and CPU
  sources and the Xcode project match the regeneration.
- The include-framework copies of the core's metal/device.h and utils.h gain
  #41's steel-tile declarations: thread_qmm_steel_tile, qmm_steel_tile and
  its minimum-groups setting.
- Metal.hpp differs only in its "Autogenerated" line and is kept as it is.

fork.yaml's pin paragraph names the merged core and C.

Tests (Ultra A, Xcode 27.1, at these pins):
- tools/update-mlx.sh regeneration from the two pins matches the tree
  (Metal.hpp aside, as above).
- check-generated-metal-sources.py: pass.
- swift build --build-tests: complete.
- Fork Tests, all 3 shards of the CI plan, with an AOT mlx.metallib from
  this core:
  - XCTest: 853 listed, 849 passed (including HadamardLayerTests and
    Float16ConstantCastTests), and the 2 permitted skips.
  - The fresh-process allocation-cap test and both multi-process ring
    suites passed.
  - Swift Testing: 749 of 749 passed.
  - test-shards.py verify: every test ran exactly once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jonathan308 added a commit that referenced this pull request Oct 27, 2026
Source/Cmlx/mlx -> ea318ee0 (core #41 with main 6bc9de5b merged; its head-dim split JIT fix now reads as main's); mlx-c stays 73720efb.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
davidtai added a commit that referenced this pull request Oct 27, 2026
Source/Cmlx/mlx moves from ea318ee0 to e637499f, the head of
Layr-Labs/mlx#41: the fast fence is updated before the event fence,
fenced inputs stay in device memory and take their waits first, a
sealed frame over INT_MAX is refused, and the JACCL mesh shares its
failure flag across wires and does all_gather offsets in 64 bits.
The range changes .cpp files that the package already compiles and
headers that it does not mirror (distributed/sealed.h, sealed_stream.h,
jaccl/lib). No Metal kernel, JIT source, CPU preamble input or CUDA
device file changes, so mlx-generated does not change. The Metal.hpp
commit stamp is written only by a cmake run and is left as it is.

Source/Cmlx/mlx-c moves from 73720efb to 2ac897e3, the head of
Layr-Labs/mlx-c#15. Only comments in mlx/c/distributed_group.h change;
both mirrored copies are refreshed as tools/update-mlx.sh and
tools/update-mlx-xcodeproj.sh copy them. The other 28 headers are
unchanged byte for byte.

fork.yaml: the Submodule pins prose names the new pins and fixes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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