• 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 mindspore.context as context
18import mindspore.nn as nn
19from mindspore import Tensor, Parameter
20from mindspore.common.initializer import initializer
21from mindspore.ops import operations as P
22
23context.set_context(mode=context.GRAPH_MODE)
24
25
26class Assign(nn.Cell):
27    def __init__(self, x, y):
28        super(Assign, self).__init__()
29        self.x = Parameter(initializer(x, x.shape), name="x")
30        self.y = Parameter(initializer(y, y.shape), name="y")
31        self.assign = P.Assign()
32
33    def construct(self):
34        self.assign(self.y, self.x)
35        return self.y
36
37
38def test_assign_bool():
39    x = Tensor(np.ones([3, 3]).astype(np.bool_))
40    y = Tensor(np.zeros([3, 3]).astype(np.bool_))
41    assign = Assign(x, y)
42    output = assign()
43    output = output.asnumpy()
44    output_expect = np.ones([3, 3]).astype(np.bool_)
45    print(output)
46    assert np.all(output == output_expect)
47
48
49def test_assign_int8():
50    x = Tensor(np.ones([3, 3]).astype(np.int8))
51    y = Tensor(np.zeros([3, 3]).astype(np.int8))
52    assign = Assign(x, y)
53    output = assign()
54    output = output.asnumpy()
55    output_expect = np.ones([3, 3]).astype(np.int8)
56    print(output)
57    assert np.all(output == output_expect)
58
59
60def test_assign_uint8():
61    x = Tensor(np.ones([3, 3]).astype(np.uint8))
62    y = Tensor(np.zeros([3, 3]).astype(np.uint8))
63    assign = Assign(x, y)
64    output = assign()
65    output = output.asnumpy()
66    output_expect = np.ones([3, 3]).astype(np.uint8)
67    print(output)
68    assert np.all(output == output_expect)
69
70
71def test_assign_int16():
72    x = Tensor(np.ones([3, 3]).astype(np.int16))
73    y = Tensor(np.zeros([3, 3]).astype(np.int16))
74    assign = Assign(x, y)
75    output = assign()
76    output = output.asnumpy()
77    output_expect = np.ones([3, 3]).astype(np.int16)
78    print(output)
79    assert np.all(output == output_expect)
80
81
82def test_assign_uint16():
83    x = Tensor(np.ones([3, 3]).astype(np.uint16))
84    y = Tensor(np.zeros([3, 3]).astype(np.uint16))
85    assign = Assign(x, y)
86    output = assign()
87    output = output.asnumpy()
88    output_expect = np.ones([3, 3]).astype(np.uint16)
89    print(output)
90    assert np.all(output == output_expect)
91
92
93def test_assign_int32():
94    x = Tensor(np.ones([3, 3]).astype(np.int32))
95    y = Tensor(np.zeros([3, 3]).astype(np.int32))
96    assign = Assign(x, y)
97    output = assign()
98    output = output.asnumpy()
99    output_expect = np.ones([3, 3]).astype(np.int32)
100    print(output)
101    assert np.all(output == output_expect)
102
103
104def test_assign_uint32():
105    x = Tensor(np.ones([3, 3]).astype(np.uint32))
106    y = Tensor(np.zeros([3, 3]).astype(np.uint32))
107    assign = Assign(x, y)
108    output = assign()
109    output = output.asnumpy()
110    output_expect = np.ones([3, 3]).astype(np.uint32)
111    print(output)
112    assert np.all(output == output_expect)
113
114
115def test_assign_int64():
116    x = Tensor(np.ones([3, 3]).astype(np.int64))
117    y = Tensor(np.zeros([3, 3]).astype(np.int64))
118    assign = Assign(x, y)
119    output = assign()
120    output = output.asnumpy()
121    output_expect = np.ones([3, 3]).astype(np.int64)
122    print(output)
123    assert np.all(output == output_expect)
124
125
126def test_assign_uint64():
127    x = Tensor(np.ones([3, 3]).astype(np.uint64))
128    y = Tensor(np.zeros([3, 3]).astype(np.uint64))
129    assign = Assign(x, y)
130    output = assign()
131    output = output.asnumpy()
132    output_expect = np.ones([3, 3]).astype(np.uint64)
133    print(output)
134    assert np.all(output == output_expect)
135
136
137def test_assign_float16():
138    x = Tensor(np.array([[0.1, 0.2, 0.3],
139                         [0.4, 0.5, 0.5],
140                         [0.6, 0.7, 0.8]]).astype(np.float16))
141    y = Tensor(np.array([[0.4, 0.5, 0.5],
142                         [0.6, 0.7, 0.8],
143                         [0.1, 0.2, 0.3]]).astype(np.float16))
144    assign = Assign(x, y)
145    output = assign()
146    output = output.asnumpy()
147    output_expect = np.array([[0.1, 0.2, 0.3],
148                              [0.4, 0.5, 0.5],
149                              [0.6, 0.7, 0.8]]).astype(np.float16)
150    print(output)
151    assert np.all(output - output_expect < 1e-6)
152
153
154def test_assign_float32():
155    x = Tensor(np.array([[0.1, 0.2, 0.3],
156                         [0.4, 0.5, 0.5],
157                         [0.6, 0.7, 0.8]]).astype(np.float32))
158    y = Tensor(np.array([[0.4, 0.5, 0.5],
159                         [0.6, 0.7, 0.8],
160                         [0.1, 0.2, 0.3]]).astype(np.float32))
161    assign = Assign(x, y)
162    output = assign()
163    output = output.asnumpy()
164    output_expect = np.array([[0.1, 0.2, 0.3],
165                              [0.4, 0.5, 0.5],
166                              [0.6, 0.7, 0.8]]).astype(np.float32)
167    print(output)
168    assert np.all(output - output_expect < 1e-6)
169
170
171def test_assign_float64():
172    x = Tensor(np.array([[0.1, 0.2, 0.3],
173                         [0.4, 0.5, 0.5],
174                         [0.6, 0.7, 0.8]]).astype(np.float64))
175    y = Tensor(np.array([[0.4, 0.5, 0.5],
176                         [0.6, 0.7, 0.8],
177                         [0.1, 0.2, 0.3]]).astype(np.float64))
178    assign = Assign(x, y)
179    output = assign()
180    output = output.asnumpy()
181    output_expect = np.array([[0.1, 0.2, 0.3],
182                              [0.4, 0.5, 0.5],
183                              [0.6, 0.7, 0.8]]).astype(np.float64)
184    print(output)
185    assert np.all(output - output_expect < 1e-6)
186
187
188class AssignAdd(nn.Cell):
189    def __init__(self, x, y):
190        super(AssignAdd, self).__init__()
191        self.x = Parameter(initializer(x, x.shape), name="x")
192        self.y = Parameter(initializer(y, y.shape), name="y")
193        self.assignadd = P.AssignAdd()
194
195    def construct(self):
196        self.assignadd(self.y, self.x)
197        return self.y
198
199
200def test_number_assignadd_number():
201    input_x = 2
202    result1 = 5
203    result2 = 5
204    result1 += input_x
205    assignadd = AssignAdd(result2, input_x)
206    result2 = assignadd()
207    expect = 7
208    assert np.all(result1 == expect)
209    assert np.all(result2 == expect)
210
211
212def test_tensor_assignadd_tensor():
213    input_x = Tensor(np.array([[2, 2], [3, 3]]))
214    result1 = Tensor(np.array([[4, -2], [2, 17]]))
215    result2 = Tensor(np.array([[4, -2], [2, 17]]))
216    result1 += input_x
217    result2 = AssignAdd(result2, input_x)()
218    expect = Tensor(np.array([[6, 0], [5, 20]]))
219    assert np.all(result1.asnumpy() == expect)
220    assert np.all(result2.asnumpy() == expect)
221
222
223def test_tensor_assignadd_number():
224    input_x = 3
225    result1 = Tensor(np.array([[4, -2], [2, 17]])).astype(np.float16)
226    result2 = Tensor(np.array([[4, -2], [2, 17]])).astype(np.float16)
227    result1 += input_x
228    result2 = AssignAdd(result2, input_x)()
229    expect = Tensor(np.array([[7, 1], [5, 20]]))
230    assert np.all(result1.asnumpy() == expect)
231    assert np.all(result2.asnumpy() == expect)
232
233
234def test_number_assignadd_tensor():
235    result1 = 3
236    result2 = 3
237    input_x = Tensor(np.array([[4, -2], [2, 17]])).astype(np.float16)
238    result1 += input_x
239    result2 = AssignAdd(result2, input_x)()
240    expect = Tensor(np.array([[7, 1], [5, 20]]))
241    assert np.all(result1.asnumpy() == expect)
242    assert np.all(result2.asnumpy() == expect)
243
244
245def test_tuple_assignadd_tuple():
246    result1 = (1, 2, 3, 4)
247    result2 = (1, 2, 3, 4)
248    input_x = (2, 3, 4, 5, 6)
249    result1 += input_x
250    result2 = AssignAdd(result2, input_x)()
251    expect = (1, 2, 3, 4, 2, 3, 4, 5, 6)
252    assert np.all(result1.asnumpy() == expect)
253    assert np.all(result2.asnumpy() == expect)
254
255
256def test_string_assignadd_string():
257    result1 = "string111"
258    result2 = "string111"
259    input_x = "string222"
260    result1 += input_x
261    result2 = AssignAdd(result2, input_x)()
262    expect = "string111string222"
263    assert result1 == expect
264    assert result2 == expect
265
266
267class AssignSub(nn.Cell):
268    def __init__(self, x, y):
269        super(AssignSub, self).__init__()
270        self.x = Parameter(initializer(x, x.shape), name="x")
271        self.y = Parameter(initializer(y, y.shape), name="y")
272        self.assignsub = P.AssignSub()
273
274    def construct(self):
275        self.assignsub(self.y, self.x)
276        return self.y
277
278
279def test_number_assignsub_number():
280    input_x = 2
281    result1 = 5
282    result2 = 5
283    result1 -= input_x
284    result2 = AssignSub(result2, input_x)()
285    expect = 3
286    assert np.all(result1 == expect)
287    assert np.all(result2 == expect)
288
289
290def test_tensor_assignsub_tensor():
291    input_x = Tensor(np.array([[2, 2], [3, 3]]))
292    result1 = Tensor(np.array([[4, -2], [2, 17]]))
293    result2 = Tensor(np.array([[4, -2], [2, 17]]))
294    result1 -= input_x
295    result2 = AssignSub(result2, input_x)()
296    expect = Tensor(np.array([[2, -4], [-1, 14]]))
297    assert np.all(result1.asnumpy() == expect)
298    assert np.all(result2.asnumpy() == expect)
299
300
301def test_tensor_assignsub_number():
302    input_x = 3
303    result1 = Tensor(np.array([[4, -2], [2, 17]])).astype(np.float16)
304    result2 = Tensor(np.array([[4, -2], [2, 17]])).astype(np.float16)
305    result1 -= input_x
306    result2 = AssignSub(result2, input_x)()
307    expect = Tensor(np.array([[1, -5], [-1, 14]]))
308    assert np.all(result1.asnumpy() == expect)
309    assert np.all(result2.asnumpy() == expect)
310
311
312def test_number_assignsub_tensor():
313    result1 = 3
314    result2 = 3
315    input_x = Tensor(np.array([[4, -2], [2, 17]])).astype(np.float16)
316    result1 -= input_x
317    result2 = AssignSub(result2, input_x)()
318    expect = Tensor(np.array([[-1, 5], [1, -14]]))
319    assert np.all(result1.asnumpy() == expect)
320    assert np.all(result2.asnumpy() == expect)
321
322
323def test_number_assignmul_number():
324    input_x = 2
325    result = 5
326    result *= input_x
327    expect = 10
328    assert np.all(result == expect)
329
330
331def test_tensor_assignmul_tensor():
332    input_x = Tensor(np.array([[2, 2], [3, 3]]))
333    result = Tensor(np.array([[4, -2], [2, 17]]))
334    result *= input_x
335    expect = Tensor(np.array([[8, -4], [6, 51]]))
336    assert np.all(result.asnumpy() == expect)
337
338
339def test_tensor_assignmul_number():
340    input_x = 3
341    result = Tensor(np.array([[4, -2], [2, 17]])).astype(np.float16)
342    result *= input_x
343    expect = Tensor(np.array([[12, -6], [6, 51]]))
344    assert np.all(result.asnumpy() == expect)
345
346
347def test_number_assignmul_tensor():
348    result = 3
349    input_x = Tensor(np.array([[4, -2], [2, 17]])).astype(np.float16)
350    result *= input_x
351    expect = Tensor(np.array([[12, -6], [6, 51]]))
352    assert np.all(result.asnumpy() == expect)
353
354
355def test_number_assigndiv_number():
356    input_x = 2
357    result = 5
358    result /= input_x
359    expect = 2.5
360    assert np.all(result == expect)
361
362
363def test_tensor_assigndiv_tensor():
364    input_x = Tensor(np.array([[2, 2], [3, 3]]))
365    result = Tensor(np.array([[4, -2], [6, 15]]))
366    result /= input_x
367    expect = Tensor(np.array([[2, -1], [2, 5]]))
368    assert np.all(result.asnumpy() == expect)
369
370
371def test_tensor_assigndiv_number():
372    input_x = 3
373    result = Tensor(np.array([[9, -3], [6, 15]])).astype(np.float16)
374    result /= input_x
375    expect = Tensor(np.array([[3, -1], [2, 5]]))
376    assert np.all(result.asnumpy() == expect)
377
378
379def test_number_assigndiv_tensor():
380    result = 3
381    input_x = Tensor(np.array([[2, -2], [2, -2]])).astype(np.float16)
382    result /= input_x
383    expect = Tensor(np.array([[1.5, -1.5], [1.5, -1.5]]))
384    assert np.all(result.asnumpy() == expect)
385
386
387def test_number_assignmod_number():
388    input_x = 2
389    result = 5
390    result %= input_x
391    expect = 1
392    assert np.all(result == expect)
393
394
395def test_tensor_assignmod_tensor():
396    input_x = Tensor(np.array([[2, 2], [3, 3]]))
397    result = Tensor(np.array([[4, -2], [6, 15]]))
398    result %= input_x
399    expect = Tensor(np.array([[0, 0], [0, 0]]))
400    assert np.all(result.asnumpy() == expect)
401
402
403def test_tensor_assignmod_number():
404    input_x = 3
405    result = Tensor(np.array([[9, -3], [7, 15]])).astype(np.float16)
406    result %= input_x
407    expect = Tensor(np.array([[0, 0], [1, 0]]))
408    assert np.all(result.asnumpy() == expect)
409
410
411def test_number_assignmod_tensor():
412    result = 3
413    input_x = Tensor(np.array([[2, -2], [2, -2]])).astype(np.float16)
414    result %= input_x
415    expect = Tensor(np.array([[1, -1], [1, -1]]))
416    assert np.all(result.asnumpy() == expect)
417
418
419def test_number_assignmulmul_number():
420    input_x = 2
421    result = 5
422    result **= input_x
423    expect = 25
424    assert np.all(result == expect)
425
426
427def test_tensor_assignmulmul_tensor():
428    input_x = Tensor(np.array([[2, 2], [3, 3]]))
429    result = Tensor(np.array([[4, -2], [6, 5]]))
430    result **= input_x
431    expect = Tensor(np.array([[16, 4], [216, 125]]))
432    assert np.all(result.asnumpy() == expect)
433
434
435def test_tensor_assignmulmul_number():
436    input_x = 3
437    result = Tensor(np.array([[9, -3], [7, 5]])).astype(np.float16)
438    result **= input_x
439    expect = Tensor(np.array([[729, -27], [343, 125]]))
440    assert np.all(result.asnumpy() == expect)
441
442
443def test_number_assignmulmul_tensor():
444    result = 3
445    input_x = Tensor(np.array([[2, 2], [2, 2]])).astype(np.float16)
446    result **= input_x
447    expect = Tensor(np.array([[9, 9], [9, 9]]))
448    assert np.all(result.asnumpy() == expect)
449
450
451def test_number_assigndivdiv_number():
452    input_x = 2
453    result = 5
454    result //= input_x
455    expect = 2
456    assert np.all(result == expect)
457
458
459def test_tensor_assigndivdiv_tensor():
460    input_x = Tensor(np.array([[2, 2], [3, 3]]))
461    result = Tensor(np.array([[4, -2], [6, 6]]))
462    result //= input_x
463    expect = Tensor(np.array([[2, -1], [2, 2]]))
464    assert np.all(result.asnumpy() == expect)
465
466
467def test_tensor_assigndivdiv_number():
468    input_x = 3
469    result = Tensor(np.array([[9, -3], [15, 9]])).astype(np.float16)
470    result //= input_x
471    expect = Tensor(np.array([[3, -1], [5, 3]]))
472    assert np.all(result.asnumpy() == expect)
473
474
475def test_number_assigndivdiv_tensor():
476    result = 3
477    input_x = Tensor(np.array([[1, 2], [2, 2]])).astype(np.float16)
478    result //= input_x
479    expect = Tensor(np.array([[3, 1], [1, 1]]))
480    assert np.all(result.asnumpy() == expect)
481