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_getitem.py 7.0 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. pipeline_for_compile_forward_ge_graph_for_case_by_case_config_exception
  26. class NetWorkFancyIndex(Cell):
  27. def __init__(self, index):
  28. super(NetWorkFancyIndex, self).__init__()
  29. self.index = index
  30. def construct(self, tensor):
  31. return tensor[self.index]
  32. class TensorItemByNone(Cell):
  33. def construct(self, tensor):
  34. ret = tensor.item()
  35. return ret
  36. class TensorItemByItem(Cell):
  37. def construct(self, tensor, index):
  38. ret = tensor.item(index)
  39. return ret
  40. def test_tensor_fancy_index_integer_list():
  41. context.set_context(mode=context.GRAPH_MODE)
  42. index = [0, 2, 1]
  43. net = NetWorkFancyIndex(index)
  44. input_np = np.arange(60).reshape(3, 4, 5)
  45. input_me = Tensor(input_np, dtype=mstype.float32)
  46. net(input_me)
  47. def test_tensor_fancy_index_boolean_list():
  48. context.set_context(mode=context.GRAPH_MODE)
  49. index = [True, True, False]
  50. net = NetWorkFancyIndex(index)
  51. input_np = np.arange(60).reshape(3, 4, 5)
  52. input_me = Tensor(input_np, dtype=mstype.float32)
  53. net(input_me)
  54. def test_tensor_fancy_index_integer_boolean_list_graph():
  55. context.set_context(mode=context.GRAPH_MODE)
  56. index = [1, 2, True, False]
  57. net = NetWorkFancyIndex(index)
  58. input_np = np.arange(60).reshape(3, 4, 5)
  59. input_me = Tensor(input_np, dtype=mstype.float32)
  60. net(input_me)
  61. def test_tensor_fancy_index_integer_list_mixed():
  62. context.set_context(mode=context.GRAPH_MODE)
  63. index = (1, [2, 1, 3], slice(1, 3, 1), ..., 4)
  64. net = NetWorkFancyIndex(index)
  65. input_np = np.arange(3*4*5*6*7*8).reshape(3, 4, 5, 6, 7, 8)
  66. input_me = Tensor(input_np, dtype=mstype.float32)
  67. net(input_me)
  68. def test_tensor_fancy_index_integer_tuple_mixed():
  69. context.set_context(mode=context.GRAPH_MODE)
  70. index = (1, (2, 1, 3), slice(1, 3, 1), ..., 4)
  71. net = NetWorkFancyIndex(index)
  72. input_np = np.arange(3*4*5*6*7*8).reshape(3, 4, 5, 6, 7, 8)
  73. input_me = Tensor(input_np, dtype=mstype.float32)
  74. net(input_me)
  75. def test_tensor_fancy_index_integer_list_tuple_mixed():
  76. context.set_context(mode=context.GRAPH_MODE)
  77. index = (1, [2, 1, 3], (3, 2, 1), slice(1, 3, 1), ..., 4)
  78. net = NetWorkFancyIndex(index)
  79. input_np = np.arange(3*4*5*6*7*8).reshape(3, 4, 5, 6, 7, 8)
  80. input_me = Tensor(input_np, dtype=mstype.float32)
  81. net(input_me)
  82. def test_tensor_fancy_index_integer_list_tuple_bool_mixed():
  83. context.set_context(mode=context.GRAPH_MODE)
  84. index = (1, [2, 1, 3], True, (3, 2, 1), slice(1, 3, 1), ..., True, 4)
  85. net = NetWorkFancyIndex(index)
  86. input_np = np.arange(3*4*5*6*7*8).reshape(3, 4, 5, 6, 7, 8)
  87. input_me = Tensor(input_np, dtype=mstype.float32)
  88. net(input_me)
  89. def test_tensor_fancy_index_integer_list_tuple_bool_mixed_error():
  90. context.set_context(mode=context.GRAPH_MODE)
  91. index = (1, [2, 1, 3], True, (3, 2, 1), slice(1, 3, 1), ..., False, 4)
  92. net = NetWorkFancyIndex(index)
  93. input_np = np.arange(3*4*5*6*7*8).reshape(3, 4, 5, 6, 7, 8)
  94. input_me = Tensor(input_np, dtype=mstype.float32)
  95. with pytest.raises(IndexError):
  96. net(input_me)
  97. input_1d_np = np.ndarray([1]).astype(np.float32)
  98. input_1d_ms = Tensor(input_1d_np, mstype.float32)
  99. input_3d_np = np.random.randint(3, size=(3, 4, 5)).astype(np.int32)
  100. input_3d_ms = Tensor(input_3d_np, mstype.float32)
  101. index_np_1, index_np_2, index_np_3, index_np_4 = 0, 1.0, 30, 60
  102. tuple_index_np_1, tuple_index_np_2, tuple_index_np_3, tuple_index_np_4, tuple_index_np_5 = \
  103. (0,), (1, 2), (1, 2, 3), (3, 4, 4), (1, 2, 3, 4)
  104. test_cases = [
  105. ('TensorItemByNone', {'block': TensorItemByNone(), 'desc_inputs': [input_1d_ms],}),
  106. ('1dTensorItemByInt', {'block': TensorItemByItem(), 'desc_inputs': [input_1d_ms, index_np_1],}),
  107. ('3dTensorItemByInt', {'block': TensorItemByItem(), 'desc_inputs': [input_3d_ms, index_np_1],}),
  108. ('3dTensorItemByInt2', {'block': TensorItemByItem(), 'desc_inputs': [input_3d_ms, index_np_3],}),
  109. ('1dTensorItemByTuple', {'block': TensorItemByItem(), 'desc_inputs': [input_1d_ms, tuple_index_np_1],}),
  110. ('3dTensorItemByTuple', {'block': TensorItemByItem(), 'desc_inputs': [input_3d_ms, tuple_index_np_3],}),
  111. ]
  112. test_error_cases = [
  113. ('TensorItemByNoneForMulDimsTensor', {
  114. 'block': (TensorItemByNone(), {'exception': ValueError}),
  115. 'desc_inputs': [input_3d_ms]
  116. }),
  117. ('TensorItemByFloatError', {
  118. 'block': (TensorItemByItem(), {'exception': TypeError}),
  119. 'desc_inputs': [input_1d_ms, index_np_2]
  120. }),
  121. ('TensorItemByFloatError2', {
  122. 'block': (TensorItemByItem(), {'exception': TypeError}),
  123. 'desc_inputs': [input_3d_ms, index_np_2]
  124. }),
  125. ('TensorItemByIntOverBoundary', {
  126. 'block': (TensorItemByItem(), {'exception': IndexError}),
  127. 'desc_inputs': [input_1d_ms, index_np_3]
  128. }),
  129. ('TensorItemByIntOverBoundary2', {
  130. 'block': (TensorItemByItem(), {'exception': IndexError}),
  131. 'desc_inputs': [input_3d_ms, index_np_4]
  132. }),
  133. ('1dTensorItemBy2dTuple', {
  134. 'block': (TensorItemByItem(), {'exception': ValueError}),
  135. 'desc_inputs': [input_1d_ms, tuple_index_np_2]
  136. }),
  137. ('3dTensorItemBy2dTuple', {
  138. 'block': (TensorItemByItem(), {'exception': ValueError}),
  139. 'desc_inputs': [input_3d_ms, tuple_index_np_2]
  140. }),
  141. ('3dTensorItemBy3dTupleOutOfBoundary', {
  142. 'block': (TensorItemByItem(), {'exception': IndexError}),
  143. 'desc_inputs': [input_3d_ms, tuple_index_np_4]
  144. }),
  145. ('3dTensorItemBy4dTuple', {
  146. 'block': (TensorItemByItem(), {'exception': ValueError}),
  147. 'desc_inputs': [input_3d_ms, tuple_index_np_5]
  148. })
  149. ]
  150. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  151. def test_exec():
  152. context.set_context(mode=context.GRAPH_MODE)
  153. return test_cases
  154. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config_exception)
  155. def test_check_exception():
  156. return test_error_cases