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

Wrap CPU model init with megatron_lazy_init_context #10219

Merged
merged 3 commits into from
Aug 26, 2024
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
3 changes: 2 additions & 1 deletion nemo/lightning/io/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def nemo_setup(self, model: pl.LightningModule, trainer: Optional[pl.Trainer] =
pl.Trainer: The trainer configured with the model and strategy.
"""
from nemo.lightning import MegatronStrategy, Trainer
from nemo.lightning._strategy_lib import megatron_lazy_init_context

_trainer = trainer or Trainer(
devices=1, accelerator="cpu", strategy=MegatronStrategy(store_optimizer_states=False)
Expand All @@ -155,7 +156,7 @@ def nemo_setup(self, model: pl.LightningModule, trainer: Optional[pl.Trainer] =

if not model.state_dict():
_trainer.strategy.lazy_init = True
with _trainer.init_module():
with _trainer.init_module(), megatron_lazy_init_context(model.config):
model.configure_model()

return _trainer
Expand Down
23 changes: 16 additions & 7 deletions nemo/lightning/io/pl.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,22 @@

validate_sharding_integrity = not (self.validated_consistency and self.assume_constant_structure)
self.validated_consistency = True
return dist_checkpointing.save(
sharded_state_dict=checkpoint,
checkpoint_dir=checkpoint_dir,
sharded_strategy=self.save_sharded_strategy,
validate_access_integrity=validate_sharding_integrity,
async_sharded_save=self.async_save,
)

try:
return dist_checkpointing.save(
sharded_state_dict=checkpoint,
checkpoint_dir=checkpoint_dir,
sharded_strategy=self.save_sharded_strategy,
validate_access_integrity=validate_sharding_integrity,
Dismissed Show dismissed Hide dismissed
async_sharded_save=self.async_save,
)
except:
logging.error(f"Failed to save checkpoint to {checkpoint_dir}")
# Do cleanup.
import shutil

shutil.rmtree(checkpoint_dir)
raise

@override
def load_checkpoint(
Expand Down
Loading