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

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 Net(nn.Cell):
  25. def __init__(self, nptype):
  26. super(Net, self).__init__()
  27. self.unstack = P.Unstack(axis=3)
  28. self.data_np = np.array([[[[[0, 0],
  29. [-2, -1]],
  30. [[0, 0],
  31. [0, 1]]],
  32. [[[0, 0],
  33. [2, 3]],
  34. [[0, 0],
  35. [4, 5]]],
  36. [[[0, 0],
  37. [6, 7]],
  38. [[0, 0],
  39. [8, 9]]]],
  40. [[[[0, 0],
  41. [10, 11]],
  42. [[0, 0],
  43. [12, 13]]],
  44. [[[0, 0],
  45. [14, 15]],
  46. [[0, 0],
  47. [16, 17]]],
  48. [[[0, 0],
  49. [18, 19]],
  50. [[0, 0],
  51. [20, 21]]]],
  52. [[[[0, 0],
  53. [22, 23]],
  54. [[0, 0],
  55. [24, 25]]],
  56. [[[0, 0],
  57. [26, 27]],
  58. [[0, 0],
  59. [28, 29]]],
  60. [[[0, 0],
  61. [30, 31]],
  62. [[0, 0],
  63. [32, 33]]]]]).astype(nptype)
  64. self.x1 = Parameter(initializer(Tensor(self.data_np), [3, 3, 2, 2, 2]), name='x1')
  65. @ms_function
  66. def construct(self):
  67. return self.unstack(self.x1)
  68. def unpack(nptype):
  69. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  70. unpack_ = Net(nptype)
  71. output = unpack_()
  72. expect = (np.reshape(np.array([0] * 36).astype(nptype), (3, 3, 2, 2)),
  73. np.arange(-2, 34, 1).reshape(3, 3, 2, 2).astype(nptype))
  74. for i, exp in enumerate(expect):
  75. assert (output[i].asnumpy() == exp).all()
  76. def unpack_pynative(nptype):
  77. context.set_context(mode=context.PYNATIVE_MODE, device_target='CPU')
  78. x1 = np.array([[[[[0, 0],
  79. [-2, -1]],
  80. [[0, 0],
  81. [0, 1]]],
  82. [[[0, 0],
  83. [2, 3]],
  84. [[0, 0],
  85. [4, 5]]],
  86. [[[0, 0],
  87. [6, 7]],
  88. [[0, 0],
  89. [8, 9]]]],
  90. [[[[0, 0],
  91. [10, 11]],
  92. [[0, 0],
  93. [12, 13]]],
  94. [[[0, 0],
  95. [14, 15]],
  96. [[0, 0],
  97. [16, 17]]],
  98. [[[0, 0],
  99. [18, 19]],
  100. [[0, 0],
  101. [20, 21]]]],
  102. [[[[0, 0],
  103. [22, 23]],
  104. [[0, 0],
  105. [24, 25]]],
  106. [[[0, 0],
  107. [26, 27]],
  108. [[0, 0],
  109. [28, 29]]],
  110. [[[0, 0],
  111. [30, 31]],
  112. [[0, 0],
  113. [32, 33]]]]]).astype(nptype)
  114. x1 = Tensor(x1)
  115. expect = (np.reshape(np.array([0] * 36).astype(nptype), (3, 3, 2, 2)),
  116. np.arange(-2, 34, 1).reshape(3, 3, 2, 2).astype(nptype))
  117. output = P.Unstack(axis=3)(x1)
  118. for i, exp in enumerate(expect):
  119. assert (output[i].asnumpy() == exp).all()
  120. @pytest.mark.level0
  121. @pytest.mark.platform_x86_cpu_training
  122. @pytest.mark.env_onecard
  123. def test_unpack_graph_float32():
  124. unpack(np.float32)
  125. @pytest.mark.level0
  126. @pytest.mark.platform_x86_cpu_training
  127. @pytest.mark.env_onecard
  128. def test_unpack_graph_float16():
  129. unpack(np.float16)
  130. @pytest.mark.level0
  131. @pytest.mark.platform_x86_cpu_training
  132. @pytest.mark.env_onecard
  133. def test_unpack_graph_int32():
  134. unpack(np.int32)
  135. @pytest.mark.level0
  136. @pytest.mark.platform_x86_cpu_training
  137. @pytest.mark.env_onecard
  138. def test_unpack_graph_int16():
  139. unpack(np.int16)
  140. @pytest.mark.level0
  141. @pytest.mark.platform_x86_cpu_training
  142. @pytest.mark.env_onecard
  143. def test_unpack_graph_uint8():
  144. unpack(np.uint8)
  145. @pytest.mark.level0
  146. @pytest.mark.platform_x86_cpu_training
  147. @pytest.mark.env_onecard
  148. def test_unpack_graph_bool():
  149. unpack(np.bool)
  150. @pytest.mark.level0
  151. @pytest.mark.platform_x86_cpu_training
  152. @pytest.mark.env_onecard
  153. def test_unpack_pynative_float32():
  154. unpack_pynative(np.float32)
  155. @pytest.mark.level0
  156. @pytest.mark.platform_x86_cpu_training
  157. @pytest.mark.env_onecard
  158. def test_unpack_pynative_float16():
  159. unpack_pynative(np.float16)
  160. @pytest.mark.level0
  161. @pytest.mark.platform_x86_cpu_training
  162. @pytest.mark.env_onecard
  163. def test_unpack_pynative_int32():
  164. unpack_pynative(np.int32)
  165. @pytest.mark.level0
  166. @pytest.mark.platform_x86_cpu_training
  167. @pytest.mark.env_onecard
  168. def test_unpack_pynative_int16():
  169. unpack_pynative(np.int16)
  170. @pytest.mark.level0
  171. @pytest.mark.platform_x86_cpu_training
  172. @pytest.mark.env_onecard
  173. def test_unpack_pynative_uint8():
  174. unpack_pynative(np.uint8)
  175. @pytest.mark.level0
  176. @pytest.mark.platform_x86_cpu_training
  177. @pytest.mark.env_onecard
  178. def test_unpack_pynative_bool():
  179. unpack_pynative(np.bool)