|
| 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