Repository navigation
fix(awq): fold a scale group across a balanced MoE device pool (#5104) - #5177
Merged
Merged
Conversation
`VramStrategy.BALANCED` assigns the routed experts of one MoE family to the pool round-robin, keyed by expert. An AWQ scale group is anchored on the tensor that produces its input, so the gate/up projections of every expert form one norm-anchored group that spans every device in the pool. AWQ assumed such a group lived on one device: * `_accumulate_awq_weight_mean` allocated `w_sum` on `layers[0]`'s device and added every other member's channel sum into it, raising `RuntimeError: Expected all tensors to be on the same device` in layer 0 after calibration capture for every MoE model; * `_compute_best_scale` applied the shared channel scale with `torch.mul(..., out=fc.weight)` and `fc.weight.div_(scales_view)` on each member, with the scale left on `layers[0]`; * `apply_scale` moved the whole group onto `layers[0]`'s device before folding, which copies an expert family and defeats the memory the pool exists to save. Gather each member's reduction onto the accumulator, apply the candidate scale on each member's own device, and keep a norm-anchored group in place while `scale_ln_fcs` divides the norm and multiplies each consumer locally. Linear and activation anchored groups still co-locate, because those folds reshape the source weight. Balanced and exclusive quantization of the same tiny 16-expert Qwen3-MoE now produce bit-identical packed tensors. Refs #5104
Qubitium
marked this pull request as ready for review
November 18, 2026 08:19
ZX-ModelCloud
force-pushed
the
fix/5104-awq-balanced-moe
branch
from
November 18, 2026 08:19
05b8e6c to
0e257cb
Compare
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.
Summary
AWQConfig(moe_vram_strategy=VramStrategy.BALANCED)— the multi-GPU MoE poolthe multi-GPU guide recommends — crashed in
layer 0 on every MoE model, after calibration capture:
build_subset_plan(gptqmodel/looper/stage_subset.py) assigns the routedexperts of one MoE family round-robin, keyed by expert. An AWQ scale group is
anchored on the producer of its input — for a MoE block the shared
post_attention_layernorm— so one group holds the gate/up projections ofevery expert and therefore spans every device in the pool (128 linears for
OLMoE's 64 experts). AWQ assumed such a group lived on one device.
Net diff: +45 / −9 in the two implementation files, plus tests and docs. No API
change; the fix is a gather plus per-member scale application.
Fixes #5104
What Changed
_accumulate_awq_weight_mean(gptqmodel/looper/awq_processor.py): gathereach member's channel sum with
.to(w_sum.device)before adding it. Stillallocation-free on a single-device group.
_compute_best_scale: apply the candidate channel scale on each member's owndevice (
torch.mul(...)anddiv_), instead of assuminglayers[0]'s device.apply_scale/scale_ln_fcs/scale_fc_fc/scale_fc_fcs(
gptqmodel/quantization/awq/quantize/scale.py): a norm-anchored group is nolonger co-located. The norm is divided on its own device and each consumer
multiplies on its own device. Linear- and activation-anchored groups still
co-locate, because those folds reshape the source weight itself. This is the
memory the balanced pool exists to save: a co-locating fold would copy a
whole expert family onto one device.
AWQ scale groups on a balanced MoE pool
with the before/after table and diagram; linked from
README.md.Out of scope: no change to
build_subset_plan's placement policy. A group whoseprovider is a linear still has to be co-located; that is a pre-existing
constraint of the fold, unchanged here.
Logic Flow Documentation
Diagram and before/after table are in the PR summary below and repeated in
docs/multi_gpu_quantization.md#awq-scale-groups-on-a-balanced-moe-pool.flowchart TD A["AWQ group for one prev_op"] --> B{"_is_norm_source prev_op?"} B -- "yes" --> C["fold norm on its device"] C --> D["scale_ln_fcs: each fc applies s<br/>on fc weight device"] B -- "no" --> E["co-locate consumers on layers[0]"] E --> F["scale_fc_fcs / scale_gelu_fc"] D --> G["restore norm placement"] F --> G_accumulate_awq_weight_mean(gptqmodel/looper/awq_processor.py)w_sum += <member sum>; added a peer device's tensor intolayers[0]'s accumulator ->RuntimeError.to(w_sum.device)_compute_best_scaletorch.mul(master, scales_view, out=fc.weight)/fc.weight.div_(scales_view)with the scale onlayers[0]apply_scale(gptqmodel/quantization/awq/quantize/scale.py)layers[0]'s device before the foldTests
New fast unit coverage runs on two real devices and asserts the trigger
occurred, not just that quantization returns:
tests/test_awq_balanced_moe_group_5104.py::test_weight_mean_accepts_members_on_two_devices— accumulator gathers a peer-device reduction; compared against an
independent host normalised-magnitude mean.
...::test_scale_ln_fcs_applies_the_scale_on_each_consumer_device— bothconsumers receive the same channel scale, each on its own device, checked
against
original * s....::test_apply_scale_does_not_co_locate_a_norm_anchored_group— recordsmove_tocalls and asserts no expert is relocated; folded weights equaloriginal * s....::test_balanced_moe_scale_search_scores_a_group_split_over_devices— the20-candidate grid over a group on two devices, with an identity quantizer,
asserts a finite loss, selected scales, and exact weight restoration on both
devices.
tests/test_awq_balanced_moe_5104.py::test_balanced_moe_pool_quantizes_and_keeps_the_expert_group_intact— tiny native 2-layer, 16-expert Qwen3-MoE (16 experts is the
_moe_subset_threshold). Quantizes once withVramStrategy.BALANCEDand onceexclusive, asserts a scale group really spanned two devices in the balanced
run, every expert projection is packed, and the packed tensors are
bit-identical between the two runs.
Red evidence: the same nodes run against
origin/mainin a separate worktreefail at the intended assertion (
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1!for theend-to-end node; the accumulator/scale/fold nodes fail on the same
cross-device op). 4/4 unit nodes red, 1/2 end-to-end red (the exclusive control
passes).
Family suite, CPU (
bash tests/run_awq_cpu.sh unit):1206 passed, 6 skipped, 2 failed. Both failures(
test_awq_hybrid_recurrent_mask_4671.py::test_awq_batch_size_one_quantizes_a_mamba_hybrid,test_definition_subset_ordering.py::test_issue_4372_tiny_model_data_flow[nemotron_h])also fail on an unmodified
origin/mainworktree with the identical command.tests/test_awq_weight_mean.py: 88 passed.tests/test_docs_contract_refs.py:33 passed.
ruff check gptqmodel tests docs/eora setup.py: clean.Skips: the two new files require two devices and skip otherwise. ROCm: the
unit file passes on two AMD devices; the end-to-end file also passes there
(2 passed).
Model Support / Module Plan
Not applicable: no new model, alias or definition change.
AWQ onboarding safety
This is an AWQ folding/search change, so the source/consumer relationships are
re-established rather than assumed:
post_attention_layernorm; its consumers arethe gate/up projections of every routed expert (one merged
:in=xgroup) plusthe non-quantized router, which remains compensated through the existing
consumer_mapinapply_scale. No group membership changed.module2inspect= the MoE block); only thedevice each member's reduction/scale lives on changed. Folding still uses the
original-coordinate input and the single shared channel scale; only the
location of the multiply moved.
_AWQScaleGroupError-> configured fallback;fallback=Nonepropagates. The partial-replay (#4675) and bypass-replay(
#4223) contracts are untouched.identical packed tensors, and the fold is neutral (
original * s) for anon-uniform scale on every consumer.
Kernel Accuracy Requirements
Not applicable: no kernel, packer, format or dispatch change. Packed payloads are
compared bit-for-bit between the balanced and exclusive runs.
Notes
devices; a test pins that no
move_totouches an expert.per member (a 1-D tensor the size of
hidden_size). That is negligible nextto the expert family a co-locating fold would have copied.