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

6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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]).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]).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(x6_np)
  85. y6_np = np.array([0, 1, 3]).astype(np.int8)
  86. y6 = Tensor(y6_np)
  87. expect6 = np.equal(x6_np, y6_np)
  88. x7_np = np.array([0, 1, 4]).astype(np.int64)
  89. x7 = Tensor(x7_np)
  90. y7_np = np.array([0, 1, 3]).astype(np.int64)
  91. y7 = Tensor(y7_np)
  92. expect7 = np.equal(x7_np, y7_np)
  93. x8_np = np.array([0, 1, 4]).astype(np.float16)
  94. x8 = Tensor(x8_np)
  95. y8_np = np.array([0, 1, 3]).astype(np.float16)
  96. y8 = Tensor(y8_np)
  97. expect8 = np.equal(x8_np, y8_np)
  98. x9_np = np.array([0, 1, 4]).astype(np.float64)
  99. x9 = Tensor(x9_np)
  100. y9_np = np.array([0, 1, 3]).astype(np.float64)
  101. y9 = Tensor(y9_np)
  102. expect9 = np.equal(x9_np, y9_np)
  103. x = [x0, x1, x2, x3, x4, x5, x6, x7, x8, x9]
  104. y = [y0, y1, y2, y3, y4, y5, y6, y7, y8, y9]
  105. expect = [expect0, expect1, expect2, expect3, expect4, expect5, expect6, expect7, expect8, expect9]
  106. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  107. equal = NetEqual()
  108. for i, xi in enumerate(x):
  109. output = equal(xi, y[i])
  110. assert np.all(output.asnumpy() == expect[i])
  111. assert output.shape == expect[i].shape
  112. print('test [%d/%d] passed!' % (i, len(x)))
  113. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  114. equal = NetEqual()
  115. for i, xi in enumerate(x):
  116. output = equal(xi, y[i])
  117. assert np.all(output.asnumpy() == expect[i])
  118. assert output.shape == expect[i].shape
  119. print('test [%d/%d] passed!' % (i, len(x)))
  120. @pytest.mark.level0
  121. @pytest.mark.platform_x86_gpu_training
  122. @pytest.mark.env_onecard
  123. def test_notequal():
  124. x0 = Tensor(np.array([[1.2, 1], [1, 0]]).astype(np.float32))
  125. y0 = Tensor(np.array([[1, 2]]).astype(np.float32))
  126. expect0 = np.array([[True, True], [False, True]])
  127. x1 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int16))
  128. y1 = Tensor(np.array([[1, 2]]).astype(np.int16))
  129. expect1 = np.array([[True, True], [False, True]])
  130. x2 = Tensor(np.array([[2, 1], [1, 2]]).astype(np.uint8))
  131. y2 = Tensor(np.array([[1, 2]]).astype(np.uint8))
  132. expect2 = np.array([[True, True], [False, False]])
  133. x3 = Tensor(np.array([[False, True], [True, False]]).astype(bool))
  134. y3 = Tensor(np.array([[True, False]]).astype(bool))
  135. expect3 = np.array([[True, True], [False, False]])
  136. x4 = Tensor(np.array([[1.2, 1], [1, 0]]).astype(np.float16))
  137. y4 = Tensor(np.array([[1, 2]]).astype(np.float16))
  138. expect4 = np.array([[True, True], [False, True]])
  139. x5 = Tensor(np.array([[2, 1], [1, 0]]).astype(np.int64))
  140. y5 = Tensor(np.array([[1, 2]]).astype(np.int64))
  141. expect5 = np.array([[True, True], [False, True]])
  142. x6 = Tensor(np.array([[2, 1], [1, 0]]).astype(np.int32))
  143. y6 = Tensor(np.array([[1, 2], [1, 2]]).astype(np.int32))
  144. expect6 = np.array([[True, True], [False, True]])
  145. x = [x0, x1, x2, x3, x4, x5, x6]
  146. y = [y0, y1, y2, y3, y4, y5, y6]
  147. expect = [expect0, expect1, expect2, expect3, expect4, expect5, expect6]
  148. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  149. notequal = NetNotEqual()
  150. for i, xi in enumerate(x):
  151. output = notequal(xi, y[i])
  152. assert np.all(output.asnumpy() == expect[i])
  153. assert output.shape == expect[i].shape
  154. print('test [%d/%d] passed!' % (i, len(x)))
  155. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  156. notequal = NetNotEqual()
  157. for i, xi in enumerate(x):
  158. output = notequal(xi, y[i])
  159. assert np.all(output.asnumpy() == expect[i])
  160. assert output.shape == expect[i].shape
  161. print('test [%d/%d] passed!' % (i, len(x)))
  162. @pytest.mark.level0
  163. @pytest.mark.platform_x86_gpu_training
  164. @pytest.mark.env_onecard
  165. def test_greaterqual():
  166. x0 = Tensor(np.array([[1.2, 1], [1, 0]]).astype(np.float32))
  167. y0 = Tensor(np.array([[1, 2], [1, 2]]).astype(np.float32))
  168. expect0 = np.array([[True, False], [True, False]])
  169. x1 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int16))
  170. y1 = Tensor(np.array([[1, 2]]).astype(np.int16))
  171. expect1 = np.array([[True, False], [True, False]])
  172. x2 = Tensor(np.array([[2, 1], [1, 2]]).astype(np.uint8))
  173. y2 = Tensor(np.array([[1, 2]]).astype(np.uint8))
  174. expect2 = np.array([[True, False], [True, True]])
  175. x3 = Tensor(np.array([[2, 1], [1, 2]]).astype(np.float64))
  176. y3 = Tensor(np.array([[1, 2]]).astype(np.float64))
  177. expect3 = np.array([[True, False], [True, True]])
  178. x4 = Tensor(np.array([[2, 1], [1, 2]]).astype(np.float16))
  179. y4 = Tensor(np.array([[1, 2]]).astype(np.float16))
  180. expect4 = np.array([[True, False], [True, True]])
  181. x5 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int64))
  182. y5 = Tensor(np.array([[1, 2]]).astype(np.int64))
  183. expect5 = np.array([[True, False], [True, False]])
  184. x6 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int32))
  185. y6 = Tensor(np.array([[1, 2]]).astype(np.int32))
  186. expect6 = np.array([[True, False], [True, False]])
  187. x7 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int8))
  188. y7 = Tensor(np.array([[1, 2]]).astype(np.int8))
  189. expect7 = np.array([[True, False], [True, False]])
  190. x = [x0, x1, x2, x3, x4, x5, x6, x7]
  191. y = [y0, y1, y2, y3, y4, y5, y6, y7]
  192. expect = [expect0, expect1, expect2, expect3, expect4, expect5, expect6, expect7]
  193. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  194. gequal = NetGreaterEqual()
  195. for i, xi in enumerate(x):
  196. output = gequal(xi, y[i])
  197. assert np.all(output.asnumpy() == expect[i])
  198. assert output.shape == expect[i].shape
  199. print('test [%d/%d] passed!' % (i, len(x)))
  200. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  201. gequal = NetGreaterEqual()
  202. for i, xi in enumerate(x):
  203. output = gequal(xi, y[i])
  204. assert np.all(output.asnumpy() == expect[i])
  205. assert output.shape == expect[i].shape
  206. print('test [%d/%d] passed!' % (i, len(x)))
  207. @pytest.mark.level0
  208. @pytest.mark.platform_x86_gpu_training
  209. @pytest.mark.env_onecard
  210. def test_equal_dynamic_shape():
  211. x0_np = np.arange(24).reshape((4, 3, 2)).astype(np.float32)
  212. x0 = Tensor(x0_np)
  213. y0_np = np.arange(24).reshape((4, 3, 2)).astype(np.float32)
  214. y0 = Tensor(y0_np)
  215. expect0 = np.equal(x0_np, y0_np)
  216. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  217. equal = NetEqualDynamic()
  218. output0 = equal(x0, y0)
  219. assert np.all(output0.asnumpy() == expect0)
  220. assert output0.shape == expect0.shape