• 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 numpy as np
16import pytest
17
18import mindspore.context as context
19import mindspore.nn as nn
20import mindspore
21from mindspore import Tensor
22from mindspore.ops import operations as P
23
24context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
25
26
27class SubNet(nn.Cell):
28    def __init__(self):
29        super(SubNet, self).__init__()
30        self.sub = P.Sub()
31
32    def construct(self, x, y):
33        return self.sub(x, y)
34
35
36class DivNet(nn.Cell):
37    def __init__(self):
38        super(DivNet, self).__init__()
39        self.div = P.Div()
40
41    def construct(self, x, y):
42        return self.div(x, y)
43
44
45class FloorDivNet(nn.Cell):
46    def __init__(self):
47        super(FloorDivNet, self).__init__()
48        self.floor_div = P.FloorDiv()
49
50    def construct(self, x, y):
51        return self.floor_div(x, y)
52
53
54class ModNet(nn.Cell):
55    def __init__(self):
56        super(ModNet, self).__init__()
57        self.mod = P.Mod()
58
59    def construct(self, x, y):
60        return self.mod(x, y)
61
62
63class FloorModNet(nn.Cell):
64    def __init__(self):
65        super(FloorModNet, self).__init__()
66        self.floor_mod = P.FloorMod()
67
68    def construct(self, x, y):
69        return self.floor_mod(x, y)
70
71
72@pytest.mark.level0
73@pytest.mark.platform_x86_cpu
74@pytest.mark.env_onecard
75def test_sub():
76    x = np.random.rand(2, 3, 4, 4).astype(np.float32)
77    y = np.random.rand(4, 1).astype(np.float32)
78    net = SubNet()
79    output = net(Tensor(x), Tensor(y, mindspore.float32))
80    expect_output = x - y
81    assert np.all(output.asnumpy() == expect_output)
82
83
84@pytest.mark.level0
85@pytest.mark.platform_x86_cpu
86@pytest.mark.env_onecard
87def test_div():
88    prop = 1 if np.random.random() < 0.5 else -1
89    x0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
90    y0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
91    x1_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
92    y1_np = np.random.randint(1, 100, (2, 1, 4, 4)).astype(np.float32) * prop
93    x2_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.float16) * prop
94    y2_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float16) * prop
95    x3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
96    y3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
97    x4_np = np.array(768).astype(np.float32) * prop
98    y4_np = np.array(3072.5).astype(np.float32) * prop
99    x5_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int32) * prop
100    y5_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
101    x6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
102    y6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
103    x7_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int64) * prop
104    y7_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int64) * prop
105
106    x0 = Tensor(x0_np)
107    y0 = Tensor(y0_np)
108    x1 = Tensor(x1_np)
109    y1 = Tensor(y1_np)
110    x2 = Tensor(x2_np)
111    y2 = Tensor(y2_np)
112    x3 = Tensor(x3_np)
113    y3 = Tensor(y3_np)
114    x4 = Tensor(x4_np)
115    y4 = Tensor(y4_np)
116    x5 = Tensor(x5_np)
117    y5 = Tensor(y5_np)
118    x6 = Tensor(x6_np)
119    y6 = Tensor(y6_np)
120    x7 = Tensor(x7_np)
121    y7 = Tensor(y7_np)
122
123    context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
124    div = DivNet()
125    output0 = div(x0, y0)
126    expect0 = np.divide(x0_np, y0_np)
127    diff0 = output0.asnumpy() - expect0
128    error0 = np.ones(shape=expect0.shape) * 1.0e-5
129    assert np.all(diff0 < error0)
130    assert output0.shape == expect0.shape
131
132    output1 = div(x1, y1)
133    expect1 = np.divide(x1_np, y1_np)
134    diff1 = output1.asnumpy() - expect1
135    error1 = np.ones(shape=expect1.shape) * 1.0e-5
136    assert np.all(diff1 < error1)
137    assert output1.shape == expect1.shape
138
139    output2 = div(x2, y2)
140    expect2 = np.divide(x2_np, y2_np).astype(np.float16)
141    diff2 = output2.asnumpy() - expect2
142    error2 = np.ones(shape=expect2.shape) * 1.0e-5
143    assert np.all(diff2 < error2)
144    assert output2.shape == expect2.shape
145
146    output3 = div(x3, y3)
147    expect3 = np.divide(x3_np, y3_np)
148    diff3 = output3.asnumpy() - expect3
149    error3 = np.ones(shape=expect3.shape) * 1.0e-5
150    assert np.all(diff3 < error3)
151    assert output3.shape == expect3.shape
152
153    output4 = div(x4, y4)
154    expect4 = np.divide(x4_np, y4_np)
155    diff4 = output4.asnumpy() - expect4
156    error4 = np.ones(shape=expect4.shape) * 1.0e-5
157    assert np.all(diff4 < error4)
158    assert output4.shape == expect4.shape
159
160    output5 = div(x5, y5)
161    expect5 = x5_np // y5_np
162    assert np.all(output5.asnumpy() == expect5)
163
164    output6 = div(x6, y6)
165    expect6 = np.divide(x6_np, y6_np)
166    diff6 = output6.asnumpy() - expect6
167    error6 = np.ones(shape=expect6.shape) * 1.0e-5
168    assert np.all(diff6 < error6)
169    assert output6.shape == expect6.shape
170
171    output7 = div(x7, y7)
172    expect7 = np.divide(x7_np, y7_np).astype(np.int64)
173    assert np.all(output7.asnumpy() == expect7)
174    assert output7.shape == expect7.shape
175
176
177@pytest.mark.level0
178@pytest.mark.platform_x86_cpu
179@pytest.mark.env_onecard
180def test_floor_div():
181    prop = 1 if np.random.random() < 0.5 else -1
182    x0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
183    y0_np = np.random.randint(1, 100, (2, 1, 4, 4)).astype(np.float32) * prop
184    x1_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.float16) * prop
185    y1_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float16) * prop
186    x2_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int32) * prop
187    y2_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
188    x3_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
189    y3_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
190    x4_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int64) * prop
191    y4_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int64) * prop
192
193    x0 = Tensor(x0_np)
194    y0 = Tensor(y0_np)
195    x1 = Tensor(x1_np)
196    y1 = Tensor(y1_np)
197    x2 = Tensor(x2_np)
198    y2 = Tensor(y2_np)
199    x3 = Tensor(x3_np)
200    y3 = Tensor(y3_np)
201    x4 = Tensor(x4_np)
202    y4 = Tensor(y4_np)
203
204    context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
205    floor_div = FloorDivNet()
206    output0 = floor_div(x0, y0)
207    expect0 = np.floor_divide(x0_np, y0_np)
208    diff0 = output0.asnumpy() - expect0
209    error0 = np.ones(shape=expect0.shape) * 1.0e-5
210    assert np.all(diff0 < error0)
211    assert output0.shape == expect0.shape
212
213    output1 = floor_div(x1, y1)
214    expect1 = np.floor_divide(x1_np, y1_np)
215    diff1 = output1.asnumpy() - expect1
216    error1 = np.ones(shape=expect1.shape) * 1.0e-5
217    assert np.all(diff1 < error1)
218    assert output1.shape == expect1.shape
219
220    output2 = floor_div(x2, y2)
221    expect2 = np.floor_divide(x2_np, y2_np).astype(np.float16)
222    diff2 = output2.asnumpy() - expect2
223    error2 = np.ones(shape=expect2.shape) * 1.0e-5
224    assert np.all(diff2 < error2)
225    assert output2.shape == expect2.shape
226
227    output3 = floor_div(x3, y3)
228    expect3 = np.floor_divide(x3_np, y3_np)
229    diff3 = output3.asnumpy() - expect3
230    error3 = np.ones(shape=expect3.shape) * 1.0e-5
231    assert np.all(diff3 < error3)
232    assert output3.shape == expect3.shape
233
234    output4 = floor_div(x4, y4)
235    expect4 = np.floor_divide(x4_np, y4_np)
236    diff4 = output4.asnumpy() - expect4
237    error4 = np.ones(shape=expect4.shape) * 1.0e-5
238    assert np.all(diff4 < error4)
239    assert output4.shape == expect4.shape
240
241
242@pytest.mark.level0
243@pytest.mark.platform_x86_cpu
244@pytest.mark.env_onecard
245def test_mod():
246    prop = 1 if np.random.random() < 0.5 else -1
247    x0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
248    y0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
249    x1_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
250    y1_np = np.random.randint(1, 100, (2, 1, 4, 4)).astype(np.float32) * prop
251    x2_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.float16) * prop
252    y2_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float16) * prop
253    x3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
254    y3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
255    x4_np = np.array(768).astype(np.float32) * prop
256    y4_np = np.array(3072.5).astype(np.float32) * prop
257    x5_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int32) * prop
258    y5_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
259    x6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
260    y6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
261    x7_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int64) * prop
262    y7_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int64) * prop
263
264    x0 = Tensor(x0_np)
265    y0 = Tensor(y0_np)
266    x1 = Tensor(x1_np)
267    y1 = Tensor(y1_np)
268    x2 = Tensor(x2_np)
269    y2 = Tensor(y2_np)
270    x3 = Tensor(x3_np)
271    y3 = Tensor(y3_np)
272    x4 = Tensor(x4_np)
273    y4 = Tensor(y4_np)
274    x5 = Tensor(x5_np)
275    y5 = Tensor(y5_np)
276    x6 = Tensor(x6_np)
277    y6 = Tensor(y6_np)
278    x7 = Tensor(x7_np)
279    y7 = Tensor(y7_np)
280
281    context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
282    mod = ModNet()
283    output0 = mod(x0, y0)
284    expect0 = np.mod(x0_np, y0_np)
285    diff0 = output0.asnumpy() - expect0
286    error0 = np.ones(shape=expect0.shape) * 1.0e-5
287    assert np.all(diff0 < error0)
288    assert output0.shape == expect0.shape
289
290    output1 = mod(x1, y1)
291    expect1 = np.mod(x1_np, y1_np)
292    diff1 = output1.asnumpy() - expect1
293    error1 = np.ones(shape=expect1.shape) * 1.0e-5
294    assert np.all(diff1 < error1)
295    assert output1.shape == expect1.shape
296
297    output2 = mod(x2, y2)
298    expect2 = np.mod(x2_np, y2_np).astype(np.float16)
299    diff2 = output2.asnumpy() - expect2
300    error2 = np.ones(shape=expect2.shape) * 1.0e-5
301    assert np.all(diff2 < error2)
302    assert output2.shape == expect2.shape
303
304    output3 = mod(x3, y3)
305    expect3 = np.mod(x3_np, y3_np)
306    diff3 = output3.asnumpy() - expect3
307    error3 = np.ones(shape=expect3.shape) * 1.0e-5
308    assert np.all(diff3 < error3)
309    assert output3.shape == expect3.shape
310
311    output4 = mod(x4, y4)
312    expect4 = np.mod(x4_np, y4_np)
313    diff4 = output4.asnumpy() - expect4
314    error4 = np.ones(shape=expect4.shape) * 1.0e-5
315    assert np.all(diff4 < error4)
316    assert output4.shape == expect4.shape
317
318    output5 = mod(x5, y5)
319    expect5 = np.mod(x5_np, y5_np)
320    assert np.all(output5.asnumpy() == expect5)
321    assert output5.shape == expect5.shape
322
323    output6 = mod(x6, y6)
324    expect6 = np.mod(x6_np, y6_np)
325    diff6 = output6.asnumpy() - expect6
326    error6 = np.ones(shape=expect6.shape) * 1.0e-5
327    assert np.all(diff6 < error6)
328    assert output6.shape == expect6.shape
329
330    output7 = mod(x7, y7)
331    expect7 = np.mod(x7_np, y7_np).astype(np.int64)
332    assert np.all(output7.asnumpy() == expect7)
333    assert output6.shape == expect6.shape
334
335
336@pytest.mark.level0
337@pytest.mark.platform_x86_cpu
338@pytest.mark.env_onecard
339def test_floor_mod():
340    prop = 1 if np.random.random() < 0.5 else -1
341    x0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
342    y0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
343    x1_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
344    y1_np = np.random.randint(1, 100, (2, 1, 4, 4)).astype(np.float32) * prop
345    x2_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.float16) * prop
346    y2_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float16) * prop
347    x3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
348    y3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
349    x4_np = np.array(768).astype(np.float32) * prop
350    y4_np = np.array(3072.5).astype(np.float32) * prop
351    x5_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int32) * prop
352    y5_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
353    x6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
354    y6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
355    x7_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int64) * prop
356    y7_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int64) * prop
357
358    x0 = Tensor(x0_np)
359    y0 = Tensor(y0_np)
360    x1 = Tensor(x1_np)
361    y1 = Tensor(y1_np)
362    x2 = Tensor(x2_np)
363    y2 = Tensor(y2_np)
364    x3 = Tensor(x3_np)
365    y3 = Tensor(y3_np)
366    x4 = Tensor(x4_np)
367    y4 = Tensor(y4_np)
368    x5 = Tensor(x5_np)
369    y5 = Tensor(y5_np)
370    x6 = Tensor(x6_np)
371    y6 = Tensor(y6_np)
372    x7 = Tensor(x7_np)
373    y7 = Tensor(y7_np)
374
375    context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
376    floor_mod = FloorModNet()
377    output0 = floor_mod(x0, y0)
378    expect0 = np.mod(x0_np, y0_np)
379    diff0 = output0.asnumpy() - expect0
380    error0 = np.ones(shape=expect0.shape) * 1.0e-5
381    assert np.all(diff0 < error0)
382    assert output0.shape == expect0.shape
383
384    output1 = floor_mod(x1, y1)
385    expect1 = np.mod(x1_np, y1_np)
386    diff1 = output1.asnumpy() - expect1
387    error1 = np.ones(shape=expect1.shape) * 1.0e-5
388    assert np.all(diff1 < error1)
389    assert output1.shape == expect1.shape
390
391    output2 = floor_mod(x2, y2)
392    expect2 = np.mod(x2_np, y2_np).astype(np.float16)
393    diff2 = output2.asnumpy() - expect2
394    error2 = np.ones(shape=expect2.shape) * 1.0e-5
395    assert np.all(diff2 < error2)
396    assert output2.shape == expect2.shape
397
398    output3 = floor_mod(x3, y3)
399    expect3 = np.mod(x3_np, y3_np)
400    diff3 = output3.asnumpy() - expect3
401    error3 = np.ones(shape=expect3.shape) * 1.0e-5
402    assert np.all(diff3 < error3)
403    assert output3.shape == expect3.shape
404
405    output4 = floor_mod(x4, y4)
406    expect4 = np.mod(x4_np, y4_np)
407    diff4 = output4.asnumpy() - expect4
408    error4 = np.ones(shape=expect4.shape) * 1.0e-5
409    assert np.all(diff4 < error4)
410    assert output4.shape == expect4.shape
411
412    output5 = floor_mod(x5, y5)
413    expect5 = np.mod(x5_np, y5_np)
414    assert np.all(output5.asnumpy() == expect5)
415    assert output5.shape == expect5.shape
416
417    output6 = floor_mod(x6, y6)
418    expect6 = np.mod(x6_np, y6_np)
419    diff6 = output6.asnumpy() - expect6
420    error6 = np.ones(shape=expect6.shape) * 1.0e-5
421    assert np.all(diff6 < error6)
422    assert output6.shape == expect6.shape
423
424    output7 = floor_mod(x7, y7)
425    expect7 = np.mod(x7_np, y7_np).astype(np.int64)
426    assert np.all(output7.asnumpy() == expect7)
427    assert output6.shape == expect6.shape
428
429
430test_sub()
431test_div()
432test_floor_div()
433test_mod()
434test_floor_mod()
435