Briefing

World's Smallest Llama2 Inference Engine Fits in 1369 Bytes

ai-dev
by github.com by rdmsr · Llama

Explore the 1369‑byte Llama2 inference engine by running ./download.sh && python3 quantize.py && make run to see minimal‑size deployment in action.

What to do now

Explore the repository, run ./download.sh && python3 quantize.py && make run to see the inference engine in action.

Summary

A new Llama2 inference engine has been built that occupies only 1,369 bytes of x86 real‑mode assembly, allowing it to boot directly from disk and generate text before any operating system loads. The engine loads a quantized stories260K model—260,000 parameters across five layers, eight attention heads, and a 512‑token vocabulary—directly into high memory, then performs a full transformer forward pass for each token. Quantization uses int8 weights with a global absmax scale, and pre‑computed exp and silu lookup tables are embedded to reduce decoding overhead. The Q/K/V and gate/up weight matrices are fused so the assembly can issue a single matmul call, while the KV cache is also quantized to int8 at runtime with a per‑token scale stored in a separate buffer, keeping the cache small enough for the full 512‑token context. Sampling is currently limited to greedy argmax, but the design leaves room for more sophisticated techniques. The code is heavily golfed, so performance and precision are not optimal, and the architecture and prompt are hard‑coded. Extending the engine to larger models like stories15M would require switching to protected or unreal mode.

The project includes a Python script (quantize.py) that packs the model into a custom binary format, a makefile that builds the assembly, and a download.sh script to fetch the model data. Contributors are invited to shrink the binary further, and the repository encourages adding a name to the code as a sign of participation.

Key changes

  • 1,369‑byte x86 real‑mode assembly inference engine for Llama2 stories260K
  • Loads quantized int8 model with global absmax scale
  • Pre‑computed exp and silu lookup tables embedded
  • Q/K/V and gate/up matrices fused for single matmul
  • KV cache quantized to int8 with per‑token scale
  • Greedy argmax sampling only
  • Hardcoded architecture and prompt
  • Extending to larger models requires protected mode

Affects

none

Customer impact

Analyzing matches…

Ask about this story

Impact on an agency? Which customers? Compare historically Risks of waiting