Files
markbaseengine/Sources/MarkBase/BatchTemps.swift
T
MarkBase Admin ac75faa0cc
CI / build-and-test (push) Has been cancelled
Initial commit: E4B-MarkBase model integration with passing tests
- E4B-MarkBase model (42 layers, 4.4GB) loaded successfully
- All Phase 1-6 tests passed (model loading, forward pass, vision/audio towers, token generation, performance)
- All stress tests passed (5/5 in 127.6s)
  - Concurrent inference
  - Memory stress (67.5 tok/s, 0 NaN)
  - Continuous generation
  - Batch processing
  - Long-running stability
- Swift Metal inference engine with multimodal support
2026-06-23 18:12:35 +08:00

58 lines
2.3 KiB
Swift

import Metal
// ══════════════════════════════════════════════════════════════════
// Batch Forward Temps - Extended buffers for batch processing
// ══════════════════════════════════════════════════════════════════
extension ForwardTemps {
/// Create batch-specific temporary buffers
/// These are separate from single-token buffers to avoid interference
public func createBatchBuffers(
device: MTLDevice,
batchSize: Int,
hiddenSize: Int,
nHeads: Int,
headDim: Int,
intermediateSize: Int
) throws -> BatchTemps {
return try BatchTemps(
device: device,
batchSize: batchSize,
hiddenSize: hiddenSize,
nHeads: nHeads,
headDim: headDim,
intermediateSize: intermediateSize
)
}
}
/// Batch-specific temporary buffers for parallel layer processing
public struct BatchTemps {
public let hBatch: MTLBuffer // [batchSize, hiddenSize] - hidden state batch
public let qBatch: MTLBuffer // [batchSize, nHeads * headDim] - query batch
public let nsBatch: MTLBuffer // [batchSize, nHeads * headDim] - norm scratch batch
public let interBatch: MTLBuffer // [batchSize, intermediateSize] - intermediate batch
public init(
device: MTLDevice,
batchSize: Int,
hiddenSize: Int,
nHeads: Int,
headDim: Int,
intermediateSize: Int
) throws {
func buf(_ n: Int) throws -> MTLBuffer {
guard let b = device.makeBuffer(length: n * MemoryLayout<Float>.stride,
options: .storageModeShared)
else { throw NSError(domain: "BatchTemps", code: -1,
userInfo: [NSLocalizedDescriptionKey: "Buffer creation failed"]) }
return b
}
hBatch = try buf(batchSize * hiddenSize)
qBatch = try buf(batchSize * nHeads * headDim)
nsBatch = try buf(batchSize * nHeads * headDim)
interBatch = try buf(batchSize * intermediateSize)
}
}