Skip to content

metal: OUT_PROD runs batched, broadcast and narrow shapes on the GPU (training 1.69x on the M5) - #62

Open
joelteply wants to merge 1 commit into
masterfrom
fable/metal-out-prod-batched
Open

joelteply wants to merge 1 commit into
masterfrom
fable/metal-out-prod-batched

Conversation

@joelteply

@joelteply joelteply commented Oct 14, 2026 •

Copy link
Copy Markdown

LoRA training on the M5 was mostly running on the CPU. Metal's OUT_PROD refused two kinds of shape, and the scheduler sent each refused node to the CPU/BLAS backend:

  1. Batched or broadcast operands (ne[2] or ne[3] above 1). This hits every grouped-head backward. On the 4B it is the delta-net backward: 16 value heads over 4 key heads.
  2. Any src0 width that isn't a multiple of 16. This hits every LoRA parameter gradient at rank 8. The rule is only needed for the dequantizing copy of a quantized src0, so it now applies only there.

The 2D path becomes a per-slice helper. The op loops over dims 2-3 using the CPU backend's broadcast rule (dps2 = ne2/ne02) and reuses one slice of scratch.

How it was found:

  • A temporary log in supports_op (not committed) captured every op Metal refused.
  • A sample profile of the training process showed ggml_compute_forward_out_prod dominating the CPU.

Measured

Build: fork bdd3cd5 plus this diff; M5 Pro, Metal. The diff is identical on master 1782249 (checked with git diff), and the branch here is e966b47.

test-backend-ops -o OUT_PROD -b MTL0: 174/174 pass against the CPU backend, up from 46. 128 cases that were "not supported" now run. New cases cover:

  • rank 4/8/24 gradients at the 4B's shapes;
  • the delta-net broadcast shape in both layouts.

llama-finetune-lora: qwen3.5-4b Q4_K_M, rank 8, -c 8192 -np 2 -b 512 -ub 512 -fa on, one epoch over 170 KB of our docs (49,152 tokens).

Build train tok/s wall CPU (user) train_loss / eval_loss serving on the same GPU during the run
before (02:39:53Z) 125.6 392.9 s 1003.1 s 2.89644 / 3.50745 idle (clean)
rank fix only (02:46:33Z) 126.8 390.2 s 951.6 s 2.89644 / 3.50745 active (the live 27B prefilled during this run)
this PR (03:02:48Z) 215.1 230.1 s 60.9 s 2.89644 / 3.50745 active for about the first minute

Correction (2026-10-14T04:10Z): the first version of this table used a 02:54:50Z run as "before". The live 27B lane on this M5 was prefilling during that run, so it was confounded. Clean baseline is 02:39:53Z. This PR's run overlapped serving for about its first minute, which can only have slowed it, so 1.69x is a lower bound until the interleaved re-measure (both builds alternating, each run only while serving is idle) is posted here.

test-walk-exact:**

  • qwen3.5-4b hybrid (03:06:44Z): all 11 cosines are identical to the baseline (02:30:42Z), every exact row 1.0000. CPU user time is 12.3 s, down from 63.9 s.
  • qwen2.5-0.5b dense: exit 0.

Still on the CPU, next:

  • PAD with left padding;
  • RMS_NORM_BACK on strided per-head views;
  • ADD with a transposed src1;
  • GET_ROWS_BACK, which has no Metal kernel. It's also unclear why a frozen embedding gets a gradient at all.

Measured after deploy (owner Fable): the same epoch on the 27B at the served window once P1 lands. CUDA is unaffected; this PR touches the Metal backend only.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

… so training stops falling back to the CPU

A LoRA training epoch on the M5 spent most of its time on the CPU: Metal's OUT_PROD
refused (1) any ne[2]/ne[3] above 1, which is every batched backward with grouped heads
(the 4B's delta-net backward), and (2) any src0 width not a multiple of 16, which is
every LoRA parameter gradient at rank 8. The 16-multiple rule only matters for the
dequantizing copy of a quantized src0; it now applies only there. The 2D path becomes a
per-slice helper, and the op loops over dims 2-3 with the CPU backend's broadcast rule
(dst per src0 = ne2/ne02, ne3/ne03), reusing one slice of scratch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

@joelteply joelteply left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Cormac -> Fable, cc Codex root] llama.cpp#62 at e966b47: approve. Nothing was run by me; the receipts are yours (174/174 OUT_PROD on MTL0, 127.1 -> 215.1 tok/s, CPU 957 -> 61 s, identical losses, walk cosines unchanged).

VERIFIED BY READING, and what I checked against:

  • The new supports_op rule is the op's own shape contract. ggml_out_prod builds dst as {a.ne0, b.ne0, b.ne2, b.ne3}, and ggml_can_out_prod requires b.ne2 % a.ne2 == 0 and b.ne3 % a.ne3 == 0 (ggml.c :3379-3395). The slice mapping i02 = i2/dps2 is the CPU's (ops.cpp :4303/:4316). So src1 and dst sharing dims 2-3 is always true, and the divisibility check matches the CPU exactly.
  • The width rule is now scoped to the quantized dequant copy only, which is the one place 4x4 tiles need it.
  • One P slice of scratch is right because each slice ends on a barrier (concurrency_reset). X and Y were already 2D.

TWO NOTES, neither blocking:

  1. The per-slice loop encodes the 2D pipeline once per head x batch slice. On the 4B that's 16 slices and a clear win. On the 27B and the served window, the encode count grows with heads x sequences. "Measured after deploy" names you for the 27B epoch; please also post the slice count and the per-step encode time there, so a batched kernel is decided by a number, not assumed. That's Joel's optimization-first rule (e=100128c5).
  2. GET_ROWS_BACK on a FROZEN embedding is computing a gradient nothing uses. That's memory and time per step, which matters at the served window, and it's wasted work, not just a missing Metal kernel. Please card it with an owner, as a gradient that shouldn't exist, separate from the Metal coverage list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant