mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-04 18:40:28 +00:00
[tensor] distributed checkpointing for parameters (#1240)
This commit is contained in:
@@ -1,13 +1,10 @@
|
||||
from .utils import InsertPostInitMethodToModuleSubClasses
|
||||
import torch
|
||||
from colossalai.tensor import ColoTensor, ColoParameter, distspec, ProcessGroup, ReplicaSpec
|
||||
|
||||
from colossalai.tensor import ColoTensor, ColoParameter
|
||||
from colossalai.nn.parallel.layers import register_colo_module, \
|
||||
ColoLinear, ColoEmbedding
|
||||
from copy import copy
|
||||
from torch import nn
|
||||
from typing import Iterator, Tuple, Union
|
||||
from functools import partialmethod
|
||||
# find named_params includes replica
|
||||
|
||||
|
||||
@@ -34,47 +31,6 @@ def ColoModulize(module):
|
||||
module._colo_visited = True
|
||||
|
||||
|
||||
def colo_state_dict(self, destination=None, prefix='', keep_vars=False, state_dict_func=None):
|
||||
# build param to spec mapping
|
||||
mapping1 = dict()
|
||||
mapping2 = dict()
|
||||
mapping3 = dict()
|
||||
# gather all params
|
||||
has_dist_parameter = False
|
||||
with torch.no_grad():
|
||||
for param in self.parameters():
|
||||
if isinstance(param, ColoParameter):
|
||||
has_dist_parameter = True
|
||||
mapping1[id(param)] = copy(param.dist_spec)
|
||||
mapping2[id(param)] = copy(param.compute_spec)
|
||||
# TODO(jiaruifang) fixme, we should elegently handle the default PG in init context
|
||||
if param.get_process_group() is None:
|
||||
param.process_group = ProcessGroup()
|
||||
param.set_dist_spec(distspec.replicate())
|
||||
mapping3[id(param)] = param.get_process_group()
|
||||
param.process_group = None
|
||||
|
||||
# TODO: fix when keep_vars = True
|
||||
# when keep_vars = False, the state_dict_func will call detach to create
|
||||
# new tensors, but when keep_vars = True, the recovery of spec will be reflected
|
||||
# in the `ret`, such that the final state dict will still contain process group,
|
||||
# raising exception as it is not serializable
|
||||
assert not (keep_vars and has_dist_parameter), 'keep_vars cannot be True when there are distributed ColoParameters.'
|
||||
|
||||
ret = state_dict_func(self, destination, prefix, keep_vars)
|
||||
|
||||
# recover
|
||||
with torch.no_grad():
|
||||
for param in self.parameters():
|
||||
param_id = id(param)
|
||||
if param_id in mapping1:
|
||||
dist_spec = mapping1[id(param)]
|
||||
compute_spec = mapping2[id(param)]
|
||||
param.process_group = mapping3[id(param)]
|
||||
param.set_tensor_spec(dist_spec, compute_spec)
|
||||
return ret
|
||||
|
||||
|
||||
class ColoInitContext(InsertPostInitMethodToModuleSubClasses):
|
||||
|
||||
def __init__(self, lazy_memory_allocate: bool = False, device: torch.device = torch.device('cpu')):
|
||||
@@ -94,8 +50,7 @@ class ColoInitContext(InsertPostInitMethodToModuleSubClasses):
|
||||
register_colo_module(torch.nn.Embedding, ColoEmbedding())
|
||||
|
||||
def _pre_context_exec(self):
|
||||
self.state_dict_func = nn.Module.state_dict
|
||||
nn.Module.state_dict = partialmethod(colo_state_dict, state_dict_func=self.state_dict_func)
|
||||
pass
|
||||
|
||||
def _post_init_method(self, module: torch.nn.Module, *args, **kwargs):
|
||||
"""
|
||||
|
Reference in New Issue
Block a user