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

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. def construct(self, tensor1, tensor2, tensor3, tensor4, tensor5, tensor6):
  31. tensor_tuple = (tensor1, tensor2, tensor3, tensor4, tensor5, tensor6)
  32. tensor_tuple_slice0 = tensor_tuple[:]
  33. tensor_tuple_slice1 = tensor_tuple[:3]
  34. tensor_tuple_slice2 = tensor_tuple[1:]
  35. tensor_tuple_slice3 = tensor_tuple[2:5:1]
  36. sum0 = self.addN(tensor_tuple_slice0)
  37. sum1 = self.addN(tensor_tuple_slice1)
  38. sum2 = self.addN(tensor_tuple_slice2)
  39. sum3 = self.addN(tensor_tuple_slice3)
  40. ret = sum0 + sum1 + sum2 + sum3
  41. return ret
  42. class NetWork_2(Cell):
  43. """ NetWork_2 definition """
  44. def __init__(self):
  45. super(NetWork_2, self).__init__()
  46. self.addN = P.AddN()
  47. def construct(self, tensor1, tensor2, tensor3, tensor4, tensor5, tensor6):
  48. tensor_tuple = (tensor1, tensor2, tensor3, tensor4, tensor5, tensor6)
  49. tensor_tuple_slice0 = tensor_tuple[::-1]
  50. tensor_tuple_slice1 = tensor_tuple[-1::-1]
  51. tensor_tuple_slice2 = tensor_tuple[:-4:-1]
  52. tensor_tuple_slice3 = tensor_tuple[-6:3]
  53. tensor_tuple_slice4 = tensor_tuple[-1:-6:-2]
  54. sum0 = self.addN(tensor_tuple_slice0)
  55. sum1 = self.addN(tensor_tuple_slice1)
  56. sum2 = self.addN(tensor_tuple_slice2)
  57. sum3 = self.addN(tensor_tuple_slice3)
  58. sum4 = self.addN(tensor_tuple_slice4)
  59. ret = sum0 + sum1 + sum2 + sum3 + sum4
  60. return ret
  61. class NetWork_3(Cell):
  62. """ NetWork_3 definition """
  63. def __init__(self):
  64. super(NetWork_3, self).__init__()
  65. self.addN = P.AddN()
  66. def construct(self, tensor_tuple, start, stop, step=1):
  67. tensor_tuple_slice0 = tensor_tuple[start:stop:step]
  68. res = self.addN(tensor_tuple_slice0)
  69. return res
  70. class NetWorkOutOfBounds(Cell):
  71. """ NetWork_3 definition """
  72. def __init__(self):
  73. super(NetWorkOutOfBounds, self).__init__()
  74. self.addN = P.AddN()
  75. def construct(self, tensor_tuple):
  76. return tensor_tuple[100]
  77. test_cases = [
  78. ('SlicePositive', {
  79. 'block': NetWork_1(),
  80. 'desc_inputs': [Tensor(np.ones([2, 3, 4], np.int32)),
  81. Tensor(np.zeros([2, 3, 4], np.int32)),
  82. Tensor(np.ones([2, 3, 4], np.int32)),
  83. Tensor(np.ones([2, 3, 4], np.int32)),
  84. Tensor(np.zeros([2, 3, 4], np.int32)),
  85. Tensor(np.ones([2, 3, 4], np.int32))],
  86. }),
  87. ('SliceNegative', {
  88. 'block': NetWork_2(),
  89. 'desc_inputs': [Tensor(np.ones([2, 3, 4], np.int32)),
  90. Tensor(np.zeros([2, 3, 4], np.int32)),
  91. Tensor(np.ones([2, 3, 4], np.int32)),
  92. Tensor(np.ones([2, 3, 4], np.int32)),
  93. Tensor(np.zeros([2, 3, 4], np.int32)),
  94. Tensor(np.ones([2, 3, 4], np.int32))],
  95. }),
  96. ]
  97. test_cases_for_verify_exception = [
  98. ('SliceStartCross', {
  99. 'block': (NetWork_3(), {'exception': TypeError}),
  100. 'desc_inputs': [Tensor(np.ones([2, 3, 4], np.int32)),
  101. Tensor(np.zeros([2, 3, 4], np.int32)),
  102. Tensor(np.ones([2, 3, 4], np.int32))],
  103. }),
  104. ('SliceStepZero', {
  105. 'block': (NetWork_3(), {'exception': TypeError}),
  106. 'desc_inputs': [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. ('SliceOutOfBounds', {
  111. 'block': (NetWorkOutOfBounds(), {'exception': IndexError}),
  112. 'desc_inputs': [(Tensor(np.ones([2, 3, 4], np.int32)),
  113. Tensor(np.zeros([2, 3, 4], np.int32)),
  114. Tensor(np.ones([2, 3, 4], np.int32)))],
  115. }),
  116. ]
  117. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  118. def test_compile():
  119. return test_cases
  120. @mindspore_test(pipeline_for_verify_exception_for_case_by_case_config)
  121. def test_check_exception():
  122. return test_cases_for_verify_exception