Skip to content

[MLX] Support PairTree DFlash2 for Muse Glimmer - #25942

Draft
Gasoonjia wants to merge 2 commits into
gasoonjia/pairtree-dflash2-cudafrom
gasoonjia/pairtree-dflash2-mlx
Draft

Gasoonjia wants to merge 2 commits into
gasoonjia/pairtree-dflash2-cudafrom
gasoonjia/pairtree-dflash2-mlx

Conversation

@Gasoonjia

@Gasoonjia Gasoonjia commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #25901 at 58c2e52 (base branch gasoonjia/pairtree-dflash2-cuda), which adds the shared pieces:

  • the cell KV cache tree step (BatchControl::declare_tree_step / retain_tree);
  • the PairTree builder runtime/dflash2_pairtree.h;
  • the draft_forward_pairtree export method (--dflash2-pairtree);
  • the mode resolver runtime/dflash_mode.h, with its --dflash_tree_budget / --dflash_serial_only options.

What this PR does

PairTree DFlash2 verifies a bounded proposal tree per greedy session instead of Serial DFlash2's single chain of proposals. This PR runs it on the MLX backend with the same options and the same semantics as CUDA:

  • A --dflash2-pairtree export runs PairTree DFlash2 by default, with B = 15 on MLX.
  • --dflash_tree_budget B sets the budget; 0 runs Serial DFlash2. The launcher flag is --dflash-tree-budget.
  • --dflash_serial_only runs the export with Serial DFlash2's programs, rounds and cache steps; the launcher flag is --dflash-serial-only. In the checks below it matched main's MLX executor bit for bit. It refuses a nonzero budget.
  • A plain export always runs Serial DFlash2.
  • Greedy sessions draft first, in tree rounds of their own. Sampled sessions (temperature > 0) then run Serial DFlash2.
  • Each owner's budget is capped by its share of one forward and by the room left in its context (pairtree::round_budget).
  • If the builder refuses one owner's candidate table, that whole round runs Serial DFlash2.

Changes:

  • MLX target cache (MLXBatchedCache, MLXSequenceCache). These are the default batched MLX cache, with one history per session. They now support tree steps:
    • A step stores its tokens densely after the session's history. RoPE and cache positions follow the tree: the root continues the sequence, and a child sits one past its parent.
    • The mask is explicit, with two parts. A history test is built on the device: a sliding layer's window is counted from each token's own tree position. A [T, T] ancestor block is copied from the host. Every layer with the same window shares one mask.
    • retain_tree moves the accepted path next to the history and drops the other branches. It stages every layer first, so a failure leaves the cache unchanged.
    • Any other verb settles a pending tree first, as in the CellCache contract: each tree keeps its root and its unbranched path. A malformed tree step leaves a pending tree pending, and pos() reports a pending tree's sequences as that resolution will leave them. MLX treats a tree as pending once its forward has run on every layer, and CellCache from its first placed layer; in both, pos() matches that cache's next action.
    • The cell layout (batched-cell) uses the shared CellCache tree step unchanged.
  • MLX executor (mlx_executor.{h,cpp}).
    • Resolves the mode with resolve_dflash_mode, using the MLX default and the target cache's tree-step support. A draft_forward_pairtree whose metadata does not match draft_forward's ABI counts as absent, so the default path runs Serial DFlash2. The executor reports the mode and reason, or the refusal, through the factory's startup note, as CUDA does.
    • Serial DFlash2 and serial-only load only draft_forward and never touch draft_forward_pairtree.
    • PairTree mode runs every draft call through draft_forward_pairtree, tree rounds and sampled sessions' Serial rounds alike, and never loads draft_forward. draft_forward_pairtree drafts as draft_forward does; with only sampled sessions, PairTree mode matches Serial DFlash2 bit for bit in the tiny-fixture test. Each MLX method materializes its own constants (3.5 GB per draft method on the 30B export), so loading both cost +3.7 GB of RSS.
    • Builds one tree per owner from the draft's candidate table and verifies every owner's tree in one target forward.
    • The target's greedy argmax walks each tree. The executor then retains the accepted paths and appends the hidden rows of the anchor and the path for the drafter.
    • Returns the path tokens plus the correction or bonus token. That token stays uncommitted, as in Serial DFlash2.
  • Export. The MLX export adds draft_forward_pairtree with draft_forward's inputs. draft_forward and the other methods lower byte for byte as without the flag, and the .pte stores their weights once.
  • Shared checks. Relaxes the checks that [CUDA] PairTree DFlash2: bounded-tree verification for Muse Glimmer #25901 limits to CUDA in backend_factory.cpp, serve_batching.py and export_dflash.py. The factory also passes the startup note to the MLX executor.
  • Docs. A "PairTree DFlash2 on MLX" section with the per-backend default budget, and README and test-README notes.
  • CI (second commit). The MLX job also builds and runs the PairTree builder and DFlash mode tests.

Review order:

  1. MLXSequenceCache.h
  2. MLXBatchedCache.h
  3. mlx_executor.cpp
  4. Tests
  5. The relaxed shared checks

Default budget per backend

The best budget depends on the target's verify cost per row. On these two machines the cost curves differ:

backend hardware verify cost, 32 vs 16 target rows default budget (one session)
MLX Apple M5 Max (64 GB) 1.39–1.50x B = 15
CUDA RTX 5090 1.00x B = 31

B = 15 verifies 16 rows, the same as Serial DFlash2 with 15 proposals. On the M5 Max its acceptance gain is therefore not spent on a wider verify. B = 31 accepts more per round, but its 32-row verify costs more than that gain on the M5 Max.

Results (MLX, Apple M5 Max 64 GB, macOS 27.2.1)

Setup:

  • These numbers were measured with --dflash_tree_budget set explicitly, on an earlier revision of this PR whose single draft_forward returned the PairTree outputs. That is the same graph draft_forward_pairtree is now.
  • Models: Muse Glimmer 30B 17 GB Q4_K_M target (meta-models/Muse-Glimmer-30B-GGUF@b44f1072, sha256 4cc57c0f…) and DFlash2 Q4_K_M draft (incoai/Muse-Glimmer-30B-DFlash2-GGUF@5a384781, sha256 0f1e7959…). Export: --backend mlx --use-offgraph-kv-cache --dflash2-pairtree --activation-dtype bfloat16.
  • Prompts: the same 160 prompts as the CUDA study (MT-Bench 80 turn-1, GSM8K test[:40], HumanEval test[:40]), with z-lab/dflash formatting and the Muse Glimmer template. Rendered-prompt sha256 is a39c0bda….
  • Decoding: greedy, stop at EOS, at most 512 new tokens, single session.
  • Order: all configs interleaved in one process, with the config order rotated per prompt.
  • Metrics: decode tok/s = Σ(tokens − 1) / Σ(speculation-round time). The ratio is paired on the same prompt, with a 95% bootstrap CI over prompts (2,000 resamples).
dataset Serial DFlash2 (N=15): tok/round, tok/s PairTree DFlash2 B=7 PairTree DFlash2 B=15 PairTree DFlash2 B=31
ALL 160 5.01, 68.8 4.64, 0.937 [0.916, 0.957] 5.89, 1.118 [1.103, 1.133] 6.54, 0.908 [0.895, 0.922]
MT-Bench (80) 4.28, 58.7 4.18, 0.988 [0.960, 1.016] 5.12, 1.140 [1.118, 1.162] 5.69, 0.926 [0.908, 0.944]
GSM8K (40) 6.51, 89.7 5.48, 0.845 [0.813, 0.881] 7.38, 1.072 [1.048, 1.097] 8.25, 0.876 [0.851, 0.903]
HumanEval (40) 5.84, 80.0 5.11, 0.884 [0.858, 0.912] 6.75, 1.097 [1.072, 1.124] 7.45, 0.887 [0.857, 0.917]

How to read the table:

  • The Serial column gives tokens per round and decode tok/s.
  • Every PairTree column gives tokens per round, then the decode-throughput ratio vs Serial with its 95% CI.

Highlights:

  • PairTree B=15 was faster on 145 of 160 prompts.
  • An earlier full run of the same benchmark, on a pre-review revision of this PR, gave 1.143 [1.127, 1.158] for B=15, 0.917 for B=31 and 0.963 for B=7. That revision built the whole tree mask on the host.
  • On the final commit, a 40-prompt re-check (every 4th prompt) gave 1.146 [1.118, 1.173] for B=15.

Round time:

config round ms (draft / verify)
Serial 72.8 (17.0 / 55.8)
B=7 72.1 (17.0 / 55.0)
B=15 76.7 (16.8 / 59.8)
B=31 104.8 (16.9 / 87.7)

Acceptance matches CUDA, because the checkpoints are the same. Tokens per round are 5.01 / 5.89 / 6.54 here, vs CUDA 5.02 / 5.93 / 6.57.

MT-Bench per category. Each cell is tokens/round / zero-accept % / full-accept %:

category Serial DFlash2 PairTree DFlash2 B=15 PairTree DFlash2 B=31 B=15 ratio
writing 3.44 / 29% / 3% 4.28 / 11% / 4% 4.75 / 10% / 5% 1.182
roleplay 3.95 / 27% / 4% 4.61 / 12% / 4% 5.19 / 9% / 5% 1.102
reasoning 4.71 / 20% / 6% 5.62 / 7% / 6% 6.24 / 5% / 8% 1.141
math 6.35 / 13% / 11% 7.18 / 4% / 12% 7.99 / 3% / 14% 1.075
coding 4.56 / 18% / 5% 5.44 / 5% / 5% 6.16 / 3% / 7% 1.132
extraction 6.84 / 13% / 20% 7.99 / 4% / 22% 8.61 / 2% / 28% 1.086
stem 3.61 / 28% / 3% 4.37 / 14% / 2% 4.95 / 7% / 4% 1.169
humanities 3.35 / 27% / 2% 4.08 / 15% / 2% 4.40 / 13% / 3% 1.167

Why B=31 loses on MLX: the target verify cost grows with rows. Forward plus all-row logits plus argmax, median ms:

ctx 1 row 8 rows 16 rows 32 rows 64 rows
512 41 49 53 80 109
2048 39 52 70 98 125
8192 44 56 78 110 140

In situ, a round's verify takes 55.8 ms at 16 rows and 87.7 ms at 32 rows. The RTX 5090 costs the same at 16 and 32 rows.

The tree path costs 4–5 ms more per round than linear verification of the same rows (59.8 vs 55.8 ms at 16 rows). A control isolates this: the linear proposals verified through the tree path gave identical text on 40/40 prompts and the same acceptance, at 0.934x [0.927, 0.941] of linear. A width sweep that runs a chain through the tree API costs as much as the tree itself, so the overhead is MLX's masked attention, not the tree shape.

All of the B=15 numbers above already include this overhead. A tree-aware attention path that removed it would put B=15 at about 1.18x; that is a projection, not a measurement.

Serial-only runs as main's MLX executor does

What was measured: on the same inputs and the same .pte, serial-only on this branch matched main's (be23a26) MLX executor bit for bit, in the scenarios below. This is evidence for those runs, not a proof for every input.

Cross-version check. Research-only, not in this PR; driver and patch are in the results bundle.

model scenario main on the PairTree .pte vs branch serial-only on it main on the plain .pte vs branch serial-only on the PairTree .pte
tiny random native-test script, 1 / 4 / 16 sessions identical (102 / 150 / 420 trace lines) identical
tiny always-accepting same, 1 / 4 / 16 sessions identical (46 / 104 / 250) identical
tiny quantized (production draft geometry) same, 1 / 4 sessions identical (110 / 150) identical
30B 1 session, 128 tokens identical (238 lines, 29 executions) identical
30B 4 sessions, one sampled identical (345 lines, 34 executions) identical
30B 2 sessions taking turns, then a rewind and a different second turn identical (226 lines, 25 executions) identical

The script scenario samples every third session, replaces one session, and gives every other session a second turn.

Further checks:

  • The branch on the plain tiny .pte (no serial-only flag needed) also matched main.
  • Negative control: the branch in PairTree mode differs from main on every tiny scenario.
  • Max RSS matched within run-to-run noise: main and branch serial-only each measured between 20.55 and 20.96 GB on the 30B run.

Size and memory.

  • The PairTree export adds 111 KB to the 19.73 GB .pte, and its weights are stored once.
  • Serial-only never loads draft_forward_pairtree.
  • PairTree mode loads only that method, so it stays at the same RSS. Loading both draft methods measured 24.3 GB.

CI (tiny fixtures, same version). check_serial_only_identity, the MLX counterpart of #25901's SerialOnlyRunsThePairTreeExportAsThePlainOne:

  • What it runs: serial-only, and budget 0, on the PairTree fixture, against Serial DFlash2 on the plain fixture. Both fixtures come from one exporter process.
  • Where: random and always-accepting fixtures, at 1, 4 and 16 sessions.
  • What it compares: tokens and, after every execution, each session's target position and pending target hidden rows, bit for bit. It also asserts which draft method is loaded.
  • PairTree mode with only sampled sessions has to match Serial DFlash2 the same way.
  • test_dflash2_pairtree_export_keeps_draft_forward checks every lowered method of the plain program and its named weights byte for byte. It also checks that the .pte stores the draft's weights once. The random fixture's .pte is 28.9 MB plain and +29 KB with PairTree.
  • The MLX CI job now builds and runs muse_glimmer_dflash2_pairtree and muse_glimmer_dflash_mode. muse_glimmer_mlx_dflash runs the identity check.

Test plan

All runs are on the Apple M5 Max, from this branch on top of the CUDA PR branch.

Cache unit tests (MLX). Each tree test checks that every tree token matches the plain chain of its root-to-token path, run in a separate per-sequence cache. It also checks that retain_tree keeps exactly that path for the following steps.

The tree tests cover:

  • multi-owner trees;
  • full-history, windowed (ring-wrapping) and mixed full/windowed layers, over two rounds;
  • refused keep sets, including repeated indices, and malformed tree steps, which leave the tree pending;
  • pos() of a pending tree: the root plus its unbranched chain, once the tree's forward has run on every layer;
  • implicit resolution before other verbs;
  • discarding a whole tree;
  • malformed declarations.

Every test runs on the batched, batched-sequence and batched-cell kinds.

cmake --preset mlx-release -DEXECUTORCH_BUILD_TESTS=ON
cmake --build cmake-out --target mlx_batch_control_test mlx_sequence_control_test extension_llm_cache_test
MLX_ENABLE_TF32=0 cmake-out/backends/mlx/test/mlx_batch_control_test     # 131 passed (17 tree tests)
MLX_ENABLE_TF32=0 cmake-out/backends/mlx/test/mlx_sequence_control_test  # 28 passed
cmake-out/extension/llm/cache/test/extension_llm_cache_test               # 73 passed

Native tiny-fixture tests. These use the real exporter with --dflash2-pairtree-equivalent fixtures: random_pairtree, quantized_pairtree (the production K=16 / block 16 / 202,048-vocabulary draft geometry) and reject_-1_pairtree.

Checks:

  • Packed multi-session, sliced input-anchor, reset/replay, and rewound/cloned continuations match target-only greedy decoding at tree budgets 1, 3, 7, 15 and 31, and so does Serial DFlash2 on the same artifacts.
  • A one-node tree accepts exactly one proposal per round.
  • A seven-node tree accepts several.
  • Greedy sessions keep their trees when batched with a sampled session.
  • Sampled sessions keep the Serial DFlash2 sampling distribution.
  • When the tree builder refuses every candidate table (a fixture whose logsumexp sits below the candidates' mass), PairTree mode matches Serial DFlash2 bit for bit, at 4 and 16 sessions with sampled ones. The same weights with valid tables run trees and differ.
  • Fixtures whose draft_forward_pairtree lacks the extras, or returns them without the logsumexp column, run Serial DFlash2 by default. An explicit budget fails on them.
  • Without a budget, a PairTree fixture runs PairTree DFlash2 at the default and matches target-only greedy decoding.
  • The startup note names the chosen mode and its reason, and a refusal's reason.
  • The serial-only identity check above.
  • The executor rejects:
    • a budget on an artifact without draft_forward_pairtree;
    • a budget wider than one forward;
    • serial-only with a nonzero budget;
    • a negative budget;
    • a budget or serial-only without DFlash.
python -m examples.models.muse_glimmer.tests.export_mlx_dflash_fixture /tmp/mg-mlx-fixtures
cmake -S examples/models/muse-glimmer -B cmake-out/examples/models/muse-glimmer-batching \
  -DMUSE_GLIMMER_BUILD_BATCHING_TESTS=ON -DMUSE_GLIMMER_MLX_TEST_FIXTURES=/tmp/mg-mlx-fixtures
cmake --build cmake-out/examples/models/muse-glimmer-batching
ctest --test-dir cmake-out/examples/models/muse-glimmer-batching --output-on-failure
# 5/5 passed: muse_glimmer_sampling, muse_glimmer_batching,
# muse_glimmer_dflash2_pairtree, muse_glimmer_dflash_mode, muse_glimmer_mlx_dflash

Python.

pytest examples/models/muse-glimmer/tests/test_serve_batching.py \
  examples/models/muse-glimmer/tests/test_export_dflash.py \
  examples/models/muse-glimmer/tests/test_dflash2.py \
  examples/models/muse-glimmer/tests/test_mlx_offgraph.py   # 87 passed

Real model (30B, research tools not in this PR). The test runs one 32-row heap-shaped tree step: siblings and cousins share positions. Each root-to-leaf path is also verified as a plain chain padded to 32 rows. Chain16 vs chain32 gives the noise floor.

context max |Δlogit| tree vs chain noise floor argmax mismatches
278 tokens 0.34 0.32 0/81
424 tokens 0.27 0.30 0/81
2,600 tokens (crosses the 2,048-token sliding window) 0.39 0.45 0/81

As a negative control, a test-only switch makes siblings visible. It gives 22.3 / 14.1 / 14.0 max |Δlogit| and 23 / 22 / 29 mismatches.

End to end:

  • Serial DFlash2's recorded acceptance replays identically from the dumped candidate tables, on 15,389 of 15,389 rounds.
  • The linear chain verified through the tree path reproduces Serial DFlash2's text on 40 of 40 prompts.

Benchmark. The method and the tables are above.

  • The PairTree runs match Serial DFlash2's text on 16 to 23 of 160 prompts. The CUDA PR showed the same pattern: verify-width bf16 near-ties.
  • Mean generated tokens are 476 to 477 and stop rates 22 to 24% for every config.

Limitations

  • The 65 W adapter could not keep up with sustained GPU load. Over the final 78-minute run the battery went from 48% to 14% while charging. Configs were interleaved and paired per prompt, so the ratios are unaffected, but absolute tok/s may be a few percent below a 140 W setup.
  • Only a single session was measured. Multi-session tree rounds are covered by the tests, not benchmarked.
  • Greedy decoding only; sampled sessions run Serial DFlash2.
  • The MLX executor keeps no tree statistics; CUDA keeps them in CudaDFlashStats.

Authored with AI assistance (Metamate).

@pytorch-bot

pytorch-bot Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/25942

Note: Links to docs will display an error until the docs builds have been completed.

❌ 2 New Failures, 3 Pending

As of commit 1403383 with merge base be23a26 (image):

NEW FAILURES - The following jobs have failed:

  • Apple / build-demo-ios / macos-job (gh)
    RuntimeError: Command bash /Users/runner/work/_temp/exec_script failed with exit code 65
  • pull / unittest / linux / linux-job (gh)
    /__w/executorch/executorch/pytorch/executorch/extension/llm/cache/test/cell_cache_tree_test.cpp:82:41: error: reference to local binding 's' declared in enclosing function 'CellCacheTreeTest_SiblingsNeverSeeEachOther_Test::TestBody'

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 1, 2026
@Gasoonjia
Gasoonjia force-pushed the gasoonjia/pairtree-dflash2-cuda branch 2 times, most recently from c30542f to 58c2e52 Compare December 3, 2026 11:30
Run PairTree DFlash2 tree verification on the MLX backend with the same
options and round semantics as CUDA: a `--dflash2-pairtree` export runs it
at B=15 unless `--dflash_tree_budget` or `--dflash_serial_only` says
otherwise, greedy sessions only, with Serial DFlash2 for sampled sessions
and refused candidate tables. Serial-only and Serial DFlash2 never load
`draft_forward_pairtree` and run bit-identically to a plain export; PairTree
mode loads only `draft_forward_pairtree`, so the draft's constants are
materialized once.

The default MLX batched target cache now takes tree steps: a step's tokens
are stored after each session's history, attend through an explicit mask
(history within each layer's window by tree position, plus in-step
ancestors), and `retain_tree` compacts the accepted path next to the
history. Unretained trees are settled before other verbs as in the cell
cache. The MLX export adds `draft_forward_pairtree` with
`--dflash2-pairtree`, and the launcher, factory and export no longer
restrict PairTree DFlash2 to CUDA.

Review order: MLXSequenceCache.h, MLXBatchedCache.h, mlx_executor.cpp,
tests, then the relaxed shared checks.

On an Apple M5 Max, PairTree B=15 decodes 1.12x faster than Serial
DFlash2 over the 160-prompt MT-Bench/GSM8K/HumanEval set; B=31 is slower
there (0.91x) because a 32-row MLX verify costs about 1.4x a 16-row one.
Hence the MLX default B=15; CUDA's single-session default is B=31.

Authored with AI assistance (Metamate).
The Muse Glimmer MLX job now builds and runs muse_glimmer_dflash2_pairtree
and muse_glimmer_dflash_mode with the other host-only batching tests; the
MLX serial-only identity check runs inside muse_glimmer_mlx_dflash.

Authored with AI assistance (Metamate).
@Gasoonjia
Gasoonjia force-pushed the gasoonjia/pairtree-dflash2-mlx branch from 48394d7 to 1403383 Compare December 3, 2026 11:36
@Gasoonjia

Copy link
Copy Markdown
Contributor Author

@claude /pr-review

Please review the current head 14033835dc against base 58c2e5236b (the head of #25901, which this PR is stacked on; review only the two MLX commits). Changes:

  • [MLX] Support PairTree DFlash2 for Muse Glimmer (82d110aa45): PairTree DFlash2 on the MLX backend, with tree steps on all three MLX cache kinds (batched, batched-sequence, batched-cell), the MLX executor's tree rounds via draft_forward_pairtree, and the runtime switch from [CUDA] PairTree DFlash2: bounded-tree verification for Muse Glimmer #25901 (--dflash-serial-only; MLX default budget B=15).
  • [MLX] Run the PairTree DFlash2 and DFlash mode tests in MLX CI (14033835dc): mlx.yml builds and runs the new builder and resolver tests.
  • Serial-only equivalence: cross-version checks against main be23a26276's MLX executor on the same .pte (tiny fixtures at 1/4/16 sessions, and the 30B model at 1 session, 4 sessions with one sampled, and a multi-turn rewind run) are bit-identical; PairTree mode differs (negative control).
  • Pending-tree semantics: MLX counts a tree as pending once its forward has run on every layer, while CellCache counts it from the first placed layer; pos() matches each cache's next action.

@claude

claude Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Claude finished @Gasoonjia's task in 38m 22s —— View job


PR Review: #25942

  • Gather context: PR body and the two MLX commits (82d110a, 1403383) against 58c2e52
  • Run three independent reviews
  • Consolidate findings
  • Fact-check
    • MLX caches (MLXBatchedCache.h, MLXSequenceCache.h)
    • Factory and bootstrap
    • mlx_executor.cpp
    • Tests, export, serving, docs, CI
  • Post review

Needs Discussion: making PairTree DFlash2 the MLX default for --dflash2-pairtree exports splits every mixed greedy/sampled batch into two draft calls and two target forwards, which is unmeasured and probably slower than Serial DFlash2; the other findings are minor.

Detailed review

Summary

The PR adds PairTree DFlash2 to the MLX backend: tree steps on the batched, batched-sequence and batched-cell caches, tree rounds through draft_forward_pairtree in the MLX executor, the MLX --dflash2-pairtree export, and CI runs of the builder and mode-resolver tests. One design question needs an answer before the default ships: the cost of mixed batches (Performance). The other findings are minor contract, test and cleanup issues, and none of them blocks. This is a static review of 82d110a and 1403383 against 58c2e52. MLX cannot be built or run on this Linux runner, so nothing was executed.

API Design

  • MLXBatchedCache::declare_tree_step adds a contiguity rule that the interface does not document (backends/mlx/runtime/MLXBatchedCache.h:120-138).

    • The first/seen check refuses a step whose sequences interleave, e.g. seq_ids = [A, B, A, B] with parents = [-1, -1, 0, 1].
    • CellCache::declare_tree_step (extension/llm/cache/cell_cache.cpp:94-120) accepts the same step, and both caches return true from supports_tree_steps().
    • The contract at extension/llm/cache/cache.h:144-153 does not mention contiguity, so batched/batched-sequence and batched-cell handle the same call differently.
    • This is latent, because the executor only builds contiguous trees.
    • Fix: either state the rule in cache.h and enforce it in CellCache too, or make the MLX cache accept interleaved tokens.

    Fix this →

Testing

  • Implicit resolution of a pending tree is tested only through declare_step (backends/mlx/test/mlx_batch_control_test.cpp:1454, ImplicitResolutionDropsBranches).

    • seq_clone, seq_rm and rewind also call resolve_tree(), and cache.h:157-162 promises the same resolution for them.
    • No MLXTreeStepTest case calls any of these three with a tree pending.

    Fix this →

  • The serial-only identity checks skip the quantized fixtures (examples/models/muse-glimmer/tests/mlx_dflash_native_test.cpp:1126-1132).

    • check_serial_only_identity runs only on the random and reject_-1 pairs.
    • The new byte-identity test in test_mlx_offgraph.py exports only through export_fixture.
    • CI therefore runs neither check on the quantized/quantized_pairtree pair. The Q4_K_M serial-only equivalence rests on the manual 30B run alone.
    • Separately, the negative control at lines 568-571 checks pairtree.find("random") against the full path. If the fixture root contains "random", the always-accepting reject_-1 pair fails. Pass the expectation in as a parameter instead.

    Fix this →

Performance

  • Under the new default, a mixed greedy/sampled batch pays for two rounds (examples/models/muse-glimmer/runtime/mlx_executor.cpp:1016-1018, 1040-1041).
    • With a tree budget, greedy and sampled sessions never share a round. An execute that holds both runs two draft calls and two target forwards, where Serial DFlash2 runs one of each.
    • The split comes from [CUDA] PairTree DFlash2: bounded-tree verification for Muse Glimmer #25901. This PR makes PairTree the MLX default for every --dflash2-pairtree export, but only single-session rounds were measured.
    • Estimate from the PR's own round times, for one greedy and one sampled session:
      • PairTree: about 150 ms per execute (a 76.7 ms tree round plus a 72.8 ms Serial round).
      • Serial DFlash2: about 105 ms for one 32-row round (17 ms draft plus at most 87.7 ms verify).
      • PairTree gains about 0.9 accepted tokens per execute (5.89 vs 5.01), so throughput would fall by roughly a quarter.
    • Please measure a mixed batch (e.g. 4 sessions, one of them sampled) before shipping this default.
    • If it regresses, either run mixed batches as Serial DFlash2, or verify the sampled sessions' chains in the tree round's forward. MLX's target forward is dynamic, so it can take both.

Code Quality

  • The startup note is set before create() can still fail (mlx_executor.cpp:442-452).

    • After the note is set, the capacity/max_seqs check at lines 455-459 can still return InvalidArgument. Anything thrown at lines 460-487 becomes Error::Internal (lines 489-492).
    • batching_bootstrap.cpp:156-162 appends the note to the failure message.
    • So a DFlash2 worker that fails the capacity check reports "MLX DFlash: PairTree DFlash2, tree budget 15 (…)" as the reason.
    • CUDA sets the note just before it returns (cuda_dflash_executor.cpp:844-847).

    Fix this →

  • Tree rounds still build the Serial verification batch (mlx_executor.cpp:962-1004).

    • When trees were built, draft() still runs sample_dflash2_path for each owner and fills verification. speculate_rounds then throws that batch away at lines 1093-1098.
    • This is dead host work in every tree round. A failure in it also fails a round that would have verified as a tree.
    • CUDA returns as soon as the trees are built (cuda_dflash_executor.cpp:1425-1428).
    • Skip only the if (verification) block; the pos check and the hidden-row erase must still run.

    Fix this →

  • The resolve_tree fallback lets siblings become history (backends/mlx/runtime/MLXBatchedCache.h:382-389).

    • The class comment allows a failing later row to leave earlier rows updated. In that case retain_tree refuses (lines 178-180), and resolve_tree falls back to invalidate_step().
    • The rows whose forward did finish then keep all T tree tokens as history, siblings included.
    • pos() (lines 256-270) had reported those rows at the root plus the unbranched chain. Both promises at cache.h:160-162 are broken.
    • Impact is limited: the runner poisons the sessions of a failed execute, so only direct BatchControl callers see this, and only before clear().
    • Resolving per row would keep the contract.

    Fix this →

  • tree_mask ignores the attend stream (backends/mlx/runtime/MLXSequenceCache.h:1264-1275).

    • It calls array, arange, reshape, greater_equal and concatenate without a stream, and the TreeMasks cache is not keyed by stream.
    • The SWA mask in the same file (lines 639-656) passes to_stream(s) to every op and keys CachedMask by stream.

    Fix this →

  • Tree-resolution helpers are duplicated between the two caches (MLXBatchedCache.h:357-377 and 152-167).

    • unbranched_paths is a verbatim copy of extension/llm/cache/cell_cache.cpp:172-193.
    • The keep-set validation repeats cell_cache.cpp:128-144.
    • Both layouts must resolve trees the same way under the shared retain_tree contract. One helper under extension/llm/cache would stop the copies drifting apart.

    Fix this →

Recommendation

Needs Discussion

The open question is whether PairTree should be the MLX default for --dflash2-pairtree exports, since it adds a second draft and verify round to every mixed greedy/sampled batch. A mixed-batch measurement would settle it. The other findings are minor and could land as follow-ups.

Done: three independent reviews covering all 18 changed files in the two MLX commits, merged and fact-checked against the code. Not done: nothing was built or run, because MLX is not available on this Linux runner. The Performance estimate extrapolates the PR's single-session timings; it is not a measurement.

This branch was previously deployed

1 inactive deployment
cadence — 14033835 Deployed Dec 3, 2026 by Gasoonjia via hifi-op-test / hifi4 #42081
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant