Skip to content

feat(cloudtpu): 1-graph decode, native S4 + BF16 + pad8, and c=16 continuous batching - #27

Open
saariuslystoned wants to merge 9 commits into
feat/ggml-cloudtpu-decode-liftfrom
feat/ggml-cloudtpu-vllm-parity
Open

saariuslystoned wants to merge 9 commits into
feat/ggml-cloudtpu-decode-liftfrom
feat/ggml-cloudtpu-vllm-parity

Conversation

@saariuslystoned

@saariuslystoned saariuslystoned commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #25 (feat/ggml-cloudtpu-decode-lift @ 161e8841) — do not merge until #18 -> #19 -> #25 land (left OPEN for review).

Summary

Closes the pure-StableHLO acceleration and continuous-batching serving gaps in ggml-cloudtpu while keeping LDD_LIBPYTHON=0, MAPS_LIBPYTHON=0, and zero external dependencies across v5e/v6e/tpu7x:

  1. Whole-Model 1-Graph Single-Token Decode (5e214ed)

    • In-slice KV SSA forwarding (SET_ROWS -> FLASH_ATTN_EXT views) eliminates per-layer alias_conflict graph splits.
    • Collapses 32-layer Llama-3.1-8B & 28-layer Qwen2.5-1.5B decode from 65+ PJRT calls/token down to 1 subgraph/eval (CLOUDTPU_FUSE_ATTN=1, CLOUDTPU_MAX_FUSE_OPS=4096) with full 2 * n_layers KV buffer donation (kv_donation_skipped=0).
  2. Native StableHLO S4 Single-Read Matmul + True BF16 Scales + pad8 MXU Alignment (63a9c99, ea016d6, HEAD)

    • CLOUDTPU_Q4_MODE=i4 (default): Packs Q4_0 (q - 8) and Q4_K (q - 8, m_s4 = m + 8*d) directly into 0.5 B/weight PJRT_Buffer_Type_S4 (0.5 B/w on HBM; host staging uses 1 B/elem low-nibble layout as required by XLA BufferFromHostBuffer byte_strides=nullptr).
    • Factored (dot_b * d) + (xsum * m) lowering for B3*B2*M <= 32 and in-loop dequant for M > 32, with GET_ROWS + MUL_MAT coexistence (S4/U4 and U8->S4 bitcast) and SHARD_N/SHARD_K SPMD support.
    • Hardware BF16 Q4_K/Q6_K scales (CLOUDTPU_Q4K_BF16_SCALES=1 default) eliminate F16->F32->BF16 roundtrips and 125 MiB f32 lm_head temporaries.
    • CLOUDTPU_MM_PAD_M=8 (default): Zero-pads M < 8 -> 8 before Q4_*_I4 & Q6_K dot_general so M=1 decode lowers to TPU (8,128) MXU instead of VPU.
    • Live v6e fixes: normalized PJRT_Buffer_ToHostBuffer S4 1-B/elem zero-extended nibbles (sign_extend_nibble) in pjrt_runtime.cpp, and defaulted CLOUDTPU_ASYNC_COLLECTIVES=0 (opt-in 1) in ggml-cloudtpu.cpp to avoid legacy Aug 15 2025 libtpu.so MegaChip base=0 compile errors on P=8.
  3. Continuous Batching Static Padded Buckets (51ff7eb, 6dad6e9, 09fda45)

    • CLOUDTPU_BATCH_BUCKETS=1,2,4,8,16,32,64,128,256,512: Dataflow token-axis analysis maps dynamic n_tokens onto static buckets (failing closed on ambiguity), pads non-KV token tables (mixed_embd) safely, uses -1 sentinel scatter indices so padded tokens never corrupt live KV cache slots, handles partial GET_ROWS (N_out < T), and slices logical outputs [0..T) cleanly on readback.

Verified Evidence

Local Host Mock (test_cloudtpu_host, post-final-edit)

  • ./llama-cpp-cloudtpu/build.sh standalone: 1/1 Passed, CLOUDTPU_MOCK=1 (1-chip & 8-chip SPMD) ALL PASSED (0 failures), MAPS_LIBPYTHON=0.
  • Clean-room audit across all 100 tracked files: PASSED.

Live Colab TPU v5e-1 (libtpu.so, LDD_LIBPYTHON=0, MAPS_LIBPYTHON=0)

  • test-backend-ops -b CLOUDTPU (09fda45 / ea016d6 / 8da23c2): MUL_MAT (1,361/1,361), MUL_MAT q4_K|q6_K|q4_0|q8_0 n=1|16 (57/57), ROPE (287/287), CPY (157/157), ADD (153/153), MUL (140/140), SOFT_MAX (107/107), CONT (89/89), SET_ROWS (65/65), RMS_NORM (61/61), GET_ROWS (56/56), SCALE (12/12), UNARY/GLU PASS, targeted FLASH_ATTN_EXT (hsk=128,kv=512,nb=1,type_K=f16,type_V=f16) 1/1 OK.
  • Instant Bucket Proof (llama-bench -p 3,5,6,7,9,10,11 -n 0 on Qwen2.5-1.5B): cache_entries=3 total across all 7 odd token lengths (bucketed=2 each, kv_donation_skipped=0).
  • End-to-End GGUF llama-bench -ngl 99 -fa 1 -p 512 -n 64 (single stream, subgraphs/eval = 1.0, kv_donation_skipped = 0):
    • Qwen2.5-1.5B Q4_K_M: pp512 = 30,176.38 (+38% vs M4C 21,836.23), tg64 = 244.97 tok/s (+16.4% vs M4C 210.47).
    • Llama-3.1-8B Q4_K_M: pp512 = 7,021.04 (vs M4C 6,555.99), tg64 = 83.25 tok/s (84.15 @ MAX_FUSE_OPS=256, +17.2% vs M4C 71.03; +20.6% over unpadded 1-graph 69.03).
  • Continuous Batching (llama-server -np 16 -cb -kvu w/ LLAMA_KV_ROWS_DISABLE=1, coherent across all streams, kv_donation_skipped = 0):
    • Llama-3.1-8B Q4_K_M: c=1 70.54 wall / 72.35 decode -> c=4 242.75 wall / 256.31 decode (3.54x) -> c=8 473.52 decode (6.54x) -> c=16 756.55 wall / 787.79 decode tok/s (10.9x scaling over c=1) (evals=418, subgraphs=418, bucketed=83).
    • Qwen2.5-1.5B Q4_K_M: c=1 196.32 wall / 203.76 decode -> c=16 1,331.86 decode tok/s (evals=342, subgraphs=342, bucketed=14).

Live GCP v6e-8 Silicon (TPU v6 lite Aug 15 2025 libtpu.so, LDD_LIBPYTHON=0, MAPS_LIBPYTHON=0)

  • test_cloudtpu_host (CLOUDTPU_MOCK=0): ALL PASSED (0 failures) on live v6e-8 (including S4 H2D/D2H, 1/4/8-chip collectives, BF16 scales, pad8, 1-graph KV donation, and continuous-batching buckets).
  • test-backend-ops -b CLOUDTPU (CLOUDTPU_SPMD_CHIPS=8): SET_ROWS & RMS_NORM passed; quantized MUL_MAT (q4_K|q6_K|q4_0|q8_0) passed 381/381; GET_ROWS (CLOUDTPU_SPMD_CHIPS=1 CLOUDTPU_Q4K_BF16_SCALES=0, q4_K|q6_K|q4_0) passed 15/15.
  • Single-Chip v6e-1 (CLOUDTPU_SPMD_CHIPS=1 -sm none -mg 0): Qwen2.5-1.5B Q4_K_M pp512 = 16,262.01 / tg64 = 73.92 (+35.6% tg64 vs M4A 54.52); Llama-3.1-8B Q4_K_M pp512 = 7,557.94 / tg64 = 52.98, coherent 16/16 via llama-server -np 16 -cb -kvu (50.00 decode at c=1, 20.30 decode at c=16, MAPS_LIBPYTHON=0).
  • Multi-Chip v6e-8 (CLOUDTPU_ASYNC_COLLECTIVES=0): Confirmed CLOUDTPU_ASYNC_COLLECTIVES=0 avoids the legacy libtpu.so MegaChip base=0 compile failure (Qwen2.5-1.5B -p 4 -n 4: pp4 = 61.25 / tg4 = 28.79, 2.54x tg4 vs FUSE_ATTN=0). Note: Ubuntu 22.04 TPU images ship Python 3.10, causing pip install libtpu to pull legacy 2025 libtpu.so instead of libtpu >= 0.0.53 (cp312); full Qwen3-32B & Llama-3.3-70B P=8 re-benchmarking against libtpu >= 0.0.53 (with build.sh overlay and -sm none notes documented in README.md) remains ready to run on demand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant