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..