Aluode/PerceptionLabPortable
0
1# coding=utf-82# Copyright 2025 The HuggingFace Inc. team. All rights reserved.3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15 16from ...configuration_utils import PretrainedConfig17from ..qwen2.configuration_qwen2 import Qwen2Config18 19 20class Ovis2VisionConfig(PretrainedConfig):21 r"""22 This is the configuration class to store the configuration of a [`Ovis2VisionModel`]. It is used to instantiate a23 Ovis2VisionModel model according to the specified arguments, defining the model architecture. Instantiating a configuration24 with the defaults will yield a similar configuration to that of Ovis2.25 26 Args:27 hidden_size (`int`, *optional*, defaults to 1024):28 Dimensionality of the encoder layers and the pooler layer.29 intermediate_size (`int`, *optional*, defaults to 2816):30 Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.31 num_hidden_layers (`int`, *optional*, defaults to 24):32 Number of hidden layers in the Transformer encoder.33 num_attention_heads (`int`, *optional*, defaults to 8):34 Number of attention heads for each attention layer in the Transformer encoder.35 num_channels (`int`, *optional*, defaults to 3):36 Number of channels in the input images.37 image_size (`int`, *optional*, defaults to 224):38 The size (resolution) of each image.39 patch_size (`int`, *optional*, defaults to 14):40 The size (resolution) of each patch.41 rms_norm_eps (`float`, *optional*, defaults to 1e-05):42 The epsilon used by the RMSNorm layers.43 attention_dropout (`float`, *optional*, defaults to 0.0):44 The dropout ratio for the attention probabilities.45 qkv_bias (`bool`, *optional*, defaults to `False`):46 Whether to add a learnable bias to the query, key, and value sequences at each attention head.47 mlp_bias (`bool`, *optional*, defaults to `False`):48 Whether to add a learnable bias to the MLP layers.49 hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):50 The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,51 `"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.52 vocab_size (`int`, *optional*, defaults to 16384):53 Vocabulary size of the Vision Transformer.54 hidden_stride (`int`, *optional*, defaults to 1):55 The stride of the hidden layer in the Vision Transformer.56 num_visual_indicator_tokens (`int`, *optional*, defaults to 5):57 Number of visual indicator tokens.58 initializer_range (`float`, *optional*, defaults to 0.02):59 The standard deviation of the truncated normal initializer for initializing all weight matrices.60 tokenize_function (`str`, *optional*, defaults to `"softmax"`):61 The function used to tokenize the visual indicator tokens.62 """63 64 base_config_key = "vision_config"65 66 def __init__(67 self,68 hidden_size: int = 1024,69 intermediate_size: int = 2816,70 num_hidden_layers: int = 24,71 num_attention_heads: int = 8,72 num_channels: int = 3,73 image_size: int = 224,74 patch_size: int = 14,75 rms_norm_eps: float = 1e-5,76 attention_dropout: float = 0.0,77 qkv_bias: bool = False,78 mlp_bias: bool = False,79 hidden_act="silu",80 vocab_size=16384,81 hidden_stride=1,82 num_visual_indicator_tokens=5,83 initializer_range=0.02,84 tokenize_function="softmax",85 **kwargs,86 ):87 super().__init__(**kwargs)88 89 self.hidden_size = hidden_size90 self.intermediate_size = intermediate_size91 self.num_hidden_layers = num_hidden_layers92 self.num_attention_heads = num_attention_heads93 self.num_channels = num_channels94 self.patch_size = patch_size95 self.image_size = image_size96 97 self.attention_dropout = attention_dropout98 self.hidden_act = hidden_act99 self.qkv_bias = qkv_bias100 self.mlp_bias = mlp_bias101 self.rms_norm_eps = rms_norm_eps102 self.vocab_size = vocab_size103 self.hidden_stride = hidden_stride104 self.num_visual_indicator_tokens = num_visual_indicator_tokens105 self.tokenize_function = tokenize_function106 self.initializer_range = initializer_range107 108 109class Ovis2Config(PretrainedConfig):110 r"""111 This is the configuration class to store the configuration of a [`Ovis2ForConditionalGeneration`]. It is used to instantiate a112 Ovis2 model according to the specified arguments, defining the model architecture. Instantiating a configuration113 with the defaults will yield a similar configuration to that of Ovis2.114 115 Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the116 documentation from [`PretrainedConfig`] for more information.117 118 e.g. [thisisiron/Ovis2-1B-hf](https://huggingface.co/thisisiron/Ovis2-1B-hf)119 120 Args:121 vision_config (`Union[AutoConfig, dict]`, *optional*, defaults to `Ovis2VisionConfig`):122 The config object or dictionary of the vision backbone.123 text_config (`Union[AutoConfig, dict]`, *optional*, defaults to `Qwen2Config`):124 The config object or dictionary of the text backbone.125 image_token_id (`int`, *optional*, defaults to 151665):126 The image token id to encode the image prompt.127 visual_indicator_token_ids (`List[int]`, *optional*, defaults to `[151666, 151667, 151668, 151669, 151670]`):128 The visual indicator token ids to encode the image prompt.129 vocab_size (`int`, *optional*, defaults to 151643):130 Vocabulary size of the text model.131 hidden_size (`int`, *optional*, defaults to 1536):132 Dimensionality of the encoder layers and the pooler layer.133 134 ```python135 >>> from transformers import Ovis2ForConditionalGeneration, Ovis2Config136 137 >>> # Initializing a Ovis2 style configuration138 >>> configuration = Ovis2Config()139 140 >>> # Initializing a model from the Ovis2-2B style configuration141 >>> model = Ovis2ForConditionalGeneration(configuration)142 143 >>> # Accessing the model configuration144 >>> configuration = model.config145 ```146 """147 148 model_type = "ovis2"149 sub_configs = {"text_config": Qwen2Config, "vision_config": Ovis2VisionConfig}150 151 def __init__(152 self,153 vision_config=None,154 text_config=None,155 image_token_id=151665,156 visual_indicator_token_ids=[151666, 151667, 151668, 151669, 151670],157 vocab_size=151643,158 hidden_size=1536,159 **kwargs,160 ):161 if isinstance(vision_config, dict):162 self.vision_config = Ovis2VisionConfig(**vision_config)163 elif isinstance(vision_config, Ovis2VisionConfig):164 self.vision_config = vision_config165 if vision_config is None:166 self.vision_config = Ovis2VisionConfig(num_visual_indicator_tokens=len(visual_indicator_token_ids))167 168 if isinstance(text_config, dict):169 self.text_config = Qwen2Config(**text_config)170 elif isinstance(text_config, Qwen2Config):171 self.text_config = text_config172 elif text_config is None:173 self.text_config = Qwen2Config()174 175 self.vocab_size = vocab_size176 self.hidden_size = hidden_size177 self.image_token_id = image_token_id178 self.visual_indicator_token_ids = visual_indicator_token_ids179 super().__init__(**kwargs)180 181 182__all__ = ["Ovis2VisionConfig", "Ovis2Config"]183 