Skip to content

fix(looper): give every data-parallel replica its own subset early-stop state (#4904) - #4982

Merged
Qubitium merged 1 commit into
mainfrom
root/dp-subset-state-4904
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
root/dp-subset-state-4904

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4904. With the default auto_forward_data_parallel=True on two or more GPUs, MoE quantization silently dropped 3-6% of the routed calibration tokens from the expert Hessians, nondeterministically. ForwardExecutor.run_parallel forwards one replica of a layer per device on a device-dedicated worker thread, but every replica shared the subset early-stop state added by #4703/#4752.

That state was one pending_members set per subset pass plus one forward_hook_last flag written to the subset's original modules (the closure's module, not the _module the hook was called with). A deep copy keeps the closures, so while the cuda:0 replica forwarded batch 0, the cuda:1 replica could refill or drain the shared set and set the stop flag on an original expert. HookedLinear then raised StopForward mid-forward and every routed expert after that member never saw the batch. Dense subsets were mostly spared by their fixed member order; MoE subsets were not, because the completing member depends on routing and on the other thread's timing.

What Changed

Tests

New tests/test_dp_subset_early_stop_4904.py:

  • test_dp_replicas_keep_every_routed_token (2 GPUs, default DP): runs the tests/test_checkpoint_quantization.py::build_native_model("qwen3_moe", ...) fixture (2 layers, 4 experts, MoEConfig(routing=ExpertsRoutingOverride()), 4 calibration rows x 32 tokens = 128 tokens per projection) and asserts every expert's gate_proj/up_proj/down_proj saw all 128 tokens, then repeats the 2-GPU run and requires identical nsamples for every module (the issue reports the amount varies run to run).
  • test_single_gpu_and_dp_off_controls_keep_every_routed_token: the single-GPU and auto_forward_data_parallel=False controls.
  • Three fast CPU unit tests (6.8s total): the pending set is per forward thread, the completing member flags the module that ran, and replica member resolution follows the layer-relative name.

nsamples per expert projection, 2 GPUs, default auto_forward_data_parallel=True (expected 128 everywhere):

module before (origin/main) after
layers.0.experts.0 gate/up/down 128 / 128 / 128 128 / 128 / 128
layers.0.experts.1 gate/up/down 128 / 128 / 128 128 / 128 / 128
layers.0.experts.2 gate/up/down 128 / 128 / 128 128 / 128 / 128
layers.0.experts.3 gate/up/down 128 / 128 / 128 128 / 128 / 128
layers.1.experts.0 gate/up/down 128 / 128 / 128 128 / 128 / 128
layers.1.experts.1 gate/up/down 64 / 64 / 128 128 / 128 / 128
layers.1.experts.2 gate/up/down 64 / 64 / 128 128 / 128 / 128
layers.1.experts.3 gate/up/down 64 / 64 / 96 128 / 128 / 128

The loss moves between runs, which is the non-determinism the test also pins down. Another origin/main run of the same fixture (before the fix) lost layers.0.experts.3.{gate,up}_proj (96 each), layers.0.experts.{1,2,3}.down_proj (64 each), and layers.1.experts.3.{gate,up}_proj (96 each) instead; the issue's own repro loses layer-0 experts. Single-GPU and auto_forward_data_parallel=False runs were 128 for every projection in every run, before and after.

# red: origin/main worktree, new test copied in
python /tmp/root-rt.py tests/test_dp_subset_early_stop_4904.py -q -o addopts= -o log_cli=false -p no:cacheprovider
# 1 failed, 1 passed  (FAILED test_dp_replicas_keep_every_routed_token:
#   routed experts lost calibration tokens: layers.0.experts.1.down_proj=64, ...)

# green: fix
python /tmp/root-rt.py tests/test_dp_subset_early_stop_4904.py -q -o addopts= -o log_cli=false -p no:cacheprovider
# 2 passed
python /tmp/root-rt.py tests/test_dp_subset_early_stop_4904.py -q -k "early_stop_state or completing_member or replica_member"
# 3 passed

Regression suites, run identically on origin/main and on this branch (2 GPUs, same env):

python /tmp/root-rt.py tests/test_checkpoint_quantization.py \
  tests/module_tree/test_forward_executor_parallel.py \
  tests/module_tree/test_forward_executor_shared_kv.py \
  tests/test_subset_plan.py tests/test_subset_declared_order_4679.py \
  tests/test_subset_cancel_hook_cleanup_4470.py \
  tests/test_moe_subset_early_stop_batches_4738.py \
  tests/module_tree/test_subset.py tests/test_stage_modules.py
# origin/main: 29 failed, 107 passed
# this branch: 29 failed, 107 passed  (identical failure sets)
# All 29 are pre-existing on this host: the subprocess driver in
# tests/test_checkpoint_quantization.py imports `tests/kernels`, which shadows
# the installed `kernels` package for transformers, plus
# test_mlp_capture_flag_propagates_to_layer_modules, which passes when
# tests/module_tree/test_subset.py runs alone (14 passed).

PYTHONSAFEPATH=1 python /tmp/root-rt.py tests/test_checkpoint_devices.py -k qwen3_moe
# the qwen3_moe multi-GPU partial-Hessian recovery matrix the issue lists
# origin/main: 6 failed, 1 passed
#   (AssertionError: model.layers.0.mlp.experts.N.*.qweight differ between the
#    baseline and resumed runs; the baseline is not reproducible)
# this branch: 7 passed
  • Unit tests have been executed and passed, or the reason they are not applicable is documented.
  • I added a new simple/fast unit test for this change, or documented why that is not applicable.

Model Support / Module Plan

Not applicable: no model definition, alias, or module-plan change.

AWQ onboarding safety

Not applicable: no model onboarding and no AWQ grouping/search/replay/fold change. The shared subset early-stop path is covered by the existing AWQ tests in the regression command above.

@Qubitium
Qubitium force-pushed the root/dp-subset-state-4904 branch from b67b6e5 to 177c1cb Compare November 17, 2026 20:45
…op state

`ForwardExecutor.run_parallel` forwards one replica of a layer per device on
a device-dedicated worker thread. The replicas are deep copies, but a deep
copy keeps the subset stage's closures and the modules they capture, so every
replica shared one `pending_members` set and the stop flag was written to the
subset's *original* modules. While the cuda:0 replica forwarded batch 0, the
cuda:1 replica could refill or drain the shared set and set
`forward_hook_last` on an original expert; `HookedLinear` then raised
`StopForward` mid-forward and every routed expert after that member silently
lost the batch from its Hessian. Dense subsets were mostly spared by their
fixed member order; MoE subsets were not, because the completing member
depends on routing and on the other thread's timing.

Keep the pending set in thread-local storage and reset it from the layer
pre-hook on the replica that is about to forward. Flag the module that
actually ran (`_module`) instead of the closure's original module, and clear
the flag on the executing replica's member. The serial path keeps the #4752
behaviour: the state is rebuilt for every calibration batch.

Regression: `tests/test_dp_subset_early_stop_4904.py` runs the tiny two-layer
Qwen3-MoE fixture with `ExpertsRoutingOverride` on two GPUs and asserts every
expert projection sees all 4 x 32 calibration tokens, that two back-to-back
runs agree, and that the single-GPU and `auto_forward_data_parallel=False`
controls still see every token.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant