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_concatv2_op.py 5.5 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.common.api import ms_function
  21. from mindspore.common.initializer import initializer
  22. from mindspore.common.parameter import Parameter
  23. from mindspore.ops import operations as P
  24. context.set_context(device_target='GPU')
  25. class ConcatV32(nn.Cell):
  26. def __init__(self):
  27. super(ConcatV32, self).__init__()
  28. self.cat = P.Concat(axis=2)
  29. self.x1 = Parameter(initializer(
  30. Tensor(np.arange(2 * 2 * 1).reshape(2, 2, 1).astype(np.float32)), [2, 2, 1]), name='x1')
  31. self.x2 = Parameter(initializer(
  32. Tensor(np.arange(2 * 2 * 2).reshape(2, 2, 2).astype(np.float32)), [2, 2, 2]), name='x2')
  33. @ms_function
  34. def construct(self):
  35. return self.cat((self.x1, self.x2))
  36. @pytest.mark.level0
  37. @pytest.mark.platform_x86_gpu_training
  38. @pytest.mark.env_onecard
  39. def test_axis32():
  40. cat = ConcatV32()
  41. output = cat()
  42. expect = [[[0., 0., 1.],
  43. [1., 2., 3.]],
  44. [[2., 4., 5.],
  45. [3., 6., 7.]]]
  46. print(output)
  47. assert (output.asnumpy() == expect).all()
  48. class ConcatV43(nn.Cell):
  49. def __init__(self):
  50. super(ConcatV43, self).__init__()
  51. self.cat = P.Concat(axis=3)
  52. self.x1 = Parameter(initializer(
  53. Tensor(np.arange(2 * 2 * 2 * 2).reshape(2, 2, 2, 2).astype(np.float32)), [2, 2, 2, 2]), name='x1')
  54. self.x2 = Parameter(initializer(
  55. Tensor(np.arange(2 * 2 * 2 * 3).reshape(2, 2, 2, 3).astype(np.float32)), [2, 2, 2, 3]), name='x2')
  56. @ms_function
  57. def construct(self):
  58. return self.cat((self.x1, self.x2))
  59. @pytest.mark.level0
  60. @pytest.mark.platform_x86_gpu_training
  61. @pytest.mark.env_onecard
  62. def test_axis43():
  63. cat = ConcatV43()
  64. output = cat()
  65. expect = [[[[0., 1., 0., 1., 2.],
  66. [2., 3., 3., 4., 5.]],
  67. [[4., 5., 6., 7., 8.],
  68. [6., 7., 9., 10., 11.]]],
  69. [[[8., 9., 12., 13., 14.],
  70. [10., 11., 15., 16., 17.]],
  71. [[12., 13., 18., 19., 20.],
  72. [14., 15., 21., 22., 23.]]]]
  73. assert (output.asnumpy() == expect).all()
  74. print(output)
  75. class ConcatV21(nn.Cell):
  76. def __init__(self):
  77. super(ConcatV21, self).__init__()
  78. self.cat = P.Concat(axis=1)
  79. self.x1 = Parameter(initializer(
  80. Tensor(np.arange(2 * 2).reshape(2, 2).astype(np.float32)), [2, 2]), name='x1')
  81. self.x2 = Parameter(initializer(
  82. Tensor(np.arange(2 * 3).reshape(2, 3).astype(np.float32)), [2, 3]), name='x2')
  83. @ms_function
  84. def construct(self):
  85. return self.cat((self.x1, self.x2))
  86. @pytest.mark.level0
  87. @pytest.mark.platform_x86_gpu_training
  88. @pytest.mark.env_onecard
  89. def test_axis21():
  90. cat = ConcatV21()
  91. output = cat()
  92. expect = [[0., 1., 0., 1., 2.],
  93. [2., 3., 3., 4., 5.]]
  94. assert (output.asnumpy() == expect).all()
  95. print(output)
  96. class Concat3INet(nn.Cell):
  97. def __init__(self):
  98. super(Concat3INet, self).__init__()
  99. self.cat = P.Concat(axis=1)
  100. def construct(self, x1, x2, x3):
  101. return self.cat((x1, x2, x3))
  102. @pytest.mark.level0
  103. @pytest.mark.platform_x86_gpu_training
  104. @pytest.mark.env_onecard
  105. def test_concat_3i():
  106. cat = Concat3INet()
  107. x1_np = np.random.randn(32, 4, 224, 224).astype(np.float32)
  108. x2_np = np.random.randn(32, 8, 224, 224).astype(np.float32)
  109. x3_np = np.random.randn(32, 10, 224, 224).astype(np.float32)
  110. output_np = np.concatenate((x1_np, x2_np, x3_np), axis=1)
  111. x1_ms = Tensor(x1_np)
  112. x2_ms = Tensor(x2_np)
  113. x3_ms = Tensor(x3_np)
  114. output_ms = cat(x1_ms, x2_ms, x3_ms)
  115. error = np.ones(shape=output_np.shape) * 10e-6
  116. diff = output_ms.asnumpy() - output_np
  117. assert np.all(diff < error)
  118. class Concat4INet(nn.Cell):
  119. def __init__(self):
  120. super(Concat4INet, self).__init__()
  121. self.cat = P.Concat(axis=1)
  122. def construct(self, x1, x2, x3, x4):
  123. return self.cat((x1, x2, x3, x4))
  124. @pytest.mark.level0
  125. @pytest.mark.platform_x86_gpu_training
  126. @pytest.mark.env_onecard
  127. def test_concat_4i():
  128. cat = Concat4INet()
  129. x1_np = np.random.randn(32, 4, 224, 224).astype(np.float32)
  130. x2_np = np.random.randn(32, 8, 224, 224).astype(np.float32)
  131. x3_np = np.random.randn(32, 10, 224, 224).astype(np.float32)
  132. x4_np = np.random.randn(32, 5, 224, 224).astype(np.float32)
  133. output_np = np.concatenate((x1_np, x2_np, x3_np, x4_np), axis=1)
  134. x1_ms = Tensor(x1_np)
  135. x2_ms = Tensor(x2_np)
  136. x3_ms = Tensor(x3_np)
  137. x4_ms = Tensor(x4_np)
  138. output_ms = cat(x1_ms, x2_ms, x3_ms, x4_ms)
  139. error = np.ones(shape=output_np.shape) * 10e-6
  140. diff = output_ms.asnumpy() - output_np
  141. assert np.all(diff < error)