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, Parameter, ParameterTuple 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_by_list = C.GradOperation(get_by_list=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, b): 36 predict = self.network(x) 37 return self.loss(predict, b)[0] 38 39 40class OneStepCell(nn.Cell): 41 def __init__(self, network): 42 super(OneStepCell, self).__init__(auto_prefix=False) 43 self.network = network 44 self.weights = ParameterTuple(network.network.trainable_params()) 45 46 def construct(self, data, label): 47 weights = self.weights 48 grads = grad_by_list(self.network, weights)(data, label) 49 return grads 50 51 52def test_one_weight_parameter(): 53 class Net(nn.Cell): 54 def __init__(self, strategy1, weight): 55 super().__init__() 56 self.weight = Parameter(weight, "w1", requires_grad=True) 57 self.matmul = P.MatMul().shard(strategy1) 58 59 def construct(self, x): 60 out = self.matmul(x, self.weight) 61 return out 62 63 context.set_auto_parallel_context(device_num=8, global_rank=0) 64 strategy1 = ((4, 1), (1, 2)) 65 strategy3 = ((8, 1), (8, 1)) 66 67 x = Tensor(np.ones([64, 32]), dtype=ms.float32) 68 weight = Tensor(np.ones([32, 64]), dtype=ms.float32) 69 b = Tensor(np.ones([64, 64]), dtype=ms.float32) 70 71 net = Net(strategy1, weight) 72 73 net_with_loss = NetWithLoss(net, strategy3) 74 75 train_net = OneStepCell(net_with_loss) 76 context.set_auto_parallel_context(parallel_mode="semi_auto_parallel") 77 train_net.set_auto_parallel() 78 79 train_net.set_train() 80 _cell_graph_executor.compile(train_net, x, b) 81