Skip to content

feat(models): add Inkling 2 Small and Mini (inkling2, inkling2_loop) - #3367

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

inureyes merged 2 commits into
mainfrom
feature/issue-3228-inkling2

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Adds Thinking Machines Inkling 2 in both released forms, Small (inkling2) and Mini (inkling2_loop), as ModelType::Inkling2VLM, wired into mlxcel generate, mlxcel-server and mlxcel arch. Ported from mlx-vlm 466069f5 (the ops path; the Metal kernels are #3359).

  • Text decoder: block attention residuals, query-dependent attention sinks, separate q/k/v input norms, value RMS norm and l2 clips, dynamic short convolutions on global layers, sigmoid routing with softcapped SwiGLU experts (SiTU), tanh-softcapped logits trimmed to 200058.
  • Mini loop: 48 layer visits over 24 physical layers, one cache per visit, cross-pass message controllers. MLXCEL_INKLING2_LOOP_PASSES overrides the pass count (documented).
  • Attention sinks (upstream f2956e68): each visit keeps a chronological KVCache. A sliding visit retains at most L + W - 1 + 64 keys and attends over the newest L + W - 1 plus a zero sink row, which is the key set AttentionSinkKVCache returns. Convolution histories keep 16 extra rows, so trim_state up to 16 tokens rewinds KV and convolution state exactly, and a deeper trim is refused before anything is mutated.
  • Vision: 40x40 tile processor (no resize, -1 padding, checkerboard alpha), 2-D RoPE ViT, rows * cols placeholder expansion through vlm_runtime. Learned tables load through load_dense_param, so the affine 4-bit export's quantized tables work.
  • Loaders: native mixed release (hf_quant_config.json NVFP4/MXFP8 experts, 2688 * amax / denominator global scale), the mlx-community 4-bit Transformers layout, mlx-vlm conversions. Family keeps checkpoint dtype at load. An MTP-only directory is refused by detection.
  • Tool calls: new Inkling parser (ToolCallFormat::Inkling) for <|message_model|>NAME<|content_invoke_tool_json|>{json}<|end_message|>, claimed before the thinking pass, and a stream-filter header hold so the recipient never streams. A malformed invoke message yields no call and is dropped from content (the feat(tokenizer): Kimi K3 tiktoken family and native XTML renderer #1743 rule, so streamed and non-streamed agree) rather than kept as text. Inkling 1's template emits the same marker, so it benefits too.
  • Batched decode is per-row (PerRowFallback, pinned in the coverage list); padded prefill is declined.

Validation (M3 Ultra, 512 GB, vs mlx-vlm 466069f5)

Check Result
Native Mini text, 128 greedy tokens identical (128/128)
Native text teacher-forced trace (1024 positions) 0/490 decided disagreements, ppl -0.21%
4-bit text trace (mlx-vlm via rename-only remap) 0/494 decided, ppl -0.17%
Native image (1280x720), 128-token trace 600 prompt ids identical, 0/76 decided
One-pass ablation output degenerates on both checkpoints
Server image_url byte-identical to CLI
Server get_weather structured tool_calls, streaming and non-streaming, empty delta.content; tool-result turn answers correctly
Decode 32.1 tok/s native, 32.0 tok/s 4-bit (mlx-vlm 32.8 / 38.5)

Narrow selectors (serial, --profile test-fast --features metal,accelerate): inkling2 (26), server::tool_calls (506), inkling (129), coverage_tests (18), models::detection (97), models::registry (21), model_metadata, model_type, loaded_model, memory_estimate. cargo clippy --lib --tests --features metal,accelerate -- -D warnings, cargo fmt --check and the scripts/ci contract checks pass.

Not included

  • Metal kernels for the short convolution and grouped experts (perf: Port Inkling 2 Mini Metal kernels (sconv, head prep, grouped experts) #3359). Each sits behind one function.
  • DSpark/MTP drafter, audio, is_varloop.
  • Small has no real-checkpoint run (needs a 512 GB host with room for the 393 GB checkpoint). Its path is the Mini path minus the loop, which the tiny-model tests cover.
  • 4-bit image was checked for coherence only. 4-bit decode is about 17% slower than mlx-vlm's kernel path.

Closes #3228

Port the Inkling 2 text decoder, the looped Mini core with cross-pass message controllers, and the 2-D RoPE tile ViT from mlx-vlm 466069f5, and wire them into `mlxcel generate`, `mlxcel-server` and `mlxcel arch` as `ModelType::Inkling2VLM`.

- Loaders for the native mixed BF16/FP32/NVFP4/MXFP8 release, the mlx-community 4-bit Transformers layout and mlx-vlm conversions; the family keeps its checkpoint dtype at load.
- Each layer visit owns its KV and f32 convolution state. Sliding visits keep `L + W - 1 + 64` keys plus a zero sink row, which is equivalent to mlx-vlm's AttentionSinkKVCache (f2956e68), and trims of up to 16 tokens rewind KV and convolution state exactly or are refused without mutation.
- Inkling tool-call parser for `<|content_invoke_tool_json|>` messages, streaming and non-streaming, with the recipient header held in the stream filter.
- Vision tables load through `load_dense_param`, so affine-quantized tables dequantize at load.
- `MLXCEL_INKLING2_LOOP_PASSES` overrides the loop count for ablations.

Closes #3228
Bilingual report for the Inkling 2 Small and Mini port: design, the AttentionSinkKVCache equivalence, checkpoint layouts, the tool-call parser, and the M3 Ultra validation against mlx-vlm.
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions priority:medium Medium priority area:models Model architectures, weights, loading, metadata status:review Under review status:done Completed and removed status:review Under review labels Nov 23, 2026
@inureyes
inureyes marked this pull request as ready for review November 23, 2026 18:00
@inureyes
inureyes merged commit b41c07b into main Nov 23, 2026
32 of 33 checks passed
@inureyes
inureyes deleted the feature/issue-3228-inkling2 branch November 23, 2026 18:01
inureyes added a commit that referenced this pull request Nov 23, 2026
## 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 #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

- [x] `--lib models::inkling2::` and `--lib inkling` (142 passed), `--lib coverage_tests` (18), serially under the GPU lock with `--profile test-fast --features metal,accelerate`
- [x] New parity and gate tests in `kernel_tests.rs` and `kernel_grouped_tests.rs`, plus `tiny_tests::a_rejected_verify_block_rewinds_exactly`
- [x] `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 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:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(vlm): add Inkling 2 Small and Mini (inkling2, inkling2_loop) support

1 participant