Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 16 additions & 3 deletions deepmd/pt/train/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,13 @@
get_optimizer_state_dict,
set_optimizer_state_dict,
)
from torch.distributed.fsdp import (
fully_shard,
)

try:
from torch.distributed.fsdp import (
fully_shard,
)
except ImportError:
fully_shard = None # type: ignore[assignment]
from torch.distributed.optim import (
ZeroRedundancyOptimizer,
)
Expand Down Expand Up @@ -853,6 +857,15 @@ def single_model_finetune(
if self.is_distributed:
torch.cuda.set_device(LOCAL_RANK)
if self.zero_stage >= 2:
if fully_shard is None:
raise RuntimeError(
"training.zero_stage>=2 requires FSDP2, which is only "
"available in PyTorch >= 2.6 "
Comment thread
OutisLi marked this conversation as resolved.
"(``torch.distributed.fsdp.fully_shard``). "
f"Current PyTorch is {torch.__version__}. "
"Please upgrade PyTorch, or set training.zero_stage "
"to 0 or 1 to stay on the DDP / ZeRO-1 path."
)
# FSDP2 does NOT broadcast params (unlike DDP constructor).
# Ensure all ranks share identical weights before sharding.
for p in self.wrapper.parameters():
Expand Down
1 change: 1 addition & 0 deletions deepmd/utils/argcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -3948,6 +3948,7 @@ def training_args(
"but reduces optimizer memory to 1/N per GPU. "
"2: FSDP2 stage-2, shards optimizer states and gradients; same communication "
"volume as stage-1 but further reduces gradient memory to 1/N per GPU. "
"Stages 2 and 3 require FSDP2, which is available in PyTorch >= 2.6. "
"Note: FSDP2 introduces DTensor dispatch overhead that can slow down "
"models with many small layers; use torch.compile to mitigate. "
"3: FSDP2 stage-3, shards parameters as well; maximum memory savings but "
Expand Down
Loading