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 8.7 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  84. equal = NetEqual()
  85. output0 = equal(x0, y0)
  86. assert np.all(output0.asnumpy() == expect0)
  87. assert output0.shape == expect0.shape
  88. output1 = equal(x1, y1)
  89. assert np.all(output1.asnumpy() == expect1)
  90. assert output1.shape == expect1.shape
  91. output2 = equal(x2, y2)
  92. assert np.all(output2.asnumpy() == expect2)
  93. assert output2.shape == expect2.shape
  94. output3 = equal(x3, y3)
  95. assert np.all(output3.asnumpy() == expect3)
  96. assert output3.shape == expect3.shape
  97. output4 = equal(x4, y4)
  98. assert np.all(output4.asnumpy() == expect4)
  99. assert output4.shape == expect4.shape
  100. output5 = equal(x5, y5)
  101. assert np.all(output5.asnumpy() == expect5)
  102. assert output5.shape == expect5.shape
  103. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  104. equal = NetEqual()
  105. output0 = equal(x0, y0)
  106. assert np.all(output0.asnumpy() == expect0)
  107. assert output0.shape == expect0.shape
  108. output1 = equal(x1, y1)
  109. assert np.all(output1.asnumpy() == expect1)
  110. assert output1.shape == expect1.shape
  111. output2 = equal(x2, y2)
  112. assert np.all(output2.asnumpy() == expect2)
  113. assert output2.shape == expect2.shape
  114. output3 = equal(x3, y3)
  115. assert np.all(output3.asnumpy() == expect3)
  116. assert output3.shape == expect3.shape
  117. output4 = equal(x4, y4)
  118. assert np.all(output4.asnumpy() == expect4)
  119. assert output4.shape == expect4.shape
  120. output5 = equal(x5, y5)
  121. assert np.all(output5.asnumpy() == expect5)
  122. assert output5.shape == expect5.shape
  123. @pytest.mark.level0
  124. @pytest.mark.platform_x86_gpu_training
  125. @pytest.mark.env_onecard
  126. def test_notequal():
  127. x0 = Tensor(np.array([[1.2, 1], [1, 0]]).astype(np.float32))
  128. y0 = Tensor(np.array([[1, 2]]).astype(np.float32))
  129. expect0 = np.array([[True, True], [False, True]])
  130. x1 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int16))
  131. y1 = Tensor(np.array([[1, 2]]).astype(np.int16))
  132. expect1 = np.array([[True, True], [False, True]])
  133. x2 = Tensor(np.array([[2, 1], [1, 2]]).astype(np.uint8))
  134. y2 = Tensor(np.array([[1, 2]]).astype(np.uint8))
  135. expect2 = np.array([[True, True], [False, False]])
  136. x3 = Tensor(np.array([[False, True], [True, False]]).astype(bool))
  137. y3 = Tensor(np.array([[True, False]]).astype(bool))
  138. expect3 = np.array([[True, True], [False, False]])
  139. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  140. notequal = NetNotEqual()
  141. output0 = notequal(x0, y0)
  142. assert np.all(output0.asnumpy() == expect0)
  143. assert output0.shape == expect0.shape
  144. output1 = notequal(x1, y1)
  145. assert np.all(output1.asnumpy() == expect1)
  146. assert output1.shape == expect1.shape
  147. output2 = notequal(x2, y2)
  148. assert np.all(output2.asnumpy() == expect2)
  149. assert output2.shape == expect2.shape
  150. output3 = notequal(x3, y3)
  151. assert np.all(output3.asnumpy() == expect3)
  152. assert output3.shape == expect3.shape
  153. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  154. notequal = NetNotEqual()
  155. output0 = notequal(x0, y0)
  156. assert np.all(output0.asnumpy() == expect0)
  157. assert output0.shape == expect0.shape
  158. output1 = notequal(x1, y1)
  159. assert np.all(output1.asnumpy() == expect1)
  160. assert output1.shape == expect1.shape
  161. output2 = notequal(x2, y2)
  162. assert np.all(output2.asnumpy() == expect2)
  163. assert output2.shape == expect2.shape
  164. output3 = notequal(x3, y3)
  165. assert np.all(output3.asnumpy() == expect3)
  166. assert output3.shape == expect3.shape
  167. @pytest.mark.level0
  168. @pytest.mark.platform_x86_gpu_training
  169. @pytest.mark.env_onecard
  170. def test_greaterqual():
  171. x0 = Tensor(np.array([[1.2, 1], [1, 0]]).astype(np.float32))
  172. y0 = Tensor(np.array([[1, 2]]).astype(np.float32))
  173. expect0 = np.array([[True, False], [True, False]])
  174. x1 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int16))
  175. y1 = Tensor(np.array([[1, 2]]).astype(np.int16))
  176. expect1 = np.array([[True, False], [True, False]])
  177. x2 = Tensor(np.array([[2, 1], [1, 2]]).astype(np.uint8))
  178. y2 = Tensor(np.array([[1, 2]]).astype(np.uint8))
  179. expect2 = np.array([[True, False], [True, True]])
  180. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  181. gequal = NetGreaterEqual()
  182. output0 = gequal(x0, y0)
  183. assert np.all(output0.asnumpy() == expect0)
  184. assert output0.shape == expect0.shape
  185. output1 = gequal(x1, y1)
  186. assert np.all(output1.asnumpy() == expect1)
  187. assert output1.shape == expect1.shape
  188. output2 = gequal(x2, y2)
  189. assert np.all(output2.asnumpy() == expect2)
  190. assert output2.shape == expect2.shape
  191. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  192. gequal = NetGreaterEqual()
  193. output0 = gequal(x0, y0)
  194. assert np.all(output0.asnumpy() == expect0)
  195. assert output0.shape == expect0.shape
  196. output1 = gequal(x1, y1)
  197. assert np.all(output1.asnumpy() == expect1)
  198. assert output1.shape == expect1.shape
  199. output2 = gequal(x2, y2)
  200. assert np.all(output2.asnumpy() == expect2)
  201. assert output2.shape == expect2.shape
  202. @pytest.mark.level0
  203. @pytest.mark.platform_x86_gpu_training
  204. @pytest.mark.env_onecard
  205. def test_equal_dynamic_shape():
  206. x0_np = np.arange(24).reshape((4, 3, 2)).astype(np.float32)
  207. x0 = Tensor(x0_np)
  208. y0_np = np.arange(24).reshape((4, 3, 2)).astype(np.float32)
  209. y0 = Tensor(y0_np)
  210. expect0 = np.equal(x0_np, y0_np)
  211. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  212. equal = NetEqualDynamic()
  213. output0 = equal(x0, y0)
  214. assert np.all(output0.asnumpy() == expect0)
  215. assert output0.shape == expect0.shape