Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto wrap modules in ZeRO 3 based on _no_split_modules as FSDP #3323

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions src/accelerate/accelerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1848,8 +1848,25 @@ def _prepare_deepspeed(self, *args):

engine, optimizer, _, lr_scheduler = ds_initialize(**kwargs)
if compare_versions("deepspeed", ">=", "0.14.4") and self.state.dynamo_plugin.backend != DynamoBackend.NO:
from deepspeed.utils import z3_leaf_module, set_z3_leaf_modules

if transformers_no_split_modules := getattr(model, "_no_split_modules", None):
leaf_cls = []
for module in model.modules():
if module.__class__ in leaf_cls:
continue
elif z3_leaf_module(module):
# have user-specificed z3 leaf module
leaf_cls.clear()
break
elif module.__class__.__name__ in transformers_no_split_modules:
leaf_cls.append(module.__class__)
if leaf_cls:
set_z3_leaf_modules(model, leaf_cls)

compile_kwargs = self.state.dynamo_plugin.to_kwargs()
engine.compile(backend=compile_kwargs.pop("backend"), compile_kwargs=compile_kwargs)

if optimizer is not None:
optimizer = DeepSpeedOptimizerWrapper(optimizer)
if scheduler is not None:
Expand Down