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_dynamic_shape_op.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. import numpy as np
  16. import pytest
  17. from mindspore import Tensor
  18. from mindspore.ops import operations as P
  19. from mindspore.ops.operations import _inner_ops as inner
  20. import mindspore.nn as nn
  21. import mindspore.context as context
  22. class DynamicShapeNet(nn.Cell):
  23. def __init__(self):
  24. super(DynamicShapeNet, self).__init__()
  25. self.convert_to_dynamic_shape_op = inner.GpuConvertToDynamicShape()
  26. self.dynamic_shape_op = P.DynamicShape()
  27. def construct(self, x):
  28. x_dynamic_shape = self.convert_to_dynamic_shape_op(x)
  29. return self.dynamic_shape_op(x_dynamic_shape)
  30. def dynamic_shape(np_type):
  31. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  32. dynamic_shape_net = DynamicShapeNet()
  33. shape = (1,)
  34. x = Tensor(np.zeros(shape).astype(np_type))
  35. ms_out = dynamic_shape_net(x).asnumpy()
  36. expected = np.array(shape)
  37. np.testing.assert_array_equal(ms_out, expected)
  38. shape = (7,)
  39. x = Tensor(np.zeros(shape).astype(np_type))
  40. ms_out = dynamic_shape_net(x).asnumpy()
  41. expected = np.array(shape)
  42. np.testing.assert_array_equal(ms_out, expected)
  43. shape = (1, 1)
  44. x = Tensor(np.zeros(shape).astype(np_type))
  45. ms_out = dynamic_shape_net(x).asnumpy()
  46. expected = np.array(shape)
  47. np.testing.assert_array_equal(ms_out, expected)
  48. shape = (1, 7)
  49. x = Tensor(np.zeros(shape).astype(np_type))
  50. ms_out = dynamic_shape_net(x).asnumpy()
  51. expected = np.array(shape)
  52. np.testing.assert_array_equal(ms_out, expected)
  53. shape = (3, 1)
  54. x = Tensor(np.zeros(shape).astype(np_type))
  55. ms_out = dynamic_shape_net(x).asnumpy()
  56. expected = np.array(shape)
  57. np.testing.assert_array_equal(ms_out, expected)
  58. shape = (2, 4)
  59. x = Tensor(np.zeros(shape).astype(np_type))
  60. ms_out = dynamic_shape_net(x).asnumpy()
  61. expected = np.array(shape)
  62. np.testing.assert_array_equal(ms_out, expected)
  63. shape = (1, 1, 1)
  64. x = Tensor(np.zeros(shape).astype(np_type))
  65. ms_out = dynamic_shape_net(x).asnumpy()
  66. expected = np.array(shape)
  67. np.testing.assert_array_equal(ms_out, expected)
  68. shape = (1, 5, 3)
  69. x = Tensor(np.zeros(shape).astype(np_type))
  70. ms_out = dynamic_shape_net(x).asnumpy()
  71. expected = np.array(shape)
  72. np.testing.assert_array_equal(ms_out, expected)
  73. shape = (2, 3, 1, 3, 1)
  74. x = Tensor(np.zeros(shape).astype(np_type))
  75. ms_out = dynamic_shape_net(x).asnumpy()
  76. expected = np.array(shape)
  77. np.testing.assert_array_equal(ms_out, expected)
  78. @pytest.mark.level0
  79. @pytest.mark.platform_x86_gpu_training
  80. @pytest.mark.env_onecard
  81. def test_dynamic_shape_int32():
  82. dynamic_shape(np.int32)
  83. @pytest.mark.level0
  84. @pytest.mark.platform_x86_gpu_training
  85. @pytest.mark.env_onecard
  86. def test_dynamic_shape_float16():
  87. dynamic_shape(np.float16)
  88. @pytest.mark.level0
  89. @pytest.mark.platform_x86_gpu_training
  90. @pytest.mark.env_onecard
  91. def test_dynamic_shape_float32():
  92. dynamic_shape(np.float32)
  93. @pytest.mark.level0
  94. @pytest.mark.platform_x86_gpu_training
  95. @pytest.mark.env_onecard
  96. def test_dynamic_shape_bool():
  97. dynamic_shape(np.bool)