• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 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
16import pytest
17import mindspore.nn as nn
18from mindspore import Tensor, Parameter, context
19from mindspore.nn import TrainOneStepCell
20from mindspore.nn.optim import FTRL, LazyAdam
21from mindspore.ops import operations as P
22
23context.set_context(enable_sparse=True,
24                    mode=context.PYNATIVE_MODE,
25                    device_target="Ascend")
26
27class NetWithSparseGatherV2(nn.Cell):
28    def __init__(self):
29        super(NetWithSparseGatherV2, self).__init__()
30        self.weight1 = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="weight1")
31        self.weight2 = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="weight2")
32        self.axis = 1
33        self.gather = P.SparseGatherV2()
34
35    def construct(self, indices, label):
36        return self.gather(self.weight1, indices, self.axis) + self.weight2
37
38@pytest.mark.level1
39@pytest.mark.platform_arm_ascend_training
40@pytest.mark.platform_x86_ascend_training
41@pytest.mark.env_onecard
42def test_pynative_ftrl_net():
43    indices = Tensor(np.array([0, 0, 1]).astype(np.int32))
44    label = Tensor(np.zeros([2, 1, 2]).astype(np.float32))
45    net = NetWithSparseGatherV2()
46
47    optimizer = FTRL(net.trainable_params(), learning_rate=0.1, weight_decay=0.9, loss_scale=2.0)
48    optimizer.target = 'Ascend'
49    train_network = TrainOneStepCell(net, optimizer)
50    output = train_network(indices, label)
51    np.allclose(output.asnumpy(), np.array([[[2, 2]], [[2, 2]], [[2, 2]]]))
52    np.allclose(net.weight1.asnumpy(), np.array([[[0.7884067, 0.7884067]],
53                                                 [[0.68213105, 0.68213105]],
54                                                 [[1.0, 1.0]]]))
55    np.allclose(net.weight2.asnumpy(), np.array([[[0.6821311, 0.6821311]],
56                                                 [[0.6821311, 0.6821311]],
57                                                 [[0.6821311, 0.6821311]]]))
58
59@pytest.mark.level1
60@pytest.mark.platform_arm_ascend_training
61@pytest.mark.platform_x86_ascend_training
62@pytest.mark.env_onecard
63def test_pynative_lazy_adam_net():
64    indices = Tensor(np.array([0, 0, 1]).astype(np.int32))
65    label = Tensor(np.zeros([2, 1, 2]).astype(np.float32))
66    net = NetWithSparseGatherV2()
67
68    optimizer = LazyAdam(net.trainable_params(), learning_rate=0.1, weight_decay=0.9, loss_scale=2.0)
69    optimizer.target = 'Ascend'
70    train_network = TrainOneStepCell(net, optimizer)
71    output = train_network(indices, label)
72    np.allclose(output.asnumpy(), np.array([[[2, 2]], [[2, 2]], [[2, 2]]]))
73    np.allclose(net.weight1.asnumpy(), np.array([[[0.9, 0.9]], [[0.9, 0.9]], [[1.0, 1.0]]]))
74    np.allclose(net.weight2.asnumpy(), np.array([[[0.9, 0.9]], [[0.9, 0.9]], [[0.9, 0.9]]]))
75