DeepSpeed icon indicating copy to clipboard operation
DeepSpeed copied to clipboard

[BUG] silently drop the auxiliary loss bias correction term

Open pengdurice opened this issue 3 months ago • 0 comments

Describe the bug

AutoEP silently discards e_score_correction_bias when replacing an MoE block, for most model families. The bias is DeepSeek-V3's auxiliary-loss-free load-balancing term ("noaux_tc"): a [num_experts] tensor added to the router scores after the scoring function, used for top-k selection only.

Dropping it silently resets expert selection to the untrained balance. There is no error, no warning, and loss still descends -- the gating weights themselves are unaffected, only which experts get chosen.

AutoEPMoELayer.__init__ (auto_ep_layer.py):

source_ecb = getattr(source_gate, 'e_score_correction_bias', None)
...
if source_ecb is not None and isinstance(source_ecb, nn.Parameter):
    self.router.e_score_correction_bias = nn.Parameter(source_ecb.data.clone(), ...)

Two independent defects:

1. isinstance(..., nn.Parameter) rejects nn.Buffer. transformers declares this tensor as nn.Buffer in 18 of the 22 model families that have it, including every DeepSeek, GLM, MiniMax and Nemotron variant. nn.Buffer is a plain torch.Tensor subclass, so the check is False and the copy is skipped. This is the larger of the two defects by a wide margin.

2. The lookup only checks the router. The tensor lives in three different places depending on the family, and getattr(source_gate, ...) finds it in only 17 of 22.

Survey (transformers 5.15.1, all 22 families that declare the tensor)

location count families
<block>.gate 17 axk1, axk2, deepseek_v3, deepseek_v32, deepseek_v4, dots1, exaone_moe, glm4_moe, glm4_moe_lite, glm4v_moe, glm_moe_dsa, inkling, laguna, mimo_v2_flash, minimax_m3_vl, nemotron_h, solar_open
<block> directly 2 hy_v3, minimax_m2
<block>.moe_statics 2 ernie4_5_moe, ernie4_5_vl_moe
<block>.router 1 longcat_flash
tensor type count families
nn.Buffer 18 all except the four below
nn.Parameter 4 inkling, laguna, ernie4_5_moe, ernie4_5_vl_moe

Combined effect on the current code:

family found by lookup? copied? net
deepseek_v3 / v32 / v4, glm4_moe, glm_moe_dsa, minimax_m3_vl, nemotron_h, dots1, exaone_moe, solar_open, axk1/2, mimo_v2_flash, glm4_moe_lite, glm4v_moe yes no (Buffer) dropped
inkling, laguna yes yes ok
hy_v3, minimax_m2 no (on block) -- dropped
longcat_flash no (attr is router) -- dropped
ernie4_5_moe, ernie4_5_vl_moe no (attr is moe_statics) -- dropped

20 of 22 families lose the bias. Only inkling and laguna -- the two that use nn.Parameter and put it on gate -- work today.

To Reproduce

Any DeepSeek-V3-family checkpoint under ZeRO-3 + AutoEP. Minimal check without training, on the meta device (no weights allocated, so model size does not matter):

import torch, torch.nn as nn
from transformers import AutoConfig, AutoModelForCausalLM
from deepspeed.module_inject.auto_ep import AutoEP
from deepspeed.module_inject.auto_ep_config import AutoEPConfig

cfg = AutoConfig.from_pretrained(MODEL, trust_remote_code=True)
inner = getattr(cfg, "text_config", cfg)
inner.num_hidden_layers = getattr(inner, "first_k_dense_replace", 0) + 1   # one MoE layer
with torch.device("meta"):
    model = AutoModelForCausalLM.from_config(cfg, trust_remote_code=True)

ep = AutoEPConfig(); ep.enabled = True; ep.autoep_size = 8; ep.preset_model = "deepseek_v3"
spec = AutoEP(model, ep).ep_parser()[0]

moe  = dict(model.named_modules())[spec.moe_module_name]
gate = getattr(moe, spec.router_name)
ecb  = getattr(gate, "e_score_correction_bias", None)
print(type(ecb).__name__, "-> AutoEP would",
      "COPY" if isinstance(ecb, nn.Parameter) else "DROP", "it")

On DeepSeek-V3.2 this prints Tensor -> AutoEP would DROP it (it is an nn.Buffer).

Expected behavior

The bias should be carried across the replacement for every family that has one, preserving parameter-vs-buffer semantics:

  • accept any tensor, not only nn.Parameter;
  • re-register a buffer as a buffer (register_buffer(..., persistent=True)) so it follows .to() and state_dict() without being trained, and a parameter as a parameter;
  • find it wherever the family puts it;
  • warn if it is present but cannot be carried, rather than dropping silently.

Suggested fix

The type half must be fixed in AutoEPMoELayer regardless -- it is copy semantics, not location.

For the location half, note that every other "where does this live for this model" question is already resolved once at detection and recorded on MoELayerSpec -- expert_storage, expert_w1_name, has_shared_experts, shared_experts_name. This lookup is the one exception, and it does structure discovery at construction time instead.

A resolved path on the spec would make it consistent and cover all 22 families:

e_score_correction_bias_path: str | None = None   # "gate" | "router" | "moe_statics" | "" for the block

Detection walks the candidates (spec.router_name, then "" for the block, then any preset-declared extra name), records what it found, and AutoEPMoELayer does one unconditional lookup with no discovery. A Literal["gate", "moe_block"] enum would not work -- it cannot express moe_statics.

A two-place getattr fallback (gate, then block) is a smaller change that covers 19 of 22, but leaves longcat_flash and both ernie variants unserved and keeps the discovery in the wrong layer.

Additional context

  • Detection must inspect the live module tree, not checkpoint key names -- they can disagree. MiniMax-M3 is a live example: the published checkpoint stores ...layers.N.block_sparse_moe.e_score_correction_bias, while the transformers 5.15.1 built-in builds self.mlp = MiniMaxM3VLSparseMoeBlock(...) with the buffer on its gate child, i.e. ...layers.N.mlp.gate.e_score_correction_bias. There is no _checkpoint_conversion_mapping and the Hub repo ships no modeling code, so how those reconcile is unresolved -- but only the module tree describes what getattr will actually find.
  • Related but separate: supports_expert_bias=False on the DeepSeek presets rejects a non-null load_balance_coeff, i.e. AutoEP does not update the bias during training either. This issue is only about preserving the pretrained value across replacement.
  • The survey was produced by locating every self.e_score_correction_bias = nn.{Buffer,Parameter} declaration in transformers/models/*/modeling_*.py, resolving the enclosing class, and checking whether that class owns self.experts (so it is the MoE block) or is instantiated as a child of one.

System info

  • transformers 5.15.1, torch 2.6.0+cu124, DeepSpeed 0.19.6 @ edaa72212
  • Python 3.10.12, Ubuntu 22.04.5 LTS
  • Reproduced on 8x NVIDIA H200; the meta-device check above needs no accelerator memory

pengdurice avatar Aug 29 '26 18:08 pengdurice