Skip to content

dflash: add beam selection and optimize small PQ2_0 verify batches - #284

Merged
bri-prism merged 2 commits into
prismfrom
codex/yukon-dflash-beam-kernel-20260926
Sep 29, 2026
Merged

bri-prism merged 2 commits into
prismfrom
codex/yukon-dflash-beam-kernel-20260926

Conversation

@bri-prism

@bri-prism bri-prism commented Sep 27, 2026 •

Copy link
Copy Markdown
Collaborator

Overview

Add an opt-in DFlash2 beam selector, remove contiguous copies from a single-block selector score run, and reuse PQ2_0 weight decode across two or three Metal matvec columns.

LLAMA_DFLASH2_BEAM_WIDTH=2..4 ranks candidate paths by conditional log probability; the default remains greedy. The minimum probability cutoff still applies to the selected path. The PQ2_0 path keeps the existing kernel for four or more columns and can be forced back to it with GGML_METAL_PQ2_0_NR1=1.

Additional information

On M5 Pro, a 17,408 x 5,120 PQ2_0 projection improved from 177.40 to 134.00 microseconds for two columns and from 211.31 to 194.47 microseconds for three columns. All 149 PQ2_0 Metal backend parity cases passed. The newer drafter checkpoint was converted to GGUF with all 81 tensors verified against its safetensors export. It preserved target output in local depth-seven tests. On an explanation prompt, the newer checkpoint accepted 84/284 drafts with greedy selection versus 85/277 for the older checkpoint; width-two beam accepted 85/277. On a code prompt, it accepted 98/199 versus 95/223 for the older checkpoint; width-two beam accepted 96/213. These two single-request samples are mixed and do not establish a throughput gain.

Requirements

  • I have read and agree with the contributing guidelines: pending contributor confirmation before review.
  • AI usage disclosure: YES - Codex assisted with implementation, tests, and this draft description.

@bri-prism

Copy link
Copy Markdown
Collaborator Author

@khosravipasha could you review this when you get a chance? #286 stacks on it.

@bri-prism
bri-prism marked this pull request as ready for review September 27, 2026 21:43
Adds kernel_mul_mm_pq2_0_f32_fewrow, a register-resident matmul2d tile of
16 src1 rows by 32 weight rows per simdgroup. PQ2_0 weights are decoded
straight into the right-hand cooperative tensor, so the K loop needs no
threadgroup staging and no barriers. On Metal 4 devices the generic tensor
mul_mm tile is 64 x 128, which computes far more columns than a 4..16 row
verify batch needs, and mul_mv_ext is ALU-bound at 2..8 columns.

The route is taken only when the device has the tensor API, src0 is PQ2_0,
src1 is contiguous F32, there is no batching in dims 2/3, and ne11 is in
[GGML_METAL_PQ2_0_FEWROW_MIN, _MAX] (default 3..32). Below 3 columns the
existing mat-vec paths stay faster. GGML_METAL_PQ2_0_FEWROW=0 turns the
route off, and GGML_METAL_PQ2_0_FEWROW_CFG selects the column-block by
K-split layout.

test-backend-ops perf on M5 Pro, 17408 x 5120, best of 2 (us/run):
  n=3   187.0 -> 151.3   (3-column mul_mv from the parent commit)
  n=8   484.5 -> 157.4   (mul_mv_ext)
  n=16  855.9 -> 172.8   (tensor mul_mm)
  n=32  864.0 -> 328.3
n=1..2 are slower on the new tile (150 vs 71 and 130 us), hence min 3.

End to end on M5 Pro (27B PQ2_0 target, DFlash2 drafter, greedy, depth 7,
8 prompts x 3 reps): with this route off, speculation runs at 0.73x of
plain decoding; with it on, 1.57x (BF16 drafter) to 1.64x (Q8_0 drafter).
Output matches plain decoding except one near-tie (top-2 logprob gap
0.00047) where the half-precision activations pick the other token.

The cooperative tensor element layout was probed on M5 with
get_multidimensional_index before writing the decode.

test-backend-ops gains PQ2_0 MUL_MAT cases for every n from 1 to 32 plus
64 and 512, and perf cases at the 27B projection shapes, plus flash attention perf cases
at the DFlash2 drafter and 27B verify shapes.

The few-row approach is adapted from the qmm_m16_block core in the Yukon
MLX.fast promoted submissions (polymorf and later contributors), used
under Apache-2.0. The PQ2_0 block layout differs, so the decode and
dispatch are new code.
@bri-prism
bri-prism merged commit 3285e75 into prism Sep 29, 2026
8 checks passed
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