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_tensor_slice.py 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_tensor_slice """
  16. import numpy as np
  17. import pytest
  18. from mindspore import Tensor
  19. from mindspore import context
  20. from mindspore.nn import Cell
  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. class NetWorkSlicePositive(Cell):
  25. def __init__(self):
  26. super(NetWorkSlicePositive, self).__init__()
  27. self.tensor_ret0 = Tensor(np.ones([1, 2, 2], np.int32))
  28. self.tensor_ret1 = Tensor(np.ones([4, 7, 4], np.int32))
  29. self.tensor_ret2 = Tensor(np.ones([6, 8, 10], np.int32))
  30. self.tensor_ret3 = Tensor(np.ones([3, 8, 10], np.int32))
  31. def construct(self, tensor):
  32. ret0 = tensor[3:4:3, 1:5:2, 3:6:2] + self.tensor_ret0
  33. ret1 = tensor[-6:4:1, 7:-8:-1, ::3] + self.tensor_ret1
  34. ret2 = tensor[::, ::, ::] + self.tensor_ret2
  35. ret3 = tensor[::2] + self.tensor_ret3
  36. return ret0, ret1, ret2, ret3
  37. class NetWorkReduceDimension(Cell):
  38. def __init__(self):
  39. super(NetWorkReduceDimension, self).__init__()
  40. self.tensor_ret0 = Tensor(np.ones([2, 4, 1], np.int32))
  41. self.tensor_ret1 = Tensor(np.ones([3, 4], np.int32))
  42. self.tensor_ret2 = Tensor(np.ones([6, 8], np.int32))
  43. self.tensor_ret3 = Tensor(np.array(8, np.int32))
  44. self.tensor_ret4 = Tensor(np.ones([8, 10], np.int32))
  45. def construct(self, tensor):
  46. ret0 = tensor[0:6:3, 1:5:1, 3:5:2] + self.tensor_ret0
  47. ret1 = tensor[::2, 1, ::3] + self.tensor_ret1
  48. ret2 = tensor[::, ::, 0] + self.tensor_ret2
  49. ret3 = tensor[3, 2, 5] + self.tensor_ret3
  50. ret4 = tensor[1] + self.tensor_ret4
  51. return ret0, ret1, ret2, ret3, ret4
  52. class NetWorkStepNegative(Cell):
  53. def __init__(self):
  54. super(NetWorkStepNegative, self).__init__()
  55. self.tensor_ret = Tensor(np.ones([6, 5, 10], np.int32))
  56. def construct(self, tensor):
  57. ret = tensor[::1, -5::, ::-1] + self.tensor_ret
  58. return ret
  59. class NetWorkReduceToScalar(Cell):
  60. def __init__(self):
  61. super(NetWorkReduceToScalar, self).__init__()
  62. self.tensor_ret = Tensor(np.array(9, np.int32))
  63. def construct(self, tensor):
  64. ret = tensor[2, 3, 4] + self.tensor_ret
  65. return ret
  66. test_cases = [
  67. ('SlicePositive', {
  68. 'block': NetWorkSlicePositive(),
  69. 'desc_inputs': [Tensor(np.ones([6, 8, 10], np.int32))],
  70. }),
  71. ('SliceReduceDimension', {
  72. 'block': NetWorkReduceDimension(),
  73. 'desc_inputs': [Tensor(np.ones([6, 8, 10], np.int32))],
  74. }),
  75. ('SliceNegative', {
  76. 'block': NetWorkStepNegative(),
  77. 'desc_inputs': [Tensor(np.ones([6, 8, 10], np.int32))],
  78. }),
  79. ('SliceReduceToScalar', {
  80. 'block': NetWorkReduceToScalar(),
  81. 'desc_inputs': [Tensor(np.ones([6, 8, 10], np.int32))],
  82. }),
  83. ]
  84. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  85. def test_compile():
  86. context.set_context(mode=context.GRAPH_MODE)
  87. return test_cases
  88. def test_tensor_slice_reduce_out_of_bounds_neg():
  89. class NetWork(Cell):
  90. def __init__(self):
  91. super(NetWork, self).__init__()
  92. self.tensor_ret = Tensor(np.array(9, np.int32))
  93. def construct(self, tensor):
  94. ret = tensor[-7, 3, 4]
  95. return ret
  96. input_tensor = Tensor(np.ones([6, 8, 10], np.int32))
  97. net = NetWork()
  98. with pytest.raises(ValueError) as ex:
  99. net(input_tensor)
  100. assert "The `begin[0]` should be an int and must greater or equal to -6, but got -7" in str(ex.value)
  101. def test_tensor_slice_reduce_out_of_bounds_positive():
  102. class NetWork(Cell):
  103. def __init__(self):
  104. super(NetWork, self).__init__()
  105. self.tensor_ret = Tensor(np.array(9, np.int32))
  106. def construct(self, tensor):
  107. ret = tensor[6, 3, 4]
  108. return ret
  109. input_tensor = Tensor(np.ones([6, 8, 10], np.int32))
  110. net = NetWork()
  111. with pytest.raises(ValueError) as ex:
  112. net(input_tensor)
  113. assert "The `begin[0]` should be an int and must less than 6, but got 6" in str(ex.value)