Skip to navigation

nemo_automodel.components.models.qwen3_omni_moe.model

View as MarkdownOpen in Claude

Qwen3-Omni-MoE Thinker on the NeMo MoE text backbone.

Qwen3OmniMoeThinkerForConditionalGeneration.get_audio_features, get_placeholder_mask, get_image_features and get_video_features are adapted from the methods of the same names on Qwen3OmniMoeThinkerModel in Hugging Face Transformers (Apache-2.0), release v5.22.2, commit 6d195c6262a0ac2ef44f7b9a308617562286ff45, file src/transformers/models/qwen3_omni_moe/modeling_qwen3_omni_moe.py: https://github.com/huggingface/transformers/blob/6d195c6262a0ac2ef44f7b9a308617562286ff45/src/transformers/models/qwen3_omni_moe/modeling_qwen3_omni_moe.py

Module Contents

Classes

NameDescription
Qwen3OmniMoeThinkerForConditionalGenerationQwen3OmniMoe Thinker for Conditional Generation with multimodal support.
Qwen3OmniMoeThinkerTextModelQwen3OmniMoe Thinker Text Model with MRoPE and sparse MoE layers.
Qwen3OmniMoeThinkerTextRotaryEmbeddingEnsure HF rotary frequency buffers stay fp32 across module-wide casts.

Data

ModelClass

API

class nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerForConditionalGeneration(
config: transformers.models.qwen3_omni_moe.configuration_qwen3_omni_moe.Qwen3OmniMoeThinkerConfig,
logits_dtype: torch.dtype | None = None,
moe_overrides: dict | None = None
)

Bases: HFCheckpointingMixin, HFQwen3OmniMoeThinkerForConditionalGeneration, MoEFSDPSyncMixin

Qwen3OmniMoe Thinker for Conditional Generation with multimodal support.

audio_tower
lm_head
model
num_experts
= text_config.num_experts
num_experts_per_tok
= text_config.num_experts_per_tok
pad_token_id
router_aux_loss_coef
= getattr(text_config, 'router_aux_loss_coef', 0.0)
spatial_merge_size
state_dict_adapter
tie_word_embeddings_support
TieSupport = TieSupport.UNTIED_ONLY
visual
vocab_size
= text_config.vocab_size
nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerForConditionalGeneration.forward(
input_ids: torch.Tensor,
input_features: torch.FloatTensor | None = None,
pixel_values: torch.FloatTensor | None = None,
pixel_values_videos: torch.FloatTensor | None = None,
image_grid_thw: torch.LongTensor | None = None,
video_grid_thw: torch.LongTensor | None = None,
attention_mask: torch.Tensor | None = None,
feature_attention_mask: torch.LongTensor | None = None,
audio_feature_lengths: torch.LongTensor | None = None,
position_ids: torch.Tensor | None = None,
padding_mask: torch.Tensor | None = None,
inputs_embeds: torch.FloatTensor | None = None,
labels: torch.LongTensor | None = None,
output_router_logits: bool | None = None,
use_audio_in_video: bool | None = None,
video_second_per_grid: torch.Tensor | None = None,
logits_to_keep: typing.Union[int, torch.Tensor] = 0,
output_hidden_states: bool | None = None,
attn_kwargs: typing.Any = {}
) -> torch.Tensor | dict | transformers.modeling_outputs.CausalLMOutputWithPast

Forward pass with multimodal fusion.

Parameters:

input_ids
torch.Tensor

Integer token IDs of shape [batch, sequence], or packed (THD) IDs of shape [1, tokens], which this method squeezes to [tokens].

input_features
torch.FloatTensor | NoneDefaults to None

Audio features of shape [audios, mel_bins, frames].

pixel_values
torch.FloatTensor | NoneDefaults to None

Packed image patches of shape [image_patches, patch_features], where patch_features flattens channels, temporal depth, height, and width.

pixel_values_videos
torch.FloatTensor | NoneDefaults to None

Packed video patches of shape [video_patches, patch_features].

image_grid_thw
torch.LongTensor | NoneDefaults to None

Integer temporal/height/width grids of shape [images, 3].

video_grid_thw
torch.LongTensor | NoneDefaults to None

Integer temporal/height/width grids of shape [videos, 3].

attention_mask
torch.Tensor | NoneDefaults to None

Padding mask of shape [batch, sequence].

feature_attention_mask
torch.LongTensor | NoneDefaults to None

Valid audio frames of shape [audios, frames].

audio_feature_lengths
torch.LongTensor | NoneDefaults to None

Valid frame counts of shape [audios].

position_ids
torch.Tensor | NoneDefaults to None

Rotary coordinates of shape [3, batch, sequence].

padding_mask
torch.Tensor | NoneDefaults to None

Optional bool mask of shape [batch, sequence], or [1, tokens] when packed, true at padding; the decoder ignores it and derives padding from attention_mask.

inputs_embeds
torch.FloatTensor | NoneDefaults to None

Optional embeddings of shape [batch, sequence, hidden].

labels
torch.LongTensor | NoneDefaults to None

Target token IDs of shape [batch, sequence].

output_router_logits
bool | NoneDefaults to None

Whether to output router logits

use_audio_in_video
bool | NoneDefaults to None

Whether audio is in video

video_second_per_grid
torch.Tensor | NoneDefaults to None

Temporal grid spacing of shape [videos].

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

Integer count or index tensor of shape [selected_positions]. If > 0, only compute logits for the last logits_to_keep token positions (0 = all positions). Enables memory-efficient fused cross-entropy by letting the recipe request a single-position lm_head projection alongside the final hidden states.

output_hidden_states
bool | NoneDefaults to None

When set, the returned output carries the final hidden states (the input to lm_head) so the recipe can run fused linear cross-entropy.

**attn_kwargs
AnyDefaults to {}

Additional attention arguments

Returns: torch.Tensor | dict | CausalLMOutputWithPast

With labels or output_router_logits, a dict holding logits, a scalar loss when labels are

nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerForConditionalGeneration.from_config(
config: transformers.models.qwen3_omni_moe.configuration_qwen3_omni_moe.Qwen3OmniMoeThinkerConfig,
kwargs = {}
)
classmethod
nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerForConditionalGeneration.from_pretrained(
pretrained_model_name_or_path: str,
model_args = (),
kwargs = {}
)
classmethod
nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerForConditionalGeneration.get_audio_features(
input_features: torch.Tensor,
feature_attention_mask: torch.Tensor | None = None,
audio_feature_lengths: torch.Tensor | None = None,
kwargs: transformers.processing_utils.Unpack[transformers.utils.TransformersKwargs] = {}
) -> transformers.modeling_outputs.BaseModelOutputWithPooling

Return audio features, calling the audio tower directly.

transformers 5.22’s inherited implementation delegates to self.model (the text model), which has no audio tower; call self.audio_tower here instead.

Parameters:

input_features
torch.Tensor

Tensor of shape [batch, mel_bins, frames] with log-mel features.

feature_attention_mask
torch.Tensor | NoneDefaults to None

Tensor of shape [batch, frames], 1 for valid frames. The clip lengths always come from this mask, so it must be given.

audio_feature_lengths
torch.Tensor | NoneDefaults to None

Unused, as in Transformers, which also recomputes the lengths from feature_attention_mask.

**kwargs
Unpack[TransformersKwargs]Defaults to {}

Forwarded to the audio tower.

Returns: BaseModelOutputWithPooling

The audio tower output. last_hidden_state and pooler_output are the same

nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerForConditionalGeneration.get_image_features(
pixel_values: torch.Tensor,
image_grid_thw: torch.Tensor,
kwargs: transformers.processing_utils.Unpack[transformers.utils.TransformersKwargs] = {}
) -> transformers.models.qwen3_omni_moe.modeling_qwen3_omni_moe.BaseModelOutputWithDeepstackFeatures

Return vision features for images, calling the vision tower directly.

transformers 5.22’s inherited implementation delegates to self.model (the text model), which has no vision tower; call self.visual here instead.

Parameters:

pixel_values
torch.Tensor

Tensor of shape [patches, patch_dim] with the flattened patches of all images, where patch_dim is channels * temporal_patch_size * patch_size**2.

image_grid_thw
torch.Tensor

Tensor of shape [images, 3] with each image’s temporal, height and width patch counts.

**kwargs
Unpack[TransformersKwargs]Defaults to {}

Forwarded to the vision tower.

Returns: BaseModelOutputWithDeepstackFeatures

The vision tower output. pooler_output is a tuple with one Tensor of shape

nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerForConditionalGeneration.get_input_embeddings()
nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerForConditionalGeneration.get_output_embeddings()
nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerForConditionalGeneration.get_placeholder_mask(
input_ids: torch.Tensor | None,
inputs_embeds: torch.Tensor,
image_features: torch.Tensor | None = None,
video_features: torch.Tensor | None = None
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]

Obtains multimodal placeholder mask from input_ids or inputs_embeds.

Copied from transformers 5.22; the inherited accessor now lives on the text model (self.model), which does not carry the token ids, so it is defined here on the thinker.

Parameters:

input_ids
torch.Tensor | None

Tensor of shape [batch, sequence], or None to find the placeholders by comparing inputs_embeds with their embeddings.

inputs_embeds
torch.Tensor

Tensor of shape [batch, sequence, hidden].

image_features
torch.Tensor | NoneDefaults to None

Tensor of shape [image_tokens, hidden]; its size must match the number of image placeholders.

video_features
torch.Tensor | NoneDefaults to None

Tensor of shape [video_tokens, hidden]; its size must match the number of video placeholders.

Returns: torch.Tensor

The image, video and audio placeholder masks, each a bool Tensor of shape

nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerForConditionalGeneration.get_video_features(
pixel_values_videos: torch.Tensor,
video_grid_thw: torch.Tensor,
kwargs: transformers.processing_utils.Unpack[transformers.utils.TransformersKwargs] = {}
) -> transformers.models.qwen3_omni_moe.modeling_qwen3_omni_moe.BaseModelOutputWithDeepstackFeatures

Return vision features for videos, calling the vision tower directly.

See get_image_features for why this bypasses the 5.22 self.model delegation.

Parameters:

pixel_values_videos
torch.Tensor

Tensor of shape [patches, patch_dim] with the flattened patches of all videos, where patch_dim is channels * temporal_patch_size * patch_size**2.

video_grid_thw
torch.Tensor

Tensor of shape [videos, 3] with each video’s temporal, height and width patch counts.

**kwargs
Unpack[TransformersKwargs]Defaults to {}

Forwarded to the vision tower.

Returns: BaseModelOutputWithDeepstackFeatures

The vision tower output. pooler_output is a tuple with one Tensor of shape

nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerForConditionalGeneration.initialize_weights(
buffer_device: torch.device | None = None,
dtype: torch.dtype = torch.bfloat16
) -> None
nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerForConditionalGeneration.set_input_embeddings(
value
)
nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerForConditionalGeneration.set_output_embeddings(
new_embeddings
)
class nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerTextModel(
config: transformers.models.qwen3_omni_moe.configuration_qwen3_omni_moe.Qwen3OmniMoeTextConfig,
moe_overrides: dict | None = None
)

Bases: Module

Qwen3OmniMoe Thinker Text Model with MRoPE and sparse MoE layers.

embed_tokens
layers
moe_config
= moe_config or MoEConfig(**moe_defaults)
norm
padding_idx
= getattr(config, 'pad_token_id', None)
rotary_emb
= Qwen3OmniMoeThinkerTextRotaryEmbedding(config)
vocab_size
= config.vocab_size
nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerTextModel._deepstack_process(
hidden_states,
visual_pos_masks,
visual_embeds
)
nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerTextModel.forward(
input_ids: torch.Tensor | None = None,
inputs_embeds: torch.Tensor | None = None,
position_ids: torch.Tensor | None = None,
attention_mask: torch.Tensor | None = None,
padding_mask: torch.Tensor | None = None,
visual_pos_masks: torch.Tensor | None = None,
deepstack_visual_embeds: list[torch.Tensor] | None = None,
attn_kwargs: typing.Any = {}
) -> torch.Tensor

Run the thinker text decoder; packed (THD) input comes back batched.

Parameters:

input_ids
torch.Tensor | NoneDefaults to None

Optional token IDs of shape [batch, sequence], or packed [tokens].

inputs_embeds
torch.Tensor | NoneDefaults to None

Optional embeddings of shape [batch, sequence, hidden], or packed [tokens, hidden].

position_ids
torch.Tensor | NoneDefaults to None

Optional positions of shape [batch, sequence] or [axes, batch, sequence], with 3 multimodal rotary axes (a 4th leading axis is dropped); packed input passes the thinker’s squeezed positions.

attention_mask
torch.Tensor | NoneDefaults to None

Tensor of shape [batch, sequence], 1 at valid tokens and 0 at padding; the decoder derives the MoE padding mask from it.

padding_mask
torch.Tensor | NoneDefaults to None

Ignored; the MoE padding mask is derived from attention_mask.

visual_pos_masks
torch.Tensor | NoneDefaults to None

Optional bool mask of shape [batch, sequence, 1], true at visual placeholder tokens; only its […, 0] slice is read.

deepstack_visual_embeds
list[torch.Tensor] | NoneDefaults to None

Optional list with one Tensor of shape [visual_tokens, hidden] per DeepStack level (https://arxiv.org/abs/2406.04334), taken from successive vision-encoder layers; entry i is added to the visual-token hidden states after decoder layer i.

**attn_kwargs
AnyDefaults to {}

Backend metadata, including packed sequence boundaries.

Returns: torch.Tensor

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

nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerTextModel.init_weights(
buffer_device: torch.device | None = None
) -> None
class nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerTextRotaryEmbedding()

Bases: HFQwen3OmniMoeThinkerTextRotaryEmbedding

Ensure HF rotary frequency buffers stay fp32 across module-wide casts.

nemo_automodel.components.models.qwen3_omni_moe.model.Qwen3OmniMoeThinkerTextRotaryEmbedding._apply(
fn: typing.Any,
recurse: bool = True
)
nemo_automodel.components.models.qwen3_omni_moe.model.ModelClass = Qwen3OmniMoeThinkerForConditionalGeneration