Skip to content

llama: load Hadamard-folded weights (prism.hadamard, Ternary Bonsai) - #34

Merged
bong-water-water-bong merged 4 commits into
1bit/hrx-vulkan-patchedfrom
1bit/hrx-bonsai
Sep 30, 2026
Merged

bong-water-water-bong merged 4 commits into
1bit/hrx-vulkan-patchedfrom
1bit/hrx-bonsai

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

PrismML's Ternary-Bonsai models (Qwen3.8-27B trained ternary) store most matmul weights rotated by a blockwise Walsh-Hadamard transform, described by prism.hadamard.* GGUF keys. This build used to load such a file and compute garbage. Now:

  • the loader reads the keys (same keys, same checks as PrismML-Eng/llama.cpp, MIT) and builds the rotation matrix and sign vectors next to the weights;
  • build_lora_mm / build_lora_mm_id rotate each activation before a folded weight (once per activation per graph); the token_embd lookup gets the inverse;
  • the first graph is checked: a folded weight used without its rotation, or the rotated table read without the inverse, is an error instead of wrong output.

The rotation is a plain F32 MUL_MAT, so it runs on HRX0 with the existing kernels. The code is in src/llama-hadamard.{h,cpp} (ours); upstream files get hook lines only (29 lines over 8 files).

The weights themselves go through the engine's tools/ternary_to_q4_0.py: PrismML's PTQ1_0 written as Q4_0, bit-exact.

Checked on HRX0 (strixhalo, gfx1151):

  • Ternary-Bonsai-2-27B, wikitext-2 running PPL 7.962 / 10.408 / 10.171 at chunks 5 / 10 / 15, against 7.958 / 10.382 / 10.142 for the F16 model on CPU in PrismML's own fork.
  • Files without the keys build the same graph as before: Qwen3.8-27B Q4_0 PPL 5.8893 (4 chunks) with and without this change.

🤖 Generated with Claude Code

@github-actions github-actions Bot added the model label Sep 29, 2026
bong-water-water-bong and others added 3 commits September 29, 2026 23:30
PrismML's Ternary-Bonsai models store most matmul weights in a blockwise
Walsh-Hadamard-rotated basis and describe it with prism.hadamard.* GGUF keys.
Without them this build loads such a file and computes garbage. Now the
loader reads the keys, builds the rotation (and sign) tensors next to the
weights, and build_lora_mm / build_lora_mm_id rotate each activation before a
folded weight (memoized per graph); token_embd lookups get the inverse. The
rotation is a plain F32 MUL_MAT, so it runs on HRX0 like any other matmul.
The first graph is checked: a folded weight consumed without its transform
or a rotated table read without the inverse is an error, not wrong output.

Follows PrismML-Eng/llama.cpp's implementation (MIT): same keys, checks and
transform. The code is in src/llama-hadamard.{h,cpp}; upstream files get
hook lines only.

Checked on HRX0: Ternary-Bonsai-2-27B (PTQ1_0 converted to Q4_0 bit-exactly
with the engine's tools/ternary_to_q4_0.py) gives wikitext-2 running PPL
7.962 / 10.408 / 10.171 at chunks 5 / 10 / 15 against 7.958 / 10.382 /
10.142 for the F16 model on CPU (PrismML's fork). Files without the keys
build the same graph as before (Qwen3.8-27B Q4_0 PPL 5.8893 both ways).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
llama_model_loader instantiates its string-keyed get_key / get_arr for a few
types only; the bool and array ones did not link with GCC (CI ubuntu jobs).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ollowed

The prism.hadamard loader follows PrismML-Eng/llama.cpp (MIT). Keep its
copyright line and permission notice with the files, as the license asks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bong-water-water-bong
bong-water-water-bong merged commit b8d587e into 1bit/hrx-vulkan-patched Sep 30, 2026
10 of 24 checks passed
bong-water-water-bong added a commit that referenced this pull request Oct 25, 2026
… in test-backend-ops (#140)

FillParams hook so two FILLs differing only in the constant cannot share a cached program; TOP_K test cases at 248320 columns, k 7/16, 1/2/8 rows, with and without ties. Pairs with the private add-on's DFlash2 selector kernels (gpu-kernels #34).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/hrx-bonsai branch November 12, 2026 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant