llama2.c

llama2.c

โ˜… 4.4 ๐Ÿ”– 7000 saves ๐Ÿ’ฐ Free GitHub

llama2.c is an open-source project by Andrej Karpathy that runs inference for Meta’s Llama 2 language models in a single file of pure C โ€” roughly a thousand lines with no dependencies. With more than 20,100 stars on GitHub, it has become a beloved reference for anyone who wants to understand exactly how a transformer model loads weights and generates text, stripped of framework magic. The repository was last updated in 2024 and remains a stable, finished piece of work.

Overview

The project pairs a compact C inference engine (llama2.c, plus an OpenMP variant for multi-core speed) with a PyTorch training codebase that trains small Llama 2 architectures from scratch on datasets such as TinyStories. Pre-trained checkpoints from 15M to 110M parameters can be downloaded and run immediately, while the original Meta Llama 2 weights (7B and up) can be exported into the same simple binary format. The result is an end-to-end, hackable pipeline that covers tokenization, training, and fast CPU inference in code a single person can read in an afternoon.

Key Features

  • Full Llama 2 inference in one dependency-free C file of about 1,000 lines
  • Train baby Llama 2 models in PyTorch on TinyStories and custom datasets
  • Ready-to-run checkpoints from 15M to 110M parameters
  • Exporter converts Meta’s Llama 2 weights (7Bโ€“70B) to the raw binary format
  • Optional OpenMP build for multi-threaded CPU inference
  • SentencePiece tokenizer support and sampling controls for temperature and top-p
  • Companion implementations in many other languages linked from the community

๐Ÿ’ณ Pricing

llama2.c is completely free and open source under the MIT license.

Who Is It For

llama2.c is perfect for students, educators, and engineers who want to learn how LLM inference really works, and for hobbyists who enjoy running language models in the most minimal environment possible. Its clarity has also made it a popular starting point for ports, experiments, and tiny on-device language model projects of every kind.

๐Ÿš€ Visit llama2.c Official Site

๐Ÿšฉ Report an issue