nemo_automodel.components.models.hy_v4_flash.model
nemo_automodel.components.models.hy_v4_flash.model
Hy4-flash decoder stack for AutoModel training (text backbone; the vision tower is in vision.py).
Forward contract (transformers reference HYV4FlashTextModel): tokens are embedded and replicated into hc_mult
residual streams [batch, sequence, hc_mult, hidden]. Each block optionally applies Engram to the streams, then
attn_hc -> input_layernorm -> gated MLA -> post_self_attn_layernorm -> scatter, then ffn_hc ->
post_attention_layernorm -> dense MLP / latent MoE -> post_mlp_layernorm -> scatter. hc_head collapses
the streams before the final RMSNorm and the fp32 lm_head.
Batch rows and packed documents are flattened into one token axis for attention and never attend to each other. Context parallelism shards the packed row and gathers keys per layer. Pipeline parallelism is not supported.
Module Contents
Classes
Functions
Data
HYV4FlashForConditionalGeneration
API
Flattened-token view of one forward pass, shared by all layers.
Bool tensor [batch, sequence] or None.
Document boundaries over the flattened batch * sequence token axis.
Integer tensor [batch, sequence, max_ngram_size] or None without Engram layers.
Bool tensor [batch, sequence], True at padding, or None.
Rotary tables per attention type over the flattened token axis.
Bases: Module
One sandwich-normed, hyper-connected decoder layer.
Run the layer on streams [batch, sequence, hc_mult, hidden]; the result has the same shape.
Bases: typing.TypedDict
Model-owned context-parallel inputs handed to the training recipe.
Bases: HFCheckpointingMixin, PreTrainedModel, MoEFSDPSyncMixin
Hy4-flash: the decoder stack, the vision tower for image-text inputs, and an fp32 lm_head.
Token embeddings [batch, sequence, hidden] with image features scattered into the image pad positions.
pixel_values has shape [patches, channels * patch_size * patch_size] and image_grid_thw [images, 3].
Register the row-owner-sharded Engram tables as DTensors before FSDP records its ignored parameters.
Parameters:
One-dimensional shard mesh whose ranks and order equal the Engram owner group.
Returns: set[nn.Parameter]
The table parameters (global shape [padded_rows, head_dim], Shard(0)) that FSDP must not own.
Compute logits (and an optional mean token loss).
Parameters:
Integer tensor of shape [batch, sequence].
Optional integer tensor of shape [batch, sequence] or [3, batch, sequence].
Optional tensor of shape [batch, sequence], 1 for real tokens.
Optional bool tensor of shape [batch, sequence], True at padding (used if no attention_mask).
Optional integer tensor of shape [batch, sequence] with packed-document ids.
Optional integer tensor of shape [batch, sequence], -100 ignored. With seq_lens the first
token of every document is not supervised.
Consumed by the recipe loss; accepted so the standard batch reaches the model.
Number of final positions, or integer positions to keep.
Attach the final hidden states [batch, sequence, hidden].
Optional tensor of shape [patches, channels * patch_size * patch_size].
Optional integer tensor of shape [images, 3] of (time, height, width) patch counts.
Optional integer tensor of shape [batch, sequence], 0 text / 1 image.
Context-parallel group; the token tensors are then the shard from the model-owned sharder.
Optional integer tensor of shape [batch, documents] of document lengths of a packed row
(non-CP); seq_lens_padded gives the physical spans and qkv_format must be "thd" or omitted.
Returns: CausalLMOutputWithPast
CausalLMOutputWithPast with logits [batch, kept_sequence, vocab].
Construct using the NeMo registry’s configuration entry point.
Return the token embedding.
Return the independent vocabulary projection.
Initialize every weight (checkpoint-free path) as the reference does.
Select contiguous packed sharding (documents are never aligned or reordered).
batch holds full tensors [batch, global_sequence]; the returned model-owned sharder yields local token
tensors of shape [1, padded_global_sequence / cp_size]. num_chunks is unused: this model uses one chunk.
Bases: Module
Embeddings, hyper-connected blocks and the output collapse.
Compute the final hidden states [batch, sequence, hidden].
inputs_embeds [batch, sequence, hidden] are the token embeddings with image features in place; the other
arguments are those of prepare_batch.
Build the flattened-token metadata shared by every layer.
Parameters:
Integer tensor of shape [batch, sequence].
Integer tensor of shape [3, batch, sequence] (time, height, width).
Optional bool/int tensor of shape [batch, sequence], 1 for real tokens.
Optional integer tensor of shape [batch, sequence] with document ids (0 = padding).
Optional bool tensor of shape [batch, sequence], True at image placeholder tokens.
Context-parallel group; all token tensors above then hold this rank’s contiguous shard (batch must be 1) and the returned document layout / rotary tables cover the global sequence.
Returns: HYV4FlashBatch
The shared batch view.
MoE settings of the latent-space sigmoid router (all routed experts run at routed_expert_hidden_size).
M-RoPE positions of mixed text / image rows (the reference get_rope_index).
Text advances all three axes by one per token; an image block takes vision_position_ids and the next
text token continues at max + 1. Padding positions get zero.
Parameters:
Integer tensor of shape [batch, sequence], 0 for text and 1 for image tokens.
Integer tensor of shape [images, 3] in row-major order of appearance, or None.
Optional tensor of shape [batch, sequence], 1 for real tokens.
Spatial merge size.
Returns: torch.Tensor
Integer tensor of shape [3, batch, sequence].
3-D positions of one image/video: every merged row has w + 1 tokens (the last is the newline).
All three axes of a frame start at the same offset and each frame starts a new block of max(h, w + 1) positions.
Returns: torch.Tensor
Integer tensor of shape [3, t * h * (w + 1)] (time, height, width), offset by start.