Repository navigation
hadamard: share the activation transform between folded weights that share an input - #137
Conversation
…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
left a comment
There was a problem hiding this comment.
nice, LGTM, asekd for coplit too but shoudl be good
we dont have lora why did it affect speed?
There was a problem hiding this comment.
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.
| // 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. |
There was a problem hiding this comment.
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.
|
Thanks! And fair confusion, the name is misleading.
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. |
…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
…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
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_mminserting 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:ffn_gate+ffn_upbuild_ffn,LLM_FFN_PAR, both takecurattn_qkv+attn_gateqwen35.cpp:237,241, both takeinputattn_q+attn_k+attn_vqwen35.cpp:270,282,285, all takecur144 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_mmandbuild_lora_mm_idmemoize 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:
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.