Repository navigation
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
Conversation
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
self-requested a review
October 29, 2026 12:19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Two costs in
Qwen4ExpForConditionalGeneration.load_weights, found whilereducing cold start of Qwen3.8-Flash-Next-PTPC-FP8-PLE-BF16 (TP2, MI308X):
load_qwen4_exp_ple_shardstoresits "already warned" flag as an attribute on itself, so its closure refers to
the function: a reference cycle. The same closure holds
params_dict, soafter
load_weightsreturns every original parameter stays alive until thecyclic GC runs. When
process_weights_after_loadingreplaces the MoE weightswith 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.
tested against all
num_experts * 3entries ofexpert_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 anonlocalvariable, which breaks the cycle.perf: index the mapping by expert id, and for each tensor visit only theentries of the expert ids in its name (
experts.<id>.). Those include everyentry whose
weight_nameis in the name, in list order, and the loop stillchecks
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}'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)
Validation
process_weights_after_loading: 96 alive before, 0 after.experts.1.experts.2.gate_projandshared_experts.0.up_proj.