1# Copyright 2020 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import numpy as np 16 17import mindspore as ms 18import mindspore.nn as nn 19from mindspore import Tensor 20from mindspore import context 21from mindspore.common.api import _cell_graph_executor 22from mindspore.ops import composite as C 23from mindspore.ops import operations as P 24from tests.ut.python.ops.test_math_ops import VirtualLoss 25 26 27grad_all = C.GradOperation(get_all=True) 28 29 30class NetWithLoss(nn.Cell): 31 def __init__(self, network): 32 super(NetWithLoss, self).__init__() 33 self.loss = VirtualLoss() 34 self.network = network 35 36 def construct(self, x, y, b): 37 predict = self.network(x, y, b) 38 return self.loss(predict) 39 40 41class GradWrap(nn.Cell): 42 def __init__(self, network): 43 super(GradWrap, self).__init__() 44 self.network = network 45 46 def construct(self, x, y, b): 47 return grad_all(self.network)(x, y, b) 48 49 50def compile_net(net, x, y, b): 51 net.set_auto_parallel() 52 net.set_train() 53 _cell_graph_executor.compile(net, x, y, b) 54 55 56# it has not redistribution 57def test_tensoradd_reshape_matmul(): 58 class Net(nn.Cell): 59 def __init__(self, strategy1, strategy2): 60 super().__init__() 61 self.add = P.Add().shard(strategy1) 62 self.reshape = P.Reshape() 63 self.matmul = P.MatMul().shard(strategy2) 64 65 def construct(self, x, y, b): 66 out = self.add(x, y) 67 out = self.reshape(out, (256, 16)) 68 out = self.matmul(out, b) 69 return out 70 71 context.set_auto_parallel_context(device_num=64, global_rank=0, gradients_mean=True) 72 strategy1 = ((8, 1, 1), (8, 1, 1)) 73 strategy2 = ((8, 1), (1, 8)) 74 net = GradWrap(NetWithLoss(Net(strategy1, strategy2))) 75 context.set_auto_parallel_context(parallel_mode="semi_auto_parallel") 76 77 x = Tensor(np.ones([32, 8, 16]), dtype=ms.float32) 78 y = Tensor(np.ones([32, 8, 16]), dtype=ms.float32) 79 b = Tensor(np.ones([16, 16]), dtype=ms.float32) 80 81 compile_net(net, x, y, b) 82 83 84def test_two_matmul(): 85 class Net(nn.Cell): 86 def __init__(self, strategy1, strategy2): 87 super().__init__() 88 self.matmul1 = P.MatMul().shard(strategy1) 89 self.matmul2 = P.MatMul().shard(strategy2) 90 91 def construct(self, x, y, b): 92 out = self.matmul1(x, y) 93 out = self.matmul2(out, b) 94 return out 95 96 context.set_auto_parallel_context(device_num=64, global_rank=0, gradients_mean=True) 97 strategy1 = ((8, 8), (8, 1)) 98 strategy2 = ((8, 1), (1, 1)) 99 net = GradWrap(NetWithLoss(Net(strategy1, strategy2))) 100 context.set_auto_parallel_context(parallel_mode="semi_auto_parallel") 101 102 x = Tensor(np.ones([128, 32]), dtype=ms.float32) 103 y = Tensor(np.ones([32, 64]), dtype=ms.float32) 104 b = Tensor(np.ones([64, 64]), dtype=ms.float32) 105 106 compile_net(net, x, y, b) 107