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_split.py 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # Copyright 2020-2021 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 import Tensor
  19. import mindspore.nn as nn
  20. from mindspore.ops.operations import _inner_ops as inner
  21. from mindspore.ops import operations as P
  22. class Net(nn.Cell):
  23. def __init__(self, axis=0, out_nums=1):
  24. super(Net, self).__init__()
  25. self.split = P.Split(axis, out_nums)
  26. def construct(self, x):
  27. return self.split(x)
  28. class NetDynamic(nn.Cell):
  29. def __init__(self, axis=0, out_nums=1):
  30. super(NetDynamic, self).__init__()
  31. self.conv = inner.GpuConvertToDynamicShape()
  32. self.split = P.Split(axis, out_nums)
  33. def construct(self, x):
  34. x_conv = self.conv(x)
  35. x_split = self.split(x_conv)
  36. return x_split
  37. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  38. def split_basic(nptype):
  39. x = np.array([[[1, -1, 1], [2, -2, 2]],
  40. [[3, -3, 3], [4, -4, 4]],
  41. [[5, -5, 5], [6, -6, 6]]]).astype(nptype)
  42. split_op = Net(0, 3)
  43. outputs = split_op(Tensor(x))
  44. for i, out in enumerate(outputs):
  45. assert (out.asnumpy() == x[i]).all()
  46. @pytest.mark.level0
  47. @pytest.mark.platform_x86_gpu_training
  48. @pytest.mark.env_onecard
  49. def test_split_basic_float16():
  50. split_basic(np.float16)
  51. @pytest.mark.level0
  52. @pytest.mark.platform_x86_gpu_training
  53. @pytest.mark.env_onecard
  54. def test_split_basic_float32():
  55. split_basic(np.float32)
  56. @pytest.mark.level0
  57. @pytest.mark.platform_x86_gpu_training
  58. @pytest.mark.env_onecard
  59. def test_split_basic_float64():
  60. split_basic(np.float64)
  61. @pytest.mark.level0
  62. @pytest.mark.platform_x86_gpu_training
  63. @pytest.mark.env_onecard
  64. def test_split_basic_int32():
  65. split_basic(np.int32)
  66. @pytest.mark.level0
  67. @pytest.mark.platform_x86_gpu_training
  68. @pytest.mark.env_onecard
  69. def test_split_basic_uint32():
  70. split_basic(np.uint32)
  71. @pytest.mark.level0
  72. @pytest.mark.platform_x86_gpu_training
  73. @pytest.mark.env_onecard
  74. def test_split_basic_int64():
  75. split_basic(np.int64)
  76. @pytest.mark.level0
  77. @pytest.mark.platform_x86_gpu_training
  78. @pytest.mark.env_onecard
  79. def test_split_basic_bool():
  80. split_basic(np.bool)
  81. @pytest.mark.level0
  82. @pytest.mark.platform_x86_gpu_training
  83. @pytest.mark.env_onecard
  84. def test_split_4d():
  85. x_np = np.random.randn(2, 6, 4, 4).astype(np.float32)
  86. y = np.split(x_np, 3, axis=1)
  87. split_op = Net(1, 3)
  88. outputs = split_op(Tensor(x_np))
  89. for i, out in enumerate(outputs):
  90. assert (out.asnumpy() == y[i]).all()
  91. @pytest.mark.level0
  92. @pytest.mark.platform_x86_gpu_training
  93. @pytest.mark.env_onecard
  94. def test_split_dynamic():
  95. x = np.array([[[1, -1, 1], [2, -2, 2]],
  96. [[3, -3, 3], [4, -4, 4]],
  97. [[5, -5, 5], [6, -6, 6]]]).astype(np.float32)
  98. net = NetDynamic(0, 3)
  99. x_split = net(Tensor(x))
  100. for i, out in enumerate(x_split):
  101. assert (out.asnumpy() == x[i]).all()
  102. @pytest.mark.level0
  103. @pytest.mark.platform_x86_gpu_training
  104. @pytest.mark.env_onecard
  105. def test_split_dynamic_axis1():
  106. x = np.array([[[1, -1, 1], [2, -2, 2]],
  107. [[3, -3, 3], [4, -4, 4]],
  108. [[5, -5, 5], [6, -6, 6]]]).astype(np.int32)
  109. y = np.split(x, 2, axis=1)
  110. net = NetDynamic(1, 2)
  111. x_split = net(Tensor(x))
  112. for i, out in enumerate(x_split):
  113. assert (out.asnumpy() == y[i]).all()
  114. @pytest.mark.level0
  115. @pytest.mark.platform_x86_gpu_training
  116. @pytest.mark.env_onecard
  117. def test_split_dynamic_axis2():
  118. x = np.array([[[1, -1, 1], [2, -2, 2]],
  119. [[3, -3, 3], [4, -4, 4]],
  120. [[5, -5, 5], [6, -6, 6]]]).astype(np.int32)
  121. y = np.split(x, 3, axis=2)
  122. net = NetDynamic(2, 3)
  123. x_split = net(Tensor(x))
  124. for i, out in enumerate(x_split):
  125. assert (out.asnumpy() == y[i]).all()
  126. @pytest.mark.level0
  127. @pytest.mark.platform_x86_gpu_training
  128. @pytest.mark.env_onecard
  129. def test_split_invalid_input():
  130. with pytest.raises(TypeError):
  131. _ = Net(0.1, 3)
  132. with pytest.raises(TypeError):
  133. _ = Net(0, 3.0)
  134. with pytest.raises(ValueError):
  135. _ = Net(0, -3)
  136. x = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.int32)
  137. split_net = Net(2, 2)
  138. with pytest.raises(ValueError):
  139. _ = split_net(Tensor(x))
  140. with pytest.raises(TypeError):
  141. _ = split_net(x)