1# Copyright 2020-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 25def cum_prod(nptype): 26 context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU') 27 x0 = np.random.rand(2, 3, 4, 4).astype(nptype) 28 axis0 = 3 29 30 x1 = np.random.rand(2, 3, 4, 4).astype(nptype) 31 axis1 = 3 32 33 x2 = np.random.rand(2, 3, 1, 4).astype(nptype) 34 axis2 = 2 35 36 x3 = np.random.rand(2, 3, 1, 4).astype(nptype) 37 axis3 = 2 38 39 x4 = np.random.rand(2, 3, 4, 4).astype(nptype) 40 axis4 = 1 41 42 x5 = np.random.rand(2, 3).astype(nptype) 43 axis5 = 1 44 45 x6 = np.random.rand(1, 1, 1, 1).astype(nptype) 46 axis6 = 0 47 48 class CumProd(nn.Cell): 49 def __init__(self, nptype): 50 super(CumProd, self).__init__() 51 52 self.x0 = Tensor(x0) 53 self.axis0 = axis0 54 55 self.x1 = Tensor(x1) 56 self.axis1 = axis1 57 58 self.x2 = Tensor(x2) 59 self.axis2 = axis2 60 61 self.x3 = Tensor(x3) 62 self.axis3 = axis3 63 64 self.x4 = Tensor(x4) 65 self.axis4 = axis4 66 67 self.x5 = Tensor(x5) 68 self.axis5 = axis5 69 70 self.x6 = Tensor(x6) 71 self.axis6 = axis6 72 73 @ms_function 74 def construct(self): 75 return (P.CumProd()(self.x0, self.axis0), 76 P.CumProd()(self.x1, self.axis1), 77 P.CumProd()(self.x2, self.axis2), 78 P.CumProd()(self.x3, self.axis3), 79 P.CumProd()(self.x4, self.axis4), 80 P.CumProd()(self.x5, self.axis5), 81 P.CumProd()(self.x6, self.axis6)) 82 83 84 cumprod = CumProd(nptype) 85 output = cumprod() 86 87 expect0 = np.cumprod(x0, axis=axis0) 88 diff0 = abs(output[0].asnumpy() - expect0) 89 error0 = np.ones(shape=expect0.shape) * 1.0e-5 90 assert np.all(diff0 < error0) 91 assert output[0].shape == expect0.shape 92 93 expect1 = np.cumprod(x1, axis=axis1) 94 diff1 = abs(output[1].asnumpy() - expect1) 95 error1 = np.ones(shape=expect1.shape) * 1.0e-5 96 assert np.all(diff1 < error1) 97 assert output[1].shape == expect1.shape 98 99 expect2 = np.cumprod(x2, axis=axis2) 100 diff2 = abs(output[2].asnumpy() - expect2) 101 error2 = np.ones(shape=expect2.shape) * 1.0e-5 102 assert np.all(diff2 < error2) 103 assert output[2].shape == expect2.shape 104 105 expect3 = np.cumprod(x3, axis=axis3) 106 diff3 = abs(output[3].asnumpy() - expect3) 107 error3 = np.ones(shape=expect3.shape) * 1.0e-5 108 assert np.all(diff3 < error3) 109 assert output[3].shape == expect3.shape 110 111 expect4 = np.cumprod(x4, axis=axis4) 112 diff4 = abs(output[4].asnumpy() - expect4) 113 error4 = np.ones(shape=expect4.shape) * 1.0e-5 114 assert np.all(diff4 < error4) 115 assert output[4].shape == expect4.shape 116 117 expect5 = np.cumprod(x5, axis=axis5) 118 diff5 = abs(output[5].asnumpy() - expect5) 119 error5 = np.ones(shape=expect5.shape) * 1.0e-5 120 assert np.all(diff5 < error5) 121 assert output[5].shape == expect5.shape 122 123 expect6 = np.cumprod(x6, axis=axis6) 124 diff6 = abs(output[6].asnumpy() - expect6) 125 error6 = np.ones(shape=expect6.shape) * 1.0e-5 126 assert np.all(diff6 < error6) 127 assert output[6].shape == expect6.shape 128 129@pytest.mark.level0 130@pytest.mark.platform_x86_gpu_training 131@pytest.mark.env_onecard 132def test_cum_prod_uint8(): 133 cum_prod(np.uint8) 134 135@pytest.mark.level1 136@pytest.mark.platform_x86_gpu_training 137@pytest.mark.env_onecard 138def test_cum_prod_int8(): 139 cum_prod(np.int8) 140 141@pytest.mark.level1 142@pytest.mark.platform_x86_gpu_training 143@pytest.mark.env_onecard 144def test_cum_prod_int32(): 145 cum_prod(np.int32) 146 147@pytest.mark.level0 148@pytest.mark.platform_x86_gpu_training 149@pytest.mark.env_onecard 150def test_cum_prod_float16(): 151 cum_prod(np.float16) 152 153@pytest.mark.level0 154@pytest.mark.platform_x86_gpu_training 155@pytest.mark.env_onecard 156def test_cum_prod_float32(): 157 cum_prod(np.float32) 158