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_numpy_ops.py 9.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. """ test numpy ops """
  16. import numpy as np
  17. import mindspore.numpy as mnp
  18. from mindspore import Tensor
  19. from mindspore.nn import Cell
  20. import mindspore.context as context
  21. from ....mindspore_test_framework.mindspore_test import mindspore_test
  22. from ....mindspore_test_framework.pipeline.forward.compile_forward \
  23. import pipeline_for_compile_forward_ge_graph_for_case_by_case_config
  24. context.set_context(mode=context.GRAPH_MODE)
  25. class MeshGrid(Cell):
  26. def construct(self, a, b, c, d):
  27. ret = mnp.meshgrid(a, b, c, d)
  28. return ret
  29. class Choose(Cell):
  30. def construct(self, a, b):
  31. ret = mnp.choose(a, b)
  32. return ret
  33. class Histogram(Cell):
  34. def construct(self, a):
  35. ret = mnp.histogram(a)
  36. return ret
  37. class Norm(Cell):
  38. def construct(self, a):
  39. ret = mnp.norm(a)
  40. return ret
  41. class Cross(Cell):
  42. def construct(self, a, b):
  43. ret = mnp.cross(a, b)
  44. return ret
  45. class Stack(Cell):
  46. def construct(self, a, b):
  47. ret = mnp.stack((a, b))
  48. return ret
  49. class Correlate(Cell):
  50. def construct(self, a, b):
  51. ret = mnp.correlate(a, b)
  52. return ret
  53. class Split(Cell):
  54. def construct(self, tensor):
  55. a = mnp.split(tensor, indices_or_sections=1)
  56. b = mnp.split(tensor, indices_or_sections=3)
  57. c = mnp.array_split(tensor, indices_or_sections=1)
  58. d = mnp.array_split(tensor, indices_or_sections=3, axis=-1)
  59. return a, b, c, d
  60. class MatrixPower(Cell):
  61. def construct(self, tensor):
  62. a = mnp.matrix_power(tensor, 3)
  63. return a
  64. class RavelMultiIndex(Cell):
  65. def construct(self, tensor):
  66. a = mnp.ravel_multi_index(tensor, (7, 6))
  67. b = mnp.ravel_multi_index(tensor, (7, 6), order='F')
  68. c = mnp.ravel_multi_index(tensor, (4, 6), mode='clip')
  69. d = mnp.ravel_multi_index(tensor, (4, 4), mode='wrap')
  70. return a, b, c, d
  71. class GeomSpace(Cell):
  72. def construct(self, start):
  73. a = mnp.geomspace(1, 256, num=9)
  74. b = mnp.geomspace(1, 256, num=8, endpoint=False)
  75. c = mnp.geomspace(start, [1000, 2000, 3000], num=4)
  76. d = mnp.geomspace(start, [1000, 2000, 3000], num=4, endpoint=False, axis=-1)
  77. return a, b, c, d
  78. class Arange(Cell):
  79. def construct(self):
  80. a = mnp.arange(10)
  81. b = mnp.arange(0, 10)
  82. c = mnp.arange(0.1, 9.9)
  83. return a, b, c
  84. class Eye(Cell):
  85. def construct(self):
  86. res = []
  87. for n in range(1, 5):
  88. for k in range(0, 5):
  89. res.append(mnp.eye(10, n, k))
  90. return res
  91. class Trace(Cell):
  92. def construct(self, arr):
  93. a = mnp.trace(arr, offset=-1, axis1=0, axis2=1)
  94. b = mnp.trace(arr, offset=0, axis1=1, axis2=0)
  95. return a, b
  96. class Pad(Cell):
  97. def construct(self, arr1, arr2):
  98. a = mnp.pad(arr1, ((1, 1), (2, 2), (3, 4)))
  99. b = mnp.pad(arr1, ((1, 1), (2, 2), (3, 4)), mode="mean", stat_length=((1, 2), (2, 10), (3, 4)))
  100. c = mnp.pad(arr1, ((1, 1), (2, 2), (3, 4)), mode="edge")
  101. d = mnp.pad(arr1, ((1, 1), (2, 2), (3, 4)), mode="wrap")
  102. e = mnp.pad(arr1, ((1, 3), (5, 2), (3, 0)), mode="linear_ramp", end_values=((0, 10), (9, 1), (-10, 99)))
  103. f = mnp.pad(arr2, ((10, 13), (5, 12), (3, 0), (2, 6)), mode='symmetric', reflect_type='even')
  104. g = mnp.pad(arr2, ((10, 13)), mode='reflect', reflect_type='even')
  105. return a, b, c, d, e, f, g
  106. class Where(Cell):
  107. def construct(self, a, b, c):
  108. ret = mnp.where(a, b, c)
  109. return ret
  110. class Select(Cell):
  111. def construct(self, a, b):
  112. ret = mnp.select(a, b)
  113. return ret
  114. class IsClose(Cell):
  115. def construct(self, a, b):
  116. ret = mnp.isclose(a, b)
  117. return ret
  118. class Average(Cell):
  119. def construct(self, a):
  120. ret = mnp.average(a)
  121. return ret
  122. class Remainder(Cell):
  123. def construct(self, a, b):
  124. ret = mnp.remainder(a, b)
  125. return ret
  126. class Diff(Cell):
  127. def construct(self, a):
  128. ret1 = mnp.diff(a)
  129. ret2 = mnp.ediff1d(a)
  130. return ret1, ret2
  131. class Trapz(Cell):
  132. def construct(self, arr):
  133. a = mnp.trapz(arr, x=[-2, 1, 2], axis=1)
  134. b = mnp.trapz(arr, dx=3, axis=0)
  135. return a, b
  136. class Lcm(Cell):
  137. def construct(self, a, b):
  138. ret = mnp.lcm(a, b)
  139. return ret
  140. class Cov(Cell):
  141. def construct(self, a):
  142. ret = mnp.cov(a, a)
  143. return ret
  144. class Gradient(Cell):
  145. def construct(self, a):
  146. ret = mnp.gradient(a)
  147. return ret
  148. class MultiDot(Cell):
  149. def construct(self, a, b, c, d):
  150. ret = mnp.multi_dot((a, b, c, d))
  151. return ret
  152. class Histogramdd(Cell):
  153. def construct(self, a):
  154. ret = mnp.histogramdd(a)
  155. return ret
  156. test_cases = [
  157. ('MeshGrid', {
  158. 'block': MeshGrid(),
  159. 'desc_inputs': [Tensor(np.full(3, 2, dtype=np.float32)),
  160. Tensor(np.full(1, 5, dtype=np.float32)),
  161. Tensor(np.full((2, 3), 9, dtype=np.float32)),
  162. Tensor(np.full((4, 5, 6), 7, dtype=np.float32))],
  163. }),
  164. ('Norm', {
  165. 'block': Norm(),
  166. 'desc_inputs': [Tensor(np.ones((5, 2, 3, 7), dtype=np.float32))],
  167. }),
  168. ('Cross', {
  169. 'block': Cross(),
  170. 'desc_inputs': [Tensor(np.arange(18, dtype=np.int32).reshape(2, 3, 1, 3)),
  171. Tensor(np.arange(9, dtype=np.int32).reshape(1, 3, 3))],
  172. }),
  173. ('Stack', {
  174. 'block': Stack(),
  175. 'desc_inputs': [Tensor(np.arange(9, dtype=np.int32).reshape(3, 3)),
  176. Tensor(np.arange(9, dtype=np.int32).reshape(3, 3)),],
  177. }),
  178. ('Correlate', {
  179. 'block': Correlate(),
  180. 'desc_inputs': [Tensor(np.array([1, 2, 3, 4, 5], dtype=np.int32)),
  181. Tensor(np.array([0, 1], dtype=np.int32)),],
  182. }),
  183. ('Split', {
  184. 'block': Split(),
  185. 'desc_inputs': [Tensor(np.arange(9, dtype=np.float32).reshape(3, 3))],
  186. }),
  187. ('MatrixPower', {
  188. 'block': MatrixPower(),
  189. 'desc_inputs': [Tensor(np.arange(9, dtype=np.float32).reshape(3, 3))],
  190. }),
  191. ('RavelMultiIndex', {
  192. 'block': RavelMultiIndex(),
  193. 'desc_inputs': [Tensor(np.array([[3, 6, 6], [4, 5, 1]], dtype=np.int32))],
  194. }),
  195. ('GeomSpace', {
  196. 'block': GeomSpace(),
  197. 'desc_inputs': [Tensor(np.arange(1, 7, dtype=np.float32).reshape(2, 3))],
  198. }),
  199. ('Arange', {
  200. 'block': Arange(),
  201. 'desc_inputs': [],
  202. }),
  203. ('Eye', {
  204. 'block': Eye(),
  205. 'desc_inputs': [],
  206. }),
  207. ('Trace', {
  208. 'block': Trace(),
  209. 'desc_inputs': [Tensor(np.ones((3, 5), dtype=np.float32))],
  210. }),
  211. ('Where', {
  212. 'block': Where(),
  213. 'desc_inputs': [Tensor(np.full((1, 1, 2), [False, True])),
  214. Tensor(np.full((1, 3, 2), 5, dtype=np.float32)),
  215. Tensor(np.full((2, 1, 1), 7, dtype=np.float32))],
  216. }),
  217. ('Select', {
  218. 'block': Select(),
  219. 'desc_inputs': [Tensor([[True, True, True, False, False], [False, False, True, False, True]]),
  220. Tensor(np.array([[0, 1, 2, 3, 4], [0, 1, 4, 9, 16]], dtype=np.int32))],
  221. }),
  222. ('IsClose', {
  223. 'block': IsClose(),
  224. 'desc_inputs': [Tensor(np.array([0, 1, 2, float('inf'), float('inf'), float('nan')], dtype=np.float32)),
  225. Tensor(np.array([0, 1, -2, float('-inf'), float('inf'), float('nan')], dtype=np.float32))],
  226. }),
  227. ('Average', {
  228. 'block': Average(),
  229. 'desc_inputs': [Tensor(np.array([[1., 2.], [3., 4.]], dtype=np.float32))],
  230. }),
  231. ('Remainder', {
  232. 'block': Remainder(),
  233. 'desc_inputs': [Tensor(np.array([4, 7], dtype=np.int32)),
  234. Tensor(np.array([[1, 2], [3, 4]], dtype=np.int32))],
  235. }),
  236. ('Diff', {
  237. 'block': Diff(),
  238. 'desc_inputs': [Tensor(np.array([1, 3, -1, 0, 4], dtype=np.int32))],
  239. }),
  240. ('Trapz', {
  241. 'block': Trapz(),
  242. 'desc_inputs': [Tensor(np.arange(6, dtype=np.int32).reshape(2, 3))],
  243. }),
  244. ('Lcm', {
  245. 'block': Lcm(),
  246. 'desc_inputs': [Tensor(np.arange(6, dtype=np.int32)),
  247. Tensor(np.array(20, dtype=np.int32))],
  248. }),
  249. ('Cov', {
  250. 'block': Cov(),
  251. 'desc_inputs': [Tensor(np.array([[2., 3., 4., 5.], [0., 2., 3., 4.], [7., 8., 9., 10.]], dtype=np.float32))],
  252. }),
  253. ('Gradient', {
  254. 'block': Gradient(),
  255. 'desc_inputs': [Tensor(np.array([[2., 3., 4., 5.], [0., 2., 3., 4.], [7., 8., 9., 10.]], dtype=np.float32))],
  256. }),
  257. ('MultiDot', {
  258. 'block': MultiDot(),
  259. 'desc_inputs': [Tensor(np.ones((10000, 100), dtype=np.float32)),
  260. Tensor(np.ones((100, 1000), dtype=np.float32)),
  261. Tensor(np.ones((1000, 5), dtype=np.float32)),
  262. Tensor(np.ones((5, 333), dtype=np.float32))],
  263. }),
  264. ]
  265. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  266. def test_exec():
  267. context.set_context(mode=context.GRAPH_MODE)
  268. return test_cases