Upload modeling.py
Browse files- modeling.py +105 -0
modeling.py
CHANGED
|
@@ -28,6 +28,111 @@ except ImportError:
|
|
| 28 |
)
|
| 29 |
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
class Qwen35MoeConfig(_BaseConfig):
|
| 32 |
"""Universal config shim. Delegates everything to PretrainedConfig
|
| 33 |
which reads all fields from config.json via ``**kwargs``."""
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
|
| 31 |
+
_LAYER_PATTERN = (["linear_attention"] * 3 + ["full_attention"]) * 10
|
| 32 |
+
|
| 33 |
+
LOCKED_TEXT_FIELDS = {
|
| 34 |
+
"hidden_size": 2048,
|
| 35 |
+
"num_hidden_layers": 40,
|
| 36 |
+
"num_attention_heads": 16,
|
| 37 |
+
"num_key_value_heads": 2,
|
| 38 |
+
"vocab_size": 248320,
|
| 39 |
+
"num_experts": 256,
|
| 40 |
+
"num_experts_per_tok": 8,
|
| 41 |
+
"moe_intermediate_size": 512,
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class Qwen35MoeTextConfig:
|
| 46 |
+
"""Nested language-model config (``text_config`` in the JSON).
|
| 47 |
+
|
| 48 |
+
This is kept as a standalone dataclass-like container for callers
|
| 49 |
+
that need to introspect text-level defaults without instantiating
|
| 50 |
+
the full ``PretrainedConfig`` chain.
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
model_type = "qwen3_5_moe_text"
|
| 54 |
+
|
| 55 |
+
def __init__(
|
| 56 |
+
self,
|
| 57 |
+
hidden_size: int = 2048,
|
| 58 |
+
num_hidden_layers: int = 40,
|
| 59 |
+
num_attention_heads: int = 16,
|
| 60 |
+
num_key_value_heads: int = 2,
|
| 61 |
+
vocab_size: int = 248320,
|
| 62 |
+
head_dim: int = 256,
|
| 63 |
+
hidden_act: str = "silu",
|
| 64 |
+
intermediate_size: int = 2048,
|
| 65 |
+
moe_intermediate_size: int = 512,
|
| 66 |
+
shared_expert_intermediate_size: int = 512,
|
| 67 |
+
num_experts: int = 256,
|
| 68 |
+
num_experts_per_tok: int = 8,
|
| 69 |
+
max_position_embeddings: int = 262144,
|
| 70 |
+
rms_norm_eps: float = 1e-6,
|
| 71 |
+
rope_theta: float = 1000000.0,
|
| 72 |
+
partial_rotary_factor: float = 0.25,
|
| 73 |
+
attention_bias: bool = False,
|
| 74 |
+
attention_dropout: float = 0.0,
|
| 75 |
+
attn_output_gate: bool = True,
|
| 76 |
+
full_attention_interval: int = 4,
|
| 77 |
+
layer_types: Optional[List[str]] = None,
|
| 78 |
+
linear_conv_kernel_dim: int = 4,
|
| 79 |
+
linear_key_head_dim: int = 128,
|
| 80 |
+
linear_num_key_heads: int = 16,
|
| 81 |
+
linear_num_value_heads: int = 32,
|
| 82 |
+
linear_value_head_dim: int = 128,
|
| 83 |
+
mtp_num_hidden_layers: int = 1,
|
| 84 |
+
mtp_use_dedicated_embeddings: bool = False,
|
| 85 |
+
tie_word_embeddings: bool = False,
|
| 86 |
+
initializer_range: float = 0.02,
|
| 87 |
+
use_cache: bool = True,
|
| 88 |
+
router_aux_loss_coef: float = 0.001,
|
| 89 |
+
output_router_logits: bool = False,
|
| 90 |
+
bos_token_id: int = 248044,
|
| 91 |
+
eos_token_id: int = 248044,
|
| 92 |
+
dtype: str = "bfloat16",
|
| 93 |
+
**kwargs,
|
| 94 |
+
):
|
| 95 |
+
self.hidden_size = hidden_size
|
| 96 |
+
self.num_hidden_layers = num_hidden_layers
|
| 97 |
+
self.num_attention_heads = num_attention_heads
|
| 98 |
+
self.num_key_value_heads = num_key_value_heads
|
| 99 |
+
self.vocab_size = vocab_size
|
| 100 |
+
self.head_dim = head_dim
|
| 101 |
+
self.hidden_act = hidden_act
|
| 102 |
+
self.intermediate_size = intermediate_size
|
| 103 |
+
self.moe_intermediate_size = moe_intermediate_size
|
| 104 |
+
self.shared_expert_intermediate_size = shared_expert_intermediate_size
|
| 105 |
+
self.num_experts = num_experts
|
| 106 |
+
self.num_experts_per_tok = num_experts_per_tok
|
| 107 |
+
self.max_position_embeddings = max_position_embeddings
|
| 108 |
+
self.rms_norm_eps = rms_norm_eps
|
| 109 |
+
self.rope_theta = rope_theta
|
| 110 |
+
self.partial_rotary_factor = partial_rotary_factor
|
| 111 |
+
self.attention_bias = attention_bias
|
| 112 |
+
self.attention_dropout = attention_dropout
|
| 113 |
+
self.attn_output_gate = attn_output_gate
|
| 114 |
+
self.full_attention_interval = full_attention_interval
|
| 115 |
+
self.layer_types = layer_types or list(_LAYER_PATTERN)
|
| 116 |
+
self.linear_conv_kernel_dim = linear_conv_kernel_dim
|
| 117 |
+
self.linear_key_head_dim = linear_key_head_dim
|
| 118 |
+
self.linear_num_key_heads = linear_num_key_heads
|
| 119 |
+
self.linear_num_value_heads = linear_num_value_heads
|
| 120 |
+
self.linear_value_head_dim = linear_value_head_dim
|
| 121 |
+
self.mtp_num_hidden_layers = mtp_num_hidden_layers
|
| 122 |
+
self.mtp_use_dedicated_embeddings = mtp_use_dedicated_embeddings
|
| 123 |
+
self.tie_word_embeddings = tie_word_embeddings
|
| 124 |
+
self.initializer_range = initializer_range
|
| 125 |
+
self.use_cache = use_cache
|
| 126 |
+
self.router_aux_loss_coef = router_aux_loss_coef
|
| 127 |
+
self.output_router_logits = output_router_logits
|
| 128 |
+
self.bos_token_id = bos_token_id
|
| 129 |
+
self.eos_token_id = eos_token_id
|
| 130 |
+
self.dtype = dtype
|
| 131 |
+
|
| 132 |
+
def to_dict(self) -> Dict[str, Any]:
|
| 133 |
+
return {k: v for k, v in self.__dict__.items() if not k.startswith("_")}
|
| 134 |
+
|
| 135 |
+
|
| 136 |
class Qwen35MoeConfig(_BaseConfig):
|
| 137 |
"""Universal config shim. Delegates everything to PretrainedConfig
|
| 138 |
which reads all fields from config.json via ``**kwargs``."""
|