LLM Inference on CPU: When It Makes Sense and How to Optimize
CPU inference for LLMs gets less credit than it deserves. Modern CPUs with AVX-512 support and large L3 caches can run 7B models at 10-20 tokens/second — fast enough for many use cases. For users without dedicated GPUs, or for machines where the GPU is occupied by other tasks, CPU inference is a legitimate option.
CPU Performance Expectations
Performance varies dramatically by CPU. Modern high-core-count desktop chips: Ryzen 9 7950X achieves 15-20 t/s on Llama 3.1 7B Q4_K_M. Intel Core i9-13900K gets 12-15 t/s. Older chips (i7-8700K) manage 4-6 t/s. System RAM bandwidth matters — DDR5 on a 7950X with quad-channel provides ~70 GB/s bandwidth vs DDR4's ~50 GB/s. More cores help (each core handles thread work), but beyond 8-12 active threads there's diminishing returns for llama.cpp's parallelism.
Optimization for CPU Inference
Use Q4_K_M or Q4_K_S quantization — smaller models = less memory bandwidth = faster CPU inference. Set --threads to your physical core count (not hyperthreads — CPU LLM inference doesn't benefit from hyper-threading). Disable --mmap and use --no-mmap to load models fully into RAM — avoids disk I/O during inference. Enable AVX-512 builds of llama.cpp if your CPU supports it (Xeon, some i9/i7) for 20-30% speedup.
Apple Neural Engine
Apple Silicon Macs blur the CPU/GPU boundary. Their Neural Engine (dedicated matrix math hardware) assists inference in addition to the GPU cores. llama.cpp uses Metal to access both the GPU and ANE aspects of Apple's SoC. The result: Apple Silicon CPUs significantly outperform x86 CPUs of similar price for LLM inference — an M4 Mac Mini CPU-only (without GPU offload) matches a mid-range discrete GPU. If your only option is a CPU, any M-series Mac is the best CPU-based AI hardware available.
Comments
Post a Comment