Last updated: 2026-11-17

API Server

Runtime

All API Sections

Backend-specific server runtime aliases and wrappers.

Keeps the HTTP/routes layer shared across Vulkan and Metal backends.

38 exports 2 methods src/server/runtime.zig

38 exports shown

constant

is_metal

#
pub const is_metal = gpu.is_metal

Whether the active GPU backend is Apple Metal.

src/server/runtime.zig:8

constant

is_vulkan

#
pub const is_vulkan = gpu.is_vulkan

Whether the active GPU backend is Vulkan.

src/server/runtime.zig:10

constant

supports_model_management

#
pub const supports_model_management = gpu.is_vulkan or gpu.is_metal

Whether the backend supports loading/unloading models at runtime.

src/server/runtime.zig:12

constant

supports_sampling_controls

#
pub const supports_sampling_controls = gpu.is_vulkan or gpu.is_metal

Whether the backend supports temperature, top-p, top-k, and repetition penalty.

src/server/runtime.zig:14

constant

supports_runtime_profiling

#
pub const supports_runtime_profiling = gpu.is_vulkan or gpu.is_metal

Whether the backend supports GPU kernel profiling during inference.

src/server/runtime.zig:16

constant

mtp_unavailable_reason

#
pub const mtp_unavailable_reason = "Per-request MTP control is unavailable on this backend; use the server's ZINC_MTP policy."

Per-request MTP overrides are currently implemented by the native serving worker.

Vulkan retains its existing model/driver/environment policy; do not equate the absence of this request control with an absent Vulkan verifier.

src/server/runtime.zig:20

function

mtpRequestError

#
pub fn mtpRequestError(enabled: ?bool) ?[]const u8

No public doc comment yet.

src/server/runtime.zig:22

constant

tokenizer_mod

#
pub const tokenizer_mod = @import("../model/tokenizer.zig")

Tokenizer module (shared across all backends).

src/server/runtime.zig:30

constant

forward_mod

#
pub const forward_mod = if (gpu.is_metal) @import("../compute/forward_metal.zig") else @import("../compute/forward.zig")

Forward-pass module, selected by the active backend.

src/server/runtime.zig:32

constant

loader_mod

#
pub const loader_mod = if (gpu.is_metal) @import("../model/loader_metal.zig") else @import("../model/loader.zig")

Model-loading module, selected by the active backend.

src/server/runtime.zig:34

constant

model_manager_mod

#
pub const model_manager_mod = if (gpu.is_metal) @import("model_manager_metal.zig") else @import("model_manager.zig")

Model-manager module, selected by the active backend.

src/server/runtime.zig:36

constant

InferenceEngine

#
pub const InferenceEngine = forward_mod.InferenceEngine

Backend-specific inference engine that runs the forward pass.

src/server/runtime.zig:39

constant

DecodeState

#
pub const DecodeState = forward_mod.DecodeState

Per-sequence decode state (KV cache position, token history, etc.).

src/server/runtime.zig:41

constant

RequestControl

#
pub const RequestControl = @import("../gpu/request_control.zig").RequestControl

No public doc comment yet.

src/server/runtime.zig:42

function

setPrefillControl

#
pub fn setPrefillControl(engine: *InferenceEngine, control: ?RequestControl) void

Vulkan checks client liveness between prefill tokens/submissions.

The Metal preview retains its existing decode-time cancellation behavior.

src/server/runtime.zig:46

constant

Model

#
pub const Model = loader_mod.Model

Loaded model handle (weights, hyperparams, GGUF metadata).

src/server/runtime.zig:54

constant

ModelManager

#
pub const ModelManager = model_manager_mod.ModelManager

Manages loading, unloading, and switching between models at runtime.

src/server/runtime.zig:56

constant

SamplingParams

#
pub const SamplingParams = forward_mod.SamplingParams

Token sampling parameters (shared across Vulkan and Metal backends).

src/server/runtime.zig:59

function

enableLogitsReadback

#
pub fn enableLogitsReadback(_engine: *InferenceEngine) void

Enable logits readback from GPU so sampling can inspect raw logits.

On Metal (UMA) logits are always CPU-accessible, so this is a no-op.

Parameters

_engine
Inference engine whose readback mode is updated.

src/server/runtime.zig:64

function

logitsReadbackEnabled

#
pub fn logitsReadbackEnabled(_engine: *const InferenceEngine) bool

Return whether logits readback is currently enabled on the engine.

Always returns `true` on Metal because UMA makes logits CPU-accessible without an explicit readback step.

Parameters

_engine
Inference engine to query.

Returns

`true` if the engine will expose logits after each decode step.

src/server/runtime.zig:76

function

setLogitsReadbackEnabled

#
pub fn setLogitsReadbackEnabled(_engine: *InferenceEngine, _enabled: bool) void

Set the logits readback intent flag on backends that can elide full logit materialization.

src/server/runtime.zig:86

function

enableProfiling

#
pub fn enableProfiling(_engine: *InferenceEngine) !void

Enable GPU kernel profiling on the inference engine.

Supported on both Vulkan and Metal backends; calls the backend's own `enableProfiling` method.

Parameters

_engine
Inference engine to configure for profiling.

Returns

An error if the backend's profiling setup fails (e.g. out of GPU resources).

src/server/runtime.zig:98

function

decodeStep

#
pub fn decodeStep( _engine: *InferenceEngine, _state: *DecodeState, _token_id: u32, _collect_output: bool, ) !void

Run a single autoregressive decode step, advancing the KV cache by one token.

Parameters

_engine
Inference engine that owns the model weights and KV cache.
_state
Per-sequence decode state tracking position and token history.
_token_id
Input token to feed into the model for this step.
_collect_output
When `true`, copy output logits to CPU (Vulkan only; ignored on Metal where logits are always accessible).

Returns

An error if the GPU submission or synchronisation fails.

src/server/runtime.zig:112

constant

mtp_max_draft

#
pub const mtp_max_draft: u32 = if (gpu.is_vulkan) InferenceEngine.mtp_max_draft_tokens else 3

NextN/MTP speculative decoding (Vulkan backend).

Greedy requests only.

src/server/runtime.zig:126

function

mtpBeginRequest

#
pub fn mtpBeginRequest(_engine: *InferenceEngine) void

Reset per-request MTP state; call before the prompt prefill.

src/server/runtime.zig:129

function

mtpLogRequest

#
pub fn mtpLogRequest(_engine: *const InferenceEngine) void

Log counters for the completed request, before another request can reset them.

src/server/runtime.zig:140

function

mtpPrime

#
pub fn mtpPrime(_engine: *InferenceEngine, _state: *DecodeState, _prompt_tokens: []const u32) bool

Prime the NextN block over the freshly prefilled prompt.

Returns false when MTP is unavailable (env off, model without NextN block, partial prefill).

src/server/runtime.zig:146

function

mtpSetPrimeDuringPrefill

#
pub fn mtpSetPrimeDuringPrefill(_engine: *InferenceEngine, _on: bool) void

Whether a chunked prefill primes the draft block chunk by chunk (off for requests that sample non-greedily, where speculation is unused).

src/server/runtime.zig:157

function

ssmCheckpointTake

#
pub fn ssmCheckpointTake(_engine: *InferenceEngine, _state: *DecodeState) bool

Recurrent-state checkpoints for chat reuse on hybrid models (see InferenceEngine.ssm_checkpoint).

All return false / null on other backends.

src/server/runtime.zig:163

function

ssmCheckpointRestore

#
pub fn ssmCheckpointRestore(_engine: *InferenceEngine, _state: *DecodeState) bool

No public doc comment yet.

src/server/runtime.zig:167

function

ssmCheckpointPosition

#
pub fn ssmCheckpointPosition(_engine: *const InferenceEngine) ?u32

No public doc comment yet.

src/server/runtime.zig:171

function

ssmCheckpointInvalidate

#
pub fn ssmCheckpointInvalidate(_engine: *InferenceEngine) void

No public doc comment yet.

src/server/runtime.zig:175

function

canRewindAttentionPrefix

#
pub fn canRewindAttentionPrefix(_engine: *const InferenceEngine) bool

A canonical suffix may overwrite old KV rows only when there is no recurrent or draft state to restore.

An enabled lazy draft counts too.

src/server/runtime.zig:181

function

canonicalPrefixDefault

#
pub fn canonicalPrefixDefault(_engine: *const InferenceEngine) bool

No public doc comment yet.

src/server/runtime.zig:188

function

mtpResetCapture

#
pub fn mtpResetCapture(_engine: *InferenceEngine) void

Forget captured prefill rows so the next prefill's capture starts at row 0.

src/server/runtime.zig:197

function

mtpPrimeSuffix

#
pub fn mtpPrimeSuffix(_engine: *InferenceEngine, _state: *DecodeState, _prompt_tokens: []const u32, _reused_prefix_len: u32) bool

Prime the NextN block over only the tokens appended after a reused prompt prefix.

Returns false when the engine no longer holds that prefix, in which case the caller keeps ordinary decode.

src/server/runtime.zig:204

struct

MtpSource

#
pub const MtpSource = struct

Token source that replaces `decodeStep` + greedy `sample` with NextN/MTP cycles when active, and falls back to the plain path otherwise.

src/server/runtime.zig:215

Methods

2

method

MtpSource.step

#
pub fn step( self: *MtpSource, _engine: *InferenceEngine, _state: *DecodeState, prev: u32, tokens_remaining: u32, _params: SamplingParams, _random: std.Random, ) !u32

Feed `prev` to the model and return the next token.

`tokens_remaining` is the number of tokens the request may still produce (>= 1).

src/server/runtime.zig:224

method

MtpSource.finish

#
pub fn finish(_: *MtpSource, _engine: *InferenceEngine) void

Wait for any decode step queued ahead by `step` (Vulkan chained greedy decode) so the engine is idle when the request ends.

src/server/runtime.zig:271

function

sample

#
pub fn sample( _engine: *const InferenceEngine, _state: *const DecodeState, _params: SamplingParams, _random: std.Random, ) u32

Sample the next token from the model's logit distribution.

Parameters

_engine
Inference engine holding the current logits.
_state
Decode state used to retrieve generated-token history for repetition penalty.
_params
Sampling configuration (temperature, top-p, top-k, repetition penalty, etc.).
_random
PRNG source for stochastic sampling.

Returns

The sampled token ID.

src/server/runtime.zig:282