Repository navigation
Optimize orthogonal CQ4 LM head layout - #663
Merged
Merged
Conversation
ncylich
force-pushed
the
feature/cq4-interleaved-lmhead
branch
from
May 26, 2026 00:58
2cf11fb to
6f350e4
Compare
Add support for using the existing 4-row interleaved CQ4 layout with full-width orthogonal output heads. The LM-head path now dispatches to an optimized interleaved decode kernel while preserving the scalar fallback for supported interleaved shapes. Teach CQ weight export, QDQ, and runtime loading about explicit INTERLEAVED_4ROW storage for orthogonal CQ4 tensors. Embedding lookup can read tied interleaved weights, and the converter policy selects this layout for large output heads and tied token embeddings. Add a temporary artifact patching tool for rewriting existing full-width orthogonal CQ4 weight files to the interleaved layout without running a full reconversion. This keeps current artifacts testable while the converter pipeline is updated. Validation: pytest python/cactus/convert/tests/test_cq.py python/cactus/convert/tests/test_policy.py python/tests/test_transpile_weight_compat.py -q; cactus-graph/build/test_ops; cactus-graph/build/test_io; cactus-kernels/build/test_matmul; cactus build --android; local e2e generation checks for Gemma, LFM, and Qwen temp interleaved bundles; Pixel Gemma shared-KV generation and token benchmark with interleaved and row-major tied weights. Signed-off-by: Noah Cylich <noahcylich@gmail.com>
ncylich
force-pushed
the
feature/cq4-interleaved-lmhead
branch
2 times, most recently
from
May 26, 2026 20:11
7128893 to
44f532e
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.
Summary
Optimize full-width orthogonal CQ4 LM-head inference by routing large output heads through the existing optimized 4-row interleaved CQ4 layout.
Key changes:
INTERLEAVED_4ROWLM-head decode path.INTERLEAVED_4ROWstorage for orthogonal CQ4 tensors.Rationale
The previous LM-head CQ4 path was not using the same optimized 4-row interleaved layout as the general CQ4 matmul path. That made decode disproportionately slow for large output heads, especially on mobile CPU inference where the LM head is on the hot path for every generated token.
This PR makes the LM-head representation match the optimized general CQ4 representation, and keeps tied embedding/LM-head artifacts clean by supporting a single interleaved weight file when the model shares those weights.
Storage and Compatibility
The runtime now treats
INTERLEAVED_4ROWas an explicit storage layout for orthogonal CQ4 tensors. Existing non-interleaved CQ4 tensors remain supported through the existing paths.For current artifacts, the temporary patching helper can rewrite the affected orthogonal CQ4 files into the new interleaved layout. Longer term, the converter policy selects this layout directly for large output heads and tied token embeddings.
Validation
Correctness and build checks:
PYTHONPATH=/Users/noahcylich/Documents/Desert/cactus-v2/python pytest python/cactus/convert/tests/test_cq.py python/cactus/convert/tests/test_policy.py python/tests/test_transpile_weight_compat.py -qcactus-graph/build/test_opscactus-graph/build/test_iocactus-kernels/build/test_matmulcactus build --androidGeneration and artifact checks:
The generation checks produced coherent outputs. On the Pixel LM-head layout run, the interleaved tied-weight artifact was faster than the row-major tied-weight artifact in the same slow device/build path, instead of taking 80ms it's now around 20.
Currently, on pixel, this only yields:
2.67 tok/s decodevs2.15 tok/s decode9.57 tok/s prefillvs9.24 tok/s prefillAlthough the performance change is small, paired with
https://github.com/cactus-compute/cactus/pull/665, we get 3-4x performance all-around on pixel:10.65 tok/s decode,64.89 tok/s prefill.19.12 tok/s decode,157.76 tok/s prefill.