Skip to content

Add a JACCL package trait to opt out of the JACCL backend - #517

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
aleroot:alessio/jaccl-trait
Oct 28, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
aleroot:alessio/jaccl-trait

Conversation

@aleroot

@aleroot aleroot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Proposed changes

Follow up on #502, a new JACCL package trait, enabled by default:

.package(url: "https://github.com/ml-explore/mlx-swift", from: "0.33.0", traits: [])

An app that does not run distributed MLX can now leave the JACCL backend (RDMA over Thunderbolt 5) out of its binary.

Why

Since #502 every macOS build whose SDK ships <infiniband/verbs.h> compiles JACCL into Cmlx, and the linker cannot dead strip it: distributed::init("any") falls back to jaccl::init, and MLXDistributed.Group and the sharded MLXNN layers keep that path reachable. So every macOS app pays for the backend, although JACCL forms a group only on a Thunderbolt 5 cluster configured through the JACCL_* environment variables.

Most apps that embed MLX never do that: a chat app, an on-device assistant, an iPhone app that also ships a Mac version. For them JACCL is about 1 MB of code they can never run, and today they have no way to opt out. Apps distributed through the App Store, where download size matters, feel this most.

The trait also makes the choice explicit. Today the only gate is __has_include(<infiniband/verbs.h>), so whether an app gets JACCL depends on which SDK it is built with, not on what the app needs. iOS, tvOS and visionOS build the stub now only because their SDKs do not ship the header. With the trait, an app states what it wants once.

mlx-swift-lm

SwiftPM enables the union of the traits that dependents request. mlx-swift-lm depends on mlx-swift with the default traits, so for its consumers JACCL stays enabled even if the app itself passes traits: []. If this set of changes is accepted, a follow up in mlx-swift-lm can forward the choice with a trait of its own, in the same way as FoundationModelsIntegration, so that an app built on mlx-swift-lm can control this too. Most apps reach MLX through mlx-swift-lm, so that is where the saving lands in practice.

Measured sizes

Minimal executable linking MLX and MLXNN, release, -dead_strip, stripped, Xcode 27.1:

JACCL trait Binary __text
enabled (default) 14,838,504 B 10,494,900 B
disabled 13,773,224 B 9,653,700 B
Delta −1,065,280 B −841,200 B

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
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

@aleroot

aleroot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

@davidkoski please have look and see if it makes sense. I think not every app needs that extra megabyte...

@davidkoski

Copy link
Copy Markdown
Member

Yeah, seems reasonable.

Since ml-explore#502 every macOS build whose SDK ships <infiniband/verbs.h> links
the JACCL distributed backend (RDMA over Thunderbolt 5), and dead
stripping cannot remove it: `distributed::init("any")` reaches
`jaccl::init`, and `MLXDistributed.Group` and the sharded MLXNN layers
keep that path alive. An app that never forms a Thunderbolt cluster has
no way to opt out. On a minimal MLX + MLXNN executable (release,
stripped) the backend costs 1,065,280 bytes, 841 KB of them `__text`.

Add a `JACCL` package trait, enabled by default, so an app can leave it
out with `traits: []`:

- `Package.swift` declares the trait and defines `MLX_USE_JACCL` for
  Cmlx when it is enabled. SwiftPM passes traits to Swift targets only,
  so C++ needs the explicit define.
- `jaccl_conditional.cpp` requires `MLX_USE_JACCL` and compiles the
  `no_jaccl.cpp` stub otherwise.
- `Cmlx.xcconfig` always defines `MLX_USE_JACCL`, so the Xcode project
  and its xcframeworks build as before.
- `install.md` documents the trait and that SwiftPM enables the union of
  the traits that dependents request.

The define follows the `MLX_USE_ACCELERATE` naming.

Default on keeps the behaviour of ml-explore#502. The define states only the
choice; platform and SDK gating stay in `jaccl_conditional.cpp`, so iOS,
tvOS and visionOS keep building the stub whatever the trait. The CMake
build keeps mlx's own JACCL gating.
@aleroot
aleroot force-pushed the alessio/jaccl-trait branch from 9366aff to f593381 Compare October 28, 2026 16:29

@davidkoski davidkoski 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.

Changes look good, thank you.

@davidkoski
davidkoski merged commit 78d3cc1 into ml-explore:main Oct 28, 2026
7 checks passed
solderzzc pushed a commit to SharpAI/mlx-swift that referenced this pull request Oct 29, 2026
Upstream's JACCL package trait (ml-explore#517) needs tools-version
6.1 or later; the fork manifest was 5.12, so the manifest no longer parsed after
the sync. Keep Swift 5 language mode (what 5.12 implied) via swiftLanguageModes.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
ronaldmannak added a commit to ronaldmannak/mlx-swift that referenced this pull request Nov 15, 2026
Brings in main through a557dd6 (upstream ml-explore#510, ml-explore#512, ml-explore#513, ml-explore#514, ml-explore#517,
ml-explore#518, ml-explore#519, ml-explore#523).

Conflicts:
- CMakeLists.txt: keep the PicoMLX/mlx-c pico/integration pin.
- Cmlx.xcconfig: keep MLX_VERSION 0.32.5 and add main's MLX_USE_JACCL=1.
- ModuleTests.swift, DistributedNNTests.swift: take main's Swift Testing
  versions. The integration side held this fork's copies of ml-explore#510 and ml-explore#512,
  whose code matches main's; main has since converted those files.

The Cmlx/mlx and Cmlx/mlx-c submodules stay on pico/integration, which
already contain the v0.32.4 and v0.8.0 commits main points at.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
goodmai pushed a commit to goodmai/mlx-swift that referenced this pull request Nov 29, 2026
- Rebased onto main (5f117a2, keeping ml-explore#517 JACCL trait, ml-explore#518 empty vector array fix, ml-explore#519 Swift Testing)
- Bump Source/Cmlx/mlx submodule from 36969c09 (v0.32.4) to cfc675e9 (v0.32.5)
- Regenerate Source/Cmlx/mlx-generated and Source/Cmlx/include-framework via ./tools/update-mlx.sh
- Update tools/update-mlx-xcodeproj.sh for backend/common/custom_kernel.h + config.h (0 missing <Cmlx/...> headers)
- Override mlx-c/mlx/c/fast.cpp with Source/Cmlx/mlx-c-overrides/fast_v0325.cpp (1-line window_size = -1 fix from ml-explore/mlx-c#145) across macOS and Linux CUDA, excluding mlx-c-overrides on Linux CPU with no duplicate exclude
- Update MLX_VERSION to 0.32.5 in Package.swift and xcode/xcconfig/Cmlx.xcconfig
goodmai pushed a commit to goodmai/mlx-swift that referenced this pull request Nov 29, 2026
- Rebased onto main (5f117a2, keeping ml-explore#517 JACCL trait, ml-explore#518 empty vector array fix, ml-explore#519 Swift Testing)
- Bump Source/Cmlx/mlx submodule from 36969c09 (v0.32.4) to cfc675e9 (v0.32.5)
- Regenerate Source/Cmlx/mlx-generated and Source/Cmlx/include-framework via ./tools/update-mlx.sh
- Update tools/update-mlx-xcodeproj.sh for backend/common/custom_kernel.h + config.h (0 missing <Cmlx/...> headers)
- Override mlx-c/mlx/c/fast.cpp with Source/Cmlx/mlx-c-overrides/fast_v0325.cpp (1-line window_size = -1 fix from ml-explore/mlx-c#145) across macOS and Linux CUDA, excluding mlx-c-overrides on Linux CPU with no duplicate exclude
- Update MLX_VERSION to 0.32.5 in Package.swift and xcode/xcconfig/Cmlx.xcconfig
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