hy4-flash-vision-encoder / native_source /image_processing_pil_hy_v4_flash.py
gatilin's picture
Add files using upload-large-folder tool
48e1b03 verified
Raw History Blame Contribute Delete
11.9 kB
# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# This file was automatically generated from src/transformers/models/hy_v4_flash/modular_hy_v4_flash.py.
# Do NOT edit this file manually as any edits will be overwritten by the generation of
# the file from the modular. If any change should be done, please apply the change to the
# modular_hy_v4_flash.py file directly. One of our CI enforces this.
# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# Copyright 2026 Tencent HunYuan Team and The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
import warnings
from collections.abc import Iterable
import numpy as np
from ...image_processing_backends import PilBackend
from ...image_processing_utils import BatchFeature
from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict
from ...processing_utils import ImagesKwargs, Unpack
from ...utils import TensorType, auto_docstring
# Adapted from transformers.models.hy_v4_flash.image_processing_hy_v4_flash.HYV4FlashImageProcessorKwargs
class HYV4FlashImageProcessorKwargs(ImagesKwargs, total=False):
r"""
min_pixels (`int`, *optional*, defaults to `512 * 512`):
The min pixels of the image to resize the image.
max_pixels (`int`, *optional*, defaults to `2048 * 2048`):
The max pixels of the image to resize the image.
patch_size (`int`, *optional*, defaults to 16):
The spatial patch size of the vision encoder.
temporal_patch_size (`int`, *optional*, defaults to 1):
The temporal patch size of the vision encoder.
merge_size (`int`, *optional*, defaults to 2):
The merge size of the vision encoder to llm encoder.
"""
min_pixels: int
max_pixels: int
patch_size: int
temporal_patch_size: int
merge_size: int
def smart_resize(
height: int, width: int, factor: int = 28, min_pixels: int = 56 * 56, max_pixels: int = 14 * 14 * 4 * 1280
):
"""Rescales the image so that the following conditions are met:
1. Both dimensions (height and width) are divisible by 'factor'.
2. The total number of pixels is within the range ['min_pixels', 'max_pixels'].
3. The aspect ratio of the image is maintained as closely as possible.
"""
if max(height, width) / min(height, width) > 200:
raise ValueError(
f"absolute aspect ratio must be smaller than 200, got {max(height, width) / min(height, width)}"
)
h_bar = round(height / factor) * factor
w_bar = round(width / factor) * factor
if h_bar * w_bar > max_pixels:
beta = math.sqrt((height * width) / max_pixels)
h_bar = max(factor, math.floor(height / beta / factor) * factor)
w_bar = max(factor, math.floor(width / beta / factor) * factor)
elif h_bar * w_bar < min_pixels:
beta = math.sqrt(min_pixels / (height * width))
h_bar = math.ceil(height * beta / factor) * factor
w_bar = math.ceil(width * beta / factor) * factor
return h_bar, w_bar
@auto_docstring
class HYV4FlashImageProcessorPil(PilBackend):
do_resize = True
resample = PILImageResampling.BICUBIC
size = {"shortest_edge": 512 * 512, "longest_edge": 2048 * 2048}
default_to_square = False
do_rescale = True
do_normalize = True
image_mean = OPENAI_CLIP_MEAN
image_std = OPENAI_CLIP_STD
do_convert_rgb = True
patch_size = 16
temporal_patch_size = 1
merge_size = 2
valid_kwargs = HYV4FlashImageProcessorKwargs
model_input_names = ["pixel_values", "image_grid_thw"]
spatial_patch_size = 1
def __init__(self, **kwargs: Unpack[HYV4FlashImageProcessorKwargs]):
# backward compatibility: override size with min_pixels and max_pixels if they are provided
size = kwargs.pop("size", None)
size = dict(self.size) if size is None else size
if (min_pixels := kwargs.pop("min_pixels", None)) is not None:
size["shortest_edge"] = min_pixels
size.pop("min_pixels", None)
if (max_pixels := kwargs.pop("max_pixels", None)) is not None:
size["longest_edge"] = max_pixels
size.pop("max_pixels", None)
super().__init__(size=size, **kwargs)
def _standardize_kwargs(
self,
size: int | Iterable[int] | dict[str, int] | SizeDict | None = None,
min_pixels: int | None = None,
max_pixels: int | None = None,
**kwargs,
) -> dict:
if min_pixels is not None or max_pixels is not None:
warnings.warn(
"Passing `min_pixels` and `max_pixels` to a processor call is deprecated and will be removed in v5.23. "
"Pass in `size={'longest_edge': xxx, 'shortest_edge': xxx} to override the target size.`",
FutureWarning,
)
size_dict = dict(size) if isinstance(size, (dict, SizeDict)) else {}
if min_pixels is not None:
size_dict["shortest_edge"] = min_pixels
if max_pixels is not None:
size_dict["longest_edge"] = max_pixels
size = SizeDict(**size_dict)
return super()._standardize_kwargs(size=size, **kwargs)
def resize(
self,
image: np.ndarray,
size: SizeDict,
resample: "PILImageResampling | int | None",
factor: int,
**kwargs,
) -> np.ndarray:
"""Resize dynamically based on input image aspect ratio."""
if not size.shortest_edge or not size.longest_edge:
raise ValueError(f"`size` dict must contain 'shortest_edge' and 'longest_edge' keys but got {size}.")
height, width = image.shape[-2:]
resized_height, resized_width = smart_resize(
height,
width,
factor=factor,
min_pixels=size.shortest_edge,
max_pixels=size.longest_edge,
)
return super().resize(
image=image,
size=SizeDict(height=resized_height, width=resized_width),
# The reference HunyuanOCR processor calls `PIL.Image.resize` without a
# resampling argument, which uses BICUBIC for RGB images. Its config has
# `resample=1` (LANCZOS), but the original implementation never uses it.
resample=PILImageResampling.BICUBIC,
)
def patchify(
self,
image: np.ndarray,
patch_size: int,
merge_size: int,
temporal_patch_size: int,
) -> tuple[np.ndarray, int, int]:
"""Patchifies each image into flat layout of shape (`seq_len`, `patch_dim`) so we can concat dynamically shaped pixels."""
# Ensure float32 for patch processing
image = np.asarray(image, dtype=np.float32)
channel, resized_height, resized_width = image.shape
grid_h, grid_w = resized_height // patch_size, resized_width // patch_size
patches = image.reshape(
channel,
grid_h // merge_size,
merge_size,
patch_size,
grid_w // merge_size,
merge_size,
patch_size,
)
# Flatten patches in row-major order - (gh, mh, gw, mw, C, ph, pw)
patches = np.transpose(patches, (1, 2, 4, 5, 0, 3, 6))
# expand temporal_patch_size as a broadcast (zero-copy)
patches = np.broadcast_to(
patches[:, :, :, :, :, None, :, :],
(*patches.shape[:5], temporal_patch_size, *patches.shape[5:]),
)
flatten_patches = patches.reshape(
grid_h * grid_w,
channel * temporal_patch_size * patch_size * patch_size,
)
return flatten_patches, grid_h, grid_w
@auto_docstring
def preprocess(
self,
images: ImageInput,
**kwargs: Unpack[HYV4FlashImageProcessorKwargs],
) -> BatchFeature:
return super().preprocess(images, **kwargs)
def _preprocess(
self,
images: list[np.ndarray],
do_resize: bool,
size: SizeDict,
resample: "PILImageResampling | None",
do_rescale: bool,
rescale_factor: float,
do_normalize: bool,
image_mean: float | list[float] | None,
image_std: float | list[float] | None,
patch_size: int,
temporal_patch_size: int,
merge_size: int,
return_tensors: str | TensorType | None,
**kwargs,
) -> BatchFeature:
all_patches = []
all_grids = []
for image in images:
if do_resize:
image = self.resize(
image,
size=size,
resample=resample,
factor=patch_size * merge_size,
)
if do_rescale:
image = self.rescale(image, rescale_factor)
if do_normalize:
image = self.normalize(image, image_mean, image_std)
patches, grid_h, grid_w = self.patchify(
image,
patch_size=patch_size,
merge_size=merge_size,
temporal_patch_size=temporal_patch_size,
)
all_patches.append(patches)
all_grids.append([1, grid_h, grid_w])
pixel_values = np.concatenate(all_patches, axis=0)
image_grid_thw = np.array(all_grids, dtype=np.int64)
return BatchFeature(
data={"pixel_values": pixel_values, "image_grid_thw": image_grid_thw}, tensor_type=return_tensors
)
def get_number_of_image_patches(
self, height: int, width: int, images_kwargs: dict | None = None
) -> tuple[int, int]:
"""
A utility that returns number of image patches for a given image size.
Note: Do not remove this method! It is used by vLLM to infer the number of patches and placeholders
without an image input.
Args:
height (`int`):
Height of the input image.
width (`int`):
Width of the input image.
images_kwargs (`dict`, *optional*)
Any kwargs to override defaults of the image processor.
Returns:
`tuple[int, int]`: Number of image patches per image, as a `(height, width)` grid.
"""
images_kwargs = images_kwargs or {}
min_pixels = images_kwargs["min_pixels"] if "min_pixels" in images_kwargs else self.size["shortest_edge"]
max_pixels = images_kwargs["max_pixels"] if "max_pixels" in images_kwargs else self.size["longest_edge"]
patch_size = images_kwargs.get("patch_size", self.patch_size)
merge_size = images_kwargs.get("merge_size", self.merge_size)
factor = patch_size * merge_size
resized_height, resized_width = smart_resize(
height, width, factor, min_pixels=min_pixels, max_pixels=max_pixels
)
return resized_height // patch_size, resized_width // patch_size
__all__ = ["HYV4FlashImageProcessorPil"]