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_logic_ops.py 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. # Copyright 2021 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. """unit tests for numpy logical operations"""
  16. import pytest
  17. import numpy as onp
  18. import mindspore.numpy as mnp
  19. from .utils import rand_int, rand_bool, run_binop_test, run_logical_test, match_res, \
  20. match_all_arrays, to_tensor
  21. class Cases():
  22. def __init__(self):
  23. self.arrs = [
  24. rand_int(2),
  25. rand_int(2, 3),
  26. rand_int(2, 3, 4),
  27. rand_int(2, 3, 4, 5),
  28. ]
  29. # scalars expanded across the 0th dimension
  30. self.scalars = [
  31. rand_int(),
  32. rand_int(1),
  33. rand_int(1, 1),
  34. rand_int(1, 1, 1, 1),
  35. ]
  36. # arrays of the same size expanded across the 0th dimension
  37. self.expanded_arrs = [
  38. rand_int(2, 3),
  39. rand_int(1, 2, 3),
  40. rand_int(1, 1, 2, 3),
  41. rand_int(1, 1, 1, 2, 3),
  42. ]
  43. # arrays which can be broadcast
  44. self.broadcastables = [
  45. rand_int(5),
  46. rand_int(6, 1),
  47. rand_int(7, 1, 5),
  48. rand_int(8, 1, 6, 1)
  49. ]
  50. # Boolean arrays
  51. self.boolean_arrs = [
  52. rand_bool(),
  53. rand_bool(5),
  54. rand_bool(6, 1),
  55. rand_bool(7, 1, 5),
  56. rand_bool(8, 1, 6, 1)
  57. ]
  58. # array which contains infs and nans
  59. self.infs = onp.array([[1.0, onp.nan], [onp.inf, onp.NINF], [2.3, -4.5], [onp.nan, 0.0]])
  60. test_case = Cases()
  61. def mnp_not_equal(a, b):
  62. return mnp.not_equal(a, b)
  63. def onp_not_equal(a, b):
  64. return onp.not_equal(a, b)
  65. @pytest.mark.level1
  66. @pytest.mark.platform_arm_ascend_training
  67. @pytest.mark.platform_x86_ascend_training
  68. @pytest.mark.platform_x86_gpu_training
  69. @pytest.mark.platform_x86_cpu
  70. @pytest.mark.env_onecard
  71. def test_not_equal():
  72. run_binop_test(mnp_not_equal, onp_not_equal, test_case)
  73. def mnp_less_equal(a, b):
  74. return mnp.less_equal(a, b)
  75. def onp_less_equal(a, b):
  76. return onp.less_equal(a, b)
  77. @pytest.mark.level1
  78. @pytest.mark.platform_arm_ascend_training
  79. @pytest.mark.platform_x86_ascend_training
  80. @pytest.mark.platform_x86_gpu_training
  81. @pytest.mark.platform_x86_cpu
  82. @pytest.mark.env_onecard
  83. def test_less_equal():
  84. run_binop_test(mnp_less_equal, onp_less_equal, test_case)
  85. def mnp_less(a, b):
  86. return mnp.less(a, b)
  87. def onp_less(a, b):
  88. return onp.less(a, b)
  89. @pytest.mark.level1
  90. @pytest.mark.platform_arm_ascend_training
  91. @pytest.mark.platform_x86_ascend_training
  92. @pytest.mark.platform_x86_gpu_training
  93. @pytest.mark.platform_x86_cpu
  94. @pytest.mark.env_onecard
  95. def test_less():
  96. run_binop_test(mnp_less, onp_less, test_case)
  97. def mnp_greater_equal(a, b):
  98. return mnp.greater_equal(a, b)
  99. def onp_greater_equal(a, b):
  100. return onp.greater_equal(a, b)
  101. @pytest.mark.level1
  102. @pytest.mark.platform_arm_ascend_training
  103. @pytest.mark.platform_x86_ascend_training
  104. @pytest.mark.platform_x86_gpu_training
  105. @pytest.mark.platform_x86_cpu
  106. @pytest.mark.env_onecard
  107. def test_greater_equal():
  108. run_binop_test(mnp_greater_equal, onp_greater_equal, test_case)
  109. def mnp_greater(a, b):
  110. return mnp.greater(a, b)
  111. def onp_greater(a, b):
  112. return onp.greater(a, b)
  113. @pytest.mark.level1
  114. @pytest.mark.platform_arm_ascend_training
  115. @pytest.mark.platform_x86_ascend_training
  116. @pytest.mark.platform_x86_gpu_training
  117. @pytest.mark.platform_x86_cpu
  118. @pytest.mark.env_onecard
  119. def test_greater():
  120. run_binop_test(mnp_greater, onp_greater, test_case)
  121. def mnp_equal(a, b):
  122. return mnp.equal(a, b)
  123. def onp_equal(a, b):
  124. return onp.equal(a, b)
  125. def test_equal():
  126. run_binop_test(mnp_equal, onp_equal, test_case)
  127. def mnp_isfinite(x):
  128. return mnp.isfinite(x)
  129. def onp_isfinite(x):
  130. return onp.isfinite(x)
  131. @pytest.mark.level1
  132. @pytest.mark.platform_arm_ascend_training
  133. @pytest.mark.platform_x86_ascend_training
  134. @pytest.mark.platform_x86_gpu_training
  135. @pytest.mark.platform_x86_cpu
  136. @pytest.mark.env_onecard
  137. def test_isfinite():
  138. match_res(mnp_isfinite, onp_isfinite, test_case.infs)
  139. def mnp_isnan(x):
  140. return mnp.isnan(x)
  141. def onp_isnan(x):
  142. return onp.isnan(x)
  143. @pytest.mark.level1
  144. @pytest.mark.platform_x86_gpu_training
  145. @pytest.mark.platform_x86_cpu
  146. @pytest.mark.env_onecard
  147. def test_isnan():
  148. match_res(mnp_isnan, onp_isnan, test_case.infs)
  149. def mnp_isinf(x):
  150. return mnp.isinf(x)
  151. def onp_isinf(x):
  152. return onp.isinf(x)
  153. @pytest.mark.level1
  154. @pytest.mark.platform_x86_gpu_training
  155. @pytest.mark.platform_x86_cpu
  156. @pytest.mark.env_onecard
  157. def test_isinf():
  158. match_res(mnp_isinf, onp_isinf, test_case.infs)
  159. def mnp_isposinf(x):
  160. return mnp.isposinf(x)
  161. def onp_isposinf(x):
  162. return onp.isposinf(x)
  163. @pytest.mark.level1
  164. @pytest.mark.platform_x86_gpu_training
  165. @pytest.mark.platform_x86_cpu
  166. @pytest.mark.env_onecard
  167. def test_isposinf():
  168. match_res(mnp_isposinf, onp_isposinf, test_case.infs)
  169. def mnp_isneginf(x):
  170. return mnp.isneginf(x)
  171. def onp_isneginf(x):
  172. return onp.isneginf(x)
  173. @pytest.mark.level1
  174. @pytest.mark.platform_x86_gpu_training
  175. @pytest.mark.platform_x86_cpu
  176. @pytest.mark.env_onecard
  177. def test_isneginf():
  178. match_res(mnp_isneginf, onp_isneginf, test_case.infs)
  179. @pytest.mark.level1
  180. @pytest.mark.platform_arm_ascend_training
  181. @pytest.mark.platform_x86_ascend_training
  182. @pytest.mark.platform_x86_gpu_training
  183. @pytest.mark.platform_x86_cpu
  184. @pytest.mark.env_onecard
  185. def test_isscalar():
  186. assert mnp.isscalar(1) == onp.isscalar(1)
  187. assert mnp.isscalar(2.3) == onp.isscalar(2.3)
  188. assert mnp.isscalar([4.5]) == onp.isscalar([4.5])
  189. assert mnp.isscalar(False) == onp.isscalar(False)
  190. assert mnp.isscalar(to_tensor(True)) == onp.isscalar(onp.array(True))
  191. assert mnp.isscalar('numpy') == onp.isscalar('numpy')
  192. @pytest.mark.level1
  193. @pytest.mark.platform_arm_ascend_training
  194. @pytest.mark.platform_x86_ascend_training
  195. @pytest.mark.platform_x86_gpu_training
  196. @pytest.mark.platform_x86_cpu
  197. @pytest.mark.env_onecard
  198. def test_isclose():
  199. a = [0, 1, 2, float('inf'), float('inf'), float('nan')]
  200. b = [0, 1, -2, float('-inf'), float('inf'), float('nan')]
  201. match_all_arrays(mnp.isclose(a, b), onp.isclose(a, b))
  202. match_all_arrays(mnp.isclose(a, b, equal_nan=True), onp.isclose(a, b, equal_nan=True))
  203. a = rand_int(2, 3, 4, 5)
  204. diff = (onp.random.random((2, 3, 4, 5)).astype("float32") - 0.5) / 1000
  205. b = a + diff
  206. match_all_arrays(mnp.isclose(to_tensor(a), to_tensor(b), atol=1e-3), onp.isclose(a, b, atol=1e-3))
  207. match_all_arrays(mnp.isclose(to_tensor(a), to_tensor(b), atol=1e-3, rtol=1e-4),
  208. onp.isclose(a, b, atol=1e-3, rtol=1e-4))
  209. match_all_arrays(mnp.isclose(to_tensor(a), to_tensor(b), atol=1e-2, rtol=1e-6),
  210. onp.isclose(a, b, atol=1e-2, rtol=1e-6))
  211. a = rand_int(2, 3, 4, 5)
  212. b = rand_int(4, 5)
  213. match_all_arrays(mnp.isclose(to_tensor(a), to_tensor(b)), onp.isclose(a, b))
  214. @pytest.mark.level1
  215. @pytest.mark.platform_arm_ascend_training
  216. @pytest.mark.platform_x86_ascend_training
  217. @pytest.mark.platform_x86_gpu_training
  218. @pytest.mark.platform_x86_cpu
  219. @pytest.mark.env_onecard
  220. def test_in1d():
  221. xi = [rand_int(), rand_int(1), rand_int(10)]
  222. yi = [rand_int(), rand_int(1), rand_int(10)]
  223. for x in xi:
  224. for y in yi:
  225. match_res(mnp.in1d, onp.in1d, x, y)
  226. match_res(mnp.in1d, onp.in1d, x, y, invert=True)
  227. @pytest.mark.level1
  228. @pytest.mark.platform_arm_ascend_training
  229. @pytest.mark.platform_x86_ascend_training
  230. @pytest.mark.platform_x86_gpu_training
  231. @pytest.mark.platform_x86_cpu
  232. @pytest.mark.env_onecard
  233. def test_isin():
  234. xi = [rand_int(), rand_int(1), rand_int(10), rand_int(2, 3)]
  235. yi = [rand_int(), rand_int(1), rand_int(10), rand_int(2, 3)]
  236. for x in xi:
  237. for y in yi:
  238. match_res(mnp.in1d, onp.in1d, x, y)
  239. match_res(mnp.in1d, onp.in1d, x, y, invert=True)
  240. def mnp_logical_or(x1, x2):
  241. return mnp.logical_or(x1, x2)
  242. def onp_logical_or(x1, x2):
  243. return onp.logical_or(x1, x2)
  244. @pytest.mark.level1
  245. @pytest.mark.platform_arm_ascend_training
  246. @pytest.mark.platform_x86_ascend_training
  247. @pytest.mark.platform_x86_gpu_training
  248. @pytest.mark.platform_x86_cpu
  249. @pytest.mark.env_onecard
  250. def test_logical_or():
  251. run_logical_test(mnp_logical_or, onp_logical_or, test_case)
  252. def mnp_logical_xor(x1, x2):
  253. return mnp.logical_xor(x1, x2)
  254. def onp_logical_xor(x1, x2):
  255. return onp.logical_xor(x1, x2)
  256. @pytest.mark.level1
  257. @pytest.mark.platform_arm_ascend_training
  258. @pytest.mark.platform_x86_ascend_training
  259. @pytest.mark.platform_x86_gpu_training
  260. @pytest.mark.platform_x86_cpu
  261. @pytest.mark.env_onecard
  262. def test_logical_xor():
  263. run_logical_test(mnp_logical_xor, onp_logical_xor, test_case)
  264. def mnp_logical_and(x1, x2):
  265. return mnp.logical_and(x1, x2)
  266. def onp_logical_and(x1, x2):
  267. return onp.logical_and(x1, x2)
  268. @pytest.mark.level1
  269. @pytest.mark.platform_arm_ascend_training
  270. @pytest.mark.platform_x86_ascend_training
  271. @pytest.mark.platform_x86_gpu_training
  272. @pytest.mark.platform_x86_cpu
  273. @pytest.mark.env_onecard
  274. def test_logical_and():
  275. run_logical_test(mnp_logical_and, onp_logical_and, test_case)
  276. def mnp_logical_not(x):
  277. return mnp.logical_not(x)
  278. def onp_logical_not(x):
  279. return onp.logical_not(x)
  280. @pytest.mark.level1
  281. @pytest.mark.platform_arm_ascend_training
  282. @pytest.mark.platform_x86_ascend_training
  283. @pytest.mark.platform_x86_gpu_training
  284. @pytest.mark.platform_x86_cpu
  285. @pytest.mark.env_onecard
  286. def test_logical_not():
  287. for arr in test_case.boolean_arrs:
  288. expected = onp_logical_not(arr)
  289. actual = mnp_logical_not(to_tensor(arr))
  290. onp.testing.assert_equal(actual.asnumpy().tolist(), expected.tolist())