Skip to content

hexagon: tensor repacking rework and support for tiled and flat quantized and fp weights - #30635

Merged
max-krasnyansky merged 35 commits into
ggml-org:masterfrom
qualcomm:hexagon-repack-rework
Oct 16, 2026
Merged

max-krasnyansky merged 35 commits into
ggml-org:masterfrom
qualcomm:hexagon-repack-rework

Conversation

@max-krasnyansky

@max-krasnyansky max-krasnyansky commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Overview

This is a long overdue cleanup/rework of ggml-hexagon tensor / weight repacking functionality.
It's another sizable update but it's hard to rework stuff like this via minor increments.

The PR cleans up the terminology and naming across the entire backend:

  • repack is now consistently used for the overall feature and configuration
  • flat is now consistently used for ops/ukernels/data that operate on flat/row-major GGML formats (f16, q4_0, ...)
  • tiled is now consistently used for ops/ukernels/data that operate on 32x32 tiled hexagon formats

We now consistently enforce a very simple and clear rule:

  • All 2D weights of supported data types & shapes are repacked into tilled format at load time

This rule applies to f16/f32/bf16 2d weights which are always repacked into f16 tiles.
Our MUL_MAT kernels were already doing this at run-time and this feature allows us to now skip the run-time tiling.
Note: That bf16 support is new and introduced in this PR.

The PR updates the ukernel collection to provide both flat and tiled favors for all key paths.

  • GET_ROWS, MAT_MUL and MUL_MAT_ID now have both flat and tiled fp ukernels, and tiled quantized ukernels
  • GET_ROWS support flat Q and K-quants

Other cleanups, refactoring, and improvements:

  • All repacking logic has been factored out into separate header and .cpp files
  • htp-tensor api has been cleaned (all tiling params, padding, etc)
  • Enabled fusion for tiled MM kernels
  • More DMA conversions in related paths (get-rows, mm-id, ...)
  • Developer guide updates based on the changes here

There is new env var GGML_HEXAGON_RT_REPACK with 3-states (0: disabled, 1: quantized only [default], 2: always, including float weights). This controls runtime (aka lazy) repack and is intended only for testing where most of the tensor buffers are not marked as usage=weights. The default is intended to rt-repack quantized weights for MUL_MAT and GET_ROWS tests to mimic the actual use-cases.

Passing all test-backend-ops -b HTP0 -o MUL_MAT.* tests (including all fusion tests) on single and dual-NPU setups.

Performance

Performance-wise it's a significant boost for the models that use 2D F16/F32/BF16 weights.
As I mentioned above we're now able to skip the runtime tiling overhead for these weights.
Here is an example from profiling runs of gemma-4-E2B_q4_0. It's a Q4_0 QAT model but it still has F16 weights.

Before
0.05.187.244 D ggml-hex: HTP0 profile-op MUL_MAT|per_layer_model_proj.weight x inp_scaled -> node_48|1536:8960 x 1536:741 -> 8960:741|f16 x f32 -> f32|2:3072 x 4:6144 -> 4:35840|hmx-tiled vtcm 8259584|usec 1439 cycles 3040455 start 1930954774 mhz 2112.9 pmu [4385794,1008414,9419735,1550042,1526029,125304,76,32]

After
0.05.675.050 D ggml-hex: HTP0 profile-op MUL_MAT|per_layer_model_proj.weight x inp_scaled -> node_48|1536:8960 x 1536:741 -> 8960:741|f16 x f32 -> f32|2:3072 x 4:6144 -> 4:35840|hmx-tiled vtcm 8259584|usec 1048 cycles 2214355 start 2392835449 mhz 2112.9 pmu [1940425,271144,5877066,674802,1544533,125304,76,33]

** 1439 usec -> 1048 usec **

Models that are heavy on F16/F32/BF16 weights should see major gains.
Please let me know if you see any regressions.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes, Anti.G with 3.8-Flash and a bit with 4-Argon, for deep dives, code review, refactoring.

…s GET_ROWS tests for K-Quants, will fix in the next round

Add support for GET_ROWS with tiled Q5_K
Add support for raw k-quant tensors
@max-krasnyansky
max-krasnyansky requested review from a team and ggerganov as code owners October 15, 2026 23:58
@max-krasnyansky

Copy link
Copy Markdown
Member Author

@lhez for review and ack (sorry for another huge PR but the updates are very targeted tiling/flat renames, etc).

@jhen0409

Copy link
Copy Markdown
Member

It looks like htp-dcache.h is missing. I added it to local and then tested it on IQ9, no regression found.

@max-krasnyansky

Copy link
Copy Markdown
Member Author

It looks like htp-dcache.h is missing. I added it to local and then tested it on IQ9, no regression found.

Fixed. Thanks!

@github-actions github-actions Bot added documentation Improvements or additions to documentation testing Everything test related ggml changes relating to the ggml tensor library for machine learning Hexagon labels Oct 16, 2026
@max-krasnyansky

Copy link
Copy Markdown
Member Author

Ok. Should be good to go. I double checked and the CI failures in other backends are unrelated (ie since I touched the test a bit).

@max-krasnyansky max-krasnyansky added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Oct 16, 2026
@max-krasnyansky
max-krasnyansky merged commit b797b9c into ggml-org:master Oct 16, 2026
34 of 40 checks passed
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 24, 2026
…ized and fp weights (ggml-org#30635)

* hex-repack: Remove need_repack hack and streamline the rules -- breaks GET_ROWS tests for K-Quants, will fix in the next round

Add support for GET_ROWS with tiled Q5_K
Add support for raw k-quant tensors

* hex-get-rows: vectorize raw k-quant ukernels

* hex-binary: refactor dma-loop thread functions (no need for macros, etc)

* hex-quant: move/cleanup flat and tiled dequant ukernels

* hex-repack: add support for repacking/tiling F16/F32/BF16 2D weights

* hex-get-rows: remove redundant cast

* hex-repack: proper filtering for BF16 that were leaking through during the tests

* hex-repack: streamling tiling rules (broken MM for now)

* hex-repack: fix mmid after tiling updates

* hex-repack: rename repacked to tiled in all flags/funcs/kernels/vars

* hex-mm: fix valid_rows calc in various kernels

* hex-mm: remove unused hvx_mm_nx_2d_flat

* hex-mm: cleanup and optimize hvx-tiled vec_dot_f16

* hex-mm: another pass at hvx-tiled f16 matmul

* hex-get-rows: optimize small src1

* hex-get-rows: one more pass at recovering get-rows perf regression

* hex-repack: export GGML_HEXAGON_RT_REPACK via run.py

* hex-mm-tiled: fix accuracy regression with hmx-tiled-f16 matmul

* hex-mm: fold tiled_f16 checks into the general flow

* hex-get-rows: remove dead checks for buffer_is_hexagon and tiling during supports_op

* hex-quant: proper tail handling in new dequant ukernels

* hex-get-rows: remove unused code and cleanup support checks

* hex-mm: cleanup ne1 padding/tiling checks

* hex-mm: cleanup fusion checks for tiled tensors

* hex-repack: add a little can_repack helper to reduce duplication

* hex-repack: never repack host buffers

* hex-repack: factor out repack functions into separate .h and .cpp

* hex-tensor: cleanup tensor ifaces for tiled/padded sizes

* hex-repack: update k-quant tiling comments and cleanup #defines

* hex-dcache: factor out l2/dcache dirty tracking and flushing

* hex-tensor: update docs and remove #defs duplication

* hex-tensor: remove unused/incorrect get_data interface

* hex-get-rows: cleanup vtcm layout build

* tests: disable huge MM tests

* hex-dcache: add missing header
auRose94 pushed a commit to auRose94/llama.cpp that referenced this pull request Oct 28, 2026
…ized and fp weights (ggml-org#30635)

* hex-repack: Remove need_repack hack and streamline the rules -- breaks GET_ROWS tests for K-Quants, will fix in the next round

Add support for GET_ROWS with tiled Q5_K
Add support for raw k-quant tensors

* hex-get-rows: vectorize raw k-quant ukernels

* hex-binary: refactor dma-loop thread functions (no need for macros, etc)

* hex-quant: move/cleanup flat and tiled dequant ukernels

* hex-repack: add support for repacking/tiling F16/F32/BF16 2D weights

* hex-get-rows: remove redundant cast

* hex-repack: proper filtering for BF16 that were leaking through during the tests

* hex-repack: streamling tiling rules (broken MM for now)

* hex-repack: fix mmid after tiling updates

* hex-repack: rename repacked to tiled in all flags/funcs/kernels/vars

* hex-mm: fix valid_rows calc in various kernels

* hex-mm: remove unused hvx_mm_nx_2d_flat

* hex-mm: cleanup and optimize hvx-tiled vec_dot_f16

* hex-mm: another pass at hvx-tiled f16 matmul

* hex-get-rows: optimize small src1

* hex-get-rows: one more pass at recovering get-rows perf regression

* hex-repack: export GGML_HEXAGON_RT_REPACK via run.py

* hex-mm-tiled: fix accuracy regression with hmx-tiled-f16 matmul

* hex-mm: fold tiled_f16 checks into the general flow

* hex-get-rows: remove dead checks for buffer_is_hexagon and tiling during supports_op

* hex-quant: proper tail handling in new dequant ukernels

* hex-get-rows: remove unused code and cleanup support checks

* hex-mm: cleanup ne1 padding/tiling checks

* hex-mm: cleanup fusion checks for tiled tensors

* hex-repack: add a little can_repack helper to reduce duplication

* hex-repack: never repack host buffers

* hex-repack: factor out repack functions into separate .h and .cpp

* hex-tensor: cleanup tensor ifaces for tiled/padded sizes

* hex-repack: update k-quant tiling comments and cleanup #defines

* hex-dcache: factor out l2/dcache dirty tracking and flushing

* hex-tensor: update docs and remove #defs duplication

* hex-tensor: remove unused/incorrect get_data interface

* hex-get-rows: cleanup vtcm layout build

* tests: disable huge MM tests

* hex-dcache: add missing header
@BrewTestBot BrewTestBot mentioned this pull request Oct 28, 2026
1 task done
yuubinnkyoku added a commit to yuubinnkyoku/llama.cpp that referenced this pull request Nov 25, 2026
Measured on host, no device needed.

ceefd16 (ggml-org#30581 HMX FA sinks) and 27c7a76 (ggml-org#30435 softplus HVX) apply with
zero conflicts. Not committed here, because the branch should not advance past
what the device has validated; they are the first two for Phase 65.

797de63 (ggml-org#30122 Q1_0/Q2_0) is the next wall at 39 hunks across four files,
28 of them in matmul-ops.c where the fork's four-column dispatch and upstream's
generic repack dispatch claim the same table. It needs the repack layer that
b797b9c (ggml-org#30635) introduces, and that file pair does not exist in the fork.

So the shape is: repack rework is the gate, and Q1_0/Q2_0 and Op Fusion both sit
behind it. Phase 65's real work is one PR with ten files and 39+ hunks.

Assisted-by: DeepSeek Harness
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation ggml changes relating to the ggml tensor library for machine learning Hexagon merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants