Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/MojoLlama/blob/main/SUMMARY.md # MojoLlama Archived Mojo/MAX quantizer and serving research repository (read-only history) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `MojoLlama / SUMMARY.md` 47 lines · 4051 bytes Raw Blame History Source ## Goal - Profile and optimize a pure Mojo TinyLlama inference engine using Mojo performance features (SIMD, parallelize, FMA, batch inference). ## Constraints & Preferences - Pure Mojo only (.mojo); exclude Python/C TurboEngine. - Profile before optimizing — let data decide. - f16 weight path is baseline; Q4_0/Q8_0 acceptable only if SIMD-vectorized decode beats f16 throughput. ## Progress ### Done - Benchmarked full scaling curve: B=1 (23.7 tok/s), B=2 (37.9 tok/s, 80% eff.), B=4 (64.7 tok/s, 68% eff.), B=8 (84.1 tok/s, 44% eff.) — 50 gen tokens, sustained load. - Implemented SIMD-vectorized GQA attention (dot product + weighted sum inner loops, W=8): 10% total improvement. - Added RoPE precompute (HD/2 cos/sin per position, not per-head): eliminated ~101K pow() calls per B-set. - Rewrote both matmul functions with explicit register accumulators (no InlineArray spill): +6% (82.3 tok/s from 77.8). - All B=4 batch items produce identical token sequences vs single baseline ("2+2=" output matches exactly across items). - Benchmarked Q4_0 matmul (2048×2048, standalone): 213 µs vs f16's 161 µs — 32% slower. Scalar nibble extraction overhead outweighs 3.56× DRAM savings. - Identified Q8_0 quantization as better path: 1.88× compression, trivial SIMD decode (int8→float32 cast, no nibble extraction). - Implemented Q8_0 matmul + weight converter (`_mm_q8_batch`, `_mm_q8_2out_batch`), integrated into batched engine. - Fixed h2f bitcast bug: `Float32(UInt32)` in Mojo does numeric conversion, not bitcast. Correct form uses pointer aliasing (`alloc[UInt8](4)` → store UInt32 → read Float32 from same address). Original batch engine was correct (pointer-based); only the new Q8 file had the regression. - Fixed `f32_to_f16_bits` similarly (was reading f32 bits with numeric `UInt32(Float32)`). - Fixed Q8_0 in-place conversion (temp buffer for f16 row reads to avoid cross-row overlap). - Fixed Q8_0 sign encoding (uint8 with +128 offset + subtract 128 in matmul). - Fixed vocab parsing single-line for-loop bug (`off += plen` was inside loop body). - Q8_0 B=4: 88.1 tok/s (+13% over f16's 77.8 tok/s). Conversion overhead: ~9.7s one-time. - All prior benchmarks valid — both `tinyllama_gen.mojo` and `tinyllama_gen_batch.mojo` already used correct pointer-based `h2f`. ### In Progress - (none) ### Known Issues - Q8_0 quantization error accumulation over 22 layers degrades output quality (garbled text). Performance gains are valid but accuracy is reduced vs f16. ## Key Decisions - B=4 is the sweet spot for batch inference — 3.28× cumulative throughput over single with 68% scaling efficiency. B=8 gives only +30% more cumulative tok/s. - Q4_0 deferred — scalar nibble extraction makes decode 32% slower than f16. Without native shuffle intrinsics (vpshufb), SIMD interleave is infeasible in Mojo. - Q8_0 implemented — 1.88× compression with 13% throughput improvement (less than ideal because f16 already has per-weight `h2f` decode overhead, and Q8_0's extra arithmetic (uint8→float32, sub 128, mul scale) partially offsets bandwidth savings). - `Float32(UInt32)` does numeric conversion, not bitcast — all bitcasts must use pointer aliasing or `SIMD.reinterpret()`. - `comptime if` replaces deprecated `@parameter if` for compile-time dispatch. - `Int(-49.5)` truncates toward zero in Mojo (same as Python). ## Relevant Files - `src/mojollama/mojo_engine/tinyllama_gen_batch.mojo`: Main batched inference engine (B=4, SIMD attention, explicit-register matmuls, correctness verification). - `src/mojollama/mojo_engine/tinyllama_gen.mojo`: Original single-token baseline (f16, max_gen=10). - `src/mojollama/mojo_engine/tinyllama_gen_q8.mojo`: Q8_0 quantized weights + batch B=4 (88.1 tok/s). - `src/mojollama/mojo_engine/bench_q4.mojo`: Q4_0 vs f16 standalone matmul benchmark. - `src/mojollama/mojo_engine/build_batch.sh` / `build_q8.sh`: Build scripts. - `src/mojollama/mojo_engine/bench_batch.sh`: Automation for B value scaling.