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_batchdot_op.py 7.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 pytest
  16. import numpy as np
  17. import mindspore
  18. from mindspore import Tensor
  19. import mindspore.nn as nn
  20. import mindspore.context as context
  21. from mindspore.ops import composite as C
  22. class NetBatchDot(nn.Cell):
  23. def __init__(self, axes):
  24. super(NetBatchDot, self).__init__()
  25. self.axes = axes
  26. def construct(self, x, y):
  27. return C.batch_dot(x, y, self.axes)
  28. # Implementation with numpy in tensorflow
  29. def _reference_batch_dot(x, y, axes):
  30. if isinstance(axes, int):
  31. axes = [axes, axes]
  32. elif isinstance(axes, tuple):
  33. axes = list(axes)
  34. if axes is None:
  35. if y.ndim == 2:
  36. axes = [x.ndim - 1, y.ndim - 1]
  37. else:
  38. axes = [x.ndim - 1, y.ndim - 2]
  39. if axes[0] < 0:
  40. axes[0] += x.ndim
  41. if axes[1] < 0:
  42. axes[1] += y.ndim
  43. result = []
  44. axes = [axes[0] - 1, axes[1] - 1]
  45. for xi, yi in zip(x, y):
  46. result.append(np.tensordot(xi, yi, axes))
  47. result = np.array(result)
  48. if result.ndim == 1:
  49. result = np.expand_dims(result, -1)
  50. return result
  51. @pytest.mark.level0
  52. @pytest.mark.platform_x86_cpu
  53. @pytest.mark.env_onecard
  54. def test_batch_dot_fp32():
  55. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  56. np.random.seed(12876)
  57. # case 1
  58. shape_x1 = (3, 12, 5, 2, 3)
  59. shape_x2 = (3, 1, 7, 3, 2)
  60. axes = (-1, -2)
  61. x1 = np.ones(shape=shape_x1).astype(np.float32)
  62. x2 = np.ones(shape=shape_x2).astype(np.float32)
  63. x1_tensor = Tensor(x1, dtype=mindspore.float32)
  64. x2_tensor = Tensor(x2, dtype=mindspore.float32)
  65. network = NetBatchDot(axes)
  66. ms_result_np = network(x1_tensor, x2_tensor).asnumpy()
  67. tf_result = _reference_batch_dot(x1, x2, axes)
  68. assert np.allclose(ms_result_np, tf_result)
  69. # case 2
  70. shape_x1 = (4, 3, 7, 5)
  71. shape_x2 = (4, 1, 7, 1)
  72. axes = 2
  73. x1 = np.random.random(shape_x1).astype(np.float32)
  74. x2 = np.random.random(shape_x2).astype(np.float32)
  75. x1_tensor = Tensor(x1, dtype=mindspore.float32)
  76. x2_tensor = Tensor(x2, dtype=mindspore.float32)
  77. network = NetBatchDot(axes)
  78. ms_result_np = network(x1_tensor, x2_tensor).asnumpy()
  79. tf_result = _reference_batch_dot(x1, x2, axes)
  80. assert np.allclose(ms_result_np, tf_result)
  81. # case 3
  82. shape_x1 = (18, 3, 5, 7)
  83. shape_x2 = (18, 1, 3, 7)
  84. axes = -1
  85. x1 = np.random.random(shape_x1).astype(np.float32)
  86. x2 = np.random.random(shape_x2).astype(np.float32)
  87. x1_tensor = Tensor(x1, dtype=mindspore.float32)
  88. x2_tensor = Tensor(x2, dtype=mindspore.float32)
  89. network = NetBatchDot(axes)
  90. ms_result_np = network(x1_tensor, x2_tensor).asnumpy()
  91. tf_result = _reference_batch_dot(x1, x2, axes)
  92. assert np.allclose(ms_result_np, tf_result)
  93. # case 4
  94. shape_x1 = (2, 11, 3, 9)
  95. shape_x2 = (2, 7, 9, 3)
  96. axes = None
  97. x1 = np.random.random(shape_x1).astype(np.float32)
  98. x2 = np.random.random(shape_x2).astype(np.float32)
  99. x1_tensor = Tensor(x1, dtype=mindspore.float32)
  100. x2_tensor = Tensor(x2, dtype=mindspore.float32)
  101. network = NetBatchDot(axes)
  102. ms_result_np = network(x1_tensor, x2_tensor).asnumpy()
  103. tf_result = _reference_batch_dot(x1, x2, axes)
  104. assert np.allclose(ms_result_np, tf_result)
  105. # case 5
  106. shape_x1 = (7, 5)
  107. shape_x2 = (7, 5)
  108. axes = None
  109. x1 = np.random.random(shape_x1).astype(np.float32)
  110. x2 = np.random.random(shape_x2).astype(np.float32)
  111. x1_tensor = Tensor(x1, dtype=mindspore.float32)
  112. x2_tensor = Tensor(x2, dtype=mindspore.float32)
  113. network = NetBatchDot(axes)
  114. ms_result_np = network(x1_tensor, x2_tensor).asnumpy()
  115. tf_result = _reference_batch_dot(x1, x2, axes)
  116. assert np.allclose(ms_result_np, tf_result)
  117. # case 6
  118. shape_x1 = (7, 3, 5)
  119. shape_x2 = (7, 5)
  120. axes = None
  121. x1 = np.random.random(shape_x1).astype(np.float32)
  122. x2 = np.random.random(shape_x2).astype(np.float32)
  123. x1_tensor = Tensor(x1, dtype=mindspore.float32)
  124. x2_tensor = Tensor(x2, dtype=mindspore.float32)
  125. network = NetBatchDot(axes)
  126. ms_result_np = network(x1_tensor, x2_tensor).asnumpy()
  127. tf_result = _reference_batch_dot(x1, x2, axes)
  128. assert np.allclose(ms_result_np, tf_result)
  129. # case 7
  130. shape_x1 = (7, 5)
  131. shape_x2 = (7, 5, 3)
  132. axes = None
  133. x1 = np.random.random(shape_x1).astype(np.float32)
  134. x2 = np.random.random(shape_x2).astype(np.float32)
  135. x1_tensor = Tensor(x1, dtype=mindspore.float32)
  136. x2_tensor = Tensor(x2, dtype=mindspore.float32)
  137. network = NetBatchDot(axes)
  138. ms_result_np = network(x1_tensor, x2_tensor).asnumpy()
  139. tf_result = _reference_batch_dot(x1, x2, axes)
  140. assert np.allclose(ms_result_np, tf_result)
  141. # case 8
  142. shape_x1 = (39, 6)
  143. shape_x2 = (39, 6)
  144. axes = -1
  145. x1 = np.random.random(shape_x1).astype(np.float32)
  146. x2 = np.random.random(shape_x2).astype(np.float32)
  147. x1_tensor = Tensor(x1, dtype=mindspore.float32)
  148. x2_tensor = Tensor(x2, dtype=mindspore.float32)
  149. network = NetBatchDot(axes)
  150. ms_result_np = network(x1_tensor, x2_tensor).asnumpy()
  151. tf_result = _reference_batch_dot(x1, x2, axes)
  152. assert np.allclose(ms_result_np, tf_result)
  153. # case 9
  154. shape_x1 = (21, 2, 3)
  155. shape_x2 = (21, 3, 2)
  156. axes = (-1, -2)
  157. x1 = np.ones(shape=shape_x1).astype(np.float32)
  158. x2 = np.ones(shape=shape_x2).astype(np.float32)
  159. x1_tensor = Tensor(x1, dtype=mindspore.float32)
  160. x2_tensor = Tensor(x2, dtype=mindspore.float32)
  161. network = NetBatchDot(axes)
  162. ms_result_np = network(x1_tensor, x2_tensor).asnumpy()
  163. tf_result = _reference_batch_dot(x1, x2, axes)
  164. assert np.allclose(ms_result_np, tf_result)
  165. # case 10
  166. shape_x1 = (4, 3, 2, 1, 7, 5)
  167. shape_x2 = (4, 5, 7, 1)
  168. axes = -2
  169. x1 = np.ones(shape=shape_x1).astype(np.float32)
  170. x2 = np.ones(shape=shape_x2).astype(np.float32)
  171. x1_tensor = Tensor(x1, dtype=mindspore.float32)
  172. x2_tensor = Tensor(x2, dtype=mindspore.float32)
  173. network = NetBatchDot(axes)
  174. ms_result_np = network(x1_tensor, x2_tensor).asnumpy()
  175. tf_result = _reference_batch_dot(x1, x2, axes)
  176. assert np.allclose(ms_result_np, tf_result)
  177. # case 10
  178. shape_x1 = (4, 3, 2, 1, 7, 5)
  179. shape_x2 = (4, 5, 7, 1)
  180. axes = -2
  181. x1 = np.ones(shape=shape_x1).astype(np.float16)
  182. x2 = np.ones(shape=shape_x2).astype(np.float16)
  183. x1_tensor = Tensor(x1, dtype=mindspore.float16)
  184. x2_tensor = Tensor(x2, dtype=mindspore.float16)
  185. network = NetBatchDot(axes)
  186. ms_result_np = network(x1_tensor, x2_tensor).asnumpy()
  187. tf_result = _reference_batch_dot(x1, x2, axes)
  188. assert np.allclose(ms_result_np, tf_result)