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.2 kB

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