Skip to content

llama2.c

A small C program that runs small Llama 2 language models, with training done in PyTorch.

Stars
20.2k
Forks
2.7k
Licence
MIT
Language
C
Last update
2.3 years ago
Type
Other

No commits in over a year — check for forks or alternatives before adopting.

What it is

This project runs Llama 2 style language models using a single short C file with no dependencies. The README says you train a model in PyTorch and then run it with that C program. It is mainly built for minimalism and learning how inference works, not for running large models quickly.

  • Download the 15M parameter TinyStories checkpoint and generate short stories on your own machine
  • Sample text with custom temperature, top-p, and prompt settings using command line flags
  • Export Meta's Llama 2 checkpoints to the llama2.c format and run the 7B base model, as described in the README
  • Chat with a Llama 2 chat model using the -m chat flag

Great for

  • Developers who want to see how LLM inference works in plain C
  • Students learning the Llama 2 architecture end to end
  • People experimenting with tiny models trained on narrow datasets like TinyStories
  • Anyone who wants a minimal inference engine with no external dependencies

Not ideal for

  • People who need to run 7B or larger models, since the README says the code only does float32 inference and 13B+ does not currently work
  • People who need fast production inference, since the README describes it as educational and float32-only
  • People who want a polished, supported product, since the README says the project is young and moving quickly
Step-by-step guide: how to use llama2.cInstall, first run, examples, tips and FAQs

How to get it

  1. 1

    Clone and build from source

    You'll need: C compiler with make available · Python with pip for exporting Meta checkpoints (requirements.txt)

    Clone the repository

    git clone https://github.com/karpathy/llama2.c.git

    Open the repository folder

    cd llama2.c

    Download the 15M parameter TinyStories model

    wget https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin

    Compile and run the C code

    make run
  2. 2

    Run it

    • Clone the repository and change into its folder
    • Download the stories15M.bin checkpoint (about 60MB)
    • Compile with make run, then run ./run stories15M.bin to see a streamed sample
    • Try the larger stories42M.bin model for more coherent output
  3. 3

    Get the most out of it

    • Use the Makefile compile flags, which the README says can significantly speed up inference
    • Tune sampling with -t for temperature or -p for top-p, but not both at once, as the README recommends
    • Use the -i flag to give the model a prompt and -n to set the number of steps
    • Export a Meta Llama 2 chat model with export.py and run it with -m chat

Downloads

Strengths

  • Inference is a single short C file with no dependencies, which makes it easy to read
  • The README includes a full train-plus-inference workflow in one repo
  • Small models trained on narrow data can produce surprisingly coherent output, as shown in the examples

Trade-offs

  • The README says only fp32 inference is supported, so large models need a lot of memory and run slowly
  • 13B and larger models do not currently work according to the README
  • Code Llama support is incomplete and the README calls it buggy

How it compares

ProjectStarsTypeLevelUpdatedBest for
llama2.c (this)20.2kOtherintermediate2.3 years agoDevelopers who want to see how LLM inference works in plain C
llama.cpp133kLibraryintermediate4 days agoDevelopers who want to run open-weight models locally without a cloud API

Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.