Duke-de-Artois commited on
Commit
36a80cb
·
verified ·
1 Parent(s): f5e463c

Create configuration_intern_vit.py

Browse files
Files changed (1) hide show
  1. configuration_intern_vit.py +119 -0
configuration_intern_vit.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # --------------------------------------------------------
2
+ # InternVL
3
+ # Copyright (c) 2024 OpenGVLab
4
+ # Licensed under The MIT License [see LICENSE for details]
5
+ # --------------------------------------------------------
6
+
7
+ import os
8
+ from typing import Union
9
+
10
+ from transformers.configuration_utils import PretrainedConfig
11
+ from transformers.utils import logging
12
+
13
+ logger = logging.get_logger(__name__)
14
+
15
+
16
+ class InternVisionConfig(PretrainedConfig):
17
+ r"""
18
+ This is the configuration class to store the configuration of a [`InternVisionModel`]. It is used to
19
+ instantiate a vision encoder according to the specified arguments, defining the model architecture.
20
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
21
+ documentation from [`PretrainedConfig`] for more information.
22
+ Args:
23
+ num_channels (`int`, *optional*, defaults to 3):
24
+ Number of color channels in the input images (e.g., 3 for RGB).
25
+ patch_size (`int`, *optional*, defaults to 14):
26
+ The size (resolution) of each patch.
27
+ image_size (`int`, *optional*, defaults to 224):
28
+ The size (resolution) of each image.
29
+ qkv_bias (`bool`, *optional*, defaults to `False`):
30
+ Whether to add a bias to the queries and values in the self-attention layers.
31
+ hidden_size (`int`, *optional*, defaults to 3200):
32
+ Dimensionality of the encoder layers and the pooler layer.
33
+ num_attention_heads (`int`, *optional*, defaults to 25):
34
+ Number of attention heads for each attention layer in the Transformer encoder.
35
+ intermediate_size (`int`, *optional*, defaults to 12800):
36
+ Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
37
+ qk_normalization (`bool`, *optional*, defaults to `True`):
38
+ Whether to normalize the queries and keys in the self-attention layers.
39
+ num_hidden_layers (`int`, *optional*, defaults to 48):
40
+ Number of hidden layers in the Transformer encoder.
41
+ use_flash_attn (`bool`, *optional*, defaults to `True`):
42
+ Whether to use flash attention mechanism.
43
+ hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
44
+ The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
45
+ `"relu"`, `"selu"` and `"gelu_new"` ``"gelu"` are supported.
46
+ layer_norm_eps (`float`, *optional*, defaults to 1e-6):
47
+ The epsilon used by the layer normalization layers.
48
+ dropout (`float`, *optional*, defaults to 0.0):
49
+ The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
50
+ drop_path_rate (`float`, *optional*, defaults to 0.0):
51
+ Dropout rate for stochastic depth.
52
+ attention_dropout (`float`, *optional*, defaults to 0.0):
53
+ The dropout ratio for the attention probabilities.
54
+ initializer_range (`float`, *optional*, defaults to 0.02):
55
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
56
+ initializer_factor (`float`, *optional*, defaults to 0.1):
57
+ A factor for layer scale.
58
+ """
59
+
60
+ model_type = 'intern_vit_6b'
61
+
62
+ def __init__(
63
+ self,
64
+ num_channels=3,
65
+ patch_size=14,
66
+ image_size=224,
67
+ qkv_bias=False,
68
+ hidden_size=3200,
69
+ num_attention_heads=25,
70
+ intermediate_size=12800,
71
+ qk_normalization=True,
72
+ num_hidden_layers=48,
73
+ use_flash_attn=True,
74
+ hidden_act='gelu',
75
+ norm_type='rms_norm',
76
+ layer_norm_eps=1e-6,
77
+ dropout=0.0,
78
+ drop_path_rate=0.0,
79
+ attention_dropout=0.0,
80
+ initializer_range=0.02,
81
+ initializer_factor=0.1,
82
+ **kwargs,
83
+ ):
84
+ super().__init__(**kwargs)
85
+
86
+ self.hidden_size = hidden_size
87
+ self.intermediate_size = intermediate_size
88
+ self.dropout = dropout
89
+ self.drop_path_rate = drop_path_rate
90
+ self.num_hidden_layers = num_hidden_layers
91
+ self.num_attention_heads = num_attention_heads
92
+ self.num_channels = num_channels
93
+ self.patch_size = patch_size
94
+ self.image_size = image_size
95
+ self.initializer_range = initializer_range
96
+ self.initializer_factor = initializer_factor
97
+ self.attention_dropout = attention_dropout
98
+ self.layer_norm_eps = layer_norm_eps
99
+ self.hidden_act = hidden_act
100
+ self.norm_type = norm_type
101
+ self.qkv_bias = qkv_bias
102
+ self.qk_normalization = qk_normalization
103
+ self.use_flash_attn = use_flash_attn
104
+
105
+ @classmethod
106
+ def from_pretrained(cls, pretrained_model_name_or_path: Union[str, os.PathLike], **kwargs) -> 'PretrainedConfig':
107
+ config_dict, kwargs = cls.get_config_dict(pretrained_model_name_or_path, **kwargs)
108
+
109
+ if 'vision_config' in config_dict:
110
+ config_dict = config_dict['vision_config']
111
+
112
+ if 'model_type' in config_dict and hasattr(cls, 'model_type') and config_dict['model_type'] != cls.model_type:
113
+ logger.warning(
114
+ f"You are using a model of type {config_dict['model_type']} to instantiate a model of type "
115
+ f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'
116
+ )
117
+
118
+ return cls.from_dict(config_dict, **kwargs)
119
+