v2: Initial clean branch with unit tests + CI/CD pipeline
- Started from ac75faa (initial E4B-MarkBase integration)
- Kept Sources/ (all engine code) + Package.swift + .gitignore
- Removed all ad-hoc tests, documentation, scripts, Python files
- Added Tests/00_Unit/ (MathTest, TokenizerTest, SamplerTest)
- Added .gitea/workflows/ci.yaml (build + unit tests + lint)
- Added Scripts/check_resources.sh (memory-aware test runner)
- Added Tests/Manifest.json (resource requirements for all tests)
- Focus: 4-bit quantized models only
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import Foundation
|
||||
import Metal
|
||||
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
// Float16 Support for MarkBase
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// Float16 data type for memory-efficient computation
|
||||
public typealias Float16 = Swift.Float16
|
||||
|
||||
/// Float16 buffer utilities
|
||||
public enum Float16Utils {
|
||||
/// Convert Float32 array to Float16
|
||||
public static func toFloat16(_ values: [Float]) -> [Float16] {
|
||||
return values.map { Float16($0) }
|
||||
}
|
||||
|
||||
/// Convert Float16 array to Float32
|
||||
public static func toFloat32(_ values: [Float16]) -> [Float] {
|
||||
return values.map { Float($0) }
|
||||
}
|
||||
|
||||
/// Create MTLBuffer from Float16 array
|
||||
public static func makeBuffer(device: MTLDevice, values: [Float16]) -> MTLBuffer? {
|
||||
return device.makeBuffer(
|
||||
bytes: values,
|
||||
length: values.count * MemoryLayout<Float16>.stride,
|
||||
options: .storageModeShared
|
||||
)
|
||||
}
|
||||
|
||||
/// Read Float16 from MTLBuffer
|
||||
public static func readFloat16(from buffer: MTLBuffer, count: Int) -> [Float16] {
|
||||
let ptr = buffer.contents().assumingMemoryBound(to: Float16.self)
|
||||
return Array(UnsafeBufferPointer(start: ptr, count: count))
|
||||
}
|
||||
|
||||
/// Convert Float32 MTLBuffer to Float16
|
||||
public static func convertBuffer(
|
||||
from buffer: MTLBuffer,
|
||||
device: MTLDevice,
|
||||
count: Int
|
||||
) -> MTLBuffer? {
|
||||
let float32Ptr = buffer.contents().assumingMemoryBound(to: Float.self)
|
||||
let float32Values = Array<Float>(UnsafeBufferPointer(start: float32Ptr, count: count))
|
||||
let float16Values = toFloat16(float32Values)
|
||||
return makeBuffer(device: device, values: float16Values)
|
||||
}
|
||||
}
|
||||
|
||||
/// Float16 quantization for model weights
|
||||
public struct Float16Quantizer {
|
||||
/// Quantize Float32 weights to Float16
|
||||
public static func quantize(weights: [Float]) -> [Float16] {
|
||||
return Float16Utils.toFloat16(weights)
|
||||
}
|
||||
|
||||
/// Dequantize Float16 weights to Float32
|
||||
public static func dequantize(weights: [Float16]) -> [Float] {
|
||||
return Float16Utils.toFloat32(weights)
|
||||
}
|
||||
|
||||
/// Calculate memory savings
|
||||
public static func memorySavings(float32Count: Int, float16Count: Int) -> Double {
|
||||
let float32Size = float32Count * MemoryLayout<Float>.stride
|
||||
let float16Size = float16Count * MemoryLayout<Float16>.stride
|
||||
return 1.0 - (Double(float16Size) / Double(float32Size))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
import Foundation
|
||||
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
// Float16 Weight Conversion Tool
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
|
||||
public struct Float16Converter {
|
||||
/// Convert Float32 safetensors to Float16
|
||||
public static func convertModel(
|
||||
from sourceDir: String,
|
||||
to targetDir: String
|
||||
) throws {
|
||||
print("Converting model from Float32 to Float16...")
|
||||
print("Source: \(sourceDir)")
|
||||
print("Target: \(targetDir)")
|
||||
|
||||
// Create target directory
|
||||
try FileManager.default.createDirectory(
|
||||
at: URL(fileURLWithPath: targetDir),
|
||||
withIntermediateDirectories: true
|
||||
)
|
||||
|
||||
// Copy config files
|
||||
let configFiles = ["config.json", "tokenizer.json", "tokenizer.model"]
|
||||
for file in configFiles {
|
||||
let source = (sourceDir as NSString).appendingPathComponent(file)
|
||||
let target = (targetDir as NSString).appendingPathComponent(file)
|
||||
|
||||
if FileManager.default.fileExists(atPath: source) {
|
||||
try FileManager.default.copyItem(atPath: source, toPath: target)
|
||||
print("✓ Copied \(file)")
|
||||
}
|
||||
}
|
||||
|
||||
// Convert safetensors
|
||||
let sourceFile = (sourceDir as NSString).appendingPathComponent("model.safetensors")
|
||||
if FileManager.default.fileExists(atPath: sourceFile) {
|
||||
try convertSafetensors(from: sourceFile, to: (targetDir as NSString).appendingPathComponent("model.safetensors"))
|
||||
}
|
||||
|
||||
print("✓ Conversion complete!")
|
||||
}
|
||||
|
||||
/// Convert single safetensors file
|
||||
private static func convertSafetensors(from source: String, to target: String) throws {
|
||||
print("Converting \(source)...")
|
||||
|
||||
// Load safetensors
|
||||
let data = try Data(contentsOf: URL(fileURLWithPath: source))
|
||||
let headerSize = data.prefix(8).withUnsafeBytes { $0.load(as: UInt64.self) }
|
||||
let header = String(data: data.subdata(in: 8..<Int(headerSize) + 8), encoding: .utf8)!
|
||||
|
||||
// Parse header
|
||||
guard let headerJson = try JSONSerialization.jsonObject(with: Data(header.utf8)) as? [String: Any] else {
|
||||
throw ConversionError.invalidHeader
|
||||
}
|
||||
|
||||
// Convert tensors
|
||||
var newHeader: [String: Any] = [:]
|
||||
var newData = Data()
|
||||
|
||||
let headerSizeInt = Int(headerSize)
|
||||
var offset = headerSizeInt + 8
|
||||
|
||||
for (name, info) in headerJson {
|
||||
guard let tensorInfo = info as? [String: Any],
|
||||
let dtype = tensorInfo["dtype"] as? String,
|
||||
let shape = tensorInfo["shape"] as? [Int],
|
||||
let dataOffsets = tensorInfo["data_offsets"] as? [Int] else {
|
||||
continue
|
||||
}
|
||||
|
||||
// Only convert Float32 tensors
|
||||
if dtype == "F32" {
|
||||
let start = dataOffsets[0]
|
||||
let end = dataOffsets[1]
|
||||
let tensorData = data.subdata(in: offset + start..<offset + end)
|
||||
|
||||
// Convert to Float16
|
||||
let floatValues = tensorData.withUnsafeBytes { Array($0.bindMemory(to: Float.self)) }
|
||||
let halfValues = floatValues.map { Float16($0) }
|
||||
let halfData = halfValues.withUnsafeBytes { Data($0) }
|
||||
|
||||
// Update header
|
||||
var newTensorInfo = tensorInfo
|
||||
newTensorInfo["dtype"] = "F16"
|
||||
newTensorInfo["data_offsets"] = [newData.count, newData.count + halfData.count]
|
||||
newHeader[name] = newTensorInfo
|
||||
|
||||
// Append data
|
||||
newData.append(halfData)
|
||||
|
||||
print(" ✓ Converted \(name) (\(floatValues.count) F32 → \(halfValues.count) F16)")
|
||||
} else {
|
||||
// Keep other dtypes as-is
|
||||
let start = dataOffsets[0]
|
||||
let end = dataOffsets[1]
|
||||
let tensorData = data.subdata(in: offset + start..<offset + end)
|
||||
|
||||
newHeader[name] = tensorInfo
|
||||
|
||||
// Update offsets
|
||||
if let offsets = newHeader[name] as? [String: Any] {
|
||||
var newOffsets = offsets
|
||||
newOffsets["data_offsets"] = [newData.count, newData.count + tensorData.count]
|
||||
newHeader[name] = newOffsets
|
||||
}
|
||||
|
||||
newData.append(tensorData)
|
||||
}
|
||||
}
|
||||
|
||||
// Write new safetensors
|
||||
let newHeaderJson = try JSONSerialization.data(withJSONObject: newHeader)
|
||||
var outputData = Data()
|
||||
|
||||
// Write header size
|
||||
var newHeaderSize = UInt64(newHeaderJson.count)
|
||||
outputData.append(Data(bytes: &newHeaderSize, count: 8))
|
||||
|
||||
// Write header
|
||||
outputData.append(newHeaderJson)
|
||||
|
||||
// Write tensor data
|
||||
outputData.append(newData)
|
||||
|
||||
try outputData.write(to: URL(fileURLWithPath: target))
|
||||
print(" ✓ Saved to \(target)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Conversion errors
|
||||
public enum ConversionError: Error, LocalizedError {
|
||||
case invalidHeader
|
||||
case invalidTensor
|
||||
case conversionFailed(String)
|
||||
|
||||
public var errorDescription: String? {
|
||||
switch self {
|
||||
case .invalidHeader:
|
||||
return "Invalid safetensors header"
|
||||
case .invalidTensor:
|
||||
return "Invalid tensor data"
|
||||
case .conversionFailed(let detail):
|
||||
return "Conversion failed: \(detail)"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
import Foundation
|
||||
|
||||
public enum Gemma4Format {
|
||||
public static func buildChatPrompt(messages: [[String: Any]], tools: [[String: Any]]? = nil) -> String {
|
||||
var prompt = "<bos>"
|
||||
|
||||
if let tools = tools, !tools.isEmpty {
|
||||
let toolDefs = tools.compactMap { t -> String? in
|
||||
guard let fn = t["function"] as? [String: Any],
|
||||
let name = fn["name"] as? String else { return nil }
|
||||
return name
|
||||
}
|
||||
if !toolDefs.isEmpty {
|
||||
prompt += "<|tool_def|>"
|
||||
for (i, name) in toolDefs.enumerated() {
|
||||
if i > 0 { prompt += "|" }
|
||||
prompt += name
|
||||
}
|
||||
prompt += "<|tool_def|>"
|
||||
}
|
||||
}
|
||||
|
||||
for msg in messages {
|
||||
guard let role = msg["role"] as? String else { continue }
|
||||
|
||||
switch role {
|
||||
case "system":
|
||||
prompt += "<|turn|>system\n"
|
||||
if let content = msg["content"] as? String {
|
||||
prompt += content
|
||||
}
|
||||
prompt += "<turn|>"
|
||||
|
||||
case "user":
|
||||
prompt += "<|turn|>user\n"
|
||||
if let content = msg["content"] as? String {
|
||||
prompt += content
|
||||
}
|
||||
prompt += "<turn|>"
|
||||
|
||||
case "assistant":
|
||||
prompt += "<|turn|>model\n"
|
||||
if let content = msg["content"] as? String {
|
||||
prompt += content
|
||||
}
|
||||
if let toolCalls = msg["tool_calls"] as? [[String: Any]] {
|
||||
for tc in toolCalls {
|
||||
if let fn = tc["function"] as? [String: Any],
|
||||
let name = fn["name"] as? String,
|
||||
let args = fn["arguments"] {
|
||||
let argsStr: String
|
||||
if let s = args as? String { argsStr = s }
|
||||
else if let d = try? JSONSerialization.data(withJSONObject: args),
|
||||
let s = String(data: d, encoding: .utf8) { argsStr = s }
|
||||
else { argsStr = "{}" }
|
||||
prompt += " <|tool_call|>\(name):\(argsStr)<|tool_call|>"
|
||||
}
|
||||
}
|
||||
}
|
||||
prompt += "<turn|>"
|
||||
|
||||
case "tool":
|
||||
prompt += "<|turn|>tool\n"
|
||||
if let content = msg["content"] as? String {
|
||||
prompt += content
|
||||
}
|
||||
if let callId = msg["tool_call_id"] as? String {
|
||||
prompt += " [call_id: \(callId)]"
|
||||
}
|
||||
prompt += "<turn|>"
|
||||
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
prompt += "<|turn|>model\n"
|
||||
return prompt
|
||||
}
|
||||
|
||||
public static func gemma4ArgsToJSON(_ rawArgs: String) -> String {
|
||||
let trimmed = rawArgs.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
// Try to parse as JSON first
|
||||
if let data = trimmed.data(using: .utf8),
|
||||
let _ = try? JSONSerialization.jsonObject(with: data) {
|
||||
return trimmed
|
||||
}
|
||||
// Convert key=value or key:value pairs to JSON
|
||||
var entries: [String: String] = [:]
|
||||
for part in trimmed.components(separatedBy: ",") {
|
||||
let kv = part.split(separator: "=", maxSplits: 1).map(String.init)
|
||||
if kv.count == 2 {
|
||||
let key = kv[0].trimmingCharacters(in: .whitespaces)
|
||||
var val = kv[1].trimmingCharacters(in: .whitespaces)
|
||||
// Remove any trailing punctuation
|
||||
while val.last == "," || val.last == ")" || val.last == "]" {
|
||||
val = String(val.dropLast())
|
||||
}
|
||||
entries[key] = val
|
||||
}
|
||||
}
|
||||
if !entries.isEmpty {
|
||||
if let data = try? JSONSerialization.data(withJSONObject: entries),
|
||||
let json = String(data: data, encoding: .utf8) {
|
||||
return json
|
||||
}
|
||||
}
|
||||
return "{\"value\":\"\(trimmed.replacingOccurrences(of: "\"", with: "\\\""))\"}"
|
||||
}
|
||||
}
|
||||
|
||||
public struct ToolCallResult: Codable, Sendable {
|
||||
public let id: String
|
||||
public let function: ToolCallFunction
|
||||
|
||||
public init(id: String, function: ToolCallFunction) {
|
||||
self.id = id
|
||||
self.function = function
|
||||
}
|
||||
}
|
||||
|
||||
public struct ToolCallFunction: Codable, Sendable {
|
||||
public let name: String
|
||||
public let arguments: String
|
||||
|
||||
public init(name: String, arguments: String) {
|
||||
self.name = name
|
||||
self.arguments = arguments
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user