Apply the per-layer SwiGLU limits from the config
What's wrong
Ling 3.0 configs can set per-layer SwiGLU limits, expert_swiglu_limit_list (routed experts) andshare_expert_swiglu_limit_list (shared experts), but nothing in modeling_bailing_moe_v3.py reads them:BailingMoeV3MLP.forward is the plain down_proj(act_fn(gate_proj(x)) * up_proj(x)). SGLang applies them
(python/sglang/srt/models/bailing_moe_v3.py): the gate is clamped after SiLU and the up projection
symmetrically, silu(gate).clamp(max=L) * up.clamp(-L, L), with each layer's limit taken aslimit_list[layer] or None. The open transformers PR
#50574 applies the same per-layer limits, in the
same form, to the native BailingHybrid implementation.
For Ling-3.0-tiny itself this changes nothing, because both lists are null in its config; it matters because
Ling-3.0-flash-FP8 and -INT4 ship this same file with configs that set the limits, so the shared file is correct
for them once they carry the updated copy.
The fix
BailingMoeV3MLPtakes an optionalswiglu_limit. With one, it computesdown_proj(silu(gate).clamp(max=L) * up.clamp(-L, L)); without one, the code path is unchanged.BailingMoeV3SparseMoeBlocktakes the layer index and picks the layer's limits with SGLang's rule: a 0, a
missing list or an index past the list's end means no limit. It hands them to every routed expert and to the
shared experts.- The decoder layers and the MTP layer pass their
layer_idx; an index past the lists, like the MTP layer's
in Ling-3.0-flash's configs, gets no limit. (SGLang builds its MTP layer with layer index 0, whose entries
are 0 there, so it gets none either.) The dense layers are unchanged. - Both new constructor arguments are optional and default to the previous behavior.
+28 / −7. A config without the lists, or with zeros or null, gives exactly the current model.
How it was checked
No check on the real weights was made, for Ling-3.0-tiny (where the change is a no-op) or for the FP8 / INT4
flash repos. The checks are on the code itself (CPU, no weights):
- with Ling-3.0-tiny's config, every one of its 23 MoE layers gets no limit, so the model is unchanged;
BailingMoeV3MLPwith a limit of 4, on inputs that go past it, equalssilu(gate).clamp(max=4) * up.clamp(-4, 4)exactly; without a limit it equals the original formula;- the per-layer rule handles 0, a value, an index past the end and a missing list;
- with Ling-3.0-flash's config (whose limit lists the FP8 / INT4 repos carry), every MoE layer's (expert,
shared) limits equal what SGLang takes.
Check it yourself
No weights needed: the block is built on the meta device, and only one shared-expert MLP gets random weights.
This uses a config that sets the limits. It loads the modeling file as the model does, so it needs the model's
dependencies (flash-linear-attention, einops); tested with transformers 4.57.6.
import torch
from torch.nn.functional import silu
from transformers import AutoConfig
from transformers.dynamic_module_utils import get_class_from_dynamic_module
cfg = AutoConfig.from_pretrained("inclusionAI/Ling-3.0-flash-FP8", trust_remote_code=True) # sets the limits
Block = get_class_from_dynamic_module("modeling_bailing_moe_v3.BailingMoeV3SparseMoeBlock",
"inclusionAI/Ling-3.0-tiny", revision="refs/pr/21") # this PR
with torch.device("meta"):
block = Block(cfg, layer_idx=40)
mlp, L = block.shared_experts.to_empty(device="cpu"), block.shared_experts.swiglu_limit
print("layer 40 limits (experts, shared):", block.experts[0].swiglu_limit, L) # 4 7
for lin in (mlp.gate_proj, mlp.up_proj, mlp.down_proj):
torch.nn.init.normal_(lin.weight, std=0.05)
x = torch.randn(8, cfg.hidden_size)
print((mlp(x) - mlp.down_proj(silu(mlp.gate_proj(x)).clamp(max=L) * mlp.up_proj(x).clamp(-L, L))).abs().max().item()) # 0.0
On main, BailingMoeV3SparseMoeBlock takes no layer index, so the block cannot know its layer's limits.
Ling-3.0-flash has its own copy of this file; the same change is proposed there in
https://huggingface.co/inclusionAI/Ling-3.0-flash/discussions/21.