Autoregressive Language Model on the 6502 Processor
TL;DR Highlight
A ternary-weight Mamba model with 13KB of weights generates text on a 1975 6502 CPU
Who Should Read
A developer curious about quantization and on-device inference who wants to fit an LLM into extremely limited memory and compute
Core Mechanics
- The model was designed around the 6502
- It runs on a 1975 8-bit CPU with 32KB RAM.
- Inference code (9KB) and weights (13KB) fit in 25KB of user space.
- The CPU has no multiply instruction.
- BitNet turns multiplies into adds and subtracts
- Weights are quantized to {-1, 0, 1}.
- An 8x8 multiply-accumulate costs 150 cycles, a ternary accumulate 30.
- Each parameter takes 1.58 bits.
- Four parameters are packed per byte
- Five per byte is denser but needs divide-by-3 to unpack, which is costly on the 6502.
- Four per byte uses 2 bits each and unpacks with a right shift.
- 13KB holds about 52k parameters.
- Mamba was chosen over attention
- Attention's KV cache grows with context and would not fit in 32KB RAM.
- Mamba keeps a fixed-size state vector.
- GRUs diverged in every training run with ternary weights, while Mamba's decay never exceeds 1 so it cannot explode.
- Activations and sampling are all integer
- 16-bit accumulators are shifted by a learned shr into 8 bits, learned in the first half of training and frozen in the second.
- The LM head is int4 and every other matrix is ternary.
- Softmax is replaced by a precomputed exponential lookup table per temperature.
- The result is not smart but it runs
- The vocabulary is 26 letters plus space.
- Training data is derived from TinyStories.
- The RNG seed is fixed, so output is identical every run.
Community Reactions
- Opinion that banked memory should be allowed
- Streaming weights in pages, as was period-appropriate, could support a bigger model.
- Opinion that assembly beats cc65
- A commenter with NES experience says the 6502 suits C poorly and hand-written assembly could save space and time.
- Experience that Markov chains beat tiny neural models
- A commenter says modern ML models scale down poorly and a simple Markov chain variant is almost always better.
How to Apply
- Try ternary quantization on your own model
- Today, wrap one Linear layer's weights with the ternary_quantize below and run a forward pass.
- Compare output quality against the original.
- Pick fixed-state architectures for edge deployment
- If memory is tight, add recurrent models like Mamba to candidates instead of attention.
- First check that memory stays flat as context grows.
- Replace softmax with a lookup table
- Where exp is unavailable, precompute exp of the logit-max difference.
- Build one table per temperature and sample with integers.
Try It
# Run in a PyTorch environment (pip install torch)
import torch
def ternary_quantize(w: torch.Tensor) -> torch.Tensor:
q = torch.clamp(torch.round(w), -1.0, 1.0) # forward: {-1,0,1}
return w + (q - w).detach() # backward: full precision gradient
w = torch.randn(4, 4, requires_grad=True)
x = torch.randn(4)
y = ternary_quantize(w) @ x
y.sum().backward()
print(ternary_quantize(w))
print(w.grad)
# Next: apply it to a small MLP's Linear weights, train it, and compare accuracyTerminology
Related Papers
A Beginner's Guide to Local AI with Inkling, Qwen, and Unsloth
Install Unsloth Desktop, load a quantized Qwen model, and share a private ChatGPT-like AI on your home LAN.
Muru – AI-assisted Rust server for Python apps by mod_WSGI's creator
mod_wsgi's creator is building Muru, an AI-assisted Rust ASGI/WSGI server, documenting it in public
87% of the Anthropic power will come from gas and 11% from coal in 2028
Analysis of Anthropic's contracted data centers: 87% gas, 11% coal power in 2028
Anthropic’s Nasdaq debut
Anthropic listed on Nasdaq and closed its first day below its opening price.
OpenAI still has a WebRTC Problem
WebRTC drops late audio, hurting voice AI; MoQ retransmits and buffers instead
Gemini 4 Neon
Gemini 4 Neon cuts cost per task 32% versus Carbon, at $3 input and $15 output per million tokens
AI is killing All About Berlin
After Google AI Overviews, a Berlin guide site lost 70-75% of its traffic.
Not Everything Needs an LLM
PostHog used small decision models to skip 98.7% of expensive LLM calls