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_tuple_slice.py 6.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_tuple_slice """
  16. import numpy as np
  17. import mindspore.ops.operations as P
  18. from mindspore import Tensor
  19. from mindspore.nn import Cell
  20. from ....mindspore_test_framework.mindspore_test import mindspore_test
  21. from ....mindspore_test_framework.pipeline.forward.compile_forward \
  22. import pipeline_for_compile_forward_ge_graph_for_case_by_case_config
  23. from ....mindspore_test_framework.pipeline.forward.verify_exception \
  24. import pipeline_for_verify_exception_for_case_by_case_config
  25. class NetWork_1(Cell):
  26. """ NetWork_1 definition """
  27. def __init__(self):
  28. super(NetWork_1, self).__init__()
  29. self.addN = P.AddN()
  30. self.index_0 = Tensor(3)
  31. self.index_1 = Tensor([5])
  32. self.index_3 = Tensor([True])
  33. def construct(self, tensor_tuple):
  34. tensor_tuple_slice0 = tensor_tuple[:]
  35. tensor_tuple_slice1 = tensor_tuple[:self.index_0]
  36. tensor_tuple_slice2 = tensor_tuple[self.index_3:]
  37. tensor_tuple_slice3 = tensor_tuple[2:self.index_1:True]
  38. sum0 = self.addN(tensor_tuple_slice0)
  39. sum1 = self.addN(tensor_tuple_slice1)
  40. sum2 = self.addN(tensor_tuple_slice2)
  41. sum3 = self.addN(tensor_tuple_slice3)
  42. ret = sum0 + sum1 + sum2 + sum3
  43. return ret
  44. class NetWork_2(Cell):
  45. """ NetWork_2 definition """
  46. def __init__(self):
  47. super(NetWork_2, self).__init__()
  48. self.addN = P.AddN()
  49. self.step = Tensor([-1])
  50. self.index_0 = Tensor(-6)
  51. def construct(self, tensor_tuple):
  52. tensor_tuple_slice0 = tensor_tuple[::self.step]
  53. tensor_tuple_slice1 = tensor_tuple[-1::-1]
  54. tensor_tuple_slice2 = tensor_tuple[:-4:-1]
  55. tensor_tuple_slice3 = tensor_tuple[self.index_0:3]
  56. tensor_tuple_slice4 = tensor_tuple[-1:-6:-2]
  57. sum0 = self.addN(tensor_tuple_slice0)
  58. sum1 = self.addN(tensor_tuple_slice1)
  59. sum2 = self.addN(tensor_tuple_slice2)
  60. sum3 = self.addN(tensor_tuple_slice3)
  61. sum4 = self.addN(tensor_tuple_slice4)
  62. ret = sum0 + sum1 + sum2 + sum3 + sum4
  63. return ret
  64. class NetWorkSliceStepZero(Cell):
  65. """ NetWork_3 definition """
  66. def __init__(self):
  67. super(NetWorkSliceStepZero, self).__init__()
  68. def construct(self, tensor_tuple):
  69. tensor_tuple_slice = tensor_tuple[0:3:0]
  70. return tensor_tuple_slice
  71. class NetWorkOutOfBounds(Cell):
  72. """ NetWork_3 definition """
  73. def __init__(self):
  74. super(NetWorkOutOfBounds, self).__init__()
  75. def construct(self, tensor_tuple):
  76. return tensor_tuple[100]
  77. class NetWorkTensorSizeGreaterThanTwo(Cell):
  78. """ NetWork_3 definition """
  79. def __init__(self):
  80. super(NetWorkTensorSizeGreaterThanTwo, self).__init__()
  81. self.index_0 = Tensor([2, 3])
  82. def construct(self, tensor_tuple):
  83. return tensor_tuple[1:self.index_0]
  84. class NetWorkTensorDtypeFloat(Cell):
  85. """ NetWork_3 definition """
  86. def __init__(self):
  87. super(NetWorkTensorDtypeFloat, self).__init__()
  88. self.index_0 = Tensor([2.1])
  89. def construct(self, tensor_tuple):
  90. return tensor_tuple[1:self.index_0]
  91. test_cases = [
  92. ('SlicePositive', {
  93. 'block': NetWork_1(),
  94. 'desc_inputs': [(Tensor(np.ones([2, 3, 4], np.int32)),
  95. Tensor(np.zeros([2, 3, 4], np.int32)),
  96. Tensor(np.ones([2, 3, 4], np.int32)),
  97. Tensor(np.ones([2, 3, 4], np.int32)),
  98. Tensor(np.zeros([2, 3, 4], np.int32)),
  99. Tensor(np.ones([2, 3, 4], np.int32)))],
  100. }),
  101. ('SliceNegative', {
  102. 'block': NetWork_2(),
  103. 'desc_inputs': [(Tensor(np.ones([2, 3, 4], np.int32)),
  104. Tensor(np.zeros([2, 3, 4], np.int32)),
  105. Tensor(np.ones([2, 3, 4], np.int32)),
  106. Tensor(np.ones([2, 3, 4], np.int32)),
  107. Tensor(np.zeros([2, 3, 4], np.int32)),
  108. Tensor(np.ones([2, 3, 4], np.int32)))],
  109. }),
  110. ]
  111. test_cases_for_verify_exception = [
  112. ('SliceStepZero', {
  113. 'block': (NetWorkSliceStepZero(), {'exception': ValueError}),
  114. 'desc_inputs': [(Tensor(np.ones([2, 3, 4], np.int32)),
  115. Tensor(np.zeros([2, 3, 4], np.int32)),
  116. Tensor(np.ones([2, 3, 4], np.int32)))],
  117. }),
  118. ('SliceOutOfBounds', {
  119. 'block': (NetWorkOutOfBounds(), {'exception': IndexError}),
  120. 'desc_inputs': [(Tensor(np.ones([2, 3, 4], np.int32)),
  121. Tensor(np.zeros([2, 3, 4], np.int32)),
  122. Tensor(np.ones([2, 3, 4], np.int32)))],
  123. }),
  124. ('SliceTensorSizeGreaterThanTwo', {
  125. 'block': (NetWorkTensorSizeGreaterThanTwo(), {'exception': TypeError}),
  126. 'desc_inputs': [(Tensor(np.ones([2, 3, 4], np.int32)),
  127. Tensor(np.zeros([2, 3, 4], np.int32)),
  128. Tensor(np.ones([2, 3, 4], np.int32)))],
  129. }),
  130. ('SliceTensorDtypeFloat', {
  131. 'block': (NetWorkTensorDtypeFloat(), {'exception': TypeError}),
  132. 'desc_inputs': [(Tensor(np.ones([2, 3, 4], np.int32)),
  133. Tensor(np.zeros([2, 3, 4], np.int32)),
  134. Tensor(np.ones([2, 3, 4], np.int32)))],
  135. }),
  136. ]
  137. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  138. def test_compile():
  139. return test_cases
  140. @mindspore_test(pipeline_for_verify_exception_for_case_by_case_config)
  141. def test_check_exception():
  142. return test_cases_for_verify_exception