Skip to navigation

nemo_automodel.components.models.qwen3_moe.model

View as MarkdownOpen in Claude

Module Contents

Classes

Data

ModelClass

API

class nemo_automodel.components.models.qwen3_moe.model.Block(
layer_idx: int,
config: transformers.models.qwen3_moe.configuration_qwen3_moe.Qwen3MoeConfig,
)

Bases: Module

input_layernorm
mlp
= MoE(moe_config, backend)
post_attention_layernorm
self_attn
= Qwen3MoeAttention(config, backend)
nemo_automodel.components.models.qwen3_moe.model.Block._mlp(
x: torch.Tensor,
padding_mask: torch.Tensor | None
) -> torch.Tensor
nemo_automodel.components.models.qwen3_moe.model.Block.forward(
x: torch.Tensor,
freqs_cis: torch.Tensor,
attention_mask: torch.Tensor | None = None,
padding_mask: torch.Tensor | None = None,
attn_kwargs: typing.Any = {}
) -> torch.Tensor
nemo_automodel.components.models.qwen3_moe.model.Block.init_weights(
buffer_device: torch.device
)
class nemo_automodel.components.models.qwen3_moe.model.Qwen3MoeForCausalLM(
config: transformers.models.qwen3_moe.configuration_qwen3_moe.Qwen3MoeConfig,
logits_dtype: torch.dtype | None = None,
moe_overrides: dict | None = None
)

Bases: HFCheckpointingMixin, Module, MoEFSDPSyncMixin

_owns_packed_attention
bool

Declare only this model’s FA4 THD input path; other backends keep their contracts.

_pp_keep_self_forward
bool = True
_uses_native_fa4
bool = True
backend
= backend or BackendConfig()
lm_head
model
requires_thd_batch_preparation
bool

Request framework-owned raw token/label flattening for the FA4 path.

This requests CP1 batch conversion only, not context or pipeline parallelism. TE retains its existing backend-owned preparation.

state_dict_adapter
tie_word_embeddings_support
TieSupport = TieSupport.UNTIED_ONLY
nemo_automodel.components.models.qwen3_moe.model.Qwen3MoeForCausalLM.forward(
input_ids: torch.Tensor,
position_ids: torch.Tensor | None = None,
attention_mask: torch.Tensor | None = None,
padding_mask: torch.Tensor | None = None,
logits_to_keep: typing.Union[int, torch.Tensor] = 0,
output_hidden_states: bool | None = None,
attn_kwargs: typing.Any = {}
) -> transformers.modeling_outputs.CausalLMOutputWithPast

Compute logits while preserving packed document and padding metadata.

Parameters:

input_ids
torch.Tensor

Token IDs of shape [batch, sequence], or [1, tokens] for pre-packed THD input. Pipeline stages after the first receive hidden states of shape [batch, sequence, hidden] in this slot.

position_ids
torch.Tensor | NoneDefaults to None

Optional document-local positions of shape [batch, sequence], or [1, tokens] for THD input.

attention_mask
torch.Tensor | NoneDefaults to None

Optional tensor of shape [batch, sequence] holding either a padding mask (nonzero at valid tokens) or packed 1-based document IDs with zero at padding, or a dense mask of shape [batch, 1, sequence, sequence] for backends that accept one.

padding_mask
torch.Tensor | NoneDefaults to None

Optional boolean tensor of shape [batch, sequence], true at padding. Native FA4 derives its valid-token mask from it when attention_mask is None and some token is padding; all-valid batches use dense FA4.

logits_to_keep
Union[int, torch.Tensor]Defaults to 0

Number of trailing positions to project, or position indices of shape [selected_tokens].

output_hidden_states
bool | NoneDefaults to None

Whether to include final hidden states.

**attn_kwargs
AnyDefaults to {}

Backend metadata. Native FA4 packing passes int64 packed_token_indices of shape [batch, sequence] (row-local token indices, -1 at padding), int32 cu_seqlens of shape [batch, max_documents + 1] (row-local offsets, -1 at unused entries) and an integer max_seqlen. The model-entry hook flattens them to indices of shape [tokens] into the flattened [batch * sequence] axis and offsets of shape [documents + 1]; already flattened metadata passes through unchanged.

Returns: CausalLMOutputWithPast

CausalLMOutputWithPast whose logits have shape [batch, sequence,

nemo_automodel.components.models.qwen3_moe.model.Qwen3MoeForCausalLM.from_config(
config: transformers.models.qwen3_moe.configuration_qwen3_moe.Qwen3MoeConfig,
kwargs = {}
)
classmethod
nemo_automodel.components.models.qwen3_moe.model.Qwen3MoeForCausalLM.from_pretrained(
pretrained_model_name_or_path: str,
model_args = (),
kwargs = {}
)
classmethod
nemo_automodel.components.models.qwen3_moe.model.Qwen3MoeForCausalLM.get_input_embeddings()
nemo_automodel.components.models.qwen3_moe.model.Qwen3MoeForCausalLM.get_output_embeddings()
nemo_automodel.components.models.qwen3_moe.model.Qwen3MoeForCausalLM.initialize_weights(
buffer_device: torch.device | None = None,
dtype: torch.dtype = torch.bfloat16
) -> None
nemo_automodel.components.models.qwen3_moe.model.Qwen3MoeForCausalLM.set_input_embeddings(
value
)
nemo_automodel.components.models.qwen3_moe.model.Qwen3MoeForCausalLM.set_output_embeddings(
new_embeddings
)
class nemo_automodel.components.models.qwen3_moe.model.Qwen3MoeModel(
config: transformers.models.qwen3_moe.configuration_qwen3_moe.Qwen3MoeConfig,
moe_overrides: dict | None = None
)

Bases: Module

embed_tokens
head_dim
layers
= torch.nn.ModuleDict()
max_seq_len
= config.max_position_embeddings
moe_config
= moe_config or MoEConfig(**moe_defaults)
norm
rotary_emb
nemo_automodel.components.models.qwen3_moe.model.Qwen3MoeModel.forward(
input_ids: torch.Tensor,
position_ids: torch.Tensor | None = None,
attention_mask: torch.Tensor | None = None,
padding_mask: torch.Tensor | None = None,
attn_kwargs: typing.Any = {}
) -> torch.Tensor

Run decoder layers, preparing private FA4 THD metadata once per model call.

Parameters:

input_ids
torch.Tensor

Token IDs of shape [batch, sequence] or physical THD token IDs of shape [1, tokens], squeezed to [tokens] for the layers. Pipeline stages after the first receive hidden states of shape [batch, sequence, hidden] or [1, tokens, hidden] in this slot.

position_ids
torch.Tensor | NoneDefaults to None

Optional positions with the token layout of input_ids.

attention_mask
torch.Tensor | NoneDefaults to None

Optional [batch, sequence] padding mask or packed document IDs, or a [batch, 1, sequence, sequence] dense mask. None for THD.

padding_mask
torch.Tensor | NoneDefaults to None

Optional bool mask with the token layout of input_ids, True at padding, including physical THD padding between documents.

**attn_kwargs
AnyDefaults to {}

Backend metadata. THD supplies int32 cu_seqlens and cu_seqlens_padded of shape [documents + 1]. For FA4 THD this adds private compact offsets of shape [documents + 1] and int64 packed_token_indices of shape [valid_tokens] into the physical [tokens] axis, shared by every layer. Inputs are not mutated.

Returns: torch.Tensor

Hidden states of shape [batch, sequence, hidden], or [1, tokens, hidden] for THD input.

nemo_automodel.components.models.qwen3_moe.model.Qwen3MoeModel.init_weights(
buffer_device: torch.device | None = None
) -> None
nemo_automodel.components.models.qwen3_moe.model.ModelClass = Qwen3MoeForCausalLM