2022-04-22 10:03:35 +00:00
|
|
|
from .utils import InsertPostInitMethodToModuleSubClasses
|
|
|
|
import torch
|
2022-06-03 10:04:22 +00:00
|
|
|
from colossalai.tensor import ColoTensor, ColoParameter
|
|
|
|
|
2022-06-06 07:34:41 +00:00
|
|
|
from colossalai.nn.parallel.layers import register_colo_module, \
|
2022-05-26 10:15:42 +00:00
|
|
|
ColoLinear, ColoEmbedding
|
2022-04-22 10:03:35 +00:00
|
|
|
|
2022-04-27 07:28:59 +00:00
|
|
|
from torch import nn
|
2022-06-03 10:04:22 +00:00
|
|
|
from typing import Iterator, Tuple, Union
|
2022-05-09 08:11:47 +00:00
|
|
|
|
2022-05-11 07:48:12 +00:00
|
|
|
# find named_params includes replica
|
2022-05-19 04:44:59 +00:00
|
|
|
|
|
|
|
|
2022-05-11 07:48:12 +00:00
|
|
|
def _named_params_with_replica(
|
2022-05-19 04:44:59 +00:00
|
|
|
module: nn.Module,
|
|
|
|
prefix: str = '',
|
|
|
|
recurse: bool = True,
|
|
|
|
) -> Iterator[Tuple[str, Union[nn.Parameter, ColoTensor]]]:
|
2022-05-11 07:48:12 +00:00
|
|
|
modules = module.named_modules(prefix=prefix) if recurse else [(prefix, module)]
|
|
|
|
|
|
|
|
for mod_prefix, mod in modules:
|
|
|
|
for name, val in mod._parameters.items():
|
|
|
|
if val is None:
|
|
|
|
continue
|
|
|
|
name = mod_prefix + ('.' if mod_prefix else '') + name
|
|
|
|
yield name, val
|
|
|
|
|
2022-06-03 10:04:22 +00:00
|
|
|
|
2022-04-27 07:28:59 +00:00
|
|
|
def ColoModulize(module):
|
|
|
|
"""
|
|
|
|
Replacing the parameters() and named_parameters() with our customized ones
|
|
|
|
"""
|
|
|
|
|
|
|
|
module._colo_visited = True
|
2022-04-22 10:03:35 +00:00
|
|
|
|
2022-05-19 04:44:59 +00:00
|
|
|
|
2022-04-22 10:03:35 +00:00
|
|
|
class ColoInitContext(InsertPostInitMethodToModuleSubClasses):
|
|
|
|
|
2022-04-25 06:24:26 +00:00
|
|
|
def __init__(self, lazy_memory_allocate: bool = False, device: torch.device = torch.device('cpu')):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
lazy_memory_allocate (bool, optional): whether to allocate memory for the parameter tensors. Defaults to False.
|
|
|
|
device (torch.device, optional): the device parameters initialized are resident on. Defaults to torch.device('cpu').
|
|
|
|
"""
|
2022-04-22 10:03:35 +00:00
|
|
|
super().__init__()
|
|
|
|
self._lazy_memory_allocate = lazy_memory_allocate
|
2022-04-25 06:24:26 +00:00
|
|
|
self._device = device
|
2022-04-22 10:03:35 +00:00
|
|
|
|
2022-05-26 10:15:42 +00:00
|
|
|
self._register_colo_modules()
|
|
|
|
|
|
|
|
def _register_colo_modules(self):
|
2022-05-26 03:50:44 +00:00
|
|
|
register_colo_module(torch.nn.Linear, ColoLinear())
|
2022-05-26 10:15:42 +00:00
|
|
|
register_colo_module(torch.nn.Embedding, ColoEmbedding())
|
2022-05-09 08:11:47 +00:00
|
|
|
|
2022-04-24 07:03:04 +00:00
|
|
|
def _post_init_method(self, module: torch.nn.Module, *args, **kwargs):
|
2022-04-22 10:03:35 +00:00
|
|
|
"""
|
|
|
|
The function to call at the end of the constructor of each module.
|
|
|
|
FIXME(fjr) The module may be passed to this function multiple times?
|
|
|
|
"""
|
2022-05-09 08:11:47 +00:00
|
|
|
|
2022-04-27 07:28:59 +00:00
|
|
|
if hasattr(module, '_colo_visited'):
|
|
|
|
return
|
|
|
|
|
2022-04-22 10:03:35 +00:00
|
|
|
name_list = []
|
2022-05-11 07:48:12 +00:00
|
|
|
for name, param in _named_params_with_replica(module):
|
2022-04-22 10:03:35 +00:00
|
|
|
if isinstance(param, ColoTensor):
|
|
|
|
continue
|
2022-04-28 07:23:40 +00:00
|
|
|
|
2022-05-11 07:48:12 +00:00
|
|
|
split = name.rfind('.')
|
2022-05-19 04:44:59 +00:00
|
|
|
if split >= 0: # param in submodule
|
2022-05-11 07:48:12 +00:00
|
|
|
module_name = name[:split]
|
2022-05-19 04:44:59 +00:00
|
|
|
param_name = name[split + 1:]
|
2022-05-11 07:48:12 +00:00
|
|
|
else:
|
2022-05-19 04:44:59 +00:00
|
|
|
module_name = '' # param in current module
|
2022-05-11 07:48:12 +00:00
|
|
|
param_name = name
|
|
|
|
name_list.append((module_name, param_name))
|
|
|
|
|
2022-05-19 04:44:59 +00:00
|
|
|
replaced_tensors = dict(
|
|
|
|
) # record mapping between (torch.Tensor, ColoTensor) to distinguish the same reference
|
2022-05-11 07:48:12 +00:00
|
|
|
for module_name, param_name in name_list:
|
|
|
|
submodule = module.get_submodule(module_name)
|
|
|
|
param = submodule.get_parameter(param_name)
|
|
|
|
if param in replaced_tensors:
|
|
|
|
colo_param = replaced_tensors[param]
|
|
|
|
else:
|
|
|
|
save_torch_payload = True if not self._lazy_memory_allocate else False
|
|
|
|
# detaching tensor is necessary for optimizers.
|
|
|
|
requires_grad = param.requires_grad
|
|
|
|
|
2022-05-19 04:44:59 +00:00
|
|
|
colo_param = ColoParameter(param.to(self._device), requires_grad=requires_grad)
|
2022-05-11 07:48:12 +00:00
|
|
|
# add mapping record
|
|
|
|
replaced_tensors[param] = colo_param
|
|
|
|
delattr(submodule, param_name)
|
|
|
|
setattr(submodule, param_name, colo_param)
|
2022-05-26 10:15:42 +00:00
|
|
|
colo_param.shared_param_modules.append(submodule)
|
2022-04-27 07:28:59 +00:00
|
|
|
|
2022-06-03 04:09:49 +00:00
|
|
|
module.to(self._device)
|
2022-05-19 04:44:59 +00:00
|
|
|
ColoModulize(module)
|