Skip to content

hadamard: share the activation transform between folded weights that share an input - #137

Merged
bri-prism merged 2 commits into
prism-v7from
hadamard-dedupe-transform
Aug 31, 2026
Merged

bri-prism merged 2 commits into
prism-v7from
hadamard-dedupe-transform

Conversation

@bri-prism

Copy link
Copy Markdown
Collaborator

Free speedup: folded weights that share an activation were each building their own copy of the same transform.

The redundancy

ggml does no common-subexpression elimination, so build_lora_mm inserting a sign-multiply and an FWHT per folded weight means the work runs once per weight rather than once per activation. Three groups share an input on this architecture:

group site redundant transforms
ffn_gate + ffn_up build_ffn, LLM_FFN_PAR, both take cur 64
attn_qkv + attn_gate qwen35.cpp:237,241, both take input 48
attn_q + attn_k + attn_v qwen35.cpp:270,282,285, all take cur 32

144 of the 401 folded weights were recomputing a transform that already existed, so 36% of the FWHT calls per token were duplicates.

The change

build_lora_mm and build_lora_mm_id memoize on (input, rotation). Both have to match for a transform to be shared: same input tensor, same rotation, which also implies the same width and therefore the same sign vector. The map lives for one graph build. 2 files, +23 lines.

Measured

M5, Metal, a folded low-bit checkpoint. Three interleaved pairs on a warm machine, since the first runs drift upward with warmup:

baseline this branch delta
pp512 358.29 370.74 +3.5%
tg128 23.20 23.91 +3.1%

Spread was at most 1.1 on pp512 and 0.11 on tg128, so the gap sits well outside the noise. Greedy output is unchanged byte for byte on the same prompt.

The magnitude matches the prediction, which is the reassuring part: the transform stack was about 10% of traced GPU time and removing 36% of it should be worth roughly 3.5%.

Notes

Metal only so far. The change is backend-agnostic, it removes graph nodes rather than touching any kernel, so CUDA should see the same or more given it has more launch overhead to save at batch-1. Worth one CUDA run to confirm.

Architectures whose folded weights do not share inputs simply never hit the memo and are unaffected.

…share an input

Folded weights that consume the same activation were each building their own
identical transform. ggml does no common-subexpression elimination, so the
sign-multiply and the FWHT ran once per weight rather than once per activation.

Three groups share an input on this architecture:

  ffn_gate + ffn_up      build_ffn LLM_FFN_PAR, both take cur     64 redundant
  attn_qkv + attn_gate   qwen35.cpp:237,241, both take input      48 redundant
  attn_q + attn_k + attn_v  qwen35.cpp:270,282,285, all take cur  32 redundant

That is 144 of 401 folded weights doing a transform that already exists, so 36%
of the FWHT calls per token were duplicates.

build_lora_mm and build_lora_mm_id now memoize on (input, rotation). Both must
match for the transform to be shared: same input tensor and same rotation, which
also implies the same width and therefore the same sign vector. The map lives for
one graph build.

Measured on M5, Metal, a folded low-bit checkpoint, three interleaved pairs
on a warm machine:

  pp512   358.29 -> 370.74   +3.5%
  tg128    23.20 ->  23.91   +3.1%

Run-to-run spread was at most 1.1 on pp512 and 0.11 on tg128, so the gap is well
outside the noise. Greedy output is unchanged, byte for byte, on the same prompt.

The size of the win matches the prediction: the transform stack was about 10% of
traced GPU time, and removing 36% of it should be worth about 3.5%.

@khosravipasha khosravipasha left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nice, LGTM, asekd for coplit too but shoudl be good

we dont have lora why did it affect speed?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Caches Hadamard activation transforms shared by folded weights, eliminating redundant graph nodes.

Changes:

  • Adds a per-graph transform memo keyed by input and rotation.
  • Reuses transforms in standard and expert matmul paths.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/llama-graph.h Defines the transform cache.
src/llama-graph.cpp Memoizes transforms in both matmul helpers.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/llama-graph.h Outdated
Comment on lines +1081 to +1085
// Folded weights that share an activation also share its transform: gate and up
// take the same post-norm tensor, as do qkv and its gate, and q/k/v. ggml does no
// common-subexpression elimination, so without this each one builds its own
// identical sign-multiply and FWHT. Keyed on the input and the rotation, since a
// shared transform needs both to match. Lives for one graph build.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in dc677ef, cut from five lines to two. Kept just the key and the lifetime, dropped the architecture-specific examples since those go stale as you say. The per-layer breakdown lives in the PR description instead.

@bri-prism

Copy link
Copy Markdown
Collaborator Author

Thanks! And fair confusion, the name is misleading.

build_lora_mm is not a LoRA-only path, it is the general matmul helper every weight goes through. LoRA is just an optional extra it applies when adapters are loaded; with none loaded it falls straight through to ggml_mul_mat. The Hadamard transform hooks in there because that is the single point all folded matmuls pass through, so the dedupe applies whether or not any LoRA exists.

What the change removes is graph nodes, not LoRA work. 144 of the 401 folded weights were building a second copy of a sign-multiply and FWHT that another weight on the same activation had already built. Nothing to do with adapters.

@bri-prism
bri-prism merged commit 555c5cd into prism-v7 Aug 31, 2026
3 checks passed
@bri-prism
bri-prism deleted the hadamard-dedupe-transform branch August 31, 2026 22:24
khosravipasha pushed a commit that referenced this pull request Sep 4, 2026
…share an input (#137)

* hadamard: share the activation transform between folded weights that share an input

Folded weights that consume the same activation were each building their own
identical transform. ggml does no common-subexpression elimination, so the
sign-multiply and the FWHT ran once per weight rather than once per activation.

Three groups share an input on this architecture:

  ffn_gate + ffn_up      build_ffn LLM_FFN_PAR, both take cur     64 redundant
  attn_qkv + attn_gate   qwen35.cpp:237,241, both take input      48 redundant
  attn_q + attn_k + attn_v  qwen35.cpp:270,282,285, all take cur  32 redundant

That is 144 of 401 folded weights doing a transform that already exists, so 36%
of the FWHT calls per token were duplicates.

build_lora_mm and build_lora_mm_id now memoize on (input, rotation). Both must
match for the transform to be shared: same input tensor and same rotation, which
also implies the same width and therefore the same sign vector. The map lives for
one graph build.

Measured on M5, Metal, a folded low-bit checkpoint, three interleaved pairs
on a warm machine:

  pp512   358.29 -> 370.74   +3.5%
  tg128    23.20 ->  23.91   +3.1%

Run-to-run spread was at most 1.1 on pp512 and 0.11 on tg128, so the gap is well
outside the noise. Greedy output is unchanged, byte for byte, on the same prompt.

The size of the win matches the prediction: the transform stack was about 10% of
traced GPU time, and removing 36% of it should be worth about 3.5%.

* hadamard: trim the memo comment to two lines per AGENTS.md
khosravipasha pushed a commit that referenced this pull request Sep 4, 2026
…share an input (#137)

* hadamard: share the activation transform between folded weights that share an input

Folded weights that consume the same activation were each building their own
identical transform. ggml does no common-subexpression elimination, so the
sign-multiply and the FWHT ran once per weight rather than once per activation.

Three groups share an input on this architecture:

  ffn_gate + ffn_up      build_ffn LLM_FFN_PAR, both take cur     64 redundant
  attn_qkv + attn_gate   qwen35.cpp:237,241, both take input      48 redundant
  attn_q + attn_k + attn_v  qwen35.cpp:270,282,285, all take cur  32 redundant

That is 144 of 401 folded weights doing a transform that already exists, so 36%
of the FWHT calls per token were duplicates.

build_lora_mm and build_lora_mm_id now memoize on (input, rotation). Both must
match for the transform to be shared: same input tensor and same rotation, which
also implies the same width and therefore the same sign vector. The map lives for
one graph build.

Measured on M5, Metal, a folded low-bit checkpoint, three interleaved pairs
on a warm machine:

  pp512   358.29 -> 370.74   +3.5%
  tg128    23.20 ->  23.91   +3.1%

Run-to-run spread was at most 1.1 on pp512 and 0.11 on tg128, so the gap is well
outside the noise. Greedy output is unchanged, byte for byte, on the same prompt.

The size of the win matches the prediction: the transform stack was about 10% of
traced GPU time, and removing 36% of it should be worth about 3.5%.

* hadamard: trim the memo comment to two lines per AGENTS.md
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