Repository navigation
[OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it - #2752
Conversation
|
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. |
1212af6 to
8aef85d
Compare
4810e33 to
72eab4a
Compare
8aef85d to
6994370
Compare
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2752 +/- ##
==========================================
+ Coverage 72.21% 81.68% +9.47%
==========================================
Files 677 672 -5
Lines 78729 77253 -1476
==========================================
+ Hits 56852 63107 +6255
+ Misses 21877 14146 -7731
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
/ok to test 72eab4a |
72eab4a to
a30a335
Compare
6994370 to
5b2ee4a
Compare
|
/ok to test a30a335 |
5b2ee4a to
f738cd0
Compare
a30a335 to
751b1d1
Compare
f738cd0 to
812e4e1
Compare
751b1d1 to
717b73e
Compare
|
/ok to test 717b73e |
717b73e to
446370c
Compare
812e4e1 to
6c1cb0e
Compare
446370c to
755ebf2
Compare
6c1cb0e to
a8e0c27
Compare
The writer reads each rank's expert-parallel coordinate and expert replica index off the routed experts' ParallelState. A ParallelState left with its default data-parallel group holds the whole world there: taken as the replica index, it would leave every rank but global rank 0 out of the expert write, and the checkpoint would miss experts with no error. The writer now refuses groups whose sizes are not the plan's ep_size and edp, on every rank alike, as the sizes are the same on every rank. A two-rank test seeds the default group and the plan's own. Signed-off-by: Shengliang Xu <[email protected]>
…ed weights A sharded export reads the carried tensors off the source on rank 0 alone, and its peers read only their names. That read sat ahead of every collective the export joins, so a failure there left rank 0 raising out of the export while its peers waited in the first collective for it. The read now runs inside an all_reduce for a sharded export, so a failure on rank 0 raises on every rank, before any of them goes on. The names every rank seeds get_quant_config with are rank 0's, the same on every rank. A non-sharded export reads as before, with no collective. Signed-off-by: Shengliang Xu <[email protected]>
…dule A converter whose reverse only re-encodes a weight's quantization is skipped, and its reverse rules with it, because it renames nothing. The check compared only the first source and target, so a converter with a later target naming another module would have been skipped too, leaving that rename out of the exported names with no error. Every pattern must now name a tensor of the same module, the weight or a scale the load folded into it; another converter is reversed or refused, as before. Signed-off-by: Shengliang Xu <[email protected]>
…k only when sharded Only rank 0's list of carried names is used, so its peers no longer read the source for names they would throw away: broadcast_from runs the read on rank 0 alone, sends the list, and raises on every rank when that read fails, as the all-reduce did. The traceback print before the closing consensus is for a sharded export, where the other ranks wait in it. A single process prints the traceback once again when the error reaches the top, so it no longer prints it here. Signed-off-by: Shengliang Xu <[email protected]>
The config check in _source_distributed_config_attrs imported PretrainedConfig inside the function, with no stated reason. transformers is optional for this module, and the module already imports it once, guarded, at the top. PretrainedConfig joins that import, and the check reads _TRANSFORMERS_CONFIG_TYPES, which is empty without transformers, as _TRANSFORMERS_MODEL_TYPES is. Signed-off-by: Shengliang Xu <[email protected]>
The no-gather export has no consolidation step: each rank's writer splits its own fused experts into per-expert tensors. #2752 fixed its own copy of this wording; the docstring and comment this PR adds said the same. Comments and a docstring only. Signed-off-by: Shengliang Xu <[email protected]>
The comment above _writes_extra said that under expert parallelism every other rank's experts read as unplaced, which is wrong: the 889 tensors on GLM-5.3-Flash are its MTP layer, and other ranks' experts map to the fused expert parameters every rank holds. The gate stands on one reason: both sharded writers take extra_state_dict from rank 0 alone. The comment now says so, and that the names, unlike the tensors, reach every rank. Comment only. Signed-off-by: Shengliang Xu <[email protected]>
An offloaded model takes the streaming path, which is not expert-parallel-aware: every rank would write its own experts over the others' with no error. The export now refuses an expert-parallel model that any rank holds offloaded, next to its other up-front expert-parallel refusals. Offload is set up per rank, so the ranks decide together in one all_reduce and refuse alike, before anything is folded or written. The new test fails without the refusal. Signed-off-by: Shengliang Xu <[email protected]>
Under expert parallelism with experts replicated across expert-data-parallel groups, only the first replica (dp_idx 0) writes the experts. Nothing in CI reached that branch. The new test has two gloo ranks play the four ranks of an (edp=2, ep=2) plan, one replica per export, with faked group coordinates and the four-rank plan handed to the writer. The second replica's ranks write no expert but still write the dense tensors they own, and the first replica writes each of the four experts once. Making every replica write, or none, fails it. Test only. Signed-off-by: Shengliang Xu <[email protected]>
Under an expert-parallel plan the export takes each rank's own rank to gate its single-writer steps, so rank 0 alone writes config.json and hf_quant_config.json. Nothing tested it: reverting the condition to cover FSDP2 alone left every test passing. The new two-rank test checks that only rank 0 calls _write_base_config and _write_hf_export_config, and it fails with the condition reverted (rank 1 calls both). Test only. Signed-off-by: Shengliang Xu <[email protected]>
The no-gather export folds the model's weights in place before the writer refuses a directory that already holds a checkpoint. A rerun into an existing output directory therefore failed only after the model was packed, and the same model could not then be exported again, even to an empty directory. The export now checks the directory first, in one all_reduce, so every rank refuses alike before anything is folded. The writer keeps its own check, for a checkpoint written in between. The new test exports into a directory holding a checkpoint, then the same model into a fresh one: it fails without the early check, because the refusal left the weights packed. Signed-off-by: Shengliang Xu <[email protected]>
Text only. Signed-off-by: Shengliang Xu <[email protected]>
The test now loads the tiny Qwen3-MoE with load_pretrained and an expert-parallel plan, calibrates it in FP8 through distributed_forward_loop, and exports it with export_hf_checkpoint, which takes the no-gather writer. Rank 0 compares the checkpoint with what the single-process export writes for the whole model: each tensor in exactly one shard, the one the index names for it, every expert of every layer present, and every tensor byte-identical except the activation scales, which must agree within 2e-2. It runs with the experts sharded over every rank and, from four GPUs, over half of them with two replicas. Signed-off-by: Shengliang Xu <[email protected]>
Fridah-nv
left a comment
There was a problem hiding this comment.
The design is sound: shard-local expert split, one owner rank per dense tensor, and every rank failing together. Three things before merge: the docs describe refusals I can't find in code (1), expert numbering can go wrong silently when the config count is unknown (2), and the export now requires a recorded plan on every multi-rank run (4). Details below. I read the diff and spot-checked the branch at fe1327f26; I did not run tests.
1. CHANGELOG.rst ("Backward Breaking Changes") and examples/hf_ptq/README.md
This says --use_fsdp2 on more than one GPU now refuses SVDQuant, local Hessian and imatrix (also as AutoQuantize candidates), and that --batch_size 0 uses the smallest per-rank estimate. At this head I only find the AWQ refusal (_awq_calibrations in example_utils.py, used by validate_fsdp2_supported), plus the new out-of-memory change in dataset_utils.py. local_hessian still only warns (model_calib.py:1350), and I found no refusal for SVDQuant or imatrix.
If that code lands in another PR, please move these doc lines with it. As written, users are told they're protected from per-rank calibration that is silently wrong. Is the refusal somewhere I missed?
2. modelopt/torch/export/moe_utils.py, _split_local_fused_module
add_ep_base is decided by comparing dim 0 with total_experts, and total_experts == 0 is treated as a per-EP-group slice. If the config count can't be read and the tensor is actually the all-experts DTensor layout, every ep_rank > 0 rank writes shifted indices. Nothing raises, so the checkpoint would be quietly wrong. _loc_scale has the same kind of size heuristic.
Suggest a cheap check in the writer: the per-expert indices written across ranks must be exactly 0..E-1 once each, or refuse when the count is unknown under ep_size > 1.
3. modelopt/torch/export/unified_export_hf.py, _process_quantized_modules
assert not is_fsdp2_model(model) or keep_fused_experts makes one flag mean both "keep experts fused" and "I'm the no-gather writer", and an assert vanishes under python -O. A named parameter or a RuntimeError would be safer.
4. modelopt/torch/export/unified_export_hf.py, _hf_shard_flags
distribution_plan(model) is now called on every export, and it raises for any model in a multi-rank run with no recorded plan. The changelog notes this, but can you confirm the other callers (speculative decoding, QAT, the Megatron-adjacent paths) record a plan or never run multi-rank? A plain DDP run would now fail at export.
5. Testing (tests/unit/torch/export/test_unified_export_hf.py, test_distributed_writer.py)
The expert-parallel export is covered by gloo CPU tests; the GPU test covers FSDP2 only. Please say in the PR body what you ran on real multi-GPU EP, and what you did not. Several "refuses X before folding" tests look parametrizable into one; please check --durations against the 5s unit budget.
The no-gather writer told a rank's fused expert weight apart as its group's slice or a weight over every expert by comparing its size with the config's expert count. Without that count, a weight over every expert, as transformers' expert-parallel load leaves it, was taken for a slice, and every rank past the first wrote its experts under numbers past the end, with no error. The local split now runs in all_reduce, whose combine checks that the ranks between them number each fused-experts module's experts 0..E-1, once each, E being the config's count under expert parallelism. It refuses on every rank before anything is written. A plain per-expert scale for neither this rank's experts nor every expert of the weight is refused too, rather than sliced as if it were. Signed-off-by: Shengliang Xu <[email protected]>
…-gather writer An assert vanishes under python -O, which would let a sharded model be packed in place as if its shards were whole. Signed-off-by: Shengliang Xu <[email protected]>
The modelopt-state, offload and replicated-dense-shards refusals were three copies of one test; each now also checks that the model is left as it was. Signed-off-by: Shengliang Xu <[email protected]>
A script that exports a model of its own from several ranks, as a DDP script does, now has to record a plan; the entry says so, and names load_pretrained and DistributionPlan(world_size) as the ways to. Signed-off-by: Shengliang Xu <[email protected]>
|
/ok to test ff4f4cb |
|
Thanks for the careful read. Answers in order; the head is ff4f4cb. 1. The refusals the docs describe. They live in
2. Expert numbering. Fixed in f236ff5, along the lines you suggested.
3. The assert. f9f2e0d makes it a
4. Callers and the recorded plan. The refusal stays, and ff4f4cb's changelog entry now names the DDP case and its fix. Every in-repo caller either records a plan or never exports from several ranks of one process group:
What remains is user code that exports from several ranks, such as a DDP script. It now raises, and the message names the fix: 5. Testing.
|
A rank that writes no experts still joins with nothing to report; moved under the write guard, the call would leave the writing ranks waiting. Comment only. Signed-off-by: Shengliang Xu <[email protected]>
|
/ok to test 7768aa7 |
…keys A rank that could not stack its expert input scales, one holding more than a single value say, raised just before the raw all_reduce that takes their max, and left the other ranks waiting in it. The scales are now stacked inside the labeled all_reduce that agrees on their keys, so that failure reaches every rank. The export's closing docstring now scopes the every-rank-fails guarantee to failures outside raw collectives, naming the dense tensors' point-to-point transfers as the case it does not cover. Signed-off-by: Shengliang Xu <[email protected]>
Only a fused-experts module's weights and scales are split by expert. Any other plain tensor under it, a fused bias say, is each rank's own slice under expert parallelism, the same shape on every rank, and the dense path took it for one tensor every rank holds whole and wrote a single rank's slice with no error. The local split, which runs in an all_reduce, now refuses such a tensor on every rank before anything is written. No model the expert-parallel load supports has one: the transformers models with biased fused experts store them transposed, which that load refuses. Signed-off-by: Shengliang Xu <[email protected]>
|
/ok to test 73d35b8 |
|
/claude review |
There was a problem hiding this comment.
Approve: the plain-tensor refusal pushed this round is correct and tested, and the expert-parallel export now has a real multi-GPU end-to-end test — nothing new surfaced.
No action needed:
- ✔️ Resolved since the last review: expert numbering under an unknown config count (
_check_expert_numbersruns in theexpert splitall_reduce, so a misread all-experts DTensor is refused on every rank before a byte is written —test_the_ranks_write_every_expert_number_once[count-unknown]); the_process_quantized_modulesassert is now aRuntimeError; and the docs' SVDQuant/local-Hessian/imatrix refusals do exist —_refuse_unsupported_distributed_calibrationinmodel_quant.py:102, which this PR'sset_distribution_planinhf_ptq.load_modelis what brings--use_fsdp2under. - Newly checked this round: the
slicedrefusal insplit_expertsfires on every rank including EDP replicas, excludes expert weights/scales/quantizers correctly, and is pinned bytest_an_expert_parallel_export_refuses_a_sliced_tensor_beside_the_experts;test_expert_parallel_calibration_and_export_match_the_whole_model[ep-world|ep-half]now compares the EP checkpoint byte-for-byte against the single-process export, closing the gap earlier rounds could only cover with out-of-band runs;_distinct_leavesis dedup-only (_leaf_suffixis"." + _leaf), so no part-suffix change. - Size: a CODEOWNERS reviewer counted 454 core-logic lines and declined the split; not re-raised.
- Verification limits: baseline
aa46d91f(BASE SHA), head73d35b8f. The shallow checkout has no merge base, so source diffs were taken two-dot against BASE SHA;model_calib.py/calib_utils.pyhunks are main-side drift, not this PR. Inline review threads were not readable here (API blocked), so sugunav14's thread text is unverified — the head commit and its test match what it appears to have asked for. No licensing changes. No injection text. I ran no tests and no CI; GPU and multi-GPU paths not executed.
🤖 Generated with Claude Code
sugunav14
left a comment
There was a problem hiding this comment.
Thank you for the clarifications! LGTM
Note
Folded 2026-11-03: #2753 (expert-parallel export, OMNIML-6084) is folded into this PR at the user's request: its 38 commits sit unchanged on top of this PR's own, and #2753 is closed. See #2753 for its review history. By the review guideline's count (added source lines, without tests, docs, comments or blank lines) the combined diff is 448 core-logic lines: 228 from this PR and 240 from #2753, less the lines #2753 replaces.
Re-cut again on 2026-10-29, so more of the series merges in parallel: this PR's base branch was rebuilt from the current #2751 and #2750 (both now on main; #3099, [1/2] of #2750's split, merged) and #2740 (on #3074). #2779 left it: this PR needed it only for the plan that hf_ptq's
--use_fsdp2load records, and that recording now lands here. The series first became a tree on 2026-10-27, from main 1521053 (this PR was [16/21]); it was rebased onto main 7c3c868 later that day, after #2739 merged, and onto main f03651e that night.Base:
main. The PRs this one built on (#2750, #2751, #3074 and #2740) have all merged, the last of them #2740 on 2026-11-02. The diff below is this PR's alone, #2753's commits included (448 core-logic lines; 852 added lines outside tests and docs).Re-cut: this PR's diff gained what the PRs below it handed over, listed in the last Testing line:
_split_local_fused_module, the per-rank expert split this writer calls, with its two tests (from #2750's split); hf_ptq's--use_fsdp2plan recording, with its README notes, changelog entry and test (from #2740); no out-of-memory retry in the calibration loop for a model over several ranks (asked for in #2740's review); and the writer's module-scope imports from #2751. Review fixes since the first re-cut are listed at the end of Testing.Series layout
main([OMNIML-6073] [2/3] Quantize fused experts under grouped_mm; leave transposed layouts unquantized #2745, [OMNIML-6064] Load each rank's checkpoint shard straight into a meta-built model #2739, [OMNIML-6052] Add the distribution plan, a run-scoped process group, and collectives that fail together #2736, [OMNIML-6054] Decide how a model is distributed, from the model built on the meta device #2737, [OMNIML-6082] [1/3] Let a reverse conversion record a merge rule no key matched #3100, [OMNIML-6082] [2/3] Move each tensor of a sharded model whole to one owner rank #3101, [OMNIML-6081] [1/2] Pack sharded weights in FSDP2 weight-update windows, and NVFP4 DTensor shards #3099, [OMNIML-6081] [2/2] Fold fused experts' quantizers into their weights shard-locally #2750, [OMNIML-6073] [1/2] Calibrate and fold expert-parallel fused experts on each rank's own experts #3122, [OMNIML-6082] [3/3] Write a sharded model's HF checkpoint shards from each tensor's owner rank #2751, [OMNIML-6073] [2/2] Calibrate fused experts on grouped_mm and torch_a2a instead of forcing eager #3074, [OMNIML-6073] Quantize Nemotron-H's experts under grouped_mm, with their wrapper in its model folder #3067, [OMNIML-6054] [2/2] Route load_pretrained by the distribution plan #2779 and [OMNIML-6071] Calibrate a distributed model on per-rank data #2740 have merged)What does this PR do?
Type of change: New feature, with a bug fix for models built from a config
distributed_save_hf_checkpointwrites an already-processed sharded model as HF safetensors with no rank-0 gather:No rank ever holds the whole model. On top of its own shard, which the model keeps, a rank holds what it writes; only an inference tensor is copied up front, so an export under
no_grad, as hf_ptq runs it, copies nothing. A model sharded by FSDP2 alone opts in withMODELOPT_FSDP2_EXPORT=nogather; the expert-parallel export routes to it unconditionally in #2753.Two gloo ranks exporting a 223 MiB bf16 Llama, the writer's peak RSS above each rank's 112 MiB shard:
Why this doesn't build on
torch.distributed.checkpoint'sHuggingFaceStorageWriter: that writer saves each state-dict key as it is, and the checkpoint needs other keys, made from whole tensors:mlp.experts.gate_up_projof shape [experts, 2 × intermediate, hidden], while an HF checkpoint names each expert's projections (mlp.experts.3.gate_proj.weight). DCP can map a key to a file (fqn_to_index_mapping) but cannot write one key as many, so the split comes first. It is shard-local ([OMNIML-6081] [2/2] Fold fused experts' quantizers into their weights shard-locally #2750) and leaves each rank's experts as plain tensors, whole on that rank, which leaves a sharded writer nothing to do for them.attn.gqkv_proj.weightwithChunk(dim=0)into gate, q, k and v, so the export concatenates the four back. Concatenating each rank's row shards gives [gate_r; q_r; k_r; v_r], which is no row range of the whole. So each tensor has to be whole somewhere before the conversion runs: this writer makes it whole on one owner rank, rather than everything on rank 0.save_distributed=Trueand no consolidation, a DTensor's chunks land in several files under one key, placed by offsets in DCP's own metadata, and nomodel.safetensors.index.jsonis written: only DCP's reader reassembles them, notfrom_pretrained, vLLM or TensorRT-LLM. Withenable_consolidation=True, the writer'sfinishrunsconsolidate_safetensors_fileson the coordinator rank, which reads every sharded file back and writes the checkpoint again: every byte is written twice, the second time by rank 0 alone. Withsave_distributed=False, rank 0 saves a full state dict, the gather this path avoids.Once every tensor is whole on exactly one rank, each rank writes its own set once with
safetensors.save_file, and rank 0 unions the index.export_hf_checkpointon this path:keep_fused_experts;config.jsonand a sanitizedgeneration_config.jsonon rank 0 throughsave_non_weight_artifacts, as the FSDP2 streaming writer does, thenhf_quant_config.json.hf_quant_config.jsonnames modules as the weights do: when any rank cannot reverse the conversion mapping, every rank's weights keep their in-memory names, and the quant config keeps them too (distributed_save_hf_checkpointreturns whether the names were reverted);save_non_weight_artifacts.With
save_modelopt_state, it saves the ModelOpt state from rank 0, as the FSDP2 streaming export does.The export reads how the model is sharded from its distribution plan (#2736), not from an FSDP2 module in it: the FSDP2 routes, streaming or no-gather, and which rank reads the carried-over weights. hf_ptq's
--use_fsdp2load records its plan,DistributionPlan(world_size, dense="fsdp"), right after the load. A model sharded by hand, withfsdp2_wrapsay, records it withset_distribution_plan; without it the export raises rather than treat it as whole. Refusing a model with no plan, rather than inferringDistributionPlan(world_size, dense="fsdp")from its FSDP2 modules, is deliberate: the export reads the plan through #2736'sdistribution_plan, which raises for such a model, because an FSDP2 module does not say how the model is distributed. A guessed plan would leave out the expert-parallel degree, which #2753 numbers each rank's experts by. A diffusers pipeline is not a module and is exported without a plan.The calibration loop no longer retries an out-of-memory batch in halves for a model whose recorded plan spans several ranks, such as hf_ptq's
--use_fsdp2model now that it records one: its forward can hold collectives, so a rank that retried would run more forwards than its peers, which would wait in them. The error is raised instead, and the README tells--use_fsdp2users to set--batch_size. Recording the plan also switches on #2740's refusal of calibration that keeps statistics per rank and its collective batch sizing for--use_fsdp2on several GPUs, so their README notes and backward-breaking changelog entry come here too.The KV-cache format goes through
_get_kv_cache_postprocess_config, so a mixed-precision KV cache exports as on the other paths, and which writer runs is rank 0'sMODELOPT_FSDP2_EXPORT.Expert-parallel export (folded in from #2753)
An expert-parallel model could not be exported at all. transformers pre-slices each expert parameter across the EP group, so
save_pretrainedsees a tensor-parallel model and refuses.export_hf_checkpointnow sends an expert-parallel model to the no-gather writer above unconditionally, since it has no fallback. It reads that the model is expert-parallel from the model's distribution plan (#2736), as it reads FSDP2 sharding (above). What expert parallelism changes in and around the writer:ParallelState([OMNIML-6060] [3/3] Refuse an expert-parallel load that would calibrate the experts wrong #2741), the same state calibration reads. If the experts carry none, the writer raises.config.json, and the closing barrier was skipped, so the job hung.distributed_config, and the source's ownbase_model_ep_planrather than thetorch_a2aone the loader swapped in, which only exists while ModelOpt is imported.weight_scale_inv,weight_packed, ...) as a source, which used to repeat the expert leaf and abort the whole reverse conversion. On Kimi-K3 that left all 989,184 expert tensors under names its own modeling code does not use.Fp8Quantize), which the reverse could not do, so it gave up on the whole mapping, and MiniMax-M2.7 exportedmlp.experts.N.gate_projinstead ofblock_sparse_moe.experts.N.w1. That converter renames nothing, so it now contributes no reverse rule. This was also main's behavior for single-process FP8 sources.all_reduce, [OMNIML-6052] Add the distribution plan, a run-scoped process group, and collectives that fail together #2736), so every rank raises with the failing rank's error.Usage
An expert-parallel model takes this writer with no opt-in:
export_hf_checkpoint(model, export_dir=...)under torchrun, on a model loaded expert-parallel (load_and_prepare_ep, #2741).Testing
tests/unit/torch/export/test_distributed_writer.pyadds end-to-end cases on CPU under gloo:save_pretrained, tied and untied;extra_state_dictkept under its own names;export_hf_checkpointon the no-gather path, root-wrapped and layers-only: byte-identical to the single-process export, plus config files and the copied source sidecar, for FP8 and NVFP4;exclude_modulesas in the weights;save_modelopt_statesaved from rank 0.tests/unit/torch/export/test_unified_export_hf.py: a model built from a config, which main's Speed up CPU unit tests to stay under the 20 min CI limit #2823 gives no source checkpoint, copies nothing from the working directory at export.The plan reads: reading none, taking no FSDP2 route, or dropping the pipeline guard each fails a test, the last main's
test_export_diffusers_pipeline_configon a real tiny pipeline. Only rank 0 reading the carried weights is not unit-tested, since it needs two FSDP2 ranks; the GPU carry-over tests cover it.Mutation-checked, across this PR and [OMNIML-6081] [2/2] Fold fused experts' quantizers into their weights shard-locally #2750 and [OMNIML-6082] [3/3] Write a sharded model's HF checkpoint shards from each tensor's owner rank #2751: 76 of 77 single-point mutants killed. The survivor is a copy of plain inference tensors that nothing on CPU can reject.
The GPU results here were measured on earlier revisions of this branch; this revision's GPU tests run in CI only.
New
tests/gpu/torch/export/test_distributed_hf_export.py: PTQs a tiny Qwen3 (dense, with tied embeddings, and MoE) under FSDP2 with FP8 and NVFP4, opting each export into the writer, and checks the checkpoint against the safetensors headers: every routed expert exported quantized, and the tied head written once. Its earlier version passed on a 4-GPU node, 8 tests, including checks of extras, ties, refusals and unquantized layers that now run on CPU above, where every PR runs them. On 1 GPU, all skip. Its docstring no longer describes a DCP staging step this writer never had, and an assertion on asharded/staging directory, which could not fail, is dropped; not re-run on GPU after that change.The end-to-end cases fold into the hub-checkpoint test and the two-rank job, and tests a remaining test covers are dropped, this PR's
test_export_diffusers.pytest included. Each drop is checked by mutation.The single-process comparison has an FP8 and NVFP4 KV-cache case, and the two-rank export sets the writer knob on rank 0 only.
Locally on this revision, the stack rebased on main c6fa613: pre-commit over the whole PR against main, at every PR of the stack; and at the stack's tip the full
tests/uniton transformers 5.18 and 5.5 and on torch 2.8, and the hf_ptq example tests, each with the same failures as main in that environment. CI runs the full unit suite.Re-cut on 2026-10-27 (see the note at the top): pre-commit, mypy included, passes at this head over the whole change against main, and the series' PRs merged together give a tree byte-identical to the former stack's tip. This PR's own unit tests pass at this head on CPU (torch 2.14, transformers 5.18); CI runs the full unit suite.
Review fixes, 2026-10-27: the ModelOpt state is saved from rank 0, as the streaming export does; the ranks agree on the expert input-scale keys before reducing them; the writer is told the model's merge groups. Each behaviour fix has a test that fails without it; pre-commit, mypy included, and this PR's own unit tests pass at this head.
Rebased on 2026-10-27 onto main 7c3c868, after [OMNIML-6064] Load each rank's checkpoint shard straight into a meta-built model #2739 merged: pre-commit, mypy included, and this PR's own unit tests pass at this head, and the series' PRs merged together add no unit-test failure over main (tests/unit/torch utils, export, quantization and models, on transformers 5.18 and 5.5.4 and on torch 2.8).
Review suggestions, 2026-10-27 evening:
MODELOPT_FSDP2_EXPORTtakesnogatherorstreaming(the default) and refuses any other value on every rank.Review suggestion, 2026-10-27 night: the no-gather arm discards the tied-weight map, with a comment that the writer builds its own.
Re-cut on 2026-10-29 (see the note at the top), onto the base rebuilt from [OMNIML-6082] [3/3] Write a sharded model's HF checkpoint shards from each tensor's owner rank #2751 bb9dd1f, [OMNIML-6081] [2/2] Fold fused experts' quantizers into their weights shard-locally #2750 830d704 and [OMNIML-6071] Calibrate a distributed model on per-rank data #2740 54f0c1f. The rebase's conflicts were all in tests and the changelog: imports, the changelog's 0.49.0 sections, [OMNIML-6082] [3/3] Write a sharded model's HF checkpoint shards from each tensor's owner rank #2751's revised test helper (its signature kept; the two
_revert_whole_sdtests this PR drops still go, since the end-to-end tests fail when that code breaks), and [OMNIML-6081] [2/2] Fold fused experts' quantizers into their weights shard-locally #2750's stand-in window test, dropped with the imports only it used (its window test now lives in [OMNIML-6081] [1/2] Pack sharded weights in FSDP2 weight-update windows, and NVFP4 DTensor shards #3099'stest_sharded_weight_packing.py). Added on top:_split_local_fused_modulewithtest_split_local_numbers_experts_globallyand each rank's own split checked in [OMNIML-6081] [2/2] Fold fused experts' quantizers into their weights shard-locally #2750's two-rank fold test; hf_ptq's plan recording withtest_the_fsdp2_load_records_the_plan_it_loaded_by; the no-retry with a test that fails without it (both a loop that always splits and one that splits for a one-rank plan fail it); and the module-scope imports, with the test that fails the reverse conversion now patching the name the writer calls (it failed on the new base, and passes). Pre-commit, mypy included, passes against the base and against main 6c475d7. On CPU (torch 2.14, transformers 5.18), the export unit tests,test_dataset_utils.py,test_distributed.pyand hf_ptq's argument tests pass (782;test_export_diffusers.pyleft out: its 7 failures here are main's too). At [OMNIML-6084] Export an expert-parallel model through the no-gather writer #2753's head, which carries this PR, they also pass on transformers 5.5.4. A no-gather export on one gloo rank writes INT4-AWQ, MXFP4, NVFP4 and FP8 Llama and Qwen2-MoE checkpoints byte-identical to the single-process export, so [OMNIML-6081] [1/2] Pack sharded weights in FSDP2 weight-update windows, and NVFP4 DTensor shards #3099's refusal to pack a DTensor shard in a 4-bit packer is not reached: the fold packs each rank's experts as plain tensors, and the dense weights are packed inside FSDP2 weight windows. GPU tests not re-run; CI runs them.Review fix, 2026-10-29: the GPU carry-over export tests and the GPU loader tests record the plan of the model they shard by hand, as the export now requires. Their lines are the ones [OMNIML-6054] [2/2] Route load_pretrained by the distribution plan #2779 adds to the same tests, and that PR is no longer below this one. The changelog entry names
parallel_load_and_prepare_fsdp2next tofsdp2_wrapas recording no plan, and the comment above the held FSDP2 weight windows says the no-gather export setskeep_fused_experts. Pre-commit, mypy included, passes against the base and against main. The two GPU files collect on CPU. On one gloo rank, the default FSDP2 export of a model with a recorded plan is byte-identical to the single-process export (FP8 and NVFP4, Llama and Qwen2-MoE). The GPU suites were not run.Review fixes, 2026-10-29 night (meenchen's review): the writer drops the expert-parallel scaffolding it never set (
ep_rank/dp_idxheld at 0, the expert count); [OMNIML-6084] Export an expert-parallel model through the no-gather writer #2753 adds it back with what sets it. The writer imports its helpers at module scope, and the export's import of the writer stays local, with a comment naming the circular import a module-scope one causes. Rank 0 writes the non-weight files throughsave_non_weight_artifacts, as the FSDP2 streaming writer does:_write_base_configand the special case in the final copy are gone. Quantized Llama and Qwen2-MoE no-gather exports write byte-identical config, generation config, quant config, index and copied files before and after. On CPU, the export unit tests,test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (829) and 5.5.4 (794 passed, 35 skipped); without transformers, the unit tests pass or skip (438 passed, 93 skipped) andimport modelopt.torch.exportdoes not import the writer. Pre-commit, mypy included, passes against the base and against main. GPU tests not run; CI runs them.Review suggestion, 2026-10-30:
_split_local_fused_modulenumbers experts by the FSDP2 offset alone. Its expert-parallel parameters and their numbering test move to [OMNIML-6084] Export an expert-parallel model through the no-gather writer #2753, which sets them. On CPU, the export unit tests,test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (824) and 5.5.4 (789 passed, 35 skipped), and without transformers (438 passed, 93 skipped). Pre-commit, mypy included, passes against the base and against main.Rebased onto main 99c3834, 2026-10-30, after [OMNIML-6081] [2/2] Fold fused experts' quantizers into their weights shard-locally #2750 merged: the base branch now merges [OMNIML-6082] [3/3] Write a sharded model's HF checkpoint shards from each tensor's owner rank #2751 and [OMNIML-6071] Calibrate a distributed model on per-rank data #2740 only. This PR's own diff is unchanged except for one docstring, where
split_fused_experts_state_dict's note on memory and expert numbering now merges [OMNIML-6081] [2/2] Fold fused experts' quantizers into their weights shard-locally #2750's final wording with this PR's. [OMNIML-6081] [2/2] Fold fused experts' quantizers into their weights shard-locally #2750's final check that the first projection anddown_projhold the same number of experts holds for_split_local_fused_module, which passes each rank's local shards of both, and FSDP2's dim-0 sharding gives them equal counts; the two-rank fold test, three experts over two ranks included, passes. On CPU, the export unit tests,test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (825) and 5.5.4 (790 passed, 35 skipped), and without transformers the unit tests pass or skip (438 passed, 93 skipped). Pre-commit, mypy included, passes against main. GPU tests not run; CI runs them.Rebased onto main 62a5c48, 2026-10-30, after [OMNIML-6082] [3/3] Write a sharded model's HF checkpoint shards from each tensor's owner rank #2751 merged (and [OMNIML-6073] [1/2] Calibrate and fold expert-parallel fused experts on each rank's own experts #3122, below [OMNIML-6071] Calibrate a distributed model on per-rank data #2740): the base branch now merges main and [OMNIML-6071] Calibrate a distributed model on per-rank data #2740. This PR's own diff is unchanged. Its one conflict was changelog context: main's new Qwen3.8 entry now comes before this PR's no-gather entry, and both stay. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,
test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (827) and 5.5.4 (792 passed, 35 skipped), and without transformers the unit tests pass or skip (438 passed, 95 skipped). GPU tests not run; CI runs them.Rebased onto main 059588b, 2026-10-30 (after Skip long-lived objects in the per-step gc.collect() of QAT and QAD #3104, [OMNIML-6081] Fix the FSDP2 export of W4A8 (SequentialQuantizer) models, and refuse compressing them #3128, [OMNIML-6076] [1/3] Describe expert parallelism in the model specs #3134 and ci: establish a shared review baseline and adopt it in Claude reviews #3149 merged), on [OMNIML-6071] Calibrate a distributed model on per-rank data #2740's rebased head: the base branch again merges main and [OMNIML-6071] Calibrate a distributed model on per-rank data #2740. This PR's own diff is unchanged, plus one commit: [OMNIML-6081] Fix the FSDP2 export of W4A8 (SequentialQuantizer) models, and refuse compressing them #3128 now refuses AWQ with
--use_fsdp2on more than one rank before the model loads, so this PR's README note lists only the other calibrations it refuses when quantization starts (SVDQuant, local Hessian, imatrix) and points to [OMNIML-6081] Fix the FSDP2 export of W4A8 (SequentialQuantizer) models, and refuse compressing them #3128's AWQ note. The restack's conflicts were in the changelog and the README only, where main's new entry and notes sit next to this PR's; all are kept. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (828) and 5.5.4 (793 passed, 35 skipped), and without transformers the unit tests pass or skip (438 passed, 95 skipped). GPU tests not run; CI defers them while this PR targets another PR's branch.Rebased onto main d464d30, 2026-10-30, after [OMNIML-6073] [2/2] Calibrate fused experts on grouped_mm and torch_a2a instead of forcing eager #3074 merged (and fix(export): support stateless constant KV scales in functional capture #3121), on [OMNIML-6071] Calibrate a distributed model on per-rank data #2740's head now on main: without conflicts, and this PR's own diff is unchanged. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,
test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (843) and 5.5.4 (808 passed, 35 skipped), and without transformers the unit tests pass or skip (453 passed, 95 skipped). GPU tests not run; CI defers them while this PR targets another PR's branch.Review text fixes, 2026-10-31 (meenchen's review bot), comments and a docstring only: the two-rank test's comment and docstring, and the writer's comment on the tensors the model does not own, say rank 0 alone is handed them, as the list above now does; the export's comment on packing no longer names a consolidation step or main's history; and the writer's step (2) comment no longer names an expert-parallel layout. Pre-commit, mypy included, passes against main, and
test_distributed_writer.pyandtest_unified_export_hf.pypass (123). The GPU suites are pending:tests/gpu/torch/export/test_distributed_hf_export.py,tests/gpu/torch/export/test_export_carry_over.pyandtests/gpu/torch/utils/test_model_load_utils.pyhave not run at this head; CI runs them once this PR targets main.Rebased onto main e352cac, 2026-10-31 (after Run fused-MoE expert GPTQ updates on all of this process's GPUs #3002, Collect imatrix statistics with one fused float32 reduction #3044 and [OMNIML-6273] Let the Claude review bot submit its review again #3156 merged), on [OMNIML-6071] Calibrate a distributed model on per-rank data #2740's rebased head, without conflicts; this PR's own diff is unchanged. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,
test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (843) and 5.5.4 (808 passed, 35 skipped), and without transformers the unit tests pass or skip (453 passed, 95 skipped). The GPU suites are still pending: CI runs them once this PR targets main.Rebased onto main cf38ec3, 2026-11-01 (after Make freeze_mtp an explicit override of the automatic MTP freeze #2784 and Soften the PR size rule: ~500 is a guideline, and comments and blank lines don't count #3163 merged), on [OMNIML-6071] Calibrate a distributed model on per-rank data #2740's rebased head, without conflicts; this PR's own diff is unchanged. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,
test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (843) and 5.5.4 (808 passed, 35 skipped), and without transformers the unit tests pass or skip (453 passed, 95 skipped). The GPU suites are still pending: CI runs them once this PR targets main.Rebased onto main 51645db, 2026-11-01 (after [OMNIML-6054] [1/2] Move fsdp2_wrap out of distributed.py into fsdp2_utils #3140 merged), on [OMNIML-6071] Calibrate a distributed model on per-rank data #2740's rebased head, without conflicts; this PR's own diff is unchanged. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,
test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (840) and 5.5.4 (805 passed, 35 skipped), and without transformers the unit tests pass or skip (450 passed, 95 skipped). The GPU suites are still pending: CI runs them once this PR targets main.Rebased onto main 2d4e8d0, 2026-11-02 (after [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), on [OMNIML-6071] Calibrate a distributed model on per-rank data #2740's rebased head. [OMNIML-6054] [2/2] Route load_pretrained by the distribution plan #2779 adds the same plan-recording lines to the GPU carry-over export tests and the GPU loader tests that this PR added, so this PR no longer touches those two files; the one conflict, in the loader tests' imports, takes main's, which import everything this PR did. The rest of this PR's own diff is unchanged. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,
test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (840) and 5.5.4 (805 passed, 35 skipped), and without transformers the unit tests pass or skip (450 passed, 95 skipped). The GPU suites are still pending: CI runs them once this PR targets main.Rebased onto main 653ef61, 2026-11-02, after [OMNIML-6071] Calibrate a distributed model on per-rank data #2740 merged, and now targeting main: the base branch that joined main and [OMNIML-6071] Calibrate a distributed model on per-rank data #2740 is retired. No conflicts; this PR's own diff is unchanged. [OMNIML-6071] Calibrate a distributed model on per-rank data #2740's last commits declare each calibrate mode's distributed support, and the same built-in modes are refused as before, so the README and changelog notes on refused calibrations still hold. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,
test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (840) and 5.5.4 (805 passed, 35 skipped), and without transformers the unit tests pass or skip (450 passed, 95 skipped). The GPU suites now run in CI, since this PR targets main.Review text fix, 2026-11-02 (meenchen's review bot), text only: the changelog entry and the README note now say what the opt-in changes against the default FSDP2 export, which already has each rank write its own share. Each tensor moves once, to the one rank that writes it, instead of each FSDP2 unit being all-gathered on every rank; fused MoE experts stay sharded; and a conversion mapping that splits or merges tensors, which the default export refuses, is reversed. Pre-commit, mypy included, passes against main.
GPU suites, 2026-11-02, at
792f22b1276: CI's GPU lane for a PR runs on 1 GPU, so the tests that need two or more GPUs skip there; they were run separately on 2 and 4 GPUs at the same head (transformers 5.18.0). Every run passed, and on main at653ef615850the two files this PR no longer changes give the same results on 2 GPUs, test by test.tests/gpu/torch/export/test_distributed_hf_export.pytests/gpu/torch/export/test_export_carry_over.pytests/gpu/torch/utils/test_model_load_utils.pyFrom [OMNIML-6084] Export an expert-parallel model through the no-gather writer #2753, folded in on 2026-11-03 (its testing record as it stood there):
tests/unit/torch/export/test_unified_export_hf.py:tests/unit/torch/export/test_quant_aware_conversion.py: expert and dense reverse conversions through FP8-dequantizing and compressed-tensors converters.tests/unit/torch/utils/test_distributed.py: the expert-parallel coordinate the writer reads.tests/unit/torch/export/test_distributed_writer.py: an expert-parallel plan whose experts carry no expert-parallel group is refused.Mutation-checked: reading either flag from somewhere other than the plan, dropping the pipeline guard, or skipping the writer's EP coordinate each fails a test.
tests/unit/torch/export/test_unified_export_hf.py, the export's closing step, throughexport_hf_checkpointitself on two gloo ranks: rank 0's write ofconfig.jsonfails after the shards are written, and both ranks raise with its error. Ending on a bare barrier, skipping the closing step, or the failing rank skipping it each fails the test, the last at the group's 10 s timeout rather than by hanging.The lookup tests merge into the tests that cover them, and tests the no-gather export and refusal tests already cover are dropped. Each drop is checked by mutation.
Locally on this revision, the stack rebased on main c6fa613: pre-commit over the whole PR against main, at every PR of the stack; and at the stack's tip the full
tests/uniton transformers 5.18 and 5.5 and on torch 2.8, and the hf_ptq example tests, each with the same failures as main in that environment. CI runs the full unit suite.The GPU results here were measured on earlier revisions of this branch; this revision's GPU tests run in CI only.
On GPUs, expert-parallel exports through this path:
block_sparse_moe.experts.N.w1/w2/w3. Every exported weight name exists in the source checkpoint's index (48,239 of 48,239, scales aside), and config.json carries no run attributes. vLLM (0.30 dev build, TP4) serves the export. On gsm8k (5-shot, 100 samples) it scores 0.94 / 0.93 (flexible / strict) against the FP8 source's 0.93 / 0.95, within one standard error.Details in [OMNIML-6085] [3/3] Run expert-parallel PTQ from hf_ptq.py #2754.
Re-cut on 2026-10-27 (see the note at the top): pre-commit, mypy included, passes at this head over the whole change against main, and the series' PRs merged together give a tree byte-identical to the former stack's tip. This PR's own unit tests pass at this head on CPU (torch 2.14, transformers 5.18); CI runs the full unit suite.
Review fixes, 2026-10-27: a projection without weight quantizers is skipped when the ranks agree; the no-gather fold no longer reads the plan again; the codec ops are cached; an expert-parallel export refuses
save_modelopt_state, since each rank holds only its own experts' ModelOpt state. Each behaviour fix has a test that fails without it; pre-commit, mypy included, and this PR's own unit tests pass at this head.Rebased on 2026-10-27 onto main 7c3c868, after [OMNIML-6064] Load each rank's checkpoint shard straight into a meta-built model #2739 merged: pre-commit, mypy included, and this PR's own unit tests pass at this head, and the series' PRs merged together add no unit-test failure over main (tests/unit/torch utils, export, quantization and models, on transformers 5.18 and 5.5.4 and on torch 2.8).
Review suggestions, 2026-10-27 evening: the FP8 integration and
core_model_loadingare imported apart, so a transformers without triton keeps the identity op that 5.5/5.6 reverse FP8 dequantization into.Review fix, 2026-10-27 night: an export's failure consensus carries any
BaseException, so aSystemExitorKeyboardInterrupton one rank fails every rank. The test'sSystemExitcase fails without the change.Re-cut on 2026-10-29 (see the note at the top), onto [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752's new head: conflicts only in test imports and in
test_distributed_run.py, which main deleted, so theexpert_parallel_statetest moved totest_distributed.py. The writer's refusal of expert-parallel experts without aParallelStatesays to load the model with an expert-parallel loader instead of naming one. Pre-commit, mypy included, passes against [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752 and against main 6c475d7. On CPU (torch 2.14, transformers 5.18), the export unit tests,test_dataset_utils.py,test_distributed.pyand hf_ptq's argument tests pass (801;test_export_diffusers.pyleft out: its 7 failures here are main's too). They also pass on transformers 5.5.4. GPU tests not re-run; CI runs them.Rebased on 2026-10-29 night onto [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752's review fixes. The expert-parallel scaffolding [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752 dropped comes in with the commit that sets it. The expert count now comes from
modelopt.torch.utils.moe._num_experts_from_config(it also readsnum_routed_expertsandllm_config), anddistribution_plan/expert_parallel_stateare imported at module scope._write_base_configis now a wrapper: it calls [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752'ssave_non_weight_artifactsinside the swap that keeps the run's DistributedConfig and torch_a2a plan out of config.json. The final copy of the source's files is skipped for the no-gather writer, which has made it already. The tests are unchanged, apart from a stub the config test no longer needs. On CPU, the export unit tests,test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (848) and 5.5.4 (813 passed, 35 skipped); without transformers, the unit tests pass or skip (450 passed, 99 skipped). Pre-commit, mypy included, passes against [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752 and against main. GPU tests not run; CI runs them.Review fixes, 2026-10-30, after Claude's review. This PR merges [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752's new head and restores the local split's expert-parallel parameters with their numbering test. The writer checks the routed experts' groups against the plan's
ep_sizeandedp, refusing on every rank alike. A data-parallel group left at its default, the whole world, used to leave every rank but rank 0 out of the expert write with no error; a two-rank test fails without the check. A sharded export reads the carried weights inside one all-reduce, so rank 0's failure there fails every rank, and every rank seeds the quant config with rank 0's names. A two-rank test fails without that too: rank 1 errors out of a broken collective. A codec converter is skipped as a decode in place only when all its patterns name one module; tested, the bare FP8 patterns included. On CPU, the export unit tests,test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (853) and 5.5.4 (818 passed, 35 skipped), and without transformers (451 passed, 102 skipped), whereimport modelopt.torch.exportdoes not import the writer. Pre-commit, mypy included, passes against [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752 and against main. GPU tests not run; CI runs them.Rebased onto main 99c3834, 2026-10-30, after [OMNIML-6081] [2/2] Fold fused experts' quantizers into their weights shard-locally #2750 merged, on [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752's rebased head. This PR's own diff is unchanged, plus one commit taken from Claude's last review: rank 0 alone reads the carried weights' names and sends them with
broadcast_from, which raises on every rank when that read fails, as the all-reduce did; and the traceback print before the closing consensus runs only for a sharded export, where the other ranks wait in it. [OMNIML-6081] [2/2] Fold fused experts' quantizers into their weights shard-locally #2750's final check that the first projection anddown_projhold the same number of experts holds for the local split, whose two shards have equal counts under FSDP2 and under expert parallelism. On CPU, the export unit tests,test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (854) and 5.5.4 (819 passed, 35 skipped), and without transformers the unit tests pass or skip (451 passed, 102 skipped), whereimport modelopt.torch.exportdoes not import the writer. Pre-commit, mypy included, passes against [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752 and against main. GPU tests not run; CI runs them.Rebased onto main 62a5c48, 2026-10-30, after [OMNIML-6082] [3/3] Write a sharded model's HF checkpoint shards from each tensor's owner rank #2751 merged, on [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752's rebased head, without conflicts; this PR's own diff is unchanged. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,
test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (856) and 5.5.4 (821 passed, 35 skipped), and without transformers the unit tests pass or skip (451 passed, 104 skipped), whereimport modelopt.torch.exportdoes not import the writer. GPU tests not run; CI runs them.Review fix, 2026-10-30 (meenchen's review bot):
PretrainedConfigmoves into the module's guarded transformers import at the top, and_source_distributed_config_attrschecks_TRANSFORMERS_CONFIG_TYPES, empty without transformers, as_TRANSFORMERS_MODEL_TYPESis. No behaviour change:test_distributed_export_writes_the_config_a_single_gpu_export_writes, whose plan sits in a VLM's nestedtext_config, passes, and fails with the config types emptied.test_unified_export_hf.pypasses on transformers 5.18 and 5.5.4 (73) and without transformers (61 passed, 12 skipped), where the module still imports. Pre-commit, mypy included, passes against main. GPU tests not run; CI runs them.GPU run, 2026-10-30: a real-model expert-parallel PTQ and export through [OMNIML-6085] [3/3] Run expert-parallel PTQ from hf_ptq.py #2754's expert-parallel path at
30551fa2df0, which merges this PR atc8ecddb3342. In the writer's files, this head differs from that one only by thePretrainedConfigimport move above. Qwen/Qwen3-30B-A3B, recipegeneral/ptq/nvfp4_default-kv_fp8, 512 calibration samples from cnn_dailymail at sequence length 2048, batch size 1; GB200, transformers 5.13.0. The two arms take both branches of the writer's group check: with edp = 1 the routed experts' data-parallel group is unset, and with edp = 2 it is the edp group of two ranks. Both exported on every rank, with NVFP4 weights and an FP8 KV cache. Theirconfig.jsoncarries nobase_model_ep_plan, as the source's has none, so no torch_a2a styles ship.Rebased onto main 059588b, 2026-10-30, on [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752's rebased head, without conflicts; this PR's own diff is unchanged. The GPU run above was at an earlier head with the same own diff. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,
test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (857) and 5.5.4 (822 passed, 35 skipped), and without transformers the unit tests pass or skip (451 passed, 104 skipped), whereimport modelopt.torch.exportdoes not import the writer. GPU tests not run here; CI defers them while this PR targets another PR's branch.Rebased onto main d464d30, 2026-10-30, on [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752's rebased head, without conflicts; this PR's own diff is unchanged, and the GPU run above, at an earlier head, had the same own diff. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,
test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (872) and 5.5.4 (837 passed, 35 skipped), and without transformers the unit tests pass or skip (466 passed, 104 skipped), whereimport modelopt.torch.exportdoes not import the writer. GPU tests not run here; CI defers them while this PR targets another PR's branch.Text fixes, 2026-10-31, comments and a docstring only: [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752 reworded the export comment this PR replaces, so this PR was restacked on it, keeping its own block; and
_process_hf_model_for_export's docstring and the call-site comment no longer name a consolidation step. Pre-commit, mypy included, passes against main, and on CPU the export unit tests,test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (872).Rebased onto main e352cac, 2026-10-31, on [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752's rebased head, without conflicts; this PR's own diff is unchanged, and the GPU run above, at an earlier head, had the same own diff. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,
test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (872) and 5.5.4 (837 passed, 35 skipped), and without transformers the unit tests pass or skip (466 passed, 104 skipped), whereimport modelopt.torch.exportdoes not import the writer. GPU tests not run here; CI runs them once this PR targets main.Review fix, 2026-10-31 (meenchen's review bot), comment only: the comment above
_writes_extranow gives only the single-sender reason. Both sharded writers takeextra_state_dictfrom rank 0 alone, so rank 0 alone reads and hands over the carried tensors (on GLM-5.3-Flash, its MTP layer). The gate covers the tensors, not their names, which reach every rank. Pre-commit, mypy included, passes against main.Rebased onto main cf38ec3, 2026-11-01, on [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752's rebased head, without conflicts; this PR's own diff is unchanged, and the GPU run above, at an earlier head, had the same code. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,
test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (872) and 5.5.4 (837 passed, 35 skipped), and without transformers the unit tests pass or skip (466 passed, 104 skipped), whereimport modelopt.torch.exportdoes not import the writer. GPU tests not run here; CI runs them once this PR targets main.Rebased onto main 51645db, 2026-11-01, on [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752's rebased head; this PR's own diff is unchanged. One conflict, in
test_distributed.py's imports, kept both sides' imports. The GPU run above, at an earlier head, had the same code. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (869) and 5.5.4 (834 passed, 35 skipped), and without transformers the unit tests pass or skip (463 passed, 104 skipped), whereimport modelopt.torch.exportdoes not import the writer. GPU tests not run here; CI runs them once this PR targets main.Rebased onto main 2d4e8d0, 2026-11-02, on [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752's rebased head, without conflicts; this PR's own diff is unchanged. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,
test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (869) and 5.5.4 (834 passed, 35 skipped), and without transformers the unit tests pass or skip (463 passed, 104 skipped), whereimport modelopt.torch.exportdoes not import the writer. GPU tests not run here; CI runs them once this PR targets main.Rebased onto main 653ef61, 2026-11-02, on [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752's rebased head, now that [OMNIML-6083] Write a sharded model's HF checkpoint without gathering it, and export an expert-parallel model through it #2752 targets main; without conflicts, and this PR's own diff is unchanged. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,
test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (869) and 5.5.4 (834 passed, 35 skipped), and without transformers the unit tests pass or skip (463 passed, 104 skipped), whereimport modelopt.torch.exportdoes not import the writer. GPU tests not run here; CI runs them once this PR targets main.Folded [OMNIML-6084] Export an expert-parallel model through the no-gather writer #2753 in, 2026-11-03: its 38 commits are cherry-picked onto this PR's head
792f22b1276, unchanged (range-diff all "="), so this PR's own commits keep their SHAs. The combined diff is this PR's previous diff plus [OMNIML-6084] Export an expert-parallel model through the no-gather writer #2753's own, and the tree equals [OMNIML-6084] Export an expert-parallel model through the no-gather writer #2753's last head plus this PR's text fix. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (869) and 5.5.4 (834 passed, 35 skipped), and without transformers the unit tests pass or skip (463 passed, 104 skipped). The GPU suites were also run on the combined head0c4f7f0d633on 2 and 4 GPUs (transformers 5.18.0), and every test has the same outcome as at792f22b1276, before the fold.tests/gpu/torch/export/test_distributed_hf_export.pytests/gpu/torch/export/test_export_carry_over.pytests/gpu/torch/utils/test_model_load_utils.pyReview fix, 2026-11-03 (Claude's review of the folded PR): an expert-parallel export of a model that any rank holds accelerate-offloaded is refused before anything is folded or written. It used to take the offloaded streaming path, which is not expert-parallel-aware, so every rank wrote its own experts over the others' with no error. The ranks decide together in one
all_reduce, since offload is set up per rank.test_an_offloaded_expert_parallel_export_is_refused_before_foldingfails without the refusal. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (870) and 5.5.4 (835 passed, 35 skipped), and without transformers the unit tests pass or skip (464 passed, 104 skipped). The GPU suites were rerun at1cd94b8ca9don 2 and 4 GPUs (transformers 5.18.0), with every test's outcome the same as at0c4f7f0d633. None of them offloads, so the refusal never fires there; the runs show that the guard and its collective leave the export's path and its collective order as they were.tests/gpu/torch/export/test_distributed_hf_export.pytests/gpu/torch/export/test_export_carry_over.pytests/gpu/torch/utils/test_model_load_utils.pyReview fix, 2026-11-03 (Claude's next review), test only:
test_only_the_first_expert_replica_writes_the_expertspins the writer's replica skip under expert parallelism with replicated experts. Two gloo ranks play the four ranks of an (edp=2, ep=2) plan, one replica per export. The second replica's ranks write no expert but still write the dense tensors they own, and the first replica writes each of the four experts once. Making every replica write, or none, fails it. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (871) and 5.5.4 (836 passed, 35 skipped), and without transformers the unit tests pass or skip (464 passed, 104 skipped).Rebased onto main 8d19f74, 2026-11-03, with a code owner's review fix on top. The rebase's only conflicts were changelog context at the top of Backward Breaking Changes, where main's new entry and this PR's are both kept; this PR's own diff is otherwise unchanged. The review fix, test only:
test_only_rank_0_writes_the_configs_of_an_expert_parallel_exportchecks on two ranks that only rank 0 calls_write_base_configand_write_hf_export_configunder an expert-parallel plan, and it fails with the rank condition reverted to cover FSDP2 alone. Pre-commit, mypy included, passes against main. On CPU, the export unit tests,test_dataset_utils.py,test_distributed.py,test_moe_a2a.pyand hf_ptq's argument tests pass on transformers 5.18 (876) and 5.5.4 (841 passed, 35 skipped), and without transformers the unit tests pass or skip (469 passed, 104 skipped).Rebased onto main 7d44246, 2026-11-03 (after [OMNIML-6060] Load an expert-parallel plan through load_pretrained, and apply the model spec when deciding and loading it #2780, Make the megatron tests run on 4-GPU nodes #3177 and Test Nemotron-H and Qwen3.5-VL bridge export with full NVFP4 #3125 merged), without conflicts; this PR's own diff is unchanged. Pre-commit, mypy included, passes against main. On CPU, the export and utils unit tests pass on transformers 5.18 and 5.5.4 and without transformers; hf_ptq's example tests fail the same 53 tests here as on main, all of which need a GPU.
Review fix, 2026-11-03 (meenchen's review): the no-gather export checks the destination on every rank, in one
all_reduce, before it folds the model in place, so a directory that already holds a checkpoint is refused with the model left as it was; the writer keeps its own check for a checkpoint written in between.test_nogather_export_refuses_an_existing_checkpoint_before_foldingrefuses an existing checkpoint with every weight unchanged, then exports the same model into a fresh directory byte-identical to the single-process export; it fails without the early check. Pre-commit, mypy included, passes against main. On CPU, the export and utils unit tests and hf_ptq's argument tests pass on transformers 5.18 (1548) and 5.5.4 (1448 passed, 100 skipped), and without transformers the unit tests pass or skip (827 passed, 111 skipped). The multi-GPU runs above predate this change.Rebased onto main 3968c37, 2026-11-03 (after [NVBug 7035880] Apply the calibrated bias in NVFP4 affine KV fake quantization #3179, [NVBug 6938145] Keep HF_TOKEN off argv and use a private temp dir in the MRCR gym recipe #3174, [OMNIML-6063] [2/2] Decompress compressed-tensors MoE experts as they load, expert-parallel loads included #3137, [OMNIML-6061] Load the dense part sharded, and size the load warmup to each rank #2742, [3/3] Add GLM-5.3-Flash support to Megatron-Bridge PTQ and HF export #2539, [OMNIML-6060] [3/3] Refuse an expert-parallel load that would calibrate the experts wrong #2741, Sync Megatron-Core TELinear quantizer ranges over Megatron's parallel groups #3199, [OMNIML-6062] Dequantize block-FP8 checkpoints as they load expert-parallel #2743 and Support text only calibration of vision-language models in the FSDP2 PTQ flow #3127 merged). The only conflicts were import lines: Support text only calibration of vision-language models in the FSDP2 PTQ flow #3127's
fsdp2_root_unshardedimport beside this PR's inunified_export_hf.py(two commits) andhf_ptq.py(one), each keeping both. This PR's own diff is otherwise unchanged. Support text only calibration of vision-language models in the FSDP2 PTQ flow #3127's two behaviour changes inunified_export_hf.pyhold under the no-gather path: its quant-config name revert passes the parameters' owners, which the no-gather path asks for on rank 0 only, after a successful reverse conversion; and its Nemotron VL dummy forward unshards an FSDP2 root, which every rank reaches alike inside_process_together.Review fix, 2026-11-03 (meenchen's review), test only:
test_moe_a2a.py's expert-parallel calibration test, nowtest_expert_parallel_calibration_and_export_match_the_whole_model, loads with the production loader (load_pretrainedwith an expert-parallel plan), calibrates FP8 and exports with the no-gather writer. It compares the checkpoint with the whole model's single-process export: each tensor in exactly one shard, the one the index names for it; every expert of every layer; every tensor byte-identical, except the activation scales, within 2e-2.[ep-world]shards the experts over every rank, and[ep-half]over half of them with two replicas. CI's PR lane has 1 GPU and skips both; nightly and on-demand runs have 2 GPUs and run[ep-world]; no CI runner has 4. At the head ff4f4cb, with transformers 5.18.0:[ep-world][ep-half]With the writer's replica skip disabled,
[ep-half]fails on 4 GPUs of the second type, refused on every rank by the numbering check below;[ep-world]still passes.Review fixes, 2026-11-03 (Fridah-nv's review):
all_reduce, which refuses on every rank, before anything is written, unless the ranks between them number each fused-experts module's experts 0..E-1 once each (E being the config's count under expert parallelism). A count the config does not name, with a weight over every expert, used to shift every later rank's experts past the end with no error. A plain per-expert scale for neither this rank's experts nor every expert of the weight is refused rather than sliced.test_the_ranks_write_every_expert_number_once[count-known|count-unknown]andtest_split_refuses_scales_for_another_number_of_expertseach fail without the fix.RuntimeErrorinstead of failing an assert, whichpython -Odrops.test_packing_an_fsdp2_model_outside_the_no_gather_writer_is_refusedfails with the assert back.load_pretrainedor recordsDistributionPlan(world_size). Every in-repo caller of the export records a plan or exports from a single process.Pre-commit, mypy included, passes against main at ff4f4cb. On CPU, the export and utils unit tests pass on transformers 5.18 (1616) and 5.5.4 (1500 passed, 116 skipped), and without transformers they pass or skip (827 passed, 112 skipped). hf_ptq's example tests fail the same 53 tests here as on main, all of which need a GPU.
Comment only, 2026-11-04: the writer says why every rank joins the expert split's
all_reduce, a rank that writes no experts included. Pre-commit, mypy included, passes against main.Review fixes, 2026-11-04 (sugunav14's review): the experts' input scales are stacked inside the labeled
all_reducethat agrees on their keys, so a rank that cannot stack them fails every rank instead of leaving the others in the raw reduce; the export's closing docstring scopes its every-rank-fails guarantee to failures outside raw collectives, naming the dense tensors' point-to-point transfers as the case it does not cover. Under an expert-parallel plan, a plain tensor under a fused-experts module other than its experts' weights and scales (a fused bias, say), which each rank holds only its own slice of, is refused on every rank before anything is written; no model the expert-parallel load supports has one.test_two_ranks_write_every_expert_with_the_largest_input_scale(a rank with a two-value scale, its reduce bounded to 10 s) andtest_an_expert_parallel_export_refuses_a_sliced_tensor_beside_the_expertseach fail without their fix. Pre-commit, mypy included, passes against main. On CPU, the export and utils unit tests pass on transformers 5.18 and 5.5.4 and without transformers; hf_ptq's example tests fail the same 53 tests here as on main, all of which need a GPU.Before your PR is "Ready for review"
fsdp2_wrapincluded, must record its plan withset_distribution_planbeforeexport_hf_checkpoint; without it the export raises.hf_ptq.py --use_fsdp2on several GPUs now refuses calibration that keeps its statistics on each rank, and raises on a calibration batch that runs out of GPU memory instead of retrying it smaller.CONTRIBUTING.md: N/Ahf_ptq.py --use_fsdp2on several GPUs now refuses.Additional Information
Part of expert-parallel PTQ for Hugging Face MoE models (OMNIML-6083; #2753's OMNIML-6084 folded in); #2751, #2750 and #2740, which it built on, have merged.
Summary by CodeRabbit
MODELOPT_FSDP2_EXPORT=nogather, ranks write checkpoint shards in parallel without gathering the full model on one host; MoE experts remain sharded.