Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
244 commits
Select commit Hold shift + click to select a range
799ac94
add distributed config
3outeille Jun 17, 2026
22d4b52
Add native FSDP2 module and migrate FSDP imports (Phase A PR-2).
3outeille Jun 17, 2026
4bfd1a6
linting
3outeille Jun 17, 2026
9487bdd
unecessary
3outeille Jun 17, 2026
588884e
copyright edit
3outeille Jun 17, 2026
8cc48a0
revert
3outeille Jun 17, 2026
672bbbe
add shard on read
3outeille Jun 17, 2026
5bbc796
Merge branch 'main' into split/a-pr-1-distributed-config
3outeille Jun 23, 2026
d0cc6a7
jsut shard on read
3outeille Jun 23, 2026
6fd7813
Merge branch 'split/a-pr-1-distributed-config' into split/a-pr-2-fsdp…
3outeille Jun 23, 2026
47519d7
Merge branch 'main' into split/a-pr-3-dual-path-loading
3outeille Jun 23, 2026
063e57b
cleaning
3outeille Jun 23, 2026
c06dc35
Merge branch 'split/a-pr-3-dual-path-loading' of https://github.com/h…
3outeille Jun 23, 2026
9d2e86d
linting
3outeille Jun 23, 2026
79457b3
fix
3outeille Jun 23, 2026
f3e8021
fix
3outeille Jun 23, 2026
acacae8
Merge branch 'split/a-pr-1-distributed-config' into split/a-pr-2-fsdp…
3outeille Jun 23, 2026
54c1f4e
Merge branch 'main' into split/a-pr-1-distributed-config
3outeille Jun 23, 2026
d23986a
Merge branch 'main' into split/a-pr-3-dual-path-loading
3outeille Jun 23, 2026
f219c74
Merge branch 'split/a-pr-1-distributed-config' into split/a-pr-2-fsdp…
3outeille Jun 23, 2026
ea8243f
remove redundant test file
3outeille Jun 24, 2026
db31b04
Merge branch 'main' into split/a-pr-1-distributed-config
3outeille Jun 24, 2026
4d840dc
Merge branch 'split/a-pr-1-distributed-config' into split/a-pr-2-fsdp…
3outeille Jun 24, 2026
c384fcd
Update src/transformers/distributed/fsdp.py
3outeille Jun 24, 2026
9625816
avoid looping, just look at dict
3outeille Jun 24, 2026
59bcec5
expand_fsdp returns reshard_targets, no_reshard_targets right away
3outeille Jun 24, 2026
ebf3585
better _resolve_tied_embed_lm_head_plan
3outeille Jun 24, 2026
e969325
cleaning
3outeille Jun 24, 2026
2376965
ruff
3outeille Jun 24, 2026
d830114
Merge branch 'main' into split/a-pr-1-distributed-config
3outeille Jun 24, 2026
a44f81f
Merge branch 'split/a-pr-1-distributed-config' into split/a-pr-2-fsdp…
3outeille Jun 24, 2026
0f62c45
more robust detection of embed and lm_head
3outeille Jun 24, 2026
020f7d3
Merge branch 'split/a-pr-2-fsdp-module' of https://github.com/hugging…
3outeille Jun 24, 2026
05d766f
Merge branch 'split/a-pr-2-fsdp-module' into split/a-pr-3-dual-path-l…
3outeille Jun 24, 2026
da302ad
cleaning
3outeille Jun 24, 2026
dfc665c
ruff
3outeille Jun 24, 2026
446fd6e
typo
3outeille Jun 24, 2026
5aeaff7
cleaner
3outeille Jun 24, 2026
819ff14
cleaner
3outeille Jun 24, 2026
413d775
Merge branch 'main' into split/a-pr-1-distributed-config
3outeille Jun 24, 2026
7bc3722
Merge branch 'split/a-pr-1-distributed-config' into split/a-pr-2-fsdp…
3outeille Jun 24, 2026
d6d8b51
Merge branch 'split/a-pr-2-fsdp-module' into split/a-pr-3-dual-path-l…
3outeille Jun 24, 2026
f8f27ff
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille Jun 24, 2026
ce2f001
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille Jun 24, 2026
ec87fff
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille Jun 25, 2026
6e156fd
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille Jun 26, 2026
606df0a
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille Jun 26, 2026
3ee8a92
typo
3outeille Jun 29, 2026
6bc43ae
Merge branch 'split/a-pr-3-dual-path-loading' of https://github.com/h…
3outeille Jun 29, 2026
c4aa4b7
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille Jun 29, 2026
c5ad67b
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille Jun 29, 2026
3c6c5f2
refactor dense path + apply_contiguous_shard
3outeille Jun 29, 2026
74490d6
linting
3outeille Jun 29, 2026
db6c99e
cleaning
3outeille Jun 29, 2026
04c124e
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille Jun 29, 2026
8f214ef
refactor _apply_strided_shard
3outeille Jun 29, 2026
ab34afb
better
3outeille Jun 29, 2026
c4bf166
refactor _slice_and_cat
3outeille Jun 29, 2026
be61c49
better comment
3outeille Jun 29, 2026
75ad5b9
refactor moe dtensor shard ops
3outeille Jul 1, 2026
91cd797
better comment
3outeille Jul 1, 2026
68069de
comment
3outeille Jul 1, 2026
83029ed
cleaning
3outeille Jul 1, 2026
1f857df
linting
3outeille Jul 1, 2026
aa140ed
Merge branch 'split/a-pr-2-fsdp-module' into split/a-pr-3-dual-path-l…
3outeille Jul 1, 2026
8b57aa4
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille Jul 1, 2026
7c11394
Add FSDP orchestration: mesh init, distribute-before-load, and DCP save.
3outeille Jul 1, 2026
70544cd
Merge branch 'split/a-pr-2-fsdp-module' into split/a-pr-3-dual-path-l…
3outeille Jul 1, 2026
17c6d40
Merge branch 'split/a-pr-3-dual-path-loading' into split/a-pr-4-fsdp-…
3outeille Jul 1, 2026
00eb116
add fsdp plan to 2 models for now
3outeille Jul 1, 2026
be296dd
add tests fsdp mixin
3outeille Jul 1, 2026
05900d6
linting
3outeille Jul 1, 2026
fc2423b
refactor test fsdp mixin
3outeille Jul 1, 2026
5bbd820
test fsdp mixin cleaning
3outeille Jul 1, 2026
b6d0b67
remove fsdp policy in tests + trim down further
3outeille Jul 1, 2026
ea36123
test fsdp clean
3outeille Jul 1, 2026
bec4d23
restore test_modeling_utils
3outeille Jul 1, 2026
8d3d329
linting
3outeille Jul 1, 2026
6316ee1
start trim down stuff
3outeille Jul 1, 2026
6e9004e
fix
3outeille Jul 1, 2026
68df491
breaking: cleaning modeling_utils.py
3outeille Jul 2, 2026
16b0b29
load path with fsdp (dtensor) and tp (old tp) is linked
3outeille Jul 2, 2026
e976a44
linting
3outeille Jul 2, 2026
a2fb155
add saving
3outeille Jul 2, 2026
5f52f19
styling
3outeille Jul 2, 2026
7f54301
fix tp ci
3outeille Jul 3, 2026
99f79ac
add fsdp to ci
3outeille Jul 3, 2026
b451490
linting
3outeille Jul 3, 2026
54ff4d1
pick one model only for this PR
3outeille Jul 3, 2026
3399539
restore
3outeille Jul 3, 2026
11cf79c
trigger fsdp ci
3outeille Jul 3, 2026
5b7ac3e
doc cleaning + tp_size remove
3outeille Jul 3, 2026
06b0c39
fix tp ci for ep
3outeille Jul 3, 2026
7a94d77
edit doc
3outeille Jul 3, 2026
f3c742b
move distributed function to utils + guarding
3outeille Jul 3, 2026
37df13e
linting
3outeille Jul 3, 2026
dd1000b
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille Jul 3, 2026
5c95559
expand_fsdp_plan iterate over modules
3outeille Jul 3, 2026
00a11b6
Merge branch 'split/a-pr-2-fsdp-module' of https://github.com/hugging…
3outeille Jul 3, 2026
fd3d4b0
Merge branch 'split/a-pr-2-fsdp-module' into split/a-pr-3-dual-path-l…
3outeille Jul 3, 2026
86875d2
Merge branch 'split/a-pr-3-dual-path-loading' into split/a-pr-4-fsdp-…
3outeille Jul 3, 2026
e4613e6
comment about tie embedding
3outeille Jul 3, 2026
c2b8451
Merge branch 'split/a-pr-2-fsdp-module' into split/a-pr-3-dual-path-l…
3outeille Jul 3, 2026
d558f99
add comment tied embedding
3outeille Jul 3, 2026
aed1818
Merge branch 'split/a-pr-2-fsdp-module' into split/a-pr-3-dual-path-l…
3outeille Jul 3, 2026
450579b
Merge branch 'split/a-pr-3-dual-path-loading' into split/a-pr-4-fsdp-…
3outeille Jul 3, 2026
91260ba
add DistributedMixin
3outeille Jul 3, 2026
1541674
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille Jul 6, 2026
9fd759d
Merge branch 'split/a-pr-2-fsdp-module' into split/a-pr-3-dual-path-l…
3outeille Jul 6, 2026
84dd107
solve merge conflict
3outeille Jul 6, 2026
9fae1b4
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille Jul 7, 2026
eb653bd
some cleaning
3outeille Jul 7, 2026
dcaa20a
Merge branch 'split/a-pr-3-dual-path-loading' of https://github.com/h…
3outeille Jul 7, 2026
d03499f
cleaning + comment
3outeille Jul 7, 2026
fb5533e
rename function for clarity
3outeille Jul 7, 2026
7838dc6
Apply suggestion from @ArthurZucker
3outeille Jul 7, 2026
877052b
doc
3outeille Jul 7, 2026
59f666f
Merge branch 'split/a-pr-3-dual-path-loading' of https://github.com/h…
3outeille Jul 7, 2026
17643a7
comment
3outeille Jul 7, 2026
7bb152c
Merge branch 'split/a-pr-2-fsdp-module' into split/a-pr-3-dual-path-l…
3outeille Jul 7, 2026
0b4d1e0
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille Jul 8, 2026
101562d
Merge branch 'split/a-pr-2-fsdp-module' into split/a-pr-3-dual-path-l…
3outeille Jul 8, 2026
99576ee
Merge branch 'split/a-pr-3-dual-path-loading' into split/a-pr-4-fsdp-…
3outeille Jul 8, 2026
22215b7
linting
3outeille Jul 8, 2026
f6ed465
Merge branch 'split/a-pr-4-fsdp-orchestration' of https://github.com/…
3outeille Jul 8, 2026
18d709c
refactor
3outeille Jul 8, 2026
566ef6e
abstract to mixin
3outeille Jul 8, 2026
b632db3
typo
3outeille Jul 8, 2026
0eec8c7
Add FSDP plans to all models from distributed branch.
3outeille Jul 8, 2026
a1f4b71
fsdp plans
3outeille Jul 8, 2026
a07e5bf
add fsdp plans
3outeille Jul 8, 2026
0dfe224
linting
3outeille Jul 8, 2026
e5a6535
linting
3outeille Jul 9, 2026
e764ceb
Add distributed runtime utils and DistributedMixin (FSDP orchestratio…
3outeille Jul 16, 2026
61362b7
Wire DistributedConfig through from_pretrained and save_pretrained (F…
3outeille Jul 16, 2026
eb34d37
Add FSDP CI and end-to-end FSDP tests (FSDP orchestration 3/3).
3outeille Jul 16, 2026
b866f00
addd ep_plan
3outeille Jul 16, 2026
3da6fb9
Merge branch 'split/a-pr-4-1-distributed-runtime' into split/a-pr-4-2…
3outeille Jul 16, 2026
ca8d037
Merge branch 'split/a-pr-4-2-orchestration' into split/a-pr-4-3-fsdp-…
3outeille Jul 16, 2026
b3f4538
restore validate module
3outeille Jul 16, 2026
b1fb1b9
Wire DistributedConfig through from_pretrained and save_pretrained.
3outeille Jul 16, 2026
1bf32cc
revert
3outeille Jul 16, 2026
1620c0f
Merge branch 'split/a-pr-4-1-distributed-runtime' into split/a-pr-4-2…
3outeille Jul 16, 2026
fe0dcf7
Merge branch 'split/a-pr-4-2-orchestration' into split/a-pr-4-3-fsdp-…
3outeille Jul 16, 2026
e52fda3
inline distribute_model
3outeille Jul 16, 2026
71dde8a
revert
3outeille Jul 16, 2026
4e23dbf
remove saving/loading
3outeille Jul 16, 2026
5742a2e
leaner mixin
3outeille Jul 16, 2026
5c81035
downgrade torch version guarding
3outeille Jul 16, 2026
004354f
remove
3outeille Jul 16, 2026
6d03414
linting
3outeille Jul 16, 2026
450987f
Merge branch 'split/a-pr-4-1-distributed-runtime' into split/a-pr-4-3…
3outeille Jul 16, 2026
47d85ad
revert
3outeille Jul 16, 2026
c577f33
Merge branch 'split/a-pr-4-2-fsdp-tests-ci' of https://github.com/hug…
3outeille Jul 16, 2026
b319d6a
revert
3outeille Jul 16, 2026
eb9a081
post_init() parallel plan move to mixin
3outeille Jul 16, 2026
d5358f3
Merge branch 'split/a-pr-4-1-distributed-runtime' into split/a-pr-4-2…
3outeille Jul 16, 2026
9a783c8
revert tp mixin
3outeille Jul 16, 2026
0448e72
Merge branch 'split/a-pr-4-1-distributed-runtime' into split/a-pr-4-2…
3outeille Jul 16, 2026
259bff4
add save/load
3outeille Jul 16, 2026
b15b63e
Merge branch 'main' into split/a-pr-4-1-distributed-runtime
3outeille Jul 16, 2026
17d611b
Merge branch 'main' into split/a-pr-4-1-distributed-runtime
3outeille Jul 16, 2026
7fe841b
Merge branch 'main' into split/a-pr-4-1-distributed-runtime
3outeille Jul 21, 2026
5f9b5ab
Merge branch 'split/a-pr-4-1-distributed-runtime' into split/a-pr-4-2…
3outeille Jul 21, 2026
b95be32
Merge branch 'main' into split/a-pr-4-2-fsdp-tests-ci
3outeille Jul 21, 2026
d625757
only FSDP save/load for now
3outeille Jul 21, 2026
9e810e4
Merge branch 'split/a-pr-4-2-fsdp-tests-ci' of https://github.com/hug…
3outeille Jul 21, 2026
049b89c
revert
3outeille Jul 21, 2026
a03f35a
Merge remote-tracking branch 'origin/split/a-pr-4-2-fsdp-tests-ci' in…
3outeille Jul 21, 2026
6b35d0e
refactor
3outeille Jul 21, 2026
862b428
Merge branch 'split/a-pr-4-2-fsdp-tests-ci' into split/a-pr-5-fsdp-plans
3outeille Jul 21, 2026
e503205
modular
3outeille Jul 21, 2026
c7fa843
Merge branch 'split/a-pr-5-fsdp-plans' of https://github.com/huggingf…
3outeille Jul 21, 2026
047bd41
Merge branch 'main' into split/a-pr-4-2-fsdp-tests-ci
3outeille Jul 23, 2026
8c96054
Merge branch 'main' into split/a-pr-4-2-fsdp-tests-ci
3outeille Jul 23, 2026
a1b0d77
Merge branch 'split/a-pr-4-2-fsdp-tests-ci' into split/a-pr-5-fsdp-plans
3outeille Jul 23, 2026
c364a54
Merge branch 'main' into split/a-pr-5-fsdp-plans
3outeille Jul 24, 2026
36ca137
ea
3outeille Jul 24, 2026
2f87b7d
Merge branch 'main' into split/a-pr-5-fsdp-plans
3outeille Jul 24, 2026
a171a19
begin migration TP
3outeille Jul 27, 2026
71255bf
clean up
3outeille Jul 27, 2026
d44d5db
Merge branch 'main' into split/a-pr-5-fsdp-plans
3outeille Jul 27, 2026
507a3c7
add it to pretrained model
3outeille Jul 27, 2026
258ab19
linting
3outeille Jul 27, 2026
70de295
fix the test by moving in init class the fsdp plan instead of post init
3outeille Jul 27, 2026
830f0e4
edit
3outeille Jul 27, 2026
0ddc172
replace everything
3outeille Jul 27, 2026
7193907
Merge branch 'split/a-pr-5-fsdp-plans' into distributed_guarding
3outeille Jul 27, 2026
4cb403f
Merge branch 'distributed_guarding' into split/b-pr-0-dual-path-tp
3outeille Jul 27, 2026
ad7248f
begin migration TP
3outeille Jul 27, 2026
1dcd5d2
clean up
3outeille Jul 27, 2026
2cccb89
revert merge conflict
3outeille Jul 27, 2026
4098e00
local params for forward
3outeille Jul 27, 2026
ed5c136
merged
3outeille Jul 27, 2026
b972782
cleaning
3outeille Jul 27, 2026
2469b2b
Merge branch 'main' into split/b-pr-0-dual-path-tp
3outeille Jul 28, 2026
c416695
migration from integration.tensor_parallel to distributed.tensor_pa…
3outeille Jul 29, 2026
68e932d
xMerge branch 'split/b-pr-0-dual-path-tp' of github.com:huggingface/t…
3outeille Jul 29, 2026
673188f
revert models
3outeille Jul 29, 2026
d2ecb9b
revert
3outeille Jul 29, 2026
7aacfdf
Merge branch 'main' into split/b-pr-0-dual-path-tp
3outeille Jul 29, 2026
5f392ff
fix
3outeille Jul 29, 2026
a2aaec7
breaking: cleaner way to_local for quantize weight(almost done need t…
3outeille Jul 29, 2026
2119865
fix all tests
3outeille Jul 30, 2026
48bf5b5
Merge branch 'main' into split/b-pr-0-dual-path-tp
3outeille Jul 30, 2026
911ac80
Merge branch 'main' into split/b-pr-0-dual-path-tp
3outeille Aug 3, 2026
42edef6
TP dtensor handle natively the to_local() for deepgem + fp8 (#47634)
3outeille Aug 3, 2026
dec0187
bench: dtensor vs legacy TP
3outeille Aug 3, 2026
c1bc2ca
Merge branch 'main' into split/b-pr-0-dual-path-tp
3outeille Aug 5, 2026
d31aa8c
bench: dtensor vs legacy TP (#47728)
3outeille Aug 5, 2026
632724b
Merge branch 'split/b-pr-0-dual-path-tp' of github.com:huggingface/tr…
3outeille Aug 5, 2026
610f3ff
remove helper script
3outeille Aug 5, 2026
c10dd03
Merge branch 'main' into split/b-pr-0-dual-path-tp
3outeille Aug 6, 2026
d0cdd9c
remove dead code in mxfp4
3outeille Aug 6, 2026
02077ae
Merge branch 'split/b-pr-0-dual-path-tp' of github.com:huggingface/tr…
3outeille Aug 6, 2026
57cf762
Merge branch 'main' into split/b-pr-0-dual-path-tp
3outeille Aug 6, 2026
6c4063d
remove old TP
3outeille Aug 6, 2026
078f128
Merge branch 'split/b-pr-0-dual-path-tp' of github.com:huggingface/tr…
3outeille Aug 6, 2026
8c66c74
cleaning
3outeille Aug 6, 2026
8b5f362
cleaning
3outeille Aug 6, 2026
8e26b06
claening
3outeille Aug 6, 2026
7febb1b
add test_shard_tensor_shape_consistency
3outeille Aug 6, 2026
1333432
cleaning
3outeille Aug 6, 2026
7eaf787
small fix
3outeille Aug 6, 2026
e62fff9
typo
3outeille Aug 6, 2026
2af1175
comment
3outeille Aug 6, 2026
5e450f3
Merge branch 'main' into split/b-pr-0-dual-path-tp
3outeille Aug 17, 2026
a29469e
rename colwise_gather_output to colwise_rep
3outeille Aug 17, 2026
637e37a
Merge branch 'split/b-pr-0-dual-path-tp' of github.com:huggingface/tr…
3outeille Aug 17, 2026
b41ddc9
keep tp import to avoid breaking chnges
3outeille Aug 17, 2026
488b801
remove async_op=True for redistribute
3outeille Aug 17, 2026
1230989
linting
3outeille Aug 17, 2026
1fc95b3
renaming MoeExpertsParallel class
3outeille Aug 17, 2026
351a4cd
ep router doc
3outeille Aug 17, 2026
2fd62de
mlinter
3outeille Aug 20, 2026
ddced0c
Merge branch 'main' into split/b-pr-0-dual-path-tp
3outeille Aug 20, 2026
2a37985
add todo
3outeille Aug 20, 2026
4eb3126
Merge branch 'split/b-pr-0-dual-path-tp' of github.com:huggingface/tr…
3outeille Aug 20, 2026
78f88a2
fix
3outeille Aug 20, 2026
d88b9b5
Merge branch 'main' into split/b-pr-0-dual-path-tp
3outeille Aug 20, 2026
e2644a6
Merge branch 'main' into split/b-pr-0-dual-path-tp
3outeille Aug 20, 2026
16dcef7
Merge branch 'main' into split/b-pr-0-dual-path-tp
3outeille Aug 21, 2026
47921ca
remove unused keys from global_mapping to avoid BC
3outeille Aug 21, 2026
ff7f904
Merge branch 'split/b-pr-0-dual-path-tp' of github.com:huggingface/tr…
3outeille Aug 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 9 additions & 35 deletions src/transformers/core_model_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@
from .distributed.sharding_utils import DtensorShardOperation, _dtensor_from_local_like
from .distributed.utils import is_dtensor
from .integrations.accelerate import get_device, offload_weight
from .integrations.tensor_parallel import ALL_PARALLEL_STYLES
from .utils import is_env_variable_true
from .utils.loading_report import LoadStateDictInfo
from .utils.logging import get_logger, tqdm


if TYPE_CHECKING:
from .integrations.tensor_parallel import TensorParallelLayer
from .modeling_utils import LoadStateDictConfig, PreTrainedModel
from .quantizers import HfQuantizer

Expand Down Expand Up @@ -1232,9 +1230,9 @@ def spawn_materialize(
) -> Future | Callable:
"""Materialize (and optionally shard) a tensor, asynchronously if a thread pool is provided.

When ``sharding_op`` is given the tensor is sharded (DTensor placement or legacy TP plan);
otherwise it is simply copied to *device*/*dtype*. Without a thread pool a deferred
callable is returned instead of a Future.
When ``sharding_op`` is given the tensor is sharded according to its DTensor placements;
otherwise it is simply copied to *device*/*dtype*. Without a thread pool a deferred callable
is returned instead of a Future.
"""

def _job():
Expand Down Expand Up @@ -1319,7 +1317,6 @@ def set_param_for_module(
target_name: str,
param_value: torch.Tensor,
loading_info: LoadStateDictInfo,
distributed_operation: TensorParallelLayer | None,
hf_quantizer: HfQuantizer,
):
module_path, _, param_name = target_name.rpartition(".")
Expand All @@ -1341,26 +1338,21 @@ def set_param_for_module(
# Remove from missing keys (it's either mismatched, or all good)
loading_info.missing_keys.discard(target_name)

# Determine expected shape: for TP/Dtensor, use sharded shape; otherwise, use full shape
if distributed_operation is not None:
expected_shape = torch.Size(distributed_operation.get_expected_sharded_shape(ref.shape))
elif is_dtensor(ref):
expected_shape = ref._local_tensor.shape
else:
expected_shape = ref.shape
# For DTensor parameters, compare against the local shard loaded on this rank.
expected_shape = ref._local_tensor.shape if is_dtensor(ref) else ref.shape

if ref is not None and param_value.shape != expected_shape and hf_quantizer is None:
loading_info.mismatched_keys.add((target_name, param_value.shape, expected_shape))
else:
if is_dtensor(ref):
local_param = param_value.detach() if isinstance(param_value, torch.nn.Parameter) else param_value
dtensor_param = _dtensor_from_local_like(local_param, ref)
param_value = torch.nn.Parameter(dtensor_param, requires_grad=ref.requires_grad)
param_value = torch.nn.Parameter(
dtensor_param, requires_grad=ref.requires_grad and dtensor_param.is_floating_point()
)
# super important otherwise _init_weight will re-init the param
param_value._is_hf_initialized = True
setattr(module_obj, param_name, param_value)
if distributed_operation is not None:

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.

No longer needed as DTensor carries global/local shape metadata + special attributes such as num_experts are updated by MoEParamShard.shard_param().

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.

that is marvellous

distributed_operation.update_module_attributes(module_obj)


def offload_and_maybe_resave_param(
Expand Down Expand Up @@ -1466,7 +1458,6 @@ def convert_and_load_state_dict_in_model(
model: PreTrainedModel,
state_dict: dict[str, Any],
load_config: LoadStateDictConfig,
tp_plan: dict[str, str] | None,
disk_offload_index: dict | None = None,
):
r"""
Expand Down Expand Up @@ -1556,11 +1547,9 @@ def convert_and_load_state_dict_in_model(

"""
base_model_prefix = model.base_model_prefix
tp_plan = tp_plan or {}
device_map = load_config.device_map or {"": "cpu"}
hf_quantizer = load_config.hf_quantizer
dtype = load_config.dtype
device_mesh = load_config.device_mesh
disk_offload_folder = load_config.disk_offload_folder
offload_buffers = load_config.offload_buffers
dtype_plan = load_config.dtype_plan or {}
Expand Down Expand Up @@ -1595,10 +1584,6 @@ def convert_and_load_state_dict_in_model(
converters = [entry for entry in weight_mapping if isinstance(entry, WeightConverter)]
param_name_to_load: dict[str, WeightRenaming | WeightConverter] = {}

# build '(?P<g0>.*.*\\.block_sparse_moe\\..*)' and group to source {'g0': '*.block_sparse_moe.'}
# and target to source {'g0': '*.mlp.'}. This allows us to quickly find which pattern matched.
if tp_plan != {}:
tp_plan_alt, tp_plan_by_group_name, _ = build_glob_alternation(list(tp_plan.keys()))
if dtype_plan != {}:
dtype_policy_alt, dtype_policy_by_group_name, _ = build_glob_alternation(list(dtype_plan.keys()))

Expand Down Expand Up @@ -1673,23 +1658,13 @@ def convert_and_load_state_dict_in_model(
else None
)

# 4. Handle TP/Dtensor sharding or device_map placement
# 4. Handle DTensor sharding or device_map placement
param_device = get_device(device_map, renamed_key, valid_torch_device=True)
sharding_op = None
materialize_device = param_device

if is_dtensor(empty_param):
sharding_op = DtensorShardOperation(empty_param)

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.

everything happens in here now?

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.

yeah it will load only the slice of the safetensors based on dtensor metadata placements

elif device_mesh and tp_plan:
if matched_tp_pattern := tp_plan_alt.search(renamed_key):
matched_tp_pattern = tp_plan_by_group_name[matched_tp_pattern.lastgroup]
if getattr(mapping, "distributed_operation", None) is None:
tp_layer = ALL_PARALLEL_STYLES[model.tp_plan[matched_tp_pattern]].__class__
mapping.distributed_operation = tp_layer(
device_mesh=device_mesh, rank=device_mesh.get_local_rank(), empty_param=empty_param.clone()
)
sharding_op = mapping.distributed_operation
materialize_device = device_map[""]

future_or_tensor = spawn_materialize(
thread_pool,
Expand Down Expand Up @@ -1732,7 +1707,6 @@ def convert_and_load_state_dict_in_model(
target_name,
param,
loading_info,
mapping.distributed_operation,
hf_quantizer,
)

Expand Down
2 changes: 1 addition & 1 deletion src/transformers/distributed/fsdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import os
from typing import TYPE_CHECKING, Any

from ..integrations.tensor_parallel import replace_layer_number_by_wildcard
from ..utils import is_torch_available, is_torch_distributed_available, is_torch_greater_or_equal, logging, strtobool
from ..utils.quantization_config import QuantizationMethod
from .tensor_parallel import replace_layer_number_by_wildcard
from .utils import _is_torch_distributed_initialized


Expand Down
32 changes: 14 additions & 18 deletions src/transformers/distributed/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@
import warnings
from typing import TYPE_CHECKING

from ..integrations.tensor_parallel import (
ALL_PARALLEL_STYLES,
apply_tensor_parallelism,
gather_state_dict_for_save,
initialize_tensor_parallelism,
)
from ..utils import is_torch_greater_or_equal, logging
from ..utils.hub import create_and_tag_model_card
from .configuration_utils import DistributedConfig
from .fsdp import apply_fully_sharded_data_parallelism, is_fsdp_managed_module
from .tensor_parallel import (
ALL_PARALLEL_STYLES,
apply_tensor_parallelism,
gather_state_dict_for_save,
)
from .utils import (
_distributed_barrier,
_ensure_torch_distributed,
Expand All @@ -36,6 +35,7 @@
_is_torch_distributed_initialized,
gather_full_state_dict,
initialize_fully_sharded_data_parallelism,
initialize_tensor_parallelism,
save_model_checkpoint_distributed,
)

Expand All @@ -48,11 +48,7 @@


class DistributedMixin:
"""Distributed orchestration and save/load hooks for [`PreTrainedModel`].

Stateless heavy lifting stays in `transformers.distributed.*` and
`integrations.tensor_parallel`. This mixin owns orchestration and instance state.
"""
"""Distributed orchestration and save/load hooks for [`PreTrainedModel`]."""

_device_mesh = None
_tp_plan: dict[str, str] | None = None
Expand Down Expand Up @@ -197,15 +193,13 @@ def maybe_distribute_model(
model._device_mesh = device_mesh

if distributed_config.tp_size > 1:
model = apply_tensor_parallelism(
model,
distributed_config.tp_plan,
distributed_config,
device_mesh,
)
tp_mesh = device_mesh["tp"] if device_mesh.ndim > 1 else device_mesh
model = apply_tensor_parallelism(model, tp_mesh)

elif distributed_config.fsdp_size > 1:
fsdp_mesh = device_mesh["fsdp"] if device_mesh.ndim > 1 else device_mesh
model = apply_fully_sharded_data_parallelism(model, fsdp_mesh)

return model

def should_save_on_this_rank(self, is_main_process: bool) -> bool:
Expand Down Expand Up @@ -267,7 +261,9 @@ def gather_sharded_state_dict_for_save(
return state_dict

if distributed_config.tp_size > 1:
state_dict = gather_state_dict_for_save(state_dict, self._tp_plan, self._device_mesh, self._tp_size)
state_dict = gather_state_dict_for_save(
state_dict, self._tp_plan, self._device_mesh, distributed_config.tp_size
)
if not save_on_this_rank:
state_dict = {}
return state_dict
Expand Down
Loading
Loading