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

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. input_32_x0 = np.arange(2).reshape((2, 1, 1)).astype(np.int32)
  32. mul_32_0 = (8, 1, 1)
  33. input_32_x1 = np.arange(32).reshape((2, 4, 4)).astype(np.int32)
  34. mul_32_1 = (2, 2, 2)
  35. input_32_x2 = np.arange(1).reshape((1, 1, 1)).astype(np.int32)
  36. mul_32_2 = (1, 1, 1)
  37. input_16_x0 = np.arange(2).reshape((2, 1, 1)).astype(np.int16)
  38. mul_16_0 = (8, 1, 1)
  39. input_16_x1 = np.arange(32).reshape((2, 4, 4)).astype(np.int16)
  40. mul_16_1 = (2, 2, 2)
  41. input_16_x2 = np.arange(1).reshape((1, 1, 1)).astype(np.int16)
  42. mul_16_2 = (1, 1, 1)
  43. input_8_x0 = np.arange(2).reshape((2, 1, 1)).astype(np.uint8)
  44. mul_8_0 = (8, 1, 1)
  45. input_8_x1 = np.arange(32).reshape((2, 4, 4)).astype(np.int8)
  46. mul_8_1 = (2, 2, 2)
  47. input_8_x2 = np.arange(1).reshape((1, 1, 1)).astype(np.uint8)
  48. mul_8_2 = (1, 1, 1)
  49. class Net(Cell):
  50. def __init__(self):
  51. super(Net, self).__init__()
  52. self.Tile = Tile()
  53. self.input_x0 = Parameter(initializer(Tensor(input_x0), input_x0.shape), name='x0')
  54. self.mul0 = mul0
  55. self.input_x1 = Parameter(initializer(Tensor(input_x1), input_x1.shape), name='x1')
  56. self.mul1 = mul1
  57. self.input_x2 = Parameter(initializer(Tensor(input_x2), input_x2.shape), name='x2')
  58. self.mul2 = mul2
  59. @ms_function
  60. def construct(self):
  61. output = (self.Tile(self.input_x0, self.mul0),
  62. self.Tile(self.input_x1, self.mul1),
  63. self.Tile(self.input_x2, self.mul2))
  64. return output
  65. class Net32(Cell):
  66. def __init__(self):
  67. super(Net32, self).__init__()
  68. self.Tile = Tile()
  69. self.input_32_x0 = Parameter(initializer(Tensor(input_32_x0), input_32_x0.shape), name='x0')
  70. self.mul_32_0 = mul_32_0
  71. self.input_32_x1 = Parameter(initializer(Tensor(input_32_x1), input_32_x1.shape), name='x1')
  72. self.mul_32_1 = mul_32_1
  73. self.input_32_x2 = Parameter(initializer(Tensor(input_32_x2), input_32_x2.shape), name='x2')
  74. self.mul_32_2 = mul_32_2
  75. @ms_function
  76. def construct(self):
  77. output = (self.Tile(self.input_32_x0, self.mul_32_0),
  78. self.Tile(self.input_32_x1, self.mul_32_1),
  79. self.Tile(self.input_32_x2, self.mul_32_2))
  80. return output
  81. class Net16(Cell):
  82. def __init__(self):
  83. super(Net16, self).__init__()
  84. self.Tile = Tile()
  85. self.input_16_x0 = Parameter(initializer(Tensor(input_16_x0), input_16_x0.shape), name='x0')
  86. self.mul_16_0 = mul_16_0
  87. self.input_16_x1 = Parameter(initializer(Tensor(input_16_x1), input_16_x1.shape), name='x1')
  88. self.mul_16_1 = mul_16_1
  89. self.input_16_x2 = Parameter(initializer(Tensor(input_16_x2), input_16_x2.shape), name='x2')
  90. self.mul_16_2 = mul_16_2
  91. @ms_function
  92. def construct(self):
  93. output = (self.Tile(self.input_16_x0, self.mul_16_0),
  94. self.Tile(self.input_16_x1, self.mul_16_1),
  95. self.Tile(self.input_16_x2, self.mul_16_2))
  96. return output
  97. @pytest.mark.level0
  98. @pytest.mark.platform_x86_gpu_training
  99. @pytest.mark.env_onecard
  100. def test_tile():
  101. net = Net()
  102. output = net()
  103. expect0 = np.tile(input_x0, mul0)
  104. diff0 = output[0].asnumpy() - expect0
  105. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  106. assert np.all(diff0 < error0)
  107. assert output[0].shape == expect0.shape
  108. expect1 = np.tile(input_x1, mul1)
  109. diff1 = output[1].asnumpy() - expect1
  110. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  111. assert np.all(diff1 < error1)
  112. assert output[1].shape == expect1.shape
  113. expect2 = np.tile(input_x2, mul2)
  114. diff2 = output[2].asnumpy() - expect2
  115. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  116. assert np.all(diff2 < error2)
  117. assert output[2].shape == expect2.shape
  118. @pytest.mark.level0
  119. @pytest.mark.platform_x86_gpu_training
  120. @pytest.mark.env_onecard
  121. def test_tile_32():
  122. net = Net32()
  123. output = net()
  124. expect0 = np.tile(input_32_x0, mul_32_0)
  125. diff0 = output[0].asnumpy() - expect0
  126. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  127. assert np.all(diff0 < error0)
  128. assert output[0].shape == expect0.shape
  129. expect1 = np.tile(input_32_x1, mul_32_1)
  130. diff1 = output[1].asnumpy() - expect1
  131. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  132. assert np.all(diff1 < error1)
  133. assert output[1].shape == expect1.shape
  134. expect2 = np.tile(input_32_x2, mul_32_2)
  135. diff2 = output[2].asnumpy() - expect2
  136. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  137. assert np.all(diff2 < error2)
  138. assert output[2].shape == expect2.shape
  139. @pytest.mark.level0
  140. @pytest.mark.platform_x86_gpu_training
  141. @pytest.mark.env_onecard
  142. def test_tile_16():
  143. net = Net16()
  144. output = net()
  145. expect0 = np.tile(input_16_x0, mul_16_0)
  146. diff0 = output[0].asnumpy() - expect0
  147. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  148. assert np.all(diff0 < error0)
  149. assert output[0].shape == expect0.shape
  150. expect1 = np.tile(input_16_x1, mul_16_1)
  151. diff1 = output[1].asnumpy() - expect1
  152. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  153. assert np.all(diff1 < error1)
  154. assert output[1].shape == expect1.shape
  155. expect2 = np.tile(input_16_x2, mul_16_2)
  156. diff2 = output[2].asnumpy() - expect2
  157. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  158. assert np.all(diff2 < error2)
  159. assert output[2].shape == expect2.shape