devin-lai's picture
Release v0.4.0: selective MLX model downloads
3da88e3 verified
Raw History Blame Contribute Delete
3.27 kB
// Synchronized MPSMatrix baseline, including CPU submission and shared buffers.
// Build: swiftc -O experiments/v2_mps.swift -o cache/v2-mps
import Foundation
import Metal
import MetalPerformanceShaders
let device = MTLCreateSystemDefaultDevice()!
let queue = device.makeCommandQueue()!
let repetitions = 7
let tokens = Int(CommandLine.arguments.dropFirst().first ?? "4181")!
var results: [[String: Any]] = []
for (k,n) in [(5376,21504),(7168,5376),(5376,28672),(14336,5376)] {
for fp16 in [true,false] {
let bytes = fp16 ? 2 : 4
let type: MPSDataType = fp16 ? .float16 : .float32
let ab = device.makeBuffer(length: tokens*k*bytes, options:.storageModeShared)!
let bb = device.makeBuffer(length: n*k*bytes, options:.storageModeShared)!
let cb = device.makeBuffer(length: tokens*n*bytes, options:.storageModeShared)!
if fp16 {
ab.contents().bindMemory(to:Float16.self, capacity:tokens*k).initialize(repeating:Float16(1.0/64),count:tokens*k)
bb.contents().bindMemory(to:Float16.self, capacity:n*k).initialize(repeating:Float16(1.0/64),count:n*k)
} else {
ab.contents().bindMemory(to:Float.self, capacity:tokens*k).initialize(repeating:1.0/64,count:tokens*k)
bb.contents().bindMemory(to:Float.self, capacity:n*k).initialize(repeating:1.0/64,count:n*k)
}
let a = MPSMatrix(buffer:ab,descriptor:MPSMatrixDescriptor(rows:tokens,columns:k,rowBytes:k*bytes,dataType:type))
let b = MPSMatrix(buffer:bb,descriptor:MPSMatrixDescriptor(rows:n,columns:k,rowBytes:k*bytes,dataType:type))
let c = MPSMatrix(buffer:cb,descriptor:MPSMatrixDescriptor(rows:tokens,columns:n,rowBytes:n*bytes,dataType:type))
let mul = MPSMatrixMultiplication(device:device,transposeLeft:false,transposeRight:true,resultRows:tokens,resultColumns:n,interiorColumns:k,alpha:1,beta:0)
var wall: [Double] = [], gpu: [Double] = []
for trial in 0..<(repetitions+2) {
let start = ProcessInfo.processInfo.systemUptime
let command = queue.makeCommandBuffer()!
mul.encode(commandBuffer:command,leftMatrix:a,rightMatrix:b,resultMatrix:c)
command.commit()
command.waitUntilCompleted()
precondition(command.status == .completed, "MPS command failed")
if trial>=2 {
wall.append(1000*(ProcessInfo.processInfo.systemUptime-start))
gpu.append(1000*(command.gpuEndTime-command.gpuStartTime))
}
}
let y = fp16 ? Float(cb.contents().bindMemory(to:Float16.self,capacity:tokens*n)[0]) : cb.contents().bindMemory(to:Float.self,capacity:tokens*n)[0]
precondition(y == Float(k)/4096, "Projection correctness check failed")
let row: [String:Any] = ["backend":"MPSMatrixMultiplication","dtype":fp16 ? "float16":"float32","shape":[tokens,k,n],"median_ms":wall.sorted()[wall.count/2],"gpu_median_ms":gpu.sorted()[gpu.count/2],"samples_ms":wall,"gpu_samples_ms":gpu,"first_element":y,"expected":Float(k)/4096]
results.append(row)
let encoded = try! JSONSerialization.data(withJSONObject:row,options:[.sortedKeys])
print(String(data:encoded,encoding:.utf8)!)
fflush(stdout)
}
}