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 24 25 26grad_all = C.GradOperation(get_all=True) 27 28 29class NetWithLoss(nn.Cell): 30 def __init__(self, network, strategy3): 31 super(NetWithLoss, self).__init__() 32 self.loss = P.SoftmaxCrossEntropyWithLogits().shard(strategy3) 33 self.network = network 34 35 def construct(self, x, y, bias, label): 36 predict = self.network(x, y, bias) 37 return self.loss(predict, label)[0] 38 39 40class GradWrap(nn.Cell): 41 def __init__(self, network): 42 super(GradWrap, self).__init__() 43 self.network = network 44 45 def construct(self, x, y, bias, label): 46 return grad_all(self.network)(x, y, bias, label) 47 48 49def test_linear(): 50 class Net(nn.Cell): 51 def __init__(self, strategy0, strategy1, strategy2): 52 super().__init__() 53 self.fc_nobias = P.MatMul(transpose_b=True).shard(strategy0) 54 self.add = P.Add().shard(strategy1) 55 self.gelu = P.GeLU().shard(strategy2) 56 57 def construct(self, x, y, bias): 58 out = self.fc_nobias(x, y) 59 out = self.add(out, bias) 60 out = self.gelu(out) 61 return out 62 63 context.set_auto_parallel_context(device_num=16, global_rank=0) 64 context.set_auto_parallel_context(parallel_mode="semi_auto_parallel") 65 strategy0 = ((2, 4), (2, 4)) 66 strategy1 = ((2, 4), (4,)) 67 strategy2 = ((2, 8),) 68 strategy3 = ((16, 1), (16, 1)) 69 net = GradWrap(NetWithLoss(Net(strategy0, strategy1, strategy2), strategy3)) 70 net.set_auto_parallel() 71 72 x = Tensor(np.ones([64, 32]), dtype=ms.float32) 73 y = Tensor(np.ones([64, 32]), dtype=ms.float32) 74 bias = Tensor(np.ones([64]), dtype=ms.float32) 75 label = Tensor(np.ones([64, 64]), dtype=ms.float32) 76 net.set_train() 77 _cell_graph_executor.compile(net, x, y, bias, label) 78