A neural network library written in Zig.
Comptime-shaped tensors, a unified autograd runtime, and GPU acceleration (Metal/MPS on macOS, CUDA on Linux).
- Tensor:
Tensor(T, shape)with comptime shape and f16/f32/f64 support - Unified runtime:
nn.unified.*exposes a single API that works overDiffCpuRuntime/DiffMpsRuntime/DiffCudaRuntimevia comptime dispatch (no vtable, no runtime cost). Switch backends bytrainer(Model, .cpu|.cuda). - Autograd: reverse-mode automatic differentiation
- Layers: Linear, Conv2D, MaxPool2D, LayerNorm, Dropout, Embedding, (Causal / Cross / Multi-head) Attention, Transformer encoder/decoder
- Optimizers: Adam with weight decay, LR schedules (cosine annealing, linear warmup, warmup-cosine)
- GPU backends:
- Metal / MPSGraph (macOS, Apple Silicon)
- CUDA / cuBLAS (Linux, opt-in via
-Dcuda=true)
- GGUF: load and run quantized models (Q4_0, Q4_1, Q8_0)
- Tokenizers: BPE (GPT-2), SentencePiece (Gemma)
- Checkpoints:
save_checkpoint/load_checkpointfor parameter I/O - Determinism: fix every stochastic path (parameter init, dropout,
BatchIterator) viaTrainer.Config{ .seed = 42 } - Logging / Profiling: scoped logger, stderr/stdout/profile-file sinks, controlled by env vars (see docs/logging.md)
- Zig 0.16.0
- macOS: Xcode Command Line Tools (Metal / MPSGraph / Accelerate)
- Linux: OpenBLAS (
OPENBLAS_PATH) for CPU, CUDA toolkit + cuBLAS (CUDA_PATH,CUBLAS_PATH) for the optional CUDA backend
The Nix flake wires all of the above up for you.
git clone https://github.com/soyukke/zig-nn.git nn
cd nn
nix develop # or `direnv allow` / `direnv reload` if you use direnv
zig build test # run tests
zig build xor # run the XOR demozig build test
zig build xorOn Nix, prepend
NIX_CFLAGS_COMPILE=""tozig buildcommands — thejustfilealready handles this.
Every training example accepts an optional backend argument
(cpu default; cuda on Linux; gemma3 additionally accepts metal and
qlora).
| Example | Command | Description | Code |
|---|---|---|---|
| XOR | zig build xor (or -- cuda) |
Basic XOR classification | examples/xor/main.zig |
| Spiral | zig build spiral |
2D spiral classification | examples/spiral/main.zig |
| CharLM | zig build charlm (or -- cuda) |
Character-level Transformer LM | examples/charlm/main.zig |
| Diffusion | zig build diffusion |
DDPM diffusion model | examples/diffusion/main.zig |
| GPT-2 | zig build gpt2 -Doptimize=ReleaseFast |
GPT-2 text generation (requires GGUF model) | examples/gpt2/main.zig |
| Gemma 3 | zig build gemma3 -Doptimize=ReleaseFast -- [cpu|metal|qlora] |
Gemma 3 1B inference / QLoRA fine-tuning | examples/gemma3/main.zig |
# GPT-2 Q4_0
mkdir -p models
curl -L -o models/gpt2.gguf "https://huggingface.co/QuantFactory/gpt2-GGUF/resolve/main/gpt2.Q4_0.gguf"Gemma 3 examples expect models/gemma3-1b.gguf.
Fix the seed to make parameter init, dropout, and shuffling bitwise deterministic across runs and backends:
var t = try nn.unified.trainer(Model, .cpu)
.init(allocator, {}, .{ .lr = 1e-3, .seed = 42 });BatchIterator.init_with_seed(...) offers the same knob for data shuffling.
NN_LOG_LEVEL, NN_LOG_SCOPES, NN_PROFILE, NN_PROFILE_DIR control
scoped logging and profile artifacts. See docs/logging.md
for the full spec.
NN_LOG_LEVEL=debug NN_LOG_SCOPES=metal,cuda zig build gemma3PyTorch comparison scripts are in benchmarks/:
python benchmarks/bench_xor.py
python benchmarks/bench_spiral.py
python benchmarks/bench_charlm.py
python benchmarks/bench_ddpm.py
python benchmarks/bench_qlora_pytorch.py # requires transformers, peft
bash benchmarks/run_all.shMIT