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 5.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
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright 2020 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. @pytest.mark.level0
  23. @pytest.mark.platform_x86_gpu_training
  24. @pytest.mark.env_onecard
  25. class BatchMatMulNet(nn.Cell):
  26. def __init__(self, transpose_a=False, transpose_b=False):
  27. super(BatchMatMulNet, self).__init__()
  28. self.batch_matmul = P.BatchMatMul(transpose_a, transpose_b)
  29. def construct(self, x, y):
  30. return self.batch_matmul(x, y)
  31. def test_4d():
  32. input_x = Tensor(np.arange(2 * 4 * 1 * 3).reshape(2, 4, 1, 3), mstype.float32)
  33. input_y = Tensor(np.arange(2 * 4 * 3 * 4).reshape(2, 4, 3, 4), mstype.float32)
  34. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  35. net = BatchMatMulNet()
  36. output = net(input_x, input_y)
  37. expect = [[[[20, 23, 26, 29]],
  38. [[200, 212, 224, 236]],
  39. [[596, 617, 638, 659]],
  40. [[1208, 1238, 1268, 1298]]],
  41. [[[2036, 2075, 2114, 2153]],
  42. [[3080, 3128, 3176, 3224]],
  43. [[4340, 4397, 4454, 4511]],
  44. [[5816, 5882, 5948, 6014]]]]
  45. assert (output.asnumpy() == expect).all()
  46. @pytest.mark.level0
  47. @pytest.mark.platform_x86_gpu_training
  48. @pytest.mark.env_onecard
  49. def test_4d_transpose_a():
  50. input_x = Tensor(np.arange(2 * 4 * 3 * 1).reshape(2, 4, 3, 1), mstype.float32)
  51. input_y = Tensor(np.arange(2 * 4 * 3 * 4).reshape(2, 4, 3, 4), mstype.float32)
  52. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  53. net = BatchMatMulNet(transpose_a=True)
  54. output = net(input_x, input_y)
  55. expect = [[[[20, 23, 26, 29]],
  56. [[200, 212, 224, 236]],
  57. [[596, 617, 638, 659]],
  58. [[1208, 1238, 1268, 1298]]],
  59. [[[2036, 2075, 2114, 2153]],
  60. [[3080, 3128, 3176, 3224]],
  61. [[4340, 4397, 4454, 4511]],
  62. [[5816, 5882, 5948, 6014]]]]
  63. assert (output.asnumpy() == expect).all()
  64. @pytest.mark.level0
  65. @pytest.mark.platform_x86_gpu_training
  66. @pytest.mark.env_onecard
  67. def test_4d_transpose_b():
  68. input_x = Tensor(np.arange(2 * 4 * 1 * 3).reshape(2, 4, 1, 3), mstype.float32)
  69. input_y = Tensor(np.arange(2 * 4 * 4 * 3).reshape(2, 4, 4, 3), mstype.float32)
  70. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  71. net = BatchMatMulNet(transpose_b=True)
  72. output = net(input_x, input_y)
  73. expect = [[[[5, 14, 23, 32]],
  74. [[158, 194, 230, 266]],
  75. [[527, 590, 653, 716]],
  76. [[1112, 1202, 1292, 1382]]],
  77. [[[1913, 2030, 2147, 2264]],
  78. [[2930, 3074, 3218, 3362]],
  79. [[4163, 4334, 4505, 4676]],
  80. [[5612, 5810, 6008, 6206]]]]
  81. assert (output.asnumpy() == expect).all()
  82. @pytest.mark.level0
  83. @pytest.mark.platform_x86_gpu_training
  84. @pytest.mark.env_onecard
  85. def test_4d_transpose_ab():
  86. input_x = Tensor(np.arange(2 * 4 * 3 * 1).reshape(2, 4, 3, 1), mstype.float32)
  87. input_y = Tensor(np.arange(2 * 4 * 4 * 3).reshape(2, 4, 4, 3), mstype.float32)
  88. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  89. net = BatchMatMulNet(transpose_a=True, transpose_b=True)
  90. output = net(input_x, input_y)
  91. expect = [[[[5, 14, 23, 32]],
  92. [[158, 194, 230, 266]],
  93. [[527, 590, 653, 716]],
  94. [[1112, 1202, 1292, 1382]]],
  95. [[[1913, 2030, 2147, 2264]],
  96. [[2930, 3074, 3218, 3362]],
  97. [[4163, 4334, 4505, 4676]],
  98. [[5612, 5810, 6008, 6206]]]]
  99. assert (output.asnumpy() == expect).all()
  100. @pytest.mark.level0
  101. @pytest.mark.platform_x86_gpu_training
  102. @pytest.mark.env_onecard
  103. def test_4D_fp16():
  104. input_x = Tensor(np.arange(2 * 4 * 1 * 3).reshape(2, 4, 1, 3), mstype.float16)
  105. input_y = Tensor(np.arange(2 * 4 * 3 * 4).reshape(2, 4, 3, 4), mstype.float16)
  106. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  107. net = BatchMatMulNet()
  108. output = net(input_x, input_y)
  109. expect = np.array([[[[20, 23, 26, 29]],
  110. [[200, 212, 224, 236]],
  111. [[596, 617, 638, 659]],
  112. [[1208, 1238, 1268, 1298]]],
  113. [[[2036, 2076, 2114, 2152]],
  114. [[3080, 3128, 3176, 3224]],
  115. [[4340, 4396, 4456, 4510]],
  116. [[5816, 5880, 5948, 6016]]]]).astype(np.float16)
  117. assert (output.asnumpy() == expect).all()