• 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 os
16import tempfile
17import pytest
18import scipy
19import numpy as np
20import mindspore.nn as nn
21import mindspore.ops.operations as P
22from mindspore import context, Tensor
23from mindspore.common import dtype as mstype
24from mindspore.common.parameter import Parameter
25from mindspore.train.summary.summary_record import SummaryRecord
26from tests.summary_utils import SummaryReader
27from tests.security_utils import security_off_wrap
28
29context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
30
31
32class AssignAddNet(nn.Cell):
33    def __init__(self, para):
34        super(AssignAddNet, self).__init__()
35        self.para = Parameter(para, name="para")
36        self.assign_add = P.AssignAdd()
37
38    def construct(self, value):
39        self.assign_add(self.para, value)
40        return self.para
41
42
43@pytest.mark.level1
44@pytest.mark.platform_arm_ascend_training
45@pytest.mark.platform_x86_ascend_training
46@pytest.mark.env_onecard
47def test_assign_add():
48    x = Tensor(1, dtype=mstype.int32)
49    y = Tensor(2, dtype=mstype.int32)
50    expect = Tensor(3, dtype=mstype.int32)
51    net = AssignAddNet(x)
52    out = net(y)
53    np.testing.assert_array_equal(out.asnumpy(), expect.asnumpy())
54
55
56class AssignSubNet(nn.Cell):
57    def __init__(self, para):
58        super(AssignSubNet, self).__init__()
59        self.para = Parameter(para, name="para")
60        self.assign_sub = P.AssignSub()
61
62    def construct(self, value):
63        self.assign_sub(self.para, value)
64        return self.para
65
66
67@pytest.mark.level1
68@pytest.mark.platform_arm_ascend_training
69@pytest.mark.platform_x86_ascend_training
70@pytest.mark.env_onecard
71def test_assign_sub():
72    x = Tensor(3, dtype=mstype.int32)
73    y = Tensor(2, dtype=mstype.int32)
74    expect = Tensor(1, dtype=mstype.int32)
75    net = AssignSubNet(x)
76    out = net(y)
77    np.testing.assert_array_equal(out.asnumpy(), expect.asnumpy())
78
79
80class ScatterAddNet(nn.Cell):
81    def __init__(self, input_x):
82        super(ScatterAddNet, self).__init__()
83        self.input_x = Parameter(input_x, name="para")
84        self.scatter_add = P.ScatterAdd()
85
86    def construct(self, indices, updates):
87        self.scatter_add(self.input_x, indices, updates)
88        return self.input_x
89
90
91@pytest.mark.level1
92@pytest.mark.platform_arm_ascend_training
93@pytest.mark.platform_x86_ascend_training
94@pytest.mark.env_onecard
95def test_scatter_add():
96    input_x = Tensor(np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]), mstype.float32)
97    indices = Tensor(np.array([[0, 1], [1, 1]]), mstype.int32)
98    updates = Tensor(np.ones([2, 2, 3]), mstype.float32)
99    expect = Tensor(np.array([[1.0, 1.0, 1.0], [3.0, 3.0, 3.0]]), mstype.float32)
100    net = ScatterAddNet(input_x)
101    out = net(indices, updates)
102    np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
103
104
105class ScatterSubNet(nn.Cell):
106    def __init__(self, input_x):
107        super(ScatterSubNet, self).__init__()
108        self.input_x = Parameter(input_x, name="para")
109        self.scatter_sub = P.ScatterSub()
110
111    def construct(self, indices, updates):
112        self.scatter_sub(self.input_x, indices, updates)
113        return self.input_x
114
115
116@pytest.mark.level1
117@pytest.mark.platform_arm_ascend_training
118@pytest.mark.platform_x86_ascend_training
119@pytest.mark.env_onecard
120def test_scatter_sub():
121    input_x = Tensor(np.array([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]]), mstype.float32)
122    indices = Tensor(np.array([[0, 1]]), mstype.int32)
123    updates = Tensor(np.array([[[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]]]), mstype.float32)
124    expect = Tensor(np.array([[-1.0, -1.0, -1.0], [-1.0, -1.0, -1.0]]), mstype.float32)
125    net = ScatterSubNet(input_x)
126    out = net(indices, updates)
127    np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
128
129
130class ScatterMulNet(nn.Cell):
131    def __init__(self, input_x):
132        super(ScatterMulNet, self).__init__()
133        self.input_x = Parameter(input_x, name="para")
134        self.scatter_mul = P.ScatterMul()
135
136    def construct(self, indices, updates):
137        self.scatter_mul(self.input_x, indices, updates)
138        return self.input_x
139
140
141@pytest.mark.level1
142@pytest.mark.platform_arm_ascend_training
143@pytest.mark.platform_x86_ascend_training
144@pytest.mark.env_onecard
145def test_scatter_mul():
146    input_x = Tensor(np.array([[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]]), mstype.float32)
147    indices = Tensor(np.array([[0, 1]]), mstype.int32)
148    updates = Tensor(np.array([[[2.0, 2.0, 2.0], [2.0, 2.0, 2.0]]]), mstype.float32)
149    expect = Tensor(np.array([[2.0, 2.0, 2.0], [4.0, 4.0, 4.0]]), mstype.float32)
150    net = ScatterMulNet(input_x)
151    out = net(indices, updates)
152    np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
153
154
155class ScatterDivNet(nn.Cell):
156    def __init__(self, input_x):
157        super(ScatterDivNet, self).__init__()
158        self.input_x = Parameter(input_x, name="para")
159        self.scatter_div = P.ScatterDiv()
160
161    def construct(self, indices, updates):
162        self.scatter_div(self.input_x, indices, updates)
163        return self.input_x
164
165
166@pytest.mark.level1
167@pytest.mark.platform_arm_ascend_training
168@pytest.mark.platform_x86_ascend_training
169@pytest.mark.env_onecard
170def test_scatter_div():
171    input_x = Tensor(np.array([[6.0, 6.0, 6.0], [2.0, 2.0, 2.0]]), mstype.float32)
172    indices = Tensor(np.array([[0, 1]]), mstype.int32)
173    updates = Tensor(np.array([[[2.0, 2.0, 2.0], [2.0, 2.0, 2.0]]]), mstype.float32)
174    expect = Tensor(np.array([[3.0, 3.0, 3.0], [1.0, 1.0, 1.0]]), mstype.float32)
175    net = ScatterDivNet(input_x)
176    out = net(indices, updates)
177    np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
178
179
180class ScatterMaxNet(nn.Cell):
181    def __init__(self, input_x):
182        super(ScatterMaxNet, self).__init__()
183        self.input_x = Parameter(input_x, name="para")
184        self.scatter_max = P.ScatterMax()
185
186    def construct(self, indices, updates):
187        self.scatter_max(self.input_x, indices, updates)
188        return self.input_x
189
190
191@pytest.mark.level1
192@pytest.mark.platform_arm_ascend_training
193@pytest.mark.platform_x86_ascend_training
194@pytest.mark.env_onecard
195def test_scatter_max():
196    input_x = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]), mstype.float32)
197    indices = Tensor(np.array([[0, 0], [1, 1]]), mstype.int32)
198    updates = Tensor(np.ones([2, 2, 3]) * 88, mstype.float32)
199    expect = Tensor(np.array([[88.0, 88.0, 88.0], [88.0, 88.0, 88.0]]), mstype.float32)
200    net = ScatterMaxNet(input_x)
201    out = net(indices, updates)
202    np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
203
204
205class ScatterMinNet(nn.Cell):
206    def __init__(self, input_x):
207        super(ScatterMinNet, self).__init__()
208        self.input_x = Parameter(input_x, name="para")
209        self.scatter_min = P.ScatterMin()
210
211    def construct(self, indices, updates):
212        self.scatter_min(self.input_x, indices, updates)
213        return self.input_x
214
215
216@pytest.mark.level1
217@pytest.mark.platform_arm_ascend_training
218@pytest.mark.platform_x86_ascend_training
219@pytest.mark.env_onecard
220def test_scatter_min():
221    input_x = Tensor(np.array([[0.0, 1.0, 2.0], [0.0, 0.0, 0.0]]), mstype.float32)
222    indices = Tensor(np.array([[0, 0], [1, 1]]), mstype.int32)
223    updates = Tensor(np.ones([2, 2, 3]), mstype.float32)
224    expect = Tensor(np.array([[0.0, 1.0, 1.0], [0.0, 0.0, 0.0]]), mstype.float32)
225    net = ScatterMinNet(input_x)
226    out = net(indices, updates)
227    np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
228
229
230class ScatterUpdateNet(nn.Cell):
231    def __init__(self, input_x):
232        super(ScatterUpdateNet, self).__init__()
233        self.input_x = Parameter(input_x, name="para")
234        self.scatter_update = P.ScatterUpdate()
235
236    def construct(self, indices, updates):
237        self.scatter_update(self.input_x, indices, updates)
238        return self.input_x
239
240
241@pytest.mark.level1
242@pytest.mark.platform_arm_ascend_training
243@pytest.mark.platform_x86_ascend_training
244@pytest.mark.env_onecard
245def test_scatter_update():
246    input_x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mstype.float32)
247    indices = Tensor(np.array([[0, 0], [1, 1]]), mstype.int32)
248    updates = Tensor(np.array([[[1.0, 2.2, 1.0], [2.0, 1.2, 1.0]], [[2.0, 2.2, 1.0], [3.0, 1.2, 1.0]]]), mstype.float32)
249    expect = Tensor(np.array([[2.0, 1.2, 1.0], [3.0, 1.2, 1.0]]), mstype.float32)
250    net = ScatterUpdateNet(input_x)
251    out = net(indices, updates)
252    np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
253
254
255class ScatterNdAddNet(nn.Cell):
256    def __init__(self, input_x):
257        super(ScatterNdAddNet, self).__init__()
258        self.input_x = Parameter(input_x, name="para")
259        self.scatter_nd_add = P.ScatterNdAdd()
260
261    def construct(self, indices, updates):
262        self.scatter_nd_add(self.input_x, indices, updates)
263        return self.input_x
264
265
266@pytest.mark.level1
267@pytest.mark.platform_arm_ascend_training
268@pytest.mark.platform_x86_ascend_training
269@pytest.mark.env_onecard
270def test_scatter_nd_add():
271    input_x = Tensor(np.array([1, 2, 3, 4, 5, 6, 7, 8]), mstype.float32)
272    indices = Tensor(np.array([[2], [4], [1], [7]]), mstype.int32)
273    updates = Tensor(np.array([6, 7, 8, 9]), mstype.float32)
274    expect = Tensor(np.array([1, 10, 9, 4, 12, 6, 7, 17]), mstype.float32)
275    net = ScatterNdAddNet(input_x)
276    out = net(indices, updates)
277    np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
278
279
280class ScatterNdSubNet(nn.Cell):
281    def __init__(self, input_x):
282        super(ScatterNdSubNet, self).__init__()
283        self.input_x = Parameter(input_x, name="para")
284        self.scatter_nd_sub = P.ScatterNdSub()
285
286    def construct(self, indices, updates):
287        self.scatter_nd_sub(self.input_x, indices, updates)
288        return self.input_x
289
290
291@pytest.mark.level1
292@pytest.mark.platform_arm_ascend_training
293@pytest.mark.platform_x86_ascend_training
294@pytest.mark.env_onecard
295def test_scatter_nd_sub():
296    input_x = Tensor(np.array([1, 2, 3, 4, 5, 6, 7, 8]), mstype.float32)
297    indices = Tensor(np.array([[2], [4], [1], [7]]), mstype.int32)
298    updates = Tensor(np.array([6, 7, 8, 9]), mstype.float32)
299    expect = Tensor(np.array([1, -6, -3, 4, -2, 6, 7, -1]), mstype.float32)
300    net = ScatterNdSubNet(input_x)
301    out = net(indices, updates)
302    np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
303
304
305class ScatterNdUpdateNet(nn.Cell):
306    def __init__(self, input_x):
307        super(ScatterNdUpdateNet, self).__init__()
308        self.input_x = Parameter(input_x, name="para")
309        self.scatter_nd_update = P.ScatterNdUpdate()
310
311    def construct(self, indices, updates):
312        self.scatter_nd_update(self.input_x, indices, updates)
313        return self.input_x
314
315
316@pytest.mark.level1
317@pytest.mark.platform_arm_ascend_training
318@pytest.mark.platform_x86_ascend_training
319@pytest.mark.env_onecard
320def test_scatter_nd_update():
321    input_x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mstype.float32)
322    indices = Tensor(np.array([[0, 0], [1, 1]]), mstype.int32)
323    updates = Tensor(np.array([1.0, 2.2]), mstype.float32)
324    expect = Tensor(np.array([[1., 0.3, 3.6], [0.4, 2.2, -3.2]]), mstype.float32)
325    net = ScatterNdUpdateNet(input_x)
326    out = net(indices, updates)
327    np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
328
329
330class ScatterNonAliasingAddNet(nn.Cell):
331    def __init__(self, input_x):
332        super(ScatterNonAliasingAddNet, self).__init__()
333        self.input_x = Parameter(input_x, name="para")
334        self.scatter_non_aliasing_add = P.ScatterNonAliasingAdd()
335
336    def construct(self, indices, updates):
337        out = self.scatter_non_aliasing_add(self.input_x, indices, updates)
338        return out
339
340
341@pytest.mark.level1
342@pytest.mark.platform_arm_ascend_training
343@pytest.mark.platform_x86_ascend_training
344@pytest.mark.env_onecard
345def test_scatter_non_aliasing_add():
346    input_x = Tensor(np.array([1, 2, 3, 4, 5, 6, 7, 8]), mstype.float32)
347    indices = Tensor(np.array([[2], [4], [1], [7]]), mstype.int32)
348    updates = Tensor(np.array([6, 7, 8, 9]), mstype.float32)
349    expect = Tensor(np.array([1.0, 10.0, 9.0, 4.0, 12.0, 6.0, 7.0, 17.0]), mstype.float32)
350    net = ScatterNonAliasingAddNet(input_x)
351    out = net(indices, updates)
352    np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
353
354
355class SummaryNet(nn.Cell):
356    def __init__(self):
357        super().__init__()
358        self.scalar_summary = P.ScalarSummary()
359        self.image_summary = P.ImageSummary()
360        self.tensor_summary = P.TensorSummary()
361        self.histogram_summary = P.HistogramSummary()
362
363    def construct(self, image_tensor):
364        self.image_summary("image", image_tensor)
365        self.tensor_summary("tensor", image_tensor)
366        self.histogram_summary("histogram", image_tensor)
367        scalar = image_tensor[0][0][0][0]
368        self.scalar_summary("scalar", scalar)
369        return scalar
370
371
372def train_summary_record(test_writer, steps):
373    """Train and record summary."""
374    net = SummaryNet()
375    out_me_dict = {}
376    for i in range(0, steps):
377        image_tensor = Tensor(np.array([[[[i]]]]).astype(np.float32))
378        out_put = net(image_tensor)
379        test_writer.record(i)
380        out_me_dict[i] = out_put.asnumpy()
381    return out_me_dict
382
383
384@pytest.mark.level1
385@pytest.mark.platform_arm_ascend_training
386@pytest.mark.platform_x86_ascend_training
387@pytest.mark.env_onecard
388@security_off_wrap
389def test_summary():
390    with tempfile.TemporaryDirectory() as tmp_dir:
391        steps = 2
392        with SummaryRecord(tmp_dir) as test_writer:
393            train_summary_record(test_writer, steps=steps)
394
395            file_name = os.path.realpath(test_writer.log_dir)
396        with SummaryReader(file_name) as summary_writer:
397            for _ in range(steps):
398                event = summary_writer.read_event()
399                tags = set(value.tag for value in event.summary.value)
400                assert tags == {'tensor', 'histogram', 'scalar', 'image'}
401
402
403@pytest.mark.level0
404@pytest.mark.platform_arm_ascend_training
405@pytest.mark.platform_x86_ascend_training
406@pytest.mark.env_onecard
407def test_igamma():
408    class IGammaTest(nn.Cell):
409        def __init__(self):
410            super().__init__()
411            self.igamma = nn.IGamma()
412
413        def construct(self, x, a):
414            return self.igamma(a=a, x=x)
415
416    x = 4.22
417    a = 2.29
418    net = IGammaTest()
419    out = net(Tensor(x, mstype.float32), Tensor(a, mstype.float32))
420    expect = scipy.special.gammainc(a, x)
421    assert np.allclose(out.asnumpy(), expect, rtol=1e-5, atol=1e-5, equal_nan=True)
422