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.ops import operations as P 22 23 24class NetWithLoss(nn.Cell): 25 def __init__(self, network): 26 super(NetWithLoss, self).__init__() 27 self.loss = P.SoftmaxCrossEntropyWithLogits() 28 self.network = network 29 30 def construct(self, x, b): 31 predict = self.network(x) 32 return self.loss(predict, b)[0] 33 34 35def test_parameter_init(): 36 class Net(nn.Cell): 37 def __init__(self, strategy1, weight): 38 super().__init__() 39 self.weight = Parameter(weight, "w1") 40 self.matmul = P.MatMul(transpose_a=False, transpose_b=True).shard(strategy1) 41 42 def construct(self, x): 43 out = self.matmul(x, self.weight) 44 return out 45 46 context.set_auto_parallel_context(device_num=2, global_rank=0) 47 context.set_auto_parallel_context(parallel_mode="semi_auto_parallel") 48 strategy1 = ((1, 1), (2, 1)) 49 context.set_context(mode=context.GRAPH_MODE) 50 51 x = Tensor(np.ones([64, 32]), dtype=ms.float32) 52 weight = Tensor(np.ones([64, 32]), dtype=ms.float32) 53 54 net = Net(strategy1, weight) 55 net(x,) 56 57 58if __name__ == '__main__': 59 test_parameter_init() 60