Section 3/171 menit
3. Teori: Core ML Runtime Pipeline
3. Teori: Core ML Runtime Pipeline
Alur Inferensi dari Model File ke Hasil
swift
.mlpackage / .mlmodelc
│
▼
MLModel.load() ──── mengcompile model ke format native hardware
│
├── Neural Engine path (ANE) ← operasi yang support ANE
├── GPU path (Metal) ← operasi yang support GPU
└── CPU path (BNNS/Accelerate) ← fallback atau CPU-only ops
│
▼
model.prediction(input:) ──── dispatch ke compute unit terpilih
│
├── Input preprocessing (normalization, reshape)
├── Layer-by-layer computation
└── Output postprocessing
│
▼
MLFeatureProvider (output) ──── hasil dikembalikan ke caller
MLComputePlan: Inspect Sebelum Run
Sebelum menjalankan inferensi, bisa inspect bagaimana Core ML akan mendistribusikan pekerjaan:
swift
// Inspect compute plan — mana yang ANE, GPU, CPU
let modelURL = Bundle.main.url(forResource: "MyModel", withExtension: "mlpackage")!
let compiledURL = try await MLModel.compileModel(at: modelURL)
let model = try MLModel(contentsOf: compiledURL)
// iOS 17+: MLComputePlan
if #available(iOS 17, *) {
let plan = try await MLComputePlan.load(contentsOf: modelURL, configuration: .init())
// Inspect structure: mana layers yang jalan di ANE vs GPU vs CPU
plan.modelStructure // MLModelStructure object
}
Compile vs Load
MLModel(contentsOf:) mengharapkan compiled model (.mlmodelc). Ketika Anda menambahkan .mlpackage ke Xcode, Xcode mengcompile-nya saat build time. Untuk model yang di-download dari server, harus compile manual:
swift
// Download model dari server, compile, simpan
func downloadAndPrepareModel(from url: URL) async throws -> URL {
// 1. Download
let (downloadedURL, _) = try await URLSession.shared.download(from: url)
// 2. Pindahkan ke lokasi permanen
let modelsDirectory = try FileManager.default
.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appending(component: "models")
try FileManager.default.createDirectory(at: modelsDirectory, withIntermediateDirectories: true)
let packageURL = modelsDirectory.appending(component: "downloaded.mlpackage")
if FileManager.default.fileExists(atPath: packageURL.path) {
try FileManager.default.removeItem(at: packageURL)
}
try FileManager.default.moveItem(at: downloadedURL, to: packageURL)
// 3. Compile (async, bisa lama untuk model besar)
let compiledURL = try await MLModel.compileModel(at: packageURL)
// 4. Pindahkan compiled model ke lokasi permanen
let permanentCompiledURL = modelsDirectory.appending(component: "model.mlmodelc")
if FileManager.default.fileExists(atPath: permanentCompiledURL.path) {
try FileManager.default.removeItem(at: permanentCompiledURL)
}
try FileManager.default.moveItem(at: compiledURL, to: permanentCompiledURL)
return permanentCompiledURL
}
Compute Unit Tradeoff
| Compute Unit | Latency | Throughput | Power | Cocok Untuk |
|---|---|---|---|---|
.cpuOnly |
Tinggi | Rendah | Rendah | Debug, custom ops |
.cpuAndGPU |
Sedang | Tinggi | Tinggi | GPU-heavy models |
.cpuAndNeuralEngine |
Rendah | Tinggi | Rendah | ANE-optimized models |
.all |
Rendah | Tertinggi | Sedang | Default production |
Neural Engine (ANE) adalah coprocessor khusus untuk ML di chip Apple Silicon. Sangat cepat dan hemat baterai untuk operasi yang didukung (convolution, matmul, normalization). Tapi tidak semua layer support ANE — jika ada layer yang tidak didukung, model akan split antara ANE dan CPU, yang menambah overhead transfer.