Skip to content

perf(models): port Inkling 2 Mini Metal kernels - #3369

Merged
inureyes merged 2 commits into
mainfrom
feature/issue-3359-inkling2-mini-kernels
Nov 23, 2026
Merged

inureyes merged 2 commits into
mainfrom
feature/issue-3359-inkling2-mini-kernels

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Ports mlx-vlm's three Inkling 2 Mini Metal kernels (02053f5c, de08fdf8, 8e80d434) on top of #3367. Each is Metal-only behind select_kernel_port, gated in src/models/inkling2/kernels.rs, and falls back to the ops path, which it matches bitwise. MLXCEL_INKLING2_KERNELS=0 turns all three off.

  • Fused short convolution for every ShortConv call (bf16, B <= 4, L <= 8, C % 128 == 0, kernel 4). Instead of upstream's W - 1 + L window it writes mlxcel's margin history directly (the newest min(W - 1 + margin, R + L) rows), so the trim exactness of feat(models): add Inkling 2 Small and Mini (inkling2, inkling2_loop) #3367's layout is kept on both paths.
  • Attention head preparation (l2 clip of q/k/r, gamma-free value norm) in one launch on looped models, reading the model's f32 scales.
  • Grouped NVFP4/MXFP8 expert matvec, only inside a speculative verify forward of 2 to 8 tokens (speculative_append_active, set by Engine::verify). It follows MLX qmv lane and sum order. It also applies to inkling-2-mini-4bit, whose experts keep the native planes.
  • Inkling 2 now reports supports_speculative_verify_rewind and rewinds through trim_state, so --prompt-lookup runs on it and reaches the grouped kernel. A verify block keeps max(16, L) extra convolution rows, so a block longer than the margin can still be dropped whole.

Not ported: the sconv padding-mask variants (mlxcel's conv path has no mask; padded prefill is declined) and the training/sharding gates (no such Inkling 2 path here).

Results (M3 Ultra)

kernels off (A) on (B)
Native decode, tok/s, mean of 3 32.07 35.29
4-bit decode, tok/s, mean of 3 36.81 44.76
Native verify forward, width 4 / 8, ms 52.0 / 60.4 45.7 / 51.8

Greedy 128-token output is byte-identical across arms on both checkpoints. With the speculative scope removed, so the grouped kernel does not run, width 4 / 8 takes 46.9 / 54.5 ms. That gives the grouped kernel alone -2.6% / -5.0%. No kernel is slower than the path it replaces. Details: docs/benchmark_results/inkling2-mini-kernels-m3ultra-2026-11-24.md.

Test plan

  • --lib models::inkling2:: and --lib inkling (142 passed), --lib coverage_tests (18), serially under the GPU lock with --profile test-fast --features metal,accelerate
  • New parity and gate tests in kernel_tests.rs and kernel_grouped_tests.rs, plus tiny_tests::a_rejected_verify_block_rewinds_exactly
  • cargo clippy --lib --tests --features metal,accelerate -- -D warnings, cargo fmt --check, check_used_by, check_kernel_dtype_keys, check_env_var_docs, check_crate_versions, check_kernel_port_dispatch, check_binary_assets, Apache header check
  • Broad gate (make verify, --workspace) left to the orchestrator

Closes #3359

@inureyes inureyes added status:review Under review type:performance Performance improvements priority:medium Medium priority area:models Model architectures, weights, loading, metadata labels Nov 23, 2026
inureyes added a commit that referenced this pull request Nov 23, 2026
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 23, 2026
Ports mlx-vlm's three Inkling 2 Mini kernels (02053f5c, de08fdf8, 8e80d434) as Metal-only launchers behind mlxcel::select_kernel_port, each gated in src/models/inkling2/kernels.rs and falling back to the ops path, which it matches bitwise. MLXCEL_INKLING2_KERNELS=0 keeps every forward on the ops path.

- Short convolution: one kernel per ShortConv call. It writes mlxcel's margin history (the newest min(W-1+margin, R+L) rows) instead of upstream's W-1+L window, so tail trims stay exact.
- Attention head preparation: l2 clipping of q/k/r and the gamma-free value norm in one launch on looped models, reading the model's f32 scales.
- Grouped expert matvec: inside a speculative verify forward (speculative_append_active) of 2 to 8 tokens, routes are sorted by expert and each NVFP4/MXFP8 expert is decoded once per tile of four routes, in MLX qmv lane and sum order.

Inkling 2 now reports supports_speculative_verify_rewind and rewinds through trim_state, so --prompt-lookup verify rounds (and the grouped kernel) run on it. A verify block keeps max(16, L) extra convolution rows so a block of any allowed length can be dropped whole.

Validated on an M3 Ultra: bitwise parity and gate tests, identical 128-token greedy output with and without the kill switch on both Mini checkpoints, decode 32.1 to 35.3 tok/s native and 36.8 to 44.8 tok/s 4-bit, verify forward of 8 tokens 60.4 to 51.8 ms (docs/benchmark_results/inkling2-mini-kernels-m3ultra-2026-11-24.md).

Closes #3359
@inureyes
inureyes force-pushed the feature/issue-3359-inkling2-mini-kernels branch from e96087e to cd0ebbf Compare November 23, 2026 19:07
@inureyes
inureyes marked this pull request as ready for review November 23, 2026 19:08
@inureyes
inureyes merged commit 7c0bf3f into main Nov 23, 2026
33 checks passed
@inureyes
inureyes deleted the feature/issue-3359-inkling2-mini-kernels branch November 23, 2026 19:09
inureyes added a commit that referenced this pull request Nov 24, 2026
…3392)

## Summary

Inkling 2 now runs one forward over every row of a batch instead of one forward per row, and batches cold prompts in a padded prefill cohort. At 8 concurrent requests the server's aggregate throughput on Mini goes from 25.9 to 62.7 tok/s (native) and from 30.3 to 62.7 tok/s (4-bit); concurrency 1 is unchanged.

- Batched forward (`src/models/inkling2/batched.rs`): projections, convolutions, residual mixes, Mini's loop messages and experts run over `[B, L]`; only the attention core (relative mask, sliding window, sink, log scaling) runs per row against its own KV cache, as Inkling 1 does (#2673). Convolution histories are stacked left-padded to the longest row, and each row gets back exactly the history its own forward keeps, so per-row trims and the speculative rollback stay exact.
- One row routes to `forward_with_sequence_id`, so the B = 1 graph, the #3369 kernel gates (short convolution `B <= 4`, head preparation and grouped experts `B == 1`) and the DSpark verify path are unchanged. B > 4 takes the ops convolution.
- Padded prefill: the server right-pads its cohort, and every Inkling 2 mask and convolution is causal, so real positions never read padding. A batched forward of more than one token keeps as many convolution rows as it appends (`conv::WideHistoryScope`), so `trim_state` removes any row's padding exactly. `supports_batched_prefill` and `supports_padded_prefill` are true, also through the VL wrapper. The cost is that this history is held from the cohort forward until the next decode step drops it.

## Validation

M3 Ultra, every GPU run under the shared lock; details in `docs/benchmark_results/inkling2-batched-decode-m3ultra-2026-11-24.md`.

- Tiny looped model (f32): B = 3 rows at 3, 12 and 30 tokens (one past the window), with a trim on one row and a row leaving mid-batch, match their single-row decodes. A 3-row cohort right-padded to 48 (25 and 43 padding rows, past the 16-row margin) matches unpadded prefill in last-row logits, KV, convolution state and the following decode. The bound is 2e-4 (observed 3e-5). Padding on the wrong side of the stacked history fails both tests by about 3.
- Traces, B = 1 against B = 4 and B = 8: 0/98 decided disagreements on both checkpoints, 2 to 7 undecided ones out of 256 (largest gap 0.19). The B = 1 traces of main and this branch are byte-identical, and greedy text is identical on both checkpoints.
- Free-running server requests at temperature 0 no longer always equal their solo answers under concurrency: they split after 47 to 238 characters at near-ties. The same splits appear with `--max-batch-prefill 1`, so they come from batched decode arithmetic, not the padded prefill.
- Selectors: `--lib inkling` (168), `--lib coverage_tests` (18), `--lib prefill_cohort` (23), `--lib pad_trim` (4); clippy `--lib --tests`, fmt and the CI contract scripts pass. `make verify` is left to the orchestrator.

Closes #3377
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:models Model architectures, weights, loading, metadata priority:medium Medium priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: Port Inkling 2 Mini Metal kernels (sconv, head prep, grouped experts)

1 participant