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
13 changes: 13 additions & 0 deletions Source/MLX/Transforms+Compile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ final class CompiledFunction: @unchecked (Sendable) {
evalLock.unlock()
}

// The backend keys its compile cache on, and builds the compiled
// primitive for, the C++ default stream, which never sees the
// task-scoped device from `Device.withDefaultDevice`. Without this, a
// function first traced on the GPU is reused inside a CPU-scoped call
// and runs its fused kernel on the GPU against CPU-stream inputs.
var previousDevice = mlx_device_new()
mlx_get_default_device(&previousDevice)
mlx_set_default_device(Device.defaultDevice().ctx)
defer {
mlx_set_default_device(previousDevice)
mlx_device_free(previousDevice)
}

let innerInputs = arguments + stateInputs
let innerInputsVector = new_mlx_vector_array(innerInputs)
defer { mlx_vector_array_free(innerInputsVector) }
Expand Down
30 changes: 30 additions & 0 deletions Tests/MLXTests/TransformTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,36 @@ class TransformTests: XCTestCase {
assertEqual(r1, r3)
}

func testCompileHonorsScopedDefaultDevice() {
// A function traced on the GPU must not be reused inside a CPU-scoped
// call: the backend caches compiled graphs per default stream, so the
// CPU call has to retrace for its own stream.
var traces = 0
let compiled = compile { (inputs: [MLXArray]) -> [MLXArray] in
traces += 1
return [square(inputs[0] * inputs[1])]
}

let i1 = MLXRandom.normal([20, 20])
let i2 = MLXRandom.normal([20, 20])
let expected = square(i1 * i2)

assertEqual(compiled([i1, i2])[0], expected)
XCTAssertEqual(traces, 1)

let cpuResult = Device.withDefaultDevice(.cpu) {
let r = compiled([i1, i2])[0]
eval(r)
return r
}
assertEqual(cpuResult, expected)
XCTAssertEqual(traces, 2)

// Back on the GPU, the original entry is reused.
assertEqual(compiled([i1, i2])[0], expected)
XCTAssertEqual(traces, 2)
}

class CompileTestState: CustomStringConvertible, Updatable {
var y: MLXArray
var o: MLXArray?
Expand Down
Loading