Module deepseek_v41_layer

Module deepseek_v41_layer 

Source
Expand description

2026-09-25: DeepSeek-V4.1 Flash: one transformer block on the generic model.

The layer runs on the f32 mHC highway the generic model provides (ctx.buffers.hc_streams(), [T, hc, H]), in the order of the CPU reference deepseek_v41_ref::model::forward:

layer 0 only:        hc_expand(embedding -> streams)
engram layers only:  streams += engram(token n-grams)          (EngramV41)
attention:           (pre_a, post_a, comb_a) = hc_mixes(streams, attn site)
                     x = rmsnorm(collapse(streams, pre_prev))   <- delayed pre
                     streams = hc_post(attn(x), streams, post_a, comb_a)
ffn:                 (pre_f, post_f, comb_f) = hc_mixes(streams, ffn site)
                     x = rmsnorm(collapse(streams, pre_a))
                     streams = hc_post(moe(x), streams, post_f, comb_f)
                     pre_prev = pre_f
last layer only:     hidden = collapse(streams, pre_prev)

pre_prev is set by layer 0 to the one-hot on stream 0 (the reference’s initial pre-mix) and lives in V41Runtime, with the shared attention slots, the engram hasher and the expert cache: attention state is shared across layers, so one runtime is shared by every layer through an Arc, its mutable parts behind mutexes. One sequence at a time: multi-sequence decode and the model-level CUDA graph are declined through the layer hooks. The layer can capture its own single-token step instead (METRALE_DS41_GRAPH=1, see GraphMode, or the multi-layer segments of METRALE_DS41_STEP_GRAPH=1): the host work in the middle of every layer (the routing download and the expert fetch; on kv and index source layers also the compressor group and the index top-k) splits the step into graph segments with host spans between them.

Routed experts are read on demand into an expert cache (ExpertLru over the ExpertArena slots, ExpertSliceMap locating each expert in the GGUF shards) with positional file reads. The arena is device memory behind a page-locked staging ring, or with METRALE_DS41_ARENA_DEVICE=0 a page-locked arena the GPU reads in place. The engram tables are read by row (EngramRowReader).

Owner: model-arch, DeepSeek-V4.1. Invariants:

  • Layer 0’s step at position 0 resets the shared attention slots, the engram hasher and the cached step hashes after its argument checks and before any engram, attention or MoE work.

Structs§

DeepSeekV41Layer
SegState
2026-09-25: The segment-graph state shared by every layer.
V41LayerState
V41Runtime
2026-09-25: Everything the layers share for the one sequence in flight.

Enums§

GraphMode
2026-09-25: How the single-token step runs, read once. METRALE_DS41_GRAPH_ORACLE=1 runs every layer both ways and logs whether the highway, the delayed mix and the hidden match bit for bit; METRALE_DS41_GRAPH=1 replays the captured segments; anything else is the eager step.

Functions§

graph_mode
step_graph_on
2026-09-25: METRALE_DS41_STEP_GRAPH=1, read once.