feat: 0.9.0 architecture integration & iOS/macOS native Llama.xcframework support - #2
Closed
sebasbad wants to merge 11 commits into
Closed
feat: 0.9.0 architecture integration & iOS/macOS native Llama.xcframework support#2sebasbad wants to merge 11 commits into
sebasbad wants to merge 11 commits into
Conversation
….xcframework podspec
… as specified in README
…o external dylib dependencies
…ndored_frameworks
…maLibrary.defaultFileName
Owner
Author
🔍 Formal Code Review & Pre-Merge Audit Report📋 Review Summary
1. Native Build & Linker Layer (
|
Owner
Author
|
Superseded by upstream PR netdur#109 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🚀 0.9.0-dev.9 Off-Thread LlamaEngine & iOS/macOS Native Llama.xcframework Support
📌 Executive Summary
This PR integrates the
0.9.0-dev.9architecture preview ofllama_cpp_dart, upgrading the runtime to an off-threadLlamaEngineactor model (Isolates) and bundling a self-contained dynamicLlama.xcframeworkwith native Metal GPU acceleration, Accelerate BLAS, and zero external.dylibruntime dependencies on iOS.📊 Comparative Analysis: BEFORE vs AFTER PR #2
graph TD subgraph BEFORE["❌ BEFORE PR #2 (Upstream 0.9.0-dev Preview)"] direction TB B_UI["Flutter App / UI Thread"] -->|Loads dyld| B_FW["Llama.framework (Incomplete)"] B_FW -->|Tries to find external| B_DYLD["@rpath/libllama.dylib (MISSING)"] B_DYLD -->|dyld Crash at Launch| B_CRASH["💥 EXC_CRASH SIGABRT"] B_SIM["CocoaPods Simulator Check"] -->|Advertised arm64 only| B_LINK["x86_64 Linker Search"] B_LINK -->|Fails Slice Validation| B_ERR["💥 Framework 'Llama' not found"] B_VLM["Moondream / Custom Models"] -->|No chat_template metadata| B_TEMPLATE["engine.createChat()"] B_TEMPLATE -->|Throws Exception| B_EXC["💥 LlamaLibraryException: no chat template"] end subgraph AFTER["✅ AFTER PR #2 (Self-Contained Dynamic Framework)"] direction TB A_UI["Flutter UI Thread"] -->|Off-Thread Isolate Msg| A_ENGINE["LlamaEngine Isolate Worker"] A_ENGINE -->|loadFromProcess / FFI| A_FW["Llama.framework (Monolithic Dynamic)"] A_FW -->|Static Link: llama + ggml + mtmd| A_METAL["Apple Metal GPU / Accelerate BLAS"] A_METAL -->|Zero External dylibs| A_BOOT["🚀 Boots Cleanly (PID Running)"] A_SIM["CocoaPods Simulator Check"] -->|Universal arm64 + x86_64 Slice| A_LINK["CocoaPods & Xcode Linker"] A_LINK -->|Passes Slice Validation| A_OK["🚀 -framework Llama Linked"] A_VLM["Moondream / Custom Models"] -->|No chat_template metadata| A_TEMPLATE["chat.generate(...)"] A_TEMPLATE -->|Automatic Fallback| A_FALLBACK["KnownChatTemplates.chatml"] A_FALLBACK -->|Successful Inference| A_OUT["🚀 Generates Tokens Cleanly"] end🎨 Architectural Overview & Sequence Flow
1. Off-Thread Actor Architecture (
LlamaEngine+ Dart Isolates)graph TD subgraph UI_THREAD["Flutter UI Thread (Main Looper)"] UI["Flutter UI Widgets / Presenter"] Chat["EngineChat Handle"] end subgraph ISOLATE_WORKER["Background Isolate (LlamaEngine Worker)"] Worker["LlamaEngine Isolate Worker"] CommandPort["ReceivePort Command Queue"] State["Session & KV Cache State"] end subgraph NATIVE_CPP["Native C++ Layer (FFI / Metal)"] FFI["Dart FFI Bindings"] LlamaLib["Llama.framework / libllama.so"] Metal["Apple Metal GPU / Accelerate BLAS"] end UI -->|1. spawn / send message| Chat Chat -->|2. Send Isolate Command| CommandPort CommandPort --> Worker Worker -->|3. FFI Call Non-blocking| FFI FFI -->|4. Execute C++ Inference| LlamaLib LlamaLib -->|5. Metal Shaders / Kernels| Metal Worker -->|6. Stream TokenEvent| UI2. Native Apple Framework Build & Resolution Pipeline (
Llama.xcframework)flowchart LR subgraph SRC["Source Code"] LlamaCPP["src/llama.cpp (C++)"] MTMD["tools/mtmd (Vision VLM)"] GGML["ggml (Metal/CPU Backends)"] end subgraph CMAKE_BUILD["CMake & Ninja Build Pipeline"] BuildScript["tool/build_apple_xcframework.sh"] Clang["xcrun clang++ (-dynamiclib)"] end subgraph BUNDLES["Output Bundles"] SliceIOS["ios-arm64 (Device)"] SliceSim["ios-arm64_x86_64-simulator (Simulator)"] SliceMac["macos-arm64 (Desktop)"] XCF["Llama.xcframework"] end LlamaCPP --> BuildScript MTMD --> BuildScript GGML --> BuildScript BuildScript --> Clang Clang --> SliceIOS Clang --> SliceSim Clang --> SliceMac SliceIOS --> XCF SliceSim --> XCF SliceMac --> XCF XCF -->|Embed & Sign| iOSApp["iOS App Bundle (Runner.app)"]🔑 Key Changes & Engineering Rationale
1. Monolithic Self-Contained Dynamic Framework
0.9.0-dev.9prebuilt binaries relied on external.dylibfiles (@rpath/libllama.dylib), causing iOSdyld(dynamic linker) to crash at launch withLibrary missing: @rpath/libllama.dylib.tool/build_apple_xcframework.shto compilellama.cpp+ggml+mtmd+ Metal shaders into a single self-contained dynamic binaryLlama.frameworkper slice using-Wl,-all_load.2. Dual-Architecture iOS Simulator Support (
arm64+x86_64)select_slicevalidation failed on simulator builds because the simulator framework slice only advertisedarm64, throwingFramework 'Llama' not found.arm64;x86_64) forios-arm64_x86_64-simulator.3. Casing & Bundle ID Standardization
llama.frameworkvsLlama.framework) and underscore bundle IDs (io.github.netdur.llama_cpp_dart) broke CocoaPods linking and Xcode Info.plist validation.Llama.frameworkand updated bundle identifiers to valid reverse-DNS format (org.ggml.llama).4. Robust Chat Template Fallback (
KnownChatTemplates.chatml)tokenizer.chat_templateGGUF header threwLlamaLibraryException: no chat template available.KnownChatTemplates.chatmlwhen callingchat.generate(templateOverride: ...).✅ Verification & Compatibility
ios-arm64): Metal GPU hardware acceleration verified.ios-arm64_x86_64-simulator): Zerodyldcrashes; tested on iPhone 16e simulator.macos-arm64): Native Metal acceleration verified.