Skip to content

feat(cloud-tpu): branchless VREG 4-bit IQ4_XS select-tree LUT kernel + parity tests (#43) - #46

Merged
saariuslystoned merged 4 commits into
mainfrom
feat/43-vreg-iq4xs-select-tree-lut
Dec 1, 2026
Merged

saariuslystoned merged 4 commits into
mainfrom
feat/43-vreg-iq4xs-select-tree-lut

Conversation

@saariuslystoned

@saariuslystoned saariuslystoned commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #43 — Phase-1 branchless 4-bit VREG select-tree LUT host math/decoders, standalone StableHLO emitters, TileIR STABLEHLO_SELECT_TREE lowering + Mosaic guard, and bit-exact/sanitizer parity tests.

Scope note (Refs #43, NOT Closes #43): This PR delivers Phase 1 only. Issue #43 remains open for ggml-cloudtpu.cpp runtime GGUF IQ4_XS ingestion/wiring, gather-cliff size sweep / XLA gather comparison, dispatch-amortized BF16 parity, bare-metal VFIO codebook_dequant, and live Cloud TPU v5e/v6e silicon timing proof.

Summary

  • Branchless 4-bit VREG select-tree LUT & GGUF/sequential decoders (freetile/include/freetile/select_tree_lut.hpp, freetile/src/select_tree_lut.cpp, tools/select_tree_lut.py): implements a 4-level branchless select(bit, hi, lo) tree (15 selects, 0 gather) over 16-entry codebooks (IQ4_XS/IQ4_NL via kValuesIq4Nl, sequential NF4, sequential FP4_E2M1; 2/3-bit truncation via select_tree_f32). Walked via byte-level little-endian loads (zero strict-aliasing UB), overflow-guarded iq4xs_dequant_matmul (including k=0 zero-fill), and zero out-of-bounds tail pointer formation. tools/select_tree_lut.py is a clean-room NumPy reference mirror (produces no MLIR IR).
  • Standalone Cloud TPU StableHLO module + Mock PJRT execution (llama-cpp-cloudtpu/stablehlo_emitter.*, llama-cpp-cloudtpu/tests/test_cloudtpu_host.cpp): emit_select_tree_lut4_program() emits a standalone @main module with 15 stablehlo.select ops and 0 stablehlo.gather ops across U32/U8/U4 index inputs, verified via Mock PJRT execution at {4, 8}.
  • TileIR planning & Mosaic refusal guard (freetile/include/freetile/tile_ir.hpp, freetile/src/tile_ir.cpp, freetile/src/mosaic_emitter.cpp): adds QuantFormat::{IQ4_XS,IQ4_NL,NF4,FP4} and LoweringTarget::STABLEHLO_SELECT_TREE (U4 planning on v5e/v6e M <= 64), and fails closed in emit_mosaic_kernel by throwing before numeric uitofp dequantization.
  • Proof & CI (proof/cloud-tpu-iq4xs-select-tree/PROOF.md, .github/workflows/ci.yml): host bit-exact (max_abs_diff = 0.0, IEEE float bit pattern + finiteness + -0.0f sign-bit parity) and Mock PJRT proof; no live-silicon timing is claimed.

Proof Block (Head a394014cedc544f21c07d8f2f6095e0c2cabba84)

  • FreeTile CMake/CTest + Make: cmake -S freetile -B build-freetile -DCMAKE_BUILD_TYPE=Release && ctest (5/5 PASS, incl. freetile_select_tree_lut_tests over 2304²/3072²/4096²/6144² full weight dequant, matmul full-K,N with M_tile=min(M,8), k=0..300 tails + scalar_iq4_xs_ref, k=0,1,31,257 tail matmul, -0.0f FP4 sign bit, and next_ssa chaining) + make -C freetile test (PASS).
  • Sanitizers (ASan+UBSan): clang++ -std=c++17 -O2 -g -fsanitize=address,undefined -fno-sanitize-recover=all -Wall -Wextra -Wpedantic -Werror ... test_select_tree_lut.cpp (PASS, 0 warnings/errors).
  • Host sibling suites: make -C libopenbuenos verify-header-host host test (PASS) & make -C libopentpu verify-header-host host test (PASS).
  • Cloud TPU Host + Mock PJRT: cmake -S llama-cpp-cloudtpu -B build-cloudtpu && ctest (PASS, test_select_tree_lut4_program verifies 15 selects / 0 gathers at 2304..6144 and executes U32/U8/U4 bit-exactly on Mock PJRT).
  • Python reference & Clean-Room Audit: python3 tools/select_tree_lut.py --self-test & python3 tools/test_select_tree_lut.py -v (7/7 OK, incl. uint8/uint16 NumPy wrap regression & unfused k-ascending accumulation); clean-room audit across 198 tracked files (PASS).
  • Adversarial Review Roundtable (Gemini Neon + Codex gpt-6.2-astra, multi-round pre-push loop):
    • Hardened C++ IQ4_XS/IQ4_NL decoders with explicit byte-offset little-endian reads, k=0 zero-fill matmul semantics, uint64_t dim-bound overflow checks before size_t narrowing in both StableHLO emitters, and constexpr codebook tables.
    • Caught & fixed NumPy 2.x uint8/uint16 wrap in tools/select_tree_lut.py, rejected fractional widths via operator.index, matched unfused k-ascending float32 accumulation, and added U32/U8/U4 Mock PJRT execution + strict float bit-pattern assertions.
    • Aligned LoweringTarget::STABLEHLO_SELECT_TREE, PROOF.md, and PR scope (Refs #43) with actual Phase-1 code behavior and remaining #43 silicon/wiring gates.

saariuslystoned and others added 2 commits November 29, 2026 03:49
…r IQ4_XS/NL dequant (freetpu#46)

- Handle non-multiple K tails (k%32/k%256) in dequant_iq4_nl_row and dequant_iq4_xs_row
  instead of silent truncation leaving outputs unwritten; both C++ and Python mirror
  now dequantize the trailing partial block with correct scale decoding
- Add null-pointer and k==0 guards (throw invalid_argument) fail-closed to all dequant entry points
- Fix iq4xs_dequant_matmul row stride to ceil(K/256)*136 for non-multiple K
- Harden select_tree_select_count against shift UB for bits>=32 (saturate UINT32_MAX) and Python count_selects bits range check
- Fuzz-validated bit-exact tree vs direct and scalar reference over 20k cases

Audit: ASan+UBSan -Wall -Wextra -Werror clean, 10k random + boundary + subnormal + unaligned
Clean-room grep: no private terms
@saariuslystoned

Copy link
Copy Markdown
Collaborator Author

Adversarial Hardening & Public-Roads Audit Receipt (6f435ce5)

  • Prior PR head: 27453f96 → Hardened head: 6f435ce5
  • Fixes landed: non-multiple-K tail truncation (C++/Python), ceil row-stride, shift-UB in select_tree_select_count, and null pointer guards.
  • Verification: Bit-exact IQ4_XS parity vs GGML/libopenbuenos, ASan+UBSan -Wall -Wextra -Werror PASS, 20,000-case fuzz + boundary + unaligned tests green, public-roads diff scan clean.

@saariuslystoned
saariuslystoned merged commit 54dd389 into main Dec 1, 2026
2 checks passed
@saariuslystoned
saariuslystoned deleted the feat/43-vreg-iq4xs-select-tree-lut branch December 1, 2026 18:26
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