Skip to navigation

nemo_automodel.components.models.hy_v4_flash.processing

View as MarkdownOpen in Claude

Image-text processor of Hy4-flash.

The released chat template renders each image as <image_start><image_pad><image_end>; this processor expands the single pad into (h / merge) * (w / merge + 1) pads (one extra slot per merged row for the newline feature) and produces the flat patches the vision tower consumes. Preprocessing follows the transformers reference (HYV4VLImageProcessor): smart_resize to multiples of patch_size * merge, bicubic resize on the uint8 tensor, rescale by 1/255, normalize with mean/std 0.5, row-major patches. The released processor_config names LANCZOS as the resampling filter, but the reference (and the original implementation) always resize bicubically; so does this code.

Module Contents

Classes

NameDescription
ChatContentBlockOne text or image block of a chat message (the Hugging Face multimodal chat format).
ChatMessageOne chat message with string content or an ordered list of content blocks.
HYV4FlashImageSettingsPreprocessing constants of processor_config.json: patch and merge size, pixel bounds, mean and std.
HYV4FlashProcessorExpand image placeholders and build vision inputs for the Hy4-flash chat format.
ImageProcessorConfigThe fields of the image_processor block of the released processor_config.json that are consumed.
ImageUrl{"url": ...} form of an image reference.
ProcessorConfigprocessor_config.json as this processor writes it.
ProcessorOptionsTokenization options forwarded by the collator through processor_kwargs.

Functions

NameDescription
_open_imageDecode an image source (PIL image, path or encoded bytes).
image_gridPatch grid (grid_h, grid_w) of an image of the given pixel size.
image_settings_from_processor_configPreprocessing constants of a checkpoint’s processor_config.json image_processor block.
num_image_tokensPlaceholder count of one image: (h / merge) * (w / merge + 1) (one newline slot per merged row).
preprocess_imageResize, normalize and patchify one PIL image (converted to RGB).
smart_resizeRound both sides to multiples of factor while keeping the area inside [min_pixels, max_pixels].
training_reasoning_effortReasoning mode of a complete (training) conversation: "no_think", as the official fine-tuning scripts use.

Data

IMAGE_PAD_TOKEN

ImageSource

TokenizerOption

API

class nemo_automodel.components.models.hy_v4_flash.processing.ChatContentBlock

Bases: typing.TypedDict

One text or image block of a chat message (the Hugging Face multimodal chat format).

image
ImageSource
image_url
str | ImageUrl
path
str
text
str
type
Required[Literal['text', 'image', 'image_url']]
class nemo_automodel.components.models.hy_v4_flash.processing.ChatMessage

Bases: typing.TypedDict

One chat message with string content or an ordered list of content blocks.

content
Required[str | list[ChatContentBlock]]
reasoning
str
reasoning_content
str
role
Required[Literal['system', 'user', 'assistant']]
class nemo_automodel.components.models.hy_v4_flash.processing.HYV4FlashImageSettings(
patch_size: int = 16,
merge_size: int = 2,
min_pixels: int = 4096,
max_pixels: int = 4194304,
mean: tuple[float, float, float] = (0.5, 0.5, 0.5),
std: tuple[float, float, float] = (0.5, 0.5, 0.5)
)
Dataclass

Preprocessing constants of processor_config.json: patch and merge size, pixel bounds, mean and std.

max_pixels
int = 4194304
mean
tuple[float, float, float] = (0.5, 0.5, 0.5)
merge_size
int = 2
min_pixels
int = 4096
patch_size
int = 16
std
tuple[float, float, float] = (0.5, 0.5, 0.5)
class nemo_automodel.components.models.hy_v4_flash.processing.HYV4FlashProcessor(
tokenizer: transformers.PreTrainedTokenizerFast,
image_token_id: int = 120120,
)

Bases: ProcessorMixin

Expand image placeholders and build vision inputs for the Hy4-flash chat format.

Parameters:

tokenizer
PreTrainedTokenizerFast

Fast tokenizer of the checkpoint (carries the chat template and the image tokens).

image_token_id
intDefaults to 120120

Id of the image pad token (config.image_token_id).

settings
HYV4FlashImageSettings | NoneDefaults to None

Image preprocessing constants.

attributes
= ['tokenizer']
settings
= settings or HYV4FlashImageSettings()
tokenizer_class
= 'AutoTokenizer'
nemo_automodel.components.models.hy_v4_flash.processing.HYV4FlashProcessor.__call__(
text: str | list[str],
images: Image.Image | list[Image.Image] | list[list[Image.Image]] | None = None,
return_tensors: str | None = None,
padding: bool | str = False,
truncation: bool = False,
max_length: int | None = None,
) -> transformers.BatchFeature

Tokenize rendered prompts, expanding every image pad.

Parameters:

text
str | list[str]

Rendered prompt(s), each containing one image pad token per image.

images
Image.Image | list[Image.Image] | list[list[Image.Image]] | NoneDefaults to None

Images grouped per prompt (or one prompt’s flat list).

return_tensors
str | NoneDefaults to None

"pt" or None (ragged lists).

padding
bool | strDefaults to False

False, True / "longest" or "max_length" (right padding).

truncation
boolDefaults to False

Whether max_length removes trailing tokens; cutting through an image raises.

max_length
int | NoneDefaults to None

Token limit after image expansion.

**kwargs
TokenizerOptionDefaults to {}

Extra tokenizer arguments.

Returns: BatchFeature

BatchFeature with input_ids / attention_mask / mm_token_type_ids of shape [batch, sequence]

nemo_automodel.components.models.hy_v4_flash.processing.HYV4FlashProcessor._split_images(

Replace image payloads with bare {"type": "image"} blocks and collect the sources in order.

nemo_automodel.components.models.hy_v4_flash.processing.HYV4FlashProcessor.apply_chat_template(
conversation: collections.abc.Sequence[nemo_automodel.components.models.hy_v4_flash.processing.ChatMessage] | collections.abc.Sequence[collections.abc.Sequence[nemo_automodel.components.models.hy_v4_flash.processing.ChatMessage]],
tokenize: bool = False,
return_dict: bool = False,
return_tensors: str | None = None,
add_generation_prompt: bool = False,
) -> str | list[str] | torch.Tensor | transformers.BatchFeature

Render conversations with the checkpoint’s jinja template and, optionally, tokenize them.

Parameters:

conversation
Sequence[ChatMessage] | Sequence[Sequence[ChatMessage]]

Chat messages (or a batch of them) with string or text / image content blocks.

tokenize
boolDefaults to False

Whether to tokenize (expanding images) instead of returning the rendered text.

return_dict
boolDefaults to False

Whether to return the whole BatchFeature.

return_tensors
str | NoneDefaults to None

"pt" or None.

add_generation_prompt
boolDefaults to False

Append the assistant header.

processor_kwargs
ProcessorOptions | NoneDefaults to None

Padding / truncation arguments for __call__.

**kwargs
TokenizerOptionDefaults to {}

Extra jinja template variables. Without reasoning_effort, a complete conversation (add_generation_prompt=False, the training case) is rendered in no_think mode and must not carry assistant reasoning (training_reasoning_effort); a generation prompt keeps the template’s own default.

Returns: str | list[str] | torch.Tensor | BatchFeature

Rendered text(s), input_ids, or the full batch.

classmethod

Load the tokenizer, the image token id and the image constants of a checkpoint.

nemo_automodel.components.models.hy_v4_flash.processing.HYV4FlashProcessor.to_dict() -> nemo_automodel.components.models.hy_v4_flash.processing.ProcessorConfig

processor_config.json content: the image_processor block that from_pretrained reads.

class nemo_automodel.components.models.hy_v4_flash.processing.ImageProcessorConfig

Bases: typing.TypedDict

The fields of the image_processor block of the released processor_config.json that are consumed.

image_mean
list[float]
image_std
list[float]
max_pixels
int
merge_size
int
min_pixels
int
patch_size
int
class nemo_automodel.components.models.hy_v4_flash.processing.ImageUrl

Bases: typing.TypedDict

{"url": ...} form of an image reference.

url
str
class nemo_automodel.components.models.hy_v4_flash.processing.ProcessorConfig

Bases: typing.TypedDict

processor_config.json as this processor writes it.

image_processor
ImageProcessorConfig
image_token_id
int
processor_class
str
class nemo_automodel.components.models.hy_v4_flash.processing.ProcessorOptions

Bases: typing.TypedDict

Tokenization options forwarded by the collator through processor_kwargs.

max_length
int
padding
bool | str
truncation
bool
nemo_automodel.components.models.hy_v4_flash.processing._open_image(
value: Image.Image | str | pathlib.Path | bytes
) -> Image.Image

Decode an image source (PIL image, path or encoded bytes).

nemo_automodel.components.models.hy_v4_flash.processing.image_grid(
height: int,
width: int,
) -> tuple[int, int]

Patch grid (grid_h, grid_w) of an image of the given pixel size.

nemo_automodel.components.models.hy_v4_flash.processing.image_settings_from_processor_config(

Preprocessing constants of a checkpoint’s processor_config.json image_processor block.

nemo_automodel.components.models.hy_v4_flash.processing.num_image_tokens(
grid_h: int,
grid_w: int,
merge: int
) -> int

Placeholder count of one image: (h / merge) * (w / merge + 1) (one newline slot per merged row).

nemo_automodel.components.models.hy_v4_flash.processing.preprocess_image(
image: Image.Image,
) -> tuple[torch.Tensor, tuple[int, int]]

Resize, normalize and patchify one PIL image (converted to RGB).

Returns (patches, (grid_h, grid_w)) with patches of shape [grid_h * grid_w, 3 * patch_size * patch_size] in row-major patch order.

nemo_automodel.components.models.hy_v4_flash.processing.smart_resize(
height: int,
width: int,
factor: int,
min_pixels: int,
max_pixels: int
) -> tuple[int, int]

Round both sides to multiples of factor while keeping the area inside [min_pixels, max_pixels].

nemo_automodel.components.models.hy_v4_flash.processing.training_reasoning_effort(
) -> typing.Literal['no_think']

Reasoning mode of a complete (training) conversation: "no_think", as the official fine-tuning scripts use.

The released template defaults to 'high' (its inference default); rendering answer-only data in that mode would train answers after an empty think block.

Raises:

  • ValueError: If an assistant message carries reasoning. Such a turn renders as <think>reasoning</think>answer, which the label builders of the training recipes do not supervise.
nemo_automodel.components.models.hy_v4_flash.processing.IMAGE_PAD_TOKEN = '<|hy_image▁pad:opensource|>'
nemo_automodel.components.models.hy_v4_flash.processing.ImageSource: TypeAlias = 'Image.Image | str | Path | bytes'
nemo_automodel.components.models.hy_v4_flash.processing.TokenizerOption: TypeAlias = 'bool | int | str | list[str] | None'