Repository navigation
qwen3_5 Metal: opt-in ANE share of prompt MLP columns (TF_ANE_COLUMNS) - #6334
joshuaswarren wants to merge 10 commits into
Conversation
qwen27 is the only family that uses the Neural Engine today. This lifts its Share into zig/src/metal/ane_share.zig — parameterized on a shape and a per-layer program maker so a family supplies checkpoint dequant and a MIL choice and reuses the worker, surfaces and SharedEvent dance — and wires the same prompt overlap into qwen3.5. qwen3.5's projection kernels are generated per shape and cannot take a column range, so it splits prompt rows instead: the Neural Engine runs rows [0, TF_ANE_COLUMNS) of each prompt MLP end to end through the existing mlpSlice MIL over 128-row fp16 planes, the GPU runs the remaining rows on its existing kernels with buffer offsets, and two small kernels copy s.x into the input plane and the output plane into s.r. Signal and wait encode in-order inside the single forward command buffer. Opt-in only: TF_ANE_COLUMNS stays 0 by default, every default path is unchanged, and admission gates on ane.available() alone so M1 through M5 qualify. Not exact-output qualified. The kernel source is embedded from the family's kernels.zig and the share reuses the existing TF_ANE_COLUMNS extent, so no shared registration files change; a dedicated per-family rule can follow. Refs ashhart#6310. Generated with [Devin](https://devin.ai)
Row-split put the whole MLP (including the 6144-wide output reduction) on the ANE in fp16: measured live on M1 Ultra it was 2.5x slower than the GPU baseline and >=64-row shares collapsed to immediate EOS. Match qwen27's model instead: the ANE owns gate/up columns [0, columns) and returns silu(gate)*up activations; the GPU keeps the remaining columns via contiguous affine-row suffix views on the existing qmm pipelines and runs the full-width down projection, so fp16 never holds a long reduction. TF_ANE_COLUMNS now names real intermediate columns; valid values are 2048/4096/5632 (GPU rest lands on qmm_4096/2048/512_2048). Wire the loaded share's out-of-footprint price into qwen35 admission. Generated with [Devin](https://devin.ai)
A fp16 silu(gate)*up computed on the Neural Engine overflows far below the fp16 range of its factors; live A/B showed corrupted logits (repetition, echo) at C=2048. Match qwen27's proven shape: the program emits gu raw into a [2C][128] plane and the merge computes the product in fp32. The output surface doubles; the weight blob is unchanged. Generated with [Devin](https://devin.ai)
The column share corrupted prompt output non-deterministically. Metal 4 defers command-buffer events to the queue at commit and lets a buffer's encoders overlap, so ending and reopening the encoder around an in-order signal raced the tail projections against the pack and attention work. Take the share's own signal/wait handoff instead: fence the encoder, commit the buffer so the queued signal releases the worker, then reopen behind the fence. The share carries the MTLFence; forward's mlp keeps a lane whose buffer the handoff swaps.
Registers both ane_share.metal kernels: the fp16 plane pack and the fp32 silu merge, each with a planted control, launch records against M1/M2 (1024 both, offline-compiled), and the launchOf mapping. SUMMARY 2 kernels: 2 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed; 0 limit problems.
The qualify check compares a wide forward against solo rows bit-for-bit; run it on the GPU pipeline the contract actually covers, then reattach the share. And bound the share to passes a merged decode round cannot reach, so co-batched rows never change a stream's tokens.
Decode parity in the mirrored ABBA, bench_concurrent alone/serial clean on both arms, and the fp16-vs-bf16 prompt-logit drift named instead of a verified receipt.
Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
Follow-up measurement on the exact-output ask — tested whether the fp16-vs-bf16 drift is a merge-kernel artifact or inherent. Hypothesis tested. The merge kernel reads the ANE's fp16 gate/up plane as Method. Result. Patched arm still drifts on the same 2/4 prompts ( Conclusion. The residual isn't a grid misalignment the merge can fix — it's the ANE's in-chip fp16 conv accumulation vs the GPU's fp32-accumulate→bf16 GEMV. Plane inputs are already lossless (bf16→fp16 is exact in-range), so the divergence lives inside the ANE's dot product and no downstream rounding can recover it. Greedy bit-equality is not achievable at this decomposition — consistent with the design's own Suggestion: land the share as the documented non-exact opt-in it is, with this quantified drift (2/4 greedy prompts on the receipt pack, divergence typically late in generation) stated in the receipt, unless the exact-output gate is meant to block the mechanism entirely. |
|
Thanks @joshuaswarren. Same finding as on #6435, which carries these commits: with
|
…e the default path's gate and up tensors; the host Metal facade gains the share stand-ins the schedule test needs
|
Cross-posting from the #6435 review follow-up since the same commits live in this stack — full detail there: #6435 (comment) Head
The bit-exactness blocker stands, measured on the M1 Ultra receipt host: the ANE conv accumulates at fp32+ grade but only emits fp16; Also flagging for merge sequencing: #6467 adds its own |
What this changes
Adds an opt-in
TF_ANE_COLUMNS=Cshare of the qwen3_5 prompt MLP: the first C columns of everygate/upprojection in a prompt pass run on the Neural Engine through a new generic
metal/ane_share.zig(program pool +worker + fence-ordered handoff), while the GPU keeps the tail columns, the fp32 SiLU·up merge and the down
projection. Unset, the binary takes the stock path.
atc overlap --json: scoped run over the 14 PRs sharing files with this diff reports 0 conflicts. Six openPRs merge-conflict with
upstream/mainitself in files I also touch — Unlimited-OCR-2 on CUDA: the family's steps on NVIDIA GPUs (sm_89 and up) #2077, qwen27 metal: qk projections, flat glue and Metal 4 verify passes, each bit-identical and switchable #2342, qwen27 Metal: DSpine drafter for Qwen3.8-27B, opt-in #2634, glm metal: use shared events for paired transfers #3293, SenseNova Loop CUDA: MTP drafting with the checkpoint's own head (--drafter mtp) #3867(shared kernel-contract registries) and logit_bias: token biases and -100 bans on each row's draw for Qwen CUDA, Qwen3.5-2B Metal and Nemotron Parse HIP #3146 (qwen3_5/backend.zig logit-bias work).
git merge-treeshowseach already conflicts with main absent my hunks, and their hunks sit in different regions than mine
(
all_metal~L118/import ~L220,resolve~L68,collect~L329,Metal.init~L50); the collisions arestale-PR-vs-upstream drift, disclosed here per the contributor instructions.
Checks on this head
733310feepython3 tools/zig/lean_check.py, on the base and on this head: base 514 findings, head 514 —0 new.
zig build test test-goldenata3c994d47,Apple M1 Ultra — 22456 passed / 6 failed / 201 skipped (22663 total). All 6 failures are in files this
change does not touch (retained_taps_host/phase_trace, lfm3, inkling2, rope, tfq5, deepseek chat) and
reproduce at the merge-base: pre-existing upstream breakage.
Receipt
docs/receipts/qwen35-ane-share-m1-ultra/— raw ABBA JSON plusbench_concurrent/bench_cache/bench_openaitranscripts for both arms.TF_ANE_COLUMNS(already registered inplatform/environment.zigfor qwen27): a whole columncount,
0/unset disables the share (the default). Values above the shared width lose outright(C=5632 measured +75% TTFT); C=2048 is the measured M1 Ultra point.
733310fee, Zig 0.17.0, Metal4/MTL4 on Apple M1 Ultra, macOS arm64;mlx-community/Qwen3.5-2B-MLX-4bit@93760be4f1f69842a46bc13dbdc0f19e291392a3.env is set — ran. Every other family/backend and the default path — unchanged; host suite above.
tools/bench_concurrent.py --alone --serial: headalone.equal12/12 on all four rows,serial.equalclean with drafts accepted (196/135 on the code rows) — identical to base. Bits: the shareevaluates fp16 where the GPU path uses bf16, so prompt logits can differ by that rounding and greedy decode
amplifies it — 2 of 4 greedy receipt-pack prompts emit different tokens vs the GPU path. Same-build
consistency (solo vs concurrent, drafted vs undrafted) is unaffected; no
perf_receipt.pyreceipt can verifya token-drift change by construction, so
decode-abba-51fa1a34.jsonis committed as raw evidence instead.tools/bench_openai.py: base 118.0–119.2, head 119.0–120.7 tok/s —parity; the row floor keeps every decode round on the stock path.
base 1.506 s vs head 1.535 s median (
bench_cache --require-cold, qualified); 9013-token ABBA base6.349/6.320 s vs head 6.288/6.200 s. The share sits at the GPU/ANE crossover on M1 Ultra — the value is the
verified path plus a knob for chips where the ANE is relatively faster (M5/M6 verification next).
bench_cacheline above.Kernel contract, only when a GPU kernel is new or changed
qwen35_ane_pack(bf16→fp16 rowgather into share planes) and
qwen35_ane_merge(fp32silu(gate)·upover the shared columns) — new, nopredecessors.
pack_zero(zeroed plane) andmerge_no_sigmoid(missing SiLU) both go red at the shapes gate as planted.SUMMARYline and exit status: metal — pack and merge pass nonfinite/repeat/shapes; sums N/A(
.accum = .none); limits pass (128/256 threads vs pipeline maximum 1024); offline compiles confirmM1 = 1024 and M2 = 1024 recorded limits, records exact.
Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.--drafter, no slower than without it, greedy and sampled — for the default path the flag is off; with the flag on, decode is parity (-0.01%) and prompt is the disclosed crossover (loses ~2% at 2.5k tokens, wins ~1.4% at 9k on M1 Ultra).