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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 2 |
+
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
| 3 |
+
# Modified from HunyuanVL configuration for BrainOCR.
|
| 4 |
+
|
| 5 |
+
from transformers import PretrainedConfig
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class BrainOCRVisionConfig(PretrainedConfig):
|
| 9 |
+
model_type = "brain_ocr"
|
| 10 |
+
base_config_key = "vision_config"
|
| 11 |
+
|
| 12 |
+
def __init__(
|
| 13 |
+
self,
|
| 14 |
+
hidden_act="gelu",
|
| 15 |
+
hidden_size=1152,
|
| 16 |
+
intermediate_size=4304,
|
| 17 |
+
interpolate_mode="bilinear",
|
| 18 |
+
rms_norm_eps=1e-05,
|
| 19 |
+
learnable_mlp_pooling_size=0,
|
| 20 |
+
num_attention_heads=16,
|
| 21 |
+
num_key_value_heads=None,
|
| 22 |
+
num_channels=3,
|
| 23 |
+
num_hidden_layers=27,
|
| 24 |
+
out_hidden_size=4096,
|
| 25 |
+
patch_size=16,
|
| 26 |
+
remove_prenorm=True,
|
| 27 |
+
spatial_merge_size=2,
|
| 28 |
+
temporal_patch_size=1,
|
| 29 |
+
resize_resolution=2048,
|
| 30 |
+
img_max_token_num=4096,
|
| 31 |
+
max_image_size=2048,
|
| 32 |
+
video_max_image_size=768,
|
| 33 |
+
video_min_image_size=256,
|
| 34 |
+
min_image_size=512,
|
| 35 |
+
anyres_vit_max_image_size=2048,
|
| 36 |
+
max_vit_seq_len=16384,
|
| 37 |
+
text_hidden_size=3072,
|
| 38 |
+
**kwargs,
|
| 39 |
+
):
|
| 40 |
+
super().__init__(**kwargs)
|
| 41 |
+
|
| 42 |
+
self.hidden_act = hidden_act
|
| 43 |
+
self.hidden_size = hidden_size
|
| 44 |
+
self.intermediate_size = intermediate_size
|
| 45 |
+
self.interpolate_mode = interpolate_mode
|
| 46 |
+
self.learnable_mlp_pooling_size = learnable_mlp_pooling_size
|
| 47 |
+
self.num_attention_heads = num_attention_heads
|
| 48 |
+
if not num_key_value_heads:
|
| 49 |
+
self.num_key_value_heads = num_attention_heads
|
| 50 |
+
else:
|
| 51 |
+
self.num_key_value_heads = num_key_value_heads
|
| 52 |
+
self.num_channels = num_channels
|
| 53 |
+
self.num_hidden_layers = num_hidden_layers
|
| 54 |
+
self.out_hidden_size = out_hidden_size
|
| 55 |
+
self.patch_size = patch_size
|
| 56 |
+
self.remove_prenorm = remove_prenorm
|
| 57 |
+
self.spatial_merge_size = spatial_merge_size
|
| 58 |
+
self.temporal_patch_size = temporal_patch_size
|
| 59 |
+
self.rms_norm_eps = rms_norm_eps
|
| 60 |
+
|
| 61 |
+
self.resize_resolution = resize_resolution
|
| 62 |
+
self.img_max_token_num = img_max_token_num
|
| 63 |
+
self.max_image_size = max_image_size
|
| 64 |
+
self.min_image_size = min_image_size
|
| 65 |
+
self.video_max_image_size = video_max_image_size
|
| 66 |
+
self.video_min_image_size = video_min_image_size
|
| 67 |
+
self.anyres_vit_max_image_size = anyres_vit_max_image_size
|
| 68 |
+
self.max_vit_seq_len = max_vit_seq_len
|
| 69 |
+
self.text_hidden_size = text_hidden_size
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class BrainOCRTextConfig(PretrainedConfig):
|
| 73 |
+
r"""
|
| 74 |
+
Configuration class for BrainOCR text model.
|
| 75 |
+
|
| 76 |
+
Args:
|
| 77 |
+
vocab_size (`int`, *optional*, defaults to 290943):
|
| 78 |
+
Vocabulary size of the model.
|
| 79 |
+
hidden_size (`int`, *optional*, defaults to 4096):
|
| 80 |
+
Dimension of the hidden representations.
|
| 81 |
+
intermediate_size (`int`, *optional*, defaults to 11008):
|
| 82 |
+
Dimension of the MLP representations.
|
| 83 |
+
num_hidden_layers (`int`, *optional*, defaults to 32):
|
| 84 |
+
Number of hidden layers in the Transformer decoder.
|
| 85 |
+
num_attention_heads (`int`, *optional*, defaults to 32):
|
| 86 |
+
Number of attention heads for each attention layer.
|
| 87 |
+
num_key_value_heads (`int`, *optional*):
|
| 88 |
+
Number of key_value heads for Grouped Query Attention.
|
| 89 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
|
| 90 |
+
The non-linear activation function in the decoder.
|
| 91 |
+
max_position_embeddings (`int`, *optional*, defaults to 2048):
|
| 92 |
+
The maximum sequence length.
|
| 93 |
+
rms_norm_eps (`float`, *optional*, defaults to 1e-05):
|
| 94 |
+
The epsilon used by the rms normalization layers.
|
| 95 |
+
rope_theta (`float`, *optional*, defaults to 10000.0):
|
| 96 |
+
The base period of the RoPE embeddings.
|
| 97 |
+
head_dim (`int`, *optional*, defaults to 128):
|
| 98 |
+
The attention head dimension.
|
| 99 |
+
"""
|
| 100 |
+
|
| 101 |
+
model_type = "brain_ocr_text"
|
| 102 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
| 103 |
+
|
| 104 |
+
def __init__(
|
| 105 |
+
self,
|
| 106 |
+
vocab_size=290943,
|
| 107 |
+
hidden_size=4096,
|
| 108 |
+
intermediate_size: int = 11008,
|
| 109 |
+
num_hidden_layers=32,
|
| 110 |
+
num_attention_heads=32,
|
| 111 |
+
num_key_value_heads=None,
|
| 112 |
+
hidden_act="silu",
|
| 113 |
+
max_position_embeddings=2048,
|
| 114 |
+
initializer_range=0.02,
|
| 115 |
+
rms_norm_eps=1e-5,
|
| 116 |
+
use_cache=True,
|
| 117 |
+
pad_token_id=0,
|
| 118 |
+
bos_token_id=1,
|
| 119 |
+
eos_token_id=2,
|
| 120 |
+
eod_token_id=3,
|
| 121 |
+
pretraining_tp=1,
|
| 122 |
+
tie_word_embeddings=False,
|
| 123 |
+
rope_theta=10000.0,
|
| 124 |
+
rope_scaling=None,
|
| 125 |
+
attention_bias=False,
|
| 126 |
+
attention_dropout=0.0,
|
| 127 |
+
head_dim=None,
|
| 128 |
+
**kwargs,
|
| 129 |
+
):
|
| 130 |
+
self.vocab_size = vocab_size
|
| 131 |
+
self.max_position_embeddings = max_position_embeddings
|
| 132 |
+
self.hidden_size = hidden_size
|
| 133 |
+
self.intermediate_size = intermediate_size
|
| 134 |
+
self.num_hidden_layers = num_hidden_layers
|
| 135 |
+
self.num_attention_heads = num_attention_heads
|
| 136 |
+
self.head_dim = head_dim
|
| 137 |
+
if num_key_value_heads is None:
|
| 138 |
+
num_key_value_heads = num_attention_heads
|
| 139 |
+
|
| 140 |
+
self.num_key_value_heads = num_key_value_heads
|
| 141 |
+
self.hidden_act = hidden_act
|
| 142 |
+
self.initializer_range = initializer_range
|
| 143 |
+
self.rms_norm_eps = rms_norm_eps
|
| 144 |
+
self.pretraining_tp = pretraining_tp
|
| 145 |
+
self.use_cache = use_cache
|
| 146 |
+
self.rope_theta = rope_theta
|
| 147 |
+
self.rope_scaling = rope_scaling
|
| 148 |
+
self.attention_bias = attention_bias
|
| 149 |
+
self.attention_dropout = attention_dropout
|
| 150 |
+
|
| 151 |
+
super().__init__(
|
| 152 |
+
pad_token_id=pad_token_id,
|
| 153 |
+
bos_token_id=bos_token_id,
|
| 154 |
+
eos_token_id=eos_token_id,
|
| 155 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 156 |
+
**kwargs,
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
def _rope_scaling_validation(self):
|
| 160 |
+
"""Validate the `rope_scaling` configuration."""
|
| 161 |
+
if self.rope_scaling is None:
|
| 162 |
+
return
|
| 163 |
+
|
| 164 |
+
if not isinstance(self.rope_scaling, dict) or len(self.rope_scaling) != 2:
|
| 165 |
+
raise ValueError(
|
| 166 |
+
"`rope_scaling` must be a dictionary with two fields, `type` and "
|
| 167 |
+
f"`factor` or `type` and `alpha`, got {self.rope_scaling}"
|
| 168 |
+
)
|
| 169 |
+
rope_scaling_type = self.rope_scaling.get("type", None)
|
| 170 |
+
rope_scaling_factor = self.rope_scaling.get("factor", None)
|
| 171 |
+
rope_scaling_alpha = self.rope_scaling.get("alpha", None)
|
| 172 |
+
if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
|
| 173 |
+
raise ValueError(
|
| 174 |
+
"`rope_scaling`'s type field must be one of ['linear', 'dynamic'], "
|
| 175 |
+
f"got {rope_scaling_type}"
|
| 176 |
+
)
|
| 177 |
+
if rope_scaling_factor is None and rope_scaling_alpha is None:
|
| 178 |
+
raise ValueError(
|
| 179 |
+
"`rope_scaling`'s factor or alpha field must be have one, "
|
| 180 |
+
"got both of none"
|
| 181 |
+
)
|
| 182 |
+
if rope_scaling_factor is not None and (
|
| 183 |
+
not isinstance(rope_scaling_factor, float) or rope_scaling_factor <= 1.0
|
| 184 |
+
):
|
| 185 |
+
raise ValueError(
|
| 186 |
+
"`rope_scaling`'s factor field must be a float > 1.0, "
|
| 187 |
+
f"got {rope_scaling_factor}"
|
| 188 |
+
)
|
| 189 |
+
if rope_scaling_alpha is not None and (
|
| 190 |
+
not isinstance(rope_scaling_alpha, float) or rope_scaling_alpha <= 1.0
|
| 191 |
+
):
|
| 192 |
+
raise ValueError(
|
| 193 |
+
"`rope_scaling`'s alpha field must be a float > 1.0, "
|
| 194 |
+
f"got {rope_scaling_alpha}"
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
class BrainOCRConfig(PretrainedConfig):
|
| 199 |
+
model_type = "brain_ocr"
|
| 200 |
+
sub_configs = {
|
| 201 |
+
"vision_config": BrainOCRVisionConfig,
|
| 202 |
+
"text_config": BrainOCRTextConfig,
|
| 203 |
+
}
|
| 204 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
| 205 |
+
|
| 206 |
+
def __init__(
|
| 207 |
+
self,
|
| 208 |
+
text_config=None,
|
| 209 |
+
vision_config=None,
|
| 210 |
+
im_start_id=120118,
|
| 211 |
+
im_end_id=120119,
|
| 212 |
+
image_token_id=120120,
|
| 213 |
+
im_newline_id=120121,
|
| 214 |
+
video_start_id=120122,
|
| 215 |
+
video_end_id=120123,
|
| 216 |
+
**kwargs,
|
| 217 |
+
):
|
| 218 |
+
super().__init__(**kwargs)
|
| 219 |
+
|
| 220 |
+
if isinstance(vision_config, dict):
|
| 221 |
+
self.vision_config = self.sub_configs["vision_config"](**vision_config)
|
| 222 |
+
elif vision_config is None:
|
| 223 |
+
self.vision_config = self.sub_configs["vision_config"]()
|
| 224 |
+
|
| 225 |
+
if isinstance(text_config, dict):
|
| 226 |
+
self.text_config = self.sub_configs["text_config"](**text_config)
|
| 227 |
+
elif text_config is None:
|
| 228 |
+
self.text_config = self.sub_configs["text_config"](**kwargs)
|
| 229 |
+
|
| 230 |
+
self.image_token_id = image_token_id
|
| 231 |
+
self.im_start_id = im_start_id
|
| 232 |
+
self.im_end_id = im_end_id
|
| 233 |
+
self.im_newline_id = im_newline_id
|
| 234 |
+
self.video_start_id = video_start_id
|
| 235 |
+
self.video_end_id = video_end_id
|
| 236 |
+
|
| 237 |
+
self.vision_config.text_hidden_size = self.text_config.hidden_size
|
| 238 |
+
|
| 239 |
+
self._attn_implementation = kwargs.pop("attn_implementation", None)
|
| 240 |
+
|
| 241 |
+
def __setattr__(self, key, value):
|
| 242 |
+
if (
|
| 243 |
+
(text_config := super().__getattribute__("__dict__").get("text_config"))
|
| 244 |
+
is not None
|
| 245 |
+
and key not in ["dtype", "_attn_implementation_internal"]
|
| 246 |
+
and key in text_config.__dict__
|
| 247 |
+
):
|
| 248 |
+
setattr(text_config, key, value)
|
| 249 |
+
else:
|
| 250 |
+
super().__setattr__(key, value)
|
| 251 |
+
|
| 252 |
+
def __getattribute__(self, key):
|
| 253 |
+
if "text_config" in super().__getattribute__("__dict__") and key not in [
|
| 254 |
+
"_name_or_path",
|
| 255 |
+
"model_type",
|
| 256 |
+
"dtype",
|
| 257 |
+
"_attn_implementation_internal",
|
| 258 |
+
]:
|
| 259 |
+
text_config = super().__getattribute__("text_config")
|
| 260 |
+
if key in text_config.__dict__:
|
| 261 |
+
return getattr(text_config, key)
|
| 262 |
+
|
| 263 |
+
return super().__getattribute__(key)
|