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_concat_op.py 3.4 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
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright 2020 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. import numpy as np
  17. from mindspore import Tensor
  18. from mindspore.ops import operations as P
  19. import mindspore.nn as nn
  20. import mindspore.context as context
  21. from mindspore.common import dtype as mstype
  22. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  23. class Concat_Axis0(nn.Cell):
  24. def __init__(self):
  25. super(Concat_Axis0, self).__init__()
  26. self.cat = P.Concat(axis=0)
  27. def construct(self, x1, x2):
  28. return self.cat((x1, x2))
  29. @pytest.mark.level0
  30. @pytest.mark.platform_x86_cpu
  31. @pytest.mark.env_onecard
  32. def test_in2_axis0():
  33. x1 = Tensor(np.arange(2 * 2 * 2).reshape(2, 2, 2), mstype.float32)
  34. x2 = Tensor(np.arange(3 * 2 * 2).reshape(3, 2, 2), mstype.float32)
  35. cat = Concat_Axis0()
  36. output_ms = cat(x1, x2)
  37. print("output:\n", output_ms)
  38. output_np = np.concatenate((x1.asnumpy(), x2.asnumpy()), axis=0)
  39. error = np.ones(shape=output_np.shape) * 10e-6
  40. diff = output_ms.asnumpy() - output_np
  41. assert np.all(diff < error)
  42. assert np.all(-diff < error)
  43. class Concat_Axis1(nn.Cell):
  44. def __init__(self):
  45. super(Concat_Axis1, self).__init__()
  46. self.cat = P.Concat(axis=1)
  47. def construct(self, x1, x2):
  48. return self.cat((x1, x2))
  49. @pytest.mark.level0
  50. @pytest.mark.platform_x86_cpu
  51. @pytest.mark.env_onecard
  52. def test_in2_axis1():
  53. x1 = Tensor(np.arange(2 * 2 * 2).reshape(2, 2, 2), mstype.float32)
  54. x2 = Tensor(np.arange(2 * 3 * 2).reshape(2, 3, 2), mstype.float32)
  55. cat = Concat_Axis1()
  56. output_ms = cat(x1, x2)
  57. print("output:\n", output_ms)
  58. output_np = np.concatenate((x1.asnumpy(), x2.asnumpy()), axis=1)
  59. error = np.ones(shape=output_np.shape) * 10e-6
  60. diff = output_ms.asnumpy() - output_np
  61. assert np.all(diff < error)
  62. assert np.all(-diff < error)
  63. class Concat_in3_Axis2(nn.Cell):
  64. def __init__(self):
  65. super(Concat_in3_Axis2, self).__init__()
  66. self.cat = P.Concat(axis=-1)
  67. def construct(self, x1, x2, x3):
  68. return self.cat((x1, x2, x3))
  69. @pytest.mark.level0
  70. @pytest.mark.platform_x86_cpu
  71. @pytest.mark.env_onecard
  72. def test_in3_axis2():
  73. x1 = Tensor(np.arange(2 * 2 * 1).reshape(2, 2, 1), mstype.float32)
  74. x2 = Tensor(np.arange(2 * 2 * 2).reshape(2, 2, 2), mstype.float32)
  75. x3 = Tensor(np.arange(2 * 2 * 3).reshape(2, 2, 3), mstype.float32)
  76. cat = Concat_in3_Axis2()
  77. output_ms = cat(x1, x2, x3)
  78. print("output:\n", output_ms)
  79. output_np = np.concatenate((x1.asnumpy(), x2.asnumpy(), x3.asnumpy()), axis=-1)
  80. error = np.ones(shape=output_np.shape) * 10e-6
  81. diff = output_ms.asnumpy() - output_np
  82. assert np.all(diff < error)
  83. assert np.all(-diff < error)
  84. if __name__ == '__main__':
  85. test_in2_axis0()
  86. test_in2_axis1()
  87. test_in3_axis2()