Skip to content

Shard on read Dtensor aware - #46717

Merged
3outeille merged 96 commits into
mainfrom
split/a-pr-3-dual-path-loading
Jul 13, 2026
Merged

Shard on read Dtensor aware#46717
3outeille merged 96 commits into
mainfrom
split/a-pr-3-dual-path-loading

Conversation

@3outeille

@3outeille 3outeille commented Jun 17, 2026

Copy link
Copy Markdown
Member

CI

What does this PR do ?

This PR adds shard-on-read support for loading DTensor-sharded model parameters during from_pretrained. Instead of materializing the full checkpoint tensor on every rank and sharding afterward, each rank reads only the slice it needs from the checkpoint, based on the target parameter’s DTensor mesh and placements.

For now I don't want to break the integration with the existing TP on main (since we haven't merged the inference-compatible DTensor TP yet). So here's what I'm doing:

  • I'm doing 2 loading paths for the moment (Dtensor and the old one for TP only)
  • I fully integrate FSDP and get it working with shard-on-read to keep the distributed PR as small as possible
  • Once only TP is left, I remove the second TP loading path and add DTensor inference for TP

Move FSDP2 wrapping and plan verification to distributed/fsdp.py, keep
integrations/fsdp.py as a backward-compatible re-export, and update core
call sites to import from transformers.distributed.fsdp.
@github-actions

Copy link
Copy Markdown
Contributor

View the CircleCI Test Summary for this PR:

https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=46717&sha=672bbb

@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.

@3outeille
3outeille marked this pull request as ready for review June 23, 2026 05:13
@3outeille 3outeille mentioned this pull request Jun 23, 2026
5 tasks
@3outeille
3outeille requested review from ArthurZucker and removed request for Rocketknight1 and ydshieh June 23, 2026 07:43
@3outeille
3outeille requested a review from ArthurZucker July 7, 2026 05:46
Base automatically changed from split/a-pr-2-fsdp-module to main July 9, 2026 00:47
@3outeille
3outeille changed the base branch from main to fix-distributed-config-sp-ep-docstring July 10, 2026 05:16
Base automatically changed from fix-distributed-config-sp-ep-docstring to main July 10, 2026 06:55

@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.

ty

sharding_op = mapping.distributed_operation
materialize_device = device_map[""]

future_or_tensor = spawn_materialize(

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.

ty

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 29221237811:1
Result: success | Jobs: 15 | Tests: 170,779 | Failures: 0 | Duration: 15h 27m

@3outeille
3outeille added this pull request to the merge queue Jul 13, 2026
Merged via the queue into main with commit 4517f14 Jul 13, 2026
104 checks passed
@3outeille
3outeille deleted the split/a-pr-3-dual-path-loading branch July 13, 2026 04:02
stevhliu pushed a commit to stevhliu/transformers that referenced this pull request Jul 30, 2026
* add distributed config

* Add native FSDP2 module and migrate FSDP imports (Phase A PR-2).

Move FSDP2 wrapping and plan verification to distributed/fsdp.py, keep
integrations/fsdp.py as a backward-compatible re-export, and update core
call sites to import from transformers.distributed.fsdp.

* linting

* unecessary

* copyright edit

* revert

* add shard on read

* jsut shard on read

* cleaning

* linting

* fix

* fix

* remove redundant test file

* Update src/transformers/distributed/fsdp.py

naming

Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>

* avoid looping, just look at dict

* expand_fsdp returns reshard_targets, no_reshard_targets right away

* better _resolve_tied_embed_lm_head_plan

* cleaning

* ruff

* more robust detection of embed and lm_head

* cleaning

* ruff

* typo

* cleaner

* cleaner

* typo

* refactor dense path + apply_contiguous_shard

* linting

* cleaning

* refactor _apply_strided_shard

* better

* refactor _slice_and_cat

* better comment

* refactor moe dtensor shard ops

* better comment

* comment

* cleaning

* linting

* expand_fsdp_plan iterate over modules

* comment about tie embedding

* add comment tied embedding

* some cleaning

* cleaning + comment

* rename function for clarity

* Apply suggestion from @ArthurZucker

Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>

* doc

* comment

---------

Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants