Apply the per-layer SwiGLU limits from the config
#21
by burakaydinofficial - opened
- modeling_bailing_moe_v3.py +28 -7
modeling_bailing_moe_v3.py
CHANGED
|
@@ -327,11 +327,13 @@ def apply_rotary_pos_emb(q, k, cos, sin, unsqueeze_dim=1):
|
|
| 327 |
|
| 328 |
|
| 329 |
class BailingMoeV3MLP(nn.Module):
|
| 330 |
-
def __init__(self, config: BailingMoeV3Config, intermediate_size: int):
|
| 331 |
super().__init__()
|
| 332 |
self.config = config
|
| 333 |
self.hidden_size = config.hidden_size
|
| 334 |
self.intermediate_size = intermediate_size
|
|
|
|
|
|
|
| 335 |
|
| 336 |
self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
|
| 337 |
self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
|
|
@@ -339,7 +341,12 @@ class BailingMoeV3MLP(nn.Module):
|
|
| 339 |
self.act_fn = ACT2FN[config.hidden_act]
|
| 340 |
|
| 341 |
def forward(self, x):
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
|
| 345 |
class BailingMoeV3Gate(nn.Module):
|
|
@@ -411,21 +418,35 @@ class BailingMoeV3SparseMoeBlock(nn.Module):
|
|
| 411 |
A mixed expert module containing shared experts.
|
| 412 |
"""
|
| 413 |
|
| 414 |
-
def __init__(self, config: BailingMoeV3Config):
|
| 415 |
super().__init__()
|
| 416 |
self.config = config
|
| 417 |
self.num_experts_per_tok = config.num_experts_per_tok
|
|
|
|
| 418 |
self._setup_experts()
|
| 419 |
self.gate = BailingMoeV3Gate(config)
|
| 420 |
if config.num_shared_experts is not None:
|
| 421 |
self.shared_experts = BailingMoeV3MLP(
|
| 422 |
-
config=config,
|
|
|
|
|
|
|
| 423 |
)
|
| 424 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 425 |
def _setup_experts(self):
|
| 426 |
self.experts = nn.ModuleList(
|
| 427 |
[
|
| 428 |
-
BailingMoeV3MLP(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
for _ in range(self.config.num_experts)
|
| 430 |
]
|
| 431 |
)
|
|
@@ -934,7 +955,7 @@ class BailingMoeV3MTPLayer(nn.Module):
|
|
| 934 |
self.eh_proj = nn.Linear(config.hidden_size * 2, config.hidden_size, bias=False)
|
| 935 |
self.post_attention_layernorm = BailingMoeV3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 936 |
self.attention = BailingMoeV3MultiLatentAttention(config=config, layer_idx=layer_idx)
|
| 937 |
-
self.mlp = BailingMoeV3SparseMoeBlock(config)
|
| 938 |
|
| 939 |
self.hnorm = BailingMoeV3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 940 |
self.final_layernorm = BailingMoeV3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
|
@@ -1014,7 +1035,7 @@ class BailingMoeV3DecoderLayer(nn.Module):
|
|
| 1014 |
self.attention = BailingMoeV3KimiDeltaAttention(config=config, layer_idx=layer_idx)
|
| 1015 |
|
| 1016 |
self.mlp = (
|
| 1017 |
-
BailingMoeV3SparseMoeBlock(config)
|
| 1018 |
if (config.num_experts is not None and layer_idx >= config.first_k_dense_replace)
|
| 1019 |
else BailingMoeV3MLP(config=config, intermediate_size=config.intermediate_size)
|
| 1020 |
)
|
|
|
|
| 327 |
|
| 328 |
|
| 329 |
class BailingMoeV3MLP(nn.Module):
|
| 330 |
+
def __init__(self, config: BailingMoeV3Config, intermediate_size: int, swiglu_limit: Optional[float] = None):
|
| 331 |
super().__init__()
|
| 332 |
self.config = config
|
| 333 |
self.hidden_size = config.hidden_size
|
| 334 |
self.intermediate_size = intermediate_size
|
| 335 |
+
# this layer's SwiGLU clamp limit from config.*_swiglu_limit_list; None means no clamp
|
| 336 |
+
self.swiglu_limit = swiglu_limit
|
| 337 |
|
| 338 |
self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
|
| 339 |
self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
|
|
|
|
| 341 |
self.act_fn = ACT2FN[config.hidden_act]
|
| 342 |
|
| 343 |
def forward(self, x):
|
| 344 |
+
gate = self.act_fn(self.gate_proj(x))
|
| 345 |
+
up = self.up_proj(x)
|
| 346 |
+
if self.swiglu_limit is not None:
|
| 347 |
+
gate = gate.clamp(max=self.swiglu_limit)
|
| 348 |
+
up = up.clamp(min=-self.swiglu_limit, max=self.swiglu_limit)
|
| 349 |
+
return self.down_proj(gate * up)
|
| 350 |
|
| 351 |
|
| 352 |
class BailingMoeV3Gate(nn.Module):
|
|
|
|
| 418 |
A mixed expert module containing shared experts.
|
| 419 |
"""
|
| 420 |
|
| 421 |
+
def __init__(self, config: BailingMoeV3Config, layer_idx: Optional[int] = None):
|
| 422 |
super().__init__()
|
| 423 |
self.config = config
|
| 424 |
self.num_experts_per_tok = config.num_experts_per_tok
|
| 425 |
+
self.expert_swiglu_limit = self._swiglu_limit(getattr(config, "expert_swiglu_limit_list", None), layer_idx)
|
| 426 |
self._setup_experts()
|
| 427 |
self.gate = BailingMoeV3Gate(config)
|
| 428 |
if config.num_shared_experts is not None:
|
| 429 |
self.shared_experts = BailingMoeV3MLP(
|
| 430 |
+
config=config,
|
| 431 |
+
intermediate_size=config.moe_shared_expert_intermediate_size * config.num_shared_experts,
|
| 432 |
+
swiglu_limit=self._swiglu_limit(getattr(config, "share_expert_swiglu_limit_list", None), layer_idx),
|
| 433 |
)
|
| 434 |
|
| 435 |
+
@staticmethod
|
| 436 |
+
def _swiglu_limit(limit_list, layer_idx):
|
| 437 |
+
"""The layer's SwiGLU limit from a per-layer list (0, a missing list or an index past its end: none)."""
|
| 438 |
+
if limit_list is None or layer_idx is None or not 0 <= layer_idx < len(limit_list):
|
| 439 |
+
return None
|
| 440 |
+
return limit_list[layer_idx] or None
|
| 441 |
+
|
| 442 |
def _setup_experts(self):
|
| 443 |
self.experts = nn.ModuleList(
|
| 444 |
[
|
| 445 |
+
BailingMoeV3MLP(
|
| 446 |
+
config=self.config,
|
| 447 |
+
intermediate_size=self.config.moe_intermediate_size,
|
| 448 |
+
swiglu_limit=self.expert_swiglu_limit,
|
| 449 |
+
)
|
| 450 |
for _ in range(self.config.num_experts)
|
| 451 |
]
|
| 452 |
)
|
|
|
|
| 955 |
self.eh_proj = nn.Linear(config.hidden_size * 2, config.hidden_size, bias=False)
|
| 956 |
self.post_attention_layernorm = BailingMoeV3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 957 |
self.attention = BailingMoeV3MultiLatentAttention(config=config, layer_idx=layer_idx)
|
| 958 |
+
self.mlp = BailingMoeV3SparseMoeBlock(config, layer_idx=layer_idx)
|
| 959 |
|
| 960 |
self.hnorm = BailingMoeV3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 961 |
self.final_layernorm = BailingMoeV3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
|
|
|
| 1035 |
self.attention = BailingMoeV3KimiDeltaAttention(config=config, layer_idx=layer_idx)
|
| 1036 |
|
| 1037 |
self.mlp = (
|
| 1038 |
+
BailingMoeV3SparseMoeBlock(config, layer_idx=layer_idx)
|
| 1039 |
if (config.num_experts is not None and layer_idx >= config.first_k_dense_replace)
|
| 1040 |
else BailingMoeV3MLP(config=config, intermediate_size=config.intermediate_size)
|
| 1041 |
)
|