Native language model inference for Apple silicon.
A from-scratch Swift inference engine with custom Metal kernels. No llama.cpp, MLX, Core ML, ONNX, or Python at runtime.
import Alpaca
// Load a GGUF model (memory-mapped, async)
let model = try await LanguageModel.load(from: modelURL)
// Stream tokens as they're generated
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: "")
}
// tokens, tok/s, time to first token, finish reason
let summary = stream.summary- 1.56×
- faster prefill than llama.cpp
- 1.38×
- faster Q4_0 decode
- 30.8k
- prefill tokens / sec
- 190 MiB
- peak memory, 135M model
Everything on-device. Nothing borrowed.
Hardened parsing.
Model files are untrusted input: bounded lengths, overflow-checked arithmetic, and 20,000 fuzzed files without a crash.
Zero-copy weights.
The GGUF file is memory-mapped and wrapped as a single no-copy MTLBuffer. Resident memory is about the file size.
Written from scratch.
Tensor engine, tokenizer, sampler and forward pass. No llama.cpp, MLX, Core ML, or ONNX.
Custom Metal kernels.
Six fused dispatches per layer, tensor-op prefill attention and GEMM, split-K decode attention.
Verified against PyTorch.
191 of 191 tokenizer strings and 72 of 72 greedy tokens match the reference.
Idiomatic Swift.
Async loading, AsyncSequence streaming, cancellation, and seeded sampling.
Faster than llama.cpp, on the same GGUF files.
Prefill runs 1.3–1.7× faster from 128 to 4096 tokens, and decode is up to 1.38× faster. At 1.7B parameters, prefill with --prefill-cache is 1.1–1.3× faster as well.
SmolLM2-135M-Instruct · Apple M5 · median of 7 runs · greedy decoding
Performance guideTechnical report (PDF)Tokens per second, by prompt length
From package to first token in minutes.
// 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)
// Stream tokens as they're generated
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: "")
}
// tokens, tok/s, time to first token, finish reason
let summary = stream.summary# Fetch the validation model (SmolLM2-135M-Instruct)
Scripts/fetch_models.sh
swift build -c release
# Generate
.build/release/alpaca generate Models/gguf/SmolLM2-135M-Instruct-Q8_0.gguf \
--chat --prompt "Name three colors." --stats
# Benchmark and profile
.build/release/alpaca bench Models/gguf/SmolLM2-135M-Instruct-Q8_0.gguf --backend metal
.build/release/alpaca profile Models/gguf/SmolLM2-135M-Instruct-Q8_0.gguf --tokens 2048Cancel with stream.cancel(). Trim memory with model.trimMemory(). A SwiftUI reference app lives in Examples/iOS/AlpacaDemo.
Checked against the reference, at every level.
Each operation has a deterministic CPU reference tested against vectors from numpy, PyTorch, gguf, and Hugging Face.
| Check | Result | Against |
|---|---|---|
| Primitives | ≤ 2.5e-6 | vs float64; dequantisation bit-exact |
| Tiny Llama, cached vs uncached | ≤ 1.3e-6 | vs float64 PyTorch; cached == uncached exactly |
| SmolLM2-135M logits (CPU) | 7e-5 | Q8_0 vs Hugging Face float32 |
| Greedy tokens (Metal) | 72 / 72 | identical on every format |
| Tokenizer | 191 / 191 | identical token IDs vs Hugging Face |
A small, layered package.
Weights are never copied: the GGUF file is memory-mapped and wrapped as one no-copy buffer. A decode step is six fused dispatches per layer, and greedy decoding runs entirely on the GPU.
AlpacaPublic API: LanguageModel, GenerationStreamAlpacaMetalMetal context, kernels, GPU Llama model and sessionsAlpacaModelsGGUF parser, weight mapping, tokenizer constructionAlpacaTokenizersByte-level BPEAlpacaCoreTensors, CPU reference ops, KV cache, sampler
Capabilities.
- Architecture
- Llama-style decoder · RMSNorm · RoPE · GQA · SwiGLU
- Weights
- GGUF v2/v3 · F16 · Q8_0 · Q4_0 (Q4_1 expanded at load)
- Tokenizer
- Byte-level BPE · gpt2 & smollm pre-tokenizers
- Sampling
- Greedy · temperature · top-k · top-p · seeded RNG
- Backends
- Metal GPU · deterministic CPU reference
- Platforms
- macOS 14+ · iOS 17+ · Apple Silicon
Swift-native model inference.
Star the repo, read the code, send a pull request.