GitHub · llama2.c: Inference Llama 2 in one file of pure C
Open the sourceKarpathy presents llama2.c, a minimal train-in-PyTorch, inference-in-pure-C implementation of the Llama 2 architecture, and uses it to teach that small models on narrow domains can perform surprisingly well. He explains practical topics such as sampling settings, int8 quantization, custom tokenizers, hyperparameter tuning for training, and CPU performance flags. He also states a design philosophy that puts simplicity, readability and hackability ahead of maximum efficiency.
- He argues that very small LLMs can perform strongly when the domain is narrow enough, citing TinyStories. - He recommends sampling with temperature 1.0 and top-p 0.9, and varying only one of the two to control diversity. - He explains int8 (Q8_0) quantization of weights and activations, which in his test gave a 3X speedup and a 4X smaller checkpoint at slightly lower quality. - He teaches that a custom small-vocab tokenizer can make models smaller and faster on a specific dataset. - He gives a brief training guide: scale dims using the Chinchilla tables, aim for about 100K tokens per update, and set the learning rate as high as training allows. - He wants the repo to stay a simple, hackable reference implementation that people fork, rather than a full-featured framework.
Based on Andrej Karpathy's public content. Not affiliated with or endorsed by Andrej Karpathy.
Supports these rules
Quotes from this video
“very small LLMs can have surprisingly strong performance if you make the domain narrow enough”
“to go "off the rails" afterwards. More generally, to control the diversity of samples use either the temperature (i.e. vary `-t` between 0 and 1 and keep top-p off with `-p 0`) or the top-p value (i.e. vary `-p` between 0 and 1 and keep `-t 1`), but not both.”
“You want this to be as high as your training allows.”
“3e-4 is a safe choice in most medium-sized applications, but can be too low for small networks, so try to increase it!”
“This repo still cares about efficiency, but not at the cost of simplicity, readability or portability.”
“It shouldn't be full-featured. It shouldn't take 100 different options or settings. It shouldn't be the most efficient.”
“I mostly only ever tune these parameters and leave most of the others unchanged.”
“But more is not always better, usually this is a bit U shaped.”