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_tile_op.py 3.0 kB

4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright 2019-2021 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.common.api import ms_function
  19. from mindspore.common.initializer import initializer
  20. from mindspore.common.parameter import Parameter
  21. from mindspore.common.tensor import Tensor
  22. from mindspore.nn import Cell
  23. from mindspore.ops.operations import Tile
  24. class TileNet(Cell):
  25. def __init__(self, numpy_input):
  26. super(TileNet, self).__init__()
  27. self.Tile = Tile()
  28. self.input_parameter = Parameter(initializer(Tensor(numpy_input), numpy_input.shape), name='x')
  29. @ms_function
  30. def construct(self, mul):
  31. return self.Tile(self.input_parameter, mul)
  32. def ms_tile(nptype):
  33. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  34. input_0 = np.arange(2).reshape((2, 1, 1)).astype(nptype)
  35. mul_0 = (8, 1, 1)
  36. input_1 = np.arange(32).reshape((2, 4, 4)).astype(nptype)
  37. mul_1 = (2, 2, 2)
  38. input_2 = np.arange(1).reshape((1, 1, 1)).astype(nptype)
  39. mul_2 = (1, 1, 1)
  40. tile_net = TileNet(input_0)
  41. np_expected = np.tile(input_0, mul_0)
  42. ms_output = tile_net(mul_0).asnumpy()
  43. np.testing.assert_array_equal(ms_output, np_expected)
  44. tile_net = TileNet(input_1)
  45. np_expected = np.tile(input_1, mul_1)
  46. ms_output = tile_net(mul_1).asnumpy()
  47. np.testing.assert_array_equal(ms_output, np_expected)
  48. tile_net = TileNet(input_2)
  49. np_expected = np.tile(input_2, mul_2)
  50. ms_output = tile_net(mul_2).asnumpy()
  51. np.testing.assert_array_equal(ms_output, np_expected)
  52. @pytest.mark.level0
  53. @pytest.mark.platform_x86_gpu_training
  54. @pytest.mark.env_onecard
  55. def test_tile_float16():
  56. ms_tile(np.float16)
  57. @pytest.mark.level0
  58. @pytest.mark.platform_x86_gpu_training
  59. @pytest.mark.env_onecard
  60. def test_tile_float32():
  61. ms_tile(np.float32)
  62. @pytest.mark.level0
  63. @pytest.mark.platform_x86_gpu_training
  64. @pytest.mark.env_onecard
  65. def test_tile_float64():
  66. ms_tile(np.float64)
  67. @pytest.mark.level0
  68. @pytest.mark.platform_x86_gpu_training
  69. @pytest.mark.env_onecard
  70. def test_tile_int16():
  71. ms_tile(np.int16)
  72. @pytest.mark.level0
  73. @pytest.mark.platform_x86_gpu_training
  74. @pytest.mark.env_onecard
  75. def test_tile_int32():
  76. ms_tile(np.int32)
  77. @pytest.mark.level0
  78. @pytest.mark.platform_x86_gpu_training
  79. @pytest.mark.env_onecard
  80. def test_tile_int64():
  81. ms_tile(np.int64)