Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ let noCudaCmlxExcludes = [
.headerSearchPath("mlx/mlx/distributed/jaccl/lib"),

.define("MLX_USE_ACCELERATE"),
.define("MLX_USE_JACCL", .when(traits: ["JACCL"])),
.define("ACCELERATE_NEW_LAPACK"),
.define("_METAL_"),
.define("SWIFTPM_BUNDLE", to: "\"mlx-swift_Cmlx\""),
Expand Down Expand Up @@ -320,7 +321,8 @@ let cmlx = Target.target(
"mlx/mlx/backend/metal/nojit_kernels.cpp",

// distributed backends: enable ring, disable MPI + NCCL. JACCL is
// built through mlx-conditional on macOS, so its sources are excluded here.
// built through mlx-conditional on macOS when the JACCL trait is
// enabled, so its sources are excluded here.
"mlx/mlx/distributed/mpi/mpi.cpp",
"mlx/mlx/distributed/ring/no_ring.cpp",
"mlx/mlx/distributed/nccl/nccl.cpp",
Expand Down Expand Up @@ -364,6 +366,14 @@ let package = Package(
.library(name: "MLXLinalg", targets: ["MLXLinalg"]),
.library(name: "MLXFast", targets: ["MLXFast"]),
],
traits: [
.trait(
name: "JACCL",
description:
"Builds the JACCL distributed backend (RDMA over Thunderbolt 5) on macOS. Disable it to leave the backend out of an app that does not run distributed MLX."
),
.default(enabledTraits: ["JACCL"]),
],
dependencies: [
// for Complex type
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.0")
Expand Down
7 changes: 4 additions & 3 deletions Source/Cmlx/mlx-conditional/jaccl_conditional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

// JACCL (RDMA over Thunderbolt 5) needs <infiniband/verbs.h>, which ships with
// the macOS 26.2 SDK and later. Build the backend and its library when the
// header is present, and the stub otherwise. librdma is loaded at runtime, so
// on an older macOS MLXDistributed.isAvailable(.jaccl) reports false.
#if TARGET_OS_OSX && __has_include(<infiniband/verbs.h>)
// JACCL package trait is enabled and the header is present, and the stub
// otherwise. librdma is loaded at runtime, so on an older macOS
// MLXDistributed.isAvailable(.jaccl) reports false.
#if TARGET_OS_OSX && defined(MLX_USE_JACCL) && __has_include(<infiniband/verbs.h>)
#include "../mlx/mlx/distributed/jaccl/jaccl.cpp"
#include "../mlx/mlx/distributed/jaccl/lib/jaccl/jaccl.cpp"
#include "../mlx/mlx/distributed/jaccl/lib/jaccl/mesh.cpp"
Expand Down
23 changes: 23 additions & 0 deletions Source/MLX/Cmlx+Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ func new_mlx_vector_array(_ arrays: some Collection<MLXArray>) -> mlx_vector_arr
}
}

/// Convert `mlx_vector_array` into a Swift array of `MLXArray`.
/// - Parameter vector_array: input vector
func mlx_vector_array_values(_ vector_array: mlx_vector_array) -> [MLXArray] {
(0 ..< mlx_vector_array_size(vector_array))
.map { index in
Expand All @@ -20,6 +22,27 @@ func mlx_vector_array_values(_ vector_array: mlx_vector_array) -> [MLXArray] {
}
}

/// Convert `mlx_vector_array` into a Swift array of `MLXArray` and ensure
/// that there are at least `count` items in the result.
///
/// This variant is useful for callers that want to assume there are at
/// least `count` values in the result, even though an error condition might
/// result in fewer.
///
/// Note: the extra values supplied after the actual length of the vector
/// are `mlxNone` and are not typically usable.
///
/// - Parameters:
/// - vector_array: input vector
/// - count: minimum number of values in the result
func mlx_vector_array_values(
_ vector_array: mlx_vector_array, count: Int
) -> [MLXArray] {
var result = mlx_vector_array_values(vector_array)
while result.count < count { result.append(.mlxNone) }
return result
}

func mlx_map_array_values(_ mlx_map: mlx_map_string_to_array) -> [String: MLXArray] {
var result = [String: MLXArray]()

Expand Down
2 changes: 2 additions & 0 deletions Source/MLX/Distributed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public enum MLXDistributed {
case ring
case mpi
case nccl
/// RDMA over Thunderbolt 5, on macOS only. Built when the `JACCL`
/// package trait is enabled, which is the default.
case jaccl
}

Expand Down
16 changes: 16 additions & 0 deletions Source/MLX/Documentation.docc/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ dependencies: [.product(name: "MLX", package: "mlx-swift"),
> Note that SwiftPM cannot build the metal shaders so the ultimate build has to be done via
Xcode or `xcodebuild`.

## Package Traits

`JACCL` builds the JACCL distributed backend (RDMA over Thunderbolt 5) on
macOS. It is enabled by default. iOS, tvOS and visionOS never build it.

An app that does not run distributed MLX can leave the backend out of its
binary by disabling the default traits:

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

SwiftPM enables the union of the traits that all dependents request. If another
package in the graph depends on `mlx-swift` with the default traits, `JACCL`
stays enabled.

## Command Line Tools

See <doc:troubleshooting> (Running From Command Line) for information about running command line tools from the shell.
Expand Down
6 changes: 3 additions & 3 deletions Source/MLX/Linalg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public enum MLXLinalg {
mlx_linalg_svd(&vec, array.ctx, true, stream.ctx)
defer { mlx_vector_array_free(vec) }

let arrays = mlx_vector_array_values(vec)
let arrays = mlx_vector_array_values(vec, count: 3)
return (arrays[0], arrays[1], arrays[2])
}

Expand All @@ -242,7 +242,7 @@ public enum MLXLinalg {
mlx_linalg_svd(&vec, array.ctx, false, stream.ctx)
defer { mlx_vector_array_free(vec) }

let arrays = mlx_vector_array_values(vec)
let arrays = mlx_vector_array_values(vec, count: 1)
return arrays[0]
}

Expand Down Expand Up @@ -369,7 +369,7 @@ public enum MLXLinalg {
var vec = mlx_vector_array_new()
mlx_linalg_lu(&vec, a.ctx, stream.ctx)
defer { mlx_vector_array_free(vec) }
let arrays = mlx_vector_array_values(vec)
let arrays = mlx_vector_array_values(vec, count: 3)
return (arrays[0], arrays[1], arrays[2])
}

Expand Down
20 changes: 19 additions & 1 deletion Source/MLX/MLXArray+Indexing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import Cmlx
import Foundation

private let log = MLXLogger(label: "MLXArray+Indexing")

private func int32Range(_ count: Int) -> [Int32] {
Array(0 ..< Int32(count))
}
Expand Down Expand Up @@ -334,6 +336,13 @@ extension MLXArray {
// conform the shapes to each other
arrayIndices = MLX.broadcast(arrays: arrayIndices)

guard !arrayIndices.isEmpty else {
// if we get to this point it means that there is an error
// handler that will catch the error
MLX.log.error("broadcast failed in subscript set")
return
}

// the shape of the broadcast value
let broadcastShape =
arrayIndices[0].shape.asInt32 + int32Range(axis + 1, self.ndim).map { dim($0) }
Expand Down Expand Up @@ -1154,7 +1163,16 @@ func scatterArguments(
}

// Broadcast the update to the indices and slices
arrayIndices = broadcast(arrays: arrayIndices)
arrayIndices = broadcast(arrays: arrayIndices, stream: stream)

guard !arrayIndices.isEmpty else {
// if we get to this point it means that there is an error
// handler that will catch the error
MLX.log.error("broadcast failed in scatterArguments")
// empty indices + src as the update -> caller does src._updateInternal(src), a no-op
return ([], src, [])
}

let updateShapeBroadcast =
arrayIndices[0].shape + sliceShapes + src.shape.dropFirst(nonNewAxisOperationCount)
update = broadcast(update, to: updateShapeBroadcast, stream: stream)
Expand Down
2 changes: 1 addition & 1 deletion Source/MLX/MLXArray+Ops.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2767,7 +2767,7 @@ extension MLXArray {
var vec = mlx_vector_array_new()
mlx_split(&vec, ctx, 2, axis.int32, stream.ctx)
defer { mlx_vector_array_free(vec) }
let pieces = mlx_vector_array_values(vec)
let pieces = mlx_vector_array_values(vec, count: 2)
return (pieces[0], pieces[1])
}

Expand Down
2 changes: 1 addition & 1 deletion Source/MLX/MLXFast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public enum MLXFast {
(mask ?? .mlxNone).ctx,
stream.ctx)

let tuple = mlx_vector_array_values(result)
let tuple = mlx_vector_array_values(result, count: 2)
return (output: tuple[0], state: tuple[1])
}

Expand Down
2 changes: 1 addition & 1 deletion Source/MLX/Ops+Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1684,7 +1684,7 @@ public func split(_ array: MLXArray, axis: Int = 0, stream: StreamOrDevice = .de
var vec = mlx_vector_array_new()
mlx_split(&vec, array.ctx, 2, axis.int32, stream.ctx)
defer { mlx_vector_array_free(vec) }
let pieces = mlx_vector_array_values(vec)
let pieces = mlx_vector_array_values(vec, count: 2)
return (pieces[0], pieces[1])
}

Expand Down
12 changes: 7 additions & 5 deletions Source/MLX/Ops.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ public func depends(input: MLXArray, dependencies: [MLXArray]) -> MLXArray {
defer { mlx_vector_array_free(dependencies) }
mlx_depends(&result, inputs, dependencies)

let arrays = mlx_vector_array_values(result)
let arrays = mlx_vector_array_values(result, count: 1)
return arrays[0]
}

Expand Down Expand Up @@ -1235,7 +1235,7 @@ public func divmod(
var vec = mlx_vector_array_new()
mlx_divmod(&vec, a.ctx, b.ctx, stream.ctx)
defer { mlx_vector_array_free(vec) }
let result = mlx_vector_array_values(vec)
let result = mlx_vector_array_values(vec, count: 2)
return (result[0], result[1])
}

Expand Down Expand Up @@ -2510,7 +2510,7 @@ public func quantized(
(globalScale ?? .mlxNone).ctx,
stream.ctx)

let arrays = mlx_vector_array_values(r)
let arrays = mlx_vector_array_values(r, count: 2)
return (arrays[0], arrays[1], arrays.count > 2 ? arrays[2] : nil)
}

Expand Down Expand Up @@ -3061,7 +3061,7 @@ public func uniqued(
(fillValue ?? .mlxNone).ctx,
stream.ctx)

let arrays = mlx_vector_array_values(vector)
let arrays = mlx_vector_array_values(vector, count: 1)
return arrays[0]
}

Expand Down Expand Up @@ -3159,7 +3159,9 @@ public func uniqued(
(fillValue ?? .mlxNone).ctx,
stream.ctx)

let arrays = mlx_vector_array_values(vector)
let expectedCount =
1 + [UniqueOptions.index, .inverse, .counts].filter { returning.contains($0) }.count
let arrays = mlx_vector_array_values(vector, count: expectedCount)
var result = UniqueResult(values: arrays[0])

var readIndex = 1
Expand Down
Loading