Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: add identity reverse_op to dequantize ops for save_pretrained#44983

Merged
SunMarc merged 1 commit into
huggingface:mainfrom
Hyungkeun-Park-Nota:fix/mxfp4-dequantize-reverse-op
Mar 27, 2026
Merged

fix: add identity reverse_op to dequantize ops for save_pretrained#44983
SunMarc merged 1 commit into
huggingface:mainfrom
Hyungkeun-Park-Nota:fix/mxfp4-dequantize-reverse-op

Conversation

@Hyungkeun-Park-Nota

@Hyungkeun-Park-Nota Hyungkeun-Park-Nota commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes save_pretrained() for models loaded with dequantize=True.

save_pretrained calls reverse_op on all weight conversion operations from loading. Dequantize ops (Mxfp4Dequantize, Fp8Dequantize, MetalDequantize) don't have reverse_op implemented, so it raises NotImplementedError.

Since dequantized weights are already in their target dtype, the reverse op should just pass them through. Added _IdentityOp in core_model_loading.py and set it as reverse_op on all three dequantize operations.

Tested with

  • MXFP4: openai/gpt-oss-20b + Mxfp4Config(dequantize=True)
  • FP8: Qwen/Qwen3-0.6B-FP8 + FineGrainedFP8Config(dequantize=True)
  • Metal: medmekk/Llama-3.2-1B-Instruct-metal + MetalConfig(dequantize=True)

All three: save ✓, no quantization_config in saved config.json ✓, reload ✓ (0 meta params)

@Hyungkeun-Park-Nota Hyungkeun-Park-Nota force-pushed the fix/mxfp4-dequantize-reverse-op branch from 1742755 to 9c59dda Compare March 25, 2026 01:24
@Rocketknight1

Copy link
Copy Markdown
Member

cc @SunMarc for quantization maybe?

@SunMarc

SunMarc commented Mar 25, 2026

Copy link
Copy Markdown
Member

model = AutoModelForCausalLM.from_pretrained(
"openai/gpt-oss-20b",
quantization_config=Mxfp4Config(dequantize=True),
)
model.save_pretrained("/tmp/test") # NotImplementedError before this fix

hmmm, this shouldn't trigger a reverse ops when we dequantized the model. I think the right behavior here would be to just save the model in its dequantized form.

@Hyungkeun-Park-Nota Hyungkeun-Park-Nota force-pushed the fix/mxfp4-dequantize-reverse-op branch from 9c59dda to b676da0 Compare March 26, 2026 01:34
@Hyungkeun-Park-Nota

Hyungkeun-Park-Nota commented Mar 26, 2026

Copy link
Copy Markdown
Contributor Author

@SunMarc Thanks for the review! Updated the PR based on your feedback:

  1. Removed re-quantization logic — replaced Mxfp4ReverseDequantize with Mxfp4IdentityOp that simply passes through bf16 weights as-is during save
  2. Remove quantization_config after dequantize — in _process_model_after_weight_loading, when dequantize=True, we delete model.config.quantization_config so the saved model loads as a regular bf16 model without triggering the MXFP4 loading path

@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: mxfp4

@Hyungkeun-Park-Nota Hyungkeun-Park-Nota force-pushed the fix/mxfp4-dequantize-reverse-op branch from cd2c8fe to 13f9355 Compare March 26, 2026 02:11

@SunMarc SunMarc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

left a few question, thanks for iterating

Comment on lines +177 to +178
if self.quantization_config.dequantize and hasattr(model.config, "quantization_config"):
del model.config.quantization_config

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

when calling dequantize, i think there is a function that is triggered to remove all traces of quantization no ? maybe we don't need to do this

@Hyungkeun-Park-Nota Hyungkeun-Park-Nota Mar 26, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right. remove_quantization_config in base.py already handles this during postprocess_model. Removed the mxfp4-specific deletion.

Comment thread src/transformers/integrations/mxfp4.py Outdated
Comment on lines +148 to +150
@property
def reverse_op(self) -> ConversionOps:
return Mxfp4IdentityOp()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

even after dequantizing, do we still need the reverse ops ? Can you check the behavior with fp8 when dequantize is called also ?

@Hyungkeun-Park-Nota Hyungkeun-Park-Nota Mar 26, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

same issue exists. Tested with Qwen/Qwen3-0.6B-FP8 using FineGrainedFP8Config(dequantize=True): save_pretrained also raises NotImplementedError because _weight_conversions remains after remove_quantization_config.

So the fix is now in base.py, remove_quantization_config clears _weight_conversions alongside hf_quantizer and quantization_config. This makes the mxfp4-specific reverse_op and config removal unnecessary, so I've removed them. The fix covers both mxfp4 and fp8.

@Hyungkeun-Park-Nota Hyungkeun-Park-Nota force-pushed the fix/mxfp4-dequantize-reverse-op branch from 13f9355 to ae3e643 Compare March 26, 2026 15:52
@Hyungkeun-Park-Nota

Hyungkeun-Park-Nota commented Mar 26, 2026

Copy link
Copy Markdown
Contributor Author

Updated based on review feedback. The fix is now a 2-line change: clearing _weight_conversions in remove_quantization_config.

Verified with both affected quantizers:

MXFP4 (openai/gpt-oss-20b, Mxfp4Config(dequantize=True)):

  • save_pretrained
  • quantization_config absent from saved config.json
  • Reload (0 meta-device params)

FP8 (Qwen/Qwen3-0.6B-FP8, FineGrainedFP8Config(dequantize=True)):

  • save_pretrained (was also failing with NotImplementedError before this fix)
  • quantization_config absent from saved config.json
  • Reload (0 meta-device params)

@Hyungkeun-Park-Nota Hyungkeun-Park-Nota changed the title fix: implement Mxfp4Dequantize.reverse_op for save_pretrained support fix: clear _weight_conversions in remove_quantization_config for save_pretrained support Mar 26, 2026
@Hyungkeun-Park-Nota Hyungkeun-Park-Nota force-pushed the fix/mxfp4-dequantize-reverse-op branch from ae3e643 to 9553783 Compare March 26, 2026 16:02
Comment thread src/transformers/quantizers/base.py Outdated
Comment on lines +206 to +207
if hasattr(model, "_weight_conversions"):
del model._weight_conversions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

hmmm but still we shouldn't just remove all weight_conversions. not all weight_conversions are related to quantization. Can we find a way to remove only the weight conversion that makes sense ? otherwise, we can just update the weight conversion ops if it is simpler

@Hyungkeun-Park-Nota Hyungkeun-Park-Nota Mar 26, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Didn't realize not all weight_conversions are quantization-related thanks for catching that.

Between the two options, adding identity reverse_op on each dequantize op seemed simpler, so went with that. Added _IdentityOp to core_model_loading.py and set it as reverse_op on Mxfp4Dequantize, Fp8Dequantize, and MetalDequantize (all three had the same issue).

Tested save + reload on:

  • MXFP4: openai/gpt-oss-20b
  • FP8: Qwen/Qwen3-0.6B-FP8
  • Metal: medmekk/Llama-3.2-1B-Instruct-metal

@Hyungkeun-Park-Nota Hyungkeun-Park-Nota force-pushed the fix/mxfp4-dequantize-reverse-op branch 2 times, most recently from de90e5b to ece5d90 Compare March 26, 2026 16:45
@Hyungkeun-Park-Nota Hyungkeun-Park-Nota changed the title fix: clear _weight_conversions in remove_quantization_config for save_pretrained support fix: add identity reverse_op to dequantize ops for save_pretrained Mar 26, 2026
@Hyungkeun-Park-Nota Hyungkeun-Park-Nota force-pushed the fix/mxfp4-dequantize-reverse-op branch from ece5d90 to 61347c0 Compare March 26, 2026 17:02

@SunMarc SunMarc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

…ined

Dequantize operations (Mxfp4Dequantize, Fp8Dequantize, MetalDequantize)
raise NotImplementedError on reverse_op, causing save_pretrained to fail
for models loaded with dequantize=True.

Add _IdentityOp as the reverse_op so dequantized weights are saved as-is.
@Hyungkeun-Park-Nota Hyungkeun-Park-Nota force-pushed the fix/mxfp4-dequantize-reverse-op branch from 0d756c1 to 4212234 Compare March 27, 2026 14:36
@SunMarc SunMarc enabled auto-merge March 27, 2026 17:00
@SunMarc SunMarc added this pull request to the merge queue Mar 27, 2026
Merged via the queue into huggingface:main with commit ecdf95c Mar 27, 2026
29 checks passed
NielsRogge pushed a commit to NielsRogge/transformers that referenced this pull request Mar 30, 2026
…uggingface#44983)

fix: add identity reverse_op to dequantize operations for save_pretrained

Dequantize operations (Mxfp4Dequantize, Fp8Dequantize, MetalDequantize)
raise NotImplementedError on reverse_op, causing save_pretrained to fail
for models loaded with dequantize=True.

Add _IdentityOp as the reverse_op so dequantized weights are saved as-is.
3outeille added a commit that referenced this pull request Apr 14, 2026
The _IdentityOp class (added by PR #44983) was accidentally deleted
during the MoE expert parallelism work. It is needed by
finegrained_fp8.py and metal_quantization.py as a pass-through
reverse_op for dequantize operations.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
3outeille added a commit that referenced this pull request Apr 14, 2026
* MoE expert parallelism + sequence parallelism

- Add PackedColwiseParallel for fused gate_up_proj weights
- Add MoEExpertsParallel with per-expert DTensor sharding
- Add PrepareModuleInputOutput for SP allgather/split hooks
- Add _AllReduceBackward for MoE routing weight gradients
- Extend TPStyle with moe_experts, packed_colwise, activation, module kinds
- _StridedShard handling in core_model_loading for interleaved weights
- MoE model configs: mixtral, deepseek_v3, qwen3 with SP plans
- DTensor rotary_pos_emb guard for mixtral

* Fix ruff linting and formatting

* Fix ruff formatting in core_model_loading.py

* Restore _IdentityOp accidentally removed in 25a1f48

The _IdentityOp class (added by PR #44983) was accidentally deleted
during the MoE expert parallelism work. It is needed by
finegrained_fp8.py and metal_quantization.py as a pass-through
reverse_op for dequantize operations.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* Backport new TP/FSDP API + fix DTensor imports in Copied-from models

* from_pretrained orchestration + distributed save/load (#45409)

* from_pretrained orchestration + save/load

- Add gather_full_state_dict() for DTensor→full tensor saving
- Add convert_strided_to_shard() / restore_strided_from_shard() for DCP
- Add _redistribute_dtensor() helper
- Full distributed_config integration in from_pretrained/save_pretrained
- Rename apply_fsdp2 → apply_fully_shard_data_parallel
- save_optimizer() / load_optimizer() in distributed/utils
- Trainer integration with distributed_config
- Updated FSDP and TP tests for new orchestration API
- DTensor shard-on-read test updates

* revert distributed utils

* eaaea

* all tests for core modeling are passing

* populate import from init for tp

* ruff

* ruff

---------

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
vasqu pushed a commit to zhang-prog/transformers that referenced this pull request May 19, 2026
* init

* FSDP2 (fully_shard) integration

- Add apply_fully_shard_data_parallel() with auto/manual mode block detection
- FSDP vs DDP loss/grad parity tests
- Distributed test helpers (testing_utils.py)
- is_fsdp_enabled(), is_fsdp_managed_module() utilities
- Minimal FSDP hooks in from_pretrained
- FSDP-aware flash attention check

* DistributedConfig + shard-on-read loading

- DtensorShardOperation for range-math shard-on-read
- spawn_materialize() enhancements
- from_pretrained wiring for distributed config
- Shard operation helpers in tensor_parallel
- Shard-on-read and LoadStateDictConfig tests

* TPStyle API + dense model tensor parallelism

- Replace hook-based TP with DTensor-based TPStyle API
- TPStyle dataclass with dense kinds: colwise, rowwise, vocab
- apply_tensor_parallel() using PyTorch parallelize_module
- verify_tp_plan() for plan validation
- Update dense model configs (llama, mistral, qwen2, phi, glm) to TPStyle
- DTensor apply_rotary_pos_emb guard for llama, mistral, qwen3
- Extended DistributedConfig with tp/fsdp size and plan fields
- DistributedConfig serialization in configuration_utils
- MXFP4 NotImplementedError for DTensor TP
- Dense TP tests

* revert some files

* Add distributed training scripts

- train_fsdp_tp.py: minimal FSDP+TP training example
- train_fsdp_tp_torchtitan_style.py: torchtitan-style training example
- verify_loading.py: save/load roundtrip verification
- run_compare.sh: FSDP+TP vs FSDP-only comparison
- run_verify_all.sh: run verification across all modes
- tmp_generate.py: quick generation test

* Remove train_fsdp_tp_torchtitan_style.py

* unify the utils for fsdp

* Fix CI: re-export moved FSDP utils + remove stale type: ignore

- Re-export is_fsdp_enabled and is_fsdp_managed_module from
  integrations/fsdp.py (moved to distributed/utils.py)
- Remove unused # type: ignore comments in generation/utils.py

* Fix ruff formatting in core_model_loading.py

* Fix ruff linting and formatting

* Backport new TP/FSDP API from orchestration-save-load branch

* Fix DTensor imports in Copied-from model files

* MoE expert parallelism + sequence parallelism (huggingface#45408)

* MoE expert parallelism + sequence parallelism

- Add PackedColwiseParallel for fused gate_up_proj weights
- Add MoEExpertsParallel with per-expert DTensor sharding
- Add PrepareModuleInputOutput for SP allgather/split hooks
- Add _AllReduceBackward for MoE routing weight gradients
- Extend TPStyle with moe_experts, packed_colwise, activation, module kinds
- _StridedShard handling in core_model_loading for interleaved weights
- MoE model configs: mixtral, deepseek_v3, qwen3 with SP plans
- DTensor rotary_pos_emb guard for mixtral

* Fix ruff linting and formatting

* Fix ruff formatting in core_model_loading.py

* Restore _IdentityOp accidentally removed in 25a1f48

The _IdentityOp class (added by PR huggingface#44983) was accidentally deleted
during the MoE expert parallelism work. It is needed by
finegrained_fp8.py and metal_quantization.py as a pass-through
reverse_op for dequantize operations.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* Backport new TP/FSDP API + fix DTensor imports in Copied-from models

* from_pretrained orchestration + distributed save/load (huggingface#45409)

* from_pretrained orchestration + save/load

- Add gather_full_state_dict() for DTensor→full tensor saving
- Add convert_strided_to_shard() / restore_strided_from_shard() for DCP
- Add _redistribute_dtensor() helper
- Full distributed_config integration in from_pretrained/save_pretrained
- Rename apply_fsdp2 → apply_fully_shard_data_parallel
- save_optimizer() / load_optimizer() in distributed/utils
- Trainer integration with distributed_config
- Updated FSDP and TP tests for new orchestration API
- DTensor shard-on-read test updates

* revert distributed utils

* eaaea

* all tests for core modeling are passing

* populate import from init for tp

* ruff

* ruff

---------

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>

* do monkey patching for rotary

* Revert modeling file diffs to match fsdp-core-model-loading base

Restores modeling files to their base branch versions so the PR diff
only shows the distributed/patches.py monkey-patch approach instead of
noisy function moves in modeling files.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* Migrate all model TP plans from strings to TPStyle

- Convert string plan values ("colwise", "rowwise", etc.) to TPStyle
  objects across 66+ model configs and modular files
- Consolidate MoE expert sub-entries into TPStyle("moe_experts", ...)
  with shard_plan
- Remove "replicated_with_grad_allreduce" entries (not needed for
  DTensor TP)
- Migrate _tp_plan class attributes in modeling files from
  "colwise_gather_output" to TPStyle("colwise", "allgather")
- Add TypeError in apply_tensor_parallel for unsupported plan values
- Remove old TensorParallelLayer tests (API removed in DTensor refactor)
- Regenerate auto-generated files via modular converter

* Restore mxfp4.py to match base branch

* Drop mla_kv_a_proj and moe_identity_expert from TP plans

These string plan values have no TPStyle equivalent in the DTensor
system. Remove them to avoid TypeError at apply_tensor_parallel time.
Affected models: deepseek_v2, glm4_moe_lite, glm_moe_dsa, longcat_flash.

* more comments

* fix tp for most models.  PyTorch doesn't implement all placement conversions (e.g. _StridedShard↔Shard). We force replicate beforehand

* fix tp through _replicate_dtensor

* revert small change

* push temporary fix for TP and strided shard for backward

* refactor a bit

* patches for rotary

* refactor MoEExpertsParallel

* fix tp for last models

* refactor moe expert parallels

* linting

* add sp plan for models

* add deepseek v2 sp plan

* undo sp plan for some tricky models

* remove lm_head from  config

* first pass of refactoring dtensor shard operator

* better refacto

* batter explanation of DtensorShardOperation

* refactor dtensor test to reflect real world scenario

* more comments

* fix tp olmo hybrid and exaone

* Enhance tensor parallel weight tying logic to prevent clobbering of lm_head when embed_tokens is not in the plan.

* fix fsdp mixin test due to missing args

* fix test non model

* skip sp plan for exaone and olmo hybrid

* linting

* fix import for ci

* test distributed config

* attempt to fix guarding import ci

* fix ci check repro

* add ALL_PARALLEL_STYLES registry alongside TPStyle

* route apply_tensor_parallel through ALL_PARALLEL_STYLES

* migrate modular files to string-based TP plans

* migrate standalone configs and modelings to string-based TP plans

* delete TPStyle dataclass

* fix use_local_output defaults for SequenceParallel and PrepareModuleInput in registry

* use parallel style from torch

* revert changes in weight converter

* remove dead code in set_param_for_module

* remove dead code

* cleaning again

* cleaning

* revert change

* linting

* refactor dtensor shard ops

* revert some stuff in core model loading

* core model loading clean

* guarding import

* better separation tensor parall and generic utils

* isolate DtensorShardOperation into a separate file

* no need to patch rotary

* better seperation

* simplify gather_full_state_dict

* simplify _replicate_dtensor

* fix and clean _replicate_dtensor

* better doc for DtensorShardOperation

* fix saving optimizer with DCP for fused weights

* save_pretrained(distributed_checkpoint=true)

* linting

* refactor into a single function _dtensor_from_local_like

* zeros_like instead of empty_like

* move tp and fsdp under distributed

* distribute_model

* fix deadlock when saving

* clip grad norm function

* maybe_disable_foreach_and_fused_for_mixed_dtensor_groups

* better TP api for ease of understanding

* remove shard_param to make it easier

* fix import in test

* _swap_dtensor_params_for_local

* fix qwen3 nanochat dots1

* add tpu

* move TP refactor experimentation scripts to backup branch

Move ad-hoc training / verification / compare scripts off this branch
into refactor-tp-dtensor-scripts so the diff stays focused on library
changes.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* linting

* register distributed sharding_utils and utils in __init__

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* rename TP plan styles to match new ALL_PARALLEL_STYLES registry

Replace pre-refactor names that no longer exist in
src/transformers/distributed/tensor_parallel.py:
  rowwise -> rowwise_allreduce
  moe_tp_experts -> moe_experts_allreduce
  replicated_with_grad_allreduce -> activation_seq_dim_2

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* enable EP

* Add enable_expert_parallel configuration option in test_distributed_config

* no more auto mode

* edit fsdp plan to every other models

* update fsdp mixin tests

* linting

* fix test fsdp

* fsdp linting

* revert gitignore

* _apply within for loop

* rename

* doc sp plan

* fix

* unified settattr + torch no grad + _local_tensor

* revert

* linting

* fix ruff

* make check-repository-consistency

* trigger fsdp mixin test in CI

* fix fsdp ci

* Reset tests/test_modeling_common.py to main

Restores legitimate improvements that were accidentally undone during a
stale merge of main into fsdp-vs-ddp:

- Restore test_resize_embeddings_untied_no_reinit_on_post_init
- Restore clipseg / Timm / evolla / parakeet_* / pi0 / musicflamingo
  special-cases
- Restore skip_base_model parameter on test_reverse_loading_mapping
- Restore "is not None" guard on subconfig in test_initialization
- Fix typo: "ot" -> "or" in test_reverse_loading_mapping assert message

---------

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
yuchenxie4645 pushed a commit to yuchenxie4645/transformers that referenced this pull request May 28, 2026
* init

* FSDP2 (fully_shard) integration

- Add apply_fully_shard_data_parallel() with auto/manual mode block detection
- FSDP vs DDP loss/grad parity tests
- Distributed test helpers (testing_utils.py)
- is_fsdp_enabled(), is_fsdp_managed_module() utilities
- Minimal FSDP hooks in from_pretrained
- FSDP-aware flash attention check

* DistributedConfig + shard-on-read loading

- DtensorShardOperation for range-math shard-on-read
- spawn_materialize() enhancements
- from_pretrained wiring for distributed config
- Shard operation helpers in tensor_parallel
- Shard-on-read and LoadStateDictConfig tests

* TPStyle API + dense model tensor parallelism

- Replace hook-based TP with DTensor-based TPStyle API
- TPStyle dataclass with dense kinds: colwise, rowwise, vocab
- apply_tensor_parallel() using PyTorch parallelize_module
- verify_tp_plan() for plan validation
- Update dense model configs (llama, mistral, qwen2, phi, glm) to TPStyle
- DTensor apply_rotary_pos_emb guard for llama, mistral, qwen3
- Extended DistributedConfig with tp/fsdp size and plan fields
- DistributedConfig serialization in configuration_utils
- MXFP4 NotImplementedError for DTensor TP
- Dense TP tests

* revert some files

* Add distributed training scripts

- train_fsdp_tp.py: minimal FSDP+TP training example
- train_fsdp_tp_torchtitan_style.py: torchtitan-style training example
- verify_loading.py: save/load roundtrip verification
- run_compare.sh: FSDP+TP vs FSDP-only comparison
- run_verify_all.sh: run verification across all modes
- tmp_generate.py: quick generation test

* Remove train_fsdp_tp_torchtitan_style.py

* unify the utils for fsdp

* Fix CI: re-export moved FSDP utils + remove stale type: ignore

- Re-export is_fsdp_enabled and is_fsdp_managed_module from
  integrations/fsdp.py (moved to distributed/utils.py)
- Remove unused # type: ignore comments in generation/utils.py

* Fix ruff formatting in core_model_loading.py

* Fix ruff linting and formatting

* Backport new TP/FSDP API from orchestration-save-load branch

* Fix DTensor imports in Copied-from model files

* MoE expert parallelism + sequence parallelism (huggingface#45408)

* MoE expert parallelism + sequence parallelism

- Add PackedColwiseParallel for fused gate_up_proj weights
- Add MoEExpertsParallel with per-expert DTensor sharding
- Add PrepareModuleInputOutput for SP allgather/split hooks
- Add _AllReduceBackward for MoE routing weight gradients
- Extend TPStyle with moe_experts, packed_colwise, activation, module kinds
- _StridedShard handling in core_model_loading for interleaved weights
- MoE model configs: mixtral, deepseek_v3, qwen3 with SP plans
- DTensor rotary_pos_emb guard for mixtral

* Fix ruff linting and formatting

* Fix ruff formatting in core_model_loading.py

* Restore _IdentityOp accidentally removed in 25a1f48

The _IdentityOp class (added by PR huggingface#44983) was accidentally deleted
during the MoE expert parallelism work. It is needed by
finegrained_fp8.py and metal_quantization.py as a pass-through
reverse_op for dequantize operations.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* Backport new TP/FSDP API + fix DTensor imports in Copied-from models

* from_pretrained orchestration + distributed save/load (huggingface#45409)

* from_pretrained orchestration + save/load

- Add gather_full_state_dict() for DTensor→full tensor saving
- Add convert_strided_to_shard() / restore_strided_from_shard() for DCP
- Add _redistribute_dtensor() helper
- Full distributed_config integration in from_pretrained/save_pretrained
- Rename apply_fsdp2 → apply_fully_shard_data_parallel
- save_optimizer() / load_optimizer() in distributed/utils
- Trainer integration with distributed_config
- Updated FSDP and TP tests for new orchestration API
- DTensor shard-on-read test updates

* revert distributed utils

* eaaea

* all tests for core modeling are passing

* populate import from init for tp

* ruff

* ruff

---------

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>

* do monkey patching for rotary

* Revert modeling file diffs to match fsdp-core-model-loading base

Restores modeling files to their base branch versions so the PR diff
only shows the distributed/patches.py monkey-patch approach instead of
noisy function moves in modeling files.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* Migrate all model TP plans from strings to TPStyle

- Convert string plan values ("colwise", "rowwise", etc.) to TPStyle
  objects across 66+ model configs and modular files
- Consolidate MoE expert sub-entries into TPStyle("moe_experts", ...)
  with shard_plan
- Remove "replicated_with_grad_allreduce" entries (not needed for
  DTensor TP)
- Migrate _tp_plan class attributes in modeling files from
  "colwise_gather_output" to TPStyle("colwise", "allgather")
- Add TypeError in apply_tensor_parallel for unsupported plan values
- Remove old TensorParallelLayer tests (API removed in DTensor refactor)
- Regenerate auto-generated files via modular converter

* Restore mxfp4.py to match base branch

* Drop mla_kv_a_proj and moe_identity_expert from TP plans

These string plan values have no TPStyle equivalent in the DTensor
system. Remove them to avoid TypeError at apply_tensor_parallel time.
Affected models: deepseek_v2, glm4_moe_lite, glm_moe_dsa, longcat_flash.

* more comments

* fix tp for most models.  PyTorch doesn't implement all placement conversions (e.g. _StridedShard↔Shard). We force replicate beforehand

* fix tp through _replicate_dtensor

* revert small change

* push temporary fix for TP and strided shard for backward

* refactor a bit

* patches for rotary

* refactor MoEExpertsParallel

* fix tp for last models

* refactor moe expert parallels

* linting

* add sp plan for models

* add deepseek v2 sp plan

* undo sp plan for some tricky models

* remove lm_head from  config

* first pass of refactoring dtensor shard operator

* better refacto

* batter explanation of DtensorShardOperation

* refactor dtensor test to reflect real world scenario

* more comments

* fix tp olmo hybrid and exaone

* Enhance tensor parallel weight tying logic to prevent clobbering of lm_head when embed_tokens is not in the plan.

* fix fsdp mixin test due to missing args

* fix test non model

* skip sp plan for exaone and olmo hybrid

* linting

* fix import for ci

* test distributed config

* attempt to fix guarding import ci

* fix ci check repro

* add ALL_PARALLEL_STYLES registry alongside TPStyle

* route apply_tensor_parallel through ALL_PARALLEL_STYLES

* migrate modular files to string-based TP plans

* migrate standalone configs and modelings to string-based TP plans

* delete TPStyle dataclass

* fix use_local_output defaults for SequenceParallel and PrepareModuleInput in registry

* use parallel style from torch

* revert changes in weight converter

* remove dead code in set_param_for_module

* remove dead code

* cleaning again

* cleaning

* revert change

* linting

* refactor dtensor shard ops

* revert some stuff in core model loading

* core model loading clean

* guarding import

* better separation tensor parall and generic utils

* isolate DtensorShardOperation into a separate file

* no need to patch rotary

* better seperation

* simplify gather_full_state_dict

* simplify _replicate_dtensor

* fix and clean _replicate_dtensor

* better doc for DtensorShardOperation

* fix saving optimizer with DCP for fused weights

* save_pretrained(distributed_checkpoint=true)

* linting

* refactor into a single function _dtensor_from_local_like

* zeros_like instead of empty_like

* move tp and fsdp under distributed

* distribute_model

* fix deadlock when saving

* clip grad norm function

* maybe_disable_foreach_and_fused_for_mixed_dtensor_groups

* better TP api for ease of understanding

* remove shard_param to make it easier

* fix import in test

* _swap_dtensor_params_for_local

* fix qwen3 nanochat dots1

* add tpu

* move TP refactor experimentation scripts to backup branch

Move ad-hoc training / verification / compare scripts off this branch
into refactor-tp-dtensor-scripts so the diff stays focused on library
changes.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* linting

* register distributed sharding_utils and utils in __init__

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* rename TP plan styles to match new ALL_PARALLEL_STYLES registry

Replace pre-refactor names that no longer exist in
src/transformers/distributed/tensor_parallel.py:
  rowwise -> rowwise_allreduce
  moe_tp_experts -> moe_experts_allreduce
  replicated_with_grad_allreduce -> activation_seq_dim_2

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* enable EP

* Add enable_expert_parallel configuration option in test_distributed_config

* no more auto mode

* edit fsdp plan to every other models

* update fsdp mixin tests

* linting

* fix test fsdp

* fsdp linting

* revert gitignore

* _apply within for loop

* rename

* doc sp plan

* fix

* unified settattr + torch no grad + _local_tensor

* revert

* linting

* fix ruff

* make check-repository-consistency

* trigger fsdp mixin test in CI

* fix fsdp ci

* Reset tests/test_modeling_common.py to main

Restores legitimate improvements that were accidentally undone during a
stale merge of main into fsdp-vs-ddp:

- Restore test_resize_embeddings_untied_no_reinit_on_post_init
- Restore clipseg / Timm / evolla / parakeet_* / pi0 / musicflamingo
  special-cases
- Restore skip_base_model parameter on test_reverse_loading_mapping
- Restore "is not None" guard on subconfig in test_initialization
- Fix typo: "ot" -> "or" in test_reverse_loading_mapping assert message

---------

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
kashif pushed a commit to kashif/transformers that referenced this pull request Jun 1, 2026
* init

* FSDP2 (fully_shard) integration

- Add apply_fully_shard_data_parallel() with auto/manual mode block detection
- FSDP vs DDP loss/grad parity tests
- Distributed test helpers (testing_utils.py)
- is_fsdp_enabled(), is_fsdp_managed_module() utilities
- Minimal FSDP hooks in from_pretrained
- FSDP-aware flash attention check

* DistributedConfig + shard-on-read loading

- DtensorShardOperation for range-math shard-on-read
- spawn_materialize() enhancements
- from_pretrained wiring for distributed config
- Shard operation helpers in tensor_parallel
- Shard-on-read and LoadStateDictConfig tests

* TPStyle API + dense model tensor parallelism

- Replace hook-based TP with DTensor-based TPStyle API
- TPStyle dataclass with dense kinds: colwise, rowwise, vocab
- apply_tensor_parallel() using PyTorch parallelize_module
- verify_tp_plan() for plan validation
- Update dense model configs (llama, mistral, qwen2, phi, glm) to TPStyle
- DTensor apply_rotary_pos_emb guard for llama, mistral, qwen3
- Extended DistributedConfig with tp/fsdp size and plan fields
- DistributedConfig serialization in configuration_utils
- MXFP4 NotImplementedError for DTensor TP
- Dense TP tests

* revert some files

* Add distributed training scripts

- train_fsdp_tp.py: minimal FSDP+TP training example
- train_fsdp_tp_torchtitan_style.py: torchtitan-style training example
- verify_loading.py: save/load roundtrip verification
- run_compare.sh: FSDP+TP vs FSDP-only comparison
- run_verify_all.sh: run verification across all modes
- tmp_generate.py: quick generation test

* Remove train_fsdp_tp_torchtitan_style.py

* unify the utils for fsdp

* Fix CI: re-export moved FSDP utils + remove stale type: ignore

- Re-export is_fsdp_enabled and is_fsdp_managed_module from
  integrations/fsdp.py (moved to distributed/utils.py)
- Remove unused # type: ignore comments in generation/utils.py

* Fix ruff formatting in core_model_loading.py

* Fix ruff linting and formatting

* Backport new TP/FSDP API from orchestration-save-load branch

* Fix DTensor imports in Copied-from model files

* MoE expert parallelism + sequence parallelism (huggingface#45408)

* MoE expert parallelism + sequence parallelism

- Add PackedColwiseParallel for fused gate_up_proj weights
- Add MoEExpertsParallel with per-expert DTensor sharding
- Add PrepareModuleInputOutput for SP allgather/split hooks
- Add _AllReduceBackward for MoE routing weight gradients
- Extend TPStyle with moe_experts, packed_colwise, activation, module kinds
- _StridedShard handling in core_model_loading for interleaved weights
- MoE model configs: mixtral, deepseek_v3, qwen3 with SP plans
- DTensor rotary_pos_emb guard for mixtral

* Fix ruff linting and formatting

* Fix ruff formatting in core_model_loading.py

* Restore _IdentityOp accidentally removed in 25a1f48

The _IdentityOp class (added by PR huggingface#44983) was accidentally deleted
during the MoE expert parallelism work. It is needed by
finegrained_fp8.py and metal_quantization.py as a pass-through
reverse_op for dequantize operations.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* Backport new TP/FSDP API + fix DTensor imports in Copied-from models

* from_pretrained orchestration + distributed save/load (huggingface#45409)

* from_pretrained orchestration + save/load

- Add gather_full_state_dict() for DTensor→full tensor saving
- Add convert_strided_to_shard() / restore_strided_from_shard() for DCP
- Add _redistribute_dtensor() helper
- Full distributed_config integration in from_pretrained/save_pretrained
- Rename apply_fsdp2 → apply_fully_shard_data_parallel
- save_optimizer() / load_optimizer() in distributed/utils
- Trainer integration with distributed_config
- Updated FSDP and TP tests for new orchestration API
- DTensor shard-on-read test updates

* revert distributed utils

* eaaea

* all tests for core modeling are passing

* populate import from init for tp

* ruff

* ruff

---------

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>

* do monkey patching for rotary

* Revert modeling file diffs to match fsdp-core-model-loading base

Restores modeling files to their base branch versions so the PR diff
only shows the distributed/patches.py monkey-patch approach instead of
noisy function moves in modeling files.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* Migrate all model TP plans from strings to TPStyle

- Convert string plan values ("colwise", "rowwise", etc.) to TPStyle
  objects across 66+ model configs and modular files
- Consolidate MoE expert sub-entries into TPStyle("moe_experts", ...)
  with shard_plan
- Remove "replicated_with_grad_allreduce" entries (not needed for
  DTensor TP)
- Migrate _tp_plan class attributes in modeling files from
  "colwise_gather_output" to TPStyle("colwise", "allgather")
- Add TypeError in apply_tensor_parallel for unsupported plan values
- Remove old TensorParallelLayer tests (API removed in DTensor refactor)
- Regenerate auto-generated files via modular converter

* Restore mxfp4.py to match base branch

* Drop mla_kv_a_proj and moe_identity_expert from TP plans

These string plan values have no TPStyle equivalent in the DTensor
system. Remove them to avoid TypeError at apply_tensor_parallel time.
Affected models: deepseek_v2, glm4_moe_lite, glm_moe_dsa, longcat_flash.

* more comments

* fix tp for most models.  PyTorch doesn't implement all placement conversions (e.g. _StridedShard↔Shard). We force replicate beforehand

* fix tp through _replicate_dtensor

* revert small change

* push temporary fix for TP and strided shard for backward

* refactor a bit

* patches for rotary

* refactor MoEExpertsParallel

* fix tp for last models

* refactor moe expert parallels

* linting

* add sp plan for models

* add deepseek v2 sp plan

* undo sp plan for some tricky models

* remove lm_head from  config

* first pass of refactoring dtensor shard operator

* better refacto

* batter explanation of DtensorShardOperation

* refactor dtensor test to reflect real world scenario

* more comments

* fix tp olmo hybrid and exaone

* Enhance tensor parallel weight tying logic to prevent clobbering of lm_head when embed_tokens is not in the plan.

* fix fsdp mixin test due to missing args

* fix test non model

* skip sp plan for exaone and olmo hybrid

* linting

* fix import for ci

* test distributed config

* attempt to fix guarding import ci

* fix ci check repro

* add ALL_PARALLEL_STYLES registry alongside TPStyle

* route apply_tensor_parallel through ALL_PARALLEL_STYLES

* migrate modular files to string-based TP plans

* migrate standalone configs and modelings to string-based TP plans

* delete TPStyle dataclass

* fix use_local_output defaults for SequenceParallel and PrepareModuleInput in registry

* use parallel style from torch

* revert changes in weight converter

* remove dead code in set_param_for_module

* remove dead code

* cleaning again

* cleaning

* revert change

* linting

* refactor dtensor shard ops

* revert some stuff in core model loading

* core model loading clean

* guarding import

* better separation tensor parall and generic utils

* isolate DtensorShardOperation into a separate file

* no need to patch rotary

* better seperation

* simplify gather_full_state_dict

* simplify _replicate_dtensor

* fix and clean _replicate_dtensor

* better doc for DtensorShardOperation

* fix saving optimizer with DCP for fused weights

* save_pretrained(distributed_checkpoint=true)

* linting

* refactor into a single function _dtensor_from_local_like

* zeros_like instead of empty_like

* move tp and fsdp under distributed

* distribute_model

* fix deadlock when saving

* clip grad norm function

* maybe_disable_foreach_and_fused_for_mixed_dtensor_groups

* better TP api for ease of understanding

* remove shard_param to make it easier

* fix import in test

* _swap_dtensor_params_for_local

* fix qwen3 nanochat dots1

* add tpu

* move TP refactor experimentation scripts to backup branch

Move ad-hoc training / verification / compare scripts off this branch
into refactor-tp-dtensor-scripts so the diff stays focused on library
changes.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* linting

* register distributed sharding_utils and utils in __init__

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* rename TP plan styles to match new ALL_PARALLEL_STYLES registry

Replace pre-refactor names that no longer exist in
src/transformers/distributed/tensor_parallel.py:
  rowwise -> rowwise_allreduce
  moe_tp_experts -> moe_experts_allreduce
  replicated_with_grad_allreduce -> activation_seq_dim_2

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* enable EP

* Add enable_expert_parallel configuration option in test_distributed_config

* no more auto mode

* edit fsdp plan to every other models

* update fsdp mixin tests

* linting

* fix test fsdp

* fsdp linting

* revert gitignore

* _apply within for loop

* rename

* doc sp plan

* fix

* unified settattr + torch no grad + _local_tensor

* revert

* linting

* fix ruff

* make check-repository-consistency

* trigger fsdp mixin test in CI

* fix fsdp ci

* Reset tests/test_modeling_common.py to main

Restores legitimate improvements that were accidentally undone during a
stale merge of main into fsdp-vs-ddp:

- Restore test_resize_embeddings_untied_no_reinit_on_post_init
- Restore clipseg / Timm / evolla / parakeet_* / pi0 / musicflamingo
  special-cases
- Restore skip_base_model parameter on test_reverse_loading_mapping
- Restore "is not None" guard on subconfig in test_initialization
- Fix typo: "ot" -> "or" in test_reverse_loading_mapping assert message

---------

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
khushali9 pushed a commit to khushali9/transformers that referenced this pull request Jun 8, 2026
* init

* FSDP2 (fully_shard) integration

- Add apply_fully_shard_data_parallel() with auto/manual mode block detection
- FSDP vs DDP loss/grad parity tests
- Distributed test helpers (testing_utils.py)
- is_fsdp_enabled(), is_fsdp_managed_module() utilities
- Minimal FSDP hooks in from_pretrained
- FSDP-aware flash attention check

* DistributedConfig + shard-on-read loading

- DtensorShardOperation for range-math shard-on-read
- spawn_materialize() enhancements
- from_pretrained wiring for distributed config
- Shard operation helpers in tensor_parallel
- Shard-on-read and LoadStateDictConfig tests

* TPStyle API + dense model tensor parallelism

- Replace hook-based TP with DTensor-based TPStyle API
- TPStyle dataclass with dense kinds: colwise, rowwise, vocab
- apply_tensor_parallel() using PyTorch parallelize_module
- verify_tp_plan() for plan validation
- Update dense model configs (llama, mistral, qwen2, phi, glm) to TPStyle
- DTensor apply_rotary_pos_emb guard for llama, mistral, qwen3
- Extended DistributedConfig with tp/fsdp size and plan fields
- DistributedConfig serialization in configuration_utils
- MXFP4 NotImplementedError for DTensor TP
- Dense TP tests

* revert some files

* Add distributed training scripts

- train_fsdp_tp.py: minimal FSDP+TP training example
- train_fsdp_tp_torchtitan_style.py: torchtitan-style training example
- verify_loading.py: save/load roundtrip verification
- run_compare.sh: FSDP+TP vs FSDP-only comparison
- run_verify_all.sh: run verification across all modes
- tmp_generate.py: quick generation test

* Remove train_fsdp_tp_torchtitan_style.py

* unify the utils for fsdp

* Fix CI: re-export moved FSDP utils + remove stale type: ignore

- Re-export is_fsdp_enabled and is_fsdp_managed_module from
  integrations/fsdp.py (moved to distributed/utils.py)
- Remove unused # type: ignore comments in generation/utils.py

* Fix ruff formatting in core_model_loading.py

* Fix ruff linting and formatting

* Backport new TP/FSDP API from orchestration-save-load branch

* Fix DTensor imports in Copied-from model files

* MoE expert parallelism + sequence parallelism (huggingface#45408)

* MoE expert parallelism + sequence parallelism

- Add PackedColwiseParallel for fused gate_up_proj weights
- Add MoEExpertsParallel with per-expert DTensor sharding
- Add PrepareModuleInputOutput for SP allgather/split hooks
- Add _AllReduceBackward for MoE routing weight gradients
- Extend TPStyle with moe_experts, packed_colwise, activation, module kinds
- _StridedShard handling in core_model_loading for interleaved weights
- MoE model configs: mixtral, deepseek_v3, qwen3 with SP plans
- DTensor rotary_pos_emb guard for mixtral

* Fix ruff linting and formatting

* Fix ruff formatting in core_model_loading.py

* Restore _IdentityOp accidentally removed in 25a1f48

The _IdentityOp class (added by PR huggingface#44983) was accidentally deleted
during the MoE expert parallelism work. It is needed by
finegrained_fp8.py and metal_quantization.py as a pass-through
reverse_op for dequantize operations.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* Backport new TP/FSDP API + fix DTensor imports in Copied-from models

* from_pretrained orchestration + distributed save/load (huggingface#45409)

* from_pretrained orchestration + save/load

- Add gather_full_state_dict() for DTensor→full tensor saving
- Add convert_strided_to_shard() / restore_strided_from_shard() for DCP
- Add _redistribute_dtensor() helper
- Full distributed_config integration in from_pretrained/save_pretrained
- Rename apply_fsdp2 → apply_fully_shard_data_parallel
- save_optimizer() / load_optimizer() in distributed/utils
- Trainer integration with distributed_config
- Updated FSDP and TP tests for new orchestration API
- DTensor shard-on-read test updates

* revert distributed utils

* eaaea

* all tests for core modeling are passing

* populate import from init for tp

* ruff

* ruff

---------

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>

* do monkey patching for rotary

* Revert modeling file diffs to match fsdp-core-model-loading base

Restores modeling files to their base branch versions so the PR diff
only shows the distributed/patches.py monkey-patch approach instead of
noisy function moves in modeling files.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* Migrate all model TP plans from strings to TPStyle

- Convert string plan values ("colwise", "rowwise", etc.) to TPStyle
  objects across 66+ model configs and modular files
- Consolidate MoE expert sub-entries into TPStyle("moe_experts", ...)
  with shard_plan
- Remove "replicated_with_grad_allreduce" entries (not needed for
  DTensor TP)
- Migrate _tp_plan class attributes in modeling files from
  "colwise_gather_output" to TPStyle("colwise", "allgather")
- Add TypeError in apply_tensor_parallel for unsupported plan values
- Remove old TensorParallelLayer tests (API removed in DTensor refactor)
- Regenerate auto-generated files via modular converter

* Restore mxfp4.py to match base branch

* Drop mla_kv_a_proj and moe_identity_expert from TP plans

These string plan values have no TPStyle equivalent in the DTensor
system. Remove them to avoid TypeError at apply_tensor_parallel time.
Affected models: deepseek_v2, glm4_moe_lite, glm_moe_dsa, longcat_flash.

* more comments

* fix tp for most models.  PyTorch doesn't implement all placement conversions (e.g. _StridedShard↔Shard). We force replicate beforehand

* fix tp through _replicate_dtensor

* revert small change

* push temporary fix for TP and strided shard for backward

* refactor a bit

* patches for rotary

* refactor MoEExpertsParallel

* fix tp for last models

* refactor moe expert parallels

* linting

* add sp plan for models

* add deepseek v2 sp plan

* undo sp plan for some tricky models

* remove lm_head from  config

* first pass of refactoring dtensor shard operator

* better refacto

* batter explanation of DtensorShardOperation

* refactor dtensor test to reflect real world scenario

* more comments

* fix tp olmo hybrid and exaone

* Enhance tensor parallel weight tying logic to prevent clobbering of lm_head when embed_tokens is not in the plan.

* fix fsdp mixin test due to missing args

* fix test non model

* skip sp plan for exaone and olmo hybrid

* linting

* fix import for ci

* test distributed config

* attempt to fix guarding import ci

* fix ci check repro

* add ALL_PARALLEL_STYLES registry alongside TPStyle

* route apply_tensor_parallel through ALL_PARALLEL_STYLES

* migrate modular files to string-based TP plans

* migrate standalone configs and modelings to string-based TP plans

* delete TPStyle dataclass

* fix use_local_output defaults for SequenceParallel and PrepareModuleInput in registry

* use parallel style from torch

* revert changes in weight converter

* remove dead code in set_param_for_module

* remove dead code

* cleaning again

* cleaning

* revert change

* linting

* refactor dtensor shard ops

* revert some stuff in core model loading

* core model loading clean

* guarding import

* better separation tensor parall and generic utils

* isolate DtensorShardOperation into a separate file

* no need to patch rotary

* better seperation

* simplify gather_full_state_dict

* simplify _replicate_dtensor

* fix and clean _replicate_dtensor

* better doc for DtensorShardOperation

* fix saving optimizer with DCP for fused weights

* save_pretrained(distributed_checkpoint=true)

* linting

* refactor into a single function _dtensor_from_local_like

* zeros_like instead of empty_like

* move tp and fsdp under distributed

* distribute_model

* fix deadlock when saving

* clip grad norm function

* maybe_disable_foreach_and_fused_for_mixed_dtensor_groups

* better TP api for ease of understanding

* remove shard_param to make it easier

* fix import in test

* _swap_dtensor_params_for_local

* fix qwen3 nanochat dots1

* add tpu

* move TP refactor experimentation scripts to backup branch

Move ad-hoc training / verification / compare scripts off this branch
into refactor-tp-dtensor-scripts so the diff stays focused on library
changes.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* linting

* register distributed sharding_utils and utils in __init__

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* rename TP plan styles to match new ALL_PARALLEL_STYLES registry

Replace pre-refactor names that no longer exist in
src/transformers/distributed/tensor_parallel.py:
  rowwise -> rowwise_allreduce
  moe_tp_experts -> moe_experts_allreduce
  replicated_with_grad_allreduce -> activation_seq_dim_2

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* enable EP

* Add enable_expert_parallel configuration option in test_distributed_config

* no more auto mode

* edit fsdp plan to every other models

* update fsdp mixin tests

* linting

* fix test fsdp

* fsdp linting

* revert gitignore

* _apply within for loop

* rename

* doc sp plan

* fix

* unified settattr + torch no grad + _local_tensor

* revert

* linting

* fix ruff

* make check-repository-consistency

* trigger fsdp mixin test in CI

* fix fsdp ci

* Reset tests/test_modeling_common.py to main

Restores legitimate improvements that were accidentally undone during a
stale merge of main into fsdp-vs-ddp:

- Restore test_resize_embeddings_untied_no_reinit_on_post_init
- Restore clipseg / Timm / evolla / parakeet_* / pi0 / musicflamingo
  special-cases
- Restore skip_base_model parameter on test_reverse_loading_mapping
- Restore "is not None" guard on subconfig in test_initialization
- Fix typo: "ot" -> "or" in test_reverse_loading_mapping assert message

---------

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
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.

4 participants