nemo_automodel.components.models.mistral4.model
nemo_automodel.components.models.mistral4.model
Module Contents
Classes
Functions
Data
API
Bases: HFCheckpointingMixin, Module, MoEFSDPSyncMixin
Full multimodal Mistral 4: Pixtral vision + projector + Mistral4 MLA/MoE text backbone.
Follows KimiK25VLForConditionalGeneration pattern: inherits from nn.Module (not HF PreTrainedModel) to avoid FSDP conflicts.
Run one text/image microbatch, consuming staged media when present.
Parameters:
Token IDs [batch, sequence], or [tokens] for THD.
Scalar coordinates with the same token axes.
Optional [batch, sequence] padding mask or [batch, 1, sequence, sequence] additive attention mask.
Optional boolean padding flags matching token axes.
Optional pixels [images, channels, height, width].
Optional original image sizes [images, 2].
Optional [batch, sequence, hidden] from a prior stage.
Packed attention metadata; cumulative token offsets are [segments + 1] and maximum segment lengths are scalars.
Returns: torch.Tensor
Logits [batch, sequence, vocab], or hidden values with final
Only handle configs whose text backbone is Mistral4 (MoE + MLA).
Bases: Module
VLM wrapper composing vision tower + projector + Mistral4 text backend.
Follows KimiK25VLModel pattern: plain nn.Module (not HF PreTrainedModel) to avoid FSDP conflicts from PreTrainedModel’s module registration hooks. Vision processing logic is replicated from HF Mistral3Model.
Encode images through vision tower + projector (from HF Mistral3Model).
Bases: HFCheckpointingMixin, Module, MoEFSDPSyncMixin
Run the decoder and project the kept positions to vocabulary logits.
Parameters:
Token IDs of shape [batch, sequence], or packed (THD) IDs of shape [1, tokens], which the decoder squeezes to [tokens]. Later pipeline stages pass hidden states of shape [batch, sequence, hidden] or [1, tokens, hidden].
Optional positions of shape [batch, sequence], or [1, tokens] for packed input, which requires them. They also set the Llama 4 query scaling when the config enables it.
Optional Tensor of shape [batch, sequence], 1 at valid tokens and 0 at padding, or, with SDPA and an explicit padding_mask, a dense mask of shape [batch, 1, sequence, sequence]; packed input ignores it.
Optional bool Tensor of shape [batch, sequence], or [1, tokens] when packed, true at padding; when None, the MoE layers derive it from attention_mask.
0 keeps every position, a positive int keeps the last N positions, and an integer
Tensor of shape [kept] keeps those positions.
Whether the output carries the final hidden states; None reads
config.output_hidden_states.
Backend metadata for the decoder. Packed input sets qkv_format="thd" and passes
int32 cu_seqlens and cu_seqlens_padded of shape [1, documents + 1], padded with -1000, and
max_seqlen.
Returns: CausalLMOutputWithPast
CausalLMOutputWithPast whose logits have shape [batch, kept, vocab], or [1, kept, vocab] for packed
Bases: MLA
MLA with Llama 4 attention scaling for Mistral 4.
Compared to DeepSeek V3 MLA, adds position-dependent scaling to the query after RoPE (llama_4_scaling_beta). RoPE itself uses the same complex-number approach as DSV3.
Apply Mistral 4 multi-head latent attention.
Parameters:
Tensor of shape [batch, sequence, hidden] for BSHD input or [tokens, hidden] for THD input.
Rotary frequencies of shape [batch, sequence, rope_dim / 2] for non-fused BSHD, [tokens, rope_dim / 2] for non-fused THD, or [sequence, 1, 1, rope_dim] for fused RoPE.
Optional tensor of shape [batch, sequence] or an explicit mask broadcastable to [batch, heads, query_sequence, key_sequence].
Backend metadata such as packed-sequence offsets, position IDs, and CP rank information.
Returns: torch.Tensor
Tensor of shape [batch, sequence, hidden] for BSHD input or [tokens, hidden] for THD input.
Bases: Module
Run the decoder layers on padded or packed (THD) input.
Packed input arrives batched and is squeezed to [tokens, …] for the layers, which TE’s packed kernels take; the hidden states come back batched.
Parameters:
Token IDs of shape [batch, sequence], or packed token IDs of shape [1, tokens]. Later pipeline stages pass hidden states of shape [batch, sequence, hidden] or [1, tokens, hidden].
Optional embeddings of shape [batch, sequence, hidden] used instead of input_ids, or packed [tokens, hidden] from a caller that already squeezed packed input.
Optional positions of shape [batch, sequence]; packed input requires them, as [1, tokens].
Optional Tensor of shape [batch, sequence], 1 at valid tokens and 0 at padding, or, with SDPA and an explicit padding_mask, a dense mask of shape [batch, 1, sequence, sequence]; packed input ignores it.
Optional bool mask of shape [batch, sequence] or [1, tokens], true at padding.
Backend metadata. Packed input sets qkv_format="thd" and passes int32
cu_seqlens and cu_seqlens_padded of shape [1, documents + 1], padded with -1000, and
max_seqlen.
Returns: torch.Tensor
Hidden states of shape [batch, sequence, hidden], or [1, tokens, hidden] for packed input.
Bases: Module
Backend-aware Mistral4 text model for use inside the multimodal wrapper.
Wraps Mistral4Model in self.model (like KimiK25VLLanguageModelBackend wraps DeepseekV3Model). This ensures embed_tokens/layers/norm are accessed via @property aliases rather than as direct nn.Module children, which avoids FSDP double-root-init when the parallelizer wraps both embed_tokens and this module.
Build MoEConfig from a Mistral4 text config.
Position-dependent attention scaling for long-context extrapolation (Llama 4 / Mistral 4).