Skip to content

Commit 41a4cd4

Browse files
committed
Add VideoLLaMA3 implementation
1 parent 4f9b4e6 commit 41a4cd4

17 files changed

Lines changed: 3585 additions & 0 deletions

src/transformers/modeling_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ def is_local_dist_rank_0():
238238
"qwen2vl",
239239
"qwen2_5_vl",
240240
"videollava",
241+
"videollama3",
241242
"vipllava",
242243
]
243244

src/transformers/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@
337337
from .univnet import *
338338
from .upernet import *
339339
from .video_llava import *
340+
from .videollama3 import *
340341
from .videomae import *
341342
from .vilt import *
342343
from .vipllava import *

src/transformers/models/auto/configuration_auto.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@
398398
("upernet", "UperNetConfig"),
399399
("van", "VanConfig"),
400400
("video_llava", "VideoLlavaConfig"),
401+
("videollama3", "Videollama3Config"),
402+
("videollama3_vision", "Videollama3VisionConfig"),
401403
("videomae", "VideoMAEConfig"),
402404
("vilt", "ViltConfig"),
403405
("vipllava", "VipLlavaConfig"),
@@ -837,6 +839,8 @@
837839
("upernet", "UPerNet"),
838840
("van", "VAN"),
839841
("video_llava", "VideoLlava"),
842+
("videollama3", "Videollama3"),
843+
("videollama3_vision", "Videollama3Vision"),
840844
("videomae", "VideoMAE"),
841845
("vilt", "ViLT"),
842846
("vipllava", "VipLlava"),
@@ -952,6 +956,7 @@
952956
("blip_2_qformer", "blip_2"),
953957
("fastspeech2_conformer_with_hifigan", "fastspeech2_conformer"),
954958
("perception_encoder", "perception_lm"),
959+
("videollama3_vision", "videollama3"),
955960
]
956961
)
957962

src/transformers/models/auto/image_processing_auto.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
("udop", ("LayoutLMv3ImageProcessor", "LayoutLMv3ImageProcessorFast")),
184184
("upernet", ("SegformerImageProcessor", "SegformerImageProcessorFast")),
185185
("van", ("ConvNextImageProcessor", "ConvNextImageProcessorFast")),
186+
("videollama3", ("Videollama3ImageProcessor", "Videollama3ImageProcessorFast")),
186187
("videomae", ("VideoMAEImageProcessor", None)),
187188
("vilt", ("ViltImageProcessor", "ViltImageProcessorFast")),
188189
("vipllava", ("CLIPImageProcessor", "CLIPImageProcessorFast")),

src/transformers/models/auto/modeling_auto.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ class _BaseModelWithGenerate(PreTrainedModel, GenerationMixin):
382382
("univnet", "UnivNetModel"),
383383
("van", "VanModel"),
384384
("video_llava", "VideoLlavaModel"),
385+
("videollama3", "Videollama3Model"),
386+
("videollama3_vision", "Videollama3VisionModel"),
385387
("videomae", "VideoMAEModel"),
386388
("vilt", "ViltModel"),
387389
("vipllava", "VipLlavaModel"),
@@ -1023,6 +1025,7 @@ class _BaseModelWithGenerate(PreTrainedModel, GenerationMixin):
10231025
("shieldgemma2", "Gemma3ForConditionalGeneration"),
10241026
("smolvlm", "SmolVLMForConditionalGeneration"),
10251027
("udop", "UdopForConditionalGeneration"),
1028+
("videollama3", "Videollama3ForConditionalGeneration"),
10261029
("vipllava", "VipLlavaForConditionalGeneration"),
10271030
("vision-encoder-decoder", "VisionEncoderDecoderModel"),
10281031
]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import TYPE_CHECKING
2+
3+
from ...utils import _LazyModule
4+
from ...utils.import_utils import define_import_structure
5+
6+
7+
if TYPE_CHECKING:
8+
from .configuration_videollama3 import *
9+
from .image_processing_videollama3 import *
10+
from .image_processing_videollama3_fast import *
11+
from .modeling_videollama3 import *
12+
from .processing_videollama3 import *
13+
from .video_processing_videollama3 import *
14+
else:
15+
import sys
16+
17+
_file = globals()["__file__"]
18+
sys.modules[__name__] = _LazyModule(__name__, _file, define_import_structure(_file), module_spec=__spec__)
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"""VideoLLaMA3 model configuration"""
2+
3+
from ..auto import AutoConfig, CONFIG_MAPPING
4+
from ..qwen2 import Qwen2Config
5+
from ...configuration_utils import PretrainedConfig
6+
from ...utils import logging
7+
8+
9+
logger = logging.get_logger(__name__)
10+
11+
12+
class Videollama3VisionConfig(PretrainedConfig):
13+
"""
14+
Args:
15+
hidden_size (`int`, *optional*, defaults to 768):
16+
Dimensionality of the encoder layers and the pooler layer.
17+
intermediate_size (`int`, *optional*, defaults to 3072):
18+
Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
19+
num_hidden_layers (`int`, *optional*, defaults to 12):
20+
Number of hidden layers in the Transformer encoder.
21+
num_attention_heads (`int`, *optional*, defaults to 12):
22+
Number of attention heads for each attention layer in the Transformer encoder.
23+
num_channels (`int`, *optional*, defaults to 3):
24+
Number of channels in the input images.
25+
patch_size (`int`, *optional*, defaults to 16):
26+
The size (resolution) of each patch.
27+
hidden_act (`str` or `function`, *optional*, defaults to `"gelu_pytorch_tanh"`):
28+
The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
29+
`"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
30+
layer_norm_eps (`float`, *optional*, defaults to 1e-06):
31+
The epsilon used by the layer normalization layers.
32+
attention_dropout (`float`, *optional*, defaults to 0.0):
33+
The dropout ratio for the attention probabilities.
34+
"""
35+
model_type = "videollama3_vision"
36+
base_config_key = "vision_config"
37+
38+
def __init__(
39+
self,
40+
hidden_size=1152,
41+
intermediate_size=4304,
42+
num_hidden_layers=27,
43+
num_attention_heads=16,
44+
num_channels=3,
45+
patch_size=14,
46+
hidden_act="gelu_pytorch_tanh",
47+
layer_norm_eps=1e-6,
48+
attention_dropout=0.0,
49+
initializer_range=0.02,
50+
**kwargs,
51+
):
52+
super().__init__(**kwargs)
53+
54+
self.hidden_size = hidden_size
55+
self.intermediate_size = intermediate_size
56+
self.num_hidden_layers = num_hidden_layers
57+
self.num_attention_heads = num_attention_heads
58+
self.num_channels = num_channels
59+
self.patch_size = patch_size
60+
self.hidden_act = hidden_act
61+
self.layer_norm_eps = layer_norm_eps
62+
self.attention_dropout = attention_dropout
63+
self.initializer_range = initializer_range
64+
65+
66+
class Videollama3Config(PretrainedConfig):
67+
"""
68+
Args:
69+
text_config (`Union[PreTrainedConfig, dict]`, *optional*, defaults to `Qwen2Config`):
70+
The config object or dictionary of the text backbone.
71+
vision_config (`Union[PreTrainedConfig, dict]`, *optional*, defaults to `Videollama3VisionConfig`):
72+
The config object or dictionary of the vision backbone.
73+
use_token_compression (`bool`, *optional*, defaults to `False`):
74+
Whether to use temporal token compression to reduce the number of video tokens.
75+
image_token_id (`int`, *optional*, defaults to -1):
76+
The image token index to encode the image prompt.
77+
video_token_id (`int`, *optional*, defaults to -1):
78+
The video token index to encode the image prompt.
79+
"""
80+
81+
model_type = "videollama3"
82+
sub_configs = {"vision_config": Videollama3VisionConfig, "text_config": AutoConfig}
83+
keys_to_ignore_at_inference = ["past_key_values"]
84+
85+
def __init__(
86+
self,
87+
text_config=None,
88+
vision_config=None,
89+
use_token_compression=False,
90+
image_token_id=-1,
91+
video_token_id=-1,
92+
**kwargs,
93+
):
94+
if text_config is None:
95+
self.text_config = Qwen2Config(**kwargs)
96+
logger.info("text_config is None, using default qwen2 config")
97+
elif isinstance(text_config, dict):
98+
assert "model_type" in text_config, "text_config must contain 'model_type' key"
99+
self.text_config = CONFIG_MAPPING[text_config["model_type"]](**text_config)
100+
elif isinstance(text_config, PretrainedConfig):
101+
self.text_config = text_config
102+
else:
103+
raise ValueError(
104+
"text_config must be a dictionary, PretrainedConfig instance, or None. "
105+
f"Got {type(text_config)} instead."
106+
)
107+
108+
if vision_config is None:
109+
self.vision_config = Videollama3VisionConfig()
110+
logger.info("vision_config is None, using default vision config")
111+
elif isinstance(vision_config, dict):
112+
assert "model_type" in vision_config, "vision_config must contain 'model_type' key"
113+
self.vision_config = CONFIG_MAPPING[vision_config["model_type"]](**vision_config)
114+
elif isinstance(vision_config, PretrainedConfig):
115+
self.vision_config = vision_config
116+
else:
117+
raise ValueError(
118+
"vision_config must be a dictionary, PretrainedConfig instance, or None. "
119+
f"Got {type(vision_config)} instead."
120+
)
121+
122+
self.use_token_compression = use_token_compression
123+
self.image_token_id = image_token_id
124+
self.video_token_id = video_token_id
125+
126+
super().__init__(**kwargs)
127+
128+
129+
__all__ = ["Videollama3Config", "Videollama3VisionConfig"]

0 commit comments

Comments
 (0)