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.common.dtype as mstype 20import mindspore.nn as nn 21from mindspore import Tensor, context 22from mindspore.common.api import ms_function 23from mindspore.ops import operations as P 24 25context.set_context(mode=context.GRAPH_MODE, device_target='CPU') 26 27 28class Net(nn.Cell): 29 def __init__(self): 30 super(Net, self).__init__() 31 self.mul = P.Mul() 32 33 @ms_function 34 def construct(self, x, y): 35 return self.mul(x, y) 36 37 38@pytest.mark.level0 39@pytest.mark.platform_x86_cpu 40@pytest.mark.env_onecard 41def test_mul(): 42 x0 = Tensor(np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32)) 43 y0 = Tensor(np.random.uniform(-2, 2, (1, 1, 1, 1)).astype(np.float32)) 44 x1 = Tensor(np.random.uniform(-2, 2, (1, 3, 1, 4)).astype(np.float32)) 45 y1 = Tensor(np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32)) 46 x2 = Tensor(np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32)) 47 y2 = Tensor(2, mstype.float32) 48 x3 = Tensor(2, mstype.float32) 49 y3 = Tensor(2, mstype.float32) 50 x4 = Tensor(np.random.uniform(-2, 2, (4)).astype(np.float32)) 51 y4 = Tensor(np.random.uniform(-2, 2, (4, 4)).astype(np.float32)) 52 mul = Net() 53 out = mul(x0, y0).asnumpy() 54 exp = x0.asnumpy() * y0.asnumpy() 55 diff = np.abs(out - exp) 56 err = np.ones(shape=exp.shape) * 1.0e-5 57 assert np.all(diff < err) 58 assert out.shape == exp.shape 59 60 out = mul(x1, y1).asnumpy() 61 exp = x1.asnumpy() * y1.asnumpy() 62 diff = np.abs(out - exp) 63 err = np.ones(shape=exp.shape) * 1.0e-5 64 assert np.all(diff < err) 65 assert out.shape == exp.shape 66 67 out = mul(x2, y2).asnumpy() 68 exp = x2.asnumpy() * y2.asnumpy() 69 diff = np.abs(out - exp) 70 err = np.ones(shape=exp.shape) * 1.0e-5 71 assert np.all(diff < err) 72 assert out.shape == exp.shape 73 74 out = mul(x3, y3).asnumpy() 75 exp = x3.asnumpy() * y3.asnumpy() 76 diff = np.abs(out - exp) 77 err = np.ones(shape=exp.shape) * 1.0e-5 78 assert np.all(diff < err) 79 assert out.shape == exp.shape 80 81 out = mul(x4, y4).asnumpy() 82 exp = x4.asnumpy() * y4.asnumpy() 83 diff = np.abs(out - exp) 84 err = np.ones(shape=exp.shape) * 1.0e-5 85 assert np.all(diff < err) 86 assert out.shape == exp.shape 87 88 89@pytest.mark.level0 90@pytest.mark.platform_x86_cpu 91@pytest.mark.env_onecard 92def test_mul_int32(): 93 x0 = Tensor(np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.int32)) 94 y0 = Tensor(np.random.uniform(-2, 2, (1, 1, 1, 1)).astype(np.int32)) 95 x1 = Tensor(np.random.uniform(-2, 2, (1, 3, 1, 4)).astype(np.int32)) 96 y1 = Tensor(np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.int32)) 97 x2 = Tensor(np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.int32)) 98 y2 = Tensor(2, mstype.int32) 99 x3 = Tensor(2, mstype.int32) 100 y3 = Tensor(2, mstype.int32) 101 x4 = Tensor(np.random.uniform(-2, 2, (4)).astype(np.int32)) 102 y4 = Tensor(np.random.uniform(-2, 2, (4, 4)).astype(np.int32)) 103 mul = Net() 104 out = mul(x0, y0).asnumpy() 105 exp = x0.asnumpy() * y0.asnumpy() 106 diff = np.abs(out - exp) 107 err = np.ones(shape=exp.shape) * 1.0e-5 108 assert np.all(diff < err) 109 assert out.shape == exp.shape 110 111 out = mul(x1, y1).asnumpy() 112 exp = x1.asnumpy() * y1.asnumpy() 113 diff = np.abs(out - exp) 114 err = np.ones(shape=exp.shape) * 1.0e-5 115 assert np.all(diff < err) 116 assert out.shape == exp.shape 117 118 out = mul(x2, y2).asnumpy() 119 exp = x2.asnumpy() * y2.asnumpy() 120 diff = np.abs(out - exp) 121 err = np.ones(shape=exp.shape) * 1.0e-5 122 assert np.all(diff < err) 123 assert out.shape == exp.shape 124 125 out = mul(x3, y3).asnumpy() 126 exp = x3.asnumpy() * y3.asnumpy() 127 diff = np.abs(out - exp) 128 err = np.ones(shape=exp.shape) * 1.0e-5 129 assert np.all(diff < err) 130 assert out.shape == exp.shape 131 132 out = mul(x4, y4).asnumpy() 133 exp = x4.asnumpy() * y4.asnumpy() 134 diff = np.abs(out - exp) 135 err = np.ones(shape=exp.shape) * 1.0e-5 136 assert np.all(diff < err) 137 assert out.shape == exp.shape 138