• 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""" test numpy ops """
16import numpy as np
17
18import mindspore.numpy as mnp
19from mindspore import Tensor
20from mindspore.nn import Cell
21import mindspore.context as context
22from ....mindspore_test_framework.mindspore_test import mindspore_test
23from ....mindspore_test_framework.pipeline.forward.compile_forward \
24    import pipeline_for_compile_forward_ge_graph_for_case_by_case_config
25
26context.set_context(mode=context.GRAPH_MODE)
27
28class MeshGrid(Cell):
29    def construct(self, a, b, c, d):
30        ret = mnp.meshgrid(a, b, c, d)
31        return ret
32
33
34class Choose(Cell):
35    def construct(self, a, b):
36        ret = mnp.choose(a, b)
37        return ret
38
39
40class Histogram(Cell):
41    def construct(self, a):
42        ret = mnp.histogram(a)
43        return ret
44
45
46class Norm(Cell):
47    def construct(self, a):
48        ret = mnp.norm(a)
49        return ret
50
51
52class Cross(Cell):
53    def construct(self, a, b):
54        ret = mnp.cross(a, b)
55        return ret
56
57
58class Stack(Cell):
59    def construct(self, a, b):
60        ret = mnp.stack((a, b))
61        return ret
62
63
64class Correlate(Cell):
65    def construct(self, a, b):
66        ret = mnp.correlate(a, b)
67        return ret
68
69
70class Split(Cell):
71    def construct(self, tensor):
72        a = mnp.split(tensor, indices_or_sections=1)
73        b = mnp.split(tensor, indices_or_sections=3)
74        c = mnp.array_split(tensor, indices_or_sections=1)
75        d = mnp.array_split(tensor, indices_or_sections=3, axis=-1)
76        return a, b, c, d
77
78
79class MatrixPower(Cell):
80    def construct(self, tensor):
81        a = mnp.matrix_power(tensor, 3)
82        return a
83
84
85class RavelMultiIndex(Cell):
86    def construct(self, tensor):
87        a = mnp.ravel_multi_index(tensor, (7, 6))
88        b = mnp.ravel_multi_index(tensor, (7, 6), order='F')
89        c = mnp.ravel_multi_index(tensor, (4, 6), mode='clip')
90        d = mnp.ravel_multi_index(tensor, (4, 4), mode='wrap')
91        return a, b, c, d
92
93
94class GeomSpace(Cell):
95    def construct(self, start):
96        a = mnp.geomspace(1, 256, num=9)
97        b = mnp.geomspace(1, 256, num=8, endpoint=False)
98        c = mnp.geomspace(start, [1000, 2000, 3000], num=4)
99        d = mnp.geomspace(start, [1000, 2000, 3000], num=4, endpoint=False, axis=-1)
100        return a, b, c, d
101
102
103class Arange(Cell):
104    def construct(self):
105        a = mnp.arange(10)
106        b = mnp.arange(0, 10)
107        c = mnp.arange(0.1, 9.9)
108        return a, b, c
109
110
111class Eye(Cell):
112    def construct(self):
113        res = []
114        for n in range(1, 5):
115            for k in range(0, 5):
116                res.append(mnp.eye(10, n, k))
117        return res
118
119
120class Trace(Cell):
121    def construct(self, arr):
122        a = mnp.trace(arr, offset=-1, axis1=0, axis2=1)
123        b = mnp.trace(arr, offset=0, axis1=1, axis2=0)
124        return a, b
125
126
127class Pad(Cell):
128    def construct(self, arr1, arr2):
129        a = mnp.pad(arr1, ((1, 1), (2, 2), (3, 4)))
130        b = mnp.pad(arr1, ((1, 1), (2, 2), (3, 4)), mode="mean", stat_length=((1, 2), (2, 10), (3, 4)))
131        c = mnp.pad(arr1, ((1, 1), (2, 2), (3, 4)), mode="edge")
132        d = mnp.pad(arr1, ((1, 1), (2, 2), (3, 4)), mode="wrap")
133        e = mnp.pad(arr1, ((1, 3), (5, 2), (3, 0)), mode="linear_ramp", end_values=((0, 10), (9, 1), (-10, 99)))
134        f = mnp.pad(arr2, ((10, 13), (5, 12), (3, 0), (2, 6)), mode='symmetric', reflect_type='even')
135        g = mnp.pad(arr2, ((10, 13)), mode='reflect', reflect_type='even')
136        return a, b, c, d, e, f, g
137
138
139class Where(Cell):
140    def construct(self, a, b, c):
141        ret = mnp.where(a, b, c)
142        return ret
143
144
145class Select(Cell):
146    def construct(self, a, b):
147        ret = mnp.select(a, b)
148        return ret
149
150
151class IsClose(Cell):
152    def construct(self, a, b):
153        ret = mnp.isclose(a, b)
154        return ret
155
156
157class Average(Cell):
158    def construct(self, a):
159        ret = mnp.average(a)
160        return ret
161
162
163class Remainder(Cell):
164    def construct(self, a, b):
165        ret = mnp.remainder(a, b)
166        return ret
167
168
169class Diff(Cell):
170    def construct(self, a):
171        ret1 = mnp.diff(a)
172        ret2 = mnp.ediff1d(a)
173        return ret1, ret2
174
175
176class Trapz(Cell):
177    def construct(self, arr):
178        a = mnp.trapz(arr, x=[-2, 1, 2], axis=1)
179        b = mnp.trapz(arr, dx=3, axis=0)
180        return a, b
181
182
183class Lcm(Cell):
184    def construct(self, a, b):
185        ret = mnp.lcm(a, b)
186        return ret
187
188
189class Cov(Cell):
190    def construct(self, a):
191        ret = mnp.cov(a, a)
192        return ret
193
194
195class Gradient(Cell):
196    def construct(self, a):
197        ret = mnp.gradient(a)
198        return ret
199
200
201class MultiDot(Cell):
202    def construct(self, a, b, c, d):
203        ret = mnp.multi_dot((a, b, c, d))
204        return ret
205
206
207class Histogramdd(Cell):
208    def construct(self, a):
209        ret = mnp.histogramdd(a)
210        return ret
211
212
213test_cases = [
214    ('MeshGrid', {
215        'block': MeshGrid(),
216        'desc_inputs': [Tensor(np.full(3, 2, dtype=np.float32)),
217                        Tensor(np.full(1, 5, dtype=np.float32)),
218                        Tensor(np.full((2, 3), 9, dtype=np.float32)),
219                        Tensor(np.full((4, 5, 6), 7, dtype=np.float32))],
220    }),
221    ('Norm', {
222        'block': Norm(),
223        'desc_inputs': [Tensor(np.ones((5, 2, 3, 7), dtype=np.float32))],
224    }),
225    ('Cross', {
226        'block': Cross(),
227        'desc_inputs': [Tensor(np.arange(18, dtype=np.int32).reshape(2, 3, 1, 3)),
228                        Tensor(np.arange(9, dtype=np.int32).reshape(1, 3, 3))],
229    }),
230    ('Stack', {
231        'block': Stack(),
232        'desc_inputs': [Tensor(np.arange(9, dtype=np.int32).reshape(3, 3)),
233                        Tensor(np.arange(9, dtype=np.int32).reshape(3, 3)),],
234    }),
235    ('Correlate', {
236        'block': Correlate(),
237        'desc_inputs': [Tensor(np.array([1, 2, 3, 4, 5], dtype=np.int32)),
238                        Tensor(np.array([0, 1], dtype=np.int32)),],
239    }),
240    ('Split', {
241        'block': Split(),
242        'desc_inputs': [Tensor(np.arange(9, dtype=np.float32).reshape(3, 3))],
243    }),
244    ('MatrixPower', {
245        'block': MatrixPower(),
246        'desc_inputs': [Tensor(np.arange(9, dtype=np.float32).reshape(3, 3))],
247    }),
248    ('RavelMultiIndex', {
249        'block': RavelMultiIndex(),
250        'desc_inputs': [Tensor(np.array([[3, 6, 6], [4, 5, 1]], dtype=np.int32))],
251    }),
252    ('GeomSpace', {
253        'block': GeomSpace(),
254        'desc_inputs': [Tensor(np.arange(1, 7, dtype=np.float32).reshape(2, 3))],
255    }),
256    ('Arange', {
257        'block': Arange(),
258        'desc_inputs': [],
259    }),
260    ('Eye', {
261        'block': Eye(),
262        'desc_inputs': [],
263    }),
264    ('Trace', {
265        'block': Trace(),
266        'desc_inputs': [Tensor(np.ones((3, 5), dtype=np.float32))],
267    }),
268    ('Where', {
269        'block': Where(),
270        'desc_inputs': [Tensor(np.full((1, 1, 2), [False, True])),
271                        Tensor(np.full((1, 3, 2), 5, dtype=np.float32)),
272                        Tensor(np.full((2, 1, 1), 7, dtype=np.float32))],
273    }),
274    ('Select', {
275        'block': Select(),
276        'desc_inputs': [Tensor([[True, True, True, False, False], [False, False, True, False, True]]),
277                        Tensor(np.array([[0, 1, 2, 3, 4], [0, 1, 4, 9, 16]], dtype=np.int32))],
278    }),
279    ('IsClose', {
280        'block': IsClose(),
281        'desc_inputs': [Tensor(np.array([0, 1, 2, float('inf'), float('inf'), float('nan')], dtype=np.float32)),
282                        Tensor(np.array([0, 1, -2, float('-inf'), float('inf'), float('nan')], dtype=np.float32))],
283    }),
284    ('Average', {
285        'block': Average(),
286        'desc_inputs': [Tensor(np.array([[1., 2.], [3., 4.]], dtype=np.float32))],
287    }),
288    ('Remainder', {
289        'block': Remainder(),
290        'desc_inputs': [Tensor(np.array([4, 7], dtype=np.int32)),
291                        Tensor(np.array([[1, 2], [3, 4]], dtype=np.int32))],
292    }),
293    ('Diff', {
294        'block': Diff(),
295        'desc_inputs': [Tensor(np.array([1, 3, -1, 0, 4], dtype=np.int32))],
296    }),
297    ('Trapz', {
298        'block': Trapz(),
299        'desc_inputs': [Tensor(np.arange(6, dtype=np.int32).reshape(2, 3))],
300    }),
301    ('Lcm', {
302        'block': Lcm(),
303        'desc_inputs': [Tensor(np.arange(6, dtype=np.int32)),
304                        Tensor(np.array(20, dtype=np.int32))],
305    }),
306    ('Cov', {
307        'block': Cov(),
308        'desc_inputs': [Tensor(np.array([[2., 3., 4., 5.], [0., 2., 3., 4.], [7., 8., 9., 10.]], dtype=np.float32))],
309    }),
310    ('Gradient', {
311        'block': Gradient(),
312        'desc_inputs': [Tensor(np.array([[2., 3., 4., 5.], [0., 2., 3., 4.], [7., 8., 9., 10.]], dtype=np.float32))],
313    }),
314    ('MultiDot', {
315        'block': MultiDot(),
316        'desc_inputs': [Tensor(np.ones((10000, 100), dtype=np.float32)),
317                        Tensor(np.ones((100, 1000), dtype=np.float32)),
318                        Tensor(np.ones((1000, 5), dtype=np.float32)),
319                        Tensor(np.ones((5, 333), dtype=np.float32))],
320    }),
321]
322
323
324@mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
325def test_exec():
326    context.set_context(mode=context.GRAPH_MODE)
327    return test_cases
328