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_unpack_op.py 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. import mindspore.ops.operations.array_ops as P
  20. from mindspore import Tensor
  21. from mindspore.common.api import ms_function
  22. from mindspore.common.initializer import initializer
  23. from mindspore.common.parameter import Parameter
  24. class UnpackNet(nn.Cell):
  25. def __init__(self, nptype):
  26. super(UnpackNet, self).__init__()
  27. self.unpack = P.Unpack(axis=3)
  28. self.data_np = np.array([[[[[0, 0],
  29. [0, 1]],
  30. [[0, 0],
  31. [2, 3]]],
  32. [[[0, 0],
  33. [4, 5]],
  34. [[0, 0],
  35. [6, 7]]]],
  36. [[[[0, 0],
  37. [8, 9]],
  38. [[0, 0],
  39. [10, 11]]],
  40. [[[0, 0],
  41. [12, 13]],
  42. [[0, 0],
  43. [14, 15]]]]]).astype(nptype)
  44. self.x1 = Parameter(initializer(Tensor(self.data_np), [2, 2, 2, 2, 2]), name='x1')
  45. @ms_function
  46. def construct(self):
  47. return self.unpack(self.x1)
  48. def unpack(nptype):
  49. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  50. unpack_ = UnpackNet(nptype)
  51. output = unpack_()
  52. expect = (np.reshape(np.array([0] * 16).astype(nptype), (2, 2, 2, 2)),
  53. np.arange(2 * 2 * 2 * 2).reshape(2, 2, 2, 2).astype(nptype))
  54. for i, exp in enumerate(expect):
  55. assert (output[i].asnumpy() == exp).all()
  56. def unpack_pynative(nptype):
  57. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  58. x1 = np.array([[[[[0, 0],
  59. [0, 1]],
  60. [[0, 0],
  61. [2, 3]]],
  62. [[[0, 0],
  63. [4, 5]],
  64. [[0, 0],
  65. [6, 7]]]],
  66. [[[[0, 0],
  67. [8, 9]],
  68. [[0, 0],
  69. [10, 11]]],
  70. [[[0, 0],
  71. [12, 13]],
  72. [[0, 0],
  73. [14, 15]]]]]).astype(nptype)
  74. x1 = Tensor(x1)
  75. expect = (np.reshape(np.array([0] * 16).astype(nptype), (2, 2, 2, 2)),
  76. np.arange(2 * 2 * 2 * 2).reshape(2, 2, 2, 2).astype(nptype))
  77. output = P.Unpack(axis=3)(x1)
  78. for i, exp in enumerate(expect):
  79. assert (output[i].asnumpy() == exp).all()
  80. @pytest.mark.level0
  81. @pytest.mark.platform_x86_gpu_training
  82. @pytest.mark.env_onecard
  83. def test_unpack_graph_float32():
  84. unpack(np.float32)
  85. @pytest.mark.level0
  86. @pytest.mark.platform_x86_gpu_training
  87. @pytest.mark.env_onecard
  88. def test_unpack_graph_float16():
  89. unpack(np.float16)
  90. @pytest.mark.level0
  91. @pytest.mark.platform_x86_gpu_training
  92. @pytest.mark.env_onecard
  93. def test_unpack_graph_int32():
  94. unpack(np.int32)
  95. @pytest.mark.level0
  96. @pytest.mark.platform_x86_gpu_training
  97. @pytest.mark.env_onecard
  98. def test_unpack_graph_int16():
  99. unpack(np.int16)
  100. @pytest.mark.level0
  101. @pytest.mark.platform_x86_gpu_training
  102. @pytest.mark.env_onecard
  103. def test_unpack_graph_uint8():
  104. unpack(np.uint8)
  105. @pytest.mark.level0
  106. @pytest.mark.platform_x86_gpu_training
  107. @pytest.mark.env_onecard
  108. def test_unpack_graph_bool():
  109. unpack(np.bool)
  110. @pytest.mark.level0
  111. @pytest.mark.platform_x86_gpu_training
  112. @pytest.mark.env_onecard
  113. def test_unpack_pynative_float32():
  114. unpack_pynative(np.float32)
  115. @pytest.mark.level0
  116. @pytest.mark.platform_x86_gpu_training
  117. @pytest.mark.env_onecard
  118. def test_unpack_pynative_float16():
  119. unpack_pynative(np.float16)
  120. @pytest.mark.level0
  121. @pytest.mark.platform_x86_gpu_training
  122. @pytest.mark.env_onecard
  123. def test_unpack_pynative_int32():
  124. unpack_pynative(np.int32)
  125. @pytest.mark.level0
  126. @pytest.mark.platform_x86_gpu_training
  127. @pytest.mark.env_onecard
  128. def test_unpack_pynative_int16():
  129. unpack_pynative(np.int16)
  130. @pytest.mark.level0
  131. @pytest.mark.platform_x86_gpu_training
  132. @pytest.mark.env_onecard
  133. def test_unpack_pynative_uint8():
  134. unpack_pynative(np.uint8)
  135. @pytest.mark.level0
  136. @pytest.mark.platform_x86_gpu_training
  137. @pytest.mark.env_onecard
  138. def test_unpack_pynative_bool():
  139. unpack_pynative(np.bool)