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 2.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.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. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  25. input_x0 = np.arange(2).reshape((2, 1, 1)).astype(np.float32)
  26. mul0 = (8, 1, 1)
  27. input_x1 = np.arange(32).reshape((2, 4, 4)).astype(np.float32)
  28. mul1 = (2, 2, 2)
  29. input_x2 = np.arange(1).reshape((1, 1, 1)).astype(np.float32)
  30. mul2 = (1, 1, 1)
  31. class Net(Cell):
  32. def __init__(self):
  33. super(Net, self).__init__()
  34. self.Tile = Tile()
  35. self.input_x0 = Parameter(initializer(Tensor(input_x0), input_x0.shape), name='x0')
  36. self.mul0 = mul0
  37. self.input_x1 = Parameter(initializer(Tensor(input_x1), input_x1.shape), name='x1')
  38. self.mul1 = mul1
  39. self.input_x2 = Parameter(initializer(Tensor(input_x2), input_x2.shape), name='x2')
  40. self.mul2 = mul2
  41. @ms_function
  42. def construct(self):
  43. output = (self.Tile(self.input_x0, self.mul0),
  44. self.Tile(self.input_x1, self.mul1),
  45. self.Tile(self.input_x2, self.mul2))
  46. return output
  47. @pytest.mark.level0
  48. @pytest.mark.platform_x86_gpu_training
  49. @pytest.mark.env_onecard
  50. def test_tile():
  51. net = Net()
  52. output = net()
  53. expect0 = np.tile(input_x0, mul0)
  54. diff0 = output[0].asnumpy() - expect0
  55. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  56. assert np.all(diff0 < error0)
  57. assert output[0].shape == expect0.shape
  58. expect1 = np.tile(input_x1, mul1)
  59. diff1 = output[1].asnumpy() - expect1
  60. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  61. assert np.all(diff1 < error1)
  62. assert output[1].shape == expect1.shape
  63. expect2 = np.tile(input_x2, mul2)
  64. diff2 = output[2].asnumpy() - expect2
  65. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  66. assert np.all(diff2 < error2)
  67. assert output[2].shape == expect2.shape