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 16import numpy as np 17import pytest 18 19import mindspore.context as context 20import mindspore.nn as nn 21from mindspore import Tensor 22from mindspore.common.api import ms_function 23from mindspore.ops import operations as P 24 25x0 = np.random.rand(2, 3, 4, 4).astype(np.float32) 26axis0 = 3 27keep_dims0 = True 28 29x1 = np.random.rand(2, 3, 4, 4).astype(np.float16) 30axis1 = 3 31keep_dims1 = False 32 33x2 = np.random.rand(2, 3, 1, 4).astype(np.int8) 34axis2 = 2 35keep_dims2 = True 36 37x3 = np.random.rand(2, 3, 1, 4).astype(np.float32) 38axis3 = 2 39keep_dims3 = False 40 41x4 = np.random.rand(2, 3, 4, 4).astype(np.float16) 42axis4 = () 43np_axis4 = None 44keep_dims4 = True 45 46x5 = np.random.rand(2, 3, 4, 4).astype(np.int8) 47axis5 = () 48np_axis5 = None 49keep_dims5 = False 50 51x6 = np.random.rand(2, 3, 4, 4).astype(np.float32) 52axis6 = -2 53keep_dims6 = False 54 55x7 = np.random.rand(2, 3, 4, 4).astype(np.float16) 56axis7 = (-2, -1) 57keep_dims7 = True 58 59x8 = np.random.rand(1, 1, 1, 1).astype(np.float32) 60axis8 = () 61np_axis8 = None 62keep_dims8 = True 63 64 65class ReduceProd(nn.Cell): 66 def __init__(self): 67 super(ReduceProd, self).__init__() 68 69 self.x0 = Tensor(x0) 70 self.axis0 = axis0 71 self.keep_dims0 = keep_dims0 72 73 self.x1 = Tensor(x1) 74 self.axis1 = axis1 75 self.keep_dims1 = keep_dims1 76 77 self.x2 = Tensor(x2) 78 self.axis2 = axis2 79 self.keep_dims2 = keep_dims2 80 81 self.x3 = Tensor(x3) 82 self.axis3 = axis3 83 self.keep_dims3 = keep_dims3 84 85 self.x4 = Tensor(x4) 86 self.axis4 = axis4 87 self.keep_dims4 = keep_dims4 88 89 self.x5 = Tensor(x5) 90 self.axis5 = axis5 91 self.keep_dims5 = keep_dims5 92 93 self.x6 = Tensor(x6) 94 self.axis6 = axis6 95 self.keep_dims6 = keep_dims6 96 97 self.x7 = Tensor(x7) 98 self.axis7 = axis7 99 self.keep_dims7 = keep_dims7 100 101 self.x8 = Tensor(x8) 102 self.axis8 = axis8 103 self.keep_dims8 = keep_dims8 104 105 @ms_function 106 def construct(self): 107 return (P.ReduceProd(self.keep_dims0)(self.x0, self.axis0), 108 P.ReduceProd(self.keep_dims1)(self.x1, self.axis1), 109 P.ReduceProd(self.keep_dims2)(self.x2, self.axis2), 110 P.ReduceProd(self.keep_dims3)(self.x3, self.axis3), 111 P.ReduceProd(self.keep_dims4)(self.x4, self.axis4), 112 P.ReduceProd(self.keep_dims5)(self.x5, self.axis5), 113 P.ReduceProd(self.keep_dims6)(self.x6, self.axis6), 114 P.ReduceProd(self.keep_dims7)(self.x7, self.axis7), 115 P.ReduceProd(self.keep_dims8)(self.x8, self.axis8)) 116 117 118@pytest.mark.level0 119@pytest.mark.platform_x86_gpu_training 120@pytest.mark.env_onecard 121def test_reduce_prod(): 122 context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU') 123 reduce_max = ReduceProd() 124 output = reduce_max() 125 126 expect1 = np.prod(x1, axis=axis1, keepdims=keep_dims1) 127 diff1 = abs(output[1].asnumpy() - expect1) 128 error1 = np.ones(shape=expect1.shape) * 1.0e-5 129 assert np.all(diff1 < error1) 130 assert output[1].shape == expect1.shape 131 132 expect2 = np.prod(x2, axis=axis2, keepdims=keep_dims2) 133 diff2 = abs(output[2].asnumpy() - expect2) 134 error2 = np.ones(shape=expect2.shape) * 1.0e-5 135 assert np.all(diff2 < error2) 136 assert output[2].shape == expect2.shape 137 138 expect3 = np.prod(x3, axis=axis3, keepdims=keep_dims3) 139 diff3 = abs(output[3].asnumpy() - expect3) 140 error3 = np.ones(shape=expect3.shape) * 1.0e-5 141 assert np.all(diff3 < error3) 142 assert output[3].shape == expect3.shape 143 144 expect4 = np.prod(x4, axis=np_axis4, keepdims=keep_dims4) 145 diff4 = abs(output[4].asnumpy() - expect4) 146 error4 = np.ones(shape=expect4.shape) * 1.0e-5 147 assert np.all(diff4 < error4) 148 assert output[4].shape == expect4.shape 149 150 expect5 = np.prod(x5, axis=np_axis5, keepdims=keep_dims5) 151 diff5 = abs(output[5].asnumpy() - expect5) 152 error5 = np.ones(shape=expect5.shape) * 1.0e-5 153 assert np.all(diff5 < error5) 154 assert output[5].shape == expect5.shape 155 156 expect6 = np.prod(x6, axis=axis6, keepdims=keep_dims6) 157 diff6 = abs(output[6].asnumpy() - expect6) 158 error6 = np.ones(shape=expect6.shape) * 1.0e-5 159 assert np.all(diff6 < error6) 160 assert output[6].shape == expect6.shape 161 162 expect7 = np.prod(x7, axis=axis7, keepdims=keep_dims7) 163 diff7 = abs(output[7].asnumpy() - expect7) 164 error7 = np.ones(shape=expect7.shape) * 1.0e-5 165 assert np.all(diff7 < error7) 166 167 expect8 = np.prod(x8, axis=np_axis8, keepdims=keep_dims8) 168 diff8 = abs(output[8].asnumpy() - expect8) 169 error8 = np.ones(shape=expect8.shape) * 1.0e-5 170 assert np.all(diff8 < error8) 171