Article complete

Andrej Karpathy Trains GPT-2 in Pure C Without PyTorch

The llm.c project, available on GitHub, offers a simple approach to implementing GPT-2 training on CPU/fp32 in just around 1,000 lines of code. 

A man with a beard smiles at the camera, set against a digitally abstract background with red circles and hexagonal patterns.

Key takeaways

  • Andrej Karpathy introduces llm.c, enabling GPT-2 training in pure C without PyTorch dependencies.
  • The project boasts around 1,000 lines of code for efficient CPU/fp32 training.
  • llm.c matches PyTorch performance with instant compilation and a constant memory footprint.
  • Karpathy meticulously implements core model functions, ensuring accuracy in tensor operations.
  • Future plans include porting llm.c to CUDA for enhanced performance without heavy dependencies.

Takeaways drafted with AI from the article text.

Former OpenAI researcher Andrej Karpathy has introduced  llm.c, a project aimed at training LLMs systems in pure C without the hefty dependencies of PyTorch and cPython. 

https://twitter.com/karpathy/status/1777427944971083809

The llm.c project, available on GitHub, offers a simple approach to implementing GPT-2 training on CPU/fp32 in just around 1,000 lines of code. 

“I chose GPT-2 as the first working example because it is the grand-daddy of LLMs, the first time the modern stack was put together,” wrote Karpathy in his GitHub repository.  

One of the key advantages of llm.c is its instant compilation and execution, matching the performance of the PyTorch reference implementation. By allocating memory in a single block at the beginning of training, llm.c maintains a constant memory footprint, enhancing efficiency during data streaming and batch processing.

The core of llm.c lies in manually implementing forward and backward passes for individual layers like layernorm, encoder, matmul, self-attention, gelu, residual, softmax, and cross-entropy loss. This meticulous process ensures accurate pointer arrangements and tensor offsets, crucial for seamless model operation.

“I am curious to learn more about Rust and totally understand the appeal. But I still find C so nice, simple, clean, portable and beautiful, aesthetically. It's as close as you want to get to direct communion with the machine,” wrote Karpathy. 

Karpathy's next endeavor involves porting llm.c to CUDA layer by layer, aiming for efficient performance comparable to PyTorch but without the heavyweight dependencies. This transition to CUDA opens doors for lowering precision from fp32 to fp16/below and supporting modern architectures like llama 2, Mistral, Gemma, and more.

Published

Spotted an error? Tell our editors. Read our editorial standards and corrections.

More in this story

Andrej Karpathy · 5 stories
  1. Andrej Karpathy Launches A New LLM Tutorial
  2. You are here: Andrej Karpathy Trains GPT-2 in Pure C Without PyTorch
  3. Andrej Karpathy Reproduces GPT-2 in Latest Tutorial
  4. Andrej Karpathy Releases nanochat, a Minimal ChatGPT Clone
  5. Andrej Karpathy Praises Cursor Over GitHub Copilot
About the author

Tech Journalist

Siddharth is a media graduate who loves to explore tech through journalism and putting forward ideas worth pondering about in the era of artificial intelligence.

All stories
Belamy · Daily

The day’s AI news, before your first meeting.