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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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, run_binop_test, match_res
  20. class Cases():
  21. def __init__(self):
  22. self.arrs = [
  23. rand_int(2),
  24. rand_int(2, 3),
  25. rand_int(2, 3, 4),
  26. rand_int(2, 3, 4, 5),
  27. ]
  28. # scalars expanded across the 0th dimension
  29. self.scalars = [
  30. rand_int(),
  31. rand_int(1),
  32. rand_int(1, 1),
  33. rand_int(1, 1, 1, 1),
  34. ]
  35. # arrays of the same size expanded across the 0th dimension
  36. self.expanded_arrs = [
  37. rand_int(2, 3),
  38. rand_int(1, 2, 3),
  39. rand_int(1, 1, 2, 3),
  40. rand_int(1, 1, 1, 2, 3),
  41. ]
  42. # arrays which can be broadcast
  43. self.broadcastables = [
  44. rand_int(5),
  45. rand_int(6, 1),
  46. rand_int(7, 1, 5),
  47. rand_int(8, 1, 6, 1)
  48. ]
  49. # array which contains infs and nans
  50. self.infs = onp.array([[1.0, onp.nan], [onp.inf, onp.NINF], [2.3, -4.5], [onp.nan, 0.0]])
  51. test_case = Cases()
  52. def mnp_not_equal(a, b):
  53. return mnp.not_equal(a, b)
  54. def onp_not_equal(a, b):
  55. return onp.not_equal(a, b)
  56. @pytest.mark.level1
  57. @pytest.mark.platform_arm_ascend_training
  58. @pytest.mark.platform_x86_ascend_training
  59. @pytest.mark.platform_x86_gpu_training
  60. @pytest.mark.platform_x86_cpu
  61. @pytest.mark.env_onecard
  62. def test_not_equal():
  63. run_binop_test(mnp_not_equal, onp_not_equal, test_case)
  64. def mnp_less_equal(a, b):
  65. return mnp.less_equal(a, b)
  66. def onp_less_equal(a, b):
  67. return onp.less_equal(a, b)
  68. @pytest.mark.level1
  69. @pytest.mark.platform_arm_ascend_training
  70. @pytest.mark.platform_x86_ascend_training
  71. @pytest.mark.platform_x86_gpu_training
  72. @pytest.mark.platform_x86_cpu
  73. @pytest.mark.env_onecard
  74. def test_less_equal():
  75. run_binop_test(mnp_less_equal, onp_less_equal, test_case)
  76. def mnp_less(a, b):
  77. return mnp.less(a, b)
  78. def onp_less(a, b):
  79. return onp.less(a, b)
  80. @pytest.mark.level1
  81. @pytest.mark.platform_arm_ascend_training
  82. @pytest.mark.platform_x86_ascend_training
  83. @pytest.mark.platform_x86_gpu_training
  84. @pytest.mark.platform_x86_cpu
  85. @pytest.mark.env_onecard
  86. def test_less():
  87. run_binop_test(mnp_less, onp_less, test_case)
  88. def mnp_greater_equal(a, b):
  89. return mnp.greater_equal(a, b)
  90. def onp_greater_equal(a, b):
  91. return onp.greater_equal(a, b)
  92. @pytest.mark.level1
  93. @pytest.mark.platform_arm_ascend_training
  94. @pytest.mark.platform_x86_ascend_training
  95. @pytest.mark.platform_x86_gpu_training
  96. @pytest.mark.platform_x86_cpu
  97. @pytest.mark.env_onecard
  98. def test_greater_equal():
  99. run_binop_test(mnp_greater_equal, onp_greater_equal, test_case)
  100. def mnp_greater(a, b):
  101. return mnp.greater(a, b)
  102. def onp_greater(a, b):
  103. return onp.greater(a, b)
  104. @pytest.mark.level1
  105. @pytest.mark.platform_arm_ascend_training
  106. @pytest.mark.platform_x86_ascend_training
  107. @pytest.mark.platform_x86_gpu_training
  108. @pytest.mark.platform_x86_cpu
  109. @pytest.mark.env_onecard
  110. def test_greater():
  111. run_binop_test(mnp_greater, onp_greater, test_case)
  112. def mnp_equal(a, b):
  113. return mnp.equal(a, b)
  114. def onp_equal(a, b):
  115. return onp.equal(a, b)
  116. def test_equal():
  117. run_binop_test(mnp_equal, onp_equal, test_case)
  118. def mnp_isfinite(x):
  119. return mnp.isfinite(x)
  120. def onp_isfinite(x):
  121. return onp.isfinite(x)
  122. @pytest.mark.level1
  123. @pytest.mark.platform_arm_ascend_training
  124. @pytest.mark.platform_x86_ascend_training
  125. @pytest.mark.platform_x86_gpu_training
  126. @pytest.mark.platform_x86_cpu
  127. @pytest.mark.env_onecard
  128. def test_isfinite():
  129. match_res(mnp_isfinite, onp_isfinite, test_case.infs)
  130. def mnp_isnan(x):
  131. return mnp.isnan(x)
  132. def onp_isnan(x):
  133. return onp.isnan(x)
  134. @pytest.mark.level1
  135. @pytest.mark.platform_arm_ascend_training
  136. @pytest.mark.platform_x86_ascend_training
  137. @pytest.mark.platform_x86_gpu_training
  138. @pytest.mark.platform_x86_cpu
  139. @pytest.mark.env_onecard
  140. def test_isnan():
  141. match_res(mnp_isnan, onp_isnan, test_case.infs)
  142. def mnp_isinf(x):
  143. return mnp.isinf(x)
  144. def onp_isinf(x):
  145. return onp.isinf(x)
  146. @pytest.mark.level1
  147. @pytest.mark.platform_arm_ascend_training
  148. @pytest.mark.platform_x86_ascend_training
  149. @pytest.mark.platform_x86_gpu_training
  150. @pytest.mark.platform_x86_cpu
  151. @pytest.mark.env_onecard
  152. def test_isinf():
  153. match_res(mnp_isinf, onp_isinf, test_case.infs)
  154. def mnp_isposinf(x):
  155. return mnp.isposinf(x)
  156. def onp_isposinf(x):
  157. return onp.isposinf(x)
  158. @pytest.mark.level1
  159. @pytest.mark.platform_arm_ascend_training
  160. @pytest.mark.platform_x86_ascend_training
  161. @pytest.mark.platform_x86_gpu_training
  162. @pytest.mark.platform_x86_cpu
  163. @pytest.mark.env_onecard
  164. def test_isposinf():
  165. match_res(mnp_isposinf, onp_isposinf, test_case.infs)
  166. def mnp_isneginf(x):
  167. return mnp.isneginf(x)
  168. def onp_isneginf(x):
  169. return onp.isneginf(x)
  170. @pytest.mark.level1
  171. @pytest.mark.platform_arm_ascend_training
  172. @pytest.mark.platform_x86_ascend_training
  173. @pytest.mark.platform_x86_gpu_training
  174. @pytest.mark.platform_x86_cpu
  175. @pytest.mark.env_onecard
  176. def test_isneginf():
  177. match_res(mnp_isneginf, onp_isneginf, test_case.infs)
  178. def test_isscalar():
  179. assert mnp.isscalar(1) == onp.isscalar(1)
  180. assert mnp.isscalar(2.3) == onp.isscalar(2.3)
  181. assert mnp.isscalar([4.5]) == onp.isscalar([4.5])
  182. assert mnp.isscalar(False) == onp.isscalar(False)
  183. assert mnp.isscalar(mnp.array(True)) == onp.isscalar(onp.array(True))
  184. assert mnp.isscalar('numpy') == onp.isscalar('numpy')