Repository navigation
perf(models): port Inkling 2 Mini Metal kernels - #3369
Merged
Merged
Conversation
inureyes
added a commit
that referenced
this pull request
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
force-pushed
the
feature/issue-3359-inkling2-mini-kernels
branch
from
November 23, 2026 19:07
e96087e to
cd0ebbf
Compare
inureyes
marked this pull request as ready for review
November 23, 2026 19:08
This was referenced Nov 24, 2026
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 insrc/models/inkling2/kernels.rs, and falls back to the ops path, which it matches bitwise.MLXCEL_INKLING2_KERNELS=0turns all three off.ShortConvcall (bf16,B <= 4,L <= 8,C % 128 == 0, kernel 4). Instead of upstream'sW - 1 + Lwindow it writes mlxcel's margin history directly (the newestmin(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.speculative_append_active, set byEngine::verify). It follows MLXqmvlane and sum order. It also applies toinkling-2-mini-4bit, whose experts keep the native planes.supports_speculative_verify_rewindand rewinds throughtrim_state, so--prompt-lookupruns on it and reaches the grouped kernel. A verify block keepsmax(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)
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,acceleratekernel_tests.rsandkernel_grouped_tests.rs, plustiny_tests::a_rejected_verify_block_rewinds_exactlycargo 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 checkmake verify,--workspace) left to the orchestratorCloses #3359