mirror of https://github.com/hpcaitech/ColossalAI
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.8 KiB
111 lines
3.8 KiB
2 years ago
|
from typing import List, Optional, Tuple
|
||
|
|
||
3 years ago
|
import torch
|
||
2 years ago
|
|
||
|
from colossalai.zero.legacy.gemini.stateful_tensor import StatefulTensor, TensorState
|
||
|
from colossalai.zero.legacy.gemini.tensor_utils import colo_tensor_mem_usage
|
||
|
|
||
|
from .sharded_tensor import ShardedTensor
|
||
3 years ago
|
|
||
3 years ago
|
EMPTY_TENSOR_DICT = {}
|
||
|
|
||
|
|
||
|
def get_empty_tensor(device: torch.device, dtype: torch.dtype):
|
||
|
key = (device, dtype)
|
||
|
if key not in EMPTY_TENSOR_DICT:
|
||
3 years ago
|
EMPTY_TENSOR_DICT[key] = torch.empty(0, dtype=dtype, device=device)
|
||
3 years ago
|
|
||
|
return EMPTY_TENSOR_DICT[key]
|
||
|
|
||
3 years ago
|
|
||
|
class ShardedParamV2(object):
|
||
|
|
||
3 years ago
|
def __init__(self, param: torch.nn.Parameter, set_data_none: bool = False) -> None:
|
||
3 years ago
|
self._sharded_data_tensor: ShardedTensor = ShardedTensor(param.data)
|
||
3 years ago
|
self.saved_grad: StatefulTensor = StatefulTensor(None, TensorState.FREE)
|
||
3 years ago
|
# This attribute must be initialized in ShardedModel
|
||
3 years ago
|
self.offload_grad: bool = False
|
||
3 years ago
|
|
||
|
# make sure the shared param is the only owner of payload
|
||
3 years ago
|
# The param.data maybe used to init the other part of the model.
|
||
|
# For example: File "resnet.py", line 190, in __init__
|
||
|
# nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
|
||
|
# So we can not empty the .data at this time
|
||
|
self.param = param
|
||
3 years ago
|
if set_data_none:
|
||
|
self.set_data_none()
|
||
3 years ago
|
|
||
3 years ago
|
def get_payload_tensors(self) -> List[StatefulTensor]:
|
||
|
"""returns stateful tensors kept by this class.
|
||
|
"""
|
||
3 years ago
|
return [self._sharded_data_tensor]
|
||
3 years ago
|
|
||
3 years ago
|
def set_data_none(self):
|
||
|
self.param.data = get_empty_tensor(self.sharded_data_tensor.device, self.sharded_data_tensor.dtype)
|
||
|
|
||
|
def set_grad_none(self):
|
||
|
self.saved_grad.set_null()
|
||
3 years ago
|
|
||
|
@property
|
||
3 years ago
|
def sharded_data_tensor(self):
|
||
|
return self._sharded_data_tensor
|
||
3 years ago
|
|
||
3 years ago
|
@property
|
||
|
def data_payload(self):
|
||
3 years ago
|
assert not self.sharded_data_tensor.is_null()
|
||
3 years ago
|
return self.sharded_data_tensor.payload
|
||
|
|
||
|
@property
|
||
|
def grad_payload(self):
|
||
|
assert not self.saved_grad.is_null()
|
||
|
return self.saved_grad.payload
|
||
|
|
||
3 years ago
|
@property
|
||
|
def param_is_sharded(self):
|
||
3 years ago
|
return self.sharded_data_tensor.is_sharded
|
||
|
|
||
3 years ago
|
def data_payload_reset(self, tensor: torch.Tensor):
|
||
3 years ago
|
assert type(tensor) is torch.Tensor
|
||
|
assert tensor.requires_grad is False
|
||
3 years ago
|
self.sharded_data_tensor.payload_reset(tensor)
|
||
3 years ago
|
|
||
3 years ago
|
def grad_payload_reset(self, tensor: torch.Tensor):
|
||
3 years ago
|
assert type(tensor) is torch.Tensor
|
||
|
assert tensor.requires_grad is False
|
||
3 years ago
|
self.saved_grad.payload_reset(tensor)
|
||
3 years ago
|
|
||
|
def get_memory_usage(self) -> Tuple[int, int]:
|
||
|
"""
|
||
|
get the memory usage of the param, including data and grad
|
||
|
Returns:
|
||
|
Tuple[int, int]: cuda mem usage in Byte, cpu memory usage in Byte
|
||
|
"""
|
||
|
cuda_mem_use, cpu_mem_use = 0, 0
|
||
|
|
||
|
def _update_mem_use(t: Optional[torch.Tensor]):
|
||
|
if t is None:
|
||
|
return
|
||
|
assert isinstance(t, torch.Tensor)
|
||
|
nonlocal cuda_mem_use
|
||
|
nonlocal cpu_mem_use
|
||
3 years ago
|
t_cuda, t_cpu = colo_tensor_mem_usage(t)
|
||
|
cuda_mem_use += t_cuda
|
||
|
cpu_mem_use += t_cpu
|
||
3 years ago
|
|
||
3 years ago
|
address_set = set()
|
||
3 years ago
|
_update_mem_use(self.data_payload)
|
||
|
address_set.add(self.data_payload.data_ptr())
|
||
3 years ago
|
|
||
3 years ago
|
if not self.saved_grad.is_null() and self.saved_grad.data_ptr() not in address_set:
|
||
3 years ago
|
_update_mem_use(self.grad_payload)
|
||
3 years ago
|
address_set.add(self.saved_grad.data_ptr())
|
||
3 years ago
|
|
||
|
if self.param.data is not None and self.param.data.data_ptr() not in address_set:
|
||
|
_update_mem_use(self.param.data)
|
||
|
address_set.add(self.param.data.data_ptr())
|
||
|
|
||
|
if self.param.grad is not None and self.param.grad.data_ptr() not in address_set:
|
||
|
_update_mem_use(self.param.grad)
|
||
3 years ago
|
|
||
|
return cuda_mem_use, cpu_mem_use
|