• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 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# ============================================================================
15
16import numpy as np
17import pytest
18import mindspore.context as context
19from mindspore import Tensor
20from mindspore.nn import Cell
21import mindspore.ops.operations as P
22from mindspore.ops import functional as F
23from mindspore.common.parameter import Parameter
24
25
26class TestOptAssignNet_1(Cell):
27    def __init__(self):
28        super(TestOptAssignNet_1, self).__init__()
29        self.add = P.Add()
30        self.reduce_max = P.ReduceMax()
31        self.param = Parameter(
32            Tensor(np.zeros([2, 2, 2]).astype(np.float32)), name='param')
33
34    def construct(self, x, y):
35        add_res = self.add(x, y)
36        F.depend(add_res, F.assign(self.param, add_res))
37
38        return self.reduce_max(add_res)
39
40
41class TestOptAssignNet_2(Cell):
42    def __init__(self):
43        super(TestOptAssignNet_2, self).__init__()
44        self.add = P.Add()
45        self.param = Parameter(
46            Tensor(np.zeros([2, 2, 2]).astype(np.float32)), name='param')
47
48    def construct(self, x, y):
49        add_res = self.add(x, y)
50        F.depend(add_res, F.assign(self.param, add_res))
51
52        return add_res
53
54
55def test_opt_assign_output_1():
56    np.random.seed(0)
57    input_x = np.random.normal(0, 1, [2, 2, 2]).astype(np.float32)
58    input_y = np.random.normal(0, 1, [2, 2, 2]).astype(np.float32)
59
60    context.set_context(mode=context.GRAPH_MODE,
61                        enable_graph_kernel=True, device_target="GPU")
62    net = TestOptAssignNet_1()
63    result_open_gk = net(Tensor(input_x), Tensor(input_y))
64
65    context.set_context(mode=context.GRAPH_MODE,
66                        enable_graph_kernel=False, device_target="GPU")
67    net_beta = TestOptAssignNet_1()
68    result_close_gk = net_beta(Tensor(input_x), Tensor(input_y))
69    res = np.allclose(result_open_gk.asnumpy(), result_close_gk.asnumpy(), rtol=1.e-4, atol=1.e-7, equal_nan=True)
70    assert res
71
72
73def test_opt_assign_output_2():
74    np.random.seed(0)
75    input_x = np.random.normal(0, 1, [2, 2, 2]).astype(np.float32)
76    input_y = np.random.normal(0, 1, [2, 2, 2]).astype(np.float32)
77
78    context.set_context(mode=context.GRAPH_MODE,
79                        enable_graph_kernel=True, device_target="GPU")
80    net = TestOptAssignNet_2()
81    result_open_gk = net(Tensor(input_x), Tensor(input_y))
82
83    context.set_context(mode=context.GRAPH_MODE,
84                        enable_graph_kernel=False, device_target="GPU")
85    net_beta = TestOptAssignNet_2()
86    result_close_gk = net_beta(Tensor(input_x), Tensor(input_y))
87    res = np.allclose(result_open_gk.asnumpy(), result_close_gk.asnumpy(), rtol=1.e-4, atol=1.e-7, equal_nan=True)
88    assert res
89
90
91@pytest.mark.level0
92@pytest.mark.platform_x86_gpu_training
93@pytest.mark.env_onecard
94def test_opt_assign_gpu_1():
95    test_opt_assign_output_1()
96
97
98@pytest.mark.level0
99@pytest.mark.platform_x86_gpu_training
100@pytest.mark.env_onecard
101def test_opt_assign_gpu_2():
102    test_opt_assign_output_2()
103