1# Copyright 2020-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 18 19import mindspore.context as context 20import mindspore.nn as nn 21from mindspore import Tensor 22from mindspore.ops import operations as P 23 24class NetDiv(nn.Cell): 25 def __init__(self): 26 super(NetDiv, self).__init__() 27 self.div = P.Div() 28 29 def construct(self, x, y): 30 return self.div(x, y) 31 32def div(nptype): 33 x0_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(nptype) 34 y0_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(nptype) 35 x1_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(nptype) 36 y1_np = np.random.randint(1, 5, (2, 1, 4, 4)).astype(nptype) 37 x2_np = np.random.randint(1, 5, (2, 1, 1, 4)).astype(nptype) 38 y2_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(nptype) 39 x3_np = np.random.randint(1, 5, 1).astype(nptype) 40 y3_np = np.random.randint(1, 5, 1).astype(nptype) 41 x4_np = np.array(78).astype(nptype) 42 y4_np = np.array(37.5).astype(nptype) 43 44 x0 = Tensor(x0_np) 45 y0 = Tensor(y0_np) 46 x1 = Tensor(x1_np) 47 y1 = Tensor(y1_np) 48 x2 = Tensor(x2_np) 49 y2 = Tensor(y2_np) 50 x3 = Tensor(x3_np) 51 y3 = Tensor(y3_np) 52 x4 = Tensor(x4_np) 53 y4 = Tensor(y4_np) 54 55 context.set_context(mode=context.GRAPH_MODE, device_target='GPU') 56 div_net = NetDiv() 57 output0 = div_net(x0, y0) 58 expect0 = np.divide(x0_np, y0_np) 59 diff0 = output0.asnumpy() - expect0 60 error0 = np.ones(shape=expect0.shape) * 1.0e-5 61 assert np.all(diff0 < error0) 62 assert output0.shape == expect0.shape 63 64 output1 = div_net(x1, y1) 65 expect1 = np.divide(x1_np, y1_np) 66 diff1 = output1.asnumpy() - expect1 67 error1 = np.ones(shape=expect1.shape) * 1.0e-5 68 assert np.all(diff1 < error1) 69 assert output1.shape == expect1.shape 70 71 output2 = div_net(x2, y2) 72 expect2 = np.divide(x2_np, y2_np) 73 diff2 = output2.asnumpy() - expect2 74 error2 = np.ones(shape=expect2.shape) * 1.0e-5 75 assert np.all(diff2 < error2) 76 assert output2.shape == expect2.shape 77 78 context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU') 79 output3 = div_net(x3, y3) 80 expect3 = np.divide(x3_np, y3_np) 81 diff3 = output3.asnumpy() - expect3 82 error3 = np.ones(shape=expect3.shape) * 1.0e-5 83 assert np.all(diff3 < error3) 84 assert output3.shape == expect3.shape 85 86 output4 = div_net(x4, y4) 87 expect4 = np.divide(x4_np, y4_np) 88 diff4 = output4.asnumpy() - expect4 89 error4 = np.ones(shape=expect4.shape) * 1.0e-5 90 assert np.all(diff4 < error4) 91 assert output4.shape == expect4.shape 92 93@pytest.mark.level0 94@pytest.mark.platform_x86_gpu_training 95@pytest.mark.env_onecard 96def test_div_float64(): 97 div(np.float64) 98 99@pytest.mark.level0 100@pytest.mark.platform_x86_gpu_training 101@pytest.mark.env_onecard 102def test_div_float32(): 103 div(np.float32) 104 105@pytest.mark.level1 106@pytest.mark.platform_x86_gpu_training 107@pytest.mark.env_onecard 108def test_div_float16(): 109 div(np.float16) 110 111@pytest.mark.level1 112@pytest.mark.platform_x86_gpu_training 113@pytest.mark.env_onecard 114def test_div_int64(): 115 div(np.int64) 116 117@pytest.mark.level1 118@pytest.mark.platform_x86_gpu_training 119@pytest.mark.env_onecard 120def test_div_int32(): 121 div(np.int32) 122