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_op.py 4.5 kB

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