Skip to content
Merged
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
14 changes: 11 additions & 3 deletions Source/MLX/IO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public func save(array: MLXArray, url: URL, stream: StreamOrDevice = .default) t
switch url.pathExtension {
case "npy":
_ = try withError {
mlx_save(path.cString(using: .utf8), array.ctx)
_ = evalLock.withLock {
mlx_save(path.cString(using: .utf8), array.ctx)
}
}

default:
Expand Down Expand Up @@ -72,7 +74,9 @@ public func save(
switch url.pathExtension {
case "safetensors":
_ = try withError {
mlx_save_safetensors(path.cString(using: .utf8), mlx_arrays, mlx_metadata)
_ = evalLock.withLock {
mlx_save_safetensors(path.cString(using: .utf8), mlx_arrays, mlx_metadata)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

internally these functions prepare contiguous arrays and use eval to realize them. eval is not thread safe so the entire thing has to be run under the evalLock.

See #378 for the use case.

}
}

default:
Expand Down Expand Up @@ -283,7 +287,11 @@ public func saveToData(
let writer = new_mlx_io_writer_dataIO()
defer { mlx_io_writer_free(writer) }

mlx_save_safetensors_writer(writer, mlx_arrays, mlx_metadata)
_ = evalLock.withLock {
_ = evalLock.withLock {
mlx_save_safetensors_writer(writer, mlx_arrays, mlx_metadata)
}
}

return getData(writer)
}
Expand Down
Loading