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_reshape_op.py 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # Copyright 2019 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. import numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. from mindspore import Tensor
  19. from mindspore.ops import operations as P
  20. def reshape(nptype):
  21. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  22. reshape_op = P.Reshape()
  23. data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]).astype(nptype)
  24. input_tensor = Tensor(np.array(data))
  25. new_shape = (2, 6)
  26. output_tensor = reshape_op(input_tensor, new_shape)
  27. assert new_shape == output_tensor.shape
  28. np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
  29. new_shape = (6, 2)
  30. output_tensor = reshape_op(input_tensor, new_shape)
  31. assert new_shape == output_tensor.shape
  32. np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
  33. new_shape = (3, 4)
  34. output_tensor = reshape_op(input_tensor, new_shape)
  35. assert new_shape == output_tensor.shape
  36. np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
  37. new_shape = (4, 3)
  38. output_tensor = reshape_op(input_tensor, new_shape)
  39. assert new_shape == output_tensor.shape
  40. np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
  41. new_shape = (1, 12)
  42. output_tensor = reshape_op(input_tensor, new_shape)
  43. assert new_shape == output_tensor.shape
  44. np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
  45. new_shape = (12, 1)
  46. output_tensor = reshape_op(input_tensor, new_shape)
  47. assert new_shape == output_tensor.shape
  48. np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
  49. def reshape_bool():
  50. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  51. reshape_op = P.Reshape()
  52. data = np.array([True, True, False, True, False, False, True, False, False, False, False, False])
  53. input_tensor = Tensor(np.array(data))
  54. new_shape = (2, 6)
  55. output_tensor = reshape_op(input_tensor, new_shape)
  56. assert new_shape == output_tensor.shape
  57. np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
  58. new_shape = (6, 2)
  59. output_tensor = reshape_op(input_tensor, new_shape)
  60. assert new_shape == output_tensor.shape
  61. np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
  62. new_shape = (3, 4)
  63. output_tensor = reshape_op(input_tensor, new_shape)
  64. assert new_shape == output_tensor.shape
  65. np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
  66. new_shape = (4, 3)
  67. output_tensor = reshape_op(input_tensor, new_shape)
  68. assert new_shape == output_tensor.shape
  69. np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
  70. new_shape = (1, 12)
  71. output_tensor = reshape_op(input_tensor, new_shape)
  72. assert new_shape == output_tensor.shape
  73. np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
  74. new_shape = (12, 1)
  75. output_tensor = reshape_op(input_tensor, new_shape)
  76. assert new_shape == output_tensor.shape
  77. np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
  78. @pytest.mark.level0
  79. @pytest.mark.platform_x86_gpu_training
  80. @pytest.mark.env_onecard
  81. def test_reshape_float():
  82. reshape(np.float32)
  83. @pytest.mark.level0
  84. @pytest.mark.platform_x86_gpu_training
  85. @pytest.mark.env_onecard
  86. def test_reshape_float16():
  87. reshape(np.float16)
  88. @pytest.mark.level0
  89. @pytest.mark.platform_x86_gpu_training
  90. @pytest.mark.env_onecard
  91. def test_reshape_int32():
  92. reshape(np.int32)
  93. @pytest.mark.level0
  94. @pytest.mark.platform_x86_gpu_training
  95. @pytest.mark.env_onecard
  96. def test_reshape_uint8():
  97. reshape(np.uint8)
  98. @pytest.mark.level0
  99. @pytest.mark.platform_x86_gpu_training
  100. @pytest.mark.env_onecard
  101. def test_reshape_bool():
  102. reshape_bool()