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

Avoid rewrapping modules with DDP/FSDP if already wrapped #12096

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 3 additions & 2 deletions nemo/lightning/fabric/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ def convert_module(self, module: nn.Module) -> nn.Module:
config = get_model_config(module.module)
config.fp16 = self.dtype_config.fp16
config.bf16 = self.dtype_config.bf16
if hasattr(module, 'module'):
# Avoid rewrapping the module if it's already of type Float16Module
if hasattr(module, 'module') and not isinstance(module.module, Float16Module):
module.module = Float16Module(config, module.module)
else:
elif not isinstance(module, Float16Module):
module = Float16Module(config, module)
Comment on lines +149 to 153
Copy link
Contributor

Choose a reason for hiding this comment

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

We need these changes to be applied to MegatronMixedPrecision as well:

if hasattr(module, 'module'):
module.module = Float16Module(config, module.module)
else:
module = Float16Module(config, module)

Copy link
Contributor

Choose a reason for hiding this comment

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

however, even with this change we are wrapping with Float16Module again. The hierarchy of modules printed just before the convert_module() method in MegatronMixedPrecision :

1st run

BionemoLightningModule(
   (module): ESM2FineTuneSeqModel(

2nd run

BionemoLightningModule(
  (module): DDP(
    (module): Float16Module(
       (module): ESM2FineTuneSeqModel(


return module
Expand Down
5 changes: 3 additions & 2 deletions nemo/lightning/megatron_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,15 +649,16 @@ def init_ddp(self):
disable_bucketing = (model_chunk_idx > 0) or overlap_param_gather_with_optimizer_step

with init_ddp_context():
if HAVE_CUSTOM_FSDP and self.ddp_config.use_custom_fsdp:
# Avoid rewrapping the module if it's already wrapped with FSDP
if HAVE_CUSTOM_FSDP and self.ddp_config.use_custom_fsdp and not isinstance(module, FullyShardedDataParallel):
FSDP = FullyShardedDataParallel
dist_module = FSDP(
module.config,
self.ddp_config,
module,
disable_bucketing=disable_bucketing,
)
else:
elif not isinstance(module, DDP):
dist_module = DDP(
module.config,
self.ddp_config,
Expand Down
Loading