Skip to content

fix(qwen4_exp): free replaced params after load and look up expert mappings by expert id - #68

Draft
bobofang11235 wants to merge 2 commits into
apinge:qwen38-flash-next-amd-current-fix-installfrom
bobofang11235:coldstart/qwen4-exp-load-weights
Draft

bobofang11235 wants to merge 2 commits into
apinge:qwen38-flash-next-amd-current-fix-installfrom
bobofang11235:coldstart/qwen4-exp-load-weights

Conversation

@bobofang11235

@bobofang11235 bobofang11235 commented Oct 27, 2026 •

Copy link
Copy Markdown

Motivation

Two costs in Qwen4ExpForConditionalGeneration.load_weights, found while
reducing cold start of Qwen3.8-Flash-Next-PTPC-FP8-PLE-BF16 (TP2, MI308X):

  1. Replaced params are pinned until GC. load_qwen4_exp_ple_shard stores
    its "already warned" flag as an attribute on itself, so its closure refers to
    the function: a reference cycle. The same closure holds params_dict, so
    after load_weights returns every original parameter stays alive until the
    cyclic GC runs. When process_weights_after_loading replaces the MoE weights
    with shuffled copies, the 96 old tensors (56.36 GiB per rank) are not freed
    immediately. Depending on GC timing this shows up as a 58 GB transient at the
    end of loading, and it always leaves allocator fragmentation behind.
  2. Linear scan of the expert mapping per tensor. Each checkpoint tensor is
    tested against all num_experts * 3 entries of expert_params_mapping
    (1,536 for this model). With one tensor per expert (147,456 of 149,318), this
    costs about 8 s on the main thread.

Changes

  • fix: keep the flag in a nonlocal variable, which breaks the cycle.
  • perf: index the mapping by expert id, and for each tensor visit only the
    entries of the expert ids in its name (experts.<id>.). Those include every
    entry whose weight_name is in the name, in list order, and the loop still
    checks weight_name, so it accepts exactly the same entries as before.

Accuracy

GSM8K on MI308X TP2, base branch c996fcb1e, 3 runs each without and with this change
(5-shot in one chat message, thinking off, temperature 0, 1,314 scored questions):
python3 -m sglang.test.run_eval --eval-name gsm8k --api chat --num-examples 1319 --max-tokens 1024 --chat-template-kwargs '{"enable_thinking": false}'

run 1 run 2 run 3 mean
base 0.9673 0.9627 0.9665 0.9655
this PR 0.9673 0.9665 0.9658 0.9665

The difference (+0.0010) is within run-to-run variation: the base runs span 0.0046, and one run's
standard error is about 0.005.

Results (MI308X, TP2, safetensors loader)

before after
replaced params still alive after post-processing 96 (56.36 GiB) 0
free memory after load 77.48 GB 78.34 GB
KV cache tokens 3,253,376 3,318,272 (+2.0%)
expert mapping lookups for all 149,318 names (isolated) 10.5 s 0.25 s

Validation

  • Tracked every replaced parameter with weakrefs after process_weights_after_loading: 96 alive before, 0 after.
  • For every checkpoint name, compared the mapping entries the loop accepts against the original full scan: identical for all 149,318 names, and for nested or prefixed names such as experts.1.experts.2.gate_proj and shared_experts.0.up_proj.
  • Server starts and answers correctly; pre-commit passes.

load_qwen4_exp_ple_shard kept its "already warned" flag as an attribute
on itself, so its closure referenced the function and formed a reference
cycle. The same closure holds params_dict, so after load_weights returns
every original parameter stays alive until the cyclic GC runs. When
process_weights_after_loading then replaces the MoE weights with shuffled
copies, the old ones (96 tensors, 56 GiB per rank for
Qwen3.8-Flash-Next-PTPC-FP8-PLE-BF16 at TP2) are not freed immediately.

Use a nonlocal flag instead. On MI308X TP2, stale params after
post-processing drop from 96 to 0, allocator fragmentation after loading
drops by 0.86 GiB, and the KV cache grows from 3,253,376 to 3,318,272
tokens (+2.0%).
…ning

Every checkpoint tensor scanned all num_experts * 3 entries of
expert_params_mapping with a substring test. With one tensor per expert
(147,456 of 149,318 tensors in Qwen3.8-Flash-Next) this cost about 8 s of
load_weights on the main thread.

Index the mapping by expert id and visit only the entries of the expert
ids that occur in the tensor name. Those include every entry whose
weight_name is in the name, in list order, and the loop still checks
weight_name, so it accepts the same entries as before. Checked against all
149,318 checkpoint names: identical; 10.5 s -> 0.25 s in isolation.
@apinge
apinge self-requested a review October 29, 2026 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant