• 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# ============================================================================
15
16import numpy as np
17import pytest
18
19import mindspore.context as context
20import mindspore.nn as nn
21from mindspore import Tensor, Parameter
22from mindspore.common.initializer import initializer
23from mindspore.ops import operations as P
24
25context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
26
27
28class Assign(nn.Cell):
29    def __init__(self, x, y):
30        super(Assign, self).__init__()
31        self.x = Parameter(initializer(x, x.shape), name="x")
32        self.y = Parameter(initializer(y, y.shape), name="y")
33        self.assign = P.Assign()
34
35    def construct(self):
36        self.assign(self.y, self.x)
37        return self.y
38
39
40@pytest.mark.level0
41@pytest.mark.platform_x86_cpu
42@pytest.mark.env_onecard
43def test_assign_bool():
44    x = Tensor(np.ones([3, 3]).astype(np.bool_))
45    y = Tensor(np.zeros([3, 3]).astype(np.bool_))
46    assign = Assign(x, y)
47    output = assign()
48    output = output.asnumpy()
49    output_expect = np.ones([3, 3]).astype(np.bool_)
50    print(output)
51    assert np.all(output == output_expect)
52
53
54@pytest.mark.level0
55@pytest.mark.platform_x86_cpu
56@pytest.mark.env_onecard
57def test_assign_int8():
58    x = Tensor(np.ones([3, 3]).astype(np.int8))
59    y = Tensor(np.zeros([3, 3]).astype(np.int8))
60    assign = Assign(x, y)
61    output = assign()
62    output = output.asnumpy()
63    output_expect = np.ones([3, 3]).astype(np.int8)
64    print(output)
65    assert np.all(output == output_expect)
66
67
68@pytest.mark.level0
69@pytest.mark.platform_x86_cpu
70@pytest.mark.env_onecard
71def test_assign_uint8():
72    x = Tensor(np.ones([3, 3]).astype(np.uint8))
73    y = Tensor(np.zeros([3, 3]).astype(np.uint8))
74    assign = Assign(x, y)
75    output = assign()
76    output = output.asnumpy()
77    output_expect = np.ones([3, 3]).astype(np.uint8)
78    print(output)
79    assert np.all(output == output_expect)
80
81
82@pytest.mark.level0
83@pytest.mark.platform_x86_cpu
84@pytest.mark.env_onecard
85def test_assign_int16():
86    x = Tensor(np.ones([3, 3]).astype(np.int16))
87    y = Tensor(np.zeros([3, 3]).astype(np.int16))
88    assign = Assign(x, y)
89    output = assign()
90    output = output.asnumpy()
91    output_expect = np.ones([3, 3]).astype(np.int16)
92    print(output)
93    assert np.all(output == output_expect)
94
95
96@pytest.mark.level0
97@pytest.mark.platform_x86_cpu
98@pytest.mark.env_onecard
99def test_assign_uint16():
100    x = Tensor(np.ones([3, 3]).astype(np.uint16))
101    y = Tensor(np.zeros([3, 3]).astype(np.uint16))
102    assign = Assign(x, y)
103    output = assign()
104    output = output.asnumpy()
105    output_expect = np.ones([3, 3]).astype(np.uint16)
106    print(output)
107    assert np.all(output == output_expect)
108
109
110@pytest.mark.level0
111@pytest.mark.platform_x86_cpu
112@pytest.mark.env_onecard
113def test_assign_int32():
114    x = Tensor(np.ones([3, 3]).astype(np.int32))
115    y = Tensor(np.zeros([3, 3]).astype(np.int32))
116    assign = Assign(x, y)
117    output = assign()
118    output = output.asnumpy()
119    output_expect = np.ones([3, 3]).astype(np.int32)
120    print(output)
121    assert np.all(output == output_expect)
122
123
124@pytest.mark.level0
125@pytest.mark.platform_x86_cpu
126@pytest.mark.env_onecard
127def test_assign_uint32():
128    x = Tensor(np.ones([3, 3]).astype(np.uint32))
129    y = Tensor(np.zeros([3, 3]).astype(np.uint32))
130    assign = Assign(x, y)
131    output = assign()
132    output = output.asnumpy()
133    output_expect = np.ones([3, 3]).astype(np.uint32)
134    print(output)
135    assert np.all(output == output_expect)
136
137
138@pytest.mark.level0
139@pytest.mark.platform_x86_cpu
140@pytest.mark.env_onecard
141def test_assign_int64():
142    x = Tensor(np.ones([3, 3]).astype(np.int64))
143    y = Tensor(np.zeros([3, 3]).astype(np.int64))
144    assign = Assign(x, y)
145    output = assign()
146    output = output.asnumpy()
147    output_expect = np.ones([3, 3]).astype(np.int64)
148    print(output)
149    assert np.all(output == output_expect)
150
151
152@pytest.mark.level0
153@pytest.mark.platform_x86_cpu
154@pytest.mark.env_onecard
155def test_assign_uint64():
156    x = Tensor(np.ones([3, 3]).astype(np.uint64))
157    y = Tensor(np.zeros([3, 3]).astype(np.uint64))
158    assign = Assign(x, y)
159    output = assign()
160    output = output.asnumpy()
161    output_expect = np.ones([3, 3]).astype(np.uint64)
162    print(output)
163    assert np.all(output == output_expect)
164
165
166@pytest.mark.level0
167@pytest.mark.platform_x86_cpu
168@pytest.mark.env_onecard
169def test_assign_float16():
170    x = Tensor(np.array([[0.1, 0.2, 0.3],
171                         [0.4, 0.5, 0.5],
172                         [0.6, 0.7, 0.8]]).astype(np.float16))
173    y = Tensor(np.array([[0.4, 0.5, 0.5],
174                         [0.6, 0.7, 0.8],
175                         [0.1, 0.2, 0.3]]).astype(np.float16))
176    assign = Assign(x, y)
177    output = assign()
178    output = output.asnumpy()
179    output_expect = np.array([[0.1, 0.2, 0.3],
180                              [0.4, 0.5, 0.5],
181                              [0.6, 0.7, 0.8]]).astype(np.float16)
182    print(output)
183    assert np.all(output - output_expect < 1e-6)
184
185
186@pytest.mark.level0
187@pytest.mark.platform_x86_cpu
188@pytest.mark.env_onecard
189def test_assign_float32():
190    x = Tensor(np.array([[0.1, 0.2, 0.3],
191                         [0.4, 0.5, 0.5],
192                         [0.6, 0.7, 0.8]]).astype(np.float32))
193    y = Tensor(np.array([[0.4, 0.5, 0.5],
194                         [0.6, 0.7, 0.8],
195                         [0.1, 0.2, 0.3]]).astype(np.float32))
196    assign = Assign(x, y)
197    output = assign()
198    output = output.asnumpy()
199    output_expect = np.array([[0.1, 0.2, 0.3],
200                              [0.4, 0.5, 0.5],
201                              [0.6, 0.7, 0.8]]).astype(np.float32)
202    print(output)
203    assert np.all(output - output_expect < 1e-6)
204
205
206@pytest.mark.level0
207@pytest.mark.platform_x86_cpu
208@pytest.mark.env_onecard
209def test_assign_float64():
210    x = Tensor(np.array([[0.1, 0.2, 0.3],
211                         [0.4, 0.5, 0.5],
212                         [0.6, 0.7, 0.8]]).astype(np.float64))
213    y = Tensor(np.array([[0.4, 0.5, 0.5],
214                         [0.6, 0.7, 0.8],
215                         [0.1, 0.2, 0.3]]).astype(np.float64))
216    assign = Assign(x, y)
217    output = assign()
218    output = output.asnumpy()
219    output_expect = np.array([[0.1, 0.2, 0.3],
220                              [0.4, 0.5, 0.5],
221                              [0.6, 0.7, 0.8]]).astype(np.float64)
222    print(output)
223    assert np.all(output - output_expect < 1e-6)
224