Skip to content

Commit 761fe31

Browse files
committed
Fix config and smart_resize
1 parent cb8ef60 commit 761fe31

5 files changed

Lines changed: 95 additions & 240 deletions

File tree

src/transformers/models/videollama3/configuration_videollama3.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
# the file from the modular. If any change should be done, please apply the change to the
55
# modular_videollama3.py file directly. One of our CI enforces this.
66
# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
7+
from transformers.configuration_utils import PretrainedConfig
78

8-
from ...configuration_utils import PretrainedConfig
9-
from ..qwen2.configuration_qwen2 import Qwen2Config
9+
from ..auto import CONFIG_MAPPING, AutoConfig
1010

1111

1212
class Videollama3VisionConfig(PretrainedConfig):
@@ -83,7 +83,7 @@ class Videollama3Config(PretrainedConfig):
8383
"""
8484

8585
model_type = "videollama3"
86-
sub_configs = {"vision_config": Videollama3VisionConfig, "text_config": Qwen2Config}
86+
sub_configs = {"vision_config": Videollama3VisionConfig, "text_config": AutoConfig}
8787
keys_to_ignore_at_inference = ["past_key_values"]
8888

8989
def __init__(
@@ -95,21 +95,21 @@ def __init__(
9595
use_token_compression=False,
9696
**kwargs,
9797
):
98-
super().__init__(**kwargs)
9998
if isinstance(vision_config, dict):
10099
self.vision_config = self.sub_configs["vision_config"](**vision_config)
101100
elif vision_config is None:
102101
self.vision_config = self.sub_configs["vision_config"]()
103102

104103
if isinstance(text_config, dict):
105-
self.text_config = self.sub_configs["text_config"](**text_config)
104+
self.text_config = CONFIG_MAPPING[text_config["model_type"]](**text_config)
106105
elif text_config is None:
107-
# For BC use all kwargs to init `TextConfig`
108-
self.text_config = self.sub_configs["text_config"](**kwargs)
106+
self.text_config = CONFIG_MAPPING["qwen2"]()
109107

110108
self.image_token_id = image_token_id
111109
self.video_token_id = video_token_id
112110
self.use_token_compression = use_token_compression
113111

112+
super().__init__(**kwargs)
113+
114114

115115
__all__ = ["Videollama3VisionConfig", "Videollama3Config"]

src/transformers/models/videollama3/image_processing_videollama3.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
2-
# This file was automatically generated from src/transformers/models/videollama3/modular_videollama3.py.
3-
# Do NOT edit this file manually as any edits will be overwritten by the generation of
4-
# the file from the modular. If any change should be done, please apply the change to the
5-
# modular_videollama3.py file directly. One of our CI enforces this.
6-
# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
1+
"""Image processor class for VideoLLaMA3."""
2+
3+
import math
74
from typing import Optional, Union
85

96
import numpy as np
@@ -26,15 +23,43 @@
2623
from ...image_processing_utils import BaseImageProcessor
2724
from ...image_transforms import convert_to_rgb, resize, to_channel_dimension_format
2825
from ...image_utils import infer_channel_dimension_format, is_scaled_image, make_list_of_images, to_numpy_array
29-
from .image_processing_videollama3_fast import smart_resize
3026

3127

3228
logger = logging.get_logger(__name__)
3329

3430

31+
def smart_resize(
32+
height: int, width: int, factor: int = 28, min_pixels: int = 56 * 56, max_pixels: int = 14 * 14 * 4 * 1280
33+
):
34+
"""Rescales the image so that the following conditions are met:
35+
36+
1. Both dimensions (height and width) are divisible by 'factor'.
37+
38+
2. The total number of pixels is within the range ['min_pixels', 'max_pixels'].
39+
40+
3. The aspect ratio of the image is maintained as closely as possible.
41+
42+
"""
43+
if max(height, width) / min(height, width) > 200:
44+
raise ValueError(
45+
f"absolute aspect ratio must be smaller than 200, got {max(height, width) / min(height, width)}"
46+
)
47+
h_bar = round(height / factor) * factor
48+
w_bar = round(width / factor) * factor
49+
if h_bar * w_bar > max_pixels:
50+
beta = math.sqrt((height * width) / max_pixels)
51+
h_bar = max(factor, math.floor(height / beta / factor) * factor)
52+
w_bar = max(factor, math.floor(width / beta / factor) * factor)
53+
elif h_bar * w_bar < min_pixels:
54+
beta = math.sqrt(min_pixels / (height * width))
55+
h_bar = math.ceil(height * beta / factor) * factor
56+
w_bar = math.ceil(width * beta / factor) * factor
57+
return h_bar, w_bar
58+
59+
3560
class Videollama3ImageProcessor(BaseImageProcessor):
3661
r"""
37-
Constructs a Qwen2-VL image processor that dynamically resizes images based on the original images.
62+
Constructs a VideoLLaMA3 image processor that dynamically resizes images based on the original images.
3863
3964
Args:
4065
do_resize (`bool`, *optional*, defaults to `True`):

src/transformers/models/videollama3/image_processing_videollama3_fast.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,20 @@
1111
import torch.nn.functional as F
1212

1313
from transformers.feature_extraction_utils import BatchFeature
14+
from transformers.image_processing_utils_fast import DefaultFastImageProcessorKwargs
1415
from transformers.image_utils import (
1516
IMAGENET_STANDARD_MEAN,
1617
IMAGENET_STANDARD_STD,
1718
ChannelDimension,
1819
ImageInput,
19-
PILImageResampling,
2020
SizeDict,
2121
)
2222
from transformers.processing_utils import Unpack
2323
from transformers.utils import TensorType, auto_docstring, logging
2424
from transformers.video_utils import VideoInput, make_batched_videos
2525

26-
from ...image_processing_utils_fast import (
27-
BaseImageProcessorFast,
28-
DefaultFastImageProcessorKwargs,
29-
group_images_by_shape,
30-
reorder_images,
31-
)
26+
from ...image_processing_utils_fast import BaseImageProcessorFast, group_images_by_shape, reorder_images
27+
from ...image_utils import PILImageResampling
3228

3329

3430
logger = logging.get_logger(__name__)

0 commit comments

Comments
 (0)