|
|
@ -23,35 +23,41 @@ from mindspore.ops import functional as F, composite as C
|
|
|
|
import mindspore.context as context
|
|
|
|
import mindspore.context as context
|
|
|
|
import pytest
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TensorIntAutoCast(nn.Cell):
|
|
|
|
class TensorIntAutoCast(nn.Cell):
|
|
|
|
def __init__(self,):
|
|
|
|
def __init__(self, ):
|
|
|
|
super(TensorIntAutoCast, self).__init__()
|
|
|
|
super(TensorIntAutoCast, self).__init__()
|
|
|
|
self.i = 2
|
|
|
|
self.i = 2
|
|
|
|
|
|
|
|
|
|
|
|
def construct(self, t):
|
|
|
|
def construct(self, t):
|
|
|
|
z = F.tensor_mul(t, self.i)
|
|
|
|
z = F.tensor_mul(t, self.i)
|
|
|
|
return z
|
|
|
|
return z
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TensorFPAutoCast(nn.Cell):
|
|
|
|
class TensorFPAutoCast(nn.Cell):
|
|
|
|
def __init__(self,):
|
|
|
|
def __init__(self, ):
|
|
|
|
super(TensorFPAutoCast, self).__init__()
|
|
|
|
super(TensorFPAutoCast, self).__init__()
|
|
|
|
self.f = 1.2
|
|
|
|
self.f = 1.2
|
|
|
|
|
|
|
|
|
|
|
|
def construct(self, t):
|
|
|
|
def construct(self, t):
|
|
|
|
z = F.tensor_mul(t, self.f)
|
|
|
|
z = F.tensor_mul(t, self.f)
|
|
|
|
return z
|
|
|
|
return z
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TensorBoolAutoCast(nn.Cell):
|
|
|
|
class TensorBoolAutoCast(nn.Cell):
|
|
|
|
def __init__(self,):
|
|
|
|
def __init__(self, ):
|
|
|
|
super(TensorBoolAutoCast, self).__init__()
|
|
|
|
super(TensorBoolAutoCast, self).__init__()
|
|
|
|
self.f = True
|
|
|
|
self.f = True
|
|
|
|
|
|
|
|
|
|
|
|
def construct(self, t):
|
|
|
|
def construct(self, t):
|
|
|
|
z = F.tensor_mul(t, self.f)
|
|
|
|
z = F.tensor_mul(t, self.f)
|
|
|
|
return z
|
|
|
|
return z
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TensorAutoCast(nn.Cell):
|
|
|
|
class TensorAutoCast(nn.Cell):
|
|
|
|
def __init__(self,):
|
|
|
|
def __init__(self, ):
|
|
|
|
super(TensorAutoCast, self).__init__()
|
|
|
|
super(TensorAutoCast, self).__init__()
|
|
|
|
|
|
|
|
|
|
|
|
def construct(self, t1, t2):
|
|
|
|
def construct(self, t1, t2):
|
|
|
|
z = F.tensor_mul(t1, t2)
|
|
|
|
z = F.tensor_mul(t1, t2)
|
|
|
|
return z
|
|
|
|
return z
|
|
|
@ -68,7 +74,7 @@ def test_tensor_auto_cast():
|
|
|
|
t_fp16 = Tensor(np.ones([2, 1, 2, 2]), mstype.float16)
|
|
|
|
t_fp16 = Tensor(np.ones([2, 1, 2, 2]), mstype.float16)
|
|
|
|
t_fp32 = Tensor(np.ones([2, 1, 2, 2]), mstype.float32)
|
|
|
|
t_fp32 = Tensor(np.ones([2, 1, 2, 2]), mstype.float32)
|
|
|
|
t_fp64 = Tensor(np.ones([2, 1, 2, 2]), mstype.float64)
|
|
|
|
t_fp64 = Tensor(np.ones([2, 1, 2, 2]), mstype.float64)
|
|
|
|
net = TensorAutoCast()
|
|
|
|
net = TensorAutoCast()
|
|
|
|
rs = net(t_uint8, t_int8)
|
|
|
|
rs = net(t_uint8, t_int8)
|
|
|
|
assert rs.dtype() == mstype.int16
|
|
|
|
assert rs.dtype() == mstype.int16
|
|
|
|
rs = net(t_uint8, t_int16)
|
|
|
|
rs = net(t_uint8, t_int16)
|
|
|
@ -96,7 +102,7 @@ def test_tensor_auto_cast():
|
|
|
|
assert rs.dtype() == mstype.float64
|
|
|
|
assert rs.dtype() == mstype.float64
|
|
|
|
rs = net(t_fp32, t_fp64)
|
|
|
|
rs = net(t_fp32, t_fp64)
|
|
|
|
assert rs.dtype() == mstype.float64
|
|
|
|
assert rs.dtype() == mstype.float64
|
|
|
|
|
|
|
|
|
|
|
|
rs = net(t_uint8, t_fp16)
|
|
|
|
rs = net(t_uint8, t_fp16)
|
|
|
|
assert rs.dtype() == mstype.float16
|
|
|
|
assert rs.dtype() == mstype.float16
|
|
|
|
rs = net(t_uint8, t_fp32)
|
|
|
|
rs = net(t_uint8, t_fp32)
|
|
|
@ -210,7 +216,6 @@ def test_tensor_auto_cast():
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
net(t_uint64, t_fp64)
|
|
|
|
net(t_uint64, t_fp64)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
tfp(t_uint16)
|
|
|
|
tfp(t_uint16)
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|