Bonsai masked gemv - #775
Open
jakmro wants to merge 14 commits into
Open
Conversation
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).
jakmro
force-pushed
the
bonsai-masked-gemv
branch
from
July 23, 2026 15:54
6c29e70 to
2d15661
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.