Skip to content

ggml-cpu: add AArch64 NEON quantize kernels - #33679

Draft
wuying39 wants to merge 1 commit into
ggml-org:masterfrom
wuying39:AArch64_NEON_quantize
Draft

wuying39 wants to merge 1 commit into
ggml-org:masterfrom
wuying39:AArch64_NEON_quantize

Conversation

@wuying39

@wuying39 wuying39 commented Dec 4, 2026

Copy link
Copy Markdown

Add hand-written AArch64 NEON implementations for eight quantize_row_*
functions that currently fall through to the scalar _ref versions, and
remove two serial dependency chains in the K-quant vec_dot kernels.

Scope: this speeds up the quantize path, i.e. llama-quantize and
non-quantized -> quantized type conversion. It does not change inference
throughput. mul_mat quantizes activations to vec_dot_type, so for a Q4_0
weight it calls quantize_row_q8_0, never quantize_row_q4_0. Measured with
llama-bench on Qwen2.5-1.5B Q4_K_M and Q4_0 (8 threads): pp512 -0.02%,
pp128 0.00%, tg32 within noise.

Mechanism: the generic quantize_row_* entry points in quants.c are renamed
to *_generic so that arch-fallback.h can route each architecture to a native
implementation; aarch64 then provides NEON versions in arch/arm/quants.c.
The renaming and the arch-fallback.h blocks are part of this change because
the mapping references the *_generic symbols.

Quantize kernels, all verified bit-exact against the _ref implementations
over the full block set, including +-0.0f, subnormals and +-1e30:

  • q4_0: pmax/pmin pairwise reduction for the signed extremum instead of a
    per-lane sign mask, whole-block 32-nibble narrow+interleave with a single
    16-byte store, 2-block unroll. A cold-path rescan (ggml_arm_first_amax)
    preserves the reference's first-in-memory-order semantics when a block
    contains both +A and -A with |A| beyond fp16 range.
  • q4_1, q5_0, q5_1: same structure; q5_0/q5_1 share ggml_arm_qh_16() for
    the 5-bit high bits.
  • q8_0, q8_1: vqmovn_s32/vqmovn_s16 saturating narrow chain plus two
    vst1q_s8 stores, replacing 32 vgetq_lane_s32 extractions.
  • q8_K: vmaxq_f32 reduction for amax with a scalar pass to recover the
    sign, then vcvtnq_s32_f32 + vqmovn + vst1q_s8, bsums via vaddlvq_s8;
    the amax sign scan uses 4-element vceqq_f32 groups.
  • q3_K: new NEON implementation replacing the scalar _ref forwarder.

vec_dot: q5_K splits the nested ggml_vdotq_s32 chain into two independent
sdot chains combined with vaddq_s32; q4_K unrolls the NEON branch by 2 blocks.

Quantize throughput (test-quantize-perf, GB/s):
q4_0 2.03 -> 4.44 (+118.7%) q4_1 1.09 -> 2.42 (+122%)
q5_0 0.89 -> 1.98 (+122%) q5_1 0.87 -> 1.95 (+124%)
q8_0 2.31 -> 4.12 (+78%) q8_1 2.28 -> 4.05 (+78%)
q8_K 1.42 -> 1.58 (+11%) q3_K 0.06 -> 0.13 (+117%)

Tested on aarch64, tsv110, GCC 12.3.1 with test-quantize-fns, test-backend-ops and test-barrier.

@wuying39
wuying39 requested a review from ggerganov as a code owner December 4, 2026 08:55
@ggml-gh-bot

ggml-gh-bot Bot commented Dec 4, 2026

Copy link
Copy Markdown

Hi @wuying39, thanks for your contribution!

Important

Your AI agent does not seem to follow AGENTS.md

Many agents do not load AGENTS.md on their own. Please tell your agent explicitly, e.g.:
Read AGENTS.md and follow it strictly. Then review all changes in this branch against it and fix any violation.

Then update this PR accordingly.

What was found

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • PR Template not respected: Please respect the template when creating a new pull request. Make sure to fill out all required sections.

  • Verbose PR description: The description is too long or verbose, and looks like AI output pasted as-is. Please keep it short, as required by AGENTS.md.

    What was found
    • text hard-wrapped at a fixed width, as if copied from a terminal

Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@ggml-gh-bot ggml-gh-bot Bot added the draft PR will be changed to draft by github-actions bot label Dec 4, 2026
@github-actions
github-actions Bot marked this pull request as draft December 4, 2026 09:24
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning and removed draft PR will be changed to draft by github-actions bot labels Dec 4, 2026

This branch has not been deployed

No deployments
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant