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.ops import operations as P 23from mindspore.ops.operations import _inner_ops as inner 24 25class NetMul(nn.Cell): 26 def __init__(self): 27 super(NetMul, self).__init__() 28 self.mul = P.Mul() 29 30 def construct(self, x, y): 31 return self.mul(x, y) 32 33 34def mul(nptype): 35 x0_np = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(nptype) 36 y0_np = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(nptype) 37 x1_np = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(nptype) 38 y1_np = np.random.uniform(-2, 2, (2, 1, 4, 4)).astype(nptype) 39 x2_np = np.random.uniform(-2, 2, (2, 1, 1, 4)).astype(nptype) 40 y2_np = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(nptype) 41 x3_np = np.random.uniform(-2, 2, 1).astype(nptype) 42 y3_np = np.random.uniform(-2, 2, 1).astype(nptype) 43 x4_np = np.array(78).astype(nptype) 44 y4_np = np.array(37.5).astype(nptype) 45 46 x0 = Tensor(x0_np) 47 y0 = Tensor(y0_np) 48 x1 = Tensor(x1_np) 49 y1 = Tensor(y1_np) 50 x2 = Tensor(x2_np) 51 y2 = Tensor(y2_np) 52 x3 = Tensor(x3_np) 53 y3 = Tensor(y3_np) 54 x4 = Tensor(x4_np) 55 y4 = Tensor(y4_np) 56 57 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 58 mul_net = NetMul() 59 output0 = mul_net(x0, y0) 60 expect0 = np.multiply(x0_np, y0_np) 61 diff0 = output0.asnumpy() - expect0 62 error0 = np.ones(shape=expect0.shape) * 1.0e-5 63 assert np.all(diff0 < error0) 64 assert output0.shape == expect0.shape 65 66 output1 = mul_net(x1, y1) 67 expect1 = np.multiply(x1_np, y1_np) 68 diff1 = output1.asnumpy() - expect1 69 error1 = np.ones(shape=expect1.shape) * 1.0e-5 70 assert np.all(diff1 < error1) 71 assert output1.shape == expect1.shape 72 73 output2 = mul_net(x2, y2) 74 expect2 = np.multiply(x2_np, y2_np) 75 diff2 = output2.asnumpy() - expect2 76 error2 = np.ones(shape=expect2.shape) * 1.0e-5 77 assert np.all(diff2 < error2) 78 assert output2.shape == expect2.shape 79 80 output3 = mul_net(x3, y3) 81 expect3 = np.multiply(x3_np, y3_np) 82 diff3 = output3.asnumpy() - expect3 83 error3 = np.ones(shape=expect3.shape) * 1.0e-5 84 assert np.all(diff3 < error3) 85 assert output3.shape == expect3.shape 86 87 output4 = mul_net(x4, y4) 88 expect4 = np.multiply(x4_np, y4_np) 89 diff4 = output4.asnumpy() - expect4 90 error4 = np.ones(shape=expect4.shape) * 1.0e-5 91 assert np.all(diff4 < error4) 92 assert output4.shape == expect4.shape 93 94 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 95 mul_net = NetMul() 96 output0 = mul_net(x0, y0) 97 expect0 = np.multiply(x0_np, y0_np) 98 diff0 = output0.asnumpy() - expect0 99 error0 = np.ones(shape=expect0.shape) * 1.0e-5 100 assert np.all(diff0 < error0) 101 assert output0.shape == expect0.shape 102 103 output1 = mul_net(x1, y1) 104 expect1 = np.multiply(x1_np, y1_np) 105 diff1 = output1.asnumpy() - expect1 106 error1 = np.ones(shape=expect1.shape) * 1.0e-5 107 assert np.all(diff1 < error1) 108 assert output1.shape == expect1.shape 109 110 output2 = mul_net(x2, y2) 111 expect2 = np.multiply(x2_np, y2_np) 112 diff2 = output2.asnumpy() - expect2 113 error2 = np.ones(shape=expect2.shape) * 1.0e-5 114 assert np.all(diff2 < error2) 115 assert output2.shape == expect2.shape 116 117 output3 = mul_net(x3, y3) 118 expect3 = np.multiply(x3_np, y3_np) 119 diff3 = output3.asnumpy() - expect3 120 error3 = np.ones(shape=expect3.shape) * 1.0e-5 121 assert np.all(diff3 < error3) 122 assert output3.shape == expect3.shape 123 124 output4 = mul_net(x4, y4) 125 expect4 = np.multiply(x4_np, y4_np) 126 diff4 = output4.asnumpy() - expect4 127 error4 = np.ones(shape=expect4.shape) * 1.0e-5 128 assert np.all(diff4 < error4) 129 assert output4.shape == expect4.shape 130 131@pytest.mark.level0 132@pytest.mark.platform_x86_gpu_training 133@pytest.mark.env_onecard 134def test_mul_float64(): 135 mul(np.float64) 136 137@pytest.mark.level0 138@pytest.mark.platform_x86_gpu_training 139@pytest.mark.env_onecard 140def test_mul_float32(): 141 mul(np.float32) 142 143@pytest.mark.level1 144@pytest.mark.platform_x86_gpu_training 145@pytest.mark.env_onecard 146def test_mul_float16(): 147 mul(np.float16) 148 149@pytest.mark.level1 150@pytest.mark.platform_x86_gpu_training 151@pytest.mark.env_onecard 152def test_mul_int64(): 153 mul(np.int64) 154 155@pytest.mark.level1 156@pytest.mark.platform_x86_gpu_training 157@pytest.mark.env_onecard 158def test_mul_int32(): 159 mul(np.int32) 160 161class NetMul_dynamic(nn.Cell): 162 def __init__(self): 163 super(NetMul_dynamic, self).__init__() 164 self.mul = P.Mul() 165 self.test_dynamic = inner.GpuConvertToDynamicShape() 166 167 def construct(self, x, y): 168 x = self.test_dynamic(x) 169 y = self.test_dynamic(y) 170 out = self.mul(x, y) 171 return out 172 173 174def mul_dynamic(nptype): 175 x1_np = np.array([78]).astype(nptype) 176 y1_np = np.array([37.5]).astype(nptype) 177 x2_np = np.random.uniform(-2, 2, (2, 1, 1, 4)).astype(nptype) 178 y2_np = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(nptype) 179 180 x1 = Tensor(x1_np) 181 y1 = Tensor(y1_np) 182 x2 = Tensor(x2_np) 183 y2 = Tensor(y2_np) 184 185 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 186 187 mul_net = NetMul_dynamic() 188 189 output1 = mul_net(x1, y1) 190 output2 = mul_net(x2, y2) 191 expect1 = np.multiply(x1_np, y1_np) 192 expect2 = np.multiply(x2_np, y2_np) 193 diff1 = output1.asnumpy() - expect1 194 diff2 = output2.asnumpy() - expect2 195 error1 = np.ones(shape=expect1.shape) * 1.0e-5 196 assert np.all(diff1 < error1) 197 assert output1.shape == expect1.shape 198 error2 = np.ones(shape=expect2.shape) * 1.0e-5 199 assert np.all(diff2 < error2) 200 assert output2.shape == expect2.shape 201 202@pytest.mark.level0 203@pytest.mark.platform_x86_gpu_training 204@pytest.mark.env_onecard 205def test_mul_dynamic_float64(): 206 mul_dynamic(np.float64) 207 208@pytest.mark.level0 209@pytest.mark.platform_x86_gpu_training 210@pytest.mark.env_onecard 211def test_mul_dynamic_float32(): 212 mul_dynamic(np.float32) 213 214@pytest.mark.level1 215@pytest.mark.platform_x86_gpu_training 216@pytest.mark.env_onecard 217def test_mul_dynamic_float16(): 218 mul_dynamic(np.float16) 219 220@pytest.mark.level1 221@pytest.mark.platform_x86_gpu_training 222@pytest.mark.env_onecard 223def test_mul_dynamic_int64(): 224 mul_dynamic(np.int64) 225 226@pytest.mark.level1 227@pytest.mark.platform_x86_gpu_training 228@pytest.mark.env_onecard 229def test_mul_dynamic_int32(): 230 mul_dynamic(np.int32) 231