For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).
Python module
max.pipelines.architectures.deepseekV4
DeepSeek-V4-Flash mixture-of-experts architecture for text generation.
DeepseekV4Config
class max.pipelines.architectures.deepseekV4.DeepseekV4Config(*, dtype, kv_params, devices, use_subgraphs=True, data_parallel_degree=1, quantization_encoding=None, max_seq_len, vocab_size=129280, hidden_size=4096, num_hidden_layers=43, num_attention_heads=64, num_key_value_heads=1, rms_norm_eps=1e-06, hidden_act='silu', tie_word_embeddings=False, attention_bias=False, attention_dropout=0.0, head_dim=512, qk_rope_head_dim=64, q_lora_rank=1024, o_lora_rank=1024, o_groups=8, sliding_window=128, compress_ratios=<factory>, compress_rope_theta=160000.0, index_head_dim=128, index_n_heads=64, index_topk=512, moe_intermediate_size=2048, n_routed_experts=256, n_shared_experts=1, num_experts_per_tok=6, routed_scaling_factor=1.5, scoring_func='sqrtsoftplus', norm_topk_prob=True, swiglu_limit=10.0, expert_dtype='fp4', num_hash_layers=3, hc_mult=4, hc_sinkhorn_iters=20, hc_eps=1e-06, dspark_block_size=5, dspark_noise_token_id=128799, dspark_target_layer_ids=<factory>, dspark_markov_rank=256, max_position_embeddings=1048576, rope_theta=10000.0, rope_scaling=None, rope_interleave=True, norm_dtype=bfloat16, gate_dtype=None, correction_bias_dtype=None, max_batch_context_length=131072, quant_config=None, native_routed_experts=None, graph_mode='auto', return_logits=ReturnLogits.LAST_TOKEN, return_hidden_states=ReturnHiddenStates.NONE, dspark_stages=True)
Bases: ArchConfigWithKVCache
Configuration for DeepSeek-V4-Flash models.
V4 is not a V3.2 variant despite the shared lineage. It replaces V3.2’s
MLA (kv_lora_rank + qk_nope_head_dim + v_head_dim + kv_b_proj)
with a single head_dim-wide shared latent produced by wkv: query and
key live in the same 512-dim space, the last rope_head_dim dims carry
RoPE, and there is no per-head KV up-projection at all. It also adds
sliding-window + compressed-token sparse attention (CSA), a grouped low-rank
output projection, hyper-connection residuals (mHC), and hash routing on the
first num_hash_layers MoE layers. Hence a standalone config rather than a
subclass of DeepseekV3_2Config.
-
Parameters:
-
- dtype (DType)
- kv_params (KVCacheParamInterface)
- devices (list[DeviceRef])
- use_subgraphs (bool)
- data_parallel_degree (int)
- quantization_encoding (SupportedEncoding | None)
- max_seq_len (int)
- vocab_size (int)
- hidden_size (int)
- num_hidden_layers (int)
- num_attention_heads (int)
- num_key_value_heads (int)
- rms_norm_eps (float)
- hidden_act (str)
- tie_word_embeddings (bool)
- attention_bias (bool)
- attention_dropout (float)
- head_dim (int)
- qk_rope_head_dim (int)
- q_lora_rank (int)
- o_lora_rank (int)
- o_groups (int)
- sliding_window (int)
- compress_ratios (list[int])
- compress_rope_theta (float)
- index_head_dim (int)
- index_n_heads (int)
- index_topk (int)
- moe_intermediate_size (int)
- n_routed_experts (int)
- n_shared_experts (int)
- num_experts_per_tok (int)
- routed_scaling_factor (float)
- scoring_func (str)
- norm_topk_prob (bool)
- swiglu_limit (float)
- expert_dtype (str | None)
- num_hash_layers (int)
- hc_mult (int)
- hc_sinkhorn_iters (int)
- hc_eps (float)
- dspark_block_size (int)
- dspark_noise_token_id (int)
- dspark_target_layer_ids (list[int])
- dspark_markov_rank (int)
- max_position_embeddings (int)
- rope_theta (float)
- rope_scaling (dict[str, Any] | None)
- rope_interleave (bool)
- norm_dtype (DType)
- gate_dtype (DType | None)
- correction_bias_dtype (DType | None)
- max_batch_context_length (int)
- quant_config (QuantConfig | None)
- native_routed_experts (bool | None)
- graph_mode (str)
- return_logits (ReturnLogits)
- return_hidden_states (ReturnHiddenStates)
- dspark_stages (bool)
DEFAULT_ENCODING
DEFAULT_ENCODING: ClassVar[SupportedEncoding] = 'float8_e4m3fn'
SUPPORTED_ENCODINGS
SUPPORTED_ENCODINGS: ClassVar[set[SupportedEncoding]] = {'float32', 'float8_e4m3fn'}
attention_bias
attention_bias: bool = False
attention_dropout
attention_dropout: float = 0.0
calculate_max_seq_len()
classmethod calculate_max_seq_len(huggingface_config, model_config)
Returns the resolved maximum sequence length.
Bounds or defaults the user’s model_config.max_length with the
model’s own limits. Construction runs this once and stores the
result on model_config.max_length; memory planning may lower
it further, but only on the memory plan.
-
Parameters:
-
- huggingface_config (AutoConfig) – The HuggingFace config to read model bounds from.
- model_config (MAXModelConfig) – The model config whose
max_lengthcarries the user’s setting.
-
Return type:
compress_ratios
compress_rope_theta
compress_rope_theta: float = 160000.0
compressed_layer_index()
compressed_layer_index(layer_idx)
Rank of a compressed layer among trunk layers sharing its ratio.
That rank is its layer index within the ratio’s zone and state leaves.
construct_kv_params()
static construct_kv_params(huggingface_config, pipeline_config, devices, kv_cache_config, cache_dtype)
Build the seven-leaf KV tree (see kv_leaf_specs).
The reference sizes each layer’s cache as window_size + max_seq_len // compress_ratio. Here the window is one sliding-window leaf over
every layer and each ratio’s compressed entries are a zone leaf with
page_size // ratio slots per page, so no layer over-allocates.
-
Parameters:
-
- huggingface_config (AutoConfig)
- pipeline_config (PipelineConfig)
- devices (list[DeviceRef])
- kv_cache_config (KVCacheConfig)
- cache_dtype (DType)
-
Return type:
correction_bias_dtype
data_parallel_degree
data_parallel_degree: int = 1
devices
dspark_block_size
dspark_block_size: int = 5
dspark_markov_rank
dspark_markov_rank: int = 256
dspark_noise_token_id
dspark_noise_token_id: int = 128799
dspark_stages
dspark_stages: bool = True
dspark_target_layer_ids
dtype
dtype: DType
expert_dtype
gate_dtype
get_kv_params()
get_kv_params()
KV cache parameters to use when running the model.
-
Return type:
get_max_seq_len()
get_max_seq_len()
Returns the effective maximum sequence length for the model.
For configs that store a deployment length, this is the value
initialize received; for metadata-only configs it derives from
the checkpoint.
-
Return type:
get_num_layers()
static get_num_layers(huggingface_config)
-
Parameters:
-
huggingface_config (AutoConfig)
-
Return type:
graph_mode
graph_mode: str = 'auto'
hc_eps
hc_eps: float = 1e-06
hc_mult
hc_mult: int = 4
hc_sinkhorn_iters
hc_sinkhorn_iters: int = 20
head_dim
head_dim: int = 512
hidden_act
hidden_act: str = 'silu'
hidden_size
hidden_size: int = 4096
index_head_dim
index_head_dim: int = 128
index_n_heads
index_n_heads: int = 64
index_topk
index_topk: int = 512
initialize()
classmethod initialize(pipeline_config, model_config=None, *, max_seq_len)
Initialize the config from a PipelineConfig.
-
Parameters:
-
- pipeline_config (PipelineConfig) – The pipeline configuration.
- model_config (MAXModelConfig | None) – The model configuration to read from. When
None(the default),pipeline_config.modelis used. Pass an explicit config (such aspipeline_config.draft_model) to initialize the arch config for a different model. - max_seq_len (int) – The effective maximum sequence length to store on
the config. The value is received, never derived here: the
pipeline model passes the memory plan’s VRAM-clamped length,
while memory planning (which runs before a plan exists)
passes the construction-resolved
model_config.max_length. Configs whose sequence length is pure model metadata (such as diffusion components) ignore it.
-
Return type:
kv_leaf_specs()
kv_leaf_specs()
-
Return type:
-
list[KVLeafSpec]
kv_params
kv_params: KVCacheParamInterface
layer_compress_ratio()
layer_compress_ratio(layer_idx)
layer_has_indexer()
layer_has_indexer(layer_idx)
layer_is_hash_routed()
layer_is_hash_routed(layer_idx)
max_batch_context_length
max_batch_context_length: int = 131072
max_position_embeddings
max_position_embeddings: int = 1048576
max_seq_len
max_seq_len: int
moe_intermediate_size
moe_intermediate_size: int = 2048
n_routed_experts
n_routed_experts: int = 256
n_shared_experts
n_shared_experts: int = 1
native_routed_experts
norm_dtype
norm_dtype: DType = 80
norm_topk_prob
norm_topk_prob: bool = True
num_attention_heads
num_attention_heads: int = 64
num_experts_per_tok
num_experts_per_tok: int = 6
num_hash_layers
num_hash_layers: int = 3
num_hidden_layers
num_hidden_layers: int = 43
num_key_value_heads
num_key_value_heads: int = 1
o_groups
o_groups: int = 8
o_lora_rank
o_lora_rank: int = 1024
q_lora_rank
q_lora_rank: int = 1024
qk_nope_head_dim
property qk_nope_head_dim: int
Width of the non-RoPE portion of the shared latent.
qk_rope_head_dim
qk_rope_head_dim: int = 64
quant_config
quant_config: QuantConfig | None = None
quantization_encoding
quantization_encoding: SupportedEncoding | None = None
return_hidden_states
return_hidden_states: ReturnHiddenStates = 'none'
return_logits
return_logits: ReturnLogits = 'last_token'
rms_norm_eps
rms_norm_eps: float = 1e-06
rope_interleave
rope_interleave: bool = True
rope_scaling
rope_theta
rope_theta: float = 10000.0
routed_experts_native
property routed_experts_native: bool
Whether the routed experts run on the W4A8 grouped kernel.
routed_scaling_factor
routed_scaling_factor: float = 1.5
scoring_func
scoring_func: str = 'sqrtsoftplus'
sliding_window
sliding_window: int = 128
swiglu_limit
swiglu_limit: float = 10.0
tie_word_embeddings
tie_word_embeddings: bool = False
use_subgraphs
use_subgraphs: bool = True
vocab_size
vocab_size: int = 129280
window_ratios
The compression ratios the trunk’s attention layers use.
DeepseekV4Model
class max.pipelines.architectures.deepseekV4.DeepseekV4Model(pipeline_config, session, devices, kv_cache_config, weights, *, memory_plan, adapter=None, return_logits=ReturnLogits.LAST_TOKEN, max_batch_size=1)
Bases: GraphPipelineModelWithKVCache[TextContext, Any]
A DeepSeek-V4-Flash pipeline model for text generation.
-
Parameters:
-
- pipeline_config (PipelineConfig)
- session (InferenceSession)
- devices (list[Device])
- kv_cache_config (KVCacheConfig)
- weights (Weights)
- memory_plan (MemoryPlan)
- adapter (WeightsAdapter | None)
- return_logits (ReturnLogits)
- max_batch_size (int)
batch_processor_cls
batch_processor_cls
alias of DeepseekV4BatchProcessor
execute()
execute(model_inputs)
Executes the graph with the given inputs.
-
Parameters:
-
model_inputs (ModelInputs) – The model inputs to execute, containing tensors and any other required data for model execution.
-
Returns:
-
ModelOutputs containing the pipeline’s output tensors.
-
Return type:
This is an abstract method that must be implemented by concrete PipelineModels to define their specific execution logic.
graph_specs()
graph_specs(model_config)
Returns the decoder graph, tensor parallel on several devices.
-
Parameters:
-
model_config (DeepseekV4Config)
-
Return type:
model
property model: Model
The compiled and initialized MAX Engine model.
model_config_cls
model_config_cls
alias of DeepseekV4Config
DeepseekV4ReasoningParser
class max.pipelines.architectures.deepseekV4.DeepseekV4ReasoningParser(think_start_token_id, think_end_token_id)
Bases: ReasoningParser
DeepSeek-V4 reasoning parser for spans framed by <think> and </think>.
In thinking mode the prompt ends with a prefilled <think>, so
reasoning begins implicitly; in chat mode it ends with </think>. Only
</think> ends reasoning: the encoder’s tool instructions require the
complete reasoning block before any <|DSML|tool_calls> block.
REASONING_END
Text delimiter that closes a reasoning span, such as "</think>".
See REASONING_START for the declaration contract.
REASONING_START
Text delimiter that opens a reasoning span, such as "<think>".
Subclasses declare their delimiters here and resolve token ids from them, so each model’s delimiters are written down exactly once. Consumers that work in the text domain rather than the token domain read them from here instead of restating them.
Declare both delimiters or neither. None means this parser has no text
form at all, so a text-domain consumer cannot bound a reasoning span and
will leave reasoning in the assistant’s content; declaring only one is a
bug, since a span needs both ends. A parser whose chat template prefills
the opening delimiter still declares it – whether a given turn emits it is
a property of the request, not of the parser.
from_tokenizer()
async classmethod from_tokenizer(tokenizer)
Constructs a reasoning parser from a tokenizer.
-
Parameters:
-
tokenizer (PipelineTokenizer[Any, Any, Any])
-
Return type:
reasoning_end_token_id()
async classmethod reasoning_end_token_id(tokenizer)
Returns the </think> token id.
-
Parameters:
-
tokenizer (PipelineTokenizer[Any, Any, Any])
-
Return type:
-
int | None
stream()
stream(delta_token_ids, is_currently_reasoning=True)
Identifies a reasoning span within a streaming delta chunk.
When is_currently_reasoning=False and the chunk contains no
<think> opener, returns an empty span so chat-mode output and
chunks after reasoning ended aren’t misclassified as reasoning.
-
Parameters:
-
Return type:
will_reason_after_prompt()
will_reason_after_prompt(prompt_token_ids)
Predicts whether the model will emit reasoning after this prompt.
Scans right-to-left: earlier turns and the tool instructions also
carry <think>/</think>, but the last delimiter is the one the
encoder prefilled after <|Assistant|>.
DeepseekV4Tokenizer
class max.pipelines.architectures.deepseekV4.DeepseekV4Tokenizer(model_path, pipeline_config, **kwargs)
Bases: ReasoningTextTokenizer
TextTokenizer for DeepSeek-V4, whose checkpoint ships no chat template.
Chat prompts are rendered by the checkpoint’s reference encoder
(encoding_dsv4.py) unless --chat-template is given. Implements
ReasoningPipelineTokenizer.
-
Parameters:
-
- model_path (str)
- pipeline_config (PipelineConfig)
apply_chat_template()
apply_chat_template(messages, tools, **chat_template_options)
Renders messages with the DeepSeek-V4 encoder (see render_prompt()).
-
Parameters:
-
- messages (list[TextGenerationRequestMessage])
- tools (list[TextGenerationRequestTool] | None)
- chat_template_options (Any)
-
Return type:
DeepseekV4ToolParser
class max.pipelines.architectures.deepseekV4.DeepseekV4ToolParser
Bases: StructuralTagToolParser
Parses DeepSeek-V4 <|DSML|tool_calls> blocks into tool calls.
The router hands this parser content only: the deepseekv4 reasoning
parser has already moved everything up to </think> into reasoning.
A DSML block inside the reasoning is therefore never a tool call. The
reference decoder rejects such output outright; here it stays verbatim in
the reasoning.
The section marker includes the \n\n the reference decoder requires
before <|DSML|tool_calls>, so the content ahead of a call streams
without it, as it parses. Arguments stream one complete parameter at a
time; string="true|false" carries each value’s type, so no schema is
needed.
CALL_BEGIN
CALL_END
SECTION_BEGIN
SECTION_END
parse_complete()
parse_complete(response)
Parses a complete response with the reference decoder.
Output without any |DSML| token is returned as content, so plain
replies never meet the decoder’s special-token checks. Otherwise the
reference decoder decides, in chat mode because reasoning was split
off upstream; the response has its EOS stripped by detokenization, so
it is restored first.
-
Parameters:
-
response (str) – The content the model generated after its reasoning.
-
Returns:
-
The content before the tool-call block (
Nonewhen empty) and the parsed tool calls. -
Raises:
-
ValueError – If the DSML is malformed or holds no tool call.
-
Return type: