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_batch_matmul.py 7.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # Copyright 2020-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.common import dtype as mstype
  21. from mindspore.ops import operations as P
  22. from mindspore.ops.operations import _inner_ops as inner
  23. class BatchMatMulNet(nn.Cell):
  24. def __init__(self, transpose_a=False, transpose_b=False):
  25. super(BatchMatMulNet, self).__init__()
  26. self.batch_matmul = P.BatchMatMul(transpose_a, transpose_b)
  27. def construct(self, x, y):
  28. return self.batch_matmul(x, y)
  29. @pytest.mark.level0
  30. @pytest.mark.platform_x86_gpu_training
  31. @pytest.mark.env_onecard
  32. def test_4d():
  33. input_x = Tensor(np.arange(2 * 4 * 1 * 3).reshape(2, 4, 1, 3), mstype.float32)
  34. input_y = Tensor(np.arange(2 * 4 * 3 * 4).reshape(2, 4, 3, 4), mstype.float32)
  35. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  36. net = BatchMatMulNet()
  37. output = net(input_x, input_y)
  38. expect = [[[[20, 23, 26, 29]],
  39. [[200, 212, 224, 236]],
  40. [[596, 617, 638, 659]],
  41. [[1208, 1238, 1268, 1298]]],
  42. [[[2036, 2075, 2114, 2153]],
  43. [[3080, 3128, 3176, 3224]],
  44. [[4340, 4397, 4454, 4511]],
  45. [[5816, 5882, 5948, 6014]]]]
  46. assert (output.asnumpy() == expect).all()
  47. @pytest.mark.level0
  48. @pytest.mark.platform_x86_gpu_training
  49. @pytest.mark.env_onecard
  50. def test_4d_float64():
  51. input_x = Tensor(np.arange(2 * 4 * 1 * 3).reshape(2, 4, 1, 3), mstype.float64)
  52. input_y = Tensor(np.arange(2 * 4 * 3 * 4).reshape(2, 4, 3, 4), mstype.float64)
  53. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  54. net = BatchMatMulNet()
  55. output = net(input_x, input_y)
  56. expect = [[[[20, 23, 26, 29]],
  57. [[200, 212, 224, 236]],
  58. [[596, 617, 638, 659]],
  59. [[1208, 1238, 1268, 1298]]],
  60. [[[2036, 2075, 2114, 2153]],
  61. [[3080, 3128, 3176, 3224]],
  62. [[4340, 4397, 4454, 4511]],
  63. [[5816, 5882, 5948, 6014]]]]
  64. assert (output.asnumpy() == expect).all()
  65. @pytest.mark.level0
  66. @pytest.mark.platform_x86_gpu_training
  67. @pytest.mark.env_onecard
  68. def test_4d_transpose_a():
  69. input_x = Tensor(np.arange(2 * 4 * 3 * 1).reshape(2, 4, 3, 1), mstype.float32)
  70. input_y = Tensor(np.arange(2 * 4 * 3 * 4).reshape(2, 4, 3, 4), mstype.float32)
  71. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  72. net = BatchMatMulNet(transpose_a=True)
  73. output = net(input_x, input_y)
  74. expect = [[[[20, 23, 26, 29]],
  75. [[200, 212, 224, 236]],
  76. [[596, 617, 638, 659]],
  77. [[1208, 1238, 1268, 1298]]],
  78. [[[2036, 2075, 2114, 2153]],
  79. [[3080, 3128, 3176, 3224]],
  80. [[4340, 4397, 4454, 4511]],
  81. [[5816, 5882, 5948, 6014]]]]
  82. assert (output.asnumpy() == expect).all()
  83. @pytest.mark.level0
  84. @pytest.mark.platform_x86_gpu_training
  85. @pytest.mark.env_onecard
  86. def test_4d_transpose_b():
  87. input_x = Tensor(np.arange(2 * 4 * 1 * 3).reshape(2, 4, 1, 3), mstype.float32)
  88. input_y = Tensor(np.arange(2 * 4 * 4 * 3).reshape(2, 4, 4, 3), mstype.float32)
  89. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  90. net = BatchMatMulNet(transpose_b=True)
  91. output = net(input_x, input_y)
  92. expect = [[[[5, 14, 23, 32]],
  93. [[158, 194, 230, 266]],
  94. [[527, 590, 653, 716]],
  95. [[1112, 1202, 1292, 1382]]],
  96. [[[1913, 2030, 2147, 2264]],
  97. [[2930, 3074, 3218, 3362]],
  98. [[4163, 4334, 4505, 4676]],
  99. [[5612, 5810, 6008, 6206]]]]
  100. assert (output.asnumpy() == expect).all()
  101. @pytest.mark.level0
  102. @pytest.mark.platform_x86_gpu_training
  103. @pytest.mark.env_onecard
  104. def test_4d_transpose_ab():
  105. input_x = Tensor(np.arange(2 * 4 * 3 * 1).reshape(2, 4, 3, 1), mstype.float32)
  106. input_y = Tensor(np.arange(2 * 4 * 4 * 3).reshape(2, 4, 4, 3), mstype.float32)
  107. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  108. net = BatchMatMulNet(transpose_a=True, transpose_b=True)
  109. output = net(input_x, input_y)
  110. expect = [[[[5, 14, 23, 32]],
  111. [[158, 194, 230, 266]],
  112. [[527, 590, 653, 716]],
  113. [[1112, 1202, 1292, 1382]]],
  114. [[[1913, 2030, 2147, 2264]],
  115. [[2930, 3074, 3218, 3362]],
  116. [[4163, 4334, 4505, 4676]],
  117. [[5612, 5810, 6008, 6206]]]]
  118. assert (output.asnumpy() == expect).all()
  119. @pytest.mark.level0
  120. @pytest.mark.platform_x86_gpu_training
  121. @pytest.mark.env_onecard
  122. def test_4D_fp16():
  123. input_x = Tensor(np.arange(2 * 4 * 1 * 3).reshape(2, 4, 1, 3), mstype.float16)
  124. input_y = Tensor(np.arange(2 * 4 * 3 * 4).reshape(2, 4, 3, 4), mstype.float16)
  125. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  126. net = BatchMatMulNet()
  127. output = net(input_x, input_y)
  128. expect = np.array([[[[20, 23, 26, 29]],
  129. [[200, 212, 224, 236]],
  130. [[596, 617, 638, 659]],
  131. [[1208, 1238, 1268, 1298]]],
  132. [[[2036, 2076, 2114, 2152]],
  133. [[3080, 3128, 3176, 3224]],
  134. [[4340, 4396, 4456, 4510]],
  135. [[5816, 5880, 5948, 6016]]]]).astype(np.float16)
  136. assert (output.asnumpy() == expect).all()
  137. class BatchMatMul_d(nn.Cell):
  138. def __init__(self, transpose_a=False, transpose_b=False):
  139. super(BatchMatMul_d, self).__init__()
  140. self.batch_matmul = P.BatchMatMul(transpose_a, transpose_b)
  141. self.test_dynamic = inner.GpuConvertToDynamicShape()
  142. def construct(self, x, y):
  143. x = self.test_dynamic(x)
  144. y = self.test_dynamic(y)
  145. return self.batch_matmul(x, y)
  146. @pytest.mark.level0
  147. @pytest.mark.platform_x86_gpu_training
  148. @pytest.mark.env_onecard
  149. def test_batchmatmul_dynamic():
  150. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  151. net = BatchMatMul_d()
  152. x1 = np.arange(8).reshape(2, 2, 2).astype(np.float32)
  153. y1 = np.arange(28).reshape(2, 2, 7).astype(np.float32)
  154. output1 = net(Tensor(x1), Tensor(y1))
  155. expect1 = np.matmul(x1, y1)
  156. assert (output1.asnumpy() == expect1).all()
  157. x2 = np.arange(2 * 4 * 1 * 3).reshape(2, 4, 1, 3).astype(np.float32)
  158. y2 = np.arange(2 * 4 * 3 * 4).reshape(2, 4, 3, 4).astype(np.float32)
  159. output2 = net(Tensor(x2), Tensor(y2))
  160. expect2 = np.matmul(x2, y2)
  161. assert (output2.asnumpy() == expect2).all()