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.

main.swift
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

128 tokens1.32×
24,106alpaca.swift
18,326llama.cpp
2048 tokens1.48×
30,797alpaca.swift
20,762llama.cpp
4096 tokens1.56×
24,069alpaca.swift
15,388llama.cpp

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")

Cancel 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.

CheckResultAgainst
Primitives≤ 2.5e-6vs float64; dequantisation bit-exact
Tiny Llama, cached vs uncached≤ 1.3e-6vs float64 PyTorch; cached == uncached exactly
SmolLM2-135M logits (CPU)7e-5Q8_0 vs Hugging Face float32
Greedy tokens (Metal)72 / 72identical on every format
Tokenizer191 / 191identical token IDs vs Hugging Face
Numerical validation details

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, GenerationStream
  • AlpacaMetalMetal context, kernels, GPU Llama model and sessions
  • AlpacaModelsGGUF parser, weight mapping, tokenizer construction
  • AlpacaTokenizersByte-level BPE
  • AlpacaCoreTensors, CPU reference ops, KV cache, sampler
Architecture guide

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.