• 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
19import mindspore.nn as nn
20from mindspore import Tensor, Parameter
21from mindspore.ops import operations as P
22
23context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
24
25
26class TestScatterAddNet(nn.Cell):
27    def __init__(self, lock, inputx, indices, updates):
28        super(TestScatterAddNet, self).__init__()
29        self.scatter_add = P.ScatterAdd(use_locking=lock)
30        self.inputx = Parameter(inputx, name="inputx")
31        self.indices = Parameter(indices, name="indices")
32        self.updates = Parameter(updates, name="updates")
33
34    def construct(self):
35        out = self.scatter_add(self.inputx, self.indices, self.updates)
36        return out
37
38
39def scatter_add_net(inputx, indices, updates):
40    lock = True
41    net = TestScatterAddNet(lock, inputx, indices, updates)
42    return net()
43
44
45def scatter_add_use_locking_false_net(inputx, indices, updates):
46    lock = False
47    net = TestScatterAddNet(lock, inputx, indices, updates)
48    return net()
49
50
51@pytest.mark.level0
52@pytest.mark.platform_x86_cpu
53@pytest.mark.env_onecard
54def test_scatter_add_small_float32():
55    inputx = Tensor(np.zeros((2, 3)).astype(np.float32))
56    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
57    updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
58    output = scatter_add_net(inputx, indices, updates)
59    expected = np.array([[6., 8., 10.],
60                         [12., 14., 16.]])
61    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
62
63
64@pytest.mark.level0
65@pytest.mark.platform_x86_cpu
66@pytest.mark.env_onecard
67def test_scatter_add_input_updated():
68    inputx = Tensor(np.zeros((2, 3)).astype(np.float32))
69    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
70    updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
71    lock = True
72    net = TestScatterAddNet(lock, inputx, indices, updates)
73    net()
74    expected = np.array([[6., 8., 10.],
75                         [12., 14., 16.]])
76    np.testing.assert_array_almost_equal(net.inputx.asnumpy(), expected)
77
78
79@pytest.mark.level0
80@pytest.mark.platform_x86_cpu
81@pytest.mark.env_onecard
82def test_scatter_add_large_shape_float32():
83    inputx = Tensor(np.ones((4, 2, 3, 4)).astype(np.float32))
84    indices = Tensor(np.array([[0, 2], [3, 1]]).astype(np.int32))
85    updates = Tensor(np.arange(96).reshape((2, 2, 2, 3, 4)).astype(np.float32))
86    output = scatter_add_net(inputx, indices, updates)
87    expected = np.array([[[[1., 2., 3., 4.],
88                           [5., 6., 7., 8.],
89                           [9., 10., 11., 12.]],
90                          [[13., 14., 15., 16.],
91                           [17., 18., 19., 20.],
92                           [21., 22., 23., 24.]]],
93                         [[[73., 74., 75., 76.],
94                           [77., 78., 79., 80.],
95                           [81., 82., 83., 84.]],
96                          [[85., 86., 87., 88.],
97                           [89., 90., 91., 92.],
98                           [93., 94., 95., 96.]]],
99                         [[[25., 26., 27., 28.],
100                           [29., 30., 31., 32.],
101                           [33., 34., 35., 36.]],
102                          [[37., 38., 39., 40.],
103                           [41., 42., 43., 44.],
104                           [45., 46., 47., 48.]]],
105                         [[[49., 50., 51., 52.],
106                           [53., 54., 55., 56.],
107                           [57., 58., 59., 60.]],
108                          [[61., 62., 63., 64.],
109                           [65., 66., 67., 68.],
110                           [69., 70., 71., 72.]]]])
111    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
112
113
114@pytest.mark.level0
115@pytest.mark.platform_x86_cpu
116@pytest.mark.env_onecard
117def test_scatter_add_small_float32_use_locking_false():
118    inputx = Tensor(np.zeros((2, 3)).astype(np.float32))
119    indices = Tensor(np.array([1, 0]).astype(np.int32))
120    updates = Tensor(np.arange(6).reshape((2, 3)).astype(np.float32))
121    output = scatter_add_use_locking_false_net(inputx, indices, updates)
122    expected = np.array([[3., 4., 5.],
123                         [0., 1., 2.]])
124    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
125
126
127@pytest.mark.level0
128@pytest.mark.platform_x86_cpu
129@pytest.mark.env_onecard
130def test_scatter_add_input_less_than_1_float32():
131    inputx = Tensor(np.array([[0.214141, 0.415151, 0.51516],
132                              [0.876542, 0.451611, 0.55112],
133                              [0.111244, 0.633333, 0.34444]]).astype(np.float32))
134    indices = Tensor(np.array([[[1, 0, 2],
135                                [2, 2, 0]],
136                               [[1, 0, 1],
137                                [2, 1, 2]]]).astype(np.int32))
138    updates = Tensor(np.arange(34, 70).reshape((2, 2, 3, 3)).astype(np.float32))
139    output = scatter_add_net(inputx, indices, updates)
140    expected = np.array([[141.21414, 144.41515, 147.51517],
141                         [208.87654, 212.45161, 216.55112],
142                         [257.11124, 262.63333, 267.34442]], dtype=np.float32)
143    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
144
145
146@pytest.mark.level0
147@pytest.mark.platform_x86_cpu
148@pytest.mark.env_onecard
149def test_scatter_add_float16():
150    inputx = Tensor(np.zeros((2, 3)).astype(np.float16))
151    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
152    updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float16))
153    output = scatter_add_net(inputx, indices, updates)
154    expected = np.array([[6., 8., 10.],
155                         [12., 14., 16.]])
156    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
157
158
159@pytest.mark.level0
160@pytest.mark.platform_x86_cpu
161@pytest.mark.env_onecard
162def test_scatter_add_large_float16():
163    inputx = Tensor(np.zeros((2, 3, 4)).astype(np.float16))
164    indices = Tensor(np.array([[0, 0], [1, 1]]).astype(np.int32))
165    updates = Tensor(np.arange(63, 111).reshape((2, 2, 3, 4)).astype(np.float16))
166    output = scatter_add_net(inputx, indices, updates)
167    expected = np.array([[[138., 140., 142., 144.],
168                          [146., 148., 150., 152.],
169                          [154., 156., 158., 160.]],
170                         [[186., 188., 190., 192.],
171                          [194., 196., 198., 200.],
172                          [202., 204., 206., 208.]]])
173    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
174
175
176@pytest.mark.level0
177@pytest.mark.platform_x86_cpu
178@pytest.mark.env_onecard
179def test_scatter_add_disordered_float16():
180    inputx = Tensor(np.flip(np.arange(34, 46).reshape(3, 4).astype(np.float16)))
181    indices = Tensor(np.array([[[0, 1, 2],
182                                [2, 1, 0]],
183                               [[0, 0, 0],
184                                [2, 2, 2]]]).astype(np.int32))
185    updates = Tensor(np.arange(63, 111).reshape((2, 2, 3, 4)).astype(np.float16))
186    output = scatter_add_net(inputx, indices, updates)
187    expected = np.array([[464., 468., 472., 476.],
188                         [187., 188., 189., 190.],
189                         [492., 496., 500., 504.]])
190    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
191
192
193@pytest.mark.level0
194@pytest.mark.platform_x86_cpu
195@pytest.mark.env_onecard
196def test_scatter_add_large_int32():
197    inputx = Tensor(np.zeros((2, 3, 4)).astype(np.int32))
198    indices = Tensor(np.array([[0, 0], [1, 1]]).astype(np.int32))
199    updates = Tensor(np.arange(63, 111).reshape((2, 2, 3, 4)).astype(np.int32))
200    output = scatter_add_net(inputx, indices, updates)
201    expected = np.array([[[138., 140., 142., 144.],
202                          [146., 148., 150., 152.],
203                          [154., 156., 158., 160.]],
204                         [[186., 188., 190., 192.],
205                          [194., 196., 198., 200.],
206                          [202., 204., 206., 208.]]]).astype(np.int32)
207    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
208
209
210@pytest.mark.level0
211@pytest.mark.platform_x86_cpu
212@pytest.mark.env_onecard
213def test_scatter_add_disordered_int32():
214    inputx = Tensor(np.flip(np.arange(34, 46).reshape(3, 4).astype(np.int32)))
215    indices = Tensor(np.array([[[0, 1, 2],
216                                [2, 1, 0]],
217                               [[0, 0, 0],
218                                [2, 2, 2]]]).astype(np.int32))
219    updates = Tensor(np.arange(63, 111).reshape((2, 2, 3, 4)).astype(np.int32))
220    output = scatter_add_net(inputx, indices, updates)
221    expected = np.array([[464., 468., 472., 476.],
222                         [187., 188., 189., 190.],
223                         [492., 496., 500., 504.]]).astype(np.int32)
224    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
225
226
227class TestScatterSubNet(nn.Cell):
228    def __init__(self, lock, inputx, indices, updates):
229        super(TestScatterSubNet, self).__init__()
230        self.scatter_sub = P.ScatterSub(use_locking=lock)
231        self.inputx = Parameter(inputx, name="inputx")
232        self.indices = Parameter(indices, name="indices")
233        self.updates = Parameter(updates, name="updates")
234
235    def construct(self):
236        out = self.scatter_sub(self.inputx, self.indices, self.updates)
237        return out
238
239
240def scatter_sub_net(inputx, indices, updates):
241    lock = True
242    net = TestScatterSubNet(lock, inputx, indices, updates)
243    return net()
244
245
246def scatter_sub_use_locking_false_net(inputx, indices, updates):
247    lock = False
248    net = TestScatterSubNet(lock, inputx, indices, updates)
249    return net()
250
251
252@pytest.mark.level0
253@pytest.mark.platform_x86_cpu
254@pytest.mark.env_onecard
255def test_scatter_sub_input_updated():
256    inputx = Tensor(np.zeros((2, 3)).astype(np.float32))
257    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
258    updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
259    lock = True
260    net = TestScatterSubNet(lock, inputx, indices, updates)
261    net()
262    expected = np.array([[-6., -8., -10.],
263                         [-12., -14., -16.]])
264    np.testing.assert_array_almost_equal(net.inputx.asnumpy(), expected)
265
266
267@pytest.mark.level0
268@pytest.mark.platform_x86_cpu
269@pytest.mark.env_onecard
270def test_scatter_sub_large_shape_float32():
271    inputx = Tensor(np.ones((4, 2, 3, 4)).astype(np.float32))
272    indices = Tensor(np.array([[0, 2], [3, 1]]).astype(np.int32))
273    updates = Tensor(np.arange(96).reshape((2, 2, 2, 3, 4)).astype(np.float32))
274    output = scatter_sub_net(inputx, indices, updates)
275    expected = np.array(
276        [[[[1.0, 0.0, -1.0, -2.0],
277           [-3.0, -4.0, -5.0, -6.0],
278           [-7.0, -8.0, -9.0, -10.0]],
279          [[-11.0, -12.0, -13.0, -14.0],
280           [-15.0, -16.0, -17.0, -18.0],
281           [-19.0, -20.0, -21.0, -22.0]]],
282         [[[-71.0, -72.0, -73.0, -74.0],
283           [-75.0, -76.0, -77.0, -78.0],
284           [-79.0, -80.0, -81.0, -82.0]],
285          [[-83.0, -84.0, -85.0, -86.0],
286           [-87.0, -88.0, -89.0, -90.0],
287           [-91.0, -92.0, -93.0, -94.0]]],
288         [[[-23.0, -24.0, -25.0, -26.0],
289           [-27.0, -28.0, -29.0, -30.0],
290           [-31.0, -32.0, -33.0, -34.0]],
291          [[-35.0, -36.0, -37.0, -38.0],
292           [-39.0, -40.0, -41.0, -42.0],
293           [-43.0, -44.0, -45.0, -46.0]]],
294         [[[-47.0, -48.0, -49.0, -50.0],
295           [-51.0, -52.0, -53.0, -54.0],
296           [-55.0, -56.0, -57.0, -58.0]],
297          [[-59.0, -60.0, -61.0, -62.0],
298           [-63.0, -64.0, -65.0, -66.0],
299           [-67.0, -68.0, -69.0, -70.0]]]])
300    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
301
302
303@pytest.mark.level0
304@pytest.mark.platform_x86_cpu
305@pytest.mark.env_onecard
306def test_scatter_sub_small_float32_use_locking_false():
307    inputx = Tensor(np.zeros((2, 3)).astype(np.float32))
308    indices = Tensor(np.array([1, 0]).astype(np.int32))
309    updates = Tensor(np.arange(6).reshape((2, 3)).astype(np.float32))
310    output = scatter_sub_use_locking_false_net(inputx, indices, updates)
311    expected = np.array([[-3., -4., -5.],
312                         [-0., -1., -2.]])
313    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
314
315
316class TestScatterMulNet(nn.Cell):
317    def __init__(self, lock, inputx, indices, updates):
318        super(TestScatterMulNet, self).__init__()
319        self.scatter_mul = P.ScatterMul(use_locking=lock)
320        self.inputx = Parameter(inputx, name="inputx")
321        self.indices = Parameter(indices, name="indices")
322        self.updates = Parameter(updates, name="updates")
323
324    def construct(self):
325        out = self.scatter_mul(self.inputx, self.indices, self.updates)
326        return out
327
328
329def scatter_mul_net(inputx, indices, updates):
330    lock = True
331    net = TestScatterMulNet(lock, inputx, indices, updates)
332    return net()
333
334
335def scatter_mul_use_locking_false_net(inputx, indices, updates):
336    lock = False
337    net = TestScatterMulNet(lock, inputx, indices, updates)
338    return net()
339
340
341@pytest.mark.level0
342@pytest.mark.platform_x86_cpu
343@pytest.mark.env_onecard
344def test_scatter_mul_input_updated():
345    inputx = Tensor(np.ones((2, 3)).astype(np.float32))
346    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
347    updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
348    lock = True
349    net = TestScatterMulNet(lock, inputx, indices, updates)
350    net()
351    expected = np.array([[0., 7., 16.],
352                         [27., 40., 55.]])
353    np.testing.assert_array_almost_equal(net.inputx.asnumpy(), expected)
354
355
356@pytest.mark.level0
357@pytest.mark.platform_x86_cpu
358@pytest.mark.env_onecard
359def test_scatter_mul_output_updated_float32():
360    inputx = Tensor(np.ones((2, 3)).astype(np.float32))
361    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
362    updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
363    output = scatter_mul_net(inputx, indices, updates)
364    expected = np.array([[0., 7., 16.],
365                         [27., 40., 55.]])
366    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
367
368
369@pytest.mark.level0
370@pytest.mark.platform_x86_cpu
371@pytest.mark.env_onecard
372def test_scatter_mul_small_float32_use_locking_false():
373    inputx = Tensor(np.ones((2, 3)).astype(np.float32))
374    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
375    updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
376    output = scatter_mul_use_locking_false_net(inputx, indices, updates)
377    expected = np.array([[0., 7., 16.],
378                         [27., 40., 55.]])
379    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
380
381
382class TestScatterDivNet(nn.Cell):
383    def __init__(self, lock, inputx, indices, updates):
384        super(TestScatterDivNet, self).__init__()
385        self.scatter_div = P.ScatterDiv(use_locking=lock)
386        self.inputx = Parameter(inputx, name="inputx")
387        self.indices = Parameter(indices, name="indices")
388        self.updates = Parameter(updates, name="updates")
389
390    def construct(self):
391        out = self.scatter_div(self.inputx, self.indices, self.updates)
392        return out
393
394
395def scatter_div_net(inputx, indices, updates):
396    lock = True
397    net = TestScatterDivNet(lock, inputx, indices, updates)
398    return net()
399
400
401def scatter_div_use_locking_false_net(inputx, indices, updates):
402    lock = False
403    net = TestScatterDivNet(lock, inputx, indices, updates)
404    return net()
405
406
407@pytest.mark.level0
408@pytest.mark.platform_x86_cpu
409@pytest.mark.env_onecard
410def test_scatter_div_input_updated():
411    inputx = Tensor(np.zeros((2, 3)).astype(np.float32))
412    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
413    updates = Tensor(np.arange(1, 13).reshape((2, 2, 3)).astype(np.float32))
414    lock = True
415    net = TestScatterDivNet(lock, inputx, indices, updates)
416    net()
417    expected = np.array([[0., 0., 0.],
418                         [0., 0., 0.]])
419    np.testing.assert_array_almost_equal(net.inputx.asnumpy(), expected)
420
421
422@pytest.mark.level0
423@pytest.mark.platform_x86_cpu
424@pytest.mark.env_onecard
425def test_scatter_div_output_updated_float32():
426    inputx = Tensor(np.zeros((2, 3)).astype(np.float32))
427    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
428    updates = Tensor(np.arange(1, 13).reshape((2, 2, 3)).astype(np.float32))
429    output = scatter_div_net(inputx, indices, updates)
430    expected = np.array([[0., 0., 0.],
431                         [0., 0., 0.]])
432    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
433
434
435@pytest.mark.level0
436@pytest.mark.platform_x86_cpu
437@pytest.mark.env_onecard
438def test_scatter_div_small_float32_use_locking_false():
439    inputx = Tensor(np.ones((2, 3)).astype(np.float32) * 10)
440    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
441    updates = Tensor(np.ones(12).reshape((2, 2, 3)).astype(np.float32))
442    output = scatter_div_use_locking_false_net(inputx, indices, updates)
443    expected = np.array([[10., 10., 10.],
444                         [10., 10., 10.]])
445    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
446
447
448class TestScatterMaxNet(nn.Cell):
449    def __init__(self, lock, inputx, indices, updates):
450        super(TestScatterMaxNet, self).__init__()
451        self.scatter_max = P.ScatterMax(use_locking=lock)
452        self.inputx = Parameter(inputx, name="inputx")
453        self.indices = Parameter(indices, name="indices")
454        self.updates = Parameter(updates, name="updates")
455
456    def construct(self):
457        out = self.scatter_max(self.inputx, self.indices, self.updates)
458        return out
459
460
461def scatter_max_net(inputx, indices, updates):
462    lock = True
463    net = TestScatterMaxNet(lock, inputx, indices, updates)
464    return net()
465
466
467def scatter_max_use_locking_false_net(inputx, indices, updates):
468    lock = False
469    net = TestScatterMaxNet(lock, inputx, indices, updates)
470    return net()
471
472
473@pytest.mark.level0
474@pytest.mark.platform_x86_cpu
475@pytest.mark.env_onecard
476def test_scatter_max_input_updated():
477    inputx = Tensor(np.zeros((2, 3)).astype(np.float32))
478    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
479    updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
480    lock = True
481    net = TestScatterMaxNet(lock, inputx, indices, updates)
482    net()
483    expected = np.array([[6., 7., 8.],
484                         [9., 10., 11.]])
485    np.testing.assert_array_almost_equal(net.inputx.asnumpy(), expected)
486
487
488@pytest.mark.level0
489@pytest.mark.platform_x86_cpu
490@pytest.mark.env_onecard
491def test_scatter_max_output_updated_float32():
492    inputx = Tensor(np.zeros((2, 3)).astype(np.float32))
493    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
494    updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
495    output = scatter_max_net(inputx, indices, updates)
496    expected = np.array([[6., 7., 8.],
497                         [9., 10., 11.]])
498    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
499
500
501@pytest.mark.level0
502@pytest.mark.platform_x86_cpu
503@pytest.mark.env_onecard
504def test_scatter_max_small_float32_use_locking_false():
505    inputx = Tensor(np.ones((2, 3)).astype(np.float32) * 10)
506    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
507    updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
508    output = scatter_max_use_locking_false_net(inputx, indices, updates)
509    expected = np.array([[10., 10., 10.],
510                         [10., 10., 11.]])
511    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
512
513
514class TestScatterMinNet(nn.Cell):
515    def __init__(self, lock, inputx, indices, updates):
516        super(TestScatterMinNet, self).__init__()
517        self.scatter_min = P.ScatterMin(use_locking=lock)
518        self.inputx = Parameter(inputx, name="inputx")
519        self.indices = Parameter(indices, name="indices")
520        self.updates = Parameter(updates, name="updates")
521
522    def construct(self):
523        out = self.scatter_min(self.inputx, self.indices, self.updates)
524        return out
525
526
527def scatter_min_net(inputx, indices, updates):
528    lock = True
529    net = TestScatterMinNet(lock, inputx, indices, updates)
530    return net()
531
532
533def scatter_min_use_locking_false_net(inputx, indices, updates):
534    lock = False
535    net = TestScatterMinNet(lock, inputx, indices, updates)
536    return net()
537
538
539@pytest.mark.level0
540@pytest.mark.platform_x86_cpu
541@pytest.mark.env_onecard
542def test_scatter_min_input_updated():
543    inputx = Tensor(np.zeros((2, 3)).astype(np.float32))
544    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
545    updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
546    lock = True
547    net = TestScatterMinNet(lock, inputx, indices, updates)
548    net()
549    expected = np.array([[0., 0., 0.],
550                         [0., 0., 0.]])
551    np.testing.assert_array_almost_equal(net.inputx.asnumpy(), expected)
552
553
554@pytest.mark.level0
555@pytest.mark.platform_x86_cpu
556@pytest.mark.env_onecard
557def test_scatter_min_output_updated_float32():
558    inputx = Tensor(np.ones((2, 3)).astype(np.float32))
559    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
560    updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
561    output = scatter_min_net(inputx, indices, updates)
562    expected = np.array([[0., 1., 1.],
563                         [1., 1., 1.]])
564    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
565
566
567@pytest.mark.level0
568@pytest.mark.platform_x86_cpu
569@pytest.mark.env_onecard
570def test_scatter_min_small_float32_use_locking_false():
571    inputx = Tensor(np.ones((2, 3)).astype(np.float32))
572    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
573    updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
574    output = scatter_min_use_locking_false_net(inputx, indices, updates)
575    expected = np.array([[0., 1., 1.],
576                         [1., 1., 1.]])
577    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
578
579
580class TestScatterUpdateNet(nn.Cell):
581    def __init__(self, lock, inputx, indices, updates):
582        super(TestScatterUpdateNet, self).__init__()
583        self.scatter_update = P.ScatterUpdate(use_locking=lock)
584        self.inputx = Parameter(inputx, name="inputx")
585        self.indices = Parameter(indices, name="indices")
586        self.updates = Parameter(updates, name="updates")
587
588    def construct(self):
589        out = self.scatter_update(self.inputx, self.indices, self.updates)
590        return out
591
592
593def scatter_update_net(inputx, indices, updates):
594    lock = True
595    net = TestScatterUpdateNet(lock, inputx, indices, updates)
596    return net()
597
598
599def scatter_update_use_locking_false_net(inputx, indices, updates):
600    lock = False
601    net = TestScatterUpdateNet(lock, inputx, indices, updates)
602    return net()
603
604
605@pytest.mark.level0
606@pytest.mark.platform_x86_cpu
607@pytest.mark.env_onecard
608def test_scatter_update_input_updated():
609    inputx = Tensor(np.zeros((2, 3)).astype(np.float32))
610    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
611    updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
612    lock = True
613    net = TestScatterUpdateNet(lock, inputx, indices, updates)
614    net()
615    expected = np.array([[6., 7., 8.],
616                         [9., 10., 11.]])
617    np.testing.assert_array_almost_equal(net.inputx.asnumpy(), expected)
618
619
620@pytest.mark.level0
621@pytest.mark.platform_x86_cpu
622@pytest.mark.env_onecard
623def test_scatter_update_output_updated_float32():
624    inputx = Tensor(np.ones((2, 3)).astype(np.float32))
625    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
626    updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
627    output = scatter_update_net(inputx, indices, updates)
628    expected = np.array([[6., 7., 8.],
629                         [9., 10., 11.]])
630    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
631
632
633@pytest.mark.level0
634@pytest.mark.platform_x86_cpu
635@pytest.mark.env_onecard
636def test_scatter_update_small_float32_use_locking_false():
637    inputx = Tensor(np.ones((2, 3)).astype(np.float32))
638    indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
639    updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
640    output = scatter_update_use_locking_false_net(inputx, indices, updates)
641    expected = np.array([[6., 7., 8.],
642                         [9., 10., 11.]])
643    np.testing.assert_array_almost_equal(output.asnumpy(), expected)
644