moebiusV/cugpt

C translations of microgpt: tape autograd version and BLAS manual backprop version

5

stars

0

commits

C

primary language

Mar 16, 2026

updated

README

cugpt — C Translation of Karpathy's microgpt

Two C implementations of Andrej Karpathy's microgpt.py: a minimal GPT that trains a character-level language model on a names dataset and generates hallucinated names.

FileDescription
microgpt.pyOriginal pure-Python reference by Karpathy (benchmark baseline, not in repo)
microgpt.cC translation — Wengert tape autograd, zero dependencies beyond libc/libm
cugpt.cC translation — manual backward pass, OpenBLAS-accelerated BLAS

Quick Start

make
./microgpt    # 452× faster than Python
./cugpt       # 2476× faster than Python

Both download input.txt automatically on first run. The original Python reference is at the gist (not included here).

Model

1-layer GPT-2 variant · vocab=27 · n_embd=16 · n_head=4 · block_size=16 · 4,192 params RMSNorm · ReLU · no biases · Adam (lr=0.01, β₁=0.85, β₂=0.99, linear decay) · 1,000 steps

Benchmark

Machine: Ubuntu 22.04 / WSL2, gcc 14.2.0, Python 3.13.3 — 1,000 training steps on names dataset.

Python baseline

Time
python3 microgpt.py146.11 s

microgpt.c — compiler flag sweep

FlagsTime (s)Speedup vs Python
-O01.59292×
-O10.760192×
-O20.718203×
-O30.729200×
-O2 -march=native0.588248×
-O3 -march=native0.637229×
-O3 -march=native -ffast-math0.337434×
-O3 -march=native -ffast-math -funroll-loops0.323452× ← default
-O3 -march=native -ffast-math -flto0.336435×
-O3 -march=native -ffast-math -funroll-loops -flto0.332440×

cugpt.c — compiler flag sweep

FlagsTime (s)Speedup vs Python
-O00.189773×
-O10.0901623×
-O20.0781873×
-O30.0732001×
-O2 -march=native0.0771897×
-O3 -march=native0.0821782×
-O3 -march=native -ffast-math0.0612395×
-O3 -march=native -ffast-math -funroll-loops0.0781873×
-O3 -march=native -ffast-math -flto0.0592476× ← default
-O3 -march=native -ffast-math -funroll-loops -flto0.0642283×

Summary

ImplementationBest timeSpeedup vs Pythonvs microgpt.c
microgpt.py (Python)146.11 s
microgpt.c (tape autograd)0.323 s452×
cugpt.c (BLAS + manual grad)0.059 s2476×5.5×

Key observations

  • -ffast-math is the biggest single-flag win (~2×) — it lets the compiler reassociate FP ops and use faster math approximations. Safe here since we only need gradient descent, not bit-exact reproducibility.
  • -funroll-loops helps microgpt.c (tight loops over tape entries) but hurts cugpt.c (BLAS already handles inner loops).
  • -flto wins for cugpt.c by inlining the thin BLAS wrapper functions across translation units.
  • Even unoptimized cugpt.c -O0 (0.189 s) beats the best microgpt.c build (0.323 s) — eliminating the autograd tape is an algorithmic win, not a compiler win.

Implementation Notes

microgpt.c translates the Python scalar-Value autograd graph to a flat Wengert tape (a pre-allocated 4M-entry arena, reset each step). Backward is a single reverse scan — no pointer chasing, no heap allocation, no recursive topo-sort.

cugpt.c discards the tape entirely. Forward activations are saved in a struct array (Activations act[BLOCK_SIZE]). The backward pass is written analytically. Every matrix-vector product is dispatched through cblas_dgemv; weight gradient accumulation uses cblas_dger (rank-1 outer-product update).

Build

./configure && make        # detects compiler, OpenBLAS, cblas.h
make check                 # smoke test (10 steps each)
make bench                 # full flag sweep + Python baseline
make install               # to /usr/local (or ./configure --prefix=~/.local)
make dist                  # creates cugpt-1.0.tar.gz

Dependencies:

  • microgpt.c — C compiler + libm (always present)
  • cugpt.c — C compiler + libopenblas-dev (apt install libopenblas-dev)

See INSTALL for manual build instructions.

License

MIT — see COPYING. Original microgpt.py by Andrej Karpathy, shared under the MIT convention used across his educational ML projects (micrograd, makemore).

moebiusV/cugpt

C translations of microgpt: tape autograd version and BLAS manual backprop version

5

stars

0

commits

C

primary language

Mar 16, 2026

updated

README

cugpt — C Translation of Karpathy's microgpt

Two C implementations of Andrej Karpathy's microgpt.py: a minimal GPT that trains a character-level language model on a names dataset and generates hallucinated names.

FileDescription
microgpt.pyOriginal pure-Python reference by Karpathy (benchmark baseline, not in repo)
microgpt.cC translation — Wengert tape autograd, zero dependencies beyond libc/libm
cugpt.cC translation — manual backward pass, OpenBLAS-accelerated BLAS

Quick Start

make
./microgpt    # 452× faster than Python
./cugpt       # 2476× faster than Python

Both download input.txt automatically on first run. The original Python reference is at the gist (not included here).

Model

1-layer GPT-2 variant · vocab=27 · n_embd=16 · n_head=4 · block_size=16 · 4,192 params RMSNorm · ReLU · no biases · Adam (lr=0.01, β₁=0.85, β₂=0.99, linear decay) · 1,000 steps

Benchmark

Machine: Ubuntu 22.04 / WSL2, gcc 14.2.0, Python 3.13.3 — 1,000 training steps on names dataset.

Python baseline

Time
python3 microgpt.py146.11 s

microgpt.c — compiler flag sweep

FlagsTime (s)Speedup vs Python
-O01.59292×
-O10.760192×
-O20.718203×
-O30.729200×
-O2 -march=native0.588248×
-O3 -march=native0.637229×
-O3 -march=native -ffast-math0.337434×
-O3 -march=native -ffast-math -funroll-loops0.323452× ← default
-O3 -march=native -ffast-math -flto0.336435×
-O3 -march=native -ffast-math -funroll-loops -flto0.332440×

cugpt.c — compiler flag sweep

FlagsTime (s)Speedup vs Python
-O00.189773×
-O10.0901623×
-O20.0781873×
-O30.0732001×
-O2 -march=native0.0771897×
-O3 -march=native0.0821782×
-O3 -march=native -ffast-math0.0612395×
-O3 -march=native -ffast-math -funroll-loops0.0781873×
-O3 -march=native -ffast-math -flto0.0592476× ← default
-O3 -march=native -ffast-math -funroll-loops -flto0.0642283×

Summary

ImplementationBest timeSpeedup vs Pythonvs microgpt.c
microgpt.py (Python)146.11 s
microgpt.c (tape autograd)0.323 s452×
cugpt.c (BLAS + manual grad)0.059 s2476×5.5×

Key observations

  • -ffast-math is the biggest single-flag win (~2×) — it lets the compiler reassociate FP ops and use faster math approximations. Safe here since we only need gradient descent, not bit-exact reproducibility.
  • -funroll-loops helps microgpt.c (tight loops over tape entries) but hurts cugpt.c (BLAS already handles inner loops).
  • -flto wins for cugpt.c by inlining the thin BLAS wrapper functions across translation units.
  • Even unoptimized cugpt.c -O0 (0.189 s) beats the best microgpt.c build (0.323 s) — eliminating the autograd tape is an algorithmic win, not a compiler win.

Implementation Notes

microgpt.c translates the Python scalar-Value autograd graph to a flat Wengert tape (a pre-allocated 4M-entry arena, reset each step). Backward is a single reverse scan — no pointer chasing, no heap allocation, no recursive topo-sort.

cugpt.c discards the tape entirely. Forward activations are saved in a struct array (Activations act[BLOCK_SIZE]). The backward pass is written analytically. Every matrix-vector product is dispatched through cblas_dgemv; weight gradient accumulation uses cblas_dger (rank-1 outer-product update).

Build

./configure && make        # detects compiler, OpenBLAS, cblas.h
make check                 # smoke test (10 steps each)
make bench                 # full flag sweep + Python baseline
make install               # to /usr/local (or ./configure --prefix=~/.local)
make dist                  # creates cugpt-1.0.tar.gz

Dependencies:

  • microgpt.c — C compiler + libm (always present)
  • cugpt.c — C compiler + libopenblas-dev (apt install libopenblas-dev)

See INSTALL for manual build instructions.

License

MIT — see COPYING. Original microgpt.py by Andrej Karpathy, shared under the MIT convention used across his educational ML projects (micrograd, makemore).

Languages

C

80.8%

Makefile

16.1%

M4

3.1%