import CONNXRuntime import Foundation enum ORTError: LocalizedError { case runtime(String) var errorDescription: String? { switch self { case .runtime(let message): "ONNX Runtime: \(message)" } } } struct ORTTensor { let shape: [Int64] var data: [Float] } /// A loaded ONNX model. The same session can run one face at a time; the /// face engine keeps it on its own actor. final class ORTSession: @unchecked Sendable { private static nonisolated(unsafe) let api: UnsafePointer = OrtGetApiBase().pointee.GetApi(UInt32(ORT_API_VERSION))! private static nonisolated(unsafe) let env: OpaquePointer = { var env: OpaquePointer? let status = api.pointee.CreateEnv(ORT_LOGGING_LEVEL_ERROR, "FaceMaker", &env) precondition(status == nil && env != nil, "OrtCreateEnv failed") return env! }() private let session: OpaquePointer private let allocator: UnsafeMutablePointer let inputNames: [String] let outputNames: [String] /// Serializes `CreateSession` across threads. ONNX Runtime lazily /// populates a global op-schema registry on first graph resolve, and that /// registration is not thread-safe — two sessions being created at once /// (e.g. the generator actor and the face-inversion actor) can segfault /// inside the graph transformer. All session creation goes through this. private static nonisolated(unsafe) let creationLock = NSLock() init(modelPath: String, useCoreML: Bool = false) throws { Self.creationLock.lock() defer { Self.creationLock.unlock() } let api = Self.api var options: OpaquePointer? try Self.check(api.pointee.CreateSessionOptions(&options)) defer { api.pointee.ReleaseSessionOptions(options) } try Self.check(api.pointee.SetSessionGraphOptimizationLevel(options, ORT_ENABLE_ALL)) if useCoreML { let cache = Self.coreMLCacheDirectory() let keys = ["ModelFormat", "MLComputeUnits", "ModelCacheDirectory"] let values = ["MLProgram", "ALL", cache] var keyPointers = keys.map { UnsafePointer(strdup($0)) } var valuePointers = values.map { UnsafePointer(strdup($0)) } defer { keyPointers.forEach { free(UnsafeMutablePointer(mutating: $0)) } valuePointers.forEach { free(UnsafeMutablePointer(mutating: $0)) } } if let status = api.pointee.SessionOptionsAppendExecutionProvider( options, "CoreML", &keyPointers, &valuePointers, keys.count) { api.pointee.ReleaseStatus(status) } } var session: OpaquePointer? try Self.check(modelPath.withCString { path in api.pointee.CreateSession(Self.env, path, options, &session) }) guard let session else { throw ORTError.runtime("CreateSession returned nil") } self.session = session var allocator: UnsafeMutablePointer? try Self.check(api.pointee.GetAllocatorWithDefaultOptions(&allocator)) self.allocator = allocator! func names( count: (OpaquePointer?, UnsafeMutablePointer?) -> OrtStatusPtr?, name: (OpaquePointer?, Int, UnsafeMutablePointer?, UnsafeMutablePointer?>?) -> OrtStatusPtr? ) throws -> [String] { var total = 0 try Self.check(count(session, &total)) return try (0..? try Self.check(name(session, index, allocator, &raw)) defer { allocator!.pointee.Free(allocator, raw) } return String(cString: raw!) } } self.inputNames = try names( count: { api.pointee.SessionGetInputCount($0, $1) }, name: { api.pointee.SessionGetInputName($0, $1, $2, $3) }) self.outputNames = try names( count: { api.pointee.SessionGetOutputCount($0, $1) }, name: { api.pointee.SessionGetOutputName($0, $1, $2, $3) }) } deinit { Self.api.pointee.ReleaseSession(session) } /// One Core ML compile cache in the OS temp directory. Reused across /// launches. macOS deletes it after the app has not been used. private static func coreMLCacheDirectory() -> String { let url = FileManager.default.temporaryDirectory .appendingPathComponent("com.lewpen.facemaker.coreml", isDirectory: true) try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true) return url.path } func run(_ inputs: [String: ORTTensor]) throws -> [ORTTensor] { let api = Self.api var memoryInfo: OpaquePointer? try Self.check(api.pointee.CreateCpuMemoryInfo(OrtArenaAllocator, OrtMemTypeDefault, &memoryInfo)) defer { api.pointee.ReleaseMemoryInfo(memoryInfo) } var inputValues: [OpaquePointer?] = [] defer { inputValues.forEach { api.pointee.ReleaseValue($0) } } var buffers: [[Float]] = [] for name in inputNames { guard let tensor = inputs[name] else { throw ORTError.runtime("missing input tensor '\(name)'") } buffers.append(tensor.data) var value: OpaquePointer? let byteCount = tensor.data.count * MemoryLayout.stride try buffers[buffers.count - 1].withUnsafeMutableBufferPointer { buffer in try tensor.shape.withUnsafeBufferPointer { shape in try Self.check(api.pointee.CreateTensorWithDataAsOrtValue( memoryInfo, buffer.baseAddress, byteCount, shape.baseAddress, tensor.shape.count, ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT, &value)) } } inputValues.append(value) } var inputNamePointers = inputNames.map { UnsafePointer(strdup($0)) } var outputNamePointers = outputNames.map { UnsafePointer(strdup($0)) } defer { inputNamePointers.forEach { free(UnsafeMutablePointer(mutating: $0)) } outputNamePointers.forEach { free(UnsafeMutablePointer(mutating: $0)) } } var outputValues = [OpaquePointer?](repeating: nil, count: outputNames.count) defer { outputValues.forEach { api.pointee.ReleaseValue($0) } } try withExtendedLifetime(buffers) { try Self.check(api.pointee.Run( session, nil, &inputNamePointers, &inputValues, inputNames.count, &outputNamePointers, outputNames.count, &outputValues)) } return try outputValues.map { value in var typeShape: OpaquePointer? try Self.check(api.pointee.GetTensorTypeAndShape(value, &typeShape)) defer { api.pointee.ReleaseTensorTypeAndShapeInfo(typeShape) } var dimCount = 0 try Self.check(api.pointee.GetDimensionsCount(typeShape, &dimCount)) var shape = [Int64](repeating: 0, count: dimCount) try Self.check(api.pointee.GetDimensions(typeShape, &shape, dimCount)) var elementCount = 0 try Self.check(api.pointee.GetTensorShapeElementCount(typeShape, &elementCount)) var raw: UnsafeMutableRawPointer? try Self.check(api.pointee.GetTensorMutableData(value, &raw)) let floats = raw!.bindMemory(to: Float.self, capacity: elementCount) return ORTTensor(shape: shape, data: Array(UnsafeBufferPointer(start: floats, count: elementCount))) } } private static func check(_ status: OrtStatusPtr?) throws { guard let status else { return } let message = String(cString: api.pointee.GetErrorMessage(status)!) api.pointee.ReleaseStatus(status) throw ORTError.runtime(message) } }