Skip to content

ggml-cpu : fix AMX batched matmul offsets and F16 fallback - #500

Open
bri-prism wants to merge 1 commit into
prismfrom
cpu/amx-batch-byte-offsets
Open

bri-prism wants to merge 1 commit into
prismfrom
cpu/amx-batch-byte-offsets

Conversation

@bri-prism

Copy link
Copy Markdown
Collaborator

What

Two fixes in the CPU AMX extra buffer (x86 with AMX, GGML_NATIVE=ON).

  1. Batched offsets. ggml_batch_offset returns bytes, but the F16 tinygemm launch added it to float * / ggml_fp16_t * pointers, and the quantized paths added the dst offset to a float *. Every batch after the first read and wrote 2-4x too far (out of bounds). Batched matmuls with a 3D weight in the AMX buffer, such as per-head MLA k_b / v_b in F16, came out wrong past the first head.
  2. F16 fallback. F16 weights sit in the AMX buffer unconverted. When the kernels can't take a shape, for example a permuted src1 with more than one token, the AMX buffer declined the op. The CPU backend then also declined it, so the node had no backend and ggml_backend_sched_split_graph aborted on GGML_ASSERT(*cur_backend_id != -1). The AMX buffer now accepts those F16 cases and returns no tensor traits for them, so the regular CPU matmul runs them. Quantized weights are converted at load and keep the old behaviour.

Upstream master has the same code.

Measured (Xeon with AMX, CPU only, an MLA model whose draft layer has F16 k_b / v_b)

  • Before: the draft context aborted at n_ubatch 32. At n_ubatch 1 its logits had 0/23 top-1 agreement with use_extra_bufts = false, and a max logit gap of 14.9.
  • After: n_ubatch 32 runs. Against use_extra_bufts = false, top-1 agreement is 21/23 (ubatch 1) and 20/23 (ubatch 32), with a max logit gap of about 2.0. Node sums along the graph agree within 1.8e-3 relative; the attention part agrees within 6e-5.
  • The remaining gap is between the repack/AMX and plain CPU quantized paths (Q4_0 / Q8_0 activations). It is not from this change. The model used has random trunk weights, which amplifies small differences.

Not covered

  • test-backend-ops doesn't exercise extra buffer types, so there's no new op test.
  • Only checked on one AMX host.

The batch offsets from ggml_batch_offset are in bytes, but the F16
tinygemm launch added them to float/fp16 pointers, and the quantized
paths added the dst offset to a float pointer. Every batch after the
first read and wrote 2-4x too far, out of bounds, so batched matmuls
with 3D weights in the AMX buffer (per-head MLA k_b / v_b in F16)
returned garbage past the first head.

F16 weights are stored in the AMX buffer unconverted. When the kernels
cannot take a shape (e.g. a permuted src1 with more than one token),
let the regular CPU matmul run it instead of declining, which left the
node with no backend and aborted in ggml_backend_sched_split_graph.
@github-actions github-actions Bot added the ggml label Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant