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, context 20from mindspore.common.parameter import Parameter 21from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits 22from mindspore.nn.optim.momentum import Momentum 23from mindspore.ops import operations as P 24from mindspore.train import Model 25from mindspore.context import ParallelMode 26from tests.dataset_mock import MindData 27 28 29class Dataset(MindData): 30 def __init__(self, predict, label, length=3): 31 super(Dataset, self).__init__(size=length) 32 self.predict = predict 33 self.label = label 34 self.index = 0 35 self.length = length 36 37 def __iter__(self): 38 return self 39 40 def __next__(self): 41 if self.index >= self.length: 42 raise StopIteration 43 self.index += 1 44 return self.predict, self.label 45 46 def reset(self): 47 self.index = 0 48 49 50class TransposeNet(nn.Cell): 51 def __init__(self, strategy1, strategy2): 52 super(TransposeNet, self).__init__() 53 self.matmul = P.MatMul().shard(((8, 1), (1, 1))) 54 self.matmul_weight = Parameter(Tensor(np.ones([128, 256]), dtype=ms.float32), name="weight") 55 self.transpose1 = P.Transpose().shard(strategy1) 56 self.transpose2 = P.Transpose().shard(strategy2) 57 58 def construct(self, x): 59 x = self.matmul(x, self.matmul_weight) 60 x = self.transpose1(x, (1, 0)) 61 x = self.transpose2(x, (1, 0)) 62 return x 63 64 65def transpose_net(strategy1, strategy2): 66 return TransposeNet(strategy1=strategy1, strategy2=strategy2) 67 68 69def transpose_common(strategy1, strategy2): 70 learning_rate = 0.1 71 momentum = 0.9 72 epoch_size = 2 73 74 context.reset_auto_parallel_context() 75 context.set_auto_parallel_context(parallel_mode=ParallelMode.SEMI_AUTO_PARALLEL, device_num=8, 76 parameter_broadcast=False) 77 78 predict = Tensor(np.ones([32, 128]), dtype=ms.float32) 79 label = Tensor(np.ones([32]), dtype=ms.int32) 80 dataset = Dataset(predict, label, 2) 81 net = transpose_net(strategy1, strategy2) 82 83 loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean') 84 loss.softmax_cross_entropy.shard(((8, 1), (8, 1))) 85 opt = Momentum(net.trainable_params(), learning_rate, momentum) 86 context.set_context(mode=context.GRAPH_MODE) 87 model = Model(net, loss, opt) 88 89 model.train(epoch_size, dataset, dataset_sink_mode=False) 90 91 92def test_transpose1(): 93 strategy1 = ((1, 8),) 94 strategy2 = ((1, 8),) 95 transpose_common(strategy1, strategy2) 96 97 98def test_transpose2(): 99 strategy1 = ((1, 4),) 100 strategy2 = ((1, 8),) 101 transpose_common(strategy1, strategy2) 102 103 104if __name__ == '__main__': 105 test_transpose1() 106 test_transpose2() 107