You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_matmul.py 3.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. import numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.ops import operations as P
  21. np.random.seed(100)
  22. class MatMulNet(nn.Cell):
  23. def __init__(self, transpose_a=False, transpose_b=False):
  24. super(MatMulNet, self).__init__()
  25. self.matmul = P.MatMul(transpose_a, transpose_b)
  26. def construct(self, x, y):
  27. return self.matmul(x, y)
  28. def judge_result_correct(result, expect):
  29. assert result.dtype == expect.dtype
  30. assert result.shape == expect.shape
  31. assert np.allclose(result, expect)
  32. @pytest.mark.level0
  33. @pytest.mark.platform_x86_cpu
  34. @pytest.mark.env_onecard
  35. @pytest.mark.parametrize('dtype', [np.float16, np.float32, np.float64])
  36. def test_matmul_no_transpose_vec(dtype):
  37. """
  38. Feature: matrix & vec
  39. Description: test cases for matmul between matrix and vector
  40. Expectation: the result match to scipy
  41. """
  42. a = np.arange(1 * 3).reshape((1, 3)).astype(dtype)
  43. b = np.arange(3 * 5).reshape((3, 5)).astype(dtype)
  44. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  45. net = MatMulNet()
  46. output = net(Tensor(a), Tensor(b)).asnumpy()
  47. expect = np.array([[25., 28., 31., 34., 37.]], dtype)
  48. judge_result_correct(output, expect)
  49. def np_matmul(a: np.ndarray, b: np.ndarray, trans_a: bool, trans_b: bool):
  50. if trans_a:
  51. a = a.T
  52. if trans_b:
  53. b = b.T
  54. return np.matmul(a, b)
  55. @pytest.mark.level0
  56. @pytest.mark.platform_x86_cpu
  57. @pytest.mark.env_onecard
  58. @pytest.mark.parametrize('trans_a', [True, False])
  59. @pytest.mark.parametrize('trans_b', [True, False])
  60. @pytest.mark.parametrize('dtype', [np.float16, np.float32, np.float64])
  61. def test_matmul_matrix(trans_a, trans_b, dtype):
  62. """
  63. Feature: ALL To ALL
  64. Description: test cases for matmul for all float types and transpose args combinations
  65. Expectation: the result match to scipy
  66. """
  67. m, k, n = 5, 3, 4
  68. a = np.random.random((m, k)).astype(dtype)
  69. b = np.random.random((k, n)).astype(dtype)
  70. if trans_a:
  71. a = a.T
  72. if trans_b:
  73. b = b.T
  74. expect = np_matmul(a, b, trans_a, trans_b)
  75. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  76. net = MatMulNet(transpose_a=trans_a, transpose_b=trans_b)
  77. output = net(Tensor(a), Tensor(b)).asnumpy()
  78. judge_result_correct(output, expect)