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 20from mindspore import context 21from mindspore.common.api import _CellGraphExecutor 22from mindspore.nn import TrainOneStepCell 23from mindspore.nn.optim import AdamWeightDecay 24from mindspore.ops import operations as P 25 26 27class NetWithLoss(nn.Cell): 28 def __init__(self, network, strategy3): 29 super(NetWithLoss, self).__init__() 30 self.loss = P.SoftmaxCrossEntropyWithLogits().shard(strategy3) 31 self.network = network 32 33 def construct(self, x, b): 34 predict = self.network(x) 35 return self.loss(predict, b)[0] 36 37 38def compile_net(net, x, b): 39 net.set_auto_parallel() 40 _CellGraphExecutor().compile(net, x, b) 41 42 43def test_optimizer_clone_weight(): 44 class Net(nn.Cell): 45 def __init__(self, strategy1, strategy2, weight): 46 super().__init__() 47 self.weight = Parameter(weight, "w1") 48 self.matmul = P.MatMul(transpose_a=False, transpose_b=True).shard(strategy1) 49 self.relu = P.ReLU().shard(strategy2) 50 51 def construct(self, x): 52 out = self.matmul(x, self.weight) 53 out = self.relu(out) 54 return out 55 56 context.set_auto_parallel_context(device_num=4, global_rank=0) 57 context.set_auto_parallel_context(parallel_mode="semi_auto_parallel") 58 59 strategy1 = ((2, 1), (2, 1)) 60 strategy2 = ((4, 1),) 61 strategy3 = ((4, 1), (4, 1)) 62 63 x = Tensor(np.ones([64, 32]), dtype=ms.float32) 64 weight = Tensor(np.ones([64, 32]), dtype=ms.float32) 65 b = Tensor(np.ones([64, 64]), dtype=ms.float32) 66 67 net = Net(strategy1, strategy2, weight) 68 69 optimizer = AdamWeightDecay(net.trainable_params()) 70 71 net_with_loss = NetWithLoss(net, strategy3) 72 73 train_net = TrainOneStepCell(net_with_loss, optimizer) 74 75 compile_net(train_net, x, b) 76 77 78def test_optimizer_clone_weight2(): 79 class Net(nn.Cell): 80 def __init__(self, strategy1, strategy2, weight): 81 super().__init__() 82 self.weight = Parameter(weight, "w1") 83 self.matmul = P.MatMul(transpose_a=False, transpose_b=True).shard(strategy1) 84 self.relu = P.ReLU().shard(strategy2) 85 86 def construct(self, x): 87 out = self.matmul(x, self.weight) 88 out = self.relu(out) 89 return out 90 91 context.set_auto_parallel_context(device_num=4, global_rank=0) 92 context.set_auto_parallel_context(parallel_mode="semi_auto_parallel") 93 94 strategy1 = ((2, 1), (2, 1)) 95 strategy2 = ((4, 1),) 96 strategy3 = ((4, 1), (4, 1)) 97 98 x = Tensor(np.ones([64, 32]), dtype=ms.float32) 99 weight = Tensor(np.ones([64, 32]), dtype=ms.float32) 100 b = Tensor(np.ones([64, 64]), dtype=ms.float32) 101 102 net = Net(strategy1, strategy2, weight) 103 104 optimizer = AdamWeightDecay(net.trainable_params()) 105 106 net_with_loss = NetWithLoss(net, strategy3) 107 108 train_net = TrainOneStepCell(net_with_loss, optimizer) 109 110 compile_net(train_net, x, b) 111