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_pack_op.py 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 PackNet(nn.Cell):
  25. def __init__(self, nptype):
  26. super(PackNet, self).__init__()
  27. self.stack = P.Stack(axis=2)
  28. self.data_np = np.array([0] * 16).astype(nptype)
  29. self.data_np = np.reshape(self.data_np, (2, 2, 2, 2))
  30. self.x1 = Parameter(initializer(
  31. Tensor(self.data_np), [2, 2, 2, 2]), name='x1')
  32. self.x2 = Parameter(initializer(
  33. Tensor(np.arange(16).reshape(2, 2, 2, 2).astype(nptype)), [2, 2, 2, 2]), name='x2')
  34. @ms_function
  35. def construct(self):
  36. return self.stack((self.x1, self.x2))
  37. def pack(nptype):
  38. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  39. pack_ = PackNet(nptype)
  40. output = pack_()
  41. expect = np.array([[[[[0, 0],
  42. [0, 0]],
  43. [[0, 1],
  44. [2, 3]]],
  45. [[[0, 0],
  46. [0, 0]],
  47. [[4, 5],
  48. [6, 7]]]],
  49. [[[[0, 0],
  50. [0, 0]],
  51. [[8, 9],
  52. [10, 11]]],
  53. [[[0, 0],
  54. [0, 0]],
  55. [[12, 13],
  56. [14, 15]]]]]).astype(nptype)
  57. assert (output.asnumpy() == expect).all()
  58. @pytest.mark.level0
  59. @pytest.mark.platform_x86_cpu
  60. @pytest.mark.env_onecard
  61. def test_pack_graph_float32():
  62. pack(np.float32)
  63. @pytest.mark.level0
  64. @pytest.mark.platform_x86_cpu
  65. @pytest.mark.env_onecard
  66. def test_pack_graph_float16():
  67. pack(np.float16)
  68. @pytest.mark.level0
  69. @pytest.mark.platform_x86_cpu
  70. @pytest.mark.env_onecard
  71. def test_pack_graph_int32():
  72. pack(np.int32)
  73. @pytest.mark.level0
  74. @pytest.mark.platform_x86_cpu
  75. @pytest.mark.env_onecard
  76. def test_pack_graph_int16():
  77. pack(np.int16)
  78. @pytest.mark.level0
  79. @pytest.mark.platform_x86_cpu
  80. @pytest.mark.env_onecard
  81. def test_pack_graph_uint8():
  82. pack(np.uint8)
  83. @pytest.mark.level0
  84. @pytest.mark.platform_x86_cpu
  85. @pytest.mark.env_onecard
  86. def test_pack_graph_bool():
  87. pack(np.bool)