Skip to navigation

nemo_automodel.components.models.qwen3_moe.layers

View as MarkdownOpen in Claude

Module Contents

Classes

NameDescription
Qwen3MoeAttentionQwen3 MoE attention with query/key RMSNorm, RoPE, and TE/SDPA/FA4 backends.

Data

logger

API

class nemo_automodel.components.models.qwen3_moe.layers.Qwen3MoeAttention(
config: transformers.models.qwen3_moe.configuration_qwen3_moe.Qwen3MoeConfig,
)

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]
head_dim
k_norm
k_proj
num_heads
= config.num_attention_heads
num_kv_heads
= config.num_key_value_heads
o_proj
q_norm
q_proj
v_proj
nemo_automodel.components.models.qwen3_moe.layers.Qwen3MoeAttention._forward_impl(
x: torch.Tensor,
freqs_cis: torch.Tensor,
attention_mask: torch.Tensor | None = None,
attn_kwargs: typing.Any = {}
) -> torch.Tensor

Project hidden states and execute the explicitly selected attention backend.

Parameters:

x
torch.Tensor

Hidden states [batch, sequence, hidden] or [tokens, hidden].

freqs_cis
torch.Tensor

RoPE tables matching x’s token axes; fused TE tables use [sequence, 1, 1, rotary_dim].

attention_mask
torch.Tensor | NoneDefaults to None

Optional [batch, sequence] padding/document IDs or [batch, 1, sequence, sequence] dense mask.

**attn_kwargs
AnyDefaults to {}

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.

nemo_automodel.components.models.qwen3_moe.layers.Qwen3MoeAttention._project_output(
out: torch.Tensor
) -> torch.Tensor

Project attention output after FA4 has restored padded token positions.

Parameters:

out
torch.Tensor

Attention output [batch, sequence, heads * head_dim] or [tokens, heads * head_dim].

Returns: torch.Tensor

Hidden states [batch, sequence, hidden] or [tokens, hidden].

nemo_automodel.components.models.qwen3_moe.layers.Qwen3MoeAttention._project_qkv(
x: torch.Tensor,
freqs_cis: torch.Tensor,
cu_seqlens: torch.Tensor | None = None,
cp_size: int = 1,
cp_rank: int = 0
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]

Project, normalize and rotate Q/K while retaining the padded token axes.

Parameters:

x
torch.Tensor

Hidden states [batch, sequence, hidden] or [tokens, hidden].

freqs_cis
torch.Tensor

RoPE tables matching x’s token axes; fused TE tables use [sequence, 1, 1, rotary_dim].

cu_seqlens
torch.Tensor | NoneDefaults to None

Optional int32 [documents + 1] offsets for fused THD RoPE.

cp_size
intDefaults to 1

Context-parallel group size.

cp_rank
intDefaults to 0

Context-parallel rank.

Returns: torch.Tensor

Q, K and V with x’s leading token axes followed by [heads, head_dim];

nemo_automodel.components.models.qwen3_moe.layers.Qwen3MoeAttention.forward(
x: torch.Tensor,
freqs_cis: torch.Tensor,
attention_mask: torch.Tensor | None = None,
attn_kwargs: typing.Any = {}
) -> torch.Tensor

Apply attention to batched or THD hidden states.

Parameters:

x
torch.Tensor

Hidden states [batch, sequence, hidden] or [tokens, hidden].

freqs_cis
torch.Tensor

RoPE tables [batch, sequence, rotary_dim] or [tokens, rotary_dim]; fused TE RoPE uses [sequence, 1, 1, rotary_dim].

attention_mask
torch.Tensor | NoneDefaults to None

Optional [batch, sequence] padding/document IDs or [batch, 1, sequence, sequence] dense mask accepted by the backend.

**attn_kwargs
AnyDefaults to {}

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.

nemo_automodel.components.models.qwen3_moe.layers.Qwen3MoeAttention.init_weights(
buffer_device: torch.device,
init_std: float = 0.02
)
nemo_automodel.components.models.qwen3_moe.layers.logger = logging.getLogger(__name__)