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_dot_op.py 8.3 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. from mindspore import Tensor
  18. import mindspore.nn as nn
  19. import mindspore.context as context
  20. from mindspore.ops import composite as C
  21. from mindspore.common.initializer import initializer
  22. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  23. class NetDot(nn.Cell):
  24. def construct(self, x, y):
  25. return C.dot(x, y)
  26. @pytest.mark.level0
  27. @pytest.mark.platform_x86_cpu
  28. @pytest.mark.env_onecard
  29. def test_dot_001():
  30. x1_tensor = Tensor(np.array([[1., 2.], [4., 5.]]).astype(np.float32))
  31. x2_tensor = Tensor(np.array([[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], \
  32. [[9., 10.], [11., 12.]]]).astype(np.float32))
  33. network = NetDot()
  34. ms_result_np = network(x1_tensor, x2_tensor)
  35. expect_result = np.array([[[7., 10.], [19., 22.], [31., 34.]], \
  36. [[19., 28.], [55., 64.], [91., 100.]]]).astype(np.float32)
  37. assert (ms_result_np.asnumpy() == expect_result).all()
  38. @pytest.mark.level0
  39. @pytest.mark.platform_x86_cpu
  40. @pytest.mark.env_onecard
  41. def test_dot_002():
  42. x1_tensor = Tensor(np.array([[1., 2.], [4., 5.]]).astype(np.float32))
  43. x2_tensor = Tensor(np.array([[[1., 2., 3.], [4., 5., 6.]], [[7., 8., 9.], [10., 11., 12.]]]).astype(np.float32))
  44. network = NetDot()
  45. ms_result_np = network(x1_tensor, x2_tensor)
  46. expect_result = np.array([[[9., 12., 15.], [27., 30., 33.]], [[24., 33., 42.], [78., 87., 96.]]]).astype(np.float32)
  47. assert (ms_result_np.asnumpy() == expect_result).all()
  48. @pytest.mark.level0
  49. @pytest.mark.platform_x86_cpu
  50. @pytest.mark.env_onecard
  51. def test_dot_003():
  52. x1_tensor = initializer(Tensor(np.arange(2 * 3 * 4).reshape(2, 3, 4).astype(np.float32)), [2, 3, 4])
  53. x2_tensor = initializer(Tensor(np.arange(1 * 5 * 4 * 2).reshape(1, 5, 4, 2).astype(np.float32)), [1, 5, 4, 2])
  54. network = NetDot()
  55. ms_result_np = network(x1_tensor, x2_tensor)
  56. expect_result = np.array([[[[[28., 34.],
  57. [76., 82.],
  58. [124., 130.],
  59. [172., 178.],
  60. [220., 226.]]],
  61. [[[76., 98.],
  62. [252., 274.],
  63. [428., 450.],
  64. [604., 626.],
  65. [780., 802.]]],
  66. [[[124., 162.],
  67. [428., 466.],
  68. [732., 770.],
  69. [1036., 1074.],
  70. [1340., 1378.]]]],
  71. [[[[172., 226.],
  72. [604., 658.],
  73. [1036., 1090.],
  74. [1468., 1522.],
  75. [1900., 1954.]]],
  76. [[[220., 290.],
  77. [780., 850.],
  78. [1340., 1410.],
  79. [1900., 1970.],
  80. [2460., 2530.]]],
  81. [[[268., 354.],
  82. [956., 1042.],
  83. [1644., 1730.],
  84. [2332., 2418.],
  85. [3020., 3106.]]]]]).astype(np.float32)
  86. assert (ms_result_np.asnumpy() == expect_result).all()
  87. @pytest.mark.level0
  88. @pytest.mark.platform_x86_cpu
  89. @pytest.mark.env_onecard
  90. def test_dot_004():
  91. x1_tensor = initializer(Tensor(np.arange(3 * 4).reshape(3, 4).astype(np.float32)), [3, 4])
  92. x2_tensor = initializer(Tensor(np.arange(4 * 5).reshape(4, 5).astype(np.float32)), [4, 5])
  93. network = NetDot()
  94. ms_result_np = network(x1_tensor, x2_tensor)
  95. expect_result = np.array([[70., 76., 82., 88., 94.],
  96. [190., 212., 234., 256., 278.],
  97. [310., 348., 386., 424., 462.]]).astype(np.float32)
  98. assert (ms_result_np.asnumpy() == expect_result).all()
  99. @pytest.mark.level0
  100. @pytest.mark.platform_x86_cpu
  101. @pytest.mark.env_onecard
  102. def test_dot_005():
  103. x1_tensor = initializer(Tensor(np.arange(2 * 3 * 4).reshape(2, 3, 4).astype(np.float32)), [2, 3, 4])
  104. x2_tensor = initializer(Tensor(np.arange(4 * 5).reshape(4, 5).astype(np.float32)), [4, 5])
  105. network = NetDot()
  106. ms_result_np = network(x1_tensor, x2_tensor)
  107. expect_result = np.array([[[70., 76., 82., 88., 94.],
  108. [190., 212., 234., 256., 278.],
  109. [310., 348., 386., 424., 462.]],
  110. [[430., 484., 538., 592., 646.],
  111. [550., 620., 690., 760., 830.],
  112. [670., 756., 842., 928., 1014.]]]).astype(np.float32)
  113. assert (ms_result_np.asnumpy() == expect_result).all()
  114. @pytest.mark.level0
  115. @pytest.mark.platform_x86_cpu
  116. @pytest.mark.env_onecard
  117. def test_dot_006():
  118. x1_tensor = initializer(Tensor(np.arange(4).reshape(4).astype(np.float32)), [4])
  119. x2_tensor = initializer(Tensor(np.arange(2 * 4 * 5).reshape(2, 4, 5).astype(np.float32)), [2, 4, 5])
  120. network = NetDot()
  121. try:
  122. network(x1_tensor, x2_tensor)
  123. except ValueError as e:
  124. assert ValueError == type(e)
  125. def test_dot_007():
  126. x1_tensor = initializer(Tensor(np.arange(4).reshape(4).astype(np.float32)), [4])
  127. x2_tensor = initializer(Tensor(np.arange(4 * 4).reshape(4, 4).astype(np.float32)), [4, 4])
  128. network = NetDot()
  129. try:
  130. network(x2_tensor, x1_tensor)
  131. except ValueError as e:
  132. assert ValueError == type(e)
  133. @pytest.mark.level0
  134. @pytest.mark.platform_x86_cpu
  135. @pytest.mark.env_onecard
  136. def test_dot_008():
  137. x1_tensor = Tensor(np.array([]).astype(np.float32))
  138. x2_tensor = Tensor(np.array([[[1., 2.], [3., 4.]],
  139. [[5., 6.], [7., 8.]],
  140. [[9., 10.], [11., 12.]]]).astype(np.float32))
  141. network = NetDot()
  142. try:
  143. network(x2_tensor, x1_tensor)
  144. except ValueError as e:
  145. assert ValueError == type(e)
  146. @pytest.mark.level0
  147. @pytest.mark.platform_x86_cpu
  148. @pytest.mark.env_onecard
  149. def test_dot_009():
  150. # for document
  151. input_x1 = Tensor(np.array(np.ones(shape=[2, 3])).astype(np.float32))
  152. input_x2 = Tensor(np.array(np.ones(shape=[1, 2, 3])).astype(np.float32))
  153. network = NetDot()
  154. try:
  155. network(input_x1, input_x2)
  156. except ValueError as e:
  157. assert ValueError == type(e)
  158. @pytest.mark.level0
  159. @pytest.mark.platform_x86_cpu
  160. @pytest.mark.env_onecard
  161. def test_dot_010():
  162. # for document
  163. input_x1 = Tensor(np.array(np.ones(shape=[2, 3])).astype(np.float32))
  164. input_x2 = Tensor(np.array(np.ones(shape=[1, 3, 2])).astype(np.float32))
  165. network = NetDot()
  166. ms_result_np = network(input_x1, input_x2)
  167. expect_result = np.array([[[3., 3.]],
  168. [[3., 3.]]]).astype(np.float32)
  169. assert (ms_result_np.asnumpy() == expect_result).all()
  170. @pytest.mark.level0
  171. @pytest.mark.platform_x86_cpu
  172. @pytest.mark.env_onecard
  173. def test_dot_011():
  174. # for document
  175. context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU")
  176. input_x1 = Tensor(np.array(np.ones(shape=[2, 3])).astype(np.float32))
  177. input_x2 = Tensor(np.array(np.ones(shape=[1, 3, 2])).astype(np.float32))
  178. network = NetDot()
  179. ms_result_np = network(input_x1, input_x2)
  180. expect_result = np.array([[[3., 3.]],
  181. [[3., 3.]]]).astype(np.float32)
  182. assert (ms_result_np.asnumpy() == expect_result).all()