Skip to content

fix(awq): fold a scale group across a balanced MoE device pool (#5104) - #5177

Merged
Qubitium merged 1 commit into
mainfrom
fix/5104-awq-balanced-moe
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5104-awq-balanced-moe

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

Summary

AWQConfig(moe_vram_strategy=VramStrategy.BALANCED) — the multi-GPU MoE pool
the multi-GPU guide recommends — crashed in
layer 0 on every MoE model, after calibration capture:

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1!
  awq_processor.py _search_best_scale -> w_mean = _compute_awq_weight_mean(layers, group_sizes)
  awq_processor.py _accumulate_awq_weight_mean -> w_sum += weight_abs.sum(dim=0, dtype=torch.float32)

build_subset_plan (gptqmodel/looper/stage_subset.py) assigns the routed
experts 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 of
every 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): gather
    each member's channel sum with .to(w_sum.device) before adding it. Still
    allocation-free on a single-device group.
  • _compute_best_scale: apply the candidate channel scale on each member's own
    device (torch.mul(...) and div_), instead of assuming layers[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 no
    longer 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.
  • Docs: new section
    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 whose
provider 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
Loading
Mechanism Before After
_accumulate_awq_weight_mean (gptqmodel/looper/awq_processor.py) w_sum += <member sum>; added a peer device's tensor into layers[0]'s accumulator -> RuntimeError Reductions gathered with .to(w_sum.device)
_compute_best_scale torch.mul(master, scales_view, out=fc.weight) / fc.weight.div_(scales_view) with the scale on layers[0] Scale moved to each member's device
apply_scale (gptqmodel/quantization/awq/quantize/scale.py) every member moved onto layers[0]'s device before the fold norm-anchored groups fold in place; only linear/activation groups co-locate

Tests

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 — both
    consumers 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 — records
    move_to calls and asserts no expert is relocated; folded weights equal
    original * s.
  • ...::test_balanced_moe_scale_search_scores_a_group_split_over_devices — the
    20-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 with VramStrategy.BALANCED and once
    exclusive, 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.
# green (CUDA, 2 visible devices)
cd tests && python -m pytest test_awq_balanced_moe_group_5104.py -q   # 4 passed
cd tests && python -m pytest test_awq_balanced_moe_5104.py -q          # 2 passed
PYTHON_GIL=0 python -m pytest test_awq_balanced_moe_5104.py -q         # 2 passed (free-threaded)

Red evidence: the same nodes run against origin/main in a separate worktree
fail 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 the
end-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/main worktree 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:

  • The group's source is the shared post_attention_layernorm; its consumers are
    the gate/up projections of every routed expert (one merged :in=x group) plus
    the non-quantized router, which remains compensated through the existing
    consumer_map in apply_scale. No group membership changed.
  • The scoring boundary is unchanged (module2inspect = the MoE block); only the
    device 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.
  • Failure contract unchanged: _AWQScaleGroupError -> configured fallback;
    fallback=None propagates. The partial-replay (#4675) and bypass-replay
    (#4223) contracts are untouched.
  • Qualified: balanced and exclusive AWQ of the tiny 16-expert Qwen3-MoE produce
    identical packed tensors, and the fold is neutral (original * s) for a
    non-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

  • A norm-anchored group's consumers are now expected to stay on their planned
    devices; a test pins that no move_to touches an expert.
  • Trade-off: the fold keeps one extra device-resident copy of the channel scale
    per member (a 1-D tensor the size of hidden_size). That is negligible next
    to the expert family a co-locating fold would have copied.

`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
Qubitium marked this pull request as ready for review November 18, 2026 08:19
@ZX-ModelCloud
ZX-ModelCloud force-pushed the fix/5104-awq-balanced-moe branch from 05b8e6c to 0e257cb Compare November 18, 2026 08:19
@Qubitium
Qubitium merged commit fba0d0e into main Nov 18, 2026
5 checks passed
@Qubitium
Qubitium deleted the fix/5104-awq-balanced-moe branch November 18, 2026 08: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

2 participants