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
131 changes: 123 additions & 8 deletions Tests/MLXTests/AllocationFootprintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,107 @@ import Foundation
import MLX
import XCTest

/// Counts calls of a `MLXArray(rawPointer:)` finalizer. MLX can call it on
/// another thread, after the GPU commands that use the buffer complete.
final class FinalizerCount: @unchecked Sendable {
private let lock = NSLock()
private var value = 0

func increment() {
lock.lock()
value += 1
lock.unlock()
}

var current: Int {
lock.lock()
defer { lock.unlock() }
return value
}

/// Waits up to 5 s until `condition` is true.
static func wait(until condition: () throws -> Bool) rethrows {
let deadline = Date().addingTimeInterval(5)
while try !condition(), Date() < deadline { usleep(1_000) }
}
}

/// One page-aligned host page of Float 1.0. Metal wraps it without a copy.
func makeWrappableHostPage() -> (base: UnsafeMutableRawPointer, bytes: Int, count: Int) {
let bytes = Int(getpagesize())
let count = bytes / MemoryLayout<Float>.size
let base = UnsafeMutableRawPointer.allocate(byteCount: bytes, alignment: bytes)
base.initializeMemory(as: Float.self, repeating: 1, count: count)
return (base, bytes, count)
}

final class AllocationFootprintTests: XCTestCase {
/// Upstream mlx #4634 marks a wrapped external buffer as not owned by MLX.
/// `isUnique` must still be true while no other array refers to the buffer:
/// mlx-swift-lm requires it for wrapped paged-KV pages.
func testWrappedExternalBufferIsUniqueUntilShared() throws {
let (base, bytes, count) = makeWrappableHostPage()
let finalizer = FinalizerCount()
do {
let array = MLXArray(rawPointer: base, [count], dtype: .float32) {
finalizer.increment()
}
// If Metal refuses the wrap, MLX copies and calls the finalizer at once.
XCTAssertEqual(finalizer.current, 0, "MLX copied the page instead of wrapping it")
XCTAssertEqual(
UnsafeRawPointer(mlx_array_data_uint8(array.ctx)), UnsafeRawPointer(base))
let info = try XCTUnwrap(array.evaluatedBufferInfo())
XCTAssertTrue(info.isUnique)
XCTAssertTrue(info.isRowContiguous)
XCTAssertEqual(info.allocatedBytes, bytes)

let allocationStream = StreamOrDevice.default
let view = array[1 ..< count]
try withError {
eval(view)
allocationStream.stream.synchronize()
}
XCTAssertFalse(try XCTUnwrap(array.evaluatedBufferInfo()).isUnique)
XCTAssertFalse(try XCTUnwrap(view.evaluatedBufferInfo()).isUnique)
withExtendedLifetime((array, view)) {}
}
FinalizerCount.wait { finalizer.current > 0 }
XCTAssertEqual(finalizer.current, 1)
// Keep the page if MLX can still use it.
if finalizer.current == 1 { base.deallocate() }
}

/// Upstream mlx #4634: MLX does not donate a buffer that it does not own.
/// An op on a wrapped array writes a new allocation and the caller's page
/// does not change, also when the graph holds the only reference to it.
func testOpOnWrappedExternalBufferDoesNotWriteTheCallerPage() throws {
let (base, _, count) = makeWrappableHostPage()
let finalizer = FinalizerCount()
let result: MLXArray
do {
let wrapped = MLXArray(rawPointer: base, [count], dtype: .float32) {
finalizer.increment()
}
XCTAssertEqual(finalizer.current, 0, "MLX copied the page instead of wrapping it")
result = wrapped + 1
}
// An owned input with no other reference is donated to the output here.
let allocationStream = StreamOrDevice.default
try withError {
eval(result)
allocationStream.stream.synchronize()
}
XCTAssertNotEqual(
UnsafeRawPointer(mlx_array_data_uint8(result.ctx)), UnsafeRawPointer(base))
XCTAssertEqual(result.asArray(Float.self), [Float](repeating: 2, count: count))
let page = UnsafeBufferPointer(
start: base.assumingMemoryBound(to: Float.self), count: count)
XCTAssertEqual(Array(page), [Float](repeating: 1, count: count))
FinalizerCount.wait { finalizer.current > 0 }
XCTAssertEqual(finalizer.current, 1)
if finalizer.current == 1 { base.deallocate() }
}

func testUnscheduledMetadataQueryDoesNotEvaluate() throws {
let allocationStream = StreamOrDevice.default
let array = MLXArray.zeros([24_576], dtype: .uint8, stream: allocationStream)
Expand All @@ -14,14 +114,18 @@ final class AllocationFootprintTests: XCTestCase {
let after = Memory.snapshot()
XCTAssertEqual(after.activeMemory, before.activeMemory)
XCTAssertEqual(after.cacheMemory, before.cacheMemory)
try withError { eval(array); allocationStream.stream.synchronize() }
try withError {
eval(array)
allocationStream.stream.synchronize()
}
let info = try XCTUnwrap(array.evaluatedBufferInfo())
XCTAssertTrue(info.isUnique)
XCTAssertTrue(info.isRowContiguous)
XCTAssertEqual(info.dataElements, array.size)
XCTAssertEqual(info.dataOffset, 0)
XCTAssertGreaterThanOrEqual(info.allocatedBytes, array.nbytes)
XCTAssertLessThanOrEqual(info.allocatedBytes,
XCTAssertLessThanOrEqual(
info.allocatedBytes,
try Memory.allocationFootprintUpperBound(byteCount: array.nbytes))
}

Expand All @@ -31,7 +135,10 @@ final class AllocationFootprintTests: XCTestCase {
try withError { eval(array) }
let original = try XCTUnwrap(array.evaluatedBufferInfo())
let view = array[1024 ..< 2048]
try withError { eval(view); allocationStream.stream.synchronize() }
try withError {
eval(view)
allocationStream.stream.synchronize()
}
let sliced = try XCTUnwrap(view.evaluatedBufferInfo())
XCTAssertEqual(sliced.allocatedBytes, original.allocatedBytes)
XCTAssertGreaterThan(sliced.allocatedBytes, view.nbytes)
Expand All @@ -44,8 +151,13 @@ final class AllocationFootprintTests: XCTestCase {
func testBoundCoversFreshAndLargerCachedBuffers() throws {
let previousLimit = Memory.cacheLimit
Memory.cacheLimit = 2 << 20
defer { Memory.clearCache(); Memory.cacheLimit = previousLimit }
for (cachedBytes, requestedBytes) in [(48 << 10, 24_576), (8192, 6000), (64 << 10, 48 << 10)] {
defer {
Memory.clearCache()
Memory.cacheLimit = previousLimit
}
for (cachedBytes, requestedBytes) in [
(48 << 10, 24_576), (8192, 6000), (64 << 10, 48 << 10),
] {
Memory.clearCache()
try autoreleasepool {
let cached = MLXArray.zeros([cachedBytes], dtype: .uint8)
Expand All @@ -55,7 +167,8 @@ final class AllocationFootprintTests: XCTestCase {
try withError { eval(array) }
let info = try XCTUnwrap(array.evaluatedBufferInfo())
XCTAssertGreaterThanOrEqual(info.allocatedBytes, array.nbytes)
XCTAssertLessThanOrEqual(info.allocatedBytes,
XCTAssertLessThanOrEqual(
info.allocatedBytes,
try Memory.allocationFootprintUpperBound(byteCount: requestedBytes))
}
}
Expand All @@ -69,8 +182,10 @@ final class AllocationFootprintTests: XCTestCase {
func testDetachedPolicyMatchesAllocatorAtSizeClassBoundaries() throws {
let policy = try XCTUnwrap(Memory.allocationFootprintPolicy())
let extra = try XCTUnwrap(policy.maximumExtraBytes)
for bytes in [0, 1, 2, 4, 7, 8, 9, 4095, 4096, 4097, 8191, 8192, 8193,
16_383, 16_384, 16_385, 24_576, 32_767, 32_768, (64 << 20) + 1] {
for bytes in [
0, 1, 2, 4, 7, 8, 9, 4095, 4096, 4097, 8191, 8192, 8193,
16_383, 16_384, 16_385, 24_576, 32_767, 32_768, (64 << 20) + 1,
] {
let bound = try XCTUnwrap(policy.upperBound(byteCount: bytes))
XCTAssertEqual(bound, try Memory.allocationFootprintUpperBound(byteCount: bytes))
XCTAssertLessThanOrEqual(bound - bytes, extra)
Expand Down
34 changes: 34 additions & 0 deletions Tests/MLXTests/ProcessAllocationCapTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,39 @@ final class ProcessAllocationCapTests: XCTestCase {
let final = try Memory.processAllocationCapSnapshot()
XCTAssertEqual(final.pendingBytes, 0)
XCTAssertLessThanOrEqual(final.chargedBytes, final.limitBytes)

// A wrapped external page is charged while MLX holds it and refunded
// once when MLX releases it. MLX does not own it (upstream mlx #4634),
// so the release runs the wrapper deleter, not the allocator free.
try FinalizerCount.wait { try Memory.processAllocationCapSnapshot().unreclaimedBytes == 0 }
let baseline = try Memory.processAllocationCapSnapshot()
XCTAssertEqual(baseline.unreclaimedBytes, 0)
let (base, bytes, count) = makeWrappableHostPage()
let finalizer = FinalizerCount()
try autoreleasepool {
let wrapped = MLXArray(rawPointer: base, [count], dtype: .float32) {
finalizer.increment()
}
let held = try Memory.processAllocationCapSnapshot()
XCTAssertEqual(held.chargedBytes, baseline.chargedBytes + bytes)
XCTAssertEqual(held.liveBytes, baseline.liveBytes + bytes)
let doubled = wrapped * 2
eval(doubled)
XCTAssertEqual(doubled.asArray(Float.self), [Float](repeating: 2, count: count))
}
Memory.clearCache()
try FinalizerCount.wait {
let charged = try Memory.processAllocationCapSnapshot().chargedBytes
return finalizer.current > 0 && charged == baseline.chargedBytes
}
let released = try Memory.processAllocationCapSnapshot()
XCTAssertEqual(finalizer.current, 1)
XCTAssertEqual(released.chargedBytes, baseline.chargedBytes)
XCTAssertEqual(released.liveBytes, baseline.liveBytes)
XCTAssertEqual(released.cachedBytes, baseline.cachedBytes)
XCTAssertEqual(released.pendingBytes, 0)
XCTAssertEqual(released.unreclaimedBytes, 0)
// Keep the page if MLX can still use it.
if finalizer.current == 1 { base.deallocate() }
}
}
Loading