ATOM model operations guide#
ATOM (AiTer Optimized Model) wraps AITER kernels with model-level abstractions for LLM inference on AMD ROCm/HIP GPUs. This guide documents every operator class in atom/model_ops/, their AITER kernel mappings, quantization paths, and fused kernel chains.
Quick reference#
ATOM Class |
File |
AITER Kernel / Import |
Purpose |
|---|---|---|---|
|
|
|
Quantized linear dispatch |
|
|
(inherits |
Column-sharded TP linear |
|
|
(inherits |
Row-sharded TP linear |
|
|
(inherits |
Fused Q/K/V projection |
|
|
(inherits |
Merged gate+up projection |
|
|
|
Unified attention entry |
MHA |
|
|
Multi-head attention |
|
|
|
Multi-head latent attention |
Shared DSA indexer scorer |
|
|
Scoring and top-k for every DSA indexer (DeepSeek-V3.2 / GLM-5.x, GLM-5.3 kpool, DeepSeek-V4, V4.1): the attention builder publishes a step’s rows once ( |
DSA FP4 index cache |
|
|
FP4 (E2M1 + e8m0) index-cache geometry and the DSA FP4 predicate |
|
|
|
FP4 MQA scoring’s ragged rows (DeepSeek-V4, V4.1, V3.2 decode and its DCP shard): rows grouped into sequences ( |
|
|
Triton |
Sparse MLA’s MTP draft steps 1+ reuse step 0’s top-k: chosen rows’ selections packed in place to the front of the sparse index buffer, one program a sequence, each waiting on its predecessors’ published lengths (an epoch-tagged flag, so eager-only) |
|
|
|
Mixture of experts |
|
|
|
RMS normalization |
|
|
|
Layer normalization |
|
|
|
SiLU gated activation |
|
|
|
Vocab embedding |
|
|
|
LM output head |
|
|
|
Rotary position embedding |
|
|
|
Token sampling |
|
|
Triton |
Speculative decoding |
|
|
Triton packed-bitmask kernel |
JSON grammar masks on selected target logits |
|
|
Triton |
|
AITER integration overview#
ATOM is a thin model-level inference engine. Every compute-heavy operation delegates to an AITER kernel. The general pattern is:
An ATOM
nn.Moduleowns model weights and configuration.Its
forward()method selects the appropriate AITER function based on quantization type, parallelism settings, and phase (prefill vs. decode).Results are optionally reduced across tensor-parallel (TP) or data-parallel (DP) groups.
AITER kernel mapping table#
ATOM Wrapper |
AITER Function / Import Path |
Backend Type |
|---|---|---|
|
|
hipBLASLt |
|
|
hipBLASLt |
|
|
CK |
|
|
CK |
|
|
CK |
|
|
CK |
MHA prefill |
|
ASM / CK |
MHA decode (ASM) |
|
ASM |
MHA decode (persistent ASM) |
|
ASM |
MHA decode (Triton) |
|
Triton |
MHA prefill (Triton unified) |
|
Triton |
MLA decode |
|
ASM |
MLA prefill |
|
ASM |
MLA KV cache |
|
CK |
RoPE |
|
Triton |
RMSNorm |
|
CK |
SiLU+Mul |
|
CK |
TopK routing |
|
CK |
Sampling |
|
CK |
FusedMoE |
|
CK |
ASM MoE |
|
ASM |
Quantization |
|
CK / Triton |
Linear operations#
All linear layers inherit from LinearBase in atom/model_ops/linear.py.
Class hierarchy#
LinearBase (nn.Module)
+-- ReplicatedLinear # No TP sharding
| +-- MergedReplicatedLinear
+-- ColumnParallelLinear # tp_dim=0, shard output
| +-- QKVParallelLinear # Fused Q/K/V with per-head sharding
+-- MergedColumnParallelLinear # tp_dim=0, merged gate+up
+-- RowParallelLinear # tp_dim=1, shard input, optional all-reduce
Quantization dispatch#
LinearBase.forward() dispatches to different GEMM kernels based on QuantType:
|
Weight dtype |
GEMM Kernel |
Scale Shape |
|---|---|---|---|
|
BF16/FP16 |
|
None |
|
FP8 |
|
|
|
INT8 |
|
|
|
FP8 |
|
|
|
FP8 |
|
|
|
MXFP4 ( |
|
|
When x_scale is not provided, the input is dynamically quantized via get_hip_quant(quant_type).
Tensor parallel sharding#
ColumnParallelLinear (
tp_dim=0): Shards weight rows (output dimension) across GPUs. Each GPU ownsoutput_size / tp_sizerows.RowParallelLinear (
tp_dim=1): Shards weight columns (input dimension). Ifreduce_results=True, output is all-reduced across TP group.QKVParallelLinear: Extends
ColumnParallelLinearwith per-head sharding. Q heads are evenly divided; KV heads are either divided or replicated whennum_kv_heads < tp_size.MergedColumnParallelLinear: Handles gate and up projections merged into a single weight with
output_sizesas a list (e.g.,[intermediate_size, intermediate_size]).
Weight processing#
After loading, process_weights_after_loading() handles:
e4m3fn to e4m3fnuz normalization (AMD FP8 format conversion).
Weight reshuffling via
shuffle_weights()for pre-shuffled GEMM kernels.Scale reshuffling via
fp4_utils.e8m0_shuffle()for MXFP4 block scales.MXFP4 K alignment: a preshuffled MXFP4 weight must have K a multiple of 256 (
MXFP4_PRESHUFFLE_K_ALIGN); the preshuffled GEMMs walk K in 256-wide steps and do not mask a tail. Agate_up -> act -> downMLP whose per-rank intermediate is unaligned pads it as a pair, like MoE’sintermediate_pad: passmxfp4_k_pad_align(quant_config, down_prefix)asoutput_pad_aligntoMergedColumnParallelLinearandinput_pad_aligntoRowParallelLinear. Only a pair that is MXFP4 or bf16 in the checkpoint is padded (a bf16 pair may be quantized online; its zeroed rows quantize to zero): the padded parameters are zeroed when allocated, and every load path writes the real shard at the front of its padded slot (requiresact(0, 0) == 0). A padded layer requantized online from a quantized source (NVFP4, FP8), or a static pair whose halves aren’t both MXFP4 or bf16, is refused at construction. Any preshuffled MXFP4 weight still unaligned is refused here at load. Exempt: the gfx1250 32x4 scale layout, whose kernels take K % 128 (mxfp4_k_pad_alignpads to 128 there), andATOM_USE_FP4_NON_SHUFFLE_TRITON_GEMM=1, whose GEMM reads the raw layout. A refused layer can be kept out of MXFP4 (online quant:exclude_layer).Per-tensor requantization via
requantize_with_max_scale()when multiple output partitions have separate scales.
Attention operations#
Base: Attention (base_attention.py)#
The top-level Attention class in base_attention.py is a dispatcher. It:
Selects the backend via
get_attn_backend()fromatom/utils/selector.py.Instantiates the backend’s implementation class (
impl_cls).Registers itself in
compilation_config.static_forward_contextunderlayer_name.On
forward(), callstorch.ops.aiter.unified_attention_with_output_base, which is a custom op decorated with@mark_spliting_op— this preventstorch.compilefrom tracing into attention internals, enabling full-graph capture.
Backend selection logic (in selector.py):
Condition |
Backend Class |
Implementation |
|---|---|---|
|
|
|
|
|
|
Multi-head attention (attention_mha.py)#
The MHA Attention class handles standard models (Llama, Qwen3, Mixtral, etc.).
Forward flow:
Reshape Q, K, V to
[num_tokens, num_heads, head_dim].Apply RoPE + KV cache write via
rope_cache().Dispatch to the appropriate backend via
dispatch_backend().
RoPE + KV cache paths:
Condition |
Kernel Chain |
|---|---|
|
|
Triton path ( |
|
ASM path + |
|
Attention dispatch:
Phase |
Condition |
Method |
AITER Kernel |
|---|---|---|---|
Prefill |
Always |
|
|
Decode |
|
|
|
Decode |
past a paged kernel’s envelope, or |
|
|
Decode |
|
|
|
Decode |
Default |
|
|
The use_triton_attn flag is set when sliding_window != -1 or head_dim != 128.
Both paged kernels stop short of unified, at different points, and drafting
multiplies the query group into both limits (base_attention.py):
Kernel |
Envelope |
Past it |
|---|---|---|
gluon |
|
no layout arm; Triton fails to compile |
|
|
|
_dispatch_decode is the single place that answers this; the runners do not
re-decide. Unified carries one descale for the whole tensor, so a per-token
quantized layer routed there raises rather than returning wrong numbers.
Multi-head latent attention (attention_mla.py)#
MLAAttention implements DeepSeek’s MLA with a compressed KV representation. Key data structures:
@dataclass
class MLAModules:
q_lora_rank: Optional[int]
kv_lora_rank: int
qk_nope_head_dim: int
qk_rope_head_dim: int
qk_head_dim: int
v_head_dim: int
rotary_emb: torch.nn.Module
q_proj: Optional[torch.nn.Module]
kv_b_proj: torch.nn.Module
o_proj: torch.nn.Module
indexer: Optional[torch.nn.Module]
Forward flow:
If prefill and not sparse: Standard MHA-style prefill with
flash_attn_varlen_func, preceded bykv_b_projGEMM to produce K_nope and V from compressedkv_c_normed.Otherwise: Fused Q projection + K up-projection via batched FP8/FP4 BMM (
_q_proj_and_k_up_proj), then:fused_qk_rope_concat_and_cache_mlawrites to KV cache.Decode:
mla_decode_fwd(ASM persistent MLA kernel).Prefill (sparse):
mla_prefill_fwd.
V up-projection + O projection via batched BMM (
_v_up_proj_and_o_proj).
Batched GEMM backends for MLA projections:
Condition |
Kernel |
|---|---|
|
|
Default |
|
Prefill GEMM optimizations (for kv_b_proj):
Condition |
Kernel |
|---|---|
|
|
|
|
Default |
|
Backend abstraction (attentions/backends.py)#
The AttentionBackend abstract class defines three required methods:
get_name()— Returns backend identifier string.get_builder_cls()— Returns theAttentionMetadataBuildersubclass.get_impl_cls()— Returns the attention implementation class.
CommonAttentionBuilder provides shared metadata preparation (slot mapping, block tables, cumulative sequence lengths) used by both AiterBackend and AiterMLABackend. It marshals block_tables on every prefill step out to running_bs, zero rows past the scheduled requests, whether or not that step also uploads the buffer, because the slot arithmetic in token_layout/prefill.py reads the packed table back rather than the batch’s ragged rows. A prefill that uploads it (_prefill_block_table_rows) uploads running_bs rows, like a decode.
KV cache operations#
Operation |
AITER Kernel |
Used By |
|---|---|---|
Standard KV cache write |
|
MHA (BF16 KV) |
FP8 KV cache write |
|
MHA (FP8 KV) |
MLA KV cache write |
|
MLA prefill |
Fused QK RoPE + MLA cache |
|
MLA decode |
Mixture of experts (MoE)#
FusedMoE class (moe.py)#
FusedMoE is the top-level MoE module. It handles:
Expert routing via
select_experts().Weight creation and quantization dispatch via
quant_method.Tensor/Expert/Data parallelism via
FusedMoEParallelConfig.Optional shared expert fusion and MORI communication.
Constructor parameters:
FusedMoE(
num_experts: int, # Global number of experts
top_k: int, # Experts per token
hidden_size: int, # Input hidden dimension
intermediate_size: int, # Expert intermediate dimension
reduce_results: bool, # Whether to all-reduce output
renormalize: bool, # Renormalize routing weights
use_grouped_topk: bool, # Use grouped top-k (DeepSeek)
activation: ActivationType, # Silu, Gelu, Swiglu, etc.
...
)
Quantization methods#
FusedMoE selects a quant_method at construction time:
Quant Config |
Method Class |
GEMM Kernel |
|---|---|---|
|
|
|
FP8 ( |
|
|
FP8 compressed-tensors |
|
|
MXFP4 ( |
|
|
The ASM MoE path (asm_moe from aiter.fused_moe_bf16_asm) is used by FP8 methods and supports a16 mode where activations remain in BF16/FP16 while weights are FP8/INT8.
TopK routing (topK.py)#
Routing Function |
AITER Kernel |
Used For |
|---|---|---|
|
|
Standard top-k (Mixtral) |
|
|
Grouped top-k (DeepSeek) |
|
|
Biased grouped top-k (DeepSeek V3) |
rocm_aiter_topk_softmax runs triton_topk_softmax instead (bit-identical) in launches of up to triton_topk_softmax_rows rows: 20 per CU on gfx950, for the shapes it takes. Under an HSA_CU_MASK set outside ATOM, export CU_NUM (the CUs it leaves) at process start: the cutoff counts AITER’s cached get_cu_num(), which ignores the mask.
Shared expert fusion: When is_rocm_aiter_fusion_shared_expert_enabled() returns True, the top-k buffers are extended with shared expert IDs appended after routed expert IDs. This allows shared expert computation to be fused into the same MoE kernel call. The metadata is initialized via init_aiter_topK_meta_data(). Under DP attention with MoRI EP, fusion is wired up only for MXFP4 / NVFP4 routed experts; FP8 MoE models such as DeepSeek-V3 must run with ATOM_FUSE_SHARED_EXPERT=0, or FusedMoE raises NotImplementedError.
FusedMoEParallelConfig#
@dataclass
class FusedMoEParallelConfig:
tp_size: int # Tensor parallel size
dp_size: int # Data parallel size
ep_size: int # Expert parallel size
tp_rank: int
dp_rank: int
ep_rank: int
use_ep: bool # Whether expert parallelism is active
local_ep_size: int # Local EP size (GPUs per node * TP)
Key properties:
use_all2all_kernels:Truewhendp_size > 1, EP is enabled, and MORI is available.use_mori_kernels: AlwaysTrue(currently).
MORI integration (fused_moe/mori_prepare_finalize.py)#
MORI (MoE Router Infrastructure) provides all-to-all communication kernels for expert parallelism. MoriPrepareAndFinalize implements:
prepare(): Dispatches tokens to remote experts viamori_op.dispatch(), in the layer’s routed-rows wire (MXFP8 with e8m0 scales where the experts take MXFP8,ATOM_MORI_FP4_DISPATCHfor MXFP4, bf16 otherwise).finalize(): Combines expert outputs viamori_op.combine()and copies results back.
The FusedMoEModularKernel orchestrates the prepare-compute-finalize pipeline.
Routing before the rows move (fused_moe/routed_rows.py)#
A MoE method splits into select_experts(layer, x, router_logits) (logical
routes), place_experts(layer, topk_weights, topk_ids, gathered) (the EPLB
remap and load record, the shared column in the layer’s expert layout) and
apply(layer, x, topk_weights, topk_ids, ..., x_scale=None). FusedMoE routes
on the rank that owns the rows, before any of them move: the MORI dispatch, the
DP gather (DP attention without the all2all kernels), the PCP gather
(ATOM_PCP_MOE_MERGE, inside FusedMoE._run_experts) and the comm-fused DPA
gather all carry top-k weights and ids rather than router logits. Rows are
placed after every gather, so a gathered row – held alike by every rank –
picks the same EPLB replica on each (gathered); and where the legacy layout
widens in the prefilled shared column (LEGACY_AITER), whose ids say by row
position which rank runs a row’s shared expert, a gather is followed by
reassign_gathered_shared_ids, which gives the rows this rank’s ids for their
gathered positions. Every routing kernel takes the pass’s real row count,
Context.scheduled_tokens_gpu (an entry of the step’s
forward_vars["batch_info"], read on device so a replayed graph follows its
own step), and routes the rows past it to -1, so they are neither sent nor
computed. Only a consumer of the pass’s whole rows (running_tokens) gets the
count (pass_scheduled_tokens_gpu): the MoE sort reads it as its row count, so
a subset of the rows – a PCP shard, gathered rows – gets None and runs every
row, unless it declares a pass of its own with its own count
(ForwardContext.use_pass_rows, as V4.1’s decoder does over a prefill’s tails
with ReplayPlan.filled_gpu / finishing_gpu). The rows travel in
RoutedRowsWire, the method’s routed_rows_wire – the one format both the
gathers and the MoRI v1 dispatch use: MXFP4 under ATOM_MORI_FP4_DISPATCH,
MXFP8 where the experts take MXFP8 activations (FlyDSL fused_moe, the Triton
a8w4 EP experts), bf16 otherwise (the TP Triton path, MegaMoE, the MoRI v2
transport). Rows already in the wire carry their e8m0 rows as x_scale through
apply, the modular kernel and prepare, so nothing is quantized twice.
MoE quantization config (fused_moe/config.py)#
FusedMoEQuantConfig describes activation and weight quantization for MoE layers:
@dataclass
class FusedMoEQuantConfig:
_a1: FusedMoEQuantDesc # First activation (input to gate_up)
_a2: FusedMoEQuantDesc # Second activation (input to down_proj)
_w1: FusedMoEQuantDesc # gate_up_proj weights
_w2: FusedMoEQuantDesc # down_proj weights
Factory functions:
fp8_w8a8_moe_quant_config()— FP8 weights and activations.mxfp4_w4a16_moe_quant_config()— MXFP4 weights, unquantized activations.FUSED_MOE_UNQUANTIZED_CONFIG— No quantization.
Triton MoE fallback (fused_moe_triton.py)#
triton_kernel_fused_experts() provides a Triton-based MoE path. It takes rows routed already (FusedMoE.select_experts, before any gather or dispatch): routing_from_dispatched() sorts their top-k ids into the Triton routing data, with a gate_valid mask for rows another rank owns or rows past the real row count, and the expert GEMMs run on that. This path is used for MXFP4 MoE under ATOM_USE_TRITON_MOE.
Normalization#
RMSNorm (layernorm.py)#
RMSNorm supports multiple forward paths depending on configuration flags:
Condition |
Kernel / Path |
Returns |
|---|---|---|
|
|
Padded output |
|
|
(output, residual) |
|
|
(output, residual) |
|
|
(FP8 output, scale) |
|
|
(MXFP4 output, scale) |
Default, no residual |
|
Output |
Default, with residual |
|
(output, residual) |
Constructor parameters:
RMSNorm(
dim: int,
eps: float = 1e-6,
x_pad_to_multiple: int = 0,
fused_allreduce: bool = False,
fused_quant: bool = False,
quant_config: Optional[QuantizationConfig] = None,
)
LayerNorm (layernorm.py)#
LayerNorm wraps layernorm2d_fwd and layernorm2d_fwd_with_add (with bias support):
LayerNorm(dim: int, eps: float = 1e-6)
Without residual:
layernorm2d_fwd(x, weight, bias, eps)With residual:
layernorm2d_fwd_with_add(out, x, residual, residual_out, weight, bias, eps)
Activation functions#
SiluAndMul (activation.py)#
SiluAndMul computes SiLU(x_first_half) * x_second_half. It splits the last dimension in half.
Condition |
Kernel |
Output |
|---|---|---|
|
|
|
|
|
|
Default |
|
BF16 output |
Constructor:
SiluAndMul(
fused_quant: bool = False,
quant_config: Optional[QuantizationConfig] = None,
)
Embedding and output head#
VocabParallelEmbedding (embed_head.py)#
Partitions the vocabulary across TP ranks. Each rank holds num_embeddings / tp_size rows.
Forward:
Mask input token IDs to this rank’s partition range
[vocab_start_idx, vocab_end_idx).F.embedding()on local partition.Zero out out-of-range positions.
all_reduce()across TP group.
ParallelLMHead (embed_head.py)#
Extends VocabParallelEmbedding for the output projection. Key differences:
Forward extracts only the last token per sequence during prefill (via
cu_seqlens_q[1:] - 1).Uses
tgemm.mm(x, self.weight, self.bias)for the logit computation (notF.linear).Calls
tensor_model_parallel_all_gather()to gather logits across TP ranks.
Rotary position embedding (RoPE)#
RotaryEmbedding (rotary_embedding.py)#
Precomputes cos/sin caches at initialization and applies RoPE in-place.
Constructor:
RotaryEmbedding(
head_size: int,
rotary_dim: int,
max_position_embeddings: int,
base: float,
is_neox_style: bool = True,
dtype: Optional[torch.dtype] = None,
)
Forward: Calls aiter.rope_cached_positions_2c_fwd_inplace(query_, key_, cos, sin, positions, rotate_style, ...) which applies RoPE to Q and K tensors in-place using precomputed caches indexed by position IDs.
get_rope() factory#
get_rope(head_size, rotary_dim, max_position, base, rope_scaling=None)
Returns a cached RotaryEmbedding instance. Currently rope_scaling must be None.
Integration in attention#
MHA (
attention_mha.py): RoPE is applied during therope_cache()phase, either via the fusedfused_qk_norm_rope_cache_quant_shufflekernel, viafused_qk_rope_reshape_and_cache, or via standalonerotary_emb(position, q, k).MLA (
attention_mla.py): RoPE is applied toq_peandk_ropetensors. During decode, this is fused intofused_qk_rope_concat_and_cache_mla. During prefill, it is applied viaself.rotary_emb(positions, prefill_q_pe, k_rope).
Sampling#
Sampler (sampler.py)#
Unified sampling supporting both greedy (temperature=0) and random (temperature>0) sampling in a single kernel call.
Forward:
def forward(self, logits, temperatures) -> sampled_tokens:
mixed_sample_outer_exponential(sampled_tokens, logits, exponential, temperatures, eps)
aiter.mixed_sample_outer_exponential performs temperature-scaled exponential sampling: it divides logits by temperature, then uses the Gumbel-max trick with pre-generated exponential random variates.
Fallback methods (currently unreachable due to early return):
greedy_sample():aiter.ops.triton.topk.topk(logits, 1)random_sample():aiter.ops.triton.softmax.softmax(logits)followed by exponential sampling andtopk.
Per-row noise. When a batch needs independent noise and has no top-k/top-p, the sampler draws a (num_tokens, vocab_size) exponential_ tensor (one noise row per token) and calls aiter’s fused_mixed_sample (ROCm/aiter#7416): the same race as mixed_sample_outer_exponential plus the greedy override (rows at or below SAMPLER_EPS take the lowest-id argmax), in one aiter call. That covers the speculative bonus token and exact-match verification rows of a non-greedy batch, any batch with n>1 fan-out siblings, and every unfiltered batch under --sampler-independent-noise that is not all greedy (an all-greedy batch takes the argmax first and draws no noise). A sampling row can differ from mixed_sample_outer_exponential only on a near tie of its two best race keys. Other unfiltered batches share one broadcast noise row and keep mixed_sample_outer_exponential + topk_select.
Required channel (triton_required_channel.py)#
Holds a request with a RequiredChannel (atom/sampling_params.py; today only
Kimi-K3 under tool_choice="required") to its tools channel by masking the
target logits before any sampler reads them. Until the channel opens, the end
ids (the format’s own plus the engine’s EOS and stop_token_ids) are -inf;
once the output ends with after (K3: <|close|>response<|sep|>), each next
row keeps only the forced id of opening. The channel is open once opening
is written, or once the model writes opening[:opens_at] itself (K3:
<|open|>tools<|sep|>); the state is then UNCONSTRAINED and the request
costs nothing more.
constrain_logitsmasks a step’s rows in place: plain decode and prefill rows by the request’s state, verification rows as if the drafts before them were accepted, and the bonus row after every draft. A mismatching draft is therefore rejected by the ordinary rejection sampler.advance_statesfolds the step’s emitted ids into each state on the GPU, so a deferred-output step reads its predecessor’s states throughdecode_srcbefore the host has seen those ids.
constrain_logits runs VOCAB_PROGRAMS (16) programs per request, each
writing every 16th 1,024-column block of a forced row without reading it;
advance_states runs one per request. On MI355X over K3’s vocabulary a step
with no forced row costs what one program per request did (5-7 us for a
DSpark7 verify step at bs 1-64), and one with all seven draft rows forced
15-52 us. A batch with no unopened channel launches neither kernel.
constrain_logits_reference / advance_states_reference (required_channel.py)
are the pure-torch twins the tests hold the kernels to.
RejectionSampler (rejection_sampler.py)#
Implements rejection sampling for speculative decoding (MTP, EAGLE, DSpark). Given the drafted token IDs, the target logits at each draft position and the bonus token sampled at each request’s last row, it accepts each request’s drafts until the first rejection and returns (output_token_ids, verdict), where verdict is [accepted, rejected, anchor row] per request. One of four rules runs per launch:
Exact match (default): a draft is accepted iff it equals the target’s own token at that row — the lowest-id argmax for greedy requests (
topk_select(..., tie="low")), the target’s temperature/top-k/top-p draw (Sampler.sample_verification_tokens) otherwise. On mismatch the target’s token is emitted. Kernel:rejection_greedy_sample_kernel.Probability ratio (
ATOM_SPEC_LOSSLESS_SAMPLING=1, DSpark drafts sampled fromq;ratio_verify.py): accept iffu * q(x) < p(x); on rejection draw fromrelu(p - q)by an exponential race over the vocab. Greedy rows of the same batch take exact match.p = softmax(logits / T)is never materialized: per-tile statistics (ratio_target_p.py,softmax_tiles.py) givep(x), and only a rejecting row is read across the vocab again.With top-k/top-p,
pisSampler’s filteredp_f, aiter’s kept set: every id at or above one floor logit per row, found on the device by a radix descent (sampling_filter_descent.py) or, for a top-k of at most 256 alone, a count (sampling_filter_count.py). Both agree with aiter to fp32 rounding, not bit for bit: aiter ranks by fp32p, which can tie two distinct logits near 0, and its top-p compares a sum againsttop_p.qcomes fromdspark_markov_sample(dspark_markov_sample.py): the top K entries (sparse_q_store.Q_WIDTH, 1024, cut further to the request’s top-k/top-p) ofsoftmax((base + W1[x_{k-1}] W2^T) / T), renormalized, drawn from exactly (sparse_q_select.py) and kept in a per-requestSparseQStore(sparse_q_store.py).qis 0 off the stored set;pis never truncated.A draft model with
lossless_block_verify(DeepSeek-V4.1) has each sampled request withqverified as one block (Sun et al. 2024,RatioVerifyInputs.block) rather than token by token: withw_0 = 1andw_i = min(w_{i-1} p(x_i) / q(x_i), 1), draftiis a stopping point with probabilityh_i = R_i / (R_i + 1 - w_i), whereR_i = sum_v relu(w_i p_{i+1}(v) - q_{i+1}(v))is the next position’s residual mass (h = wat the last draft); the request keeps its firsttau = max{i : u_i <= h_i}drafts and, short of the whole block, resamples positiontaufromrelu(w_tau p - q). The output law is the token rule’s and the expected number of accepted drafts is never lower.R_ireadsponly at the next row’s stored ids (qis 0 elsewhere, so the rest contributesw_i (1 - p(stored))). Greedy rows and requests withoutqkeep the token rule; Kimi-K3 keeps the token rule.No warmup or capture forward verifies, so
warmup_ratio_verifycompiles these kernels at startup.
Relaxed (
ATOM_ENABLE_RELAXED_MTP=1, all-greedy batches only; a sampled batch keeps exact match): accept a draft that is among the target’s topRELAXED_TOP_N(10) ids with probability withinRELAXED_DELTA(0.6) of the top-1’s; on rejection emit the top-1. Not distribution-preserving, soDSparkProposerrefuses it whereATOM_SPEC_LOSSLESS_SAMPLINGarms. Kernel:rejection_relaxed_sample_kernel.Synthetic (
--spec-decode-acceptance-length/-rate): forces a configured acceptance curve for benchmarking, ignoring the real comparison. Kernel:rejection_synthetic_sample_kernel.
Every rank must reach the same verdict. Each rule gets there its own way:
Probability ratio: Philox (
tl.rand) keyed on one SPMD step counter, the step each forward stages (DraftQStaging) and hands the verify inRatioVerifyInputs.step: the draft’s noise, the accept coins and the residual race each under their own high key word – so every TP rank draws the same coins without exchanging them.Exact match: the target’s own draw comes from
Samplerand is broadcast from TP rank 0 before the kernel compares against it.Synthetic: a per-device
torch.Generatorre-seeded from a step counter each step.Relaxed and greedy rows draw nothing.
Only the tokens are broadcast and every rank computes its own verdict, so every rule also needs the target logits bitwise equal across TP ranks; otherwise accepted counts differ while tokens follow rank 0, and next_token_locs, the KDA state commit and the KV release diverge silently. Minimum aiter: a build that includes ROCm/aiter#6954. From aiter 2039d2b96c (2026-09-13) until that PR, the custom all-reduce’s LL/LL128 paths summed ranks in a rank-dependent order.
Lossless speculative sampling (ATOM_SPEC_LOSSLESS_SAMPLING)#
The flag arms both halves at once: the DSpark draft samples its block from softmax(logits / T) and the verify accepts by the ratio. Either half alone is a regression: the ratio rule over a greedy q is exact match, and a sampled draft is matched less often than a greedy one.
Defaults off: both rules are target-exact, but they are not the same draw. A request’s
seeddoes not reach the draft’s or the verify’s draws: both are keyed on one SPMD step counter, so every TP rank draws alike without exchanging noise.Filters and
Sampler: once armed, everySamplerdraw keeps a tie group at the top-k cut whole, as aiter’s fused kernel does, rather than taking aiter’s top-k-first path (exactly k ids for a uniform top-k <= 256 with a top-p at a vocab >= 65536); on such a cut tokens can differ from the flag off. An engine that does not arm (a refused configuration, a P/D producer, another drafter) keeps aiter’s own law, and says why once at startup. The native fallback keeps exactly k, so the verify does not arm there.Which draft block runs: greedy rows keep exact-match acceptance. Armed with
data_parallel_size == 1,DSparkProposercaptures the block twice, sampled (block) and greedy (block_greedy), and a step in which no row draws aq(all-greedy, dummy, align-only, or every row mid-prompt;DraftQStaging.draws_q) replays the greedy one: the argmax op’s two launches per draft position, the same ids the sampled op drafts for such rows, and noqwritten. Any other step runs the sampled block for every row, greedy rows included: six launches per draft position (the histogram, cut and compact programs of a greedy row return at once). With DP > 1 only the sampled block is declared, as the choice is per rank while a replay is agreed across the DP group.ATOM_DSPARK_FUSED_MARKOV_SAMPLE=0selects the reference block on Kimi-K3 only in the greedy block.Sharded draft head: a flavor whose greedy block keeps the LM head vocab-sharded (DeepSeek-V4.1 under TP,
_sharded_markov_sample) keeps it sharded in the sampled block too (dspark_markov_sample_tp,select_and_draw_q_tp): each rank scores its own shard; the stage-1 tile partials are gathered, so the row max and argmax are the whole row’s; the level histograms are gathered, so every rank makes the same cut; each rank compacts only its own share of the stored entries into a[2, B, K]staging pair, and the gathered pairs are published to the store, each slot from the rank that owns it. The stored set and the draw are the whole-vocab op’s bit for bit, and per draft position three small gathers cross the ranks instead of the[B, V]logits. Where the greedy head gathers the logits, so does the sampled one.Memory, allocated before the KV cache is sized: the
qstore,2 * max_num_seqs * T * (8 * K + 8)bytes (128 MiB atmax_num_seqs=512, T=16, K=1024); the draft’s scratch, about 348 MiB at 512 (V=163840), of which 320 MiB are the[max_num_seqs, V]fp32 scores it keeps between its launches; the verify’s per-step scratch, about 191 MiB, of which 160 MiB are the run maxima of the top-k count (max_num_seqs * T * V / 8bytes,sampling_filter.CountRuns), which only a batch with some top-k of at most 256 and no top-p reads. TheLossless speculative sampling armedlog line prints the draft’s; the verify’s scratch isRejectionSampler’s (arm_ratio).Which K: any K is exact, as the draft samples only the stored entries; a smaller K gives up
q’s tail mass, i.e. some acceptance. The stored set is every id within 19 nats of the max, cut to K by 1/8-nat level (lowest ids first in the boundary level), which likewise only moves acceptance. K = V stores everything, for acceptance studies rather than throughput (~200-400 us per draft position on MI355X, vs ~5-20 us at 1024).Large batch: the select reads the fp32 score scratch twice (histogram, then compact). On MI355X (V=163840, K=1024) it takes 15.7/17.5/45.9/263.8 us per draft position at B=1/8/64/512 sampled rows, vs 46.4/50.3/56.7/141.1 us for
aiter.topk_selectplus the store: faster up to at least B=64, ~120 us per position slower at B=512, mostly the re-read.topk_selectis not a drop-in: its stream backend at large B returns ids in a nondeterministic order, so TP ranks would store different rows, and the per-request top-k/top-p cut needs sorted output. Like K, the select moves acceptance only, not the law.Where it does not arm: only a draft model with
supports_lossless_draft(today both Kimi-K3 DSpark drafts, MLA and GQA, and DeepSeek-V4.1 DSpark); others keep the greedy pair and say so once. Where it would arm,ATOM_ENABLE_RELAXED_MTPandsynthetic_acceptance_ratesare refused. It warns and keeps the greedy pair where the Markov tables cannot run the fused sampled op (markov_weights_unsupported), where the draft block is narrower than the verify, under PP (forward_varsis ringed per in-flight step, and the draft binds one slot’s filters), under PCP (draft ids are broadcast, theirqis not), on a P/D producer (it drafts for handoff only), or without aiter’s top-k/top-p ops. Where it arms on DeepSeek-V4.1, the mono hi path (hi_gate) and the gfx1250 single-launch step stand down, as their draft publishes noq. Read at drafter construction, so set it before the server starts.
Fused kernel chains#
ATOM uses fused kernels to reduce memory traffic by combining multiple operations into a single kernel launch.
Fused Operation |
Components |
Controlled By |
AITER Kernel |
|---|---|---|---|
RMSNorm + FP8 quant |
RMSNorm, per-tensor FP8 static quant |
|
|
RMSNorm + MXFP4 quant |
RMSNorm, per-1x32 MXFP4 quant |
|
|
RMSNorm + add + pad |
Residual add, RMSNorm, output padding |
|
|
AllReduce + RMSNorm |
TP all-reduce, RMSNorm |
|
|
SiLU + mul + FP8 quant |
SiLU activation, multiply, FP8 quant |
|
|
SiLU + mul + MXFP4 quant |
SiLU activation, multiply, MXFP4 quant |
|
|
QK norm + RoPE + cache + quant |
Q/K norm, RoPE, KV cache write, optional FP8 quant, weight shuffle |
|
|
RoPE + reshape + cache |
RoPE, K reshape, KV cache write |
Triton attention path |
|
QK RoPE + MLA cache |
Q RoPE, KV concat, MLA cache write, FP8 quant |
MLA decode path |
|
GEMM + split + cat (FP4) |
KV_b_proj GEMM, split K_nope/V, cat K_rope |
|
|
GEMM + split + cat (FP8) |
KV_b_proj GEMM, split K_nope/V, cat K_rope |
|
|
FP8 BMM + RoPE + cache (MLA) |
Batched FP8 BMM, RoPE, MLA KV cache write |
MLA decode with FP8 |
|
FP4 BMM + RoPE + cache (MLA) |
Batched FP4 BMM, RoPE, MLA KV cache write |
MLA decode with MXFP4 |
|
Source files#
atom/model_ops/#
File |
Description |
|---|---|
|
|
|
|
|
|
|
Top-level |
|
MHA implementation: prefill (flash), decode (ASM/Triton paged attention) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Helper utilities: |
atom/spec_decode/ (lossless sampling)#
File |
Description |
|---|---|
|
|
|
|
|
|
atom/model_ops/attentions/#
File |
Description |
|---|---|
|
|
|
|
|
|
atom/model_ops/attentions/pool_layout/ and .../token_layout/#
Two packages, two axes. pool_layout answers where a byte lives in the
cache pools and is a function of the config; token_layout answers where
this step’s tokens go and is a function of the batch, so it is rebuilt every
step. A backend consumes both and belongs to neither.
Membership has three parts, and only the third is machine-checkable. By
topic — the axis decides, not the dependencies. Arithmetic, not staging —
a member computes an answer and does not write or upload a forward_vars
mirror; page_unit_geometry is a mixin over self.model_runner and still
qualifies because it only reads, so taking self is not the line, having a
side effect on the step’s buffers is. Reachable without aiter or the rest
of atom, so a member is importable on a runner with no AITER build and no
GPU — CI is such a runner, and one import failure during collection aborts the
whole run rather than one test, so tests/test_layout_packages.py enforces
that part over both packages. Do not mistake the checkable part for the rule.
Two absences are deliberate. v4_kernels/pool_index.py is
v4_pool_geometry’s device-side half — the same row formulas as @triton.jit
device functions for the eight kernels that address the pool — and stays with
the kernels; tests/test_pool_index.py pins the two sides together. And a
per-token shape that only one caller has stays with that caller: V4’s decode
positions are ragged, and the one-token decode slot mapping comes from
last_block_num_tokens rather than from a position.
File |
Description |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
atom/model_ops/fused_moe/#
File |
Description |
|---|---|
|
|
|
|
|
|
|
|
|
MoE utility functions |
atom/utils/#
File |
Description |
|---|---|
|
|