2022-07-06 09:22:03 +00:00
|
|
|
import torch
|
|
|
|
import torch.distributed as dist
|
2022-07-19 06:15:28 +00:00
|
|
|
from colossalai.tensor import ColoTensor
|
2022-07-15 01:52:55 +00:00
|
|
|
from colossalai.nn.optimizer import ColossalaiOptimizer
|
2022-07-19 06:15:28 +00:00
|
|
|
from colossalai.utils.checkpoint.utils import gather_tensor, scatter_tensor
|
2022-07-28 07:56:52 +00:00
|
|
|
from typing import Optional, Dict
|
2022-07-07 10:16:38 +00:00
|
|
|
|
|
|
|
|
2022-07-28 01:29:30 +00:00
|
|
|
def save_checkpoint(path: str,
|
2022-07-06 09:22:03 +00:00
|
|
|
epoch: int,
|
|
|
|
model: torch.nn.Module,
|
2022-07-15 01:52:55 +00:00
|
|
|
optimizer: Optional[ColossalaiOptimizer] = None,
|
2022-07-06 09:22:03 +00:00
|
|
|
lr_scheduler: torch.optim.lr_scheduler._LRScheduler = None,
|
|
|
|
*args,
|
|
|
|
**kwargs):
|
|
|
|
"""save_checkpoint
|
|
|
|
save a model, whose parameters are `ColoTensor`s.
|
|
|
|
Args:
|
2022-07-28 01:29:30 +00:00
|
|
|
path (str): directory to save the checkpoint files.
|
2022-07-06 09:39:46 +00:00
|
|
|
epoch (int): the number of epoch
|
|
|
|
model (torch.nn.Module): a torch module initialized by ColoInitContext
|
2022-07-15 01:52:55 +00:00
|
|
|
optimizer (ColossalaiOptimizer, optional): optimizers. Defaults to None.
|
2022-07-06 09:39:46 +00:00
|
|
|
lr_scheduler (torch.optim.lr_scheduler._LRScheduler, optional): lr schedule. Defaults to None.
|
2022-07-06 09:22:03 +00:00
|
|
|
"""
|
2022-07-19 06:15:28 +00:00
|
|
|
rank = dist.get_rank()
|
|
|
|
model_state = model.state_dict()
|
2022-07-12 07:51:06 +00:00
|
|
|
# save the dist context about the tensors in a new dict, while still maintain the original dict.
|
2022-07-19 06:15:28 +00:00
|
|
|
for k, v in model_state.items():
|
2022-07-12 07:51:06 +00:00
|
|
|
if isinstance(v, ColoTensor):
|
2022-07-27 03:03:14 +00:00
|
|
|
gather_tensor(v) # gather shared tensors to rank0
|
2022-07-19 06:15:28 +00:00
|
|
|
# don't recover tensors in rank0, since the dict is only a copy of model
|
|
|
|
|
|
|
|
if rank == 0:
|
|
|
|
# sanity check
|
|
|
|
for k, v in model_state.items():
|
2022-07-12 07:51:06 +00:00
|
|
|
if isinstance(v, ColoTensor):
|
2022-07-19 06:15:28 +00:00
|
|
|
assert v.save_ready
|
2022-07-12 07:51:06 +00:00
|
|
|
assert v.is_replicate()
|
2022-07-19 06:15:28 +00:00
|
|
|
delattr(v, 'save_ready')
|
|
|
|
# model saving
|
|
|
|
save_state = {'epoch': epoch, 'model': model_state}
|
2022-07-28 01:29:30 +00:00
|
|
|
torch.save(save_state, path + '/epoch_{}_model.pth'.format(epoch), *args, **kwargs)
|
2022-07-08 08:33:13 +00:00
|
|
|
|
2022-07-19 06:15:28 +00:00
|
|
|
# delete old dicts
|
|
|
|
del model_state
|
|
|
|
# synchronize all the processes
|
|
|
|
dist.barrier()
|
2022-07-06 09:22:03 +00:00
|
|
|
|
2022-07-26 02:53:53 +00:00
|
|
|
if optimizer is not None:
|
|
|
|
mapping = dict()
|
|
|
|
optim_state = optimizer.state_dict()
|
|
|
|
for k, v in optim_state['state'].items():
|
2022-07-19 06:15:28 +00:00
|
|
|
for n, t in v.items():
|
|
|
|
if isinstance(t, ColoTensor):
|
2022-07-26 02:53:53 +00:00
|
|
|
mapping[(k, n)] = t.dist_spec
|
|
|
|
gather_tensor(t)
|
2022-07-19 06:15:28 +00:00
|
|
|
|
2022-07-26 02:53:53 +00:00
|
|
|
if rank == 0:
|
|
|
|
save_state = {'epoch': epoch, 'optim': optim_state}
|
2022-07-28 01:29:30 +00:00
|
|
|
torch.save(save_state, path + '/epoch_{}_optim.pth'.format(epoch), *args, **kwargs)
|
2022-07-26 02:53:53 +00:00
|
|
|
# recover colo tensors in rank0
|
|
|
|
for k, v in optimizer.state_dict()['state'].items():
|
|
|
|
for n, t in v.items():
|
|
|
|
if isinstance(t, ColoTensor):
|
|
|
|
assert hasattr(t, 'save_ready')
|
|
|
|
t.set_dist_spec(mapping[(k, n)])
|
|
|
|
delattr(t, 'save_ready')
|
|
|
|
|
|
|
|
del optim_state
|
|
|
|
del mapping
|
|
|
|
dist.barrier()
|
2022-07-15 01:52:55 +00:00
|
|
|
|
2022-07-06 09:22:03 +00:00
|
|
|
|
2022-07-28 07:56:52 +00:00
|
|
|
def load_checkpoint(path: str,
|
2022-07-06 09:22:03 +00:00
|
|
|
epoch: int,
|
|
|
|
model: torch.nn.Module,
|
2022-07-15 01:52:55 +00:00
|
|
|
optimizer: Optional[ColossalaiOptimizer] = None,
|
2022-07-06 09:22:03 +00:00
|
|
|
lr_scheduler: torch.optim.lr_scheduler._LRScheduler = None,
|
2022-07-28 07:56:52 +00:00
|
|
|
torch_load_kwargs: Optional[Dict] = None,
|
|
|
|
load_state_dict_kwargs: Optional[Dict] = None):
|
2022-07-06 09:22:03 +00:00
|
|
|
"""load_checkpoint
|
|
|
|
load a model, whose parameters are `ColoTensor`s.
|
|
|
|
Args:
|
2022-07-28 07:56:52 +00:00
|
|
|
path (str): directory to save the checkpoint files.
|
|
|
|
epoch (int): the number of epoch
|
|
|
|
model (torch.nn.Module): a torch module initialized by ColoInitContext
|
|
|
|
optimizer (ColossalaiOptimizer, optional): optimizers. Defaults to None.
|
|
|
|
lr_scheduler (torch.optim.lr_scheduler._LRScheduler, optional): lr schedule. Defaults to None.
|
|
|
|
torch_load_kwargs: (dict, optional): The kwargs of torch.load inside the function
|
|
|
|
load_state_dict_kwargs (dict, optional): The kwargs of load_state_dict inside the function
|
2022-07-06 09:22:03 +00:00
|
|
|
"""
|
2022-07-29 07:58:06 +00:00
|
|
|
# initialize the default paramters
|
|
|
|
if not torch_load_kwargs:
|
|
|
|
torch_load_kwargs = dict()
|
|
|
|
if not load_state_dict_kwargs:
|
|
|
|
load_state_dict_kwargs = dict()
|
|
|
|
|
2022-07-19 06:15:28 +00:00
|
|
|
rank = dist.get_rank()
|
2022-07-12 07:51:06 +00:00
|
|
|
mapping = dict()
|
2022-07-19 06:15:28 +00:00
|
|
|
for n, p in model.named_parameters():
|
|
|
|
if isinstance(p, ColoTensor):
|
|
|
|
mapping[n] = p.dist_spec
|
|
|
|
gather_tensor(p)
|
|
|
|
|
|
|
|
if rank == 0:
|
2022-07-28 07:56:52 +00:00
|
|
|
load_state = torch.load(path + '/epoch_{}_model.pth'.format(epoch), **torch_load_kwargs)
|
|
|
|
model.load_state_dict(load_state['model'], **load_state_dict_kwargs)
|
2022-07-19 06:15:28 +00:00
|
|
|
dist.barrier()
|
|
|
|
|
|
|
|
# scatter loaded parameters
|
|
|
|
for n, p in model.named_parameters():
|
|
|
|
if isinstance(p, ColoTensor):
|
|
|
|
scatter_tensor(p, mapping[n])
|
|
|
|
if rank == 0:
|
|
|
|
assert hasattr(p, 'save_ready')
|
|
|
|
delattr(p, 'save_ready')
|
2022-07-15 01:52:55 +00:00
|
|
|
del mapping
|
|
|
|
|
2022-07-26 02:53:53 +00:00
|
|
|
if optimizer is not None:
|
|
|
|
mapping = dict()
|
|
|
|
for k, v in optimizer.state_dict()['state'].items():
|
|
|
|
for n, t in v.items():
|
|
|
|
if isinstance(t, ColoTensor):
|
|
|
|
mapping[(k, n)] = t.dist_spec
|
|
|
|
gather_tensor(t)
|
2022-07-15 01:52:55 +00:00
|
|
|
|
2022-07-26 02:53:53 +00:00
|
|
|
if rank == 0:
|
2022-07-28 07:56:52 +00:00
|
|
|
colo_checkpoint = torch.load(path + '/epoch_{}_optim.pth'.format(epoch), **torch_load_kwargs)
|
|
|
|
optimizer.load_state_dict(colo_checkpoint['optim'], **load_state_dict_kwargs)
|
2022-07-26 02:53:53 +00:00
|
|
|
dist.barrier()
|
2022-07-15 01:52:55 +00:00
|
|
|
|
2022-07-26 02:53:53 +00:00
|
|
|
for k, v in optimizer.state_dict()['state'].items():
|
|
|
|
for n, t in v.items():
|
|
|
|
if isinstance(t, ColoTensor):
|
|
|
|
scatter_tensor(t, mapping[(k, n)])
|
2022-07-19 06:15:28 +00:00
|
|
|
|
2022-07-26 02:53:53 +00:00
|
|
|
del mapping
|