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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. test_cases = [
  69. ('SlicePositive', {
  70. 'block': NetWork_1(),
  71. 'desc_inputs': [(Tensor(np.ones([2, 3, 4], np.int32)),
  72. Tensor(np.zeros([2, 3, 4], np.int32)),
  73. Tensor(np.ones([2, 3, 4], np.int32)),
  74. Tensor(np.ones([2, 3, 4], np.int32)),
  75. Tensor(np.zeros([2, 3, 4], np.int32)),
  76. Tensor(np.ones([2, 3, 4], np.int32)))],
  77. }),
  78. ('SliceNegative', {
  79. 'block': NetWork_2(),
  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. ]
  88. test_cases_for_verify_exception = [
  89. ('SliceStartCross', {
  90. 'block': (NetWork_3(), {'exception': RuntimeError}),
  91. 'desc_inputs': [*(Tensor(np.ones([2, 3, 4], np.int32)),
  92. Tensor(np.zeros([2, 3, 4], np.int32)),
  93. Tensor(np.ones([2, 3, 4], np.int32)))],
  94. }),
  95. ('SliceStepZero', {
  96. 'block': (NetWork_3(), {'exception': RuntimeError}),
  97. 'desc_inputs': [*(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. ]
  102. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  103. def test_compile():
  104. return test_cases
  105. @mindspore_test(pipeline_for_verify_exception_for_case_by_case_config)
  106. def test_check_exception():
  107. return test_cases_for_verify_exception