|
|
@ -1,8 +1,11 @@
|
|
|
|
import math
|
|
|
|
import operator
|
|
|
|
|
|
|
|
from functools import reduce
|
|
|
|
|
|
|
|
from typing import Optional, Union
|
|
|
|
|
|
|
|
|
|
|
|
import torch
|
|
|
|
import torch
|
|
|
|
from colossalai.tensor.op_wrapper import colo_op_impl
|
|
|
|
|
|
|
|
from colossalai.tensor import ColoTensor, ColoTensorSpec, ReplicaSpec
|
|
|
|
from colossalai.tensor import ColoTensor, ColoTensorSpec, ReplicaSpec
|
|
|
|
from typing import Optional, Union
|
|
|
|
from colossalai.tensor.op_wrapper import colo_op_impl
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _all_int(my_iter):
|
|
|
|
def _all_int(my_iter):
|
|
|
@ -37,8 +40,8 @@ def _shape_infer(org_sp, tgt_sp):
|
|
|
|
if cnt > 1:
|
|
|
|
if cnt > 1:
|
|
|
|
raise RuntimeError("only one dimension can be inferred")
|
|
|
|
raise RuntimeError("only one dimension can be inferred")
|
|
|
|
|
|
|
|
|
|
|
|
org_prod = math.prod(org_sp)
|
|
|
|
org_prod = reduce(operator.mul, org_sp, 1)
|
|
|
|
tgt_prod = math.prod(tgt_sp)
|
|
|
|
tgt_prod = reduce(operator.mul, tgt_sp, 1)
|
|
|
|
|
|
|
|
|
|
|
|
if cnt == 0:
|
|
|
|
if cnt == 0:
|
|
|
|
if org_prod != tgt_prod:
|
|
|
|
if org_prod != tgt_prod:
|
|
|
@ -49,7 +52,7 @@ def _shape_infer(org_sp, tgt_sp):
|
|
|
|
raise RuntimeError("shape '{}' is invalid for input of size {}".format(tgt_sp, org_prod))
|
|
|
|
raise RuntimeError("shape '{}' is invalid for input of size {}".format(tgt_sp, org_prod))
|
|
|
|
|
|
|
|
|
|
|
|
infer_dim = -(org_prod // tgt_prod)
|
|
|
|
infer_dim = -(org_prod // tgt_prod)
|
|
|
|
return tgt_sp[: pos] + (infer_dim,) + tgt_sp[pos + 1:]
|
|
|
|
return tgt_sp[:pos] + (infer_dim,) + tgt_sp[pos + 1:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@colo_op_impl(torch.Tensor.view)
|
|
|
|
@colo_op_impl(torch.Tensor.view)
|
|
|
@ -77,15 +80,11 @@ def colo_view(self: ColoTensor, *shape) -> 'ColoTensor':
|
|
|
|
res = self.view(*new_shape)
|
|
|
|
res = self.view(*new_shape)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
replicated_t = self.redistribute(dist_spec=ReplicaSpec())
|
|
|
|
replicated_t = self.redistribute(dist_spec=ReplicaSpec())
|
|
|
|
return ColoTensor.from_torch_tensor(
|
|
|
|
return ColoTensor.from_torch_tensor(tensor=replicated_t.view(*shape),
|
|
|
|
tensor=replicated_t.view(*shape),
|
|
|
|
spec=ColoTensorSpec(self.get_process_group()))
|
|
|
|
spec=ColoTensorSpec(self.get_process_group()))
|
|
|
|
|
|
|
|
|
|
|
|
return ColoTensor.from_torch_tensor(tensor=res,
|
|
|
|
return ColoTensor.from_torch_tensor(
|
|
|
|
spec=ColoTensorSpec(pg=self.get_process_group(), dist_attr=self.dist_spec))
|
|
|
|
tensor=res,
|
|
|
|
|
|
|
|
spec=ColoTensorSpec(
|
|
|
|
|
|
|
|
pg=self.get_process_group(),
|
|
|
|
|
|
|
|
dist_attr=self.dist_spec))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@colo_op_impl(torch.Tensor.size)
|
|
|
|
@colo_op_impl(torch.Tensor.size)
|
|
|
|