Skip to content

feat(inkling2_loop): add inkling-2-mini support - #942

Open
ischencheng wants to merge 31 commits into
FlashML-org:mainfrom
ischencheng:feat/inkling2-mini
Open

ischencheng wants to merge 31 commits into
FlashML-org:mainfrom
ischencheng:feat/inkling2-mini

Conversation

@ischencheng

Copy link
Copy Markdown
Contributor

Fixes #941

Adds thinkingmachines/Inkling-2-Mini (Inkling2LoopForConditionalGeneration, revision 6e7c8248f7): text and image input, the official NVFP4/MXFP8 checkpoint, its DSpark draft, and fused, offload, hybrid and cpu experts.

It's an Inkling 2 MoE (24 layers, hidden 2304, 128 experts top-8, 20 sliding-window layers and 4 global ones) whose layers 6-17 run three times per token with shared weights. Passes 2 and 3 restart from the residual state after layer 5 and mix in four messages per layer (q, k, v, MLP inputs) that learned queries read from the previous pass, through low-rank tanh gates. Layers 18-23 then run once on the last pass's state. The reference is huggingface/transformers#51050 (6173a2f679), models/inkling2_loop/.

What it needed here:

  • Every run of a layer keeps its own KV and short-convolution state, as in the reference's cache_offset: 48 cache layers (40 sliding, 8 global) on 24 weight layers. ModelConfig.num_kv_layers sizes the KV pool apart from num_layers, and the convolution windows (k, v, after attention, after the MLP, 3 tokens each) are slot_states of LinearStatePool, which now builds with no GDN group.
  • The attention has a data-dependent relative position bias (d_rel 16 over the last 1024 tokens), a log scale on the global layers, L2 clipping of q, k and the relative states, an unweighted RMSNorm on v, one sink logit per head and a sigmoid output gate. None of the existing backends take that bias, so it has its own Triton kernel for extend, decode and verify on the paged pool (kernel/triton/inkling2_attention.py). Logits are fp32. The kernel reads unquantized KV only, so --kv-cache-dtype other than the default is refused at config time (ModelConfig.kv_formats).
  • Block attention residuals (a softmax over depth every 6 layers, fp32), the loop's message queries and gates, and the head: a bf16 GEMM, then a softcap of 30 in fp32 as in the reference, with the 201024 -> 200058 vocab padding masked.
  • Experts: sigmoid routing with a correction bias, route scale 8, and a softcapped SwiGLU (swiglu_softcap: 4*tanh(g/4)*sigmoid(g)*25*tanh(u/25)) in the GPU kernels and the CPU executor. Layers 0-19 are NVFP4 and run W4A16; the experts declare no static activation scale. Layers 20-23 are MXFP8, which has no expert kernel here yet, so they are dequantized to bf16 at load (lossless, about 4 GB more). w13 stores gate and up rows interleaved. The checkpoint's config.json calls the whole model NVFP4, so a new ModelSpec.quant_config hook builds a ModelOpt MIXED_PRECISION config from hf_quant_config.json's parameter groups instead.
  • Experts of several formats can now be resident (the first open item of 🚧 [WIP] MoE cache, CPU experts and GGUF support #671): each format gets its own resident banks and each layer reads only its kernel's roles. --moe-strategy cpu takes them too: with several slot pools it sizes each pool so every bank stages two whole layers in its widest format. --moe-cpu-layers with several formats still refuses.
  • The expert cache stays keyed by weight layer, so in decode passes 2 and 3 hit what pass 1 fetched. The offload prefill streams every run's whole layer, 48 per chunk, in the model's run order (moe_layer_order, since layer 6 follows layer 17 twice) and clears its staging after the last call in that order. That is why even a 26-token prompt takes about 1.35 s to first token under offload on the A10.
  • Vision: the ViT tower (23 layers, 40x40-pixel tiles, axial 2D RoPE) and the image processor, which pads instead of resizing and composites transparent images on the checkpoint's checkerboard. Since nothing shrinks an image, one with more tiles than --image-max-tokens (16384 when unset, a 5120x5120 image) is refused. The features replace the token embeddings after embed_norm, as in the reference.
  • inkling2 reasoning and tool-call parsers, following the checkpoint's chat_template.jinja and response_template: <|message_model|>[name]<|content_KIND|>...<|end_message|> messages, thinking to reasoning, invoke_tool_json calls with {"name": ..., "args": {...}}. auto picks both from model_type / architectures.
  • DSpark (mtp.safetensors, 3 layers, block 8, Markov head) under --speculative-algorithm mtp. Its taps are the inputs of runs 5, 23 and 41 in execution order and the final hidden. Convolution windows roll back on rejection like the GDN verify.

Shared code this touches, and what it does for other models:

  • Resident experts of several formats used to raise at load; now they load. --moe-strategy cpu used to refuse several slot pools; now it sizes them by bytes, so other mixed-format checkpoints can take it too. Single-format models keep 2E slots.
  • swiglu_softcap is a new CPU MoE activation id, so a CPU MoE module built before this is refused by the ABI probe instead of computing silu.
  • LinearStatePool builds with only slot states; ModelConfig.num_kv_layers and no_prefix_reuse and ModelSpec.quant_config default to what every other family does today.

If you'd rather take the resident mixed-format experts or the DSpark draft as their own PRs, I can split them out.

Tested on an RTX PRO 6000 (sm_120) and an A10 (sm_86), driver 610.57.04 on the runs that logged it (620.70.06 on the A10G of the post-merge test run), torch 2.14.1+cu130, triton 3.8.0, checkpoint thinkingmachines/Inkling-2-Mini@6e7c8248f7. The containers report no CPU model name (4 cores with the RTX PRO 6000, 16 with AVX-512 VNNI on the A10). The runs drive freetoken.LLM from a script, except GSM8K, which starts the server:

  • parity: LLM(model, moe_strategy="fused", max_running_req=2, max_seq_len_override=16384), once eager (cuda_graph_bs=[]) and once with cuda_graph_max_bs=2
  • 16 GB: LLM(model, moe_strategy=<offload|hybrid|cpu>, memory_ratio=0.85, max_running_req=2, cuda_graph_max_bs=2), with moe_cache_auto=True for offload and hybrid and max_seq_len_override 8192 on the A10, 16384 on the RTX PRO 6000
  • DSpark: LLM(model, max_running_req=4, max_seq_len_override=16384) with and without speculative_algorithm="mtp"
  • GSM8K: python -m freetoken.cli serve --model thinkingmachines/Inkling-2-Mini --max-running-requests 32 --cuda-graph-max-bs 32 --moe-strategy fused --max-seq-len-override 8192 --max-extend-length 2048, then /v1/chat/completions with temperature 0

Parity against the reference (bf16, experts dequantized to bf16, eager attention) on four prompts: a 26-token chat, a 157-token tool call, a 3993-token code summary and a 462-token image prompt (432 image tokens). The comparison is per run of a layer (48 per forward), relative L2:

  • Each run fed the reference's own inputs (embedding, block states, messages), on the three text prompts: median 0.004-0.009, max 0.031, about the same for sliding and global layers, the three passes and the NVFP4 and dequantized-MXFP8 layers.
  • Free running, the error grows with depth to 0.15-0.30 at the final norm, mostly from routing near ties (a token swaps one expert). The last 16 positions' top-1 agrees 16/16, 15/16, 16/16 and 14/16. Greedy 48 tokens match the reference on the tool call; the others split at token 5, 11 and 39. With the reference's bf16 rounding of the attention logits imitated in the kernel, the 26-token chat matches 48/48.
  • The vision tower against the reference in fp32: 0.033, the reference's own bf16 gives 0.032. The processor's pixels and expanded input_ids are equal to Inkling2ImageProcessor / Inkling2Processor on 7 images (RGB, L, RGBA, LA, P with transparency, odd sizes).
  • CUDA graphs (max bs 2) give the same per-run errors, logits and tokens as eager.
  • GSM8K through the OpenAI server, the first 400 test questions, chat template, greedy, reasoning_effort low, max_tokens 4096, 32 at a time on the RTX PRO 6000 with fused experts: 390/400 (97.5%), none cut by length or without an answer.

16 GB: another process holds the rest of the card, so the engine sees 16.0 GiB, --memory-ratio 0.85, CUDA graphs. On the A10 the 26-token chat's 198-token answer equals the reference's text under all three strategies.

GPU --moe-strategy decode tok/s, 1 stream TTFT 26 / 3993 tokens
RTX PRO 6000 offload 28.7 -
A10 offload 19.9 1.35 / 3.54 s
A10 hybrid 22.0 1.35 / 3.54 s
A10 cpu 16.3 1.35 / 3.53 s

At the default --memory-ratio 0.9 the 3993-token prefill runs out of memory in fused_experts_nvfp4 with 1.5 GiB left after init; docs/models.md says to start at 0.85. The test containers' PCIe (13-25 GB/s between A10 hosts) and server CPUs don't say how a desktop does.

DSpark on the RTX PRO 6000 (moe_strategy auto, full card, CUDA graphs 1/2/4): 3.21 tokens per verify (138 verifies over 3 prompts, up to 256 tokens each), one stream 34.0 -> 54.2 tok/s. Greedy output with and without drafts is the same on the tool call (82/82); the chat splits at token 5, where it also splits from the reference, and the code summary at token 76.

Tests on the RTX PRO 6000 at 80b6ef2, before merging main: pytest -m "not slow" tests/ 2688 passed, 315 skipped, 4 failed, and the same four fail on main 2fb371e: test_capture_rows_per_req.py::test_graph_replay_matches_eager_attention[fa-1] and [fa-4] (no FA3/FA4 kernels for sm_120), test_pinned_tensor.py::test_host_device_ptr_is_identity_under_uva and test_swiglu_clamp.py::test_triton_matches_reference. After merging main c84946b (#916, the CPU executor's per-expert grouping), tests/moe/test_cpu_moe.py, tests/moe/test_mixed_expert_formats.py, tests/kernels/test_swiglu_clamp.py and tests/models/inkling2_loop/ on an A10G at this head: 86 passed, 27 skipped. The new tests check against independent references: a port of the reference's ops for attention, convolution, routing and the activation (checked equal to the transformers classes on CPU, maxabs 0 for all but the activation at 7.6e-6), the reference's conversion ops for the checkpoint transforms, the HF image processor, a template render -> parse -> render round trip for the parsers, and a dense recompute for every DSpark acceptance count.

Not covered

  • The prefix cache is off for this model: a cached prefix would need the 48x4 convolution windows at the cut, and nothing snapshots them yet. A new ModelConfig.no_prefix_reuse makes the engine serve --cache-type radix as naive with a warning.
  • The naive cache reserves each running request one window plus one prefill chunk of KV on all 40 sliding layers, about 1.4 GiB at the default --max-extend-length 8192, so many concurrent requests need a smaller --max-extend-length (docs/models.md says so).
  • MXFP8 experts are served as bf16, not natively.
  • The default image cap (16384 tokens) is a guard, not a measured limit: the largest image I ran is 432 tokens, and the tower's activations for a big image are not in the memory budget, so a 16 GB card may need a lower --image-max-tokens.
  • GGUF: llama.cpp has two open conversions (Inkling 2 Mini ggml-org/llama.cpp#32694, model: Inkling-2-Mini - looped arch ggml-org/llama.cpp#32695) with different architecture names and layouts, so there is no file format to read yet.
  • The variable-loop research checkpoint (is_varloop).
  • --moe-strategy cpu with other mixed-format checkpoints (mixed-type GGUFs) loads now, but I only ran it on this one.
  • TP, ROCm, consumer cards, a local NVMe. The 16 GB numbers come from a 96 GB and a 24 GB card with the rest of the memory held by another process.

# Conflicts:
#	tests/moe/test_mixed_expert_formats.py
KarrAcaRn pushed a commit to KarrAcaRn/FreeToken-ByAI that referenced this pull request Nov 21, 2026
…-mini support

Conflicts: the model's own-kernel KV format check joins next's
kv_format_unsupported_reason (so ft info's tips skip it too); the
offload prefill hook start_prefill_call is skipped on a layer-major chunk
(FlashML-org#686), whose prefill the engine already began and which runs layer 0 once
per chunk; docs keep next's Flint link next to the new Inkling row.

Assisted-by: Claude Opus 5.5
KarrAcaRn pushed a commit to KarrAcaRn/FreeToken-ByAI that referenced this pull request Nov 21, 2026
KarrAcaRn pushed a commit to KarrAcaRn/FreeToken-ByAI that referenced this pull request Nov 21, 2026
Batch 2026-11-21: FlashML-org#942 FlashML-org#940 FlashML-org#918 FlashML-org#917 adopted, FlashML-org#915 deferred; FlashML-org#354
superseded by upstream FlashML-org#894, FlashML-org#408 ported onto its KVFormat framework.

Assisted-by: Claude Opus 5.5
KarrAcaRn pushed a commit to KarrAcaRn/FreeToken-ByAI that referenced this pull request Nov 21, 2026
…efstand

Debug: code-review 1.0, Delta-E 0.9 (1 correction), result-init 1.0;
echt (3 runs, generation_config temperature 1.0): code-review 0.67/1/1,
Delta-E 0.24/0.9/0.79, result-init 1/1/0.67. ~40 tok/s, 82k context with
--memory-ratio 0.85 --kv-reserve-tokens 82000.

New think_format inkling2 (TMLv0 thinking message -> text message) for
the think-stop continuation. run.py finds the FreeToken checkouts next to
its own instead of the old ~/claude/ paths.

Assisted-by: Claude Opus 5.5

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New architecture request: Inkling-2-Mini (thinkingmachines/Inkling-2-Mini, 26.4B-A3.4B looped MoE)

1 participant