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# ============================================================================ 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 24 25class NetRealDiv(nn.Cell): 26 def __init__(self): 27 super(NetRealDiv, self).__init__() 28 self.divide = P.RealDiv() 29 30 def construct(self, x, y): 31 return self.divide(x, y) 32 33 34@pytest.mark.level0 35@pytest.mark.platform_x86_cpu 36@pytest.mark.env_onecard 37def test_real_div(): 38 x0_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32) 39 y0_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32) 40 x1_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32) 41 y1_np = np.random.randint(1, 5, (2, 1, 4, 4)).astype(np.float32) 42 x2_np = np.random.randint(1, 5, (2, 1, 1, 4)).astype(np.float32) 43 y2_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32) 44 x3_np = np.random.randint(1, 5, 1).astype(np.float32) 45 y3_np = np.random.randint(1, 5, 1).astype(np.float32) 46 x4_np = np.array(768).astype(np.float32) 47 y4_np = np.array(3072.5).astype(np.float32) 48 49 x0 = Tensor(x0_np) 50 y0 = Tensor(y0_np) 51 x1 = Tensor(x1_np) 52 y1 = Tensor(y1_np) 53 x2 = Tensor(x2_np) 54 y2 = Tensor(y2_np) 55 x3 = Tensor(x3_np) 56 y3 = Tensor(y3_np) 57 x4 = Tensor(x4_np) 58 y4 = Tensor(y4_np) 59 60 context.set_context(mode=context.GRAPH_MODE, device_target='CPU') 61 real_div = NetRealDiv() 62 output0 = real_div(x0, y0) 63 expect0 = np.divide(x0_np, y0_np) 64 diff0 = output0.asnumpy() - expect0 65 error0 = np.ones(shape=expect0.shape) * 1.0e-5 66 assert np.all(diff0 < error0) 67 assert output0.shape == expect0.shape 68 69 output1 = real_div(x1, y1) 70 expect1 = np.divide(x1_np, y1_np) 71 diff1 = output1.asnumpy() - expect1 72 error1 = np.ones(shape=expect1.shape) * 1.0e-5 73 assert np.all(diff1 < error1) 74 assert output1.shape == expect1.shape 75 76 output2 = real_div(x2, y2) 77 expect2 = np.divide(x2_np, y2_np) 78 diff2 = output2.asnumpy() - expect2 79 error2 = np.ones(shape=expect2.shape) * 1.0e-5 80 assert np.all(diff2 < error2) 81 assert output2.shape == expect2.shape 82 83 output3 = real_div(x3, y3) 84 expect3 = np.divide(x3_np, y3_np) 85 diff3 = output3.asnumpy() - expect3 86 error3 = np.ones(shape=expect3.shape) * 1.0e-5 87 assert np.all(diff3 < error3) 88 assert output3.shape == expect3.shape 89 90 output4 = real_div(x4, y4) 91 expect4 = np.divide(x4_np, y4_np) 92 diff4 = output4.asnumpy() - expect4 93 error4 = np.ones(shape=expect4.shape) * 1.0e-5 94 assert np.all(diff4 < error4) 95 assert output4.shape == expect4.shape 96