NoTorch: Neural networks in pure C (2-file library, BitNet 1.58) [P]
I'm tired of `pip install torch` eating 2.7 GB every time I want to train a 10m-param model, so I wrote NOTORCH: a complete neural network training/inference library in pure C. Two files (`notorch.h` + `notorch.c`, ~3300 LOC). No Python. Enough.
Compiles (under a second):
'''
cc -O2 notorch.c your_model.c -lm -o train
'''
**Example:** All we know Karpathy's nanoGPT, so for the sake of code I ported nanoGPT to NOTORCH and retrained from scratch on a Dracula corpus instead of Shakespeare (because enough of fairy tailes).
Same architecture, same training loop, zero PyTorch. Runs, converges, produces coherent-ish output. The link:
https://github.com/ariannamethod/nanoGPT-notorch
---
Core:
- Full autograd, 31 ops with finite-difference-verified backward
- Adam / AdamW / Chuck (our variant of Adam, dedicated to Chuck Norris RIP)
- BitNet b1.58 ternary quantization — forward + STE backward + BLAS `sgemm` fast path
- SwiGLU / GQA / RoPE / MHA / GEGLU / RMSNorm / LayerNorm
- BPE tokenizer, GGUF loader (F32/F16/Q4_0/Q5_0/Q8_0/Q4_K/Q6_K)
- LR schedules, NaN guard, gradient clipping/accumulation, checkpointing
- LoRA-style parameter freezing
- DPO / GRPO / knowledge-distillation training examples
- Apple Accelerate (macOS) / OpenBLAS (Linux) / CUDA
Brutal Reality Stress Check: two transformer trainings running concurrently on a poor **2019 Intel i5 MacBook, 8 GB RAM**, ~222 MB total for both. Not M1. Pre-AMX Intel. Import overhead: 0 ms (it's C). So even this 2019 calculator is able to handle this.
Limits: CPU-friendly up to ~100M params (let's be realistic); for bigger models you want a GPU. CUDA backend exists, CPU+BLAS is the daily driver.
GitHub repo:
https://github.com/ariannamethod/notorch
(the list of models trained on NOTORCH + projects built on it: see the README's "Projects powered by notorch" section)
Feedbacks, commits, criticism, thoughts, anything — yall are welcome.
[link] [comments]
Want to read more?
Check out the full article on the original site