Show HN: Run an 80B Qwen in 4.3 GB of RAM on a Mac, and a 35B on an iPhone
https://github.com/leonickson1/Swiftlet📌 【Swiftlet 開源專案】在 iPhone 上跑 35B 模型!透過專家流式傳輸實現超低記憶體佔用
TL;DR:Swiftlet 透過「專家流式傳輸」技術,讓 80B 規模模型僅需 4.3 GB RAM 即可在 Mac 執行。
隨著大型語言模型(LLM)規模不斷擴張,如何在行動裝置或記憶體有限的電腦上執行高參數模型,一直是工程師的挑戰。Swiftlet 透過創新的架構設計,讓 35B 規模的模型能在 iPhone 上以約 2.5 GB 的記憶體佔用順暢運作,實現了同等級模型在手機端原生運行的紀錄。
🤔 解決記憶體與模型規模的矛盾
傳統的大型模型需要將所有參數載入記憶體(RAM),這對行動裝置來說是不可能的任務。Swiftlet 針對 Qwen3 系列的混合架構(MoE, Mixture-of-Experts)進行了優化,核心策略是:「只將模型中極小的密集核心(Dense Core)保留在記憶體中,並根據需求從儲存裝置即時串流(Stream)路由到的專家權重(Routed Experts)。」
🧩 核心技術:專家流式傳輸與混合架構
Swiftlet 的設計精髓在於它如何處理 MoE 架構中的專家權重:
- 記憶體中常駐權重:僅保留 Attention、DeltaNet 投影、Router、共享專家(Shared Experts)與 Embedding 等核心層,這部分在 4-bit 量化下僅需約 1.3 GB (35B) 到 2.5 GB (80B)。
- 專家權重串流(Expert Streaming):將成千上萬個專家權重打包成固定間隔(Fixed-stride)的
.qpack容器。當模型需要特定專家時,直接透過pread從 SSD 讀取,避免了使用mmap可能導致的 page-cache 抖動(thrash)。 - 高效緩存機制:使用 LFU(Least Frequently Used)結合近期性(Recency)的機制,將熱門專家緩存在受限的池(Bounded Pool)中。
- 線性注意力(Linear Attention):75% 的層使用 Gated DeltaNet 線性注意力,具有固定大小的遞歸狀態(Recurrent State),這意味著無論 Context 長度多長,都不會像傳統 Transformer 那樣導致 KV Cache 持續膨脹。
📊 效能表現:低記憶體佔用,高參數質感
雖然模型規模巨大,但由於每顆 Token 僅會激活約 3B 的參數,因此其表現具備「大模型的對話品質」與「小模型的反應速度」雙重特性。
| 模型類型 | 參數規模 | 磁碟佔用 | 峰值 RAM (M5 Mac) | 推論速度 (M5 Mac) |
|---|---|---|---|---|
| Qwen3.6-35B-A3B | 35B (4-bit) | 18 GB | 2.6 GB | 7 ~ 11 tok/s |
| Qwen3-Next-80B-A3B | 80B (4-bit) | 42 GB | 4.3 GB | 4.5 ~ 5 tok/s |
註:35B 模型在 iPhone 17 上僅需約 2.5 GB RAM,速度約為 1 tok/s。
💡 深入分析:為什麼這對工程師很重要?
Swiftlet 的成功在於它證明了「專家流式傳輸」在行動裝置上的可行性。它不僅僅是簡單的權重切分,還包含了一套完整的工程實踐:
- Runtime-compiled Shaders:在執行時編譯 Metal Shader,確保 iOS 與 macOS 程式碼的一致性。
- 驗證機制:開發者對每一層的 Forward Pass(包含 Gated DeltaNet、Sparse MoE 等)都進行了與
mlx-lm參考實作的逐層比對,確保量化後的精確度。 - 開發靈活性:Swiftlet 提供 Swift Package 供 App 開發者整合,也提供 OpenAI 相容的伺服器模式,讓開發者可以輕鬆將本地模型整合進現有的 UI 介面中。
⚠️ 限制與注意事項
- 效能瓶頸:目前的效能瓶頸在於 Decode 迴圈的 Dispatch Bound(派遣受限),而非 IO 受限,這表示未來仍有提升空間。
- 硬體要求:需要 Apple Silicon 晶片,macOS 14+ 或 iOS 17+。
- 儲存空間:雖然 RAM 佔用低,但仍需預留大量 SSD 空間來存放權重容器(35B 需 18 GB,80B 需 42 GB)。
🎯 實務啟示
如果你正在開發需要本地 AI 能力的應用程式(如 Priv AI),Swiftlet 提供的 SwiftletCore 提供了一套完整的解決方案,包括對話快取(Conversation Caching)、重複控制(Repetition Control)以及針對 iOS 的記憶體壓力協調(Memory-pressure coordination),這比從零開始處理 Metal 核心更具開發效率。
🔗 來源
- 標題:Show HN: Run an 80B Qwen in 4.3 GB of RAM on a Mac, and a 35B on an iPhone
- 作者/機構:leonickson
- 連結:https://github.com/leonickson1/Swiftlet
#AI #MachineLearning #LLM #Qwen #Metal #Swift #AppleSilicon #MoE #OnDeviceAI #EdgeAI #OpenSource
原始資料 Hacker News · 收集於 2026-08-04
摘要原文
Swiftlet Run 35B and 80B Qwen models on ordinary Apple devices, including iPhones. Swiftlet is a Swift + Metal runtime for the Qwen3-Next and Qwen3.5/3.6 MoE hybrid model family. It keeps only the small dense core of a model resident in memory and streams the routed Mixture-of-Experts weights from storage on demand. The result: Model Disk Peak RAM Decode speed (M5 Mac) Qwen3.6-35B-A3B, 4-bit 18 GB 2.6 GB 7 to 11 tok/s Qwen3-Next-80B-A3B, 4-bit 42 GB 4.3 GB 4.5 to 5 tok/s The 35B also runs on an iPhone 17 in about 2.5 GB of RAM, at about 1 tok/s today. As far as we know, that is the first time a model of this class has run natively on a phone. Status: working end to end. Both models generate correct, validated output. The current focus is kernel speed (the decode loop is dispatch bound, not IO bound, so there is clear headroom). One expectation to set honestly: only about 3B parameters are active per token, so these models chat and write like large models but recall facts like small ones. Quick start: try it on a Mac git clone https://github.com/leonickson1/Swiftlet.git && cd Swiftlet swift build -c release # Download the 35B container from Hugging Face (resumable): .build/release/swiftlet-repack \ --from-hf Leonickson/Qwen3.6-35B-A3B-qpack \ --output ~ /models/qwen3.6-35b.qpack # Or the 80B (42 GB on disk, still only ~4.3 GB of RAM): .build/release/swiftlet-repack \ --from-hf Leonickson/Qwen3-Next-80B-A3B-qpack \ --output ~ /models/qwen3-next-80b.qpack # Chat (applies the model chat template, disables the reasoning block, # keeps conversation state so follow-ups prefill only the new turn): .build/release/swiftlet chat ~ /models/qwen3.6-35b.qpack \ " Who wrote One Hundred Years of Solitude? " " What language did he write it in? " # One-shot generation with stats: .build/release/swiftlet generate ~ /models/qwen3.6-35b.qpack \ --gpu --chat --prompt " Explain expert streaming in one paragraph. " # OpenAI-compatible server (loopback only): .build/release/swiftlet-server --model ~ /models/qwen3.6-35b.qpack --port 8080 The same command also repacks raw MLX checkpoints ( --from-hf mlx-community/... or --source /path/to/checkpoint ). Requirements: Apple Silicon, macOS 14+ or iOS 17+, free SSD space for the container (18 GB for the 35B, 42 GB for the 80B). Try it on your phone The 35B runs on iPhone inside Priv AI on the App Store : open Settings, then Experimental Models, and download the model. It streams from storage and chats on-device with no server involved. The Experimental Models feature ships in the newest app version, which is still in App Store review, so it may not appear for a couple of days. If you want the phone experience today, build the app from source: the app is open source at leonickson1/localLLM . Clone this repo next to it as swiftlet , open the Xcode project, and run it on your iPhone. How it works These models activate only about 3B of their parameters per token. Each layer routes every token to 10 of 512 experts (80B) or 8 of 256 (35B). Swiftlet: keeps the dense weights resident: attention, DeltaNet projections, routers, shared experts, embeddings. About 1.3 GB (35B) or 2.5 GB (80B) at 4-bit; repacks the tens of thousands of routed experts into fixed-stride blobs in a .qpack container, so fetching one expert is exactly one pread from SSD, no mmap and no page-cache thrash; caches hot experts in a bounded pool with LFU plus recency eviction. Cache size barely affects speed (measured 43 to 70 percent hit rates at the same throughput), because Apple SSDs absorb the misses; runs the whole forward pass on Metal with runtime-compiled shaders, so no Metal toolchain is needed at build time and the same code ships on iOS. 75 percent of the layers use Gated DeltaNet linear attention with a fixed-size recurrent state, so there is no growing KV cache for those layers at any context length. Four ways to use it Swiftlet is a library first: The Swift package. Add SwiftletCore to any macOS or iOS app and use SwiftletSession for chat with streaming deltas, conversation caching, sampling with repetition control, and memory-pressure handling built in. The CLI. swiftlet chat and swiftlet generate for local use and benchmarking, swiftlet-repack to build containers from MLX checkpoints (including streaming straight from Hugging Face with resume). The server. swiftlet-server speaks the OpenAI chat-completions API on loopback, so any chat UI that talks to OpenAI-compatible endpoints can use a streamed local model. An app. Priv AI on iOS embeds SwiftletCore as its streamed-model engine. End users tap Download and chat. Nothing here is terminal-only. The app itself is open source at leonickson1/localLLM if you want to build it yourself (clone this repo next to it as swiftlet ). Correctness Every layer of the forward pass (Gated DeltaNet recurrence, gated GQA attention, sparse MoE routing) is validated against mlx-lm reference implementations with per-layer fixtures, in f32 and int4 quantized form. Incremental decoding is verified against whole-sequence processing. Metal kernels are tested against the exact CPU reference, and the fast and scalar GPU kernels are verified to produce identical outputs. Containers are byte-verifiable against their source checkpoints. Streaming placement never changes model semantics: an expert answers identically from cache or disk. swift test Relationship to TurboFieldfare TurboFieldfare proved the expert-streaming thesis for Gemma on Macs, and Swiftlet adopts several of its published design lessons with gratitude: stream experts with pread into a bounded slot pool instead of mmap, evict with LFU plus recency, pack experts at fixed stride so one fetch is one read, install by routing downloaded bytes straight into their final container positions, and compile shaders at runtime. Everything else is built here, from scratch, in about 10k lines of Swift and Metal written against mlx-lm references rather than TurboFieldfare code: support for a different model family with a fundamentally different architecture: the Qwen hybrid stack with Gated DeltaNet linear attention, gated GQA, and high-sparsity MoE with a shared expert (TurboFieldfare runs Gemma, a classical dense transformer); MLX affine int4/int8 group quantization compute in Metal, byte-addressed kernels with 64-bit offsets for multi-gigabyte shards, a cooperative simdgroup GEMV fast path, and explicit hazard management; a validated CPU reference implementation and the fixture infrastructure that gates every kernel change; the .qpack container and repacker, the resumable Hugging Face streaming installer with stall recovery, and download cancellation; the chat session layer: template handling for thinking and non-thinking Qwen variants, sampling with presence and frequency penalties and minimum-length and sentence-completion stopping, conversation caching with delta prefill, and iOS memory-pressure coordination; iPhone support end to end, including the app engine integration. colibrì informed the caching and placement policy thinking. mlx-lm is the correctness reference throughout. Swiftlet was built in collaboration with Claude Code . License Apache 2.0. Model weights are downloaded separately and remain governed by their own terms (Qwen models: Apache 2.0). See THIRD_PARTY_NOTICES.md. (147 points, 58 comments on Hacker News)
由 tencent/hy3:free 自動生成