2022-05-06 04:57:14 +00:00
|
|
|
from .colo_tensor import ColoTensor
|
|
|
|
from .const import TensorType
|
|
|
|
import torch
|
2022-05-19 04:44:59 +00:00
|
|
|
from colossalai.tensor import TensorSpec, distspec
|
|
|
|
from copy import copy
|
2022-05-06 04:57:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ColoParameter(ColoTensor):
|
|
|
|
r"""A kind of ColoTensor to be considered as a module parameter.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2022-05-19 04:44:59 +00:00
|
|
|
def __new__(cls,
|
|
|
|
data: torch.Tensor,
|
|
|
|
requires_grad: bool = True,
|
|
|
|
spec: TensorSpec = TensorSpec(distspec.replicate())) -> 'ColoParameter':
|
|
|
|
if data is None:
|
|
|
|
data = torch.empty(0)
|
|
|
|
return torch.Tensor._make_subclass(cls, data, requires_grad)
|
2022-05-06 04:57:14 +00:00
|
|
|
|
2022-05-19 04:44:59 +00:00
|
|
|
def __init__(self,
|
|
|
|
data: torch.Tensor,
|
|
|
|
requires_grad: bool = True,
|
|
|
|
spec: TensorSpec = TensorSpec(distspec.replicate())) -> None:
|
|
|
|
self._spec = copy(spec)
|
|
|
|
self._type = TensorType.MODEL
|
|
|
|
self._graph_node = None
|
2022-05-06 04:57:14 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-05-19 04:44:59 +00:00
|
|
|
def from_torch_tensor(tensor: torch.Tensor,
|
|
|
|
requires_grad: bool = True,
|
|
|
|
spec: TensorSpec = TensorSpec(distspec.replicate())) -> 'ColoParameter':
|
|
|
|
tensor = tensor.as_subclass(ColoParameter)
|
|
|
|
tensor.__init__(tensor, requires_grad=requires_grad, spec=spec)
|
|
|
|
return tensor
|
2022-05-20 12:19:58 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f'ColoParameter: {torch.Tensor.__repr__(self)}'
|