← Back to Pull requests

feat(inkling2): train the released DSpark drafter #4839

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 19, 2026 17:13 UTC
Author

What does this PR do ?

Adds DSpark draft training for Inkling-2-Small on top of the native Inkling 2 model from #4755. The release ships its drafter in mtp.safetensors (84 tensors: 5 dense layers, block of 8, context K/V projections over 8 mixed target features, an rnn_hidden Markov head). This PR builds that drafter in the released layout, captures the target features from the #4755 model, and trains it with train_dspark.py. The consolidated export has the same 84 names and shapes plus a config.json with the mtp_config.

#4751 had a version of this drafter on its own Inkling 2 model; this one is rebuilt on #4755's classes.

Changelog

  • models/inkling2/dspark.py (new): the drafter. Anchor slot plus mask_embed for the 7 drafted slots, per-layer softmax mix of the target features (fuse_weight), context K/V from the mixed features with their own norms, bidirectional attention inside the block, a 1024-token window before the anchor on local layers and the full prefix on layer 2, the gated rnn_hidden Markov head, logits through the frozen target LM head with the release's soft cap, and the state-dict adapter for the released names.

  • models/inkling2/dspark_target.py (new): loads the frozen target through the recipe's EP/FSDP setup. The backend defaults to the one the Inkling 2 fine-tuning recipes use (flex attention, torch_mm experts, HybridEP); target_attn_backend, target_experts and target_dispatcher override it, as they do for the V4.1 target.

  • models/inkling2/model.py: Inkling2DecoderLayer gets a parameter-free partial_input = nn.Identity() that its forward passes state.partial through, and Inkling2ForCausalLM.get_dspark_target_feature_modules(layer_ids) returns the modules whose first input is each feature (the same hook M3 and V4.1 implement). The layers take and return ResidualState, so without this no module sees the partial tensor. No parameters or buffers change, so state dicts, FSDP units and PP splits stay the same. A looped decoder (loop_config, Inkling 2 Mini) is rejected there, since its core layers run once per pass and have no single first input.

  • models/inkling2/config.py: Inkling2Config.build_dspark_draft and build_dspark_target; mtp_config is typed as Inkling2MtpConfig.

  • speculative/dspark/registry.py: ModelOwnedDSparkTargetProvider protocol. recipes/speculative_decoding/train_dspark.py: a config that implements build_dspark_target loads its own target on the recipe's mesh with the target_ backend overrides, cp_size > 1 is rejected for such a target (it does not run the HF SDPA path the CP hook needs), and the text config comes from get_text_config().

  • examples/speculative/dspark/inkling2_small_dspark.yaml (new) and the DSpark guide / Inkling 2 model page. The guide lists the checkpoint's mtp.safetensors as a draft_weights_path source (#4725).

  • Tests: tests/unit_tests/models/inkling2/test_inkling2_dspark.py (forward/backward vs the oracle, released names and shapes, a consolidated save and reload through the real Checkpointer, a warm start from a released-layout mtp.safetensors through load_dspark_draft_weights), test_inkling2_dspark_target.py (feature capture vs the native model and reference.py, backend defaults and overrides, the recipe training a step on a tiny native target, the CP and looped-decoder rejections).
  • Feature ids: the checkpoint labels features by the layer they enter (5, 11, ..., 41, and 48 for the post-final-norm output). DSpark ids count the producing layer, so they are [4, 10, ..., 40, 47], plus -1 for the normalized embedding entering layer 0, which fills the anchor slot. Reading the anchor embedding from the target that way keeps the draft from holding its own copy of the 201k x 4608 embedding and embed_norm.

    Code provenance

    Not applicable.

    Before your PR is "Ready for review"

    Pre checks:

  • Make sure you read and followed Contributor guidelines

  • Did you write any new necessary tests?

  • Did you add or update any necessary documentation?
  • Additional Information

    Draft vs an FP64 per-block oracle written from the TokenSpeed and vLLM serving math for this drafter (CPU, FP32, TF32 off, tiny config: 3 layers, window 3, 4 slots):

    | tensor | max abs err | max rel err |
    |---|---|---|
    | draft logits | 4.6e-7 | 2.9e-7 |
    | parameter grads (all) | <= 3.0e-6 | <= 1.2e-6 |
    | grad of the target features (incl. anchor embedding) | 3.2e-7 | 3.5e-7 |

    Swapping in a per-row window instead of the block-anchored one gives 0.23 relative error on the logits, so the test tells the two window rules apart.

    Target features on a 7-layer tiny Inkling2ForCausalLM and Inkling2ForConditionalGeneration (ids [-1, 1, 4, 5, 6], covering a global layer and a block boundary): bitwise equal to the native model's layer outputs, and within 4.8e-7 of tests/unit_tests/models/inkling2/reference.py.

    Released weights: the 84 tensors of mtp.safetensors load with no missing or unexpected keys (only the target's lm_head.weight is shared) and are bitwise equal after loading; a BF16 forward gives finite logits. With draft_weights_path pointing at the real file, load_dspark_draft_weights fills all 84 tensors of a fresh FP32 draft with the file's values and leaves the target's LM head as it was.

    Recipe smoke on 2x H100 (EP2 + FSDP2, AC on): train_dspark.py with this YAML on a tiny random Inkling 2 checkpoint in the HF layout that keeps the released 48-layer structure (hidden 256, 32 experts top-8), so the YAML's feature ids stay valid; 2-layer draft, seq 512, 64 anchors, lr 3e-4 instead of the YAML's 1e-5 so the loss moves.

  • The target loads through build_dspark_target with flex attention in all 48 layers and GroupedExpertsDeepEP on HybridEP, each rank holding 16 of the 32 experts. 11 spot-checked tensors (embedding, norms, router, two EP-sharded expert weights) match the written checkpoint by checksum.

  • 8 steps with finite, decreasing loss (1.553 to 1.455), then validation and the final save. The consolidated export has exactly the draft adapter's names, no LM head, and a config.json with mtp_config.

  • About 0.45 s/step after the first (step 1 includes the HybridEP JIT and flex compile), 1.9 GiB peak per GPU.
  • Tests, CPU (x86 test container), on b28c42eea (merged with current main 4ac010479): tests/unit_tests/models/inkling2, tests/unit_tests/recipes/speculative_decoding, all 15 tests/unit_tests/speculative/test_dspark_.py files and the two MiniMax M3 DSpark tests: 1016 passed, 4 skipped. ruff 0.12.12 check and format on the changed files, tools/lint_no_globals.py and tools/recipes/check_recipes.py pass; the source validation in tools/docs/check_docs.py passes (its local docs-stitch step needs the versioned docs tree, which I don't have here).

    Not tested: the real-size target and its NVFP4/MXFP8 dequantizing load (the feature mapping on the real checkpoint relies on #4755 matching the HF/release layer semantics, checked on the tiny model), EP above 2 and multi-node, the GPU feature values against reference.py, padded batches, a training run started from the released draft, resume, offline cached targets and packing (the draft does not support packing; the recipe rejects it).

    Two things I'd like your take on: the nn.Identity capture point in Inkling2DecoderLayer (the other option is a shared hook in HFDSparkTargetModel that reads ResidualState.partial from layer outputs), and reading the anchor embedding as feature -1, which makes validate_target_layer_ids log its SGLang warning for -1 and 47.

    Implementation and validation were AI-assisted.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: ischencheng Review duration: 15938m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #4839"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.7% · Safe to merge
    27.7%