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

Check for meta tensors in checkpoint #10661

Closed
wants to merge 3 commits into from
Closed
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
16 changes: 15 additions & 1 deletion nemo/lightning/pytorch/strategies/megatron_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,26 @@ def optimizer_sharded_state_dict(self, is_loading=False):

@override
def save_checkpoint(
self, checkpoint: Dict[str, Any], filepath: Union[str, Path], storage_options: Optional[Any] = None
self,
checkpoint: Dict[str, Any],
filepath: Union[str, Path],
storage_options: Optional[Any] = None,
allow_meta_tensors: bool = True,
) -> None:
checkpoint["state_dict"] = OrderedDict([]) # remove device state_dict
# retrieve `sharded_state_dict` if it has not already been configured in `on_save_checkpoint`
if "sharded_state_dict" not in checkpoint:
checkpoint["sharded_state_dict"] = self.megatron_parallel.sharded_state_dict()
meta_tensors = list(
filter(
lambda x: isinstance(x[1], torch.Tensor) and x[1].device.type == 'meta',
checkpoint['sharded_state_dict'].items(),
)
)
for name, tensor in meta_tensors:
logging.warning(f"Got device=meta for {name}")
if not allow_meta_tensors:
assert len(meta_tensors) == 0, meta_tensors

## replace unsharded optimizer_states with sharded dict.
## note that if trainer.save_checkpoint(path, save_weights_only=True) is called,
Expand Down
Loading