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

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. class NetEqual(Cell):
  22. def __init__(self):
  23. super(NetEqual, self).__init__()
  24. self.Equal = P.Equal()
  25. def construct(self, x, y):
  26. return self.Equal(x, y)
  27. class NetNotEqual(Cell):
  28. def __init__(self):
  29. super(NetNotEqual, self).__init__()
  30. self.NotEqual = P.NotEqual()
  31. def construct(self, x, y):
  32. return self.NotEqual(x, y)
  33. class NetGreaterEqual(Cell):
  34. def __init__(self):
  35. super(NetGreaterEqual, self).__init__()
  36. self.GreaterEqual = P.GreaterEqual()
  37. def construct(self, x, y):
  38. return self.GreaterEqual(x, y)
  39. @pytest.mark.level0
  40. @pytest.mark.platform_x86_gpu_training
  41. @pytest.mark.env_onecard
  42. def test_equal():
  43. x0_np = np.arange(24).reshape((4, 3, 2)).astype(np.float32)
  44. x0 = Tensor(x0_np)
  45. y0_np = np.arange(24).reshape((4, 3, 2)).astype(np.float32)
  46. y0 = Tensor(y0_np)
  47. expect0 = np.equal(x0_np, y0_np)
  48. x1_np = np.array([0, 1, 3]).astype(np.float32)
  49. x1 = Tensor(x1_np)
  50. y1_np = np.array([0, 1, -3]).astype(np.float32)
  51. y1 = Tensor(y1_np)
  52. expect1 = np.equal(x1_np, y1_np)
  53. x2_np = np.array([0, 1, 3]).astype(np.int32)
  54. x2 = Tensor(x2_np)
  55. y2_np = np.array([0, 1, -3]).astype(np.int32)
  56. y2 = Tensor(y2_np)
  57. expect2 = np.equal(x2_np, y2_np)
  58. x3_np = np.array([0, 1, 3]).astype(np.int16)
  59. x3 = Tensor(x3_np)
  60. y3_np = np.array([0, 1, -3]).astype(np.int16)
  61. y3 = Tensor(y3_np)
  62. expect3 = np.equal(x3_np, y3_np)
  63. x4_np = np.array([0, 1, 4]).astype(np.uint8)
  64. x4 = Tensor(x4_np)
  65. y4_np = np.array([0, 1, 3]).astype(np.uint8)
  66. y4 = Tensor(y4_np)
  67. expect4 = np.equal(x4_np, y4_np)
  68. x5_np = np.array([True, False, True]).astype(bool)
  69. x5 = Tensor(x5_np)
  70. y5_np = np.array([True, False, False]).astype(bool)
  71. y5 = Tensor(y5_np)
  72. expect5 = np.equal(x5_np, y5_np)
  73. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  74. equal = NetEqual()
  75. output0 = equal(x0, y0)
  76. assert np.all(output0.asnumpy() == expect0)
  77. assert output0.shape == expect0.shape
  78. output1 = equal(x1, y1)
  79. assert np.all(output1.asnumpy() == expect1)
  80. assert output1.shape == expect1.shape
  81. output2 = equal(x2, y2)
  82. assert np.all(output2.asnumpy() == expect2)
  83. assert output2.shape == expect2.shape
  84. output3 = equal(x3, y3)
  85. assert np.all(output3.asnumpy() == expect3)
  86. assert output3.shape == expect3.shape
  87. output4 = equal(x4, y4)
  88. assert np.all(output4.asnumpy() == expect4)
  89. assert output4.shape == expect4.shape
  90. output5 = equal(x5, y5)
  91. assert np.all(output5.asnumpy() == expect5)
  92. assert output5.shape == expect5.shape
  93. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  94. equal = NetEqual()
  95. output0 = equal(x0, y0)
  96. assert np.all(output0.asnumpy() == expect0)
  97. assert output0.shape == expect0.shape
  98. output1 = equal(x1, y1)
  99. assert np.all(output1.asnumpy() == expect1)
  100. assert output1.shape == expect1.shape
  101. output2 = equal(x2, y2)
  102. assert np.all(output2.asnumpy() == expect2)
  103. assert output2.shape == expect2.shape
  104. output3 = equal(x3, y3)
  105. assert np.all(output3.asnumpy() == expect3)
  106. assert output3.shape == expect3.shape
  107. output4 = equal(x4, y4)
  108. assert np.all(output4.asnumpy() == expect4)
  109. assert output4.shape == expect4.shape
  110. output5 = equal(x5, y5)
  111. assert np.all(output5.asnumpy() == expect5)
  112. assert output5.shape == expect5.shape
  113. @pytest.mark.level0
  114. @pytest.mark.platform_x86_gpu_training
  115. @pytest.mark.env_onecard
  116. def test_notequal():
  117. x0 = Tensor(np.array([[1.2, 1], [1, 0]]).astype(np.float32))
  118. y0 = Tensor(np.array([[1, 2]]).astype(np.float32))
  119. expect0 = np.array([[True, True], [False, True]])
  120. x1 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int16))
  121. y1 = Tensor(np.array([[1, 2]]).astype(np.int16))
  122. expect1 = np.array([[True, True], [False, True]])
  123. x2 = Tensor(np.array([[2, 1], [1, 2]]).astype(np.uint8))
  124. y2 = Tensor(np.array([[1, 2]]).astype(np.uint8))
  125. expect2 = np.array([[True, True], [False, False]])
  126. x3 = Tensor(np.array([[False, True], [True, False]]).astype(bool))
  127. y3 = Tensor(np.array([[True, False]]).astype(bool))
  128. expect3 = np.array([[True, True], [False, False]])
  129. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  130. notequal = NetNotEqual()
  131. output0 = notequal(x0, y0)
  132. assert np.all(output0.asnumpy() == expect0)
  133. assert output0.shape == expect0.shape
  134. output1 = notequal(x1, y1)
  135. assert np.all(output1.asnumpy() == expect1)
  136. assert output1.shape == expect1.shape
  137. output2 = notequal(x2, y2)
  138. assert np.all(output2.asnumpy() == expect2)
  139. assert output2.shape == expect2.shape
  140. output3 = notequal(x3, y3)
  141. assert np.all(output3.asnumpy() == expect3)
  142. assert output3.shape == expect3.shape
  143. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  144. notequal = NetNotEqual()
  145. output0 = notequal(x0, y0)
  146. assert np.all(output0.asnumpy() == expect0)
  147. assert output0.shape == expect0.shape
  148. output1 = notequal(x1, y1)
  149. assert np.all(output1.asnumpy() == expect1)
  150. assert output1.shape == expect1.shape
  151. output2 = notequal(x2, y2)
  152. assert np.all(output2.asnumpy() == expect2)
  153. assert output2.shape == expect2.shape
  154. output3 = notequal(x3, y3)
  155. assert np.all(output3.asnumpy() == expect3)
  156. assert output3.shape == expect3.shape
  157. @pytest.mark.level0
  158. @pytest.mark.platform_x86_gpu_training
  159. @pytest.mark.env_onecard
  160. def test_greaterqual():
  161. x0 = Tensor(np.array([[1.2, 1], [1, 0]]).astype(np.float32))
  162. y0 = Tensor(np.array([[1, 2]]).astype(np.float32))
  163. expect0 = np.array([[True, False], [True, False]])
  164. x1 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int16))
  165. y1 = Tensor(np.array([[1, 2]]).astype(np.int16))
  166. expect1 = np.array([[True, False], [True, False]])
  167. x2 = Tensor(np.array([[2, 1], [1, 2]]).astype(np.uint8))
  168. y2 = Tensor(np.array([[1, 2]]).astype(np.uint8))
  169. expect2 = np.array([[True, False], [True, True]])
  170. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  171. gequal = NetGreaterEqual()
  172. output0 = gequal(x0, y0)
  173. assert np.all(output0.asnumpy() == expect0)
  174. assert output0.shape == expect0.shape
  175. output1 = gequal(x1, y1)
  176. assert np.all(output1.asnumpy() == expect1)
  177. assert output1.shape == expect1.shape
  178. output2 = gequal(x2, y2)
  179. assert np.all(output2.asnumpy() == expect2)
  180. assert output2.shape == expect2.shape
  181. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  182. gequal = NetGreaterEqual()
  183. output0 = gequal(x0, y0)
  184. assert np.all(output0.asnumpy() == expect0)
  185. assert output0.shape == expect0.shape
  186. output1 = gequal(x1, y1)
  187. assert np.all(output1.asnumpy() == expect1)
  188. assert output1.shape == expect1.shape
  189. output2 = gequal(x2, y2)
  190. assert np.all(output2.asnumpy() == expect2)
  191. assert output2.shape == expect2.shape