| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | from typing import Any |
| 5 | |
| 6 | from transformers import PreTrainedConfig |
| 7 | from transformers.models.auto import CONFIG_MAPPING |
| 8 | |
| 9 | |
| 10 | class QianfanOCRVisionConfig(PreTrainedConfig): |
| 11 | model_type = "qianfan_ocr_vision" |
| 12 | |
| 13 | def __init__( |
| 14 | self, |
| 15 | hidden_size: int = 1024, |
| 16 | intermediate_size: int = 4096, |
| 17 | num_hidden_layers: int = 24, |
| 18 | num_attention_heads: int = 16, |
| 19 | num_channels: int = 3, |
| 20 | image_size: int = 448, |
| 21 | patch_size: int = 14, |
| 22 | hidden_act: str = "gelu", |
| 23 | layer_norm_eps: float = 1e-6, |
| 24 | attention_dropout: float = 0.0, |
| 25 | drop_path_rate: float = 0.1, |
| 26 | qkv_bias: bool = True, |
| 27 | qk_normalization: bool = False, |
| 28 | norm_type: str = "layer_norm", |
| 29 | initializer_range: float = 0.02, |
| 30 | initializer_factor: float = 0.1, |
| 31 | use_mask_token: bool = False, |
| 32 | use_mean_pooling: bool = True, |
| 33 | **kwargs: Any, |
| 34 | ): |
| 35 | super().__init__(**kwargs) |
| 36 | self.hidden_size = hidden_size |
| 37 | self.intermediate_size = intermediate_size |
| 38 | self.num_hidden_layers = num_hidden_layers |
| 39 | self.num_attention_heads = num_attention_heads |
| 40 | self.num_channels = num_channels |
| 41 | self.image_size = image_size |
| 42 | self.patch_size = patch_size |
| 43 | self.hidden_act = hidden_act |
| 44 | self.layer_norm_eps = layer_norm_eps |
| 45 | self.attention_dropout = attention_dropout |
| 46 | self.drop_path_rate = drop_path_rate |
| 47 | self.qkv_bias = qkv_bias |
| 48 | self.qk_normalization = qk_normalization |
| 49 | self.norm_type = norm_type |
| 50 | self.initializer_range = initializer_range |
| 51 | self.initializer_factor = initializer_factor |
| 52 | self.use_mask_token = use_mask_token |
| 53 | self.use_mean_pooling = use_mean_pooling |
| 54 | |
| 55 | |
| 56 | class QianfanOCRConfig(PreTrainedConfig): |
| 57 | model_type = "qianfan_ocr" |
| 58 | |
| 59 | def __init__( |
| 60 | self, |
| 61 | vision_config: dict | None = None, |
| 62 | text_config: dict | None = None, |
| 63 | downsample_ratio: float = 0.5, |
| 64 | dynamic_image_size: bool = True, |
| 65 | force_image_size: int = 448, |
| 66 | image_token_id: int = 151671, |
| 67 | max_dynamic_patch: int = 12, |
| 68 | min_dynamic_patch: int = 1, |
| 69 | pad2square: bool = False, |
| 70 | ps_version: str = "v2", |
| 71 | select_layer: int = -1, |
| 72 | template: str = "internvl2_5", |
| 73 | use_thumbnail: bool = True, |
| 74 | tie_word_embeddings: bool = False, |
| 75 | **kwargs: Any, |
| 76 | ): |
| 77 | super().__init__(**kwargs) |
| 78 | |
| 79 | if isinstance(vision_config, dict): |
| 80 | self.vision_config = QianfanOCRVisionConfig(**vision_config) |
| 81 | elif vision_config is None: |
| 82 | self.vision_config = QianfanOCRVisionConfig() |
| 83 | else: |
| 84 | self.vision_config = vision_config |
| 85 | |
| 86 | if isinstance(text_config, dict): |
| 87 | model_type = text_config.get("model_type", "qwen3") |
| 88 | self.text_config = CONFIG_MAPPING[model_type](**text_config) |
| 89 | elif text_config is None: |
| 90 | self.text_config = CONFIG_MAPPING["qwen3"]() |
| 91 | else: |
| 92 | self.text_config = text_config |
| 93 | |
| 94 | self.downsample_ratio = downsample_ratio |
| 95 | self.dynamic_image_size = dynamic_image_size |
| 96 | self.force_image_size = force_image_size |
| 97 | self.image_token_id = image_token_id |
| 98 | self.max_dynamic_patch = max_dynamic_patch |
| 99 | self.min_dynamic_patch = min_dynamic_patch |
| 100 | self.pad2square = pad2square |
| 101 | self.ps_version = ps_version |
| 102 | self.select_layer = select_layer |
| 103 | self.template = template |
| 104 | self.use_thumbnail = use_thumbnail |
| 105 | self.tie_word_embeddings = tie_word_embeddings |