Skip to content

feat(inkling2): GPU batched prefill, quantized KV, chat/serve parity, inkling2v vision, and DSpark/N-gram speculative decoding - #65

Merged
Vasyl-Trefilov merged 1 commit into
RustingStudio:mainfrom
ybenyedder:feat/inkling2-complete
Nov 22, 2026
Merged

Vasyl-Trefilov merged 1 commit into
RustingStudio:mainfrom
ybenyedder:feat/inkling2-complete

Conversation

@ybenyedder

Copy link
Copy Markdown
Contributor

Closes #62

Summary

Completes and optimizes inkling2_loop (Inkling 2 Mini 21B-A1.5B MoE) across decode, batched GPU prefill, quantized KV cache, CLI/chat/serve parity, multimodal inkling2v vision, and speculative decoding (--draft-model DSpark + --spec-type context-lookup drafters).

1. Decode & Zero-Round-Trip GPU MoE Optimizations

  • situ_q (src/vulkan/shaders/situ_q.comp, src/cuda/kernels.cu): Fuses Inkling 2's bounded SITU activation directly with Q8_1 (int8 + per-32 scale) quantization so the down-projection moe_gemv skips a separate quant_xq pass.
  • softcap (src/vulkan/shaders/softcap.comp, src/cuda/kernels.cu) & Device::step_argmax: Keeps the 200,064-vocab final logit softcap (y * tanh(x / y)) and greedy argmax on the GPU, transferring 4 bytes instead of 800 KB per greedy step.
  • Zero-Round-Trip GPU Router (ink_route.comp / ink_route CUDA) + Priority FullExps Slab Preload: Top-8 sigmoid + e_score_correction_bias + normalized exp_scale routing on the GPU; prioritizes the 12 core layers (each visited 3× per token across loop_count = 3, i.e. 36/48 = 75% of all slot runs) before prelude/coda layers when preloading FullExps slabs, and fixes non-FullExps coda layers to deterministic GPU execution when Device is active.
  • Dev::upload Deduplication (src/deepseek/mat.rs): Deduplicates dense GPU tensor uploads by (data.as_ptr(), data.len()).

2. Batched GPU Prefill, Dynamic --ctx, and Quantized KV (--kv f16|q8|q4|q2)

  • Batched GPU Prefill (Device::prefill_chunk_seq / prefill_layer): Runs prompt chunks on the GPU (ink_mix_n, ink_ctrl_n, ink_conv_batch, ink_clip_n, ink_attn_prefill with staging of new SWA K/V rows into kc_tmp/vc_tmp so ring wrap never overwrites live history < pos0, and batched GPU MoE).
  • Dynamic Context (--ctx up to cfg.ctx = 262,144): Replaces the hardcoded 4,096-token limit with dynamic KV capacity growth up to --ctx (capped at cfg.ctx).
  • Quantized KV Cache (--kv f16|q8|q4|q2): Updates ink_clip.comp, ink_attn.comp, ink_attn_prefill.comp, and CUDA ink_clip / ink_attn / ink_attn_prefill to use kv_put / ldkv with be.kv_words(kw) row sizing across all 4 KV modes.

3. Full chat / serve / CLI Parity & Checkpoint Prefix Reuse

  • Inkling2Opts (src/inkling2/chat.rs, src/main.rs): Wires Sampler (--temp, --top-k, --top-p, --seed, --repeat-penalty, --penalty-last-n, --presence-penalty, --frequency-penalty, --dry-*), --ctx, --think, --thinking-budget, and --allowed-hosts into inkling2::chat::{generate_image_opts, chat_image_opts, serve_opts}.
  • Thinking Effort & Budget: Maps OpenAI reasoning_effort (none|minimal|low|medium|high|xhigh|max or 0..=0.99) and think boolean to Thinking effort level: <val>, and enforces thinking_budget by closing <|content_thinking|> with <|end_message|><|message_model|><|content_text|> on the GPU before sampling the final answer.
  • Stop Strings, SSE include_usage, /v1/messages, and Multi-Turn Checkpoint Prefix Reuse: Holds back partial stop strings via Hold, emits usage (prompt_tokens_details.cached_tokens) on final SSE chunks when stream_options.include_usage is set, supports Anthropic /v1/messages, and snapshots (Checkpoint, logits) right after prefill so subsequent turns where the client omits prior reasoning_content restore the exact KV + conv + SWA prefix state in O(1) without re-prefilling history.

4. Multimodal Vision (inkling2v in src/vision/ & src/inkling2/)

  • ProjectorKind::Inkling2V (src/vision/mmproj.rs, src/vision/image.rs, src/vision/projector.rs): Implements inkling2v ViT + 3×3 PatchMerger (post_norm -> 3×3 grouped patch gather -> input_norm -> gated SwiGLU gate_proj/up_proj/down_proj), 2D learned position embeddings, and aspect-preserving bilinear resize + bottom/right zero-padding without upscaling small images (matching llama.cpp mtmd_graph_inkling2v & mtmd_image_preprocessor_inkling2v).
  • Raw Vision Embedding Prefill (Inkling2Model::prefill_embds): Expands <|content_image|><|unused_200054|><|end_message|> placeholders into (nx / 3) * (ny / 3) vision embeddings and skips tok_norm on vision tokens (matching inkling2.cpp::build_inputs()).

5. Speculative Decoding (Device::verify_forward + commit_verify + --spec-type + DSpark --draft-model)

  • Exact Batched GPU Verify & Rollback (Device::verify_forward, Device::commit_verify): Verifies [anchor, drafts..] (1 <= nt <= 16) in one batched pass on the GPU with exact Device::step numerics (rms_norm_q + gemvq, ink_attn, and router-rank-ordered FFN) and rolls back rejected draft tokens' SWA ring rows and 4 causal conv histories in place on the GPU.
  • Inkling 2 DSpark Companion Drafter (src/inkling2/dspark.rs, --draft-model): Implements the 3-layer DSpark speculative draft model (dspark-Inkling-2-Mini-Q8_0.gguf, matching llama.cpp PR #32218): target feature extraction at visits [5, 23, 41, 48], softmax dspark_fuse.weight mixing, dflare_norm, attn_k_ctx/attn_v_ctx KV cache injection with attn_k_ctx_norm and NEOX RoPE (theta = 1e6), 8-token non-causal block attention (anchor + 7 mask_embd), and chained Markov RNN head (markov_w1 + joint_proj + dspark_hidden_out) with target GPU lm_head_argmax.
  • Context-Lookup Drafters (--spec-type): Integrates NgramDrafter (ngram-simple, ngram-map-k, ngram-map-k4v, ngram-mod, ngram-cache, suffix) into Inkling2Model.

Benchmarks & Verification (AMD Radeon 890M gfx1150, Vulkan RADV, Inkling-2-Mini-IQ4_NL.gguf)

Benchmark (--gpu --backend vulkan) Baseline (10db14c) This PR Speedup
pp 64 (prefill tok/s) 36.9 tok/s 53.5 tok/s +45.0%
pp 512 (prefill tok/s) 95.8 tok/s 135.3 tok/s +41.2%
pp 2048 (prefill tok/s) 108.0 tok/s 124.7 tok/s +15.5%
tg 64 @ pp 64 (decode tok/s) 7.4 tok/s 9.0 tok/s +21.6%
tg 64 @ pp 512 (decode tok/s) 7.6 tok/s 8.9 tok/s +17.1%
tg 64 @ pp 2048 (decode tok/s) 7.5 tok/s 8.6 tok/s +14.7%
DSpark speculative acceptance (--draft-model dspark-Inkling-2-Mini-Q8_0.gguf) N/A 40/70 (57.1%) 100% greedy SHA-256 parity

Test Suite

  • cargo test --release --lib --locked: 96/96 passed (0 warnings)
  • cargo test --release --test backend --locked: 10/10 passed (spirv_is_fresh ok)
  • cargo test --release --test kernels --locked: 35/35 passed (situ_q_matches_situ_and_quant_xq, softcap_matches_cpu, ink_route_matches_cpu, ink_conv_batch_matches_sequential, ink_attn_prefill_matches_single_step, ink_attn_quantized_kv_modes_match_f16 ok)
  • cargo test --release --test vision --locked: 14/14 passed (inkling2v_encode_and_pad, inkling2v_red_image_predicts_red ok)
  • STRATA_INKLING2_MODEL=.../Inkling-2-Mini-IQ4_NL.gguf STRATA_DS_GPU=1 cargo test --release --test inkling2_forward --locked: 4/4 passed (short_prompt_follows_llama_cpp, long_prompt_follows_llama_cpp, chunked_prefill_matches_token_by_token, speculative_ngram_and_dspark_match_greedy ok)
  • Note: CUDA kernels in src/cuda/kernels.cu and src/cuda/mod.rs are updated in lockstep with Vulkan/CPU and not compiled on this AMD host.

@aletheore

aletheore Bot commented Nov 22, 2026

Copy link
Copy Markdown

Aletheore Flash review

No issues found in this diff.

Note: 11 of 40 changed file(s) were not included in this review (the review reads at most 30 files, and skips any file over 100KB). Issues in them would not be found, and citations pointing into them could not be checked against the real file: src/vulkan/spv/ink_attn.spv, src/vulkan/spv/ink_attn_prefill.spv, src/vulkan/spv/ink_clip.spv, src/vulkan/spv/ink_conv_batch.spv, src/vulkan/spv/ink_ctrl_gate.spv, src/vulkan/spv/ink_ctrl_out.spv, src/vulkan/spv/ink_ffn_sum.spv, src/vulkan/spv/ink_mix.spv, src/vulkan/spv/ink_route.spv, src/vulkan/spv/situ_q.spv….

@aletheore

aletheore Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

🔍 Aletheore evidence diff

graph LR
    n0["src/backend.rs"]
    n1["src/cuda/mod.rs"]
    n2["mat.rs"]
    n3["src/inkling2/mod.rs"]
    n4["src/serve.rs"]
    n5["image.rs"]
    n6["mmproj.rs"]
    n7["projector.rs"]
    n8["src/vulkan/mod.rs"]
    n9["src/chat.rs"]
    n10["cpu_worker.rs"]
    n11["src/deepseek/chat.rs"]
    n12["src/deepseek/forward.rs"]
    n13["src/deepseek/mod.rs"]
    n14["expert_cache.rs"]
    n15["src/gemma4/forward.rs"]
    n16["src/gemma4/mod.rs"]
    n17["src/glm/forward.rs"]
    n18["src/glm/mod.rs"]
    n19["src/gpu_model.rs"]
    n20["prefill.rs"]
    n21["hf.rs"]
    n22["src/hy4/chat.rs"]
    n23["src/hy4/forward.rs"]
    n24["lib.rs"]
    n25["model.rs"]
    n26["src/vision/mod.rs"]
    n27["preflight.rs"]
    n28["tests/backend.rs"]
    n29["tests/common/mod.rs"]
    n30["tests/gpu_model.rs"]
    n31["kv_q8.rs"]
    n32["llama_numerics.rs"]
    n33["tests/serve.rs"]
    n0 --> n10
    n0 --> n14
    n0 --> n19
    n0 --> n20
    n0 --> n24
    n0 --> n25
    n0 --> n28
    n0 --> n29
    n0 --> n30
    n0 --> n31
    n0 --> n32
    n1 --> n24
    n1 --> n27
    n1 --> n29
    n1 --> n30
    n1 --> n31
    n1 --> n32
    n2 --> n12
    n2 --> n13
    n2 --> n15
    n2 --> n17
    n2 --> n23
    n3 --> n24
    n4 --> n9
    n4 --> n11
    n4 --> n16
    n4 --> n18
    n4 --> n21
    n4 --> n22
    n4 --> n24
    n4 --> n33
    n5 --> n16
    n5 --> n26
    n6 --> n26
    n6 --> n29
    n7 --> n16
    n7 --> n26
    n8 --> n24
    n8 --> n27
    n8 --> n28
    n8 --> n29
    n8 --> n30
    n8 --> n31
    n8 --> n32
    classDef changed fill:#d9772c,color:#fff,stroke:#b85f1c;
    classDef changedFinding fill:#c0392b,color:#fff,stroke:#922b21;
    classDef dependent fill:#f2f0ec,color:#18140f,stroke:#d6d0c5;
    class n0 changed
    class n1 changed
    class n2 changed
    class n3 changed
    class n4 changed
    class n5 changed
    class n6 changed
    class n7 changed
    class n8 changed
    class n9 dependent
    class n10 dependent
    class n11 dependent
    class n12 dependent
    class n13 dependent
    class n14 dependent
    class n15 dependent
    class n16 dependent
    class n17 dependent
    class n18 dependent
    class n19 dependent
    class n20 dependent
    class n21 dependent
    class n22 dependent
    class n23 dependent
    class n24 dependent
    class n25 dependent
    class n26 dependent
    class n27 dependent
    class n28 dependent
    class n29 dependent
    class n30 dependent
    class n31 dependent
    class n32 dependent
    class n33 dependent
Loading

What changed
Deterministic, computed from this commit's real scan and import graph - posted on every run, whether or not Flash Review found anything.

  • src/backend.rs · modified · +62/-8 · +9 functions · 11 other dependents
  • src/cuda/kernels.cu · modified · +340/-98
  • src/cuda/mod.rs · modified · +64/-15 · +9 functions, -4 functions · 6 other dependents
  • src/deepseek/mat.rs · modified · +68/-5 · +5 functions, +1 class · 5 other dependents
  • src/inkling2/chat.rs · modified · +539/-66 · +16 functions, -1 function, +1 class
  • src/inkling2/dspark.rs · +409/-0 · new file, 10 symbols
  • src/inkling2/forward.rs · modified · +422/-23 · +17 functions, +1 class
  • src/inkling2/forward/device.rs · modified · +1153/-139 · +18 functions, +3 classes
  • src/inkling2/mod.rs · modified · +4/-1 · 1 other dependent
  • src/main.rs · modified · +126/-7 · +1 function
  • src/serve.rs · modified · +19/-1 · 8 other dependents
  • src/vision/image.rs · modified · +39/-0 · +1 function · 2 other dependents
  • src/vision/mmproj.rs · modified · +18/-5 · 2 other dependents
  • src/vision/projector.rs · modified · +253/-5 · +1 function · 2 other dependents
  • src/vulkan/mod.rs · modified · +53/-9 · +9 functions, -4 functions · 7 other dependents
  • src/vulkan/shaders/ink_attn.comp · modified · +3/-7
  • src/vulkan/shaders/ink_attn_prefill.comp · added · +61/-0
  • src/vulkan/shaders/ink_clip.comp · modified · +20/-7
  • src/vulkan/shaders/ink_conv_batch.comp · added · +36/-0
  • src/vulkan/shaders/ink_ctrl_gate.comp · modified · +8/-6
  • +20 more changed file(s)

Aggregate deltas

  • Modules: +1
  • Dependency graph edges: +24
  • Commits: 1

@ybenyedder
ybenyedder force-pushed the feat/inkling2-complete branch from edaf155 to b4b1a69 Compare November 22, 2026 07:39
…kling2v vision, and DSpark/ngram speculative decoding
@ybenyedder
ybenyedder force-pushed the feat/inkling2-complete branch from b4b1a69 to 4246396 Compare November 22, 2026 07:47
@Vasyl-Trefilov
Vasyl-Trefilov merged commit 1ea5c14 into RustingStudio:main Nov 22, 2026
6 checks passed
@Vasyl-Trefilov

Vasyl-Trefilov commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

@ybenyedder Merged, ty, this is a lot of work. It is also the answer to #66. I ran it on an RTX 3060 12 GB (CUDA and Vulkan) and fixed these on top, in the merge commit:

  • CUDA: INFINITY was used in kernels.cu before its #define, so the source did not compile.
  • CUDA: gemvq_pair takes 1 to 4 rows, and the verify pass sends up to 16. Every draft batch of 5 or more rows failed with "GPU verify failed". It is now sent in launches of 4 rows.
  • After that error the decode moved to the host, and the host fallback ran every candidate through the full step, also the rejected ones, so the state kept rejected drafts and the text went wrong. The host fallback now checks only the first token, and no drafts are proposed without a device.
  • share() returned a CPU share of 0 whenever a device is open. On a card that does not hold the model this cut plain decode from 28.3 to 17-19 tok/s (IQ4_NL, 17.6 GB file, 12 GB card). The line is removed, and decode is 29.9 tok/s.
  • tests/inkling2_forward.rs fell back to a DSpark path on your machine, it reads STRATA_INKLING2_DSPARK only now. The draft test compares with the token-by-token decode up to the first near tie, because the two paths round differently on the GPU.
  • The "flags are ignored" warning for Inkling 2 now names only --mtp and --mtp-model.

Measured after the fixes, same card, bench --pp 512 --tg 128, before the merge / after:

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.

[Idea]: Inkling-2-Mini support

2 participants