Skip to content

Add distributed runtime utils and DistributedMixin - #47352

Merged
3outeille merged 17 commits into
mainfrom
split/a-pr-4-1-distributed-runtime
Jul 21, 2026
Merged

Add distributed runtime utils and DistributedMixin #47352
3outeille merged 17 commits into
mainfrom
split/a-pr-4-1-distributed-runtime

Conversation

@3outeille

@3outeille 3outeille commented Jul 16, 2026

Copy link
Copy Markdown
Member

CI

Summary

Part of FSDP orchestration stack (1/2). Replaces #46990.

  • Add distributed/utils.py (mesh init, distribute_model)
  • Add DistributedMixin with plan properties and orchestration hooks
  • Extend DistributedConfig with deferred validate() and FSDP+TP guard
  • Rename apply_fully_sharded_data_parallelapply_fully_sharded_data_parallelism (torch>=2.7)
  • Refactor PreTrainedModel to inherit DistributedMixin (plan properties moved out)
  • Rename distribute_modelapply_tensor_parallelism in tensor_parallel.py
  • Wire prepare_distribute_model / maybe_distribute_model into from_pretrained
  • Remove tp_plan / tp_size kwargs from from_pretrained; use distributed_config=DistributedConfig(tp_size=N) instead
  • Migrate TP tests and docs to explicit DistributedConfig

…n 1/3).

Introduce distributed/utils.py and DistributedMixin, defer DistributedConfig
validation to load time, and refactor PreTrainedModel plan properties without
changing the from_pretrained distributed_config API yet.
@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.

Route distributed load/save orchestration through DistributedMixin so TP and FSDP paths share the same entry points.
@3outeille 3outeille changed the title Add distributed runtime utils and DistributedMixin (FSDP orchestration 1/3) Add distributed runtime utils and DistributedMixin (FSDP orchestration 1/2) Jul 16, 2026
@3outeille 3outeille changed the title Add distributed runtime utils and DistributedMixin (FSDP orchestration 1/2) Add distributed runtime utils and DistributedMixin Jul 16, 2026
@3outeille

Copy link
Copy Markdown
Member Author

run-slow: deepseek_v4, glm_moe_dsa, gpt_oss

@github-actions

Copy link
Copy Markdown
Contributor

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs:

models: ["models/deepseek_v4", "models/glm_moe_dsa", "models/gpt_oss"]
quantizations: []

@github-actions

Copy link
Copy Markdown
Contributor

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN c46c05f2 workflow commit (merge commit)
PR eb9a081e branch commit (from PR)
main 28596623 base commit (on main)

⚠️ Model CI failed to report results

The test failure analysis could not be completed. Please check the workflow run for details.

@3outeille
3outeille requested a review from ArthurZucker July 16, 2026 14:09

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM ty!

tp_plan="auto",
distributed_config=DistributedConfig(enable_expert_parallel=True),
distributed_config=DistributedConfig(
tp_size=int(os.environ["WORLD_SIZE"]),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

tp size could be auto no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

we need to specify the tp_size because having 8 gpus doesnt mean we want to automatically split on 8 gpus. However tp_plan is automatically triggered if tp_size > 1

Comment on lines +81 to +88
if not is_torch_available():
raise RuntimeError("PyTorch is required to use DistributedConfig.")

if not torch.distributed.is_available() or not torch.distributed.is_initialized():
raise RuntimeError(
"torch.distributed must be initialized before using DistributedConfig with tp_size > 1 or "
"fsdp_size > 1. Call dist.init_process_group(...) first, or launch with torchrun."
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

let's make sure we don't already gate this somewhere else

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

i'll defer to another PR as for now TP + FSDP is not linked together. They are both initialising the process group which is redudant. Once it will be unified, i'll remove gate once and for all

@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: deepseek_v4, glm_moe_dsa, gpt_oss

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 29791005625:2
Result: success | Jobs: 1 | Tests: 13,456 | Failures: 0 | Duration: 23m 17s

@3outeille
3outeille added this pull request to the merge queue Jul 21, 2026
@3outeille
3outeille removed this pull request from the merge queue due to a manual request Jul 21, 2026
@3outeille
3outeille added this pull request to the merge queue Jul 21, 2026
Merged via the queue into main with commit 29985e6 Jul 21, 2026
196 of 198 checks passed
@3outeille
3outeille deleted the split/a-pr-4-1-distributed-runtime branch July 21, 2026 02:27
@BenjaminBossan

Copy link
Copy Markdown
Member
  • Remove tp_plan / tp_size kwargs from from_pretrained; use distributed_config=DistributedConfig(tp_size=N) instead

@3outeille Isn't this a backwards incompatible change? I thought the idea with Transformers v5 was to minimize those. This change doesn't even come with a deprecation cycle or useful error message for users to update their code.

stevhliu pushed a commit to stevhliu/transformers that referenced this pull request Jul 30, 2026
* Add distributed runtime utils and DistributedMixin (FSDP orchestration 1/3).

Introduce distributed/utils.py and DistributedMixin, defer DistributedConfig
validation to load time, and refactor PreTrainedModel plan properties without
changing the from_pretrained distributed_config API yet.

* addd ep_plan

* restore validate module

* Wire DistributedConfig through from_pretrained and save_pretrained.

Route distributed load/save orchestration through DistributedMixin so TP and FSDP paths share the same entry points.

* revert

* inline distribute_model

* revert

* remove saving/loading

* leaner mixin

* downgrade torch version guarding

* remove

* linting

* post_init() parallel plan move to mixin

* revert tp mixin
@3outeille

Copy link
Copy Markdown
Member Author

@BenjaminBossan fair point indeed, this is backwards incompatible and I should have shipped a deprecation path with it. I would be more mindful next time as the distributed features are not stable yet

Sainava pushed a commit to Sainava/Sai-transformers that referenced this pull request Aug 3, 2026
* Add distributed runtime utils and DistributedMixin (FSDP orchestration 1/3).

Introduce distributed/utils.py and DistributedMixin, defer DistributedConfig
validation to load time, and refactor PreTrainedModel plan properties without
changing the from_pretrained distributed_config API yet.

* addd ep_plan

* restore validate module

* Wire DistributedConfig through from_pretrained and save_pretrained.

Route distributed load/save orchestration through DistributedMixin so TP and FSDP paths share the same entry points.

* revert

* inline distribute_model

* revert

* remove saving/loading

* leaner mixin

* downgrade torch version guarding

* remove

* linting

* post_init() parallel plan move to mixin

* revert tp mixin
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