- Category
- AI Tools
- Rank
- No. 453Tools index
- Pricing
- Open Source
- Type
- TOOL
- Builder
- karpathy
- GitHub
- 20.1k stars
- Date
About
Andrej Karpathy's Llama 2 inference in one file of pure C — minimalist reference for understanding LLM inference end-to-end.
What it does
Train a Llama 2 architecture in PyTorch, export its weights, then generate text through a compiled command-line program. The inference engine memory-maps checkpoints, tokenizes prompts, maintains an attention cache, and samples tokens autoregressively. A separate path runs quantized weights with integer-heavy matrix multiplication.
Why it's ranked here
This is a strong teaching and experimentation tool because training, checkpoint conversion, tokenization, sampling, and inference sit in one compact repository. Small models can run interactively, while the readable implementation exposes the full computation. Its value drops for large models, where float32 storage and CPU execution become slow and memory-heavy.
What's good
The project connects model training directly to a small native inference engine. It supports prompts, chat mode, temperature, top-p sampling, grouped multiquery attention, and memory-mapped checkpoints. The quantized path keeps sensitive normalization parameters in float32 while moving matrix-multiplication weights and activations to int8. Build targets cover basic, debug, optimized, multithreaded, and Windows configurations.
Tradeoffs
The default inference path stores every weight in float32, producing large checkpoints and poor practicality beyond seven billion parameters. Quantization reduces size and can improve speed, but adds runtime conversion overhead and gives up some numerical correctness. Code Llama support is explicitly incomplete and buggy because some architecture constants differ. Meta checkpoints also require separate access and conversion.
How to use it well
Use it to learn transformer inference, validate exported checkpoints, or train narrow small models before running them locally. Start with the supplied tiny-model workflow, compare deterministic and top-p sampling, then inspect the quantized path to understand performance tradeoffs. Do not choose it for large-model throughput, broad architecture compatibility, or a ready-made network serving API.
Technical notes+
train.py trains the architecture defined in model.py, including distributed data parallel support, gradient accumulation, mixed precision, and checkpoint export. export.py converts repository, Meta, and Hugging Face model forms into legacy float32, header-based float32, or Q8_0 binary formats. run.c memory-maps float32 weights and implements RMS normalization, rotary embeddings, grouped multiquery attention, a key-value cache, and OpenMP-parallel matrix multiplication. runq.c reads version 2 checkpoints, dynamically quantizes activations, and performs int8-oriented inference while retaining normalization tensors in float32. Makefile provides standard, debug, fast, OpenMP, GNU, Windows, and test targets. test.c checks prompt tokenization against expected token sequences, while win.c supplies Windows memory-mapping compatibility.
Observed
- Primary languages
- C for native inference and Python for training, export, tokenization, and sampling workflows.
- Install surface
- Native binaries compile through Make; Python dependencies are pinned in requirements.txt.
- Interface
- Command-line programs accept checkpoints, prompts, sampling controls, tokenizer paths, and chat mode.
- Checkpoint formats
- Supports legacy float32, header-based float32, and grouped Q8_0 int8 exports.
- Platform support
- Build targets cover GCC or Clang environments, OpenMP builds, GNU-compatible systems, and Windows cross-compilation.
- Testing
- Make targets run pytest and a standalone C tokenizer test.
Read from README.md, Makefile, requirements.txt, doc/stories260K.md, doc/train_llama_tokenizer.md, run.c, win.c, runq.c, test.c, model.py, train.py, export.py, sample.py.
What it can do
Run Llama 2 model inference
Text prompt → Generated text response
Load pre-trained Llama 2 model weights
Model checkpoint file → Loaded model in memory
Tokenize input text
Raw text string → Token sequence
Generate text completion
Partial text prompt → Completed text sequence
Perform forward pass computation
Token embeddings → Next token probabilities
Tags
Tech Stack
Comments (0)
No comments yet
Editorially curated, with community endorsements as a secondary signal. Corrections welcome.
