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