- ld::inkling_graph: emit_decode_base() generates the complete base decode forward from the IR over the unrolled physical schedule: embed -> [per-layer: norm -> QKV -> flash -> O-proj -> residual-add -> MLP norm -> router -> experts w13/w2 -> reduce -> residual-add] -> final norm -> unembed. Live-verified on Inkling: 675 ops, 236 arena decls, validated by abi::arena (no overlap / checked bounds / no use-before-write). This is the structurally-complete base; Inkling parity refinements (learned AttnRes block-mix, loop controllers, ShortConv4, relpos, SWA window, paged KV) layer on top next. - abi::vplan: add the Inkling stage opcodes AttnResMix, ShortConv4, LoopCtrl, L2Clip, KvStore, RelPosBias, MoeRouterTopk, VecAdd (30 total, roundtrip). - ld::inkling_ir: LlmComp derives Copy (used by value in the emitter). - docs/inkling-forward-design.md: full per-layer sequence extracted from forward_batch_chunk (k1 AttnRes block-mix + controllers, k2 projections + shortconv + relpos + KV store, k3 flash, O-proj block-boundary rule, k4 sigmoid router + SiTU experts), real scratch buffer set, deep-residual block mechanism (block_size=6 snapshots + learned queries). Verified live: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS; 5 ignored live tests pass (base graph 675 ops); lean proofs (4 files, 42 theorems) green.
5.9 KiB
Inkling forward-pass design (grounded in the strata reference)
Status: design foundation for the graph.vplan generator (M1).
Reference: ../strata-safetensors/engine/src/inkling2/hip/ink_hip_ops.hip.h
(kernel families), ink_hip_backend.hip.cpp (forward_batch_chunk dispatch),
ink_hip_tree.hip.h (MTP beam-tree). Config: ld::inkling::InklingConfig.
Two forward paths
- Main LLM — 24 layers, looped MoE, hybrid SWA + relative attention.
This is the decode critical path and the first
graph.vplantarget. - MTP / DSpark draft — separate
mtp.safetensorsshard, 3 non-causal block-8 layers (standard attention + SwiGLU) + Markov head (rank 256) + beam-tree verification (ink_hip_tree.hip.h). Simpler; lands after the main path.target_layer_ids [5,23,41,48]tap the main model.
Per-layer stage order (verified from kernel stage numbering)
The reference kernels are numbered k1_..k4_ — the numbering IS the
per-layer forward order:
| Stage | Reference kernel(s) | Role | Our opcode |
|---|---|---|---|
| k1 | k1_attn_res_mix_norm_fused |
attention-residual mix + norm | rms_norm (+residual) |
| k2 | k2_shortconv4_* |
short conv (kernel 4) on q/k/v/attn/mlp | (new shortconv4) |
| k2 | k2_qrkv_norm_clip_fused |
q/k/v proj + input-norm + L2 clip | gemm_* + rms_norm |
| k2 | k2_qrkv_kvstore_relpos_fused |
KV store + relative position | rope_kv_write (+rel) |
| k3 | k3_flash_swa_gqa / _splitkv / _prefill_wmma |
SWA flash attention (decode/prefill) | flash_gqa_wmma |
| k3 | k_relpos_bias_proj |
relative-position bias (full-attn layers) | (rel attn variant) |
| k4 | k4_moe_router_topk |
MoE routing top-8 of 128 | moe_route_bucket |
| k4 | expert GEMM + k_scatter_add_expert |
NVFP4 experts w13/w2 + reduce | moe_w13_wmma/moe_w2_wmma/moe_reduce |
| — | k_embed_lookup / k_unembed_softcap |
trunk embed / unembed (softcap 30) | embed_bf16 / gpu_penalties_topk |
Notes:
- SWA layers (20 of 24) use
attn_norm; full-attention layers {5,11,17,23} useq/k/v_input_norm+ relative bias (verified on the live inventory). - Controllers (
gain/forget/down× attn/k/v/mlp) gate the looped passes. - Trunk:
embed → embed_norm → [layers] → norm → unembed(softcap).
Loop structure (from config)
loop_config {core_start 6, core_end 18, num_passes 3, gate_rank 64}: the
core layers [6,18) run 3 passes; middle_cycle_cross_pass_queries /
..._kv_queries carry state across passes; attn_res.attn_queries[0..22] /
mlp_queries[0..23] / output_query are the attention-residual block
queries (n_layers_per_attn_res_block = 6). The graph encodes this as
loop_layer over the core body ×3 with the cross-pass query buffers.
Next brick (executable graph)
Extract the exact operand wiring (buffer offsets, fusion boundaries, the
controller gate application point) from forward_batch_chunk in
ink_hip_backend.hip.cpp, then emit graph.vplan + memory.vplan from
InklingIr following the k1→k2→k3→k4 stage order. The memory.vplan is
validated by abi::arena (proof arena_plan.lean) before any GPU launch.
Open detail to resolve from the reference: precise short-conv history
buffering (snapshot/rollback for speculative) and the relpos bias indexing.
Full per-layer sequence (extracted from forward_batch_chunk, lines 4553–5160)
Real scratch buffers: cur (residual), in0/in1/in2 (q/k/v inputs), mx3,
msgs (cross-pass), q, k_raw/v_raw, k_cv/v_cv (post-sconv), r_raw,
rbias (relpos), sinks, gatea, k_dyn/v_dyn, aout (attn out), oproj,
asc (attn sconv), mlpin, mlpf16, rlog (router logits), tk_i/tk_w
(top-k ids/weights), moeout, msc (mlp sconv), sc_blocks_[] (block
snapshots), sc_splitkv_part_.
Per physical visit ip (logical layer), is_swa = local_layer_ids.contains(ip):
- k1 — AttnRes mix + input norm. For
ip>0: mix the block-snapshot pointersblk_ptrswithcurusing queryattn_res.attn_queries[ip-1]. SWA → fused mix+attn_norm→in0(k/v sharein0); full-attn → mix×3 →mx3→ split intoin0/in1/in2, thenq/k/v_input_norm.ip==0copiescur. Ifhas_msgs(looped core passes): run controllers (controllers.{attn,k,v}: low-rank down→gain/forget up,gate_rank) on the inputs before norm. - k2 — projections.
wq_du→q,wk_dv→k_raw,wv_dv→v_raw,wr_du→r_raw,gate_attn→gatea,attn_sink_proj→sinks (all from the normed inputs). Full-attn alsok/v_sconv.weight_proj→k_dyn/v_dyn. ShortConv4 on k_raw/v_raw (statick_sconv/v_sconv+ dyn) → k_cv/v_cv. Q/R L2-clip (q_l2_clip/rel_l2_clip), KV paged store, relpos bias (rel_logits_proj.proj→ rbias). - k3 — flash attention.
flash_swa_gqa(q, gatea, rbias, sinks, KV pool) → aout. Decode = split-KV 2-stage; prefill = WMMA. SWA window vs global. - O-proj + block boundary.
wo_ud(aout)→oproj; post-attn ShortConv (attn_sconv). AttnRes block rule: ifip % block_size == 0snapshotcurintosc_blocks_[n_blocks](push toblk_ptrs), thencur = asc; elsecur += sconv(oproj). - MoE sublayer. AttnRes mix (
blk_ptrs+cur+mlp_queries[ip]) +mlp_norm→ mlpin (+controllers.mlpif has_msgs). Router:gate.weight(mlpin)→rlog,k4_moe_router_topk(rlog +gate.bias, route_scale)→tk_i/tk_w (top-8/128, sigmoid). Experts: w13 (SiTU activation,situ_beta/situ_lb)→act, w2→y_part, reduce(tk_w)→moeout. NVFP4 path*_sf4mx8. Then mlp ShortConv + residual update.
The attention-residual block mix (k1_attn_res_mix, learned queries over
block snapshots) is Inkling's deep-residual mechanism — the residual stream
is a learned mixture of block outputs, not a plain add. The graph emitter
must reproduce the block-snapshot cadence (block_size=6) and the query
weights. Controllers gate the looped-core passes (has_msgs).