Last updated: 2026-11-17
Inference Runtime
Forward Cuda
Shared ROCm/CUDA Qwen forward pass for dense, MoE and hybrid-SSM models.
Orchestrates the native device/buffer/pipeline/command modules through their shared C ABI: AMD HIP kernels on ROCm or NVIDIA NVRTC kernels on CUDA. Weights retain their GGUF quantization (Q4_K/Q5_K/Q6_K/Q8_0 blocks) during upload via loader_cuda and are dequantized inside the matrix-vector kernels.
Layer schedule (qwen35, full_attention_interval=4): layer L is full attention when ((L+1) % 4 == 0) → L in {3,7,11,15,19,23,27,31}; the other 24 layers are gated-delta-net SSM. Every layer is followed by a SwiGLU FFN block.
3 exports shown
struct
MtpCycleResult
pub const MtpCycleResult = struct No public doc comment yet.
struct
MtpPerf
pub const MtpPerf = struct No public doc comment yet.
struct
ForwardCuda
pub const ForwardCuda = struct Per-token GPU forward state for qwen35 greedy decode.
Methods
34method
ForwardCuda.readMtpProcessPolicy
pub fn readMtpProcessPolicy() MtpProcessPolicy No public doc comment yet.
method
ForwardCuda.init
pub fn init(allocator: std.mem.Allocator, model: *loader.Model, max_ctx: u32) !ForwardCuda Compile kernels, allocate every device buffer, upload inv_freq + sinks, zero the KV cache and SSM state.
method
ForwardCuda.deinit
pub fn deinit(self: *ForwardCuda) void No public doc comment yet.
method
ForwardCuda.canReuseSingleSlotState
pub fn canReuseSingleSlotState(n_slots: u32, slot_ctx: u32, max_ctx: u32) bool A single slot shares the already allocated target KV/SSM cache when its capacity fits (including Q8 KV).
Multiple slots need independent regions.
method
ForwardCuda.allocSlotState
pub fn allocSlotState(self: *ForwardCuda, n_slots: u32, slot_ctx: u32) !void Allocate per-sequence slot state for `n_slots` concurrent sequences, each with up to `slot_ctx` positions.
Attn layers get slot KV; ssm layers get per-slot conv + recurrent state (zero-initialised). A single slot aliases the existing state instead of allocating a second full KV cache.
method
ForwardCuda.freeSlotState
pub fn freeSlotState(self: *ForwardCuda) void No public doc comment yet.
method
ForwardCuda.slotKvOffsetBytes
pub fn slotKvOffsetBytes(self: *const ForwardCuda, slot: u32, pos: u32) usize Byte offset of slot `slot`'s K/V for position `pos` in attn layer `L`'s slot KV row offset using the active storage format, including Q8 scales.
method
ForwardCuda.slotConvOffsetBytes
pub fn slotConvOffsetBytes(self: *const ForwardCuda, slot: u32) usize Byte offset of slot `slot`'s conv ring (ssm layer): slot*conv_state_len*f4.
method
ForwardCuda.slotStateOffsetBytes
pub fn slotStateOffsetBytes(self: *const ForwardCuda, slot: u32) usize Byte offset of slot `slot`'s recurrent state (ssm layer): slot*ssm_state_len*f4.
method
ForwardCuda.resetSlot
pub fn resetSlot(self: *ForwardCuda, slot: u32) !void Effort 28 increment 4 (qwen serving): zero a REUSED slot's SSM conv ring + recurrent state so a new request prefilling from pos=0 into this slot starts from clean state.
The serving engine reuses slots across requests (nslots < concurrent clients) and qwen's SSM state is ACCUMULATED (not position-indexed), so without this a reused slot inherits the prior request's recurrent state → corruption. The attention slot KV needs NO reset (it is position-indexed and overwritten on prefill; reads never reach beyond the current sequence's positions — same reason gemma's slot KV reuses cleanly). No-op if slot state is unallocated. Call on ADMIT, before per-token prefill.
method
ForwardCuda.slotStateSmoke
pub fn slotStateSmoke(self: *ForwardCuda) !bool Sub-step 4a plumbing smoke: prove the slot-offset arithmetic round-trips and that distinct slots map to NON-overlapping device regions for BOTH the attention KV and the SSM conv + recurrent state.
Requires allocSlotState with n_slots >= 2 (so slot 0 and slot n_slots-1 differ). Returns true on success.
method
ForwardCuda.resetState
pub fn resetState(self: *ForwardCuda) !void Reset the production single-sequence recurrent state to a fresh-process start: zero every ssm layer's conv ring + recurrent state and clear the per-layer conv offset.
The attention KV cache needs no reset — it is position-indexed and each sequence overwrites from pos 0, reading only what it wrote. Used by the batch harness to make the per-sequence SERIAL reference truly single-sequence (without this, the unindexed SSM recurrent state leaks from one reference sequence into the next). Additive — never called on the real decode/prefill path; that path runs one sequence/process.
method
ForwardCuda.checkExecution
pub fn checkExecution(self: *const ForwardCuda) !void No public doc comment yet.
method
ForwardCuda.decodeStep
pub fn decodeStep(self: *ForwardCuda, token: u32, pos: u32, run_layers: bool) !u32 Run one greedy decode step for `token` at sequence position `pos`, returning the argmax token id.
v0: embed → final rms_norm → LM head → argmax (layers are gated by `run_layers`).
method
ForwardCuda.prefillStep
pub fn prefillStep(self: *ForwardCuda, token: u32, pos: u32) !void Prefill helper: run every layer (build the KV cache) but SKIP the tail rms_norm + LM head + argmax.
A prompt-internal token's logits are never used — only the final prompt token's argmax seeds generation — so the (vocab x n_embd) head matvec is pure waste on T-1 of the T prompt tokens. It is a large share for the MoE models, whose active per-token forward is small next to the full-vocab head. The async layer ops are drained here (waitPending) so the ring is freed each token; bit-identical generation.
method
ForwardCuda.prefillBatched
pub fn prefillBatched(self: *ForwardCuda, tokens: []const u32) !u32 No public doc comment yet.
method
ForwardCuda.mtpUnavailableReason
pub fn mtpUnavailableReason(self: *const ForwardCuda) ?[]const u8 Model compatibility is separate from the default policy, allowing a CUDA server request to opt in while an unset CUDA default stays off.
An explicit ZINC_MTP=0/off/false/no remains an operator kill switch.
method
ForwardCuda.mtpModelUnavailableReason
pub fn mtpModelUnavailableReason(self: *const ForwardCuda) ?[]const u8 Inspect NextN compatibility without conflating the process default or operator policy with the weights supplied by the model.
method
ForwardCuda.mtpSupported
pub fn mtpSupported(self: *const ForwardCuda) bool No public doc comment yet.
method
ForwardCuda.mtpDefaultEnabled
pub fn mtpDefaultEnabled(self: *const ForwardCuda) bool No public doc comment yet.
method
ForwardCuda.setMtpExecutionOverride
pub fn setMtpExecutionOverride(self: *ForwardCuda, enabled: ?bool) ?bool Called only before starting the GPU worker, or by that worker while it owns the single sequence.
Restore the returned value at the end of scope.
method
ForwardCuda.mtpEnabled
pub fn mtpEnabled(self: *const ForwardCuda) bool No public doc comment yet.
method
ForwardCuda.mtpPrepare
pub fn mtpPrepare(self: *ForwardCuda) !bool Allocate the speculative context before prompt timing begins.
This is model/runtime setup (equivalent to creating the reference draft context), not prompt processing, and can involve hundreds of MiB of rollback rows.
method
ForwardCuda.mtpPrime
pub fn mtpPrime(self: *ForwardCuda, prompt_tokens: []const u32) !bool Prime the draft head from the target prompt hidden rows left by prefillBatched.
Initialization is charged to prefill, matching the reference runtime.
method
ForwardCuda.mtpPerf
pub fn mtpPerf(self: *const ForwardCuda) MtpPerf No public doc comment yet.
method
ForwardCuda.mtpCycle
pub fn mtpCycle(self: *ForwardCuda, seed: u32, pos: u32, max_drafts: u32, eos_id: u32) !MtpCycleResult Draft up to three tokens, verify them in one real-target batch, restore the recurrent state at the accepted boundary, then repair the NextN KV.
The caller commits `seed` plus the returned accepted draft prefix only.
method
ForwardCuda.useCliMoeSlotPrefill
pub fn useCliMoeSlotPrefill(self: *const ForwardCuda, tokens: usize) bool No public doc comment yet.
method
ForwardCuda.prefillBatchedSlot
pub fn prefillBatchedSlot(self: *ForwardCuda, tokens: []const u32, slot: u32) !u32 Batched prefill that writes KV cache + SSM state into the given slot's per-sequence region (instead of the single-seq scratch).
Used by the serve engine so server-mode prefill gets the 5× batched-GEMM speedup instead of falling back to per-token decodeBatch (B=1).
method
ForwardCuda.decodeBatch
pub fn decodeBatch(self: *ForwardCuda, tokens: []const u32, positions: []const u32, slots: []const u32, out: []u32) !void No public doc comment yet.
method
ForwardCuda.attentionLayerPub
pub fn attentionLayerPub(self: *ForwardCuda, L: u32, pos: u32) !void No public doc comment yet.
method
ForwardCuda.ssmLayerPub
pub fn ssmLayerPub(self: *ForwardCuda, L: u32) !void No public doc comment yet.
method
ForwardCuda.ffnBlockPub
pub fn ffnBlockPub(self: *ForwardCuda, L: u32) !void No public doc comment yet.
method
ForwardCuda.readLogits
pub fn readLogits(self: *ForwardCuda, out: []f32) void Download the top of the logits buffer (for top-k reporting).