1# Copyright 2019 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): 37 predict = self.network(x, y) 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): 47 return grad_all(self.network)(x, y) 48 49 50def compile_net(net, x, y): 51 net.set_auto_parallel() 52 net.set_train() 53 _cell_graph_executor.compile(net, x, y) 54 55 56def test_prelu_single_success1(): 57 class Net(nn.Cell): 58 def __init__(self): 59 super().__init__() 60 self.prelu = P.PReLU() 61 62 def construct(self, x, y): 63 out = self.prelu(x, y) 64 return out 65 66 context.reset_auto_parallel_context() 67 net = GradWrap(NetWithLoss(Net())) 68 x = Tensor(np.random.rand(1, 33, 4, 4), ms.float32) 69 w = Tensor(np.random.rand(33), ms.float32) 70 compile_net(net, x, w) 71 72 73def test_prelu_single_success2(): 74 class Net(nn.Cell): 75 def __init__(self): 76 super().__init__() 77 self.prelu = P.PReLU() 78 79 def construct(self, x, y): 80 out = self.prelu(x, y) 81 return out 82 83 context.reset_auto_parallel_context() 84 net = GradWrap(NetWithLoss(Net())) 85 x = Tensor(np.random.rand(1, 33, 4, 4), ms.float32) 86 w = Tensor([0.1], ms.float32) 87 compile_net(net, x, w) 88 89 90def test_prelu_parallel_success1(): 91 class Net(nn.Cell): 92 def __init__(self, strategy): 93 super().__init__() 94 self.prelu = P.PReLU().shard(strategy) 95 96 def construct(self, x, y): 97 out = self.prelu(x, y) 98 return out 99 100 context.reset_auto_parallel_context() 101 context.set_auto_parallel_context(device_num=8, global_rank=0) 102 context.set_auto_parallel_context(parallel_mode="semi_auto_parallel") 103 strategy = ((1, 1, 1, 1), (1,)) 104 x = Tensor(np.random.rand(4, 4, 32, 64), dtype=ms.float32) 105 w = Tensor(np.random.rand(4), dtype=ms.float32) 106 net = GradWrap(NetWithLoss(Net(strategy))) 107 compile_net(net, x, w) 108 109 110def test_prelu_parallel_success2(): 111 class Net(nn.Cell): 112 def __init__(self, strategy): 113 super().__init__() 114 self.prelu = P.PReLU().shard(strategy) 115 116 def construct(self, x, y): 117 out = self.prelu(x, y) 118 return out 119 120 context.reset_auto_parallel_context() 121 context.set_auto_parallel_context(device_num=64, global_rank=0) 122 context.set_auto_parallel_context(parallel_mode="semi_auto_parallel") 123 strategy = ((2, 1, 4, 8), (1,)) 124 x = Tensor(np.random.rand(4, 4, 32, 64), dtype=ms.float32) 125 w = Tensor(np.random.rand(4), dtype=ms.float32) 126 net = GradWrap(NetWithLoss(Net(strategy))) 127 compile_net(net, x, w) 128 129 130def test_prelu_parallel_success3(): 131 class NetWithLoss3(nn.Cell): 132 def __init__(self, network): 133 super(NetWithLoss3, self).__init__() 134 self.loss = VirtualLoss() 135 self.network = network 136 137 def construct(self, x, y, w): 138 predict = self.network(x, y, w) 139 return self.loss(predict) 140 141 class GradWrap3(nn.Cell): 142 def __init__(self, network): 143 super(GradWrap3, self).__init__() 144 self.network = network 145 146 def construct(self, x, y, w): 147 return grad_all(self.network)(x, y, w) 148 149 class Net(nn.Cell): 150 def __init__(self, strategy1, strategy2): 151 super().__init__() 152 self.matmul = P.MatMul().shard(strategy1) 153 self.prelu = P.PReLU().shard(strategy2) 154 155 def construct(self, x, y, w): 156 out = self.matmul(x, y) 157 out = self.prelu(out, w) 158 return out 159 160 context.reset_auto_parallel_context() 161 context.set_auto_parallel_context(device_num=64, global_rank=0) 162 context.set_auto_parallel_context(parallel_mode="semi_auto_parallel") 163 strategy1 = ((2, 4), (4, 2)) 164 strategy2 = ((32, 1), (1,)) 165 x = Tensor(np.random.rand(128, 64), dtype=ms.float32) 166 y = Tensor(np.random.rand(64, 16), dtype=ms.float32) 167 w = Tensor(np.random.rand(16), dtype=ms.float32) 168 net = GradWrap3(NetWithLoss3(Net(strategy1, strategy2))) 169 net.set_auto_parallel() 170 net.set_train() 171 _cell_graph_executor.compile(net, x, y, w) 172 173 174def test_prelu_parallel_success4(): 175 class Net(nn.Cell): 176 def __init__(self, strategy): 177 super().__init__() 178 self.prelu = P.PReLU().shard(strategy) 179 180 def construct(self, x, y): 181 out = self.prelu(x, y) 182 return out 183 184 context.reset_auto_parallel_context() 185 context.set_auto_parallel_context(device_num=64, global_rank=0) 186 context.set_auto_parallel_context(parallel_mode="semi_auto_parallel") 187 strategy = ((2, 4, 4, 2), (4,)) 188 x = Tensor(np.random.rand(4, 16, 32, 64), dtype=ms.float32) 189 w = Tensor(np.random.rand(16), dtype=ms.float32) 190 net = GradWrap(NetWithLoss(Net(strategy))) 191 compile_net(net, x, w) 192 193 194def test_prelu_parallel_success5(): 195 class Net(nn.Cell): 196 def __init__(self, strategy): 197 super().__init__() 198 self.prelu = P.PReLU().shard(strategy) 199 200 def construct(self, x, y): 201 out = self.prelu(x, y) 202 return out 203 204 context.reset_auto_parallel_context() 205 context.set_auto_parallel_context(device_num=64, global_rank=0) 206 context.set_auto_parallel_context(parallel_mode="semi_auto_parallel") 207 strategy = ((2, 4, 4, 2), (1,)) 208 x = Tensor(np.random.rand(4, 16, 32, 64), dtype=ms.float32) 209 w = Tensor(np.random.rand(1), dtype=ms.float32) 210 net = GradWrap(NetWithLoss(Net(strategy))) 211 compile_net(net, x, w) 212