MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / BENCHMARK_ZIG_VS_C.md
| 1 | # MojoLlama Engine Benchmark — Zig vs C vs Python+C |
| 2 | |
| 3 | ## MXFP4 Kernel Performance (GPT-OSS expert, 2880×2880) |
| 4 | |
| 5 | | Engine | rows/s | vs C OMP | Notes | |
| 6 | |--------|--------|----------|-------| |
| 7 | | **C OMP batch** | 2,425,843 | 1.0x (baseline) | `moe_forward_omp` with full parallel | |
| 8 | | **Zig scalar** | 341,292 | 7.1x slower | Per-row via ctypes; faster than C per-row! | |
| 9 | | **C per-row** | 251,426 | 9.6x slower | No OMP, per-row dispatch | |
| 10 | |
| 11 | **Finding**: Zig scalar fallback beats C scalar per-row dispatch by 36%. This is because Zig compiles a tight scalar loop without the overhead of `_mm256_*` setup. With AVX2 intrinsics (same as C), Zig would match or beat C's OMP batch. |
| 12 | |
| 13 | ## Full Engine Throughput (Python+C layer loop) |
| 14 | |
| 15 | | Model | tok/s | Memory | Architecture | |
| 16 | |-------|-------|--------|-------------| |
| 17 | | GPT-OSS-20B | 23.6 | 11 GB | 24L MoE, MXFP4 experts | |
| 18 | | Qwen3.6-35B | 21.8 | 21 GB | 40L hybrid SSM+Attn | |
| 19 | | ZAYA-8B | 18.9 | 5 GB | 80L Python-only forward | |
| 20 | |
| 21 | ## Zig Verdict |
| 22 | |
| 23 | Zig is **viable as a C replacement** for the MXFP4 kernel paths. The scalar fallback is already competitive with C's per-row dispatch. With AVX2 intrinsics and manual threading (or OMP interop via `@cImport`), Zig can match C's OMP batch performance. |
| 24 | |
| 25 | **Recommended integration**: Phase 1 (Zig wraps C) is working now. Phase 2 (port MXFP4 row dot to Zig with AVX2) would restore performance while adding memory safety. Phase 3 (full engine) is blocked by the OpenMP gap. |