Skip to content

Speed-up ggml_vec_dot_q4_1() ARM_NEON - #900

Merged
ggerganov merged 6 commits into
masterfrom
opt-q4_1
Apr 13, 2023
Merged

ggerganov merged 6 commits into
masterfrom
opt-q4_1

Conversation

@ggerganov

@ggerganov ggerganov commented Apr 11, 2023 •

Copy link
Copy Markdown
Member

Time per token for Q4_1 drops by ~5%

Also, make it build on ARMv7 Raspberry Pi 4

🤖 Generated by Copilot at 63f7ecf

Summary

🚀🔧🔄

This pull request enhances the ggml.c file to run faster and more reliably on ARM devices by using inline functions, simpler macros, and NEON optimizations.

We're sailing on the ARM, me hearties, on the ARM
We're coding for the NEON, me hearties, for the NEON
We'll inline and simplify, and loop by two and dot
And we'll make our ggml.c run faster than a shot

Walkthrough

  • Add inline functions to emulate missing NEON intrinsics and fix a wrong feature check (link)
  • Simplify the macros and scalar computations for reducing vectors by removing conditional compilation and using the emulated intrinsics (link, link, link, link, link)
  • Fix a typo in a comment (link)
  • Remove empty lines for better readability and style (link, link)
  • Modify the loop iteration and body in ggml.c to process two blocks of x and y at a time using more NEON registers and operations (link, link)

@unbounded

unbounded commented Apr 11, 2023 •

Copy link
Copy Markdown
Contributor

What is the reason this is gated on __ARM_FEATURE_QRDMX?

https://developer.arm.com/documentation/101028/0012/13--Advanced-SIMD--Neon--intrinsics says this define indicates the availability of SQRDMLAH and SQRDMLSH.

https://arm-software.github.io/acle/neon_intrinsics/advsimd.html doesn't list any requirements for the vaddv instructions other than A64, my impression is that they should be available if __aarch64__ is defined.

@ggerganov

Copy link
Copy Markdown
Member Author

@unbounded
It was a mistake - thanks for the suggestion

@ggerganov
ggerganov merged commit d990e3f into master Apr 13, 2023
@ggerganov
ggerganov deleted the opt-q4_1 branch April 13, 2023 15:32
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
…ml-org#900)

* ggml : speed-up q4_1 ARM_NEON by ~5%

* ggml : implement vaddvq when missing

* ggml : implement vminvq and vmaxvq when missing

* ggml : implement vzip when missing

* ggml : fix comment

* ggml : try to use correct ifdef
phuongncn pushed a commit to phuongncn/llama.cpp-gx10-dgx-sparks-deepseekv4 that referenced this pull request Apr 28, 2026
…ml-org#900)

* ggml : speed-up q4_1 ARM_NEON by ~5%

* ggml : implement vaddvq when missing

* ggml : implement vminvq and vmaxvq when missing

* ggml : implement vzip when missing

* ggml : fix comment

* ggml : try to use correct ifdef
phuongncn pushed a commit to phuongncn/llama.cpp-gx10-dgx-sparks-deepseekv4 that referenced this pull request Apr 28, 2026
ljubomirj pushed a commit to ljubomirj/llama.cpp that referenced this pull request May 6, 2026
…ml-org#900)

* ggml : speed-up q4_1 ARM_NEON by ~5%

* ggml : implement vaddvq when missing

* ggml : implement vminvq and vmaxvq when missing

* ggml : implement vzip when missing

* ggml : fix comment

* ggml : try to use correct ifdef
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
…ml-org#900)

* ggml : speed-up q4_1 ARM_NEON by ~5%

* ggml : implement vaddvq when missing

* ggml : implement vminvq and vmaxvq when missing

* ggml : implement vzip when missing

* ggml : fix comment

* ggml : try to use correct ifdef
bong-water-water-bong pushed a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 4, 2026
…ort (engine ggml-org#900)

arena_reserve() fails on the free list, not on the size: the issue measured 193.2 MiB free in 5
blocks against a 121.7 MiB slot, and the refusal told the operator the model's working set does not
fit - then advised "Raise it to at least 4030 MiB" while the budget in force was 4096 MiB.

The free list can tell the two cases apart (free >= need with no block that large means fragmented),
so the refusal now reports which one happened, with the largest block, and drops the size advice in
the fragmented case - where the number it printed was not a fixed point, because resident tracks the
budget. The genuine capacity case keeps the engine ggml-org#788 wording and its raise-to number.

The miss is recorded whole in the latch, so a later acquire in the same run repeats the first
refusal's reason instead of re-deriving the wrong one. Diagnostic only: no allocation, eviction or
placement behaviour changes.

tests/test-hrx-arena-refusal.cpp: 5 arms - both of the issue's measured states, a genuine capacity
miss, the unbounded default, and the corner where a large enough free block exists (not called
fragmentation).
bong-water-water-bong added a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 4, 2026
…ort (engine ggml-org#900) (#180)

arena_reserve() fails on the free list, not on the size: the issue measured 193.2 MiB free in 5
blocks against a 121.7 MiB slot, and the refusal told the operator the model's working set does not
fit - then advised "Raise it to at least 4030 MiB" while the budget in force was 4096 MiB.

The free list can tell the two cases apart (free >= need with no block that large means fragmented),
so the refusal now reports which one happened, with the largest block, and drops the size advice in
the fragmented case - where the number it printed was not a fixed point, because resident tracks the
budget. The genuine capacity case keeps the engine ggml-org#788 wording and its raise-to number.

The miss is recorded whole in the latch, so a later acquire in the same run repeats the first
refusal's reason instead of re-deriving the wrong one. Diagnostic only: no allocation, eviction or
placement behaviour changes.

tests/test-hrx-arena-refusal.cpp: 5 arms - both of the issue's measured states, a genuine capacity
miss, the unbounded default, and the corner where a large enough free block exists (not called
fragmentation).

Co-authored-by: bong-water-water-bong <[email protected]>
bong-water-water-bong added a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 4, 2026
…m (engine ggml-org#900) (#182)

A slot parked while an execution is in flight (arena_deferred_) survives the arena being
re-created, so end_execution() reclaims it into the NEW free list at its OLD offset. Those
offsets interleave with live allocations: the free list then claims 193.2 MiB in 5 blocks in a
200 MiB arena whose only weight is 121.7 MiB, the next weight of that size is refused, and the
refusal reports 205 MiB parked against a 200 MiB limit - both numbers the issue quotes.

It is not only a wrong count. arena_take_locked() hands out whatever block fits, so a stale
extent that is large enough would be given to a second weight while the first still holds it:
one extent, two weights, silent corruption.

The grow path already drops everything else that pointed into the released arena (entries_,
last_use_, resident_bytes_, the free list); the parked list belongs with them, and no lease can
reference a parked extent because eviction only takes entries the cache solely owns
(use_count() == 1).

Measured on Qwen3-0.6B-Q4_K_M, -ngl 0, one gpu-hold ticket, base = the pin's staged binary vs
this build, budgets 200/210/220/230/240. Base: parked exceeds the limit at every budget
(205/200 ... 245/240) and a 121.7 MiB weight is refused at every budget. Fix: resident + parked
+ free sums to the limit (199.3 of 200 ... 239.9 of 240), the free list is one block instead of
4-5, and the 121.7 MiB weight is admitted every time.

Co-authored-by: bong-water-water-bong <[email protected]>
bong-water-water-bong pushed a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 4, 2026
…t use it where it can (engine ggml-org#900)

The residency budget was never compared with the set of host weights a run needs:
placement tested each weight against its file size, acquire() charges the
materialized length (a carrier the dispatch builder picks after placement: 132 B
per row-block for the shared4 layout against 136 B in the file for IQ4_XS, 144 B
for the row64 ones), and the arena's advice is a need+resident+parked snapshot
that moves with the budget - following it cycles 38 <-> 44 MiB on a 4B.

Now the sum is taken before the first request: one entry per distinct
(identity, generation), keeping the largest materialized length seen for each, so
the total is the model's set and not one program's slice of it. When it exceeds
the budget, one line says so; placement declines host-weight ops when the set
alone cannot fit. The other direction is deliberately not inferred: a set that
fits does not mean a run that fits, because the arena also holds the slots parked
while programs are in flight.

Measured on Qwen3-4B-IQ4_XS at -dev HRX0 -ngl 0 -fit off: the set is 304.3 MiB
(579 program observations, GGML_HRX_HOST_WEIGHT_TRACE=1), while the host-weight
path only runs at a budget of 3072 MiB or more - 2048 MiB fails with 505.1 MiB
resident and 1448.6 MiB parked. The number is a lower bound, and the message says
so; the parked-slot accounting is engine ggml-org#900's remaining subject.
bong-water-water-bong added a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 4, 2026
…t use it where it can (engine ggml-org#900) (#200)

The residency budget was never compared with the set of host weights a run needs:
placement tested each weight against its file size, acquire() charges the
materialized length (a carrier the dispatch builder picks after placement: 132 B
per row-block for the shared4 layout against 136 B in the file for IQ4_XS, 144 B
for the row64 ones), and the arena's advice is a need+resident+parked snapshot
that moves with the budget - following it cycles 38 <-> 44 MiB on a 4B.

Now the sum is taken before the first request: one entry per distinct
(identity, generation), keeping the largest materialized length seen for each, so
the total is the model's set and not one program's slice of it. When it exceeds
the budget, one line says so; placement declines host-weight ops when the set
alone cannot fit. The other direction is deliberately not inferred: a set that
fits does not mean a run that fits, because the arena also holds the slots parked
while programs are in flight.

Measured on Qwen3-4B-IQ4_XS at -dev HRX0 -ngl 0 -fit off: the set is 304.3 MiB
(579 program observations, GGML_HRX_HOST_WEIGHT_TRACE=1), while the host-weight
path only runs at a budget of 3072 MiB or more - 2048 MiB fails with 505.1 MiB
resident and 1448.6 MiB parked. The number is a lower bound, and the message says
so; the parked-slot accounting is engine ggml-org#900's remaining subject.

Co-authored-by: bong-water-water-bong <[email protected]>
bong-water-water-bong added a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 4, 2026
ggml-org#900) (#202)

resident + parked is bounded by the arena and the arena by the limit, so the snapshot of a capacity miss is
always limit + one weight. Printed as advice that is the measured 6 MiB-at-a-time climb (2304 -> 2310 on a run
whose floor is 2560) and, below the budget in force, the measured 38 <-> 44 MiB cycle. The capacity branch now
names the numbers and says the snapshot cannot size the run; the fragmented wording (#180) is untouched and the
unbounded arm no longer prints limit 0.0 MiB.

Co-authored-by: bong-water-water-bong <[email protected]>
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