Apply the per-layer SwiGLU limits from config.json

#21

What's wrong

config.json sets per-layer SwiGLU limits: expert_swiglu_limit_list (the routed experts: 4 in layers
35–41) and share_expert_swiglu_limit_list (the shared experts: 5 in layers 34–39, 7 in layers 40–41).
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 as
limit_list[layer] or None. So in those layers the Transformers code computes a different function from
SGLang's, wherever an activation passes its limit. The open transformers PR
#50574 applies the same per-layer limits, in the
same form, to the native BailingHybrid implementation.

The fix

  • BailingMoeV3MLP takes an optional swiglu_limit. With one, it computes
    down_proj(silu(gate).clamp(max=L) * up.clamp(-L, L)); without one, the code path is unchanged.
  • BailingMoeV3SparseMoeBlock takes 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. The MTP layer's index, 42, is past the
    42-entry lists, so it gets no limit; SGLang builds its MTP layer with layer index 0, whose entries are 0, so
    it gets none there 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, gives exactly the current model.

How it was checked

No check on the real weights was made: comparing this model's outputs with SGLang's on the same prompts needs
hardware for the full model, which I did not run. The checks are on the code itself (CPU, no weights):

  • BailingMoeV3MLP with a limit of 4, on inputs that go past it, equals
    silu(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 this repo's config.json, every MoE layer's (expert, shared) limits equal what SGLang takes:
    experts 4 or none, shared experts 5, 7 or none.

Check it yourself

No weights needed: the block is built on the meta device, and only one shared-expert MLP gets random weights.
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
repo, rev = "inclusionAI/Ling-3.0-flash", "refs/pr/21"  # this PR
cfg = AutoConfig.from_pretrained(repo, revision=rev, trust_remote_code=True)
Block = get_class_from_dynamic_module("modeling_bailing_moe_v3.BailingMoeV3SparseMoeBlock", repo, revision=rev)
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-FP8 and -INT4 ship Ling-3.0-tiny's version of this file, with configs that set the same limits;
the same change is proposed for that file in https://huggingface.co/inclusionAI/Ling-3.0-tiny/discussions/21.

burakaydinofficial changed pull request status to open
Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment