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

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 pytest
  18. from mindspore import Tensor
  19. from mindspore.nn import Cell
  20. import mindspore.ops.operations as P
  21. from ....mindspore_test_framework.mindspore_test import mindspore_test
  22. from ....mindspore_test_framework.pipeline.forward.compile_forward \
  23. import pipeline_for_compile_forward_ge_graph_for_case_by_case_config
  24. from ....mindspore_test_framework.pipeline.forward.verify_exception \
  25. import pipeline_for_verify_exception_for_case_by_case_config
  26. class NetWork_1(Cell):
  27. """ NetWork_1 definition """
  28. def __init__(self):
  29. super(NetWork_1, self).__init__()
  30. self.addN = P.AddN()
  31. def construct(self, tensor_tuple):
  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, tensor_tuple):
  48. tensor_tuple_slice0 = tensor_tuple[::-1]
  49. tensor_tuple_slice1 = tensor_tuple[-1::-1]
  50. tensor_tuple_slice2 = tensor_tuple[:-4:-1]
  51. tensor_tuple_slice3 = tensor_tuple[-6:3]
  52. tensor_tuple_slice4 = tensor_tuple[-1:-6:-2]
  53. sum0 = self.addN(tensor_tuple_slice0)
  54. sum1 = self.addN(tensor_tuple_slice1)
  55. sum2 = self.addN(tensor_tuple_slice2)
  56. sum3 = self.addN(tensor_tuple_slice3)
  57. sum4 = self.addN(tensor_tuple_slice4)
  58. ret = sum0 + sum1 + sum2 + sum3 + sum4
  59. return ret
  60. class NetWork_3(Cell):
  61. """ NetWork_3 definition """
  62. def __init__(self):
  63. super(NetWork_3, self).__init__()
  64. self.addN = P.AddN()
  65. def construct(self, tensor_tuple, start, stop, step=1):
  66. tensor_tuple_slice0 = tensor_tuple[start:stop:step]
  67. res = self.addN(tensor_tuple_slice0)
  68. return res
  69. test_cases = [
  70. ('SlicePositive', {
  71. 'block': NetWork_1(),
  72. 'desc_inputs': [(Tensor(np.ones([2, 3, 4], np.int32)),
  73. Tensor(np.zeros([2, 3, 4], np.int32)),
  74. Tensor(np.ones([2, 3, 4], np.int32)),
  75. Tensor(np.ones([2, 3, 4], np.int32)),
  76. Tensor(np.zeros([2, 3, 4], np.int32)),
  77. Tensor(np.ones([2, 3, 4], np.int32)))],
  78. }),
  79. ('SliceNegative', {
  80. 'block': NetWork_2(),
  81. 'desc_inputs': [(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. Tensor(np.ones([2, 3, 4], np.int32)),
  85. Tensor(np.zeros([2, 3, 4], np.int32)),
  86. Tensor(np.ones([2, 3, 4], np.int32)))],
  87. }),
  88. ]
  89. test_cases_for_verify_exception = [
  90. ('SliceStartCross', {
  91. 'block': (NetWork_3(), {'exception': RuntimeError}),
  92. 'desc_inputs': [*(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. ('SliceStepZero', {
  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. ]
  103. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  104. def test_compile():
  105. return test_cases
  106. @mindspore_test(pipeline_for_verify_exception_for_case_by_case_config)
  107. def test_check_exception():
  108. return test_cases_for_verify_exception