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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 pytest
  16. from mindspore import Tensor
  17. from mindspore.ops import operations as P
  18. import mindspore.nn as nn
  19. from mindspore.common.api import ms_function
  20. import numpy as np
  21. import mindspore.context as context
  22. from mindspore.common.initializer import initializer
  23. from mindspore.common.parameter import Parameter
  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)