nemo_automodel.components.models.qwen3_moe.layers
nemo_automodel.components.models.qwen3_moe.layers
Module Contents
Classes
Data
API
Bases: Module
Qwen3 MoE attention with query/key RMSNorm, RoPE, and TE/SDPA/FA4 backends.
Shapes:
- Input: x -> [B, S, H]
- Projections: q: [B, S, n_heads, head_dim] k/v: [B, S, n_kv_heads, head_dim] -> repeated to n_heads via groups
- Output: [B, S, H]
Project hidden states and execute the explicitly selected attention backend.
Parameters:
Hidden states [batch, sequence, hidden] or [tokens, hidden].
RoPE tables matching x’s token axes; fused TE tables use [sequence, 1, 1, rotary_dim].
Optional [batch, sequence] padding/document IDs or [batch, 1, sequence, sequence] dense mask.
Backend metadata. Packed FA4 uses int32 cu_seqlens of shape [documents + 1] and int64 packed_token_indices of shape [tokens], indexing the flattened [batch * sequence] axis. FA4 THD instead uses private compact offsets of shape [documents + 1] and indices of shape [valid_tokens] into the physical [tokens] axis, prepared once at model entry.
Returns: torch.Tensor
Hidden states with the same shape and dtype as x.
Project attention output after FA4 has restored padded token positions.
Parameters:
Attention output [batch, sequence, heads * head_dim] or [tokens, heads * head_dim].
Returns: torch.Tensor
Hidden states [batch, sequence, hidden] or [tokens, hidden].
Project, normalize and rotate Q/K while retaining the padded token axes.
Parameters:
Hidden states [batch, sequence, hidden] or [tokens, hidden].
RoPE tables matching x’s token axes; fused TE tables use [sequence, 1, 1, rotary_dim].
Optional int32 [documents + 1] offsets for fused THD RoPE.
Context-parallel group size.
Context-parallel rank.
Returns: torch.Tensor
Q, K and V with x’s leading token axes followed by [heads, head_dim];
Apply attention to batched or THD hidden states.
Parameters:
Hidden states [batch, sequence, hidden] or [tokens, hidden].
RoPE tables [batch, sequence, rotary_dim] or [tokens, rotary_dim]; fused TE RoPE uses [sequence, 1, 1, rotary_dim].
Optional [batch, sequence] padding/document IDs or [batch, 1, sequence, sequence] dense mask accepted by the backend.
Backend metadata. Packed FA4 uses int32 cu_seqlens of shape [documents + 1] and int64 packed_token_indices of shape [tokens], indexing the flattened [batch * sequence] axis. FA4 THD instead uses private compact offsets of shape [documents + 1] and indices of shape [valid_tokens] into the physical [tokens] axis, prepared once at model entry.
Returns: torch.Tensor
Hidden states with the same shape and dtype as x.