Skip to content

hexagon: L2 cache handling rework (dirty bit tracking with lazy flushing) and more MUL_MAT updates - #25762

Merged
max-krasnyansky merged 49 commits into
ggml-org:masterfrom
qualcomm:hexagon-l2-dirty
Jul 16, 2026
Merged

max-krasnyansky merged 49 commits into
ggml-org:masterfrom
qualcomm:hexagon-l2-dirty

Conversation

@max-krasnyansky

Copy link
Copy Markdown
Member

Overview

This PR started as a follow up to the last MUL_MAT updates to add tiled activation processing for better HVX work distribution, and some fixes for DMA-overflow, and MUL_MAT_ID robustness for multi-chunk cases.
But as part of debugging/profiling I added tracing to the L2 cache flushes and realized that we really need optimize that in order to see better gains.

So it ended up being a full L2 cache flush rework. Per-op eager flush has been replaced with a dirty-bit tracking model.
dst tensors are marked dirty in a bitmap (dirty_map), and src tensors are flushed lazily only when dirty before an op reads them.
The flush method scales with size — sequential ranged flush (small), multi-threaded block-partitioned flush (medium),
or full dcache invalidate (>4 MiB).

In order to quickly recover from the full cache invalidate I re-wrote the layout of the core internal blocks (context, thread stacks, and the work/dma/hmx queues). All that stuff is now allocated as a single 4K-aligned memalign allocation (kind of like the original llama.cpp CPU implementation did in the past) so that we can quickly l2fetch it into the cache.
This particular change is kind of invasive anyway so I ended up doing a long overdue api cleanup for work_queue and things.

I could technically split MatMul changes into a separate PR but it'd be more testing work.
As always the updates have been tested on all supported devices with Hexagon NPUs v73 ... v81.

Additional information

I was primarily using gemma-4-E2/4B q4_0 models for testing and tunning.
Here are some perf results.


gemma-4-E2B_q4_0 (Google's QAT model) on Galaxy S26+
master:
   common_perf_print: prompt eval time =  373.34 ms / 741 tokens (  0.50 ms per token, 1984.77 tokens per second)
   common_perf_print:        eval time = 1910.68 ms /  63 runs   ( 30.33 ms per token,   32.97 tokens per second)

this-pr
   common_perf_print: prompt eval time =  358.48 ms / 741 tokens (  0.48 ms per token, 2067.08 tokens per second)
   common_perf_print:        eval time = 1901.58 ms /  63 runs   ( 30.18 ms per token,   33.13 tokens per second)

$ ./scripts/snapdragon/adb/run-tool.sh llama-bench --delay 10 --device HTP0 -m gemma-4-E2B_q4_0-it.gguf -t 6 --poll 1000 -ngl 99 -fa 1 --ubatch-size 1024 -p 512,800,1024,1500,2048,4096,8000,16000 -r 3

| model             |      size | dev   |     test |  master          t/s |  this-pr         t/s |
| ----------------- | --------: | ----- | -------: | -------------------: | -------------------: |
| gemma4 E2B Q4_0   |  3.10 GiB | HTP0  |    pp512 |      1996.82 ± 27.47 |      2047.85 ± 24.68 |
| gemma4 E2B Q4_0   |  3.10 GiB | HTP0  |    pp800 |      2017.06 ± 19.89 |      2090.38 ± 11.37 |
| gemma4 E2B Q4_0   |  3.10 GiB | HTP0  |   pp1024 |      2039.69 ± 25.78 |      2112.32 ± 14.69 |
| gemma4 E2B Q4_0   |  3.10 GiB | HTP0  |   pp1500 |      1972.21 ± 12.44 |       2039.08 ± 7.99 |
| gemma4 E2B Q4_0   |  3.10 GiB | HTP0  |   pp2048 |      1962.54 ± 32.58 |       2059.23 ± 3.13 |
| gemma4 E2B Q4_0   |  3.10 GiB | HTP0  |   pp4096 |      1779.47 ± 74.89 |       1983.64 ± 1.23 |
| gemma4 E2B Q4_0   |  3.10 GiB | HTP0  |   pp8000 |      1502.35 ± 61.65 |      1846.35 ± 22.37 |
| gemma4 E2B Q4_0   |  3.10 GiB | HTP0  |  pp16000 |      1340.79 ± 20.66 |      1545.37 ± 47.19 |
| gemma4 E2B Q4_0   |  3.10 GiB | HTP0  |    tg128 |         30.77 ± 0.69 |         32.42 ± 0.42 |

Other models see similar gains

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes, Antigravity for refactoring and trying out ideas

@max-krasnyansky
max-krasnyansky requested a review from a team as a code owner July 16, 2026 07:30
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning Hexagon labels Jul 16, 2026
@max-krasnyansky

Copy link
Copy Markdown
Member Author

@ggml-org/maintainers may I get the second approval please. The CI failures are not related.

@max-krasnyansky
max-krasnyansky merged commit b2dd28a into ggml-org:master Jul 16, 2026
18 of 32 checks passed
satindergrewal pushed a commit to satindergrewal/llama.cpp that referenced this pull request Aug 12, 2026
…ing) and more MUL_MAT updates (ggml-org#25762)

* hex-mm: fix artificial limit in the solver that restricted number of act-prep threads

* hex-mm: fix warning

* hex-prof: do not apply --top to the timeline report

* hmx-mm: add suport for tiled act-processing to better distribute hvx work

* hex-l2: add tracing for l2flush events

* workqueue: redo the legacy workpool api to match hmx-queue and dma-queue

* hmx-mm: fix f32 activation buffer alignmnet for nhvx=5,6,7

* hex-work: minor cleanup for work-queue apis

* hex-work: further cleanup of the work-queue api

* hex-l2: optimize l2flushes at the opbatch level

* hex-work: remove unused mask

* hex-work: no need to drop hvx ctx in the work-queue

* hex-work: add explicit wakeup/suspend and make threads spin

* hex-bufs: mark any non-weight tensor as compute

* hex-dma: dma-queue support for alias queues and cached dma

* hex-l2: track tensor aliases and delay or skip flushes as much as possible

* hex-l2: simplify tensor alias handling

* hex-l2: handle overlapping views as a circular list of aliases

* hex-tens: add flags helper

* hex-l2: add helper for marking tensors clearn/dirty

* hex-l2: mark binary and rope outputs as l2-clean and keep the rest as is for now

* hex-l2: proper support for handling all tensor overlap scenarios

* hex-trace: instrument matmul init code and cleanup trace checks

* hex-thread: introduce dedicated main thread with explicit stack and priority

* hex-l2: track dirty state as bitmap and introduce threaded flush

* hex-trace: remove redundant checks for ctx != null

* hex-l2: allocate entire context as one buffer and l2fetch it after big flushes

* hex-l2: disable tensor clearing in binary and rope for now seems to cause issues with fusion

* hmx-mm: update act proc to use fastdivs and fix DMA overflow

* hmx-mm: make MUL_MAT_ID kernels robust to multi-chunk cases (start_row>0)

* hex-queue: remove obsolete queue interfaces and flush hmx-queue at the end of the op-batch

* hex-queue: dont use early wakeup for small op-batches

* hex-tensors: properly cap max_tensors in op-batches and dirty_map

* hex-l2: make sure threaded l2flush does proper rounding

* hex-l2: factor out htp_tensor_flush for reuse (if needed)

* hex-l2: optimize tensor flushes by coalescing flush-all

* hex-l2: optimize multi-threaded flush

* hex-drv: futureproof version checks

* hexagon: fix errors and warnings on windows

* hex-main: update main thread to only use dspqueue_read, dspqueue_peek is not available on some platforms

* hex-main: add fallback mode for dspqueue with callbacks

* hex-main: introduce fallback mode for using dspqueue callbacks for full op processing

* hex-main: remove early wakeup, not helping and seems to cause some errors with certain batch sizes

* hex-l2: make sure to use invalidate version of flushall

* hex-l2: dont try to trace early l2flush at the start of op-batch

* hex-main: remove offset_ctx that must be zero anyway

* hex-hmx: fix hmx_queue_depth to use idx_write - idx_read

* hex-hmx: use atomic_load for idx_read/write

* hex-main: add static assert to make sure n_threads are aligned
zbrad pushed a commit to zbrad/llama.cpp that referenced this pull request Sep 10, 2026
…ing) and more MUL_MAT updates (ggml-org#25762)

* hex-mm: fix artificial limit in the solver that restricted number of act-prep threads

* hex-mm: fix warning

* hex-prof: do not apply --top to the timeline report

* hmx-mm: add suport for tiled act-processing to better distribute hvx work

* hex-l2: add tracing for l2flush events

* workqueue: redo the legacy workpool api to match hmx-queue and dma-queue

* hmx-mm: fix f32 activation buffer alignmnet for nhvx=5,6,7

* hex-work: minor cleanup for work-queue apis

* hex-work: further cleanup of the work-queue api

* hex-l2: optimize l2flushes at the opbatch level

* hex-work: remove unused mask

* hex-work: no need to drop hvx ctx in the work-queue

* hex-work: add explicit wakeup/suspend and make threads spin

* hex-bufs: mark any non-weight tensor as compute

* hex-dma: dma-queue support for alias queues and cached dma

* hex-l2: track tensor aliases and delay or skip flushes as much as possible

* hex-l2: simplify tensor alias handling

* hex-l2: handle overlapping views as a circular list of aliases

* hex-tens: add flags helper

* hex-l2: add helper for marking tensors clearn/dirty

* hex-l2: mark binary and rope outputs as l2-clean and keep the rest as is for now

* hex-l2: proper support for handling all tensor overlap scenarios

* hex-trace: instrument matmul init code and cleanup trace checks

* hex-thread: introduce dedicated main thread with explicit stack and priority

* hex-l2: track dirty state as bitmap and introduce threaded flush

* hex-trace: remove redundant checks for ctx != null

* hex-l2: allocate entire context as one buffer and l2fetch it after big flushes

* hex-l2: disable tensor clearing in binary and rope for now seems to cause issues with fusion

* hmx-mm: update act proc to use fastdivs and fix DMA overflow

* hmx-mm: make MUL_MAT_ID kernels robust to multi-chunk cases (start_row>0)

* hex-queue: remove obsolete queue interfaces and flush hmx-queue at the end of the op-batch

* hex-queue: dont use early wakeup for small op-batches

* hex-tensors: properly cap max_tensors in op-batches and dirty_map

* hex-l2: make sure threaded l2flush does proper rounding

* hex-l2: factor out htp_tensor_flush for reuse (if needed)

* hex-l2: optimize tensor flushes by coalescing flush-all

* hex-l2: optimize multi-threaded flush

* hex-drv: futureproof version checks

* hexagon: fix errors and warnings on windows

* hex-main: update main thread to only use dspqueue_read, dspqueue_peek is not available on some platforms

* hex-main: add fallback mode for dspqueue with callbacks

* hex-main: introduce fallback mode for using dspqueue callbacks for full op processing

* hex-main: remove early wakeup, not helping and seems to cause some errors with certain batch sizes

* hex-l2: make sure to use invalidate version of flushall

* hex-l2: dont try to trace early l2flush at the start of op-batch

* hex-main: remove offset_ctx that must be zero anyway

* hex-hmx: fix hmx_queue_depth to use idx_write - idx_read

* hex-hmx: use atomic_load for idx_read/write

* hex-main: add static assert to make sure n_threads are aligned
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning Hexagon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants