|
|
|
@ -6,6 +6,21 @@ import types
|
|
|
|
|
from torch import nn
|
|
|
|
|
from typing import Iterator, Tuple, Union, Optional
|
|
|
|
|
|
|
|
|
|
# find named_params includes replica
|
|
|
|
|
def _named_params_with_replica(
|
|
|
|
|
module: nn.Module,
|
|
|
|
|
prefix: str = '',
|
|
|
|
|
recurse: bool = True,
|
|
|
|
|
) -> Iterator[Tuple[str, Union[nn.Parameter, ColoTensor]]]:
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
# Adapted from torch.nn.module.Module.register_param
|
|
|
|
|
def _register_parameter_with_colotensor(self, name: str, param):
|
|
|
|
|
if '_parameters' not in self.__dict__:
|
|
|
|
@ -139,21 +154,36 @@ class ColoInitContext(InsertPostInitMethodToModuleSubClasses):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
name_list = []
|
|
|
|
|
for name, param in module.named_parameters(recurse=False):
|
|
|
|
|
for name, param in _named_params_with_replica(module):
|
|
|
|
|
if isinstance(param, ColoTensor):
|
|
|
|
|
continue
|
|
|
|
|
name_list.append((name, param))
|
|
|
|
|
|
|
|
|
|
save_torch_payload = True if not self._lazy_memory_allocate else False
|
|
|
|
|
for name, param in name_list:
|
|
|
|
|
delattr(module, name)
|
|
|
|
|
|
|
|
|
|
# detaching tensor is necessary for optimizers.
|
|
|
|
|
requires_grad = param.requires_grad
|
|
|
|
|
tensor_detached = param.to(self._device).detach()
|
|
|
|
|
tensor_detached.requires_grad = requires_grad
|
|
|
|
|
|
|
|
|
|
colo_param = ColoParameter.init_from_torch_tensor(tensor=tensor_detached, save_payload=save_torch_payload)
|
|
|
|
|
setattr(module, name, colo_param)
|
|
|
|
|
split = name.rfind('.')
|
|
|
|
|
if split >= 0: # param in submodule
|
|
|
|
|
module_name = name[:split]
|
|
|
|
|
param_name = name[split+1:]
|
|
|
|
|
else:
|
|
|
|
|
module_name = '' # param in current module
|
|
|
|
|
param_name = name
|
|
|
|
|
name_list.append((module_name, param_name))
|
|
|
|
|
|
|
|
|
|
replaced_tensors = dict() # record mapping between (torch.Tensor, ColoTensor) to distinguish the same reference
|
|
|
|
|
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
|
|
|
|
|
tensor_detached = param.to(self._device).detach()
|
|
|
|
|
tensor_detached.requires_grad = requires_grad
|
|
|
|
|
|
|
|
|
|
colo_param = ColoParameter.init_from_torch_tensor(tensor=tensor_detached, save_payload=save_torch_payload)
|
|
|
|
|
# add mapping record
|
|
|
|
|
replaced_tensors[param] = colo_param
|
|
|
|
|
delattr(submodule, param_name)
|
|
|
|
|
setattr(submodule, param_name, colo_param)
|
|
|
|
|
|
|
|
|
|
ColoModulize(module)
|