Skip to content

Commit 50ec1bf

Browse files
davidkoskiawni
andauthored
add withWiredLimit (#243)
* add withWiredLimit * Apply suggestions from code review Co-authored-by: Awni Hannun <awni@apple.com> --------- Co-authored-by: Awni Hannun <awni@apple.com>
1 parent d3f89b1 commit 50ec1bf

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

Source/MLX/GPU.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,52 @@ public enum GPU {
244244
}
245245
}
246246

247+
/// Perform the block with a temporarily altered wired memory limit.
248+
///
249+
/// Note: this manipulates a global value. Nested calls will work as expected but
250+
/// concurrent calls cannot.
251+
///
252+
/// See also ``DeviceInfo/maxRecommendedWorkingSetSize``.
253+
///
254+
/// - Parameters:
255+
/// - limit: new limit in bytes
256+
/// - body: block to perform
257+
public static func withWiredLimit<R>(
258+
_ limit: Int, _ body: () throws -> R
259+
) rethrows -> R {
260+
var current = 0
261+
mlx_set_wired_limit(&current, limit)
262+
defer {
263+
var tmp = 0
264+
mlx_set_wired_limit(&tmp, current)
265+
}
266+
267+
return try body()
268+
}
269+
270+
/// Perform the block with a temporarily altered wired memory limit.
271+
///
272+
/// Note: this manipulates a global value. Nested calls will work as expected but
273+
/// concurrent calls cannot.
274+
///
275+
/// See also ``DeviceInfo/maxRecommendedWorkingSetSize``.
276+
///
277+
/// - Parameters:
278+
/// - limit: new limit in bytes
279+
/// - body: block to perform
280+
public static func withWiredLimit<R>(
281+
_ limit: Int, _ body: () async throws -> R
282+
) async rethrows -> R {
283+
var current = 0
284+
mlx_set_wired_limit(&current, limit)
285+
defer {
286+
var tmp = 0
287+
mlx_set_wired_limit(&tmp, current)
288+
}
289+
290+
return try await body()
291+
}
292+
247293
/// Cause all cached metal buffers to be deallocated.
248294
public static func clearCache() {
249295
mlx_clear_cache()

Tests/MLXTests/GPUTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright © 2025 Apple Inc.
2+
3+
import Foundation
4+
import MLX
5+
import XCTest
6+
7+
class GPUTests: XCTestCase {
8+
9+
func testWiredMemory() {
10+
GPU.withWiredLimit(1024 * 1024 * 256) {
11+
let x = MLXArray(10)
12+
print(x * x)
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)