nemo_automodel.components.models.qwen3_5.model
nemo_automodel.components.models.qwen3_5.model
Qwen3.5 dense causal LM with Megatron-style MTP support.
Module Contents
Classes
Functions
Data
API
Bases: Qwen3_5TextRotaryEmbedding
Ensure inv_freq stays in float32 across .to(dtype) calls.
Bases: CausalLMOutputWithPast
Qwen3.5 causal-LM output extended with MTP auxiliary hidden states.
Bases: Block
Qwen3.5 dense decoder block on top of the Qwen3-Next Block.
Identical to Qwen3_5MoeBlock except the MLP degrades to a dense MLP
(no experts). The CP-aware GatedDeltaNet is built natively for
linear-attention layers, and the forward threads NEAT-packing kwargs.
Bases: Qwen3_5DecoderLayer
One full-attention Qwen3.5 dense MTP sublayer.
Bases: Module
Qwen3.5 dense text decoder rebuilt on the Qwen3-Next Block.
Native counterpart of Qwen3_5MoeTextModelBackend for the dense model:
reuses the same blocks/GatedDeltaNet/norm/rotary so dense and MoE share one
code path, with the fp32 SSMGate built at construction (no runtime patch).
Bases: HFCheckpointingMixin, Module
Qwen3.5 dense causal LM with optional Megatron-style MTP head.
Bases: HFCheckpointingMixin, HFQwen3_5ForConditionalGeneration
Qwen3.5/Qwen3.6 dense VLM with optional Megatron-style MTP head.
The base VLM stays on the upstream HF implementation so image/video feature insertion, M-RoPE position handling, and generation helpers remain intact. MTP is added as an auxiliary train-time module over the final language hidden states, matching the dense text-only MTP architecture.
Embed token ids and splice image/video features into the embeddings.
The VLM->LM multimodal scatter runs on the full (unsharded) sequence
inside the forward before the CP sequence shard, so it is identical to
the pre-CP-refactor pre-embed. Vision features may be frame-sharded
across the CP group before get_placeholder_mask scatters them into
the full input_ids sequence.
Parameters:
Token ids [batch, sequence] (full, unsharded).
Optional packed image patches for HF vision encoding.
Optional packed video patches.
Per-image [num_images, 3] T/H/W grid.
Per-video [num_videos, 3] T/H/W grid.
Returns: torch.Tensor
inputs_embeds of shape [batch, sequence, hidden] with image /
Encode one modality’s patches into flat, entry-ordered visual tokens under CP.
Under context parallelism the vision tower otherwise runs redundantly on the full,
un-sharded patch set on every CP rank. When the CP vision-frame sharding group is published
(see :func:~nemo_automodel.components.distributed.cp_vision_frame_shard.set_cp_vision_group),
route the single self.model.visual call through
:func:~nemo_automodel.components.distributed.cp_vision_frame_shard.maybe_distribute_visual,
which shards the frames across the CP group and all-gathers the per-frame embeds in
original entry order. Its pooler_output is the flat, already-concatenated tensor,
identical to torch.cat(get_image_features(...).pooler_output, dim=0) (HF
get_image_features splits that same flat tensor per entry). When sharding is
disabled / inactive this is the exact replicated get_image_features /
get_video_features path.
Parameters:
Patch rows of shape [total_patch_rows, patch_dim] for ALL entries
of one modality, frame-contiguous in entry order.
[num_entries, 3] tensor of per-entry (t, h, w) grid sizes.
Select the video (True) vs image (False) replicated fallback.
Returns: torch.Tensor
[total_patch_rows / spatial_merge_size**2, hidden] flat merged-token embeddings
Per-stage input/output meta tensors for the PP schedule’s shape inference.
Matches the framework default (first stage consumes full token ids
[mb, seq]; later stages consume hidden states; the last stage owning
lm_head emits logits, earlier stages emit hidden states) except that
under context parallelism the first stage embeds the full sequence and
shards it in forward, so every stage output and later-stage input carries
the LOCAL (padded-to-2*cp then //cp) sequence length while the
first-stage input stays full-length. At cp_size == 1 this reduces to
the default symmetric shapes.
Return a sharder-only CP backend plus the full-sequence mRoPE positions.
Embedding and the VLM->LM multimodal scatter now run inside forward
per microbatch (see :meth:_embed_and_splice_for_cp), so this hook only
(a) computes the mRoPE position_ids on the full (unsharded) sequence
via get_rope_index and returns them for :func:shard_batch_aux_only
to round-robin-shard on the mRoPE axis, and (b) returns the
:class:ContextParallelSharder. input_ids and the media inputs are
left in the batch for the forward; mm_token_type_ids is consumed here
(only get_rope_index needs it) so the sharded forward never sees a
full-length copy.
Parameters:
The batch dict (with input_ids and optional multimodal
keys). input_ids is [batch, sequence].
Accepted for hook-signature parity; unused (round-robin CP).
Tie lm_head to the active VLM text embedding when requested.
Bases: HFQwen3_5Model
Thin VLM wrapper exposing language_model internals as properties and
routing the forward: HF vision+scatter path when media is present, else the
NeMo dense backbone directly. Mirrors Qwen3_5MoeModel.
Trivial MoEConfig for the dense Qwen3.5 backbone.
The dense model has no experts (num_experts is 0/absent), so Block
builds a dense MLP and never consults this config; it is only required to
satisfy Block.__init__’s signature.
Build a 4D block-causal attention mask from an indexed packing mask.
packing_mask is [B, S] with the 1-based document index per token
(0 = padding). The returned bool mask [B, 1, S, S] (True = attend)
keeps attention causal and within each packed document, matching the
backbone’s packed-sequence semantics. Used for the MTP sublayers, which run
SDPA self-attention over the same packed batch (NVBugs 6330129).
Return a Qwen3.5 backend with TE fused RoPE disabled.
Qwen3.5 VLM training can feed full-attention layers in packed/THD shape via the shared Qwen3-Next attention block. TE fused RoPE expects 4D inputs there, so keep the non-fused RoPE path while preserving the rest of the backend selection (TE Linear, attention backend, etc.).
Build Qwen3.5 MTP runtime config from HF-style config fields.
Construct dense Qwen3.5 MTP blocks.
Qwen3.5 MTP follows Megatron Bridge: each depth is one full-attention Qwen3.5 decoder block, regardless of the backbone’s GatedDeltaNet layers.