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_tensor_slice.py 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. # Copyright 2020 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_tensor_slice """
  16. import numpy as np
  17. import pytest
  18. from mindspore import Tensor
  19. from mindspore import context
  20. from mindspore import dtype as mstype
  21. from mindspore.nn import Cell
  22. from ....mindspore_test_framework.mindspore_test import mindspore_test
  23. from ....mindspore_test_framework.pipeline.forward.compile_forward \
  24. import pipeline_for_compile_forward_ge_graph_for_case_by_case_config
  25. class NetWorkSlicePositive(Cell):
  26. def __init__(self):
  27. super(NetWorkSlicePositive, self).__init__()
  28. self.tensor_ret0 = Tensor(np.ones([1, 2, 2], np.int32))
  29. self.tensor_ret1 = Tensor(np.ones([4, 7, 4], np.int32))
  30. self.tensor_ret2 = Tensor(np.ones([6, 8, 10], np.int32))
  31. self.tensor_ret3 = Tensor(np.ones([3, 8, 10], np.int32))
  32. def construct(self, tensor):
  33. ret0 = tensor[3:4:3, 1:5:2, 3:6:2] + self.tensor_ret0
  34. ret1 = tensor[-6:4:1, 7:-8:-1, ::3] + self.tensor_ret1
  35. ret2 = tensor[::, ::, ::] + self.tensor_ret2
  36. ret3 = tensor[::2] + self.tensor_ret3
  37. return ret0, ret1, ret2, ret3
  38. class NetWorkSliceEllipsis(Cell):
  39. def __init__(self):
  40. super(NetWorkSliceEllipsis, self).__init__()
  41. self.tensor_ret0 = Tensor(np.ones([2, 7, 8], np.int32))
  42. self.tensor_ret1 = Tensor(np.ones([6, 7, 8, 9], np.int32))
  43. self.tensor_ret2 = Tensor(np.ones([1, 6, 7, 8, 9], np.int32))
  44. def construct(self, tensor):
  45. ret0 = tensor[0:4:2, ..., 1] + self.tensor_ret0
  46. ret1 = tensor[...] + self.tensor_ret1
  47. ret2 = tensor[None] + self.tensor_ret2
  48. ret3 = tensor[True] + self.tensor_ret2
  49. return ret0, ret1, ret2, ret3
  50. class NetWorkReduceDimension(Cell):
  51. def __init__(self):
  52. super(NetWorkReduceDimension, self).__init__()
  53. self.tensor_ret0 = Tensor(np.ones([2, 4, 1], np.int32))
  54. self.tensor_ret1 = Tensor(np.ones([3, 4], np.int32))
  55. self.tensor_ret2 = Tensor(np.ones([6, 8], np.int32))
  56. self.tensor_ret3 = Tensor(np.array(8, np.int32))
  57. self.tensor_ret4 = Tensor(np.ones([8, 10], np.int32))
  58. def construct(self, tensor):
  59. ret0 = tensor[0:6:3, 1:5:1, 3:5:2] + self.tensor_ret0
  60. ret1 = tensor[::2, 1, ::3] + self.tensor_ret1
  61. ret2 = tensor[::, ::, 0] + self.tensor_ret2
  62. ret3 = tensor[3, 2, 5] + self.tensor_ret3
  63. ret4 = tensor[1] + self.tensor_ret4
  64. return ret0, ret1, ret2, ret3, ret4
  65. class NetWorkStepNegative(Cell):
  66. def __init__(self):
  67. super(NetWorkStepNegative, self).__init__()
  68. self.tensor_ret = Tensor(np.ones([6, 5, 10], np.int32))
  69. def construct(self, tensor):
  70. ret = tensor[::1, -5::, ::-1] + self.tensor_ret
  71. return ret
  72. class NetWorkReduceToScalar(Cell):
  73. def __init__(self):
  74. super(NetWorkReduceToScalar, self).__init__()
  75. self.tensor_ret = Tensor(np.array(9, np.int32))
  76. def construct(self, tensor):
  77. ret = tensor[2, 3, 4] + self.tensor_ret
  78. return ret
  79. class TensorAssignWithSliceError1(Cell):
  80. def __init__(self):
  81. super(TensorAssignWithSliceError1, self).__init__()
  82. def construct(self, a, b):
  83. a[1:3:-1,::] = b
  84. return a
  85. class TensorAssignWithSliceError2(Cell):
  86. def __init__(self):
  87. super(TensorAssignWithSliceError2, self).__init__()
  88. def construct(self, a, b):
  89. a[1:3:-1] = b
  90. return a
  91. class TensorAssignWithSlice2(Cell):
  92. def __init__(self):
  93. super(TensorAssignWithSlice2, self).__init__()
  94. def construct(self, a, b):
  95. a[1:5] = b
  96. a[3:4] = 5
  97. a[-1:1:-1] = b
  98. a[-1:3:-1] = 5
  99. a[::] = b
  100. a[::] = 9
  101. return a
  102. class TensorAssignWithSlice(Cell):
  103. def __init__(self):
  104. super(TensorAssignWithSlice, self).__init__()
  105. self.c = 2
  106. def construct(self, a, b):
  107. a[1:3,::] = b
  108. a[2:3:,3:] = b
  109. a[::] = b
  110. a[::] = self.c
  111. a[::,::] = b
  112. a[::,::] = self.c
  113. a[2:3:,0:, 4:1:-1] = b
  114. a[2:3:,0:, 4:1:-1] = self.c
  115. z = a
  116. return z
  117. def test_tensor_assign_with_slice():
  118. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  119. net = TensorAssignWithSlice()
  120. net2= TensorAssignWithSlice2()
  121. net_e1 = TensorAssignWithSliceError1()
  122. net_e2 = TensorAssignWithSliceError2()
  123. a = np.arange(60).reshape(3,4,5)
  124. b = Tensor([1])
  125. Ta = Tensor(a)
  126. Tb= Tensor([1,3])
  127. Tc= Tensor([])
  128. t = Tensor([1, 2, 3, 4, 5, 6, 7, 8])
  129. net(Ta, b)
  130. net2(t, b)
  131. # Error for A[Slice] = Number
  132. # 1. A[Slice] = Number, Slice error
  133. with pytest.raises(ValueError):
  134. net_e2(t, 2)
  135. # Error for A[Slice] = U, U is a Tensor
  136. # 1. A[Slice] = U, u.size is error
  137. with pytest.raises(ValueError):
  138. net2(t, Tb)
  139. # 2. A[Slice] = U, U is empty
  140. with pytest.raises(ValueError):
  141. net2(t, Tc)
  142. # 3. A[Slice] = U, U.size error
  143. with pytest.raises(ValueError):
  144. net2(t, Tb)
  145. # Error for A[Tuple(Slice...)] = Tensor
  146. # 1. A[Tuple(Slice...)] = U, U is empty
  147. with pytest.raises(ValueError):
  148. net(Ta, Tc)
  149. # 2. A[Tuple(Slice...)] = U, U.size error
  150. with pytest.raises(ValueError):
  151. net(Ta, Tb)
  152. # 3. A[Tuple(Slice...)] = U, Slice error
  153. with pytest.raises(ValueError):
  154. net_e1(Ta, b)
  155. # Error for A[Tuple(Slice...)] = Number
  156. # 1. A[Tuple(Slice...)] = Number, Slice error
  157. with pytest.raises(ValueError):
  158. net_e1(Ta, 2)
  159. class TensorAssignWithBoolTensorIndex(Cell):
  160. def __init__(self):
  161. super(TensorAssignWithBoolTensorIndex, self).__init__()
  162. self.t = Tensor(np.arange(60).reshape([3,4,5]), dtype = mstype.float64)
  163. def construct(self, a, b, c, u_tensor, _scalar):
  164. a[c] = u_scalar
  165. a[b] = u_tensor
  166. z = a + self.t
  167. return z
  168. class TensorAssignWithBoolTensorIndexError(Cell):
  169. def __init__(self):
  170. super(TensorAssignWithBoolTensorIndexError, self).__init__()
  171. def construct(self, a, b, c, u_tensor):
  172. a[b][c] = u_tensor
  173. return a
  174. class TensorAssignWithBoolTensorIndex2(Cell):
  175. def __init__(self):
  176. super(TensorAssignWithBoolTensorIndex2, self).__init__()
  177. self.t = Tensor(np.arange(6).reshape([2, 3]), dtype=mstype.float64)
  178. self.t = Tensor(np.arange(60).reshape([3,4,5]), dtype = mstype.float64)
  179. def construct(self, a, u_tensor, _scalar):
  180. a[a > 8] = u_tensor
  181. a[a >= 6] = u_scalar
  182. a[a < 3] = u_scalar
  183. a[a <= 5] = u_tensor
  184. a[a == 5] = u_scalar
  185. z = a + self.t
  186. return z
  187. class TensorAssignWithBoolTensorIndex2Error(Cell):
  188. def __init__(self):
  189. super(TensorAssignWithBoolTensorIndex2Error, self).__init__()
  190. def construct(self, a, u_tensor):
  191. a[a > 8][a > 5] = u_tensor
  192. return a
  193. a = np.random.uniform(1,10,[3,4,5])
  194. b = a > 5
  195. c = a < 3
  196. Ta = Tensor(a)
  197. Tb = Tensor(b)
  198. Tc = Tensor(c)
  199. Td = Tensor([True, True])
  200. u_tensor = Tensor([1])
  201. u_tensor_error = Tensor([1, 2])
  202. t_1d = Tensor([1, 2, 3, 4, 5, 6, 7, 8])
  203. u_scalar = 5
  204. def test_tensor_assign_bool_index():
  205. net1 = TensorAssignWithBoolTensorIndex()
  206. net2 = TensorAssignWithBoolTensorIndex2()
  207. net1(Ta, Tb, Tc, u_tensor, u_scalar)
  208. net1(Ta, Tb, Tc, u_tensor, u_scalar)
  209. with pytest.raises(ValueError):
  210. net1(Ta, Td, Tc, u_tensor, u_scalar)
  211. with pytest.raises(ValueError):
  212. net1(Ta, u_tensor, Tc, u_tensor, u_scalar)
  213. with pytest.raises(ValueError):
  214. net1(Ta, Tb, Td, u_tensor, u_scalar)
  215. with pytest.raises(ValueError):
  216. net1(Ta, Tb, Ta, u_tensor, u_scalar)
  217. with pytest.raises(ValueError):
  218. net1(Ta, Tb, Tc, u_tensor_error, u_scalar)
  219. # net1(Ta, u_tensor, Tc, u_tensor_error, u_scalar)
  220. with pytest.raises(ValueError):
  221. net2(Ta, u_tensor_error, u_scalar)
  222. net3 = TensorAssignWithBoolTensorIndexError()
  223. with pytest.raises(AttributeError):
  224. net3(Ta, Tb, Tc, u_tensor)
  225. with pytest.raises(AttributeError):
  226. net3(Ta, Tb, Tc, u_scalar)
  227. net4 = TensorAssignWithBoolTensorIndex2Error()
  228. with pytest.raises(AttributeError):
  229. net4(Ta, u_tensor)
  230. with pytest.raises(AttributeError):
  231. net4(Ta, u_scalar)
  232. test_cases = [
  233. ('TensorAssignWithSlice', {
  234. 'block': TensorAssignWithSlice(),
  235. 'desc_inputs': [Ta, u_tensor],
  236. }),
  237. ('TensorAssignWithSlice2', {
  238. 'block': TensorAssignWithSlice2(),
  239. 'desc_inputs': [t_1d, u_tensor],
  240. }),
  241. ('TensorAssignWithBoolTensorIndex', {
  242. 'block': TensorAssignWithBoolTensorIndex(),
  243. 'desc_inputs': [Ta, Tb, Tc, u_tensor, u_scalar],
  244. }),
  245. ('TensorAssignWithBoolTensorIndex2', {
  246. 'block': TensorAssignWithBoolTensorIndex2(),
  247. 'desc_inputs': [Ta, u_tensor, u_scalar],
  248. }),
  249. ('SlicePositive', {
  250. 'block': NetWorkSlicePositive(),
  251. 'desc_inputs': [Tensor(np.ones([6, 8, 10], np.int32))],
  252. }),
  253. ('SliceReduceDimension', {
  254. 'block': NetWorkReduceDimension(),
  255. 'desc_inputs': [Tensor(np.ones([6, 8, 10], np.int32))],
  256. }),
  257. ('SliceNegative', {
  258. 'block': NetWorkStepNegative(),
  259. 'desc_inputs': [Tensor(np.ones([6, 8, 10], np.int32))],
  260. }),
  261. ('SliceReduceToScalar', {
  262. 'block': NetWorkReduceToScalar(),
  263. 'desc_inputs': [Tensor(np.ones([6, 8, 10], np.int32))],
  264. }),
  265. ('TensorSliceEllipsis', {
  266. 'block': NetWorkSliceEllipsis(),
  267. 'desc_inputs': [Tensor(np.ones([6, 7, 8, 9], np.int32))],
  268. }),
  269. ]
  270. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  271. def test_compile():
  272. context.set_context(mode=context.GRAPH_MODE)
  273. return test_cases
  274. def test_tensor_slice_reduce_out_of_bounds_neg():
  275. class NetWork(Cell):
  276. def __init__(self):
  277. super(NetWork, self).__init__()
  278. self.tensor_ret = Tensor(np.array(9, np.int32))
  279. def construct(self, tensor):
  280. ret = tensor[-7, 3, 4]
  281. return ret
  282. input_tensor = Tensor(np.ones([6, 8, 10], np.int32))
  283. net = NetWork()
  284. with pytest.raises(ValueError) as ex:
  285. net(input_tensor)
  286. assert "The `begin[0]` should be an int and must greater or equal to -6, but got -7" in str(ex.value)
  287. def test_tensor_slice_reduce_out_of_bounds_positive():
  288. class NetWork(Cell):
  289. def __init__(self):
  290. super(NetWork, self).__init__()
  291. self.tensor_ret = Tensor(np.array(9, np.int32))
  292. def construct(self, tensor):
  293. ret = tensor[6, 3, 4]
  294. return ret
  295. input_tensor = Tensor(np.ones([6, 8, 10], np.int32))
  296. net = NetWork()
  297. with pytest.raises(ValueError) as ex:
  298. net(input_tensor)
  299. assert "The `begin[0]` should be an int and must less than 6, but got 6" in str(ex.value)