_config.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # This file is based on https://github.com/Kwai-Keye/Keye/blob/main/keye-vl-8b-preview/configuration_keye.py
  15. # Original header:
  16. # Licensed under the Apache License, Version 2.0 (the "License");
  17. # you may not use this file except in compliance with the License.
  18. # You may obtain a copy of the License at
  19. #
  20. # http://www.apache.org/licenses/LICENSE-2.0
  21. #
  22. # Unless required by applicable law or agreed to in writing, software
  23. # distributed under the License is distributed on an "AS IS" BASIS,
  24. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. # See the License for the specific language governing permissions and
  26. # limitations under the License.
  27. from ....common.vlm.transformers import PretrainedConfig
  28. class PPOCRVisionConfig(PretrainedConfig):
  29. model_type = "paddleocr_vl"
  30. base_config_key = "vision_config"
  31. def __init__(
  32. self,
  33. hidden_size=768,
  34. intermediate_size=3072,
  35. num_hidden_layers=12,
  36. num_attention_heads=12,
  37. num_channels=3,
  38. image_size=224,
  39. patch_size=14,
  40. hidden_act="gelu_pytorch_tanh",
  41. layer_norm_eps=1e-6,
  42. attention_dropout=0.0,
  43. spatial_merge_size=2,
  44. temporal_patch_size=2,
  45. tokens_per_second=2,
  46. **kwargs,
  47. ):
  48. super().__init__(**kwargs)
  49. self.hidden_size = hidden_size
  50. self.intermediate_size = intermediate_size
  51. self.num_hidden_layers = num_hidden_layers
  52. self.num_attention_heads = num_attention_heads
  53. self.num_channels = num_channels
  54. self.patch_size = patch_size
  55. self.image_size = image_size
  56. self.attention_dropout = attention_dropout
  57. self.layer_norm_eps = layer_norm_eps
  58. self.hidden_act = hidden_act
  59. self.spatial_merge_size = spatial_merge_size
  60. self.temporal_patch_size = temporal_patch_size
  61. self.tokens_per_second = tokens_per_second
  62. class PaddleOCRVLConfig(PretrainedConfig):
  63. model_type = "paddleocr_vl"
  64. keys_to_ignore_at_inference = ["past_key_values"]
  65. sub_configs = {"vision_config": PPOCRVisionConfig}
  66. base_model_tp_plan = {
  67. "layers.*.self_attn.q_proj": "colwise",
  68. "layers.*.self_attn.k_proj": "colwise",
  69. "layers.*.self_attn.v_proj": "colwise",
  70. "layers.*.self_attn.o_proj": "rowwise",
  71. "layers.*.mlp.gate_proj": "colwise",
  72. "layers.*.mlp.up_proj": "colwise",
  73. "layers.*.mlp.down_proj": "rowwise",
  74. }
  75. base_model_pp_plan = {
  76. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  77. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  78. "norm": (["hidden_states"], ["hidden_states"]),
  79. }
  80. def __init__(
  81. self,
  82. vocab_size=32000,
  83. hidden_size=768,
  84. intermediate_size=11008,
  85. max_position_embeddings=32768,
  86. num_hidden_layers=2,
  87. num_attention_heads=2,
  88. image_token_id=101304,
  89. video_token_id=101305,
  90. vision_start_token_id=101306,
  91. rope_scaling=None,
  92. rms_norm_eps=1e-6,
  93. use_cache=False,
  94. use_flash_attention=False,
  95. pad_token_id=0,
  96. bos_token_id=1,
  97. eos_token_id=2,
  98. head_dim=128,
  99. hidden_act="silu",
  100. use_bias=False,
  101. rope_theta=10000,
  102. weight_share_add_bias=True,
  103. ignored_index=-100,
  104. attention_probs_dropout_prob=0.0,
  105. hidden_dropout_prob=0.0,
  106. compression_ratio: float = 1.0,
  107. num_key_value_heads=None,
  108. max_sequence_length=None,
  109. tie_word_embeddings=False,
  110. vision_config=None,
  111. **kwargs,
  112. ):
  113. # Set default for tied embeddings if not specified.
  114. super().__init__(
  115. pad_token_id=pad_token_id,
  116. bos_token_id=bos_token_id,
  117. eos_token_id=eos_token_id,
  118. **kwargs,
  119. )
  120. if isinstance(vision_config, dict):
  121. self.vision_config = self.sub_configs["vision_config"](**vision_config)
  122. elif vision_config is None:
  123. self.vision_config = self.sub_configs["vision_config"]()
  124. self.vocab_size = vocab_size
  125. self.hidden_size = hidden_size
  126. self.intermediate_size = intermediate_size
  127. self.max_position_embeddings = max_position_embeddings
  128. self.num_hidden_layers = num_hidden_layers
  129. self.num_attention_heads = num_attention_heads
  130. self.rope_scaling = rope_scaling
  131. self.rms_norm_eps = rms_norm_eps
  132. self.use_cache = use_cache
  133. self.use_flash_attention = use_flash_attention
  134. self.pad_token_id = pad_token_id
  135. self.bos_token_id = bos_token_id
  136. self.eos_token_id = eos_token_id
  137. self.image_token_id = image_token_id
  138. self.video_token_id = video_token_id
  139. self.vision_start_token_id = vision_start_token_id
  140. self.head_dim = head_dim
  141. if hidden_act != "silu":
  142. raise NotImplementedError
  143. self.hidden_act = hidden_act
  144. self.hidden_size = hidden_size
  145. self.use_bias = use_bias
  146. self.weight_share_add_bias = weight_share_add_bias
  147. self.rope_theta = rope_theta
  148. self.ignored_index = ignored_index
  149. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  150. self.hidden_dropout_prob = hidden_dropout_prob
  151. self.compression_ratio = compression_ratio
  152. self.num_key_value_heads = num_key_value_heads
  153. self.max_sequence_length = max_sequence_length
  154. super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs)
  155. # Currently, these configuration items are hard-coded
  156. from ......utils.env import get_paddle_cuda_version
  157. cuda_version = get_paddle_cuda_version()
  158. if cuda_version and cuda_version[0] > 11:
  159. self.fuse_rms_norm = True
  160. else:
  161. self.fuse_rms_norm = False
  162. self.use_sparse_flash_attn = True
  163. self.use_var_len_flash_attn = False
  164. self.scale_qk_coeff = 1.0
  165. self.fuse_softmax_mask = False
  166. self.use_sparse_head_and_loss_fn = False
  167. self.use_recompute_loss_fn = False
  168. self.use_fused_head_and_loss_fn = False
  169. self.fuse_linear = False
  170. self.token_balance_seqlen = False
  171. self.use_rmsnorm = True
  172. self.fuse_ln = False
  173. self.cachekv_quant = False
  174. self.fuse_swiglu = False
  175. self.freq_allocation = 20