feat(cpu): optional BLAS for matmul forward/backward (2.6x end-to-end speedup)#840
Open
BigJai wants to merge 1 commit intokarpathy:masterfrom
Open
feat(cpu): optional BLAS for matmul forward/backward (2.6x end-to-end speedup)#840BigJai wants to merge 1 commit intokarpathy:masterfrom
BigJai wants to merge 1 commit intokarpathy:masterfrom
Conversation
Add opt-in BLAS integration (USE_BLAS=1) for CPU matmul operations, replacing the hand-rolled triple-loop with cblas_sgemm calls. Benchmarked on AMD EPYC (Zen 1), 6 cores, GPT-2 124M, B=4 T=64: - End-to-end training: 2.63x speedup (6454 ms → 2453 ms per step) - Matmul-only (dev/cpu benchmark): 11.6x faster than tiled kernel Supports OpenBLAS (Linux), Apple Accelerate (macOS), or any CBLAS-compatible library. All changes are guarded by #ifdef USE_BLAS; zero behavior change when the flag is not set. Ref: Discussion karpathy#253 (Karpathy greenlit BLAS/SIMD optimization) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds opt-in BLAS integration (
USE_BLAS=1) for CPU matmul operations intrain_gpt2.c, replacing hand-rolled loops withcblas_sgemmcalls. This closes the largest performance gap between llm.c's CPU path and PyTorch (which uses BLAS internally).#ifdef USE_BLAS— zero behavior change when flag is offRef: Discussion #253 — @karpathy greenlit BLAS/SIMD optimizations for the CPU path.
Usage
On macOS,
USE_BLAS=1automatically links Apple's Accelerate framework (no install needed).Benchmarks
Hardware: AMD EPYC (Zen 1), 6 cores, 12GB RAM
Model: GPT-2 124M, B=4, T=64, 40 training steps
End-to-end training (
train_gpt2)Validation loss comparison (numerically equivalent):
Matmul-only (
dev/cpu/matmul_forward.c)B=8, T=1024, C=768, OC=3072 (4 runs each):
Files Changed
Makefiletrain_gpt2.c#ifdef USE_BLASinmatmul_forward+matmul_backwarddev/cpu/matmul_forward.cTotal: +104 lines, clean
#ifdefguards, no changes to existing code paths.Notes
🤖 Generated with Claude Code