1# Copyright 2019 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 24from mindspore.ops.operations import _inner_ops as inner 25 26x0 = np.random.rand(2, 3, 4, 4).astype(np.float32) 27axis0 = 3 28keep_dims0 = True 29 30x1 = np.random.rand(2, 3, 4, 4).astype(np.float32) 31axis1 = 3 32keep_dims1 = False 33 34x2 = np.random.rand(2, 3, 1, 4).astype(np.float32) 35axis2 = 2 36keep_dims2 = True 37 38x3 = np.random.rand(2, 3, 1, 4).astype(np.float32) 39axis3 = 2 40keep_dims3 = False 41 42x4 = np.random.rand(2, 3, 4, 1).astype(np.float32) 43axis4 = 3 44keep_dims4 = True 45 46x5 = np.random.rand(2, 3, 4, 1).astype(np.float32) 47axis5 = 3 48keep_dims5 = False 49 50x6 = np.random.rand(2, 3, 4, 4).astype(np.float32) 51axis6 = (1, 2) 52keep_dims6 = False 53 54x7 = np.random.rand(2, 3, 4, 4).astype(np.float32) 55axis7 = (1, 2) 56keep_dims7 = True 57 58x8 = np.random.rand(2, 1, 1, 4).astype(np.float32) 59axis8 = (1, 2) 60keep_dims8 = True 61 62x9 = np.random.rand(2, 1, 1, 4).astype(np.float32) 63axis9 = (1, 2) 64keep_dims9 = False 65 66x10 = np.random.rand(2, 3, 4, 4).astype(np.float32) 67axis10 = (0, 1, 2, 3) 68keep_dims10 = False 69 70x11 = np.random.rand(1, 1, 1, 1).astype(np.float32) 71axis11 = (0, 1, 2, 3) 72keep_dims11 = False 73 74x12 = np.random.rand(2, 3, 4, 4, 5, 6).astype(np.float32) 75axis12 = -2 76keep_dims12 = False 77 78x13 = np.random.rand(2, 3, 4, 4).astype(np.float32) 79axis13 = (-2, -1) 80keep_dims13 = True 81 82x14 = np.random.rand(1, 1, 1, 1).astype(np.float32) 83axis14 = () 84np_axis14 = None 85keep_dims14 = True 86 87 88class ReduceMean(nn.Cell): 89 def __init__(self): 90 super(ReduceMean, self).__init__() 91 92 self.x0 = Tensor(x0) 93 self.axis0 = axis0 94 self.keep_dims0 = keep_dims0 95 96 self.x1 = Tensor(x1) 97 self.axis1 = axis1 98 self.keep_dims1 = keep_dims1 99 100 self.x2 = Tensor(x2) 101 self.axis2 = axis2 102 self.keep_dims2 = keep_dims2 103 104 self.x3 = Tensor(x3) 105 self.axis3 = axis3 106 self.keep_dims3 = keep_dims3 107 108 self.x4 = Tensor(x4) 109 self.axis4 = axis4 110 self.keep_dims4 = keep_dims4 111 112 self.x5 = Tensor(x5) 113 self.axis5 = axis5 114 self.keep_dims5 = keep_dims5 115 116 self.x6 = Tensor(x6) 117 self.axis6 = axis6 118 self.keep_dims6 = keep_dims6 119 120 self.x7 = Tensor(x7) 121 self.axis7 = axis7 122 self.keep_dims7 = keep_dims7 123 124 self.x8 = Tensor(x8) 125 self.axis8 = axis8 126 self.keep_dims8 = keep_dims8 127 128 self.x9 = Tensor(x9) 129 self.axis9 = axis9 130 self.keep_dims9 = keep_dims9 131 132 self.x10 = Tensor(x10) 133 self.axis10 = axis10 134 self.keep_dims10 = keep_dims10 135 136 self.x11 = Tensor(x11) 137 self.axis11 = axis11 138 self.keep_dims11 = keep_dims11 139 140 self.x12 = Tensor(x12) 141 self.axis12 = axis12 142 self.keep_dims12 = keep_dims12 143 144 self.x13 = Tensor(x13) 145 self.axis13 = axis13 146 self.keep_dims13 = keep_dims13 147 148 self.x14 = Tensor(x14) 149 self.axis14 = axis14 150 self.keep_dims14 = keep_dims14 151 152 @ms_function 153 def construct(self): 154 return (P.ReduceMean(self.keep_dims0)(self.x0, self.axis0), 155 P.ReduceMean(self.keep_dims1)(self.x1, self.axis1), 156 P.ReduceMean(self.keep_dims2)(self.x2, self.axis2), 157 P.ReduceMean(self.keep_dims3)(self.x3, self.axis3), 158 P.ReduceMean(self.keep_dims4)(self.x4, self.axis4), 159 P.ReduceMean(self.keep_dims5)(self.x5, self.axis5), 160 P.ReduceMean(self.keep_dims6)(self.x6, self.axis6), 161 P.ReduceMean(self.keep_dims7)(self.x7, self.axis7), 162 P.ReduceMean(self.keep_dims8)(self.x8, self.axis8), 163 P.ReduceMean(self.keep_dims9)(self.x9, self.axis9), 164 P.ReduceMean(self.keep_dims10)(self.x10, self.axis10), 165 P.ReduceMean(self.keep_dims11)(self.x11, self.axis11), 166 P.ReduceMean(self.keep_dims12)(self.x12, self.axis12), 167 P.ReduceMean(self.keep_dims13)(self.x13, self.axis13), 168 P.ReduceMean(self.keep_dims14)(self.x14, self.axis14)) 169 170 171@pytest.mark.level0 172@pytest.mark.platform_x86_gpu_training 173@pytest.mark.env_onecard 174def test_ReduceMean(): 175 context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU') 176 reduce_mean = ReduceMean() 177 output = reduce_mean() 178 179 expect0 = np.mean(x0, axis=axis0, keepdims=keep_dims0) 180 diff0 = abs(output[0].asnumpy() - expect0) 181 error0 = np.ones(shape=expect0.shape) * 1.0e-5 182 assert np.all(diff0 < error0) 183 assert output[0].shape == expect0.shape 184 185 expect1 = np.mean(x1, axis=axis1, keepdims=keep_dims1) 186 diff1 = abs(output[1].asnumpy() - expect1) 187 error1 = np.ones(shape=expect1.shape) * 1.0e-5 188 assert np.all(diff1 < error1) 189 assert output[1].shape == expect1.shape 190 191 expect2 = np.mean(x2, axis=axis2, keepdims=keep_dims2) 192 diff2 = abs(output[2].asnumpy() - expect2) 193 error2 = np.ones(shape=expect2.shape) * 1.0e-5 194 assert np.all(diff2 < error2) 195 assert output[2].shape == expect2.shape 196 197 expect3 = np.mean(x3, axis=axis3, keepdims=keep_dims3) 198 diff3 = abs(output[3].asnumpy() - expect3) 199 error3 = np.ones(shape=expect3.shape) * 1.0e-5 200 assert np.all(diff3 < error3) 201 assert output[3].shape == expect3.shape 202 203 expect4 = np.mean(x4, axis=axis4, keepdims=keep_dims4) 204 diff4 = abs(output[4].asnumpy() - expect4) 205 error4 = np.ones(shape=expect4.shape) * 1.0e-5 206 assert np.all(diff4 < error4) 207 assert output[4].shape == expect4.shape 208 209 expect5 = np.mean(x5, axis=axis5, keepdims=keep_dims5) 210 diff5 = abs(output[5].asnumpy() - expect5) 211 error5 = np.ones(shape=expect5.shape) * 1.0e-5 212 assert np.all(diff5 < error5) 213 assert output[5].shape == expect5.shape 214 215 expect6 = np.mean(x6, axis=axis6, keepdims=keep_dims6) 216 diff6 = abs(output[6].asnumpy() - expect6) 217 error6 = np.ones(shape=expect6.shape) * 1.0e-5 218 assert np.all(diff6 < error6) 219 assert output[6].shape == expect6.shape 220 221 expect7 = np.mean(x7, axis=axis7, keepdims=keep_dims7) 222 diff7 = abs(output[7].asnumpy() - expect7) 223 error7 = np.ones(shape=expect7.shape) * 1.0e-5 224 assert np.all(diff7 < error7) 225 assert output[7].shape == expect7.shape 226 227 expect8 = np.mean(x8, axis=axis8, keepdims=keep_dims8) 228 diff8 = abs(output[8].asnumpy() - expect8) 229 error8 = np.ones(shape=expect8.shape) * 1.0e-5 230 assert np.all(diff8 < error8) 231 assert output[8].shape == expect8.shape 232 233 expect9 = np.mean(x9, axis=axis9, keepdims=keep_dims9) 234 diff9 = abs(output[9].asnumpy() - expect9) 235 error9 = np.ones(shape=expect9.shape) * 1.0e-5 236 assert np.all(diff9 < error9) 237 assert output[9].shape == expect9.shape 238 239 expect10 = np.mean(x10, axis=axis10, keepdims=keep_dims10) 240 diff10 = abs(output[10].asnumpy() - expect10) 241 error10 = np.ones(shape=expect10.shape) * 1.0e-5 242 assert np.all(diff10 < error10) 243 assert output[10].shape == expect10.shape 244 245 expect11 = np.mean(x11, axis=axis11, keepdims=keep_dims11) 246 diff11 = abs(output[11].asnumpy() - expect11) 247 error11 = np.ones(shape=expect11.shape) * 1.0e-5 248 assert np.all(diff11 < error11) 249 assert output[11].shape == expect11.shape 250 251 expect12 = np.mean(x12, axis=axis12, keepdims=keep_dims12) 252 diff12 = abs(output[12].asnumpy() - expect12) 253 error12 = np.ones(shape=expect12.shape) * 1.0e-5 254 assert np.all(diff12 < error12) 255 assert output[12].shape == expect12.shape 256 257 expect13 = np.mean(x13, axis=axis13, keepdims=keep_dims13) 258 diff13 = abs(output[13].asnumpy() - expect13) 259 error13 = np.ones(shape=expect13.shape) * 1.0e-5 260 assert np.all(diff13 < error13) 261 assert output[13].shape == expect13.shape 262 263 expect14 = np.mean(x14, axis=np_axis14, keepdims=keep_dims14) 264 diff14 = abs(output[14].asnumpy() - expect14) 265 error14 = np.ones(shape=expect14.shape) * 1.0e-5 266 assert np.all(diff14 < error14) 267 assert output[14].shape == expect14.shape 268 269class ReduceMeanDynamic(nn.Cell): 270 def __init__(self, x, axis, keepdims=False): 271 super(ReduceMeanDynamic, self).__init__() 272 self.test_dynamic = inner.GpuConvertToDynamicShape() 273 self.reducemean = P.ReduceMean(keep_dims=keepdims) 274 self.x = x 275 self.axis = axis 276 277 def construct(self): 278 dynamic_x = self.test_dynamic(self.x) 279 output = self.reducemean(dynamic_x, self.axis) 280 return output 281 282@pytest.mark.level0 283@pytest.mark.platform_x86_gpu_training 284@pytest.mark.env_onecard 285def test_dynamic_reduce_mean_keepdims_true(): 286 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 287 net1 = ReduceMeanDynamic(Tensor(x14), axis14, keepdims=True) 288 net2 = ReduceMeanDynamic(Tensor(x0), axis0, keepdims=True) 289 output1 = net1() 290 output2 = net2() 291 292 expect_1 = np.mean(x14, axis=np_axis14, keepdims=True) 293 diff_1 = abs(output1.asnumpy() - expect_1) 294 error_1 = np.ones(shape=expect_1.shape) * 1.0e-5 295 assert np.all(diff_1 < error_1) 296 assert output1.shape == expect_1.shape 297 298 expect_2 = np.mean(x0, axis=axis0, keepdims=True) 299 diff_2 = abs(output2.asnumpy() - expect_2) 300 error_2 = np.ones(shape=expect_2.shape) * 1.0e-5 301 assert np.all(diff_2 < error_2) 302 assert output2.shape == expect_2.shape 303 304@pytest.mark.level0 305@pytest.mark.platform_x86_gpu_training 306@pytest.mark.env_onecard 307def test_dynamic_reduce_mean_keepdims_false(): 308 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 309 net = ReduceMeanDynamic(Tensor(x12), axis12, keepdims=False) 310 output = net() 311 312 expect = np.mean(x12, axis=axis12, keepdims=False) 313 diff = abs(output.asnumpy() - expect) 314 error = np.ones(shape=expect.shape) * 1.0e-5 315 assert np.all(diff < error) 316 assert output.shape == expect.shape 317