Skip to content

Optimize orthogonal CQ4 LM head layout - #663

Merged
HenryNdubuaku merged 1 commit into
v2from
feature/cq4-interleaved-lmhead
May 26, 2026
Merged

HenryNdubuaku merged 1 commit into
v2from
feature/cq4-interleaved-lmhead

Conversation

@ncylich

@ncylich ncylich commented May 26, 2026 •

Copy link
Copy Markdown
Collaborator

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:

  • Add an optimized orthogonal CQ4 INTERLEAVED_4ROW LM-head decode path.
  • Preserve the existing scalar/fallback path for unsupported interleaved shapes.
  • Teach CQ weight export and QDQ loading to explicitly preserve INTERLEAVED_4ROW storage for orthogonal CQ4 tensors.
  • Allow tied token embedding lookup to read the same interleaved orthogonal CQ4 weight file used by the LM head.
  • Update converter policy to prefer interleaved orthogonal CQ4 storage for large output heads and tied token embeddings.
  • Add a temporary artifact patching helper so existing full-width orthogonal CQ4 bundles can be rewritten to the interleaved layout without a full reconversion.
  • Remove the old separate LM-head-only CQ4 path in favor of the general optimized CQ4 interleaved format.

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_4ROW as 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 -q
  • cactus-graph/build/test_ops
  • cactus-graph/build/test_io
  • cactus-kernels/build/test_matmul
  • cactus build --android

Generation and artifact checks:

  • Local e2e generation sanity checks for temporary interleaved Gemma, LFM, and Qwen bundles.
  • Pixel Gemma shared-KV short generation using the patched interleaved tied embedding/LM-head file.
  • Pixel Gemma shared-KV token benchmark comparing interleaved and row-major tied weights with the same rebuilt branch binary.

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 decode vs 2.15 tok/s decode
  • 9.57 tok/s prefill vs 9.24 tok/s prefill

Although the performance change is small, paired with https://github.com/cactus-compute/cactus/pull/665, we get 3-4x performance all-around on pixel:

  • Pixel: 10.65 tok/s decode, 64.89 tok/s prefill.
  • Samsung: 19.12 tok/s decode, 157.76 tok/s prefill.

@ncylich
ncylich force-pushed the feature/cq4-interleaved-lmhead branch from 2cf11fb to 6f350e4 Compare May 26, 2026 00:58
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
ncylich force-pushed the feature/cq4-interleaved-lmhead branch 2 times, most recently from 7128893 to 44f532e Compare May 26, 2026 20:11
@HenryNdubuaku
HenryNdubuaku merged commit cad2ebc into v2 May 26, 2026
9 checks passed
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.

2 participants