|
|
|
@ -24,6 +24,17 @@ from mindspore.ops import operations as P
|
|
|
|
|
from tests.ut.python.ops.test_math_ops import VirtualLoss
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NetWithLossNoBias(nn.Cell):
|
|
|
|
|
def __init__(self, network):
|
|
|
|
|
super(NetWithLossNoBias, self).__init__()
|
|
|
|
|
self.loss = VirtualLoss()
|
|
|
|
|
self.network = network
|
|
|
|
|
|
|
|
|
|
def construct(self, x, y):
|
|
|
|
|
predict = self.network(x, y)
|
|
|
|
|
return self.loss(predict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NetWithLoss(nn.Cell):
|
|
|
|
|
def __init__(self, network):
|
|
|
|
|
super(NetWithLoss, self).__init__()
|
|
|
|
@ -35,6 +46,15 @@ class NetWithLoss(nn.Cell):
|
|
|
|
|
return self.loss(predict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GradWrapNoBias(nn.Cell):
|
|
|
|
|
def __init__(self, network):
|
|
|
|
|
super(GradWrapNoBias, self).__init__()
|
|
|
|
|
self.network = network
|
|
|
|
|
|
|
|
|
|
def construct(self, x, y):
|
|
|
|
|
return C.grad_all(self.network)(x, y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GradWrap(nn.Cell):
|
|
|
|
|
def __init__(self, network):
|
|
|
|
|
super(GradWrap, self).__init__()
|
|
|
|
@ -44,6 +64,11 @@ class GradWrap(nn.Cell):
|
|
|
|
|
return C.grad_all(self.network)(x, y, b)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def compile_net_no_bias(net, x, y):
|
|
|
|
|
net.set_auto_parallel()
|
|
|
|
|
_executor.compile(net, x, y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def compile_net(net, x, y, b):
|
|
|
|
|
net.set_auto_parallel()
|
|
|
|
|
_executor.compile(net, x, y, b)
|
|
|
|
@ -165,7 +190,7 @@ def test_sum_mul5():
|
|
|
|
|
self.mul1 = P.Mul().set_strategy(strategy1)
|
|
|
|
|
self.reduce_sum = P.ReduceSum(keep_dims=True).set_strategy(strategy2)
|
|
|
|
|
|
|
|
|
|
def construct(self, x, y, b):
|
|
|
|
|
def construct(self, x, y):
|
|
|
|
|
out = self.mul1(x, y)
|
|
|
|
|
out = self.reduce_sum(out, 0)
|
|
|
|
|
return out
|
|
|
|
@ -173,13 +198,12 @@ def test_sum_mul5():
|
|
|
|
|
context.set_auto_parallel_context(device_num=64, global_rank=0)
|
|
|
|
|
strategy1 = ((1, 8, 8), (1, 8, 8))
|
|
|
|
|
strategy2 = ((2, 4, 1),)
|
|
|
|
|
net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
|
|
|
|
|
net = GradWrapNoBias(NetWithLossNoBias(Net(strategy1, strategy2)))
|
|
|
|
|
context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
|
|
|
|
|
|
|
|
|
|
x = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
|
|
|
|
|
y = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
|
|
|
|
|
b = Tensor(np.ones([1, 32, 64]), dtype=ms.float32)
|
|
|
|
|
compile_net(net, x, y, b)
|
|
|
|
|
compile_net_no_bias(net, x, y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sum_mul6():
|
|
|
|
@ -189,7 +213,7 @@ def test_sum_mul6():
|
|
|
|
|
self.mul1 = P.Mul().set_strategy(strategy1)
|
|
|
|
|
self.reduce_sum = P.ReduceSum(keep_dims=True).set_strategy(strategy2)
|
|
|
|
|
|
|
|
|
|
def construct(self, x, y, b):
|
|
|
|
|
def construct(self, x, y):
|
|
|
|
|
out = self.mul1(x, y)
|
|
|
|
|
out = self.reduce_sum(out, 1)
|
|
|
|
|
return out
|
|
|
|
@ -197,13 +221,12 @@ def test_sum_mul6():
|
|
|
|
|
context.set_auto_parallel_context(device_num=64, global_rank=0)
|
|
|
|
|
strategy1 = ((1, 8, 8), (1, 8, 8))
|
|
|
|
|
strategy2 = ((2, 1, 4),)
|
|
|
|
|
net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
|
|
|
|
|
net = GradWrapNoBias(NetWithLossNoBias(Net(strategy1, strategy2)))
|
|
|
|
|
context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
|
|
|
|
|
|
|
|
|
|
x = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
|
|
|
|
|
y = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
|
|
|
|
|
b = Tensor(np.ones([128, 1, 64]), dtype=ms.float32)
|
|
|
|
|
compile_net(net, x, y, b)
|
|
|
|
|
compile_net_no_bias(net, x, y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sum_mul7():
|
|
|
|
@ -213,7 +236,7 @@ def test_sum_mul7():
|
|
|
|
|
self.mul1 = P.Mul().set_strategy(strategy1)
|
|
|
|
|
self.reduce_sum = P.ReduceSum(keep_dims=True).set_strategy(strategy2)
|
|
|
|
|
|
|
|
|
|
def construct(self, x, y, b):
|
|
|
|
|
def construct(self, x, y):
|
|
|
|
|
out = self.mul1(x, y)
|
|
|
|
|
out = self.reduce_sum(out, (0, 1))
|
|
|
|
|
return out
|
|
|
|
@ -221,13 +244,12 @@ def test_sum_mul7():
|
|
|
|
|
context.set_auto_parallel_context(device_num=64, global_rank=0)
|
|
|
|
|
strategy1 = ((1, 8, 8), (1, 8, 8))
|
|
|
|
|
strategy2 = ((2, 4, 1),)
|
|
|
|
|
net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
|
|
|
|
|
net = GradWrapNoBias(NetWithLossNoBias(Net(strategy1, strategy2)))
|
|
|
|
|
context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
|
|
|
|
|
|
|
|
|
|
x = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
|
|
|
|
|
y = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
|
|
|
|
|
b = Tensor(np.ones([1, 64]), dtype=ms.float32)
|
|
|
|
|
compile_net(net, x, y, b)
|
|
|
|
|
compile_net_no_bias(net, x, y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_max_mul():
|
|
|
|
@ -347,6 +369,12 @@ def gen_inputs_and_compile_net(net):
|
|
|
|
|
compile_net(net, x, y, b)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def gen_inputs_and_compile_net_no_bias(net):
|
|
|
|
|
x = Tensor(np.ones([128, 64, 64]), dtype=ms.float32)
|
|
|
|
|
y = Tensor(np.ones([128, 64, 64]), dtype=ms.float32)
|
|
|
|
|
compile_net_no_bias(net, x, y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def tobefixed_test_arg_max_with_value_mul_semi_axis_parallel():
|
|
|
|
|
context.set_auto_parallel_context(device_num=8, global_rank=0)
|
|
|
|
|
strategy1 = ((1, 4, 2), (1, 4, 2))
|
|
|
|
@ -414,7 +442,7 @@ class ArgMinWithValueNet2(nn.Cell):
|
|
|
|
|
self.arg_min_with_value = P.ArgMinWithValue(keep_dims=True, axis=-1).set_strategy(strategy2)
|
|
|
|
|
self.relu = P.ReLU().set_strategy(strategy3)
|
|
|
|
|
|
|
|
|
|
def construct(self, x, y, b):
|
|
|
|
|
def construct(self, x, y):
|
|
|
|
|
out = self.mul1(x, y)
|
|
|
|
|
_, out = self.arg_min_with_value(out)
|
|
|
|
|
out = self.relu(out)
|
|
|
|
@ -426,9 +454,9 @@ def tobefixed_test_arg_min_with_value_mul_semi_axis_parallel2():
|
|
|
|
|
strategy1 = ((1, 4, 2), (1, 4, 2))
|
|
|
|
|
strategy2 = ((4, 1, 2),)
|
|
|
|
|
strategy3 = ((2, 4, 1),)
|
|
|
|
|
net = GradWrap(NetWithLoss(ArgMinWithValueNet2(strategy1, strategy2, strategy3)))
|
|
|
|
|
net = GradWrapNoBias(NetWithLossNoBias(ArgMinWithValueNet2(strategy1, strategy2, strategy3)))
|
|
|
|
|
context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
|
|
|
|
|
gen_inputs_and_compile_net(net)
|
|
|
|
|
gen_inputs_and_compile_net_no_bias(net)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_arg_min_with_value_mul_semi2():
|
|
|
|
@ -436,9 +464,9 @@ def test_arg_min_with_value_mul_semi2():
|
|
|
|
|
strategy1 = ((1, 4, 2), (1, 4, 2))
|
|
|
|
|
strategy2 = ((4, 1, 1),)
|
|
|
|
|
strategy3 = ((2, 4, 1),)
|
|
|
|
|
net = GradWrap(NetWithLoss(ArgMinWithValueNet2(strategy1, strategy2, strategy3)))
|
|
|
|
|
net = GradWrapNoBias(NetWithLossNoBias(ArgMinWithValueNet2(strategy1, strategy2, strategy3)))
|
|
|
|
|
context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
|
|
|
|
|
gen_inputs_and_compile_net(net)
|
|
|
|
|
gen_inputs_and_compile_net_no_bias(net)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_arg_min_with_value_mul_auto2():
|
|
|
|
@ -446,9 +474,9 @@ def test_arg_min_with_value_mul_auto2():
|
|
|
|
|
strategy1 = None
|
|
|
|
|
strategy2 = None
|
|
|
|
|
strategy3 = None
|
|
|
|
|
net = GradWrap(NetWithLoss(ArgMinWithValueNet2(strategy1, strategy2, strategy3)))
|
|
|
|
|
net = GradWrapNoBias(NetWithLossNoBias(ArgMinWithValueNet2(strategy1, strategy2, strategy3)))
|
|
|
|
|
context.set_auto_parallel_context(parallel_mode="auto_parallel")
|
|
|
|
|
gen_inputs_and_compile_net(net)
|
|
|
|
|
gen_inputs_and_compile_net_no_bias(net)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cross_batch():
|
|
|
|
@ -459,7 +487,7 @@ def test_cross_batch():
|
|
|
|
|
self.reduce_sum = P.ReduceSum(keep_dims=False).set_strategy(strategy2)
|
|
|
|
|
self.reduce_mean = P.ReduceMean(keep_dims=False).set_strategy(strategy3).add_prim_attr("cross_batch", True)
|
|
|
|
|
|
|
|
|
|
def construct(self, x, y, b):
|
|
|
|
|
def construct(self, x, y):
|
|
|
|
|
out = self.mul1(x, y)
|
|
|
|
|
out = self.reduce_sum(out, -1)
|
|
|
|
|
out = self.reduce_mean(out, 0)
|
|
|
|
@ -469,13 +497,12 @@ def test_cross_batch():
|
|
|
|
|
strategy1 = ((4, 2), (4, 2))
|
|
|
|
|
strategy2 = ((2, 1),)
|
|
|
|
|
strategy3 = ((8,),)
|
|
|
|
|
net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
|
|
|
|
|
net = GradWrapNoBias(NetWithLossNoBias(Net(strategy1, strategy2, strategy3)))
|
|
|
|
|
context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
|
|
|
|
|
|
|
|
|
|
x = Tensor(np.ones([32, 64]), dtype=ms.float32)
|
|
|
|
|
y = Tensor(np.ones([32, 64]), dtype=ms.float32)
|
|
|
|
|
b = Tensor(np.ones([32, 64]), dtype=ms.float32)
|
|
|
|
|
compile_net(net, x, y, b)
|
|
|
|
|
compile_net_no_bias(net, x, y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cross_batch2():
|
|
|
|
@ -486,7 +513,7 @@ def test_cross_batch2():
|
|
|
|
|
self.reduce_mean = P.ReduceMean(keep_dims=False).set_strategy(strategy2)
|
|
|
|
|
self.reduce_sum = P.ReduceSum(keep_dims=False).set_strategy(strategy3).add_prim_attr("cross_batch", True)
|
|
|
|
|
|
|
|
|
|
def construct(self, x, y, b):
|
|
|
|
|
def construct(self, x, y):
|
|
|
|
|
out = self.mul1(x, y)
|
|
|
|
|
out = self.reduce_mean(out, -1)
|
|
|
|
|
out = self.reduce_sum(out, 0)
|
|
|
|
@ -496,13 +523,12 @@ def test_cross_batch2():
|
|
|
|
|
strategy1 = ((4, 2), (4, 2))
|
|
|
|
|
strategy2 = ((2, 1),)
|
|
|
|
|
strategy3 = ((8,),)
|
|
|
|
|
net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
|
|
|
|
|
net = GradWrapNoBias(NetWithLossNoBias(Net(strategy1, strategy2, strategy3)))
|
|
|
|
|
context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
|
|
|
|
|
|
|
|
|
|
x = Tensor(np.ones([32, 64]), dtype=ms.float32)
|
|
|
|
|
y = Tensor(np.ones([32, 64]), dtype=ms.float32)
|
|
|
|
|
b = Tensor(np.ones([32, 64]), dtype=ms.float32)
|
|
|
|
|
compile_net(net, x, y, b)
|
|
|
|
|
compile_net_no_bias(net, x, y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cross_batch_auto():
|
|
|
|
@ -513,20 +539,19 @@ def test_cross_batch_auto():
|
|
|
|
|
self.reduce_mean = P.ReduceMean(keep_dims=False)
|
|
|
|
|
self.reduce_sum = P.ReduceSum(keep_dims=False).add_prim_attr("cross_batch", True)
|
|
|
|
|
|
|
|
|
|
def construct(self, x, y, b):
|
|
|
|
|
def construct(self, x, y):
|
|
|
|
|
out = self.mul1(x, y)
|
|
|
|
|
out = self.reduce_mean(out, -1)
|
|
|
|
|
out = self.reduce_sum(out, 0)
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
context.set_auto_parallel_context(device_num=8, global_rank=0)
|
|
|
|
|
net = GradWrap(NetWithLoss(Net()))
|
|
|
|
|
net = GradWrapNoBias(NetWithLossNoBias(Net()))
|
|
|
|
|
context.set_auto_parallel_context(parallel_mode="auto_parallel")
|
|
|
|
|
|
|
|
|
|
x = Tensor(np.ones([32, 64]), dtype=ms.float32)
|
|
|
|
|
y = Tensor(np.ones([32, 64]), dtype=ms.float32)
|
|
|
|
|
b = Tensor(np.ones([32, 64]), dtype=ms.float32)
|
|
|
|
|
compile_net(net, x, y, b)
|
|
|
|
|
compile_net_no_bias(net, x, y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_max_empty_tuple():
|
|
|
|
|