Skip to navigation

nemo_automodel.components.models.hy_v4_flash.vision

View as MarkdownOpen in Claude

Hy4-flash vision tower: patch embedding, ViT blocks and the spatial-merge projector.

Semantics follow HYV4VLVisionModel of the transformers reference (PR 50903): a learned position grid interpolated with an explicit scale_factor of (size + 0.1) / grid (this shifts the sampling grid relative to size=), full bidirectional attention inside each image, an RMSNorm + 2x2 conv merge + 1x1 conv projector, and one image_newline embedding appended to every merged row. Module names follow the released vit.* keys, which the state-dict adapter therefore leaves unchanged.

Module Contents

Classes

NameDescription
HYV4FlashPerceiveSpatial-merge projector: RMSNorm, 2x2 conv, GELU, 1x1 conv, plus a newline embedding per merged row.
HYV4FlashVisionAttentionBidirectional multi-head attention with biases.
HYV4FlashVisionBlockPre-norm ViT block.
HYV4FlashVisionEmbeddingsConv patch embedding plus an interpolated learned position grid.
HYV4FlashVisionMLPGELU MLP named like the release (dense_h_to_4h / dense_4h_to_h).
HYV4FlashVisionTowervit: embeddings, blocks and the perceive projector.

API

class nemo_automodel.components.models.hy_v4_flash.vision.HYV4FlashPerceive(
)

Bases: Module

Spatial-merge projector: RMSNorm, 2x2 conv, GELU, 1x1 conv, plus a newline embedding per merged row.

image_newline
= nn.Parameter(torch.empty(config.out_hidden_size))
norm
proj
nemo_automodel.components.models.hy_v4_flash.vision.HYV4FlashPerceive.forward(
hidden_states: torch.Tensor,
grid_thw: torch.Tensor
) -> torch.Tensor

Merge and project hidden_states [patches, hidden] per image (frames are merged independently).

Returns [tokens, out_hidden]; each frame of grid_thw [images, 3] gives (h/m) * (w/m + 1) tokens.

class nemo_automodel.components.models.hy_v4_flash.vision.HYV4FlashVisionAttention(
)

Bases: Module

Bidirectional multi-head attention with biases.

head_dim
= config.hidden_size // config.num_attention_heads
k_proj
= nn.Linear(config.hidden_size, config.hidden_size)
num_heads
= config.num_attention_heads
o_proj
= nn.Linear(config.hidden_size, config.hidden_size)
q_proj
= nn.Linear(config.hidden_size, config.hidden_size)
v_proj
= nn.Linear(config.hidden_size, config.hidden_size)
nemo_automodel.components.models.hy_v4_flash.vision.HYV4FlashVisionAttention.forward(
hidden_states: torch.Tensor,
lengths: list[int]
) -> torch.Tensor

Attend inside each image: hidden_states [patches, hidden], lengths the patch count of each image.

class nemo_automodel.components.models.hy_v4_flash.vision.HYV4FlashVisionBlock(
)

Bases: Module

Pre-norm ViT block.

input_layernorm
mlp
= HYV4FlashVisionMLP(config)
post_attention_layernorm
self_attn
= HYV4FlashVisionAttention(config)
nemo_automodel.components.models.hy_v4_flash.vision.HYV4FlashVisionBlock.forward(
hidden_states: torch.Tensor,
lengths: list[int]
) -> torch.Tensor

Run the block on a tensor of shape [patches, hidden].

class nemo_automodel.components.models.hy_v4_flash.vision.HYV4FlashVisionEmbeddings(
)

Bases: Module

Conv patch embedding plus an interpolated learned position grid.

edge
= config.max_image_size // config.patch_size
patch_embedding
patch_size
= config.patch_size
position_embedding
nemo_automodel.components.models.hy_v4_flash.vision.HYV4FlashVisionEmbeddings.forward(
pixel_values: torch.Tensor,
grid_thw: torch.Tensor
) -> torch.Tensor

Embed flat patches into [patches, hidden].

pixel_values has shape [patches, channels * patch_size * patch_size], row-major per image, and grid_thw [images, 3] holds the (time, height, width) patch counts.

nemo_automodel.components.models.hy_v4_flash.vision.HYV4FlashVisionEmbeddings.interpolate_positions(
height: int,
width: int,
dtype: torch.dtype
) -> torch.Tensor

Learned grid resampled to height x width patches.

Returns: torch.Tensor

Tensor of shape [height * width, hidden].

class nemo_automodel.components.models.hy_v4_flash.vision.HYV4FlashVisionMLP(
)

Bases: Module

GELU MLP named like the release (dense_h_to_4h / dense_4h_to_h).

dense_4h_to_h
dense_h_to_4h
nemo_automodel.components.models.hy_v4_flash.vision.HYV4FlashVisionMLP.forward(
hidden_states: torch.Tensor
) -> torch.Tensor

Apply the MLP to a tensor of shape […, hidden].

class nemo_automodel.components.models.hy_v4_flash.vision.HYV4FlashVisionTower(
)

Bases: Module

vit: embeddings, blocks and the perceive projector.

embeddings
= HYV4FlashVisionEmbeddings(config)
layers
perceive
= HYV4FlashPerceive(config)
nemo_automodel.components.models.hy_v4_flash.vision.HYV4FlashVisionTower.forward(
pixel_values: torch.Tensor,
grid_thw: torch.Tensor
) -> torch.Tensor

Encode images into language-width tokens [tokens, out_hidden], the images’ tokens concatenated.

pixel_values has shape [patches, channels * patch_size * patch_size] and grid_thw [images, 3].

nemo_automodel.components.models.hy_v4_flash.vision.HYV4FlashVisionTower.reset_parameters(
std: float
) -> None

Initialize as the reference does: zero biases, unit norm weights, normal everything else.