|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright 2025 The Qwen Team and The HuggingFace Inc. team. |
| 3 | +# All rights reserved. |
| 4 | +"""Qwen3.5 (non-MoE) model configuration for vLLM plugin.""" |
| 5 | + |
| 6 | +from transformers.configuration_utils import PretrainedConfig |
| 7 | + |
| 8 | + |
| 9 | +def _layer_type_validation(layer_types, num_hidden_layers): |
| 10 | + if layer_types is not None and num_hidden_layers is not None: |
| 11 | + if len(layer_types) != num_hidden_layers: |
| 12 | + raise ValueError( |
| 13 | + f"Length of layer_types ({len(layer_types)}) must match " |
| 14 | + f"num_hidden_layers ({num_hidden_layers})" |
| 15 | + ) |
| 16 | + |
| 17 | + |
| 18 | +class Qwen3_5TextConfig(PretrainedConfig): |
| 19 | + model_type = "qwen3_5_text" |
| 20 | + keys_to_ignore_at_inference = ["past_key_values"] |
| 21 | + base_config_key = "text_config" |
| 22 | + |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + vocab_size=248320, |
| 26 | + hidden_size=4096, |
| 27 | + intermediate_size=12288, |
| 28 | + num_hidden_layers=32, |
| 29 | + num_attention_heads=16, |
| 30 | + num_key_value_heads=4, |
| 31 | + hidden_act="silu", |
| 32 | + max_position_embeddings=32768, |
| 33 | + initializer_range=0.02, |
| 34 | + rms_norm_eps=1e-6, |
| 35 | + use_cache=True, |
| 36 | + tie_word_embeddings=False, |
| 37 | + rope_parameters=None, |
| 38 | + attention_bias=False, |
| 39 | + attention_dropout=0.0, |
| 40 | + head_dim=256, |
| 41 | + linear_conv_kernel_dim=4, |
| 42 | + linear_key_head_dim=128, |
| 43 | + linear_value_head_dim=128, |
| 44 | + linear_num_key_heads=16, |
| 45 | + linear_num_value_heads=32, |
| 46 | + layer_types=None, |
| 47 | + full_attention_interval=4, |
| 48 | + attn_output_gate=True, |
| 49 | + pad_token_id=None, |
| 50 | + bos_token_id=None, |
| 51 | + eos_token_id=None, |
| 52 | + **kwargs, |
| 53 | + ): |
| 54 | + kwargs.pop("ignore_keys_at_rope_validation", None) |
| 55 | + self.vocab_size = vocab_size |
| 56 | + self.max_position_embeddings = max_position_embeddings |
| 57 | + self.hidden_size = hidden_size |
| 58 | + self.intermediate_size = intermediate_size |
| 59 | + self.num_hidden_layers = num_hidden_layers |
| 60 | + self.num_attention_heads = num_attention_heads |
| 61 | + self.num_key_value_heads = num_key_value_heads |
| 62 | + self.hidden_act = hidden_act |
| 63 | + self.initializer_range = initializer_range |
| 64 | + self.rms_norm_eps = rms_norm_eps |
| 65 | + self.use_cache = use_cache |
| 66 | + self.attention_bias = attention_bias |
| 67 | + self.attention_dropout = attention_dropout |
| 68 | + self.head_dim = head_dim |
| 69 | + self.rope_parameters = rope_parameters |
| 70 | + self.attn_output_gate = attn_output_gate |
| 71 | + |
| 72 | + self.layer_types = layer_types |
| 73 | + if self.layer_types is None: |
| 74 | + self.layer_types = [ |
| 75 | + "linear_attention" |
| 76 | + if (i + 1) % full_attention_interval != 0 |
| 77 | + else "full_attention" |
| 78 | + for i in range(num_hidden_layers) |
| 79 | + ] |
| 80 | + _layer_type_validation(self.layer_types, num_hidden_layers) |
| 81 | + |
| 82 | + self.full_attention_interval = full_attention_interval |
| 83 | + self.linear_conv_kernel_dim = linear_conv_kernel_dim |
| 84 | + self.linear_key_head_dim = linear_key_head_dim |
| 85 | + self.linear_value_head_dim = linear_value_head_dim |
| 86 | + self.linear_num_key_heads = linear_num_key_heads |
| 87 | + self.linear_num_value_heads = linear_num_value_heads |
| 88 | + |
| 89 | + super().__init__( |
| 90 | + pad_token_id=pad_token_id, |
| 91 | + bos_token_id=bos_token_id, |
| 92 | + eos_token_id=eos_token_id, |
| 93 | + tie_word_embeddings=tie_word_embeddings, |
| 94 | + **kwargs, |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +class Qwen3_5VisionConfig(PretrainedConfig): |
| 99 | + model_type = "qwen3_5" |
| 100 | + |
| 101 | + def __init__( |
| 102 | + self, |
| 103 | + depth=27, |
| 104 | + hidden_size=1152, |
| 105 | + hidden_act="gelu_pytorch_tanh", |
| 106 | + intermediate_size=4304, |
| 107 | + num_heads=16, |
| 108 | + in_channels=3, |
| 109 | + patch_size=16, |
| 110 | + spatial_merge_size=2, |
| 111 | + temporal_patch_size=2, |
| 112 | + num_position_embeddings=2304, |
| 113 | + out_hidden_size=4096, |
| 114 | + initializer_range=0.02, |
| 115 | + **kwargs, |
| 116 | + ): |
| 117 | + super().__init__(**kwargs) |
| 118 | + self.depth = depth |
| 119 | + self.hidden_size = hidden_size |
| 120 | + self.hidden_act = hidden_act |
| 121 | + self.intermediate_size = intermediate_size |
| 122 | + self.num_heads = num_heads |
| 123 | + self.in_channels = in_channels |
| 124 | + self.patch_size = patch_size |
| 125 | + self.spatial_merge_size = spatial_merge_size |
| 126 | + self.temporal_patch_size = temporal_patch_size |
| 127 | + self.num_position_embeddings = num_position_embeddings |
| 128 | + self.out_hidden_size = out_hidden_size |
| 129 | + self.initializer_range = initializer_range |
| 130 | + |
| 131 | + |
| 132 | +class Qwen3_5Config(PretrainedConfig): |
| 133 | + model_type = "qwen3_5" |
| 134 | + sub_configs = { |
| 135 | + "vision_config": Qwen3_5VisionConfig, |
| 136 | + "text_config": Qwen3_5TextConfig, |
| 137 | + } |
| 138 | + keys_to_ignore_at_inference = ["past_key_values"] |
| 139 | + |
| 140 | + def __init__( |
| 141 | + self, |
| 142 | + text_config=None, |
| 143 | + vision_config=None, |
| 144 | + image_token_id=248056, |
| 145 | + video_token_id=248057, |
| 146 | + vision_start_token_id=248053, |
| 147 | + vision_end_token_id=248054, |
| 148 | + tie_word_embeddings=False, |
| 149 | + **kwargs, |
| 150 | + ): |
| 151 | + if isinstance(vision_config, dict): |
| 152 | + self.vision_config = self.sub_configs["vision_config"](**vision_config) |
| 153 | + elif vision_config is None: |
| 154 | + self.vision_config = self.sub_configs["vision_config"]() |
| 155 | + |
| 156 | + if isinstance(text_config, dict): |
| 157 | + self.text_config = self.sub_configs["text_config"](**text_config) |
| 158 | + elif text_config is None: |
| 159 | + self.text_config = self.sub_configs["text_config"]() |
| 160 | + |
| 161 | + self.image_token_id = image_token_id |
| 162 | + self.video_token_id = video_token_id |
| 163 | + self.vision_start_token_id = vision_start_token_id |
| 164 | + self.vision_end_token_id = vision_end_token_id |
| 165 | + super().__init__(**kwargs) |
| 166 | + self.tie_word_embeddings = tie_word_embeddings |
| 167 | + |
| 168 | + |
| 169 | +__all__ = ["Qwen3_5Config", "Qwen3_5TextConfig", "Qwen3_5VisionConfig"] |
0 commit comments