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 24class NetFloorDiv(nn.Cell): 25 def __init__(self): 26 super(NetFloorDiv, self).__init__() 27 self.floordiv = P.FloorDiv() 28 29 def construct(self, x, y): 30 return self.floordiv(x, y) 31 32@pytest.mark.level0 33@pytest.mark.platform_x86_gpu_training 34@pytest.mark.env_onecard 35def test_floor_div(): 36 x0_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32) 37 y0_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32) 38 x1_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32) 39 y1_np = np.random.randint(1, 5, (2, 1, 4, 4)).astype(np.float32) 40 x2_np = np.random.randint(1, 5, (2, 1, 1, 4, 9)).astype(np.float32) 41 y2_np = np.random.randint(1, 5, (2, 3, 4, 4, 9)).astype(np.float32) 42 x3_np = np.random.randint(1, 5, 1).astype(np.float32) 43 y3_np = np.random.randint(1, 5, 1).astype(np.float32) 44 x4_np = np.array(768).astype(np.float32) 45 y4_np = np.array(3072.5).astype(np.float32) 46 x5_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float16) 47 y5_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float16) 48 x6_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.int32) 49 y6_np = np.random.randint(1, 5, (2, 1, 4, 4)).astype(np.int32) 50 51 x0 = Tensor(x0_np) 52 y0 = Tensor(y0_np) 53 x1 = Tensor(x1_np) 54 y1 = Tensor(y1_np) 55 x2 = Tensor(x2_np) 56 y2 = Tensor(y2_np) 57 x3 = Tensor(x3_np) 58 y3 = Tensor(y3_np) 59 x4 = Tensor(x4_np) 60 y4 = Tensor(y4_np) 61 x5 = Tensor(x5_np) 62 y5 = Tensor(y5_np) 63 x6 = Tensor(x6_np) 64 y6 = Tensor(y6_np) 65 66 context.set_context(mode=context.GRAPH_MODE, device_target='GPU') 67 floor_div = NetFloorDiv() 68 output0 = floor_div(x0, y0) 69 expect0 = np.floor_divide(x0_np, y0_np) 70 diff0 = output0.asnumpy() - expect0 71 error0 = np.ones(shape=expect0.shape) * 1.0e-5 72 assert np.all(diff0 < error0) 73 assert output0.shape == expect0.shape 74 75 output1 = floor_div(x1, y1) 76 expect1 = np.floor_divide(x1_np, y1_np) 77 diff1 = output1.asnumpy() - expect1 78 error1 = np.ones(shape=expect1.shape) * 1.0e-5 79 assert np.all(diff1 < error1) 80 assert output1.shape == expect1.shape 81 82 output2 = floor_div(x2, y2) 83 expect2 = np.floor_divide(x2_np, y2_np) 84 diff2 = output2.asnumpy() - expect2 85 error2 = np.ones(shape=expect2.shape) * 1.0e-5 86 assert np.all(diff2 < error2) 87 assert output2.shape == expect2.shape 88 89 context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU') 90 output3 = floor_div(x3, y3) 91 expect3 = np.floor_divide(x3_np, y3_np) 92 diff3 = output3.asnumpy() - expect3 93 error3 = np.ones(shape=expect3.shape) * 1.0e-5 94 assert np.all(diff3 < error3) 95 assert output3.shape == expect3.shape 96 97 output4 = floor_div(x4, y4) 98 expect4 = np.floor_divide(x4_np, y4_np) 99 diff4 = output4.asnumpy() - expect4 100 error4 = np.ones(shape=expect4.shape) * 1.0e-5 101 assert np.all(diff4 < error4) 102 assert output4.shape == expect4.shape 103 104 output5 = floor_div(x5, y5) 105 expect5 = np.floor_divide(x5_np, y5_np) 106 diff5 = output5.asnumpy() - expect5 107 error5 = np.ones(shape=expect5.shape) * 1.0e-5 108 assert np.all(diff5 < error5) 109 assert output5.shape == expect5.shape 110 111 output6 = floor_div(x6, y6) 112 expect6 = np.floor_divide(x6_np, y6_np) 113 diff6 = output6.asnumpy() - expect6 114 error6 = np.ones(shape=expect6.shape) * 1.0e-5 115 assert np.all(diff6 < error6) 116 assert output6.shape == expect6.shape 117