Skip to content

[OMNIML-6060] Load an expert-parallel plan through load_pretrained, and apply the model spec when deciding and loading it - #2780

Merged
shengliangxu merged 70 commits into
mainfrom
shengliangx/hfep-9b-ep-load-routing
Nov 3, 2026
Merged

shengliangxu merged 70 commits into
mainfrom
shengliangx/hfep-9b-ep-load-routing

Conversation

@shengliangxu

@shengliangxu shengliangxu commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Note

Folded in #2746 [3/3] on 2026-11-03, at the user's request: this PR now also applies the model spec when deciding and loading expert-parallel. #2746's commits are here unchanged (this PR's head is #2746's last head, 22b6d23); #2746 keeps their review history, and this PR covers its Jira ticket, OMNIML-6076, too.

Re-parented on 2026-10-30 onto #2741 [2/3] (#3136), the expert-parallel loader's core, when #2741 was split. This PR also adopts #2779's newer collective load checks; see the end of Testing.

Base: main, since #2741 [2/3] (#3136) merged on 2026-11-03. The diff below is this PR's alone. It merges after #2741 [3/3] (#2741), whose refusals keep load_pretrained's new expert-parallel route from quantizing unsupported experts wrong.

Series layout

What does this PR do?

Type of change: New feature

#2779 routes load_pretrained by a DistributionPlan, but refuses a plan that shards the routed experts: the expert-parallel loader (#2741) comes later in the series. With it in, this PR adds that route, and applies the model's spec (#3134), through the applier (#3135), when deciding and loading it:

  • An expert-parallel plan loads through load_and_prepare_ep, handed the plan itself. Its arguments are checked with the FSDP2 load's, in _sharded_load_kwargs inside [OMNIML-6054] [2/2] Route load_pretrained by the distribution plan #2779's collective "load checks" block; _load_sharded then runs the load. That load replicates the dense part; FSDP2-sharding it comes with [OMNIML-6061] Load the dense part sharded, and size the load warmup to each rank #2742, a sibling of this PR. Its freeze and require_lm_head options pass through, and so do the Hub options (token, cache_dir, local_files_only), which its config read and from_pretrained use. The sharded loads' refusals from [OMNIML-6054] [2/2] Route load_pretrained by the distribution plan #2779 cover it too: a Nemotron-H checkpoint's MTP head, which load_and_prepare_ep does not build, is refused rather than exported unquantized. Until [OMNIML-6061] Load the dense part sharded, and size the load warmup to each rank #2742 is in, an expert-parallel plan that also shards the dense part is refused before anything loads, naming the ways out: replicate it anyway, or FSDP2-shard the whole model. A plan left to decide shards the dense part when a whole copy of it and a rank's share of the experts would exceed 60% of a GPU.
  • The decision refuses to leave an expert-parallel MoE whole on every rank unasked, naming the expert count the EP degree must divide and every way out: shard its experts (an EP degree is offered only when one divides both the experts and the world), shard the whole model with FSDP2 (an unquantized checkpoint), load it in a single process, or ask for the replication: MODELOPT_EP_DEGREE=1 with MODELOPT_FSDP_SIZE=1, or a replicated plan handed to load_pretrained, which skips the decision. A replication both knobs ask for loads, with a note that every rank holds the whole MoE. So [OMNIML-6054] Decide how a model is distributed, from the model built on the meta device #2737's refusals of routed experts the decision can neither count nor shard by now advise MODELOPT_EP_DEGREE=1 with MODELOPT_FSDP_SIZE=1 (ep_size=1 with dense='replicate' for a caller passing the arguments): an EP degree of 1 alone now meets the refusal above.
  • An MoE the expert-parallel load cannot take loads as a dense model does, whole on every rank or FSDP2-sharded, when the EP degree is left to the decision or set to 1. The load may refuse it for the model's spec, the transformers release it needs, its checkpoint format or its plan. A rank-0 warning names the reason, and the fix where there is one (an unquantized checkpoint, a later transformers); a replication both knobs ask for gets only the "as asked" note. An EP degree above 1 asked for such a model is still refused by the load.

The model spec in the decision and the load (folded in from #2746). Without it, an MoE that only its spec makes expert-parallel, such as DeepSeek-V3 before transformers 5.13, is decided as dense.

  • Deciding. Unless the run rules expert parallelism out (a single process, or an EP degree of 1), resolve_distribution_plan applies the model's spec to a copy of the config before rank 0 builds the model on meta.
    • Every rank applies it inside a collective, so a rank that cannot fails every rank.
    • An expert-parallel plan returns the prepared copy. Any other plan returns the config as read, which the export would otherwise write the spec's plan into.
    • Rank 0's knobs decide whether the spec applies, and rank 0 sends that decision as it does the plan: a rank whose own environment skipped the spec would load without the expert-parallel plan the others shard by, and fail once the mesh is up.
  • Auto and the floors. An EP degree left to auto resolves to 1 for a model whose spec refuses expert parallelism, or that needs a later transformers than the one installed: its spec's release or the expert-parallel load's 5.7 floor, whichever is later.
    • The load's floor holds for a model with no spec too (_spec_refuses_ep).
    • Only an explicit EP degree above 1, or a handed expert-parallel plan, meets the refusal: MODELOPT_FSDP_SIZE=world alone plans FSDP2 for GPT-OSS, and for Qwen3-MoE on transformers 5.5.
  • The left-whole refusal above sees spec plans. At an EP degree of 1, which skips the spec, it still finds a spec-supplied plan's experts, through the plan the spec would supply (or the one it states prepare attaches), without applying anything.
  • An MoE whose spec rules expert parallelism out (GPT-OSS, Llama-4), or whose transformers is below the release its expert parallelism needs, loads as a dense model does when the EP degree is left to auto or set to 1. It loads whole on every rank, or FSDP2-sharded if it doesn't fit one GPU, with the rank-0 warning above carrying the spec's reason or the upgrade (moe._spec_refusal). An EP degree above 1 asked for such a model is still refused, with the way around expert parallelism added (check_distributed_support).
  • Loading. The expert-parallel route applies the spec as it checks the load's arguments, inside [OMNIML-6054] [2/2] Route load_pretrained by the distribution plan #2779's collective "load checks" block, so a rank that cannot apply it fails every rank instead of leaving the others in the load.
    • A load handed the config that deciding prepared does not prepare it again, since a model's preparation can patch its modeling code. It still meets its spec's refusal and transformers floor (check_distributed_support).
    • A config= passed for the load with a plan handed in is the caller's own, so an expert-parallel load prepares a copy of it, once.
  • Why the spec's prepare runs before deciding here, and where it goes in [OMNIML-6078] [2/2] Add the Kimi-K2.5 expert-parallel adapter #2748. The decision builds the model on the meta device and finds and counts its routed experts through the plan. For a model whose plan only its spec supplies, the spec must therefore be applied first. This PR applies the whole spec there, prepare included, and documents the interim contract on EPSpec.prepare: it must leave a whole-model or FSDP2 load as it would be without it, and may run more than once. No spec in this PR sets prepare.
    • [OMNIML-6078] [2/2] Add the Kimi-K2.5 expert-parallel adapter #2748 adds the first specs that set it (Kimi-VL, Kimi-K2.5) and replaces the contract. The decision runs only a pure-import step (EPSpec.make_importable) and attaches the spec's stated plan (prepared_ep_plan) with no prepare run. prepare then runs only once the plan is decided expert-parallel: in a collective step after the decision, or in the load after its own checks for a plan handed in.
    • That puts prepare where models/hf.py's prepare_for_loading hooks already run: on the route that loads the model it prepares, never on one that does not. The hooks are scoped to the load as a context; prepare only runs on that route.

This closes the series' second phase: an MoE whose expert-parallel plan ships with transformers, such as Qwen3-MoE, or that only its spec makes expert-parallel, such as DeepSeek-V3, now loads expert-parallel through load_pretrained and calibrates.

Usage

# torchrun --nproc_per_node 8 quantize.py
from modelopt.torch.utils.distributed import distributed_run
from modelopt.torch.utils.plugins.model_load_utils import load_pretrained

with distributed_run():
    model = load_pretrained("Qwen/Qwen3-30B-A3B")  # its plan ships with transformers
    # A model whose plan only its spec supplies loads the same way:
    # model = load_pretrained("/path/to/DeepSeek-V3-BF16")
    # ... mtq.quantize(model, ...), export_hf_checkpoint(model, ...)

Either model's routed experts are sharded over the 8 ranks. The DeepSeek-V3 path is an unquantized (BF16) copy. The published deepseek-ai/DeepSeek-V3 checkpoint is FP8, which this PR refuses before anything loads; the expert-parallel load dequantizes FP8 checkpoints from #2743 on.

Testing

transformers Passed Skipped Failed
5.18 210 0 0
5.5.4 182 28 0
GPUs Passed Failed
2 4 0
4 4 0
  • Rebased on 2026-10-30 (d78aaf8) onto [OMNIML-6060] [2/3] Load an MoE expert-parallel: shard the experts at load time #3136's restack (b726fb3) after [OMNIML-6060] [1/3] Add require_transformers, and build a VLM without its LM head #3131 merged to main (62a5c48). The base under [OMNIML-6060] [2/3] Load an MoE expert-parallel: shard the experts at load time #3136 now carries [OMNIML-6054] [2/2] Route load_pretrained by the distribution plan #2779's newer round, so 7 hunks in 3 files conflicted. All are resolved by applying this PR's change to the new text:
  • Review round, 2026-10-30 (50e64f0):
    • The dtype refusal names FSDP2's mp_policy for the FSDP2 load only (90c914a).
    • A rank without a GPU of its own is refused before the expert-parallel load too, as before the FSDP2 load. The remedy names the way to a whole model for the route, from one helper shared with the placement refusal (50e64f0).
    • test_a_sharded_load_refuses_what_it_cannot_honor gains a no_gpu row and an mp_policy assertion. Each fails without its change.
    • Pre-commit passes against main. test_model_load_utils.py, test_ep_loader.py, test_moe_a2a.py and tests/unit/torch/models pass on transformers 5.18 (469) and 5.5.4 (427, 31 skipped).
  • Review round, 2026-10-30 (3c353af):
    • A whole-model load refuses the options only the expert-parallel load takes (freeze, require_lm_head) before anything loads, naming that load. Before, they reached from_pretrained and failed inside transformers.
    • test_a_load_refuses_what_it_cannot_load gains ep-option-on-a-whole-load, which fails without the change.
    • Pre-commit passes against main. test_model_load_utils.py, test_ep_loader.py, test_moe_a2a.py and tests/unit/torch/models pass on transformers 5.18 (470) and 5.5.4 (428, 31 skipped).
  • Review round, 2026-10-30 (b1f5d8c):
    • The left-whole refusal names the knobs as the caller set them: ep_size/dense arguments, which override the environment knobs, or the knobs (b1f5d8c). A new explicit_degree_one_as_an_argument row fails without it.
    • The docstring says the expert-parallel load freezes the model unless freeze=False, unlike the other loads (1072e8f).
    • Pre-commit passes against main. test_model_load_utils.py, test_ep_loader.py, test_moe_a2a.py and tests/unit/torch/models pass on transformers 5.18 (471) and 5.5.4 (428, 32 skipped).
  • Review round, 2026-10-30 (4a1e020):
    • The refusal of an MoE left whole on every rank is a ValueError, like the decision's other refusals of what its inputs resolve to (d0a63ff).
    • A load handed an expert-parallel plan and no config reads the checkpoint's config once, for its model type and for the load, as the FSDP2 load already did (4a1e020). The EP-loader test asserts the load gets that read.
    • Pre-commit passes against main. test_model_load_utils.py, test_ep_loader.py, test_moe_a2a.py and tests/unit/torch/models pass on transformers 5.18 (471) and 5.5.4 (428, 32 skipped).
  • Review round, 2026-10-30 (4040ea3):
    • freeze or require_lm_head passed as None asks nothing, as other options passed as None do, rather than reading as False and dropping the LM head (83d9ce0).
    • The expert-parallel route returns the model unfrozen unless freeze=True, as the other routes do (69e56a9).
    • A decided expert-parallel layout says it was decided when it refuses a whole-model placement, as the FSDP2 route already did (4040ea3).
    • Each change fails a test when reverted. Pre-commit passes against main. test_model_load_utils.py, test_ep_loader.py, test_moe_a2a.py and tests/unit/torch/models pass on transformers 5.18 (472) and 5.5.4 (428, 33 skipped).
  • Restack and review round, 2026-10-30 (8f9c8d9), onto [OMNIML-6060] [2/3] Load an MoE expert-parallel: shard the experts at load time #3136 1491b66. The rebase had no conflicts, and this PR's own commits are unchanged; only the first commit's context moved.
    • A decided expert-parallel layout now takes the load's own freeze and require_lm_head, rather than refusing them as a whole-model placement (1db4cc3). A new its-own-options row fails without the change.
    • The docstring of _refuse_a_rank_without_its_gpu says what the check adds beside [OMNIML-6060] [2/3] Load an MoE expert-parallel: shard the experts at load time #3136's GPU check in the expert-parallel load, which now refuses a run where only some ranks have a GPU (8f9c8d9).
    • Pre-commit passes against main. test_model_load_utils.py, test_ep_loader.py, test_moe_a2a.py and tests/unit/torch/models pass on transformers 5.18 (476) and 5.5.4 (430, 35 skipped).
  • Review round, 2026-10-30 (d466147):
    • A sharded load now refuses an option that only the other sharded load takes by naming that load, rather than with the whole-model way out, which would refuse the option again. broadcast_chunk_size=None asks the FSDP2 load to broadcast every layer at once. The expert-parallel load used to drop it silently and now refuses it as the FSDP2 load's option. The two new rows of test_a_load_refuses_what_it_cannot_load each fail without the change.
    • Pre-commit passes against main. test_model_load_utils.py, test_ep_loader.py, test_moe_a2a.py and tests/unit/torch/models pass on transformers 5.18 (478) and 5.5.4 (432, 35 skipped).
  • Restack and review round, 2026-10-30 (b692242), onto [OMNIML-6060] [2/3] Load an MoE expert-parallel: shard the experts at load time #3136 56353ef on main 059588b, after [OMNIML-6054] [2/2] Route load_pretrained by the distribution plan #2779 reworked the load checks:
  • Review round, 2026-10-30 (1692141), onto [OMNIML-6060] [2/3] Load an MoE expert-parallel: shard the experts at load time #3136 75bc2d6:
    • An EP degree left to the plan decision gives way to FSDP2 for the whole model when the dense part, replicated beside a rank's share of the experts, does not fit one GPU, instead of planning a layout the expert-parallel load refuses (1692141). An EP degree the caller asks for still stands and is refused by the load, naming the ways out. test_what_a_rank_holds_with_the_dense_part_replicated_must_fit asserts both outcomes, and its decided case fails without the change.
    • The restack kept [OMNIML-6060] [2/3] Load an MoE expert-parallel: shard the experts at load time #3136's trimmed load_and_prepare_ep docstring and comments, plus this PR's sentence on hub_options.
    • Pre-commit passes against main. test_model_load_utils.py, test_ep_loader.py, test_moe_a2a.py and tests/unit/torch/models pass on transformers 5.18 (515) and 5.5.4 (463, 41 skipped).
  • Restack and review round, 2026-10-30 (093c652), onto [OMNIML-6060] [2/3] Load an MoE expert-parallel: shard the experts at load time #3136 ab06860 on main d464d30:
    • An EP degree the decision chose now also gives way to none when the expert-parallel load would refuse the model, by its plan (the load's own style checks, now one function, _ep_plan_refusal) or by its checkpoint format. Such an MoE is not refused for being left whole either (9cc4589). test_an_moe_the_expert_parallel_load_refuses_is_decided_without_it fails without the change in both of its cases.
    • The refusal of an expert-parallel plan that FSDP2-shards the dense part says that an EP degree left to the decision gives way (381219c).
    • [OMNIML-6054] [2/2] Route load_pretrained by the distribution plan #2779's texts written before this load now name it: the auto_class refusal, the no-group refusal and the docstring (093c652).
    • Pre-commit passes against main. test_model_load_utils.py, test_ep_loader.py, test_moe_a2a.py and tests/unit/torch/models pass on transformers 5.18 (519) and 5.5.4 (464, 44 skipped).
  • Review round, 2026-10-31 (1960174):
    • An EP degree the decision chose now gives way for every reason the config tells why the expert-parallel load would refuse the model: a model spec ruling it out (Llama-4's, GPT-OSS's), a transformers below the load's floor or the spec's, the checkpoint format, or the plan's styles. The its-spec and its-transformers cases fail without the change.
    • Each give-way now says why on rank 0. The CHANGELOG names only the MoEs the expert-parallel load takes as refused when left whole.
    • Pre-commit passes against main. test_model_load_utils.py, test_ep_loader.py, test_moe_a2a.py and tests/unit/torch/models pass on transformers 5.18 (521) and 5.5.4 (464, 46 skipped).
  • Restacked on 2026-10-31 (fac2c56) onto [OMNIML-6060] [2/3] Load an MoE expert-parallel: shard the experts at load time #3136 7747c5d, which moved test_ep_loader.py into test_model_load_utils.py. This PR's one change to that file, the Hub-options checks in test_the_weights_load_in_the_checkpoints_dtype_through_its_class, now sits in the moved section. Its own diff is otherwise unchanged. Pre-commit passes against main. test_model_load_utils.py, test_moe_a2a.py and tests/unit/torch/models pass on transformers 5.18 (521) and 5.5.4 (464, 46 skipped).
  • Review round, 2026-10-31 (3fc02b8):
    • The expert-parallel load's transformers floor is one constant, _EP_MIN_TRANSFORMERS, that the decision and the load both read (6210606).
    • The decision now resolves whether the expert-parallel load refuses an MoE whatever its EP degree, so an EP degree of 1 the caller set no longer meets the left-whole refusal for a model that load refuses (3fc02b8). The give-way test runs each refusal with the degree on auto and with MODELOPT_EP_DEGREE=1, and the set-to-1 cases fail without the change.
    • Pre-commit passes against main. test_model_load_utils.py, test_moe_a2a.py and tests/unit/torch/models pass on transformers 5.18 (525) and 5.5.4 (464, 50 skipped).
  • Review round, 2026-10-31 (3c40b57):
    • load_and_prepare_ep refuses what the plan decision gives way for (_ep_load_refusal), after its own floor and format checks. An EP degree asked for, or a plan handed in, no longer loads a model whose spec rules expert parallelism out. The load test's new GPT-OSS case fails without the change.
    • Pre-commit passes against main. test_model_load_utils.py, test_moe_a2a.py and tests/unit/torch/models pass on transformers 5.18 (526) and 5.5.4 (465, 50 skipped).
  • Restacked on 2026-10-31 (c2d18e6) onto [OMNIML-6060] [2/3] Load an MoE expert-parallel: shard the experts at load time #3136 930bb0f, on the new main e352cac and [OMNIML-6054] [2/2] Route load_pretrained by the distribution plan #2779 c78d2e5, with Claude's 04:08Z review round folded in:
    • [OMNIML-6054] [2/2] Route load_pretrained by the distribution plan #2779's texts meet the expert-parallel load. Its new init_quantized_weights refusal names the expert-parallel route's own way to a whole model, and a new test covers both a knob and a handed plan; the plain FSDP2 wording fails it. The loading guide [OMNIML-6054] [2/2] Route load_pretrained by the distribution plan #2779 added now describes the expert-parallel route, which this PR's docstring paragraphs moved into (5455a0c). The decided EP degree is no longer forced to 1 in load_pretrained.
    • _ep_load_refusal returns the error itself, so a floor refusal is a RuntimeError whichever floor it trips (eda4c88). A spec's floor above the load's own is tested (608b0b8). Both cases fail without their change.
    • Pre-commit passes against main. test_model_load_utils.py, test_moe_a2a.py and tests/unit/torch/models pass on transformers 5.18 (536) and 5.5.4 (471, 54 skipped).
  • Review round, 2026-10-31 (f7b11fe): the dense-sharding give-way test also leaves the EP degree to the decision, where a caller asking for FSDP2 makes the decided degree give way; it fails if only rank 0's GPU memory may. Pre-commit passes against main. The same CPU tests pass on transformers 5.18 (537) and 5.5.4 (471, 55 skipped).
  • Restacked on 2026-11-01 (36863bc) onto [OMNIML-6060] [2/3] Load an MoE expert-parallel: shard the experts at load time #3136 4dd38a3, on main cf38ec3 and [OMNIML-6054] [2/2] Route load_pretrained by the distribution plan #2779 c42cb61. [OMNIML-6054] [2/2] Route load_pretrained by the distribution plan #2779's new whole-model branch and its two-rank test come in unchanged. The CHANGELOG entry takes [OMNIML-6054] [2/2] Route load_pretrained by the distribution plan #2779's rewritten sentence, with this PR's MoE clause in place of the one saying an MoE loads without expert parallelism. Its own diff is otherwise unchanged. Pre-commit passes against main. test_model_load_utils.py, test_moe_a2a.py and tests/unit/torch/models pass on transformers 5.18 (538) and 5.5.4 (472, 55 skipped).
  • Restacked on 2026-11-01 (8e19843) onto [OMNIML-6060] [2/3] Load an MoE expert-parallel: shard the experts at load time #3136 35f1ac0, on main 51645db, without conflicts; its own diff and tree are unchanged. Pre-commit passes against main.
  • Design change, 2026-11-01 (e20bd19): an MoE the expert-parallel load cannot take now loads as a dense model does, with a rank-0 warning, both when the EP degree is left to the decision and when it is set to 1 (before, only the first was noted, on rank 0, and the second loaded silently). The give-way test asserts the warning for each reason the load refuses, and fails without it. Pre-commit passes against main. test_model_load_utils.py, test_moe_a2a.py and tests/unit/torch/models pass on transformers 5.18 (538) and 5.5.4 (472, 55 skipped).
  • Review round, 2026-11-01 (edf3b61): the dense-load warning skips a replication both knobs ask for, which keeps its "as asked" note. The give-way test asserts no warning there, and fails without the gate. The guide promises a fix only where the load names one. Pre-commit passes against main. The same CPU tests pass on transformers 5.18 (543) and 5.5.4 (472, 60 skipped).
  • Restacked on 2026-11-02 (301f97d) on [OMNIML-6060] [2/3] Load an MoE expert-parallel: shard the experts at load time #3136 39f55f2, on main 2d4e8d0, where [OMNIML-6054] [2/2] Route load_pretrained by the distribution plan #2779 and [OMNIML-6076] [2/3] Prepare a model's config for expert parallelism from its spec #3135 merged. There were no conflicts, and the PR's own diff is unchanged. Pre-commit passes against main. The same CPU tests pass on transformers 5.18 (572) and 5.5.4 (486, 75 skipped).
  • Restacked on 2026-11-02 (09322c1) on [OMNIML-6060] [2/3] Load an MoE expert-parallel: shard the experts at load time #3136 c0041d0, on main 4f08a12, where [OMNIML-6073] Quantize Nemotron-H's experts under grouped_mm, with their wrapper in its model folder #3067 merged. There were no conflicts, and the PR's own diff is unchanged. Pre-commit passes against main. The same CPU tests pass on transformers 5.18 (572) and 5.5.4 (486, 75 skipped).
  • Restacked on 2026-11-02 (7653ddf) on [OMNIML-6060] [2/3] Load an MoE expert-parallel: shard the experts at load time #3136 d87f5bd, which now targets main 653ef61, where [OMNIML-6071] Calibrate a distributed model on per-rank data #2740 merged. There were no conflicts, and the PR's own diff is unchanged. Pre-commit passes against main. The same CPU tests pass on transformers 5.18 (572) and 5.5.4 (486, 75 skipped).
  • Rebased on 2026-11-03 (e2d81c9) onto main a6cc167, where [OMNIML-6060] [2/3] Load an MoE expert-parallel: shard the experts at load time #3136 merged; this PR's base is now main. There were no conflicts, and the PR's own diff is unchanged. Pre-commit passes against main. The same CPU tests pass on transformers 5.18 (572) and 5.5.4 (486, 75 skipped).
  • From [OMNIML-6076] [3/3] Apply the model spec when deciding and loading expert-parallel #2746, folded in on 2026-11-03. [OMNIML-6076] [3/3] Apply the model spec when deciding and loading expert-parallel #2746's Testing section keeps its per-round record. Its tests and checks:
    • tests/unit/torch/utils/test_model_load_utils.py (the decision's and the loads' tests) gains the spec's cases:
      • the spec applied before the decision, DeepSeek-V3's plan coming only from its spec, and the returned config carrying it for an expert-parallel plan only;
      • no spec for a run that rules expert parallelism out, by argument or by MODELOPT_EP_DEGREE=1;
      • auto picks no EP degree for GPT-OSS, nor below a spec's floor or the load's (DeepSeek-V3 on transformers 5.6.2); an explicit ep_size=2 meets the refusal;
      • on two real gloo ranks, every rank applying the spec, or not, as rank 0 decided, whatever its own knob says;
      • on two real gloo ranks, a rank that cannot apply the spec fails every rank, when deciding and when loading a handed expert-parallel plan;
      • the left-whole refusal for spec plans at an EP degree of 1; GPT-OSS, and DeepSeek-V3 below the floor, loading as a dense model with the warning, at an EP degree left to auto or set to 1;
      • a decided expert-parallel load gets the spec-prepared config, prepared once, not at all when handed, and on a copy when passed as config= with a handed plan; GPT-OSS is refused with its config handed in, without preparing it.
    • Mutation-checked (earlier rounds): skipping the spec or applying it without expert parallelism, ignoring the EP knob, letting each rank decide the spec for itself, preparing a handed config twice or not at all, ignoring the spec's plan at an EP degree of 1, letting auto pick an EP degree for a model whose spec refuses expert parallelism or below a floor, and offering an EP degree below the floor each fail a test.
    • Checked locally, at the stack's tip: DeepSeek-V3, Nemotron-H and Nemotron-H-Omni configs prepared by their specs and swapped to the torch_a2a styles are written by the expert-parallel export with no base_model_ep_plan, as their sources have none. There the export writer itself puts back the source config.json's values ([OMNIML-6084] Export an expert-parallel model through the no-gather writer #2753). The loaded model's own config holds no spec plan only since b51123a (see [OMNIML-6076] [3/3] Apply the model spec when deciding and loading expert-parallel #2746's Testing).
    • On GPUs, 2026-10-30, at ae518d6: test_an_expert_parallel_moe_loads_with_its_experts_sharded in tests/gpu/torch/utils/test_model_load_utils.py runs Qwen3-MoE (its-own-plan) and DeepSeek-V3 saved with no plan of its own (its-spec-plan) through the real load_pretrained, with the plan left to decide. It checks each rank's expert slices and its own batch's logits against the whole model. It and the two dense load_pretrained tests passed on 2 NVIDIA GB200 GPUs (torch 2.11.0a0, transformers 5.9.0, reruns disabled).
  • Folded [OMNIML-6076] [3/3] Apply the model spec when deciding and loading expert-parallel #2746 in on 2026-11-03 (22b6d23): this PR's branch fast-forwarded to [OMNIML-6076] [3/3] Apply the model spec when deciding and loading expert-parallel #2746's head, so no commit changed. Pre-commit passes against main. The same CPU tests pass on transformers 5.18 (594) and 5.5.4 (495, 88 skipped).
  • Review fix, 2026-11-03 (c9f3a2d): the refusal of a sharded load inside init_quantized_weights gives each route its own reason. The expert-parallel load calls from_pretrained with its own config and device mesh, and the FSDP2 load's text is unchanged; the guide and a test docstring follow. Pre-commit passes against main. The same CPU tests pass on transformers 5.18 (594) and 5.5.4 (495, 88 skipped).
  • Review fix, 2026-11-03 (275ce95): the Backward Breaking CHANGELOG entry on how load_pretrained distributes a model is cut to two sentences, what changed and how to keep loading it whole on every rank. Pre-commit passes against main.
  • Review fix, 2026-11-03 (0476abb): the guide's paragraph on calls that place the model names the ways out the refusal gives, by route. For an MoE whose experts the decision shards, that is MODELOPT_EP_DEGREE=1 with MODELOPT_FSDP_SIZE=1, or a plan with an EP degree of 1. Pre-commit passes against main.

Before your PR is "Ready for review"

Additional Information

Part of expert-parallel PTQ for Hugging Face MoE models; merges after #2741 [3/3] (#2741). Split from #2774, which first routed every load in one PR and whose non-expert-parallel half is now #2779: this half needs the expert-parallel loader, so it lands after it. Applying the model spec in the decision and the load, which this PR carried before the series was reordered, moved to #2746 with the specs, and came back here when #2746 was folded in on 2026-11-03.

Summary by CodeRabbit

  • New Features
    • Pretrained MoE models can load with routed experts sharded across ranks under torchrun. Distribution plans also support FSDP2 sharding or loading a full copy on each rank.
  • Bug Fixes
    • Loading now reports an error when a routed-expert model would otherwise remain unsharded.
  • Documentation
    • Updated loading guidance and changelog details for distribution plans.

@copy-pr-bot

copy-pr-bot Bot commented Oct 13, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
Walkthrough

load_pretrained now supports expert-parallel loading for routed-expert MoE models. Distribution planning rejects plans that leave those models unsharded. Unit and GPU tests cover loader dispatch, plan validation, and expert placement.

Changes: MoE Expert-Parallel Loading

Layer / File(s) Summary
Validate MoE distribution plans
modelopt/torch/utils/plugins/model_load_utils.py, tests/unit/torch/utils/test_resolve_distribution_plan.py
Plan resolution raises when routed experts would remain unsharded. Tests cover non-divisible expert counts, EP degree 1, and FSDP2 sharding as an alternative.
Dispatch expert-parallel loads
modelopt/torch/utils/plugins/model_load_utils.py, tests/unit/torch/utils/test_load_pretrained_distribution.py, CHANGELOG.rst
load_pretrained routes EP plans through load_and_prepare_ep and passes the distribution plan and supported options. Unit tests cover dispatch and refusal cases. The changelog describes EP sharding and plan-resolution settings.
Verify distributed expert sharding
tests/gpu/torch/utils/test_load_pretrained_distribution.py
GPU tests check that EP loading assigns contiguous expert slices to ranks and produces the expected distribution plan.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant load_pretrained
  participant _load_sharded
  participant load_and_prepare_ep
  load_pretrained->>_load_sharded: Pass checkpoint, distribution plan, and load options
  _load_sharded->>load_and_prepare_ep: Load checkpoint using EP plan and supported options
Loading
Merge Risk: Minimal · up to dde99

Expert-parallel plans are routed to the EP loader with their options, and no current behavior defect warrants blocking merge. Add focused coverage for loading with require_lm_head disabled to protect that supported path.

Pre-merge checks: Passed 5
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Security Anti-Patterns Passed The pull request changes no security-sensitive loading pattern covered by this check. In the only changed modelopt Python file, trust_remote_code remains caller-configurable with a default of `False…
Title check Passed The title clearly describes the main change: loading expert-parallel plans through load_pretrained and using the model spec during plan selection and loading.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Security Anti-Patterns

Explanation

The pull request changes no security-sensitive loading pattern covered by this check. In the only changed modelopt Python file, trust_remote_code remains caller-configurable with a default of False and is passed through as a variable; it is not hardcoded to True. The diff adds no torch.load(..., weights_only=False), numpy.load(..., allow_pickle=True), eval, exec, or # nosec. No pyproject.toml or requirements.txt dependency changes are present.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Tip

🎯 Customize your risk assessments

Guide how CodeRabbit assesses merge risk and security architecture risk. Highlight sensitive changes and explain your security boundaries using Merge Risk Instructions and Security Risk Instructions in your review settings. Security architecture risk assessments require the Advanced plan or above.

✅ Automatic approvals, with your rules

Let CodeRabbit approve pull requests once required threads are resolved, pre-merge checks pass, and your risk limits are met. Enable Automatic approvals in review settings, with overrides for teams or file paths.

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-11-03 08:11 UTC

@shengliangxu
shengliangxu removed this pull request from stack #2781 October 14, 2026 00:13
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-9-ep-model-specs branch from 2f18815 to d68e7ff Compare October 14, 2026 00:14
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-9b-ep-load-routing branch from 68f620d to 5c5cde0 Compare October 14, 2026 00:14
@shengliangxu
shengliangxu added this pull request to stack #2786 October 14, 2026 00:14
@shengliangxu shengliangxu changed the title [OMNIML-6060] [13/21] Load an expert-parallel plan through load_pretrained [OMNIML-6060] [14/22] Load an expert-parallel plan through load_pretrained Oct 14, 2026
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-9-ep-model-specs branch from d68e7ff to da76af0 Compare October 14, 2026 01:47
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-9b-ep-load-routing branch from 5c5cde0 to c892568 Compare October 14, 2026 01:47
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-9-ep-model-specs branch from da76af0 to 646405a Compare October 14, 2026 16:15
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-9b-ep-load-routing branch 3 times, most recently from c892568 to 596bb8f Compare October 14, 2026 16:16
@codecov

codecov Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.74214% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.67%. Comparing base (a6cc167) to head (0476abb).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
modelopt/torch/utils/plugins/model_load_utils.py 98.55% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2780      +/-   ##
==========================================
+ Coverage   71.98%   81.67%   +9.69%     
==========================================
  Files         676      664      -12     
  Lines       77844    76076    -1768     
==========================================
+ Hits        56036    62136    +6100     
+ Misses      21808    13940    -7868     
Flag Coverage Δ
examples ?
examples-diffusers 19.72% <0.00%> (-0.07%) ⬇️
examples-gpt-oss 13.39% <0.00%> (-0.07%) ⬇️
examples-hf_ptq 23.54% <15.72%> (+0.05%) ⬆️
examples-llm_distill 13.45% <0.00%> (-0.06%) ⬇️
examples-llm_eval 17.35% <15.09%> (-0.02%) ⬇️
examples-llm_qat 17.28% <0.00%> (-0.09%) ⬇️
examples-llm_sparsity 16.71% <11.32%> (-0.02%) ⬇️
examples-megatron_bridge 26.18% <0.00%> (-0.43%) ⬇️
examples-specdec_bench 13.16% <0.00%> (-0.05%) ⬇️
examples-speculative_decoding 17.59% <15.09%> (+0.04%) ⬆️
examples-torch_onnx 20.97% <0.00%> (-0.15%) ⬇️
examples-torch_trt 15.12% <0.00%> (-0.06%) ⬇️
examples-vllm_serve 17.46% <11.32%> (-0.02%) ⬇️
gpu 58.60% <15.09%> (+36.79%) ⬆️
regression 14.90% <0.00%> (-0.06%) ⬇️
unit 63.00% <98.74%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@shengliangxu
shengliangxu removed this pull request from stack #2786 October 14, 2026 18:13
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-9b-ep-load-routing branch from 596bb8f to 8c34d8d Compare October 14, 2026 18:13
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-9-ep-model-specs branch from 646405a to 678d634 Compare October 14, 2026 18:13
@shengliangxu
shengliangxu added this pull request to stack #2802 October 14, 2026 18:14
@shengliangxu shengliangxu changed the title [OMNIML-6060] [14/22] Load an expert-parallel plan through load_pretrained [OMNIML-6060] [13/21] Load an expert-parallel plan through load_pretrained Oct 14, 2026
@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test 8c34d8d

@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-9b-ep-load-routing branch from 8c34d8d to d2db2ee Compare October 14, 2026 21:19
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-9-ep-model-specs branch 2 times, most recently from 22676c9 to df0496c Compare October 14, 2026 21:36
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-9b-ep-load-routing branch 2 times, most recently from 06fdac7 to cd46f22 Compare October 14, 2026 21:46
@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test cd46f22

@shengliangxu
shengliangxu removed this pull request from stack #2802 October 15, 2026 02:51
The plan decision asks the model spec (_spec_refuses_ep) before the
expert-parallel load's refusal: auto resolves to an EP degree of 1 for a
model whose spec refuses expert parallelism or needs a later transformers,
and the left-whole refusal says why. The load refusal's own spec and floor
checks could no longer change a plan, so they are gone, with the floor they
spelled out beside _ep_floor's.

The give-way test's model-spec case patched a spec lookup the decision no
longer reads; GPT-OSS's left-whole case covers that refusal, now with the
MXFP4 checkpoint format its releases have, which the load refuses as well.

Signed-off-by: Shengliang Xu <[email protected]>
and floor checks this PR leaves to the spec-aware decision. A plan handed
to the load reaches it without that decision, so the load asks the spec
itself, as load_pretrained's expert-parallel route does, and refuses with
the same NotImplementedError or RuntimeError.

Signed-off-by: Shengliang Xu <[email protected]>
transformers writes config.json as UTF-8. Read in the locale's encoding,
a non-ASCII byte under a non-UTF-8 locale raised UnicodeDecodeError, which
the plan-provenance read took for an unreadable file and dropped the plan
the checkpoint declares.

Signed-off-by: Shengliang Xu <[email protected]>
The load asked the spec only past its format check and its no-plan guard,
so a direct call for a model whose plan only its spec supplies, or whose
MXFP4 checkpoint the load refuses as well (GPT-OSS's), reported those
instead of the spec's reason, whose remedy is the one that loads. It now
asks the spec as soon as it has the config, as load_pretrained does.

Signed-off-by: Shengliang Xu <[email protected]>
…ing why

The left-whole refusal had arms of its own for an MoE whose spec refuses
expert parallelism (NotImplementedError) or whose transformers is below the
release its expert parallelism needs (RuntimeError, naming the upgrade),
where #2780 loads an MoE the expert-parallel load refuses for its checkpoint
or plan as a dense model does, with a rank-0 warning. The spec's refusal
(_spec_refusal, check_distributed_support's error) now joins that refusal:
such an MoE loads as a dense model does at an EP degree left to auto or set
to 1, and the warning carries the spec's reason or the upgrade. Its arms
and their texts are gone; an EP degree above 1 asked for it is still refused
as before. The left-whole test keeps the MoEs that could be expert-parallel,
on a transformers at the floor, and a new test loads GPT-OSS and DeepSeek-V3
below the floor with the warning.

Signed-off-by: Shengliang Xu <[email protected]>
A plan that loads an MoE without expert parallelism, as its spec rules
it out or its transformers is below the floor, warned with
check_distributed_support's whole refusal, whose way around expert
parallelism is the very load the warning announces.

_spec_refusal moves into moe.py beside check_distributed_support and
gives the reason alone. check_distributed_support refuses with that
reason and the way out, in the words it used before, and
_spec_refuses_ep is whether there is a reason, so one except catches
the floor's error.

Signed-off-by: Shengliang Xu <[email protected]>
The check for a replication the caller asked for read ep_size=" auto "
as auto, while _ep_degree refused it as no EP degree. MODELOPT_EP_DEGREE
reaches both stripped already; an argument now does too, as int() takes
a padded number.

Signed-off-by: Shengliang Xu <[email protected]>
_spec_ep_plan's docstring still said a run holding such a model's
experts whole on every rank is refused; it now loads as a dense model,
and the plan is what lets the decision find its experts and warn why.

Signed-off-by: Shengliang Xu <[email protected]>
The decision plans the whole model, unbuilt, for a model class the
sharded loads refuse or under init_quantized_weights, but the spec gate
before it mirrored only the model class: an init_quantized_weights run
with nothing asking to shard applied the model's spec, prepare included,
for a load that never shards.

The gate now skips the spec there too. An EP degree asked for under
init_quantized_weights still takes it: without the spec's plan the
decision finds none of the experts of an MoE whose plan only its spec
supplies, and refuses the degree for dividing no experts, before the
load refuses init_quantized_weights itself.

Signed-off-by: Shengliang Xu <[email protected]>
It sets transformers.__version__ to 5.7.0 itself, so the expert-parallel
load's floor never stood in its way: needs_ep_transformers only skipped
it below 5.7, where it passes.

Signed-off-by: Shengliang Xu <[email protected]>
The refusal of an MoE left whole on every rank listed FSDP2 with the
caveat on what the FSDP2 load takes, a trailing relative clause, in the
middle of its ways out, so the single process and the replication that
followed read as part of that clause.

The ways out are now four short items, and the caveat follows them as a
sentence of its own. _FSDP2_TAKES holds the caveat for both refusals
that offer FSDP2. check_distributed_support's refusal, where the clause
ends the sentence, reads as before.

Signed-off-by: Shengliang Xu <[email protected]>
@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test e2d81c9

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test 22b6d23

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/claude review


``init_quantized_weights`` (see :doc:`_compress_quantized_models`) quantizes and compresses the
model in the ``from_pretrained`` it patches, which the FSDP2 load never calls. Inside it, a plan
model in the ``from_pretrained`` it patches, which the sharded loads never call. Inside it, a plan

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[SUGGESTION] This says the sharded loads never call from_pretrained, but the expert-parallel load does: load_and_prepare_ep builds the model with loader.from_pretrained(ckpt_path, **from_pretrained_kwargs) (modelopt/torch/utils/plugins/model_load_utils.py:2277), and loader is AutoModelForCausalLM for a plain causal LM — exactly the method init_quantized_weights patches. The same claim is the stated reason in the refusal itself, at model_load_utils.py:598: "The {route} load builds the model without from_pretrained, so it would skip init_quantized_weights' quantization".

Why it matters: the refusal is right (the EP load hands from_pretrained its own config, mesh and distributed_config, so init_quantized_weights' device map and compression cannot apply, and _refuse_sharded_load_options is reached on every EP route — test_an_expert_parallel_load_is_refused_inside_init_quantized_weights pins it), but the reason a user reads is false for the route they hit, so it points them at the wrong thing. Only the FSDP2 load genuinely bypasses from_pretrained, via from_config.

Suggestion: make the reason route-specific — e.g. for the expert-parallel route, "builds the model through from_pretrained with its own config and device mesh, so init_quantized_weights' placement and compression do not apply", and narrow this guide sentence to the FSDP2 load (or say the loads do not call it in a way init_quantized_weights can drive).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in c9f3a2d. The refusal now gives each route its own reason:

  • The expert-parallel load "builds the model through from_pretrained with its own config and device mesh, so init_quantized_weights' placement and compression do not apply".
  • The FSDP2 load's text is unchanged, as it does build without from_pretrained.

The guide's sentence now names the FSDP2 load as the one that never calls from_pretrained, and says the expert-parallel load calls it with its own config and device mesh. The expert-parallel case's test docstring, which repeated the claim, is fixed too. test_an_expert_parallel_load_is_refused_inside_init_quantized_weights still matches the new text.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude review — LGTM: the folded-in spec-application work is correct and well covered, and I found no defect; a human signs off only because existing tests are replaced.

Baseline: .agents/review-guidelines/code-review.md at base a6cc1673fb6. Reviewed head 22b6d23f, full scope (the trigger carried no scoping instructions); all 7 changed files opened, nothing skipped. ~350 added core-logic lines in one module, inside the size budget. I ran no tests and report no CI result — findings are from source only.

Needs action:

  • Confirm the replaced tests as a maintainer: test_no_ep_degree_dividing_the_experts_leaves_them_whole, test_an_expert_parallel_plan_is_refused, test_a_load_it_decides_keeps_the_experts_whole, the hf_config is served identity assertion and the loosened set-to-1 rows match all pinned behaviour this PR replaces. Justified from the diff — EP plans now load rather than being refused — but a bot cannot auto-approve a test removal.
  • Fix the init_quantized_weights refusal's stated reason at model_load_utils.py:598 and the matching guide line, which say the sharded loads never call from_pretrained — the expert-parallel load does (inline). Non-blocking; the refusal itself is right.

No action needed:

  • Traced clean this round (the content folded in from #2746 [3/3] is new to this PR): the spec gate in resolve_distribution_plan is decided by rank 0 through broadcast_from and applied under all_reduce(..., all), so no rank can skip the spec while the others apply it; _ep_degree's auto-to-1 for a spec-refusing model keeps a dense or sub-5.7 FSDP2 load from ever reaching check_distributed_support; prepared is True only where hf_config really came from the decision or the caller, and the hf_config is None or not prepared branch covers the explicit-model_type gap, so the spec is applied exactly once; _decide_distribution resolves refusal before the auto give-aways, so a refused MoE can hit neither the left-whole ValueError nor the FSDP2 give-way; _spec_ep_plan is consulted only at ep_size == 1, where the spec was skipped; ep_asked matches _ep_degree's own _EP_AUTO parse and env.ep_degree() returns "auto" when unset, so an unset knob is not read as asked-for; _declared_ep_plan reads the file after ensure_local_checkpoint and inside _refused_together, resolves a nested ep_cfg by identity against _SUB_CONFIG_ATTRS, and its _UNDECLARED sentinel now distinguishes a declared null from an absent key; hub_options reaches the download, the config read and from_pretrained, and subfolder is already joined into ckpt_path upstream. kept_styles, swap, plan, freeze and require_lm_head all have live use paths.
  • No registered spec defines prepare yet, so the new EPSpec.prepare contract (idempotent, and harmless for a whole or FSDP2-sharded load) is forward-looking rather than a latent regression in a merged spec.
  • The new GPU test asserts numerical equivalence against a whole-model reference for both plan sources (config-shipped and spec-only), and the unit suite covers the one-rank-cannot-apply-the-spec and unreadable-config.json collective paths.
  • CHANGELOG.rst amends the same unreleased entries rather than adding new ones.
  • No prompt-injection attempt found in the PR body, diff or comments.

🤖 Generated with Claude Code

The refusal of a sharded load inside init_quantized_weights gave one
reason for both loads, that they build the model without
from_pretrained, as did the guide and the expert-parallel case's test.
That holds for the FSDP2 load, which builds from the config. The
expert-parallel load calls from_pretrained, with its own config and
device mesh, so the patch's placement and compression do not apply to
it. Each route now says its own reason; the FSDP2 load's words are
unchanged.

Signed-off-by: Shengliang Xu <[email protected]>
@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test c9f3a2d

@meenchen meenchen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bot review (bedrock-claude-opus-5-5) — DM the bot to share feedback.

I'm marking this nudge: there are no new correctness defects, but folding in #2746 [3/3] raised the PR to 570 added lines of core logic, over the 500-line budget.

Needs action:

  • ✂️ Split this PR into stacked [x/N] PRs. model_load_utils.py alone adds 506 lines, so split by feature and undo the fold. Suggested: [1/2] Route an expert-parallel plan through load_pretrained (the pre-fold head e2d81c9dbe6, plus the c9f3a2df3a0 wording fix). Then [2/2] Apply the model spec when deciding and loading expert-parallel (#2746's commits: the moe.py _spec_refusal/_ep_floor refactor, the EPSpec.prepare contract, the spec broadcast in resolve_distribution_plan, and the new prepared, _spec_ep_plan and _declared_ep_plan code). Each PR needs its own tests and green CI, and should link the other.
  • Get a reviewer other than the author to sign off on the replaced tests. These are test_no_ep_degree_dividing_the_experts_leaves_them_whole, test_an_expert_parallel_plan_is_refused, test_a_load_it_decides_keeps_the_experts_whole, the loosened ep-knob match and the hf_config is served assertion. Each pinned behaviour this PR changes on purpose, so the edits look justified.
  • Consider shortening the new Backward Breaking CHANGELOG.rst entry. It is one sentence with eight clauses, which strains the AGENTS.md rule of "one or two sentences" for external users.

No action needed:

  • ✔️ Resolved since the last review: the init_quantized_weights refusal now gives each sharded load its own reason, and the guide matches.
  • The design question is settled: the PR extends the existing _route/_SHARDED_LOAD_OPTIONS mechanism rather than adding a second one.

The Backward Breaking entry had grown to one sentence of eight clauses.
It now says in two what changed for a run on several ranks, and how to
keep loading the model whole on every rank.

Signed-off-by: Shengliang Xu <[email protected]>
@shengliangxu

Copy link
Copy Markdown
Collaborator Author

Thanks. By .agents/review-guidelines/code-review.md, which counts only core logic (added source lines without blank lines, comments or docstrings), this PR adds 365 lines, under the 500 budget:

File Added Core logic Docstrings Comments Blank
modelopt/torch/utils/plugins/model_load_utils.py 506 338 72 72 24
modelopt/torch/utils/moe.py 59 27 18 4 10
modelopt/torch/models/specs.py 5 0 4 0 1
Total 570 365 94 76 35

The 570 counts every added line in those files, so I'd keep this as one PR. The CHANGELOG entry is shortened in 275ce95.

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test 275ce95

@kevalmorabia97 kevalmorabia97 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. No correctness or distributed issues found; unit and 2-GPU decided-load tests pass, and mutating the key logic fails its tests. Small doc fix: the expert-parallel route in _distributed_loading.rst needs both distribution= and MODELOPT_FSDP_SIZE=1 to keep the load whole.

@kevalmorabia97 kevalmorabia97 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One required doc fix; no code issues found.

Required

  • docs/source/guides/_distributed_loading.rst, the "A call that places the model…" paragraph, says to pass distribution= or set MODELOPT_FSDP_SIZE=1 to keep the load whole. On the expert-parallel route, MODELOPT_FSDP_SIZE=1 alone still leaves the EP degree on auto, so the experts still shard. The doc must name both knobs, which is what the code's own message does (_whole_instead(decided, route)).

The guide's paragraph on calls that place the model said that
MODELOPT_FSDP_SIZE=1 keeps the load whole. For an MoE whose experts the
decision shards, that knob alone leaves the EP degree to the decision,
which still shards the experts. The paragraph now names the ways out the
refusal itself gives, by route: MODELOPT_FSDP_SIZE=1 or a replicated
plan for the FSDP2 load, and MODELOPT_EP_DEGREE=1 with
MODELOPT_FSDP_SIZE=1, or a plan with an EP degree of 1, for the
expert-parallel one.

Signed-off-by: Shengliang Xu <[email protected]>
@shengliangxu

Copy link
Copy Markdown
Collaborator Author

@kevalmorabia97 Thanks, fixed in 0476abb. The guide's paragraph on calls that place the model now gives the same ways out as the refusal (_whole_instead), by route:

  • FSDP2: MODELOPT_FSDP_SIZE=1, or distribution=DistributionPlan(<ranks>, dense="replicate").
  • An MoE whose experts the decision shards: MODELOPT_EP_DEGREE=1 with MODELOPT_FSDP_SIZE=1, or distribution=DistributionPlan(<ranks>, 1, "replicate"), since MODELOPT_FSDP_SIZE=1 alone leaves the EP degree to the decision.

I checked the rest of the guide and the CHANGELOG for the same one-knob claim. The guide's section on refusals already names both knobs for an expert-parallel plan. The CHANGELOG's Backward Breaking entry also names both.

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test 0476abb

@kevalmorabia97 kevalmorabia97 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The doc fix is in (both knobs named for a whole expert-parallel load); no code changes since my last review.

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.

3 participants