configuration_glm4v.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/glm4v/modular_glm4v.py.
  3. # Do NOT edit this file manually as any edits will be overwritten by the generation of
  4. # the file from the modular. If any change should be done, please apply the change to the
  5. # modular_glm4v.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # Copyright 2025 The ZhipuAI Inc. team and HuggingFace Inc. team. All rights reserved.
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License");
  10. # you may not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http://www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS,
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20. from huggingface_hub.dataclasses import strict
  21. from ...configuration_utils import PreTrainedConfig
  22. from ...modeling_rope_utils import RopeParameters
  23. from ...utils import auto_docstring
  24. @auto_docstring(checkpoint="zai-org/GLM-4.1V-9B-Thinking")
  25. @strict
  26. class Glm4vVisionConfig(PreTrainedConfig):
  27. r"""
  28. out_hidden_size (`int`, *optional*, defaults to 4096):
  29. The output hidden size of the vision model.
  30. Example:
  31. ```python
  32. >>> from transformers import Glm4vVisionConfig, Glm4vVisionModel
  33. >>> # Initializing a Glm4vVisionConfig GLM-4.1V-9B style configuration
  34. >>> configuration = Glm4vVisionConfig()
  35. >>> # Initializing a model (with random weights) from the GLM-4.1V-9B configuration
  36. >>> model = Glm4vVisionModel(configuration)
  37. >>> # Accessing the model configuration
  38. >>> configuration = model.config
  39. ```"""
  40. model_type = "glm4v_vision"
  41. base_config_key = "vision_config"
  42. depth: int = 24
  43. hidden_size: int = 1536
  44. hidden_act: str = "silu"
  45. attention_bias: bool = False
  46. attention_dropout: float | int = 0.0
  47. num_heads: int = 12
  48. in_channels: int = 3
  49. image_size: int | list[int] | tuple[int, int] = 336
  50. patch_size: int | list[int] | tuple[int, int] = 14
  51. rms_norm_eps: float = 1e-05
  52. spatial_merge_size: int = 2
  53. temporal_patch_size: int | list[int] | tuple[int, int] = 2
  54. out_hidden_size: int = 4096
  55. intermediate_size: int = 13696
  56. initializer_range: float = 0.02
  57. @auto_docstring(checkpoint="zai-org/GLM-4.1V-9B-Thinking")
  58. @strict
  59. class Glm4vTextConfig(PreTrainedConfig):
  60. r"""
  61. Example:
  62. ```python
  63. >>> from transformers import Glm4vTextModel, Glm4vConfig
  64. >>> # Initializing a GLM-4.1V style configuration
  65. >>> configuration = Glm4vConfig()
  66. >>> # Initializing a model from the GLM-4.1V style configuration
  67. >>> model = Glm4vTextModel(configuration)
  68. >>> # Accessing the model configuration
  69. >>> configuration = model.config
  70. ```"""
  71. model_type = "glm4v_text"
  72. base_config_key = "text_config"
  73. keys_to_ignore_at_inference = ["past_key_values"]
  74. # Default tensor parallel plan for base model `Glm4v`
  75. base_model_tp_plan = {
  76. "layers.*.self_attn.q_proj": "colwise",
  77. "layers.*.self_attn.k_proj": "colwise",
  78. "layers.*.self_attn.v_proj": "colwise",
  79. "layers.*.self_attn.o_proj": "rowwise",
  80. "layers.*.mlp.gate_up_proj": "colwise_gather_output", # we need to replicate here due to the `chunk` operation
  81. "layers.*.mlp.down_proj": "rowwise_split_input", # input is replicated due to the `chunk` operation
  82. }
  83. base_model_pp_plan = {
  84. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  85. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  86. "norm": (["hidden_states"], ["hidden_states"]),
  87. }
  88. ignore_keys_at_rope_validation = {"mrope_section"}
  89. vocab_size: int = 151552
  90. hidden_size: int = 4096
  91. intermediate_size: int = 13696
  92. num_hidden_layers: int = 40
  93. num_attention_heads: int = 32
  94. num_key_value_heads: int | None = 2
  95. hidden_act: str = "silu"
  96. max_position_embeddings: int = 32768
  97. initializer_range: float = 0.02
  98. rms_norm_eps: float = 1e-05
  99. use_cache: bool = True
  100. attention_dropout: float | int = 0.0
  101. rope_parameters: RopeParameters | dict | None = None
  102. pad_token_id: int | None = None
  103. def __post_init__(self, **kwargs):
  104. if self.num_key_value_heads is None:
  105. self.num_key_value_heads = self.num_attention_heads
  106. super().__post_init__(**kwargs)
  107. @auto_docstring(checkpoint="zai-org/GLM-4.1V-9B-Thinking")
  108. @strict
  109. class Glm4vConfig(PreTrainedConfig):
  110. r"""
  111. image_start_token_id (`int`, *optional*, defaults to 151339):
  112. The image start token index to encode the start of image.
  113. image_end_token_id (`int`, *optional*, defaults to 151340):
  114. The image end token index to encode the end of image.
  115. video_start_token_id (`int`, *optional*, defaults to 151341):
  116. The video start token index to encode the start of video.
  117. video_end_token_id (`int`, *optional*, defaults to 151342):
  118. The video end token index to encode the end of video.
  119. ```python
  120. >>> from transformers import Glm4vForConditionalGeneration, Glm4vConfig
  121. >>> # Initializing a GLM-4.1V style configuration
  122. >>> configuration = Glm4vConfig()
  123. >>> # Initializing a model from the GLM-4.1V style configuration
  124. >>> model = Glm4vForConditionalGeneration(configuration)
  125. >>> # Accessing the model configuration
  126. >>> configuration = model.config
  127. ```"""
  128. model_type = "glm4v"
  129. sub_configs = {"vision_config": Glm4vVisionConfig, "text_config": Glm4vTextConfig}
  130. keys_to_ignore_at_inference = ["past_key_values"]
  131. text_config: dict | PreTrainedConfig | None = None
  132. vision_config: dict | PreTrainedConfig | None = None
  133. image_token_id: int = 151343
  134. video_token_id: int = 151344
  135. image_start_token_id: int = 151339
  136. image_end_token_id: int = 151340
  137. video_start_token_id: int = 151341
  138. video_end_token_id: int = 151342
  139. tie_word_embeddings: bool = False
  140. def __post_init__(self, **kwargs):
  141. if isinstance(self.vision_config, dict):
  142. self.vision_config = self.sub_configs["vision_config"](**self.vision_config)
  143. elif self.vision_config is None:
  144. self.vision_config = self.sub_configs["vision_config"](**kwargs)
  145. if isinstance(self.text_config, dict):
  146. self.text_config = self.sub_configs["text_config"](**self.text_config)
  147. elif self.text_config is None:
  148. self.text_config = self.sub_configs["text_config"](**kwargs)
  149. super().__post_init__(**kwargs)
  150. __all__ = ["Glm4vConfig", "Glm4vTextConfig", "Glm4vVisionConfig"]