1# Copyright 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 import composite as C 24from mindspore.ops.operations import _inner_ops as inner 25 26class MatMulNet(nn.Cell): 27 def __init__(self): 28 super(MatMulNet, self).__init__() 29 self.matmul = P.MatMul() 30 31 def construct(self, x, y): 32 return self.matmul(x, y) 33 34 35class MatMul_d(nn.Cell): 36 def __init__(self): 37 super(MatMul_d, self).__init__() 38 self.test_dynamic = inner.GpuConvertToDynamicShape() 39 self.matmul = P.MatMul() 40 41 def construct(self, x, y): 42 x = self.test_dynamic(x) 43 y = self.test_dynamic(y) 44 return self.matmul(x, y) 45 46 47class MatMulComposite(nn.Cell): 48 def __init__(self): 49 super(MatMulComposite, self).__init__() 50 self.matmul = C.matmul 51 52 def construct(self, x, y): 53 return self.matmul(x, y) 54 55 56@pytest.mark.level0 57@pytest.mark.platform_x86_gpu_training 58@pytest.mark.env_onecard 59def test_MatMul_dynamic(): 60 61 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 62 net = MatMul_d() 63 64 x1 = np.arange(2).reshape(1, 2).astype(np.float32) 65 y1 = np.arange(4).reshape(2, 2).astype(np.float32) 66 output1 = net(Tensor(x1), Tensor(y1)) 67 expect1 = np.matmul(x1, y1) 68 np.testing.assert_array_almost_equal(output1.asnumpy(), expect1) 69 70 x2 = np.arange(102).reshape(34, 3).astype(np.float32) 71 y2 = np.arange(18).reshape(3, 6).astype(np.float32) 72 output2 = net(Tensor(x2), Tensor(y2)) 73 expect2 = np.matmul(x2, y2) 74 np.testing.assert_array_almost_equal(output2.asnumpy(), expect2) 75 76 77@pytest.mark.level0 78@pytest.mark.platform_x86_gpu_training 79@pytest.mark.env_onecard 80def test_matmul_float64(): 81 82 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 83 net = MatMulNet() 84 85 x = np.arange(102).reshape(34, 3).astype(np.float64) 86 y = np.arange(18).reshape(3, 6).astype(np.float64) 87 output = net(Tensor(x), Tensor(y)) 88 expect = np.matmul(x, y) 89 np.testing.assert_array_almost_equal(output.asnumpy(), expect) 90 91@pytest.mark.level0 92@pytest.mark.platform_x86_gpu_training 93@pytest.mark.env_onecard 94def test_matmul_composite(): 95 96 context.set_context(mode=context.GRAPH_MODE, device_target='GPU') 97 net = MatMulComposite() 98 99 scalars = [np.random.randn(1).astype(np.float32), np.random.randn(1).astype(np.float32), 100 np.random.randn(1, 1).astype(np.float32), 101 np.random.randn(1, 1, 1).astype(np.float32)] 102 for x in scalars: 103 for y in scalars: 104 output = net(Tensor(x), Tensor(y)) 105 expect = np.matmul(x, y) 106 np.testing.assert_array_almost_equal(output.asnumpy(), expect, decimal=4) 107 108 broadcastables = [ 109 np.random.randn(3).astype(np.float32), np.random.randn(3).astype(np.float32), 110 np.random.randn(6).astype(np.float32), np.random.randn(6, 4).astype(np.float32), 111 np.random.randn(5, 2).astype(np.float32), np.random.randn(2).astype(np.float32), 112 np.random.randn(2, 9).astype(np.float32), np.random.randn(9, 8).astype(np.float32), 113 np.random.randn(6).astype(np.float32), np.random.randn(2, 6, 5).astype(np.float32), 114 np.random.randn(9, 2, 7).astype(np.float32), np.random.randn(7).astype(np.float32), 115 np.random.randn(5, 2, 4).astype(np.float32), np.random.randn(6, 1, 4, 9).astype(np.float32), 116 np.random.randn(7, 1, 5, 3, 2).astype(np.float32), np.random.randn(8, 1, 6, 1, 2, 9).astype(np.float32) 117 ] 118 for i in range(8): 119 x = broadcastables[2*i] 120 y = broadcastables[2*i + 1] 121 output = net(Tensor(x), Tensor(y)) 122 expect = np.matmul(x, y) 123 np.testing.assert_array_almost_equal(output.asnumpy(), expect, decimal=4) 124