Skip to content

Commit 9e935bb

Browse files
committed
Fix MPSNDArray assertion on M5 Pro by enforcing 64-byte minimum buffer size (apple#61)
The decode output buffer was allocated at only 4 bytes (MemoryLayout<Int32>.size), but MPSNDArray enforces 64-byte row-stride alignment on all backing buffers. This triggered a fatal assertion on M5 Pro / macOS 27 beta 2.
1 parent 5a0f161 commit 9e935bb

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

swift/Sources/CoreAILanguageModels/InferenceEngines/CoreAIPipelinedEngine.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ private let pipelineDepth = 3
2828
private let averageExpectedPromptSize = 256
2929
private let temperatureTolerance: Double = 0.001
3030

31+
/// MPSNDArray enforces 64-byte row-stride alignment on backing buffers.
32+
private let minimumMPSNDArrayBufferSize = 64
33+
3134
// MARK: - Core AI Pipelined Engine (Public Wrapper)
3235

3336
/// GPU-pipelined inference engine using Core AI's encode API.
@@ -513,9 +516,10 @@ private struct EngineImpl: ~Copyable {
513516
// Allocate pipeline-depth-matched decode output buffers (sampler writes next token)
514517
var decodeOutBuffers: [MTLBuffer] = []
515518
for _ in 0..<pipelineDepth {
516-
guard let buf = device.makeBuffer(length: MemoryLayout<Int32>.size, options: .storageModeShared) else {
519+
let decodeOutSize = max(minimumMPSNDArrayBufferSize, MemoryLayout<Int32>.size)
520+
guard let buf = device.makeBuffer(length: decodeOutSize, options: .storageModeShared) else {
517521
throw InferenceRuntimeError.bufferAllocationFailed(
518-
"decodeOutputBuffer (\(MemoryLayout<Int32>.size) bytes)")
522+
"decodeOutputBuffer (\(decodeOutSize) bytes)")
519523
}
520524
decodeOutBuffers.append(buf)
521525
}

0 commit comments

Comments
 (0)