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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 mindspore import context
  20. from .utils import rand_int, rand_bool, run_binop_test, run_logical_test, match_res, \
  21. match_all_arrays, to_tensor
  22. context.set_context(mode=context.PYNATIVE_MODE)
  23. class Cases():
  24. def __init__(self):
  25. self.arrs = [
  26. rand_int(2),
  27. rand_int(2, 3),
  28. rand_int(2, 3, 4),
  29. rand_int(2, 3, 4, 5),
  30. ]
  31. # scalars expanded across the 0th dimension
  32. self.scalars = [
  33. rand_int(),
  34. rand_int(1),
  35. rand_int(1, 1),
  36. rand_int(1, 1, 1, 1),
  37. ]
  38. # arrays of the same size expanded across the 0th dimension
  39. self.expanded_arrs = [
  40. rand_int(2, 3),
  41. rand_int(1, 2, 3),
  42. rand_int(1, 1, 2, 3),
  43. rand_int(1, 1, 1, 2, 3),
  44. ]
  45. # arrays which can be broadcast
  46. self.broadcastables = [
  47. rand_int(5),
  48. rand_int(6, 1),
  49. rand_int(7, 1, 5),
  50. rand_int(8, 1, 6, 1)
  51. ]
  52. # Boolean arrays
  53. self.boolean_arrs = [
  54. rand_bool(),
  55. rand_bool(5),
  56. rand_bool(6, 1),
  57. rand_bool(7, 1, 5),
  58. rand_bool(8, 1, 6, 1)
  59. ]
  60. # array which contains infs and nans
  61. self.infs = onp.array([[1.0, onp.nan], [onp.inf, onp.NINF], [2.3, -4.5], [onp.nan, 0.0]])
  62. test_case = Cases()
  63. def mnp_not_equal(a, b):
  64. return mnp.not_equal(a, b)
  65. def onp_not_equal(a, b):
  66. return onp.not_equal(a, b)
  67. @pytest.mark.level1
  68. @pytest.mark.platform_arm_ascend_training
  69. @pytest.mark.platform_x86_ascend_training
  70. @pytest.mark.platform_x86_gpu_training
  71. @pytest.mark.platform_x86_cpu
  72. @pytest.mark.env_onecard
  73. def test_not_equal():
  74. run_binop_test(mnp_not_equal, onp_not_equal, test_case)
  75. def mnp_less_equal(a, b):
  76. return mnp.less_equal(a, b)
  77. def onp_less_equal(a, b):
  78. return onp.less_equal(a, b)
  79. @pytest.mark.level1
  80. @pytest.mark.platform_arm_ascend_training
  81. @pytest.mark.platform_x86_ascend_training
  82. @pytest.mark.platform_x86_gpu_training
  83. @pytest.mark.platform_x86_cpu
  84. @pytest.mark.env_onecard
  85. def test_less_equal():
  86. run_binop_test(mnp_less_equal, onp_less_equal, test_case)
  87. def mnp_less(a, b):
  88. return mnp.less(a, b)
  89. def onp_less(a, b):
  90. return onp.less(a, b)
  91. @pytest.mark.level1
  92. @pytest.mark.platform_arm_ascend_training
  93. @pytest.mark.platform_x86_ascend_training
  94. @pytest.mark.platform_x86_gpu_training
  95. @pytest.mark.platform_x86_cpu
  96. @pytest.mark.env_onecard
  97. def test_less():
  98. run_binop_test(mnp_less, onp_less, test_case)
  99. def mnp_greater_equal(a, b):
  100. return mnp.greater_equal(a, b)
  101. def onp_greater_equal(a, b):
  102. return onp.greater_equal(a, b)
  103. @pytest.mark.level1
  104. @pytest.mark.platform_arm_ascend_training
  105. @pytest.mark.platform_x86_ascend_training
  106. @pytest.mark.platform_x86_gpu_training
  107. @pytest.mark.platform_x86_cpu
  108. @pytest.mark.env_onecard
  109. def test_greater_equal():
  110. run_binop_test(mnp_greater_equal, onp_greater_equal, test_case)
  111. def mnp_greater(a, b):
  112. return mnp.greater(a, b)
  113. def onp_greater(a, b):
  114. return onp.greater(a, b)
  115. @pytest.mark.level1
  116. @pytest.mark.platform_arm_ascend_training
  117. @pytest.mark.platform_x86_ascend_training
  118. @pytest.mark.platform_x86_gpu_training
  119. @pytest.mark.platform_x86_cpu
  120. @pytest.mark.env_onecard
  121. def test_greater():
  122. run_binop_test(mnp_greater, onp_greater, test_case)
  123. def mnp_equal(a, b):
  124. return mnp.equal(a, b)
  125. def onp_equal(a, b):
  126. return onp.equal(a, b)
  127. def test_equal():
  128. run_binop_test(mnp_equal, onp_equal, test_case)
  129. def mnp_isfinite(x):
  130. return mnp.isfinite(x)
  131. def onp_isfinite(x):
  132. return onp.isfinite(x)
  133. @pytest.mark.level0
  134. @pytest.mark.platform_arm_ascend_training
  135. @pytest.mark.platform_x86_ascend_training
  136. @pytest.mark.platform_x86_gpu_training
  137. @pytest.mark.platform_x86_cpu
  138. @pytest.mark.env_onecard
  139. def test_isfinite():
  140. match_res(mnp_isfinite, onp_isfinite, test_case.infs)
  141. def mnp_isnan(x):
  142. return mnp.isnan(x)
  143. def onp_isnan(x):
  144. return onp.isnan(x)
  145. @pytest.mark.level0
  146. @pytest.mark.platform_x86_gpu_training
  147. @pytest.mark.platform_x86_cpu
  148. @pytest.mark.env_onecard
  149. def test_isnan():
  150. match_res(mnp_isnan, onp_isnan, test_case.infs)
  151. def mnp_isinf(x):
  152. return mnp.isinf(x)
  153. def onp_isinf(x):
  154. return onp.isinf(x)
  155. @pytest.mark.level0
  156. @pytest.mark.platform_x86_gpu_training
  157. @pytest.mark.platform_x86_cpu
  158. @pytest.mark.env_onecard
  159. def test_isinf():
  160. match_res(mnp_isinf, onp_isinf, test_case.infs)
  161. def mnp_isposinf(x):
  162. return mnp.isposinf(x)
  163. def onp_isposinf(x):
  164. return onp.isposinf(x)
  165. @pytest.mark.level1
  166. @pytest.mark.platform_x86_gpu_training
  167. @pytest.mark.platform_x86_cpu
  168. @pytest.mark.env_onecard
  169. def test_isposinf():
  170. match_res(mnp_isposinf, onp_isposinf, test_case.infs)
  171. def mnp_isneginf(x):
  172. return mnp.isneginf(x)
  173. def onp_isneginf(x):
  174. return onp.isneginf(x)
  175. @pytest.mark.level1
  176. @pytest.mark.platform_x86_gpu_training
  177. @pytest.mark.platform_x86_cpu
  178. @pytest.mark.env_onecard
  179. def test_isneginf():
  180. match_res(mnp_isneginf, onp_isneginf, test_case.infs)
  181. @pytest.mark.level1
  182. @pytest.mark.platform_arm_ascend_training
  183. @pytest.mark.platform_x86_ascend_training
  184. @pytest.mark.platform_x86_gpu_training
  185. @pytest.mark.platform_x86_cpu
  186. @pytest.mark.env_onecard
  187. def test_isscalar():
  188. assert mnp.isscalar(1) == onp.isscalar(1)
  189. assert mnp.isscalar(2.3) == onp.isscalar(2.3)
  190. assert mnp.isscalar([4.5]) == onp.isscalar([4.5])
  191. assert mnp.isscalar(False) == onp.isscalar(False)
  192. assert mnp.isscalar(to_tensor(True)) == onp.isscalar(onp.array(True))
  193. assert mnp.isscalar('numpy') == onp.isscalar('numpy')
  194. @pytest.mark.level0
  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())
  291. @pytest.mark.level1
  292. @pytest.mark.platform_x86_gpu_training
  293. @pytest.mark.platform_x86_cpu
  294. @pytest.mark.env_onecard
  295. def test_array_equal():
  296. a = [0, 1, 2, float('inf'), float('nan')]
  297. b = [0, 1, 2, float('inf'), float('nan')]
  298. match_all_arrays(mnp.array_equal(a, b), onp.array_equal(a, b))
  299. a = [0, 1, 2]
  300. b = [[0, 1, 2], [0, 1, 2]]
  301. assert mnp.array_equal(a, b) == onp.array_equal(a, b)
  302. @pytest.mark.level1
  303. @pytest.mark.platform_x86_gpu_training
  304. @pytest.mark.platform_x86_cpu
  305. @pytest.mark.env_onecard
  306. def test_array_equiv():
  307. a = [0, 1, 2, float('inf'), float('nan')]
  308. b = [0, 1, 2, float('inf'), float('nan')]
  309. match_all_arrays(mnp.array_equal(a, b), onp.array_equal(a, b))
  310. a = [0, 1, 2]
  311. b = [[0, 1, 2], [0, 1, 2]]
  312. assert mnp.array_equal(a, b) == onp.array_equal(a, b)
  313. def mnp_signbit(*arrs):
  314. arr1 = arrs[0]
  315. arr2 = arrs[1]
  316. a = mnp.signbit(arr1)
  317. b = mnp.signbit(arr2, dtype=mnp.bool_)
  318. return a, b
  319. def onp_signbit(*arrs):
  320. arr1 = arrs[0]
  321. arr2 = arrs[1]
  322. a = onp.signbit(arr1)
  323. b = onp.signbit(arr2, dtype='bool')
  324. return a, b
  325. @pytest.mark.level1
  326. @pytest.mark.platform_arm_ascend_training
  327. @pytest.mark.platform_x86_ascend_training
  328. @pytest.mark.platform_x86_gpu_training
  329. @pytest.mark.platform_x86_cpu
  330. @pytest.mark.env_onecard
  331. def test_signbit():
  332. onp_arrs = [onp.arange(-10, 10).astype('float32'), onp.arange(-10, 10).astype('int32')]
  333. mnp_arrs = [mnp.arange(-10, 10).astype('float32'), mnp.arange(-10, 10).astype('int32')]
  334. for actual, expected in zip(mnp_signbit(*mnp_arrs), onp_signbit(*onp_arrs)):
  335. onp.testing.assert_equal(actual.asnumpy().tolist(), expected.tolist())
  336. def mnp_sometrue(x):
  337. a = mnp.sometrue(x)
  338. b = mnp.sometrue(x, axis=0)
  339. c = mnp.sometrue(x, axis=(0, -1))
  340. d = mnp.sometrue(x, axis=(0, 1), keepdims=True)
  341. e = mnp.sometrue(x, axis=(0, 1), keepdims=-1)
  342. f = mnp.sometrue(x, axis=(0, 1), keepdims=0)
  343. return a, b, c, d, e, f
  344. def onp_sometrue(x):
  345. a = onp.sometrue(x)
  346. b = onp.sometrue(x, axis=0)
  347. c = onp.sometrue(x, axis=(0, -1))
  348. d = onp.sometrue(x, axis=(0, 1), keepdims=True)
  349. e = onp.sometrue(x, axis=(0, 1), keepdims=-1)
  350. f = onp.sometrue(x, axis=(0, 1), keepdims=0)
  351. return a, b, c, d, e, f
  352. @pytest.mark.level1
  353. @pytest.mark.platform_arm_ascend_training
  354. @pytest.mark.platform_x86_ascend_training
  355. @pytest.mark.platform_x86_gpu_training
  356. @pytest.mark.platform_x86_cpu
  357. @pytest.mark.env_onecard
  358. def test_sometrue():
  359. onp_arr = onp.full((3, 2), [True, False])
  360. mnp_arr = to_tensor(onp_arr)
  361. for actual, expected in zip(mnp_sometrue(mnp_arr), onp_sometrue(onp_arr)):
  362. onp.testing.assert_equal(actual.asnumpy().tolist(), expected.tolist())