Skip to content

Bonsai masked gemv - #775

Open
jakmro wants to merge 14 commits into
mainfrom
bonsai-masked-gemv
Open

Bonsai masked gemv#775
jakmro wants to merge 14 commits into
mainfrom
bonsai-masked-gemv

Conversation

@jakmro

@jakmro jakmro commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

rshemet and others added 13 commits July 16, 2026 17:03
All qwen3_5 adapters called the HF rotary module with meta-device
position ids against the cpu-materialized inv_freq buffer, crashing
torch.export with a mixed-device bmm. Route the rotary through the
inv_freq device and rejoin the activation device via a shared
_qwen35_rotary_embeddings helper (7 call sites).
Tensors whose 128-groups hold exactly {-s,+s} (e.g. PrismML Bonsai) are
repacked at --bits 1 into sign-bit CQ1 with the checkpoint's own scales
as group norms and an explicit {-1,+1} codebook — no Hadamard rotation,
no GPTQ, zero quantization error. Non-binary tensors fall through to
the existing rotated path. New file flag FLAG_NO_ROTATION=1<<5 (bits
0-4 are taken by HAS_SCALES/ORTHOGONAL/INTERLEAVED_4ROW/INT8-INTERLEAVED/
EXTENDED_SHAPE). Repack tensors use the linear LSB layout: the
interleaved cb_factor=1/127 rescale would break fp16 norm exactness.
Round-trip bit-exactness covered in test_cq.py.
File flag 1<<5 remaps to kernel flag CACTUS_QUANT_FLAG_NO_ROTATION
(1u<<4) in all three mmap loaders, with null sign/perm tails. The
shared activation transform and the hadamard embedding-row dequant
(now taking flags) skip the FWHT under the flag, so every bit width
and both packed layouts inherit the bypass. New tests: kernel
matmul_cq1_norot (MSE<1e-3 vs fp32 reference) and graph-level
no-rotation mmap matmul.
Truncated 4-layer Bonsai fixture (real QAT weights), transformers-vs-
engine greedy parity runner, bit-exactness gate (gate_b), and worklog.
Gates A-D recorded: repack round-trips bit-exactly on all checkpoint
tensors incl. 27B embed/lm_head samples; engine output deterministic.
At --bits 1, QAT-binary embeddings and output head repack to rotation-
free CQ1 instead of the forced orthogonal CQ4 (policy override happens
at emission with values in hand; manifest rows reflect the actual CQ1).
Repacked tensors skip the interleaved_4row layout override. Truncated
bundle: 1.59 GB -> 0.57 GB, 33/33 tensors bit-exact, engine embedding
lookup + lm_head verified via the no-rotation dequant path.
Repack CQ1 3.88 GB: GSM8K-20 90%, factual 4/4 (CQ4 14 GB: 85%).
Android compile green; on-device deferred pending hardware.
Two engine-logic bugs surfaced by running the `llm` suite against a
recurrent (deltanet) model:

- supports_dynamic_batch() reported true whenever the decoder had dynamic
  shapes, but generate_batch() bails to {} for any CONV/RECURRENT cache
  state. The predicate now mirrors that condition, so recurrent/conv models
  correctly report no dynamic batch (callers skip instead of getting 0
  streams). General to any recurrent/conv model, not Bonsai-specific.

- The recurrent-tail prefill path reported tail_padding relative to the
  in-chunk real tokens (effective_chunk - chunk_real), one higher than the
  padded-tail path's convention (effective_chunk - tail_tokens). Telemetry
  only; aligned to the padded-tail semantics. Inference was already correct
  (the test's divergence check passes).
Copilot AI review requested due to automatic review settings July 23, 2026 15:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@jakmro
jakmro force-pushed the bonsai-masked-gemv branch from 6c29e70 to 2d15661 Compare July 23, 2026 15:54
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.

3 participants