Skip to content

ci: cache the Metal library and run the tests in parallel shards - #60

Merged
davidtai merged 4 commits into
sync/upstream-2026-10-22from
ci/parallel-tests
Oct 23, 2026
Merged

davidtai merged 4 commits into
sync/upstream-2026-10-22from
ci/parallel-tests

Conversation

@davidtai

@davidtai davidtai commented Oct 23, 2026 •

Copy link
Copy Markdown

Proposed changes

This PR makes the Fork Tests workflow faster. On a warm cache, the run takes about 8 minutes. The old single job took 26 minutes. Each test still runs exactly once. No test is skipped, removed or changed.

Stacked on #59 (sync/upstream-2026-10-22). The base of this PR is that branch.

Design

The old workflow had one job. It built the package (6.9 min), then built the Metal library (8.2 min), then ran all tests (9.5 min plus 0.9 min for the ring tests), one after the other.

The new workflow has these jobs:

Job Runner What it does
Build the package and its tests macos-26 Builds the package and its tests one time with ccache. Packs the test bundle, the package state and the dependency checkouts into a tar. Uploads the tar as an artifact.
Build the full Metal library macos-26 Runs at the same time as the build job. Restores mlx.metallib from the cache, or builds it. Uploads it as an artifact.
Test shard 1..3 macos-26 Downloads the two artifacts. Copies the Metal library into the test bundle. Lists the tests, plans the shards and runs the tests of its shard.
build-and-test macos-26 Keeps the old check name. Fails if a job before it failed. Checks that every test ran exactly once. Merges the coverage profiles of all shards and writes the coverage report.

A run uses 6 macOS jobs. At most 3 run at the same time. On a pull request, a new push cancels the older run (cancel-in-progress), so the older run frees its runners.

The shared Select the newest release of Xcode 26 steps are now one composite action, .github/actions/select-xcode. The Lint workflow uses it too.

scripts/build-metallib.sh has two new modes. build FILE builds the library and writes it to FILE. install FILE copies FILE into the test bundles. With no arguments, the script works as before.

Shard map and how it stays complete

.github/scripts/test-shards.json has the shard count (3) and the measured durations from run 39876188612. .github/scripts/test-shards.py plan makes the plan:

  • Each XCTest class is one unit.
  • A Class/method key in seconds makes that method its own unit. Today this is only SortedGatherQuantizedMMTests/testQwenSortedGatherQuantizedMMMatchesLegacyOnRandomTensors (208 s in the baseline).
  • ProcessAllocationCapTests, DistributedRingTests and DistributedNNRingTests are one unit. They run in their own processes with their own environment, as before.
  • All Swift Testing tests are one unit. They run in parallel in one process.
  • A class with no measured duration costs 0.5 s. A new test class therefore goes into a shard with no change to the file.
  • The planner gives each unit, longest first, to the shard with the least work. Ties go by name, so the plan does not change from run to run.

The plan comes from swift test list on the built bundle. It does not come from a fixed list. Thus a new test is always in a shard.

Plan of the last run (estimated seconds from the baseline):

Shard Estimated seconds XCTest tests Isolated processes Swift Testing Largest units
1 213 86 no no the Qwen sorted-gather test, GraphUtilsTests, LossTests
2 213 334 no yes Swift Testing, MemoryTests, SortedGatherQuantizedTailTests
3 213 426 yes no CompileLockOrderingTests, the isolated processes, CompileOverloadsTests

The guard

There are two checks:

  1. test-shards.py plan fails when a listed test is in no shard or in two or more shards. It also fails when a class in isolated is not in the list. A stale duration gives a warning only, because it does not change which tests run. Each shard makes the same plan. The final job checks that all plans are the same.
  2. test-shards.py verify in the final job reads the logs of all shards. It fails when a listed XCTest test did not run, ran two or more times, failed, or skipped without permission. Only QwenExpertTilePerfTests/testQwenRoutedPrefillUnitTimings and SortedGatherQuantizedMMTests/testQwenSortednessViolationRetractsToLegacyRoute may skip, as before. Each ring test must pass in every rank and run in no other process. For Swift Testing, the count in the summary line must equal the listed count, and every result must be passed.

Each shard also keeps the old checks: the fresh-process allocation-cap check, the ring-suite checks, the zero-tests check and the unexpected-skip check.

Guard result on every green run of this PR: XCTest: 849 listed, 847 results outside the ring ranks, 2 ring tests. Swift Testing: 749 listed. 847 + 2 = 849. Every listed test ran exactly once.

Cache keys

Cache Key Restore keys Size
Metal library macOS-metallib-xcode-<version>-mlx-<mlx gitlink>-<hash of scripts/build-metallib.sh and Source/Cmlx/mlx-generated/**> none (exact only) 25.5 MB (219 MB raw)
Build (ccache and SwiftPM checkouts) macOS-build-xcode-<version>-mlx-<mlx gitlink>-mlxc-<mlx-c gitlink>-<hash of Package.swift and Source/** without the submodules> the same key without the source hash, then the same Xcode version only 52 MB

The old cache saved .build (291 MB) and only on main. It missed on every PR that changed Package.swift. The new build cache stores ccache objects. ccache finds an object by a hash of its compile, so an older cache gives correct objects for the files that did not change. After the build, ccache removes the objects that the build did not use, so the cache does not grow. Both caches save on the pull request and on main. A pull request can read the caches of main, of its base branch and of its own branch.

The C and C++ core compiles through a clang wrapper in CC that calls ccache. The Swift compile does not use ccache, and it is most of the warm build time (about 2 min).

Measured times

All runs are on hosted macos-26. Times are job wall-clock from the GitHub API. "Run" is from the run start to the end of the last job.

Job Before: run 39876188612 Cold, first run: 39900086130 Warm: 39900634809 Cold, new mlx pin: 39914755072 Warm, same commit: 39914755072 attempt 2
Build the package and its tests 6.9 min (one job) 4.3 min build step, 0/194 ccache hits 3.4 min (2.5 min build step, 194/194 hits) 6.5 min (5.6 min build step, 62/194 hits) 4.1 min (3.1 min build step, 194/194 hits)
Build the full Metal library 8.2 min (one job) 3.7 min (miss) 0.3 min (hit) 6.3 min (miss) 0.4 min (hit)
Tests 9.5 min + 0.9 min ring failed at listing (fixed) shards 2.9, 3.0, 4.0 min shards 3.8, 5.5, 3.3 min shards 3.7, 3.7, 3.6 min
build-and-test (verify, coverage) (part of the one job) not run 0.5 min 0.8 min 0.3 min
Run 26.6 min 8.1 min 12.9 min 8.3 min

The cold run on the new mlx pin is the slow case. It happens only on a re-pin. In that run, the Metal library download in shard 2 took 51 s. In the other runs it took 4 to 6 s.

Long tests in the warm run: the Qwen sorted-gather test 112 s, CompileLockOrderingTests/testConcurrentDeinitErasesAndCompiledCalls 63 s, MemoryTests/testResourceCountStaysUnderLimitUnderChurn 19 s (43 s in the cold run). The churn test no longer controls the run time.

The test-products artifact is 275 MB before compression and 49 MB after. It does not have the object files, the executables or the dSYMs of the example programs.

Not changed

  • The tests, their assertions and their environment (MLX_ENABLE_TF32=0, MLX_GATHER_QMM_EXPERT_SLICES=1, MLX_RESOURCE_LIMIT=50000, MLX_TEST_PROCESS_CAP=1, MLX_TEST_DISTRIBUTED=1).
  • The format-only Lint check and the Fork Diff Check.
  • The runners. All jobs use hosted macos-26.
  • Every action is pinned to a full commit SHA. The new actions are the same pins as before (actions/cache v6.1.0, actions/upload-artifact v4.6.2) and actions/download-artifact v4.3.0 (d3f86a106a0bac45b974a628896c90dbdf5c8093). ccache comes from Homebrew.

Checklist

Put an x in the boxes that apply.

  • 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 (no Swift files change)
  • I have added tests that prove my fix is effective or that my feature works (CI only. The CI runs on this PR are the proof.)
  • I have updated the necessary documentation (if needed) (fork.yaml describes the new build-metallib.sh modes)

An AI agent (Claude Code) wrote this PR. A person reviews and merges it.

🤖 Generated with Claude Code

davidtai and others added 4 commits October 22, 2026 23:23
Build the package and its tests one time, build or restore the Metal
library in a parallel job, and run the tests in shards planned from
measured durations. A guard fails when a listed test is in no shard or
in two shards, and a final job checks that every test ran exactly once.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Listing loads the test bundle, and the bundle needs the Metal library.
The build job does not have the library, so each shard now lists the
tests and makes the plan. The final job checks that the plans match.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The five example dSYMs are 670 MB of the 921 MB tar. The tests do not use them.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@davidtai
davidtai marked this pull request as ready for review October 23, 2026 07:28
@davidtai
davidtai merged commit ae06ce3 into sync/upstream-2026-10-22 Oct 23, 2026
14 checks passed
@davidtai
davidtai deleted the ci/parallel-tests branch October 23, 2026 13:57
jonathan308 added a commit that referenced this pull request Oct 26, 2026
…ack (#56)

Forward merge of the refreshed #54 (main 6959486 merged into the JACCL fixes
pin) into this branch. This branch already carried main up to the #58 squash
67dc752, so the new main content is the upstream ml-explore/mlx-swift merge
ce46846 (#59, with #60 and #62), the core and C repin with regenerated sources
(#63) and the wrapped external buffer test (#64).

Conflicts:
- Source/Cmlx/mlx and Source/Cmlx/mlx-c (gitlinks): kept this branch's pins,
  core 360d11b9 and mlx-c 8a50797b. They are restitched to the merged core and
  C heads separately.
- fork.yaml, "Submodule pins": one paragraph that states main's pins (mlx
  ff4bb35b, mlx-c 004c735f, merges of upstream main) and then what the
  cluster core and C add on top. Every other section merged without conflict;
  the cluster's startup-wait section is kept.

Merged without conflict and checked: the mirrored fast.h keeps the fenced
kernel and prepared launch declarations next to main's math-mode entry point,
distributed_group.h keeps the bootstrap, startup-wait and sealed-codec
declarations, and MLXArray.sharingArray() still calls copyContext(), which
main keeps. The tree is main plus this branch's own delta: the two gitlinks,
those two headers, MLXArray.swift, MLXFastKernel.swift,
docs/startup-wait-budget.md and fork.yaml.

Main's Transforms+Eval.swift now schedules under evalLock and waits outside
it (upstream ml-explore#474). withSerializedEvaluation still runs its body under
evalLock, so native evaluation, fences and collectives stay serialized against
scheduling; a Swift eval's wait no longer excludes them.

Tests (Ultra A, Xcode 27.1). These ran on this merge with the two follow-up
commits on top (swift-format and fork.yaml). The committed gitlinks are not
changed. For the build, the nested repos were checked out at merged heads:
core 2b5253ad (the core merge into mlx#35; 69c0a0f6 gave the same results)
and a temporary merge of mlx-c main 004c735f into 8a50797b. Its C sources
merge cleanly; CMakeLists.txt and fork.yaml are taken from 8a50797b.
- swift build --build-tests: complete.
- Fork Tests, all 3 shards of the CI plan, with an AOT mlx.metallib from
  that core:
  - XCTest: 852 listed, 848 passed, 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.
- check-generated-metal-sources.py passes against that core.
- The mirrored mlx/c headers are byte-identical to the merged C headers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jonathan308 added a commit that referenced this pull request Oct 26, 2026
Brings in main up to 6959486 (2026-10-24): the upstream ml-explore/mlx-swift
merge ce46846 (#59, with #60 and #62), the core and C repin with regenerated
sources (#63) and the wrapped external buffer test (#64).

Conflicts:
- Source/Cmlx/mlx and Source/Cmlx/mlx-c (gitlinks): kept this branch's pins,
  core fdae3174 and mlx-c 489e9654, as the cluster stack has them. Main pins
  core ff4bb35b and mlx-c 004c735f. The cluster pins are restitched to the
  merged core and C heads separately; the Swift layer of this merge needs
  main's C API and does not link against the old pins.

The merged tree is main plus this branch's own delta: the two gitlinks and the
mirrored distributed_group.h copies that declare
mlx_distributed_init_jaccl_with_bootstrap (main did not change those headers).

Tests (Ultra A, Xcode 27.1). The committed gitlinks are not changed. For
the build, the nested repos were checked out at merged heads: core
187b6c67 (the core merge of main f78f0fa9 into mlx#33) and a clean,
temporary merge of mlx-c main 004c735f into 489e9654.
- swift build --build-tests: complete.
- Fork Tests, all 3 shards of the CI plan, with an AOT mlx.metallib from
  that core:
  - XCTest: 852 listed, 848 passed, 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.
- check-generated-metal-sources.py passes against that core. At the
  committed core it fails, because main regenerated the quantized sources.
- check_forkdiff.py --upstream-ref upstream/main: 211 of 211 files
  described.
- Lint: this branch changes no Swift file against main.

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.

1 participant