返回资讯中心
外部精选
软件工程
#开发者实时流

17岁开发者发布Swift原生LLM推理引擎Alpaca

来源标题:Im 17, Built a Swift-native LLM inference engine for Apple Silicon

17岁的Torin Etheridge从零构建了面向Apple Silicon的Swift原生推理引擎Alpaca,不依赖llama.cpp、MLX、Core ML或Python运行时。在SmolLM2-135M-Instruct(Q8_0)与Apple M5上,其预填充速度比llama.cpp快1.3–1.7倍。目前仅支持Llama风格解码器与F16、Q8_0、Q4_0三种权重格式,最快路径需Apple10系列GPU,尚未在真机iPhone或iPad上运行。

Hacker News 最新torinriley2026/11/13 · 4 天前30 分钟阅读

以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。

为 Apple Silicon 原生语言模型推理。 一个从零开始编写的 Swift 推理引擎,带自定义 Metal kernel——运行时不依赖 llama.cpp、MLX、Core ML、ONNX 或 Python。 0.2.0-pre——已实现一个架构家族(Llama 风格 decoder,byte-level BPE)和三种权重格式(F16、Q8_0、Q4_0),并与 PyTorch 做了验证。在验证模型(SmolLM2-135M-Instruct,Q8_0,Apple M5)上,128–4096 token 的 prefill 比 llama.cpp 快 1.3–1.7 倍。 注意事项:所有 benchmark 均在一台 Apple M5(16 GB)机器上测得。最快的 prefill 路径需要 Apple10 家族 GPU(M5/A19)以及 macOS/iOS 26;在其他设备上,吞吐量大约只有 1/3 到 1/2。尚未在真实 iPhone 或 iPad 上运行过;该库和示例 app 只针对 iOS 模拟器编译。 作者:Torin Etheridge · 2026-10-09 · MIT License Swift Package Manager // Package.swift .package(url: "https://github.com/torinriley/alpaca.git", from: "0.2.0") // target dependency .product(name: "Alpaca", package: "alpaca") 用法 import Alpaca // Load a GGUF model (memory-mapped, async) let model = try await LanguageModel.load(from: modelURL) // Generate text with streaming let stream = model.generate( prompt: "Explain how transformers work.", configuration: .init(maxTokens: 256, temperature: 0.7, seed: 42) ) for try await token in stream { print(token.text, terminator: "") // Check generation summary let summary = stream.summary // tokens, tok/s, finish reason 命令行

Download the validation model (SmolLM2-135M-Instruct, Apache-2.0)

Scripts/fetch_models.sh

Build

swift build -c release

Generate text

.build/release/alpaca generate Models/gguf/SmolLM2-135M-Instruct-Q8_0.gguf
--chat --prompt "Name three colors." --stats

Benchmark

.build/release/alpaca bench Models/gguf/SmolLM2-135M-Instruct-Q8_0.gguf
--backend metal --json out.json

Profile GPU stages

.build/release/alpaca profile Models/gguf/SmolLM2-135M-Instruct-Q8_0.gguf
--tokens 2048 --context 2200 一个 SwiftUI 参考 app(模型选择器、流式输出、取消、吞吐量和内存诊断)位于 Examples/iOS/AlpacaDemo。 Apple M5(10 核 GPU),16 GB,macOS 27.0,Swift 6.4。SmolLM2-135M-Instruct(30 层,GQA 9/3,head dim 64)。7 次运行的中位数,greedy decoding,两个引擎使用相同的 GGUF 文件。测量在带有后台负载的机器上进行(应在同一张图内比较,而非跨运行比较)。完整方法和数据:Docs/PERFORMANCE.md | 技术报告。 其他格式:Q4_0 在 16-token 上下文下 decode:712 tok/s(llama.cpp 518,1.38×)。F16 decode:360 tok/s(llama.cpp 319,1.13×)。 峰值内存:4400-token 上下文下为 175–194 MiB(135M 参数模型)。 技术报告将对比扩展到 SmolLM2-360M 和 1.7B,记录了使 prefill 在更大规模下具备竞争力的 GEMM 扩展工作,并解释了 16-token prompt 为何更慢。对于 1.7B 配合 --prefill-cache,128–4096 token 的 prefill 比 llama.cpp 快 1.1–1.3 倍。 Highlights · Capabilities · Results · Numerical validation · Design · Repository layout · Reproducing the results · Limitations · Roadmap · Citation · License 独立实现。Tensor 引擎、CPU 参考算子、GGUF 解析器、BPE tokenizer、Llama 前向传播、sampler 以及所有 Metal kernel 均在本仓库中编写。Python/PyTorch 仅用于离线生成参考 fixture。 正确性优先。每个操作都有确定性的 CPU 参考实现,并针对独立生成的向量(numpy/PyTorch float64、gguf 包、Hugging Face transformers/tokenizers)进行测试,带有明确且有依据的容差。CPU 后端在 logits 上与 Hugging Face float32 匹配到 5e-5(F16)和 7e-5(Q8_0);191/191 个 tokenizer 测试字符串产生相同的 token ID;每种格式下 72/72 个 greedy token 匹配。 有竞争力的性能,经实测。在验证模型(SmolLM2-135M-Instruct,Q8_0,Apple M5)上,alpaca.swift 在 128 到 4096 token 的 prefill 比 llama.cpp 快 1.3–1.7 倍,在 16–4096 token 上下文下 decode 快 1.02–1.38 倍。仅在非常短的 prompt 下更慢(16 token:0.72–0.90×)。技术报告表明,借助 GEMM 扩展工作,1.7B 的 prefill 在配合 --prefill-cache 时也比 llama.cpp 快。方法、瓶颈分析和优化见 Docs/PERFORMANCE.md。 技术报告。paper/alpaca-swift.pdf 在 135M、360M 和 1.7B 参数以及 F16、Q8_0 和 Q4_0 格式下评估 alpaca.swift 与 llama.cpp 的对比,诊断了 prefill 最初为何无法扩展到 1.7B,并测量了修复该问题的 GEMM 改动(LaTeX 源码、实验)。 对恶意输入具有鲁棒性。GGUF 解析器将模型文件视为不可信:每个长度都以实际存在的字节数为界,所有算术都做溢出检查,并且在 20,000 个变异模糊测试文件下没有崩溃。 符合语言习惯的 API。async 加载、AsyncSequence token 流式输出、取消、带种子的采样、每次生成隔离的 session,以及内存预算检查——如果模型放不下则拒绝加载。 Capabilities Architecture Llama 风格 decoder:RMSNorm、RoPE(无缩放)、grouped-query attention、SwiGLU、tied 或 untied 输出头 权重格式(GGUF v2/v3) F16、Q8_0、Q4_0 直接从内存映射文件原地使用;Q4_1 tensor 在加载时展开为 F16 并报告 Metal 后端 Tensor-op 因果 prefill attention 和 GEMM(Metal 4,Apple10 家族 GPU),带 simdgroup-matrix 回退 · split-K GQA decode attention · 融合单 token kernel · GPU 常驻 greedy decoding · f16 或 f32 KV cache CPU 后端 确定性参考实现(测试、调试、没有 Metal 的机器) Tokenizer Byte-level BPE,带 gpt2 和 smollm pre-tokenizer,UTF-8 安全的流式 decode Sampling Greedy、temperature、top-k、top-p、带种子的 RNG、EOS 和自定义 stop token Platforms macOS 14+、iOS 17+、Apple Silicon(arm64)。Swift 6 语言模式;使用 Swift 6.4 / Xcode 27 开发 快速开始 // Package.swift .package(url: "https://github.com/torinriley/alpaca.git", from: "0.1.0") // target dependency

(原文较长,此处译出前 6000 字,完整内容请点「打开原文」。)

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →