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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 NetWorkReduceDimension(Cell):
  39. def __init__(self):
  40. super(NetWorkReduceDimension, self).__init__()
  41. self.tensor_ret0 = Tensor(np.ones([2, 4, 1], np.int32))
  42. self.tensor_ret1 = Tensor(np.ones([3, 4], np.int32))
  43. self.tensor_ret2 = Tensor(np.ones([6, 8], np.int32))
  44. self.tensor_ret3 = Tensor(np.array(8, np.int32))
  45. self.tensor_ret4 = Tensor(np.ones([8, 10], np.int32))
  46. def construct(self, tensor):
  47. ret0 = tensor[0:6:3, 1:5:1, 3:5:2] + self.tensor_ret0
  48. ret1 = tensor[::2, 1, ::3] + self.tensor_ret1
  49. ret2 = tensor[::, ::, 0] + self.tensor_ret2
  50. ret3 = tensor[3, 2, 5] + self.tensor_ret3
  51. ret4 = tensor[1] + self.tensor_ret4
  52. return ret0, ret1, ret2, ret3, ret4
  53. class NetWorkStepNegative(Cell):
  54. def __init__(self):
  55. super(NetWorkStepNegative, self).__init__()
  56. self.tensor_ret = Tensor(np.ones([6, 5, 10], np.int32))
  57. def construct(self, tensor):
  58. ret = tensor[::1, -5::, ::-1] + self.tensor_ret
  59. return ret
  60. class NetWorkReduceToScalar(Cell):
  61. def __init__(self):
  62. super(NetWorkReduceToScalar, self).__init__()
  63. self.tensor_ret = Tensor(np.array(9, np.int32))
  64. def construct(self, tensor):
  65. ret = tensor[2, 3, 4] + self.tensor_ret
  66. return ret
  67. class TensorAssignWithBoolTensorIndex(Cell):
  68. def __init__(self):
  69. super(TensorAssignWithBoolTensorIndex, self).__init__()
  70. self.t = Tensor(np.arange(6).reshape([2,3]), dtype = mstype.float64)
  71. def construct(self, a, b, c, u_tensor, _scalar):
  72. a[c] = u_scalar
  73. a[b] = u_tensor
  74. z = a + self.t
  75. return z
  76. class TensorAssignWithBoolTensorIndexError(Cell):
  77. def __init__(self):
  78. super(TensorAssignWithBoolTensorIndexError, self).__init__()
  79. def construct(self, a, b, c, u_tensor):
  80. a[b][c] = u_tensor
  81. return a
  82. class TensorAssignWithBoolTensorIndex2(Cell):
  83. def __init__(self):
  84. super(TensorAssignWithBoolTensorIndex2, self).__init__()
  85. self.t = Tensor(np.arange(6).reshape([2,3]), dtype = mstype.float64)
  86. def construct(self, a, u_tensor, _scalar):
  87. a[a>8] = u_tensor
  88. a[a>=6] = u_scalar
  89. a[a<3] = u_scalar
  90. a[a<=5] = u_tensor
  91. a[a==5] = u_scalar
  92. z = a + self.t
  93. return z
  94. class TensorAssignWithBoolTensorIndex2Error(Cell):
  95. def __init__(self):
  96. super(TensorAssignWithBoolTensorIndex2Error, self).__init__()
  97. def construct(self, a, u_tensor):
  98. a[a>8][a>5] = u_tensor
  99. return a
  100. a = np.random.uniform(1,10,[2,3])
  101. b = a > 5
  102. c = a < 3
  103. Ta = Tensor(a)
  104. Tb = Tensor(b)
  105. Tc = Tensor(c)
  106. Td = Tensor([True, True])
  107. u_tensor = Tensor([1])
  108. u_tensor_error = Tensor([1, 2])
  109. u_scalar = 5
  110. def test_tensor_assign_bool_index():
  111. net1 = TensorAssignWithBoolTensorIndex()
  112. net2 = TensorAssignWithBoolTensorIndex2()
  113. net1(Ta, Tb, Tc, u_tensor, u_scalar)
  114. with pytest.raises(ValueError):
  115. net1(Ta, Td, Tc, u_tensor, u_scalar)
  116. with pytest.raises(ValueError):
  117. net1(Ta, u_tensor, Tc, u_tensor, u_scalar)
  118. with pytest.raises(ValueError):
  119. net1(Ta, Tb, Td, u_tensor, u_scalar)
  120. with pytest.raises(ValueError):
  121. net1(Ta, Tb, Ta, u_tensor, u_scalar)
  122. with pytest.raises(ValueError):
  123. net1(Ta, Tb, Tc, u_tensor_error, u_scalar)
  124. #net1(Ta, u_tensor, Tc, u_tensor_error, u_scalar)
  125. with pytest.raises(ValueError):
  126. net2(Ta, u_tensor_error, u_scalar)
  127. net3 = TensorAssignWithBoolTensorIndexError()
  128. with pytest.raises(AttributeError):
  129. net3(Ta, Tb, Tc, u_tensor)
  130. with pytest.raises(AttributeError):
  131. net3(Ta, Tb, Tc, u_scalar)
  132. net4 = TensorAssignWithBoolTensorIndex2Error()
  133. with pytest.raises(AttributeError):
  134. net4(Ta, u_tensor)
  135. with pytest.raises(AttributeError):
  136. net4(Ta, u_scalar)
  137. test_cases = [
  138. ('TensorAssignWithBoolTensorIndex', {
  139. 'block': TensorAssignWithBoolTensorIndex(),
  140. 'desc_inputs': [Ta, Tb, Tc, u_tensor, u_scalar],
  141. }),
  142. ('TensorAssignWithBoolTensorIndex2', {
  143. 'block': TensorAssignWithBoolTensorIndex2(),
  144. 'desc_inputs': [Ta, u_tensor, u_scalar],
  145. }),
  146. ('SlicePositive', {
  147. 'block': NetWorkSlicePositive(),
  148. 'desc_inputs': [Tensor(np.ones([6, 8, 10], np.int32))],
  149. }),
  150. ('SliceReduceDimension', {
  151. 'block': NetWorkReduceDimension(),
  152. 'desc_inputs': [Tensor(np.ones([6, 8, 10], np.int32))],
  153. }),
  154. ('SliceNegative', {
  155. 'block': NetWorkStepNegative(),
  156. 'desc_inputs': [Tensor(np.ones([6, 8, 10], np.int32))],
  157. }),
  158. ('SliceReduceToScalar', {
  159. 'block': NetWorkReduceToScalar(),
  160. 'desc_inputs': [Tensor(np.ones([6, 8, 10], np.int32))],
  161. }),
  162. ]
  163. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  164. def test_compile():
  165. context.set_context(mode=context.GRAPH_MODE)
  166. return test_cases
  167. def test_tensor_slice_reduce_out_of_bounds_neg():
  168. class NetWork(Cell):
  169. def __init__(self):
  170. super(NetWork, self).__init__()
  171. self.tensor_ret = Tensor(np.array(9, np.int32))
  172. def construct(self, tensor):
  173. ret = tensor[-7, 3, 4]
  174. return ret
  175. input_tensor = Tensor(np.ones([6, 8, 10], np.int32))
  176. net = NetWork()
  177. with pytest.raises(ValueError) as ex:
  178. net(input_tensor)
  179. assert "The `begin[0]` should be an int and must greater or equal to -6, but got -7" in str(ex.value)
  180. def test_tensor_slice_reduce_out_of_bounds_positive():
  181. class NetWork(Cell):
  182. def __init__(self):
  183. super(NetWork, self).__init__()
  184. self.tensor_ret = Tensor(np.array(9, np.int32))
  185. def construct(self, tensor):
  186. ret = tensor[6, 3, 4]
  187. return ret
  188. input_tensor = Tensor(np.ones([6, 8, 10], np.int32))
  189. net = NetWork()
  190. with pytest.raises(ValueError) as ex:
  191. net(input_tensor)
  192. assert "The `begin[0]` should be an int and must less than 6, but got 6" in str(ex.value)