Skip to content

Commit 1f1f922

Browse files
authored
CompiledFunction is not thread safe (#226)
- fixes #225 - although CompiledFunction was marked as unchecked(Sendable) it really wasn't - guard the state with a lock
1 parent df5d5c7 commit 1f1f922

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

Source/MLX/Transforms+Compile.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ final class CompiledFunction: @unchecked (Sendable) {
99
/// unique (for the lifetime of the object) identifier for the compiled function
1010
private var id: UInt!
1111

12+
let lock = NSLock()
13+
1214
/// the function to compile
1315
let f: ([MLXArray]) -> [MLXArray]
1416

@@ -35,6 +37,12 @@ final class CompiledFunction: @unchecked (Sendable) {
3537
}
3638

3739
func call(_ arguments: [MLXArray]) -> [MLXArray] {
40+
lock.withLock {
41+
innerCall(arguments)
42+
}
43+
}
44+
45+
func innerCall(_ arguments: [MLXArray]) -> [MLXArray] {
3846
let stateInputs = inputs.flatMap { $0.innerState() }
3947
let argumentsCount = arguments.count
4048

0 commit comments

Comments
 (0)