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_equal_op.py 9.6 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. # Copyright 2019 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. from mindspore.common.tensor import Tensor
  19. from mindspore.nn import Cell
  20. from mindspore.ops import operations as P
  21. from mindspore.ops.operations import _inner_ops as inner
  22. class NetEqual(Cell):
  23. def __init__(self):
  24. super(NetEqual, self).__init__()
  25. self.Equal = P.Equal()
  26. def construct(self, x, y):
  27. return self.Equal(x, y)
  28. class NetEqualDynamic(Cell):
  29. def __init__(self):
  30. super(NetEqualDynamic, self).__init__()
  31. self.conv = inner.GpuConvertToDynamicShape()
  32. self.Equal = P.Equal()
  33. def construct(self, x, y):
  34. x_conv = self.conv(x)
  35. y_conv = self.conv(y)
  36. return self.Equal(x_conv, y_conv)
  37. class NetNotEqual(Cell):
  38. def __init__(self):
  39. super(NetNotEqual, self).__init__()
  40. self.NotEqual = P.NotEqual()
  41. def construct(self, x, y):
  42. return self.NotEqual(x, y)
  43. class NetGreaterEqual(Cell):
  44. def __init__(self):
  45. super(NetGreaterEqual, self).__init__()
  46. self.GreaterEqual = P.GreaterEqual()
  47. def construct(self, x, y):
  48. return self.GreaterEqual(x, y)
  49. @pytest.mark.level0
  50. @pytest.mark.platform_x86_gpu_training
  51. @pytest.mark.env_onecard
  52. def test_equal():
  53. x0_np = np.arange(24).reshape((4, 3, 2)).astype(np.float32)
  54. x0 = Tensor(x0_np)
  55. y0_np = np.arange(24).reshape((4, 3, 2)).astype(np.float32)
  56. y0 = Tensor(y0_np)
  57. expect0 = np.equal(x0_np, y0_np)
  58. x1_np = np.array([0, 1, 3]).astype(np.float32)
  59. x1 = Tensor(x1_np)
  60. y1_np = np.array([0, 1, -3]).astype(np.float32)
  61. y1 = Tensor(y1_np)
  62. expect1 = np.equal(x1_np, y1_np)
  63. x2_np = np.array([0, 1, 3]).astype(np.int32)
  64. x2 = Tensor(x2_np)
  65. y2_np = np.array([0, 1, -3]).astype(np.int32)
  66. y2 = Tensor(y2_np)
  67. expect2 = np.equal(x2_np, y2_np)
  68. x3_np = np.array([0, 1, 3]).astype(np.int16)
  69. x3 = Tensor(x3_np)
  70. y3_np = np.array([0, 1, -3]).astype(np.int16)
  71. y3 = Tensor(y3_np)
  72. expect3 = np.equal(x3_np, y3_np)
  73. x4_np = np.array([0, 1, 4]).astype(np.uint8)
  74. x4 = Tensor(x4_np)
  75. y4_np = np.array([0, 1, 3]).astype(np.uint8)
  76. y4 = Tensor(y4_np)
  77. expect4 = np.equal(x4_np, y4_np)
  78. x5_np = np.array([True, False, True]).astype(bool)
  79. x5 = Tensor(x5_np)
  80. y5_np = np.array([True, False, False]).astype(bool)
  81. y5 = Tensor(y5_np)
  82. expect5 = np.equal(x5_np, y5_np)
  83. x6_np = np.array([0, 1, 4]).astype(np.int8)
  84. x6 = Tensor(x4_np)
  85. y6_np = np.array([0, 1, 3]).astype(np.int8)
  86. y6 = Tensor(y4_np)
  87. expect6 = np.equal(x6_np, y6_np)
  88. x7_np = np.array([0, 1, 4]).astype(np.int64)
  89. x7 = Tensor(x4_np)
  90. y7_np = np.array([0, 1, 3]).astype(np.int64)
  91. y7 = Tensor(y4_np)
  92. expect7 = np.equal(x7_np, y7_np)
  93. x8_np = np.array([0, 1, 4]).astype(np.float16)
  94. x8 = Tensor(x4_np)
  95. y8_np = np.array([0, 1, 3]).astype(np.float16)
  96. y8 = Tensor(y4_np)
  97. expect8 = np.equal(x8_np, y8_np)
  98. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  99. equal = NetEqual()
  100. output0 = equal(x0, y0)
  101. assert np.all(output0.asnumpy() == expect0)
  102. assert output0.shape == expect0.shape
  103. output1 = equal(x1, y1)
  104. assert np.all(output1.asnumpy() == expect1)
  105. assert output1.shape == expect1.shape
  106. output2 = equal(x2, y2)
  107. assert np.all(output2.asnumpy() == expect2)
  108. assert output2.shape == expect2.shape
  109. output3 = equal(x3, y3)
  110. assert np.all(output3.asnumpy() == expect3)
  111. assert output3.shape == expect3.shape
  112. output4 = equal(x4, y4)
  113. assert np.all(output4.asnumpy() == expect4)
  114. assert output4.shape == expect4.shape
  115. output5 = equal(x5, y5)
  116. assert np.all(output5.asnumpy() == expect5)
  117. assert output5.shape == expect5.shape
  118. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  119. equal = NetEqual()
  120. output0 = equal(x0, y0)
  121. assert np.all(output0.asnumpy() == expect0)
  122. assert output0.shape == expect0.shape
  123. output1 = equal(x1, y1)
  124. assert np.all(output1.asnumpy() == expect1)
  125. assert output1.shape == expect1.shape
  126. output2 = equal(x2, y2)
  127. assert np.all(output2.asnumpy() == expect2)
  128. assert output2.shape == expect2.shape
  129. output3 = equal(x3, y3)
  130. assert np.all(output3.asnumpy() == expect3)
  131. assert output3.shape == expect3.shape
  132. output4 = equal(x4, y4)
  133. assert np.all(output4.asnumpy() == expect4)
  134. assert output4.shape == expect4.shape
  135. output5 = equal(x5, y5)
  136. assert np.all(output5.asnumpy() == expect5)
  137. assert output5.shape == expect5.shape
  138. output6 = equal(x6, y6)
  139. assert np.all(output6.asnumpy() == expect6)
  140. assert output6.shape == expect6.shape
  141. output7 = equal(x7, y7)
  142. assert np.all(output7.asnumpy() == expect7)
  143. assert output7.shape == expect7.shape
  144. output8 = equal(x8, y8)
  145. assert np.all(output8.asnumpy() == expect8)
  146. assert output8.shape == expect8.shape
  147. @pytest.mark.level0
  148. @pytest.mark.platform_x86_gpu_training
  149. @pytest.mark.env_onecard
  150. def test_notequal():
  151. x0 = Tensor(np.array([[1.2, 1], [1, 0]]).astype(np.float32))
  152. y0 = Tensor(np.array([[1, 2]]).astype(np.float32))
  153. expect0 = np.array([[True, True], [False, True]])
  154. x1 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int16))
  155. y1 = Tensor(np.array([[1, 2]]).astype(np.int16))
  156. expect1 = np.array([[True, True], [False, True]])
  157. x2 = Tensor(np.array([[2, 1], [1, 2]]).astype(np.uint8))
  158. y2 = Tensor(np.array([[1, 2]]).astype(np.uint8))
  159. expect2 = np.array([[True, True], [False, False]])
  160. x3 = Tensor(np.array([[False, True], [True, False]]).astype(bool))
  161. y3 = Tensor(np.array([[True, False]]).astype(bool))
  162. expect3 = np.array([[True, True], [False, False]])
  163. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  164. notequal = NetNotEqual()
  165. output0 = notequal(x0, y0)
  166. assert np.all(output0.asnumpy() == expect0)
  167. assert output0.shape == expect0.shape
  168. output1 = notequal(x1, y1)
  169. assert np.all(output1.asnumpy() == expect1)
  170. assert output1.shape == expect1.shape
  171. output2 = notequal(x2, y2)
  172. assert np.all(output2.asnumpy() == expect2)
  173. assert output2.shape == expect2.shape
  174. output3 = notequal(x3, y3)
  175. assert np.all(output3.asnumpy() == expect3)
  176. assert output3.shape == expect3.shape
  177. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  178. notequal = NetNotEqual()
  179. output0 = notequal(x0, y0)
  180. assert np.all(output0.asnumpy() == expect0)
  181. assert output0.shape == expect0.shape
  182. output1 = notequal(x1, y1)
  183. assert np.all(output1.asnumpy() == expect1)
  184. assert output1.shape == expect1.shape
  185. output2 = notequal(x2, y2)
  186. assert np.all(output2.asnumpy() == expect2)
  187. assert output2.shape == expect2.shape
  188. output3 = notequal(x3, y3)
  189. assert np.all(output3.asnumpy() == expect3)
  190. assert output3.shape == expect3.shape
  191. @pytest.mark.level0
  192. @pytest.mark.platform_x86_gpu_training
  193. @pytest.mark.env_onecard
  194. def test_greaterqual():
  195. x0 = Tensor(np.array([[1.2, 1], [1, 0]]).astype(np.float32))
  196. y0 = Tensor(np.array([[1, 2]]).astype(np.float32))
  197. expect0 = np.array([[True, False], [True, False]])
  198. x1 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int16))
  199. y1 = Tensor(np.array([[1, 2]]).astype(np.int16))
  200. expect1 = np.array([[True, False], [True, False]])
  201. x2 = Tensor(np.array([[2, 1], [1, 2]]).astype(np.uint8))
  202. y2 = Tensor(np.array([[1, 2]]).astype(np.uint8))
  203. expect2 = np.array([[True, False], [True, True]])
  204. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  205. gequal = NetGreaterEqual()
  206. output0 = gequal(x0, y0)
  207. assert np.all(output0.asnumpy() == expect0)
  208. assert output0.shape == expect0.shape
  209. output1 = gequal(x1, y1)
  210. assert np.all(output1.asnumpy() == expect1)
  211. assert output1.shape == expect1.shape
  212. output2 = gequal(x2, y2)
  213. assert np.all(output2.asnumpy() == expect2)
  214. assert output2.shape == expect2.shape
  215. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  216. gequal = NetGreaterEqual()
  217. output0 = gequal(x0, y0)
  218. assert np.all(output0.asnumpy() == expect0)
  219. assert output0.shape == expect0.shape
  220. output1 = gequal(x1, y1)
  221. assert np.all(output1.asnumpy() == expect1)
  222. assert output1.shape == expect1.shape
  223. output2 = gequal(x2, y2)
  224. assert np.all(output2.asnumpy() == expect2)
  225. assert output2.shape == expect2.shape
  226. @pytest.mark.level0
  227. @pytest.mark.platform_x86_gpu_training
  228. @pytest.mark.env_onecard
  229. def test_equal_dynamic_shape():
  230. x0_np = np.arange(24).reshape((4, 3, 2)).astype(np.float32)
  231. x0 = Tensor(x0_np)
  232. y0_np = np.arange(24).reshape((4, 3, 2)).astype(np.float32)
  233. y0 = Tensor(y0_np)
  234. expect0 = np.equal(x0_np, y0_np)
  235. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  236. equal = NetEqualDynamic()
  237. output0 = equal(x0, y0)
  238. assert np.all(output0.asnumpy() == expect0)
  239. assert output0.shape == expect0.shape