|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- # Copyright 2020 Huawei Technologies Co., Ltd
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- # ============================================================================
-
- import numpy as np
- import pytest
-
- import mindspore.context as context
- import mindspore.nn as nn
- import mindspore.ops.operations.array_ops as P
- from mindspore import Tensor
- from mindspore.common.api import ms_function
- from mindspore.common.initializer import initializer
- from mindspore.common.parameter import Parameter
-
-
- class UnpackNet(nn.Cell):
- def __init__(self, nptype):
- super(UnpackNet, self).__init__()
-
- self.unpack = P.Unpack(axis=3)
- self.data_np = np.array([[[[[0, 0],
- [0, 1]],
- [[0, 0],
- [2, 3]]],
- [[[0, 0],
- [4, 5]],
- [[0, 0],
- [6, 7]]]],
- [[[[0, 0],
- [8, 9]],
- [[0, 0],
- [10, 11]]],
- [[[0, 0],
- [12, 13]],
- [[0, 0],
- [14, 15]]]]]).astype(nptype)
- self.x1 = Parameter(initializer(Tensor(self.data_np), [2, 2, 2, 2, 2]), name='x1')
-
- @ms_function
- def construct(self):
- return self.unpack(self.x1)
-
-
- def unpack(nptype):
- context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
- unpack_ = UnpackNet(nptype)
- output = unpack_()
- expect = (np.reshape(np.array([0] * 16).astype(nptype), (2, 2, 2, 2)),
- np.arange(2 * 2 * 2 * 2).reshape(2, 2, 2, 2).astype(nptype))
-
- for i, exp in enumerate(expect):
- assert (output[i].asnumpy() == exp).all()
-
- def unpack_pynative(nptype):
- context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
- x1 = np.array([[[[[0, 0],
- [0, 1]],
- [[0, 0],
- [2, 3]]],
- [[[0, 0],
- [4, 5]],
- [[0, 0],
- [6, 7]]]],
- [[[[0, 0],
- [8, 9]],
- [[0, 0],
- [10, 11]]],
- [[[0, 0],
- [12, 13]],
- [[0, 0],
- [14, 15]]]]]).astype(nptype)
- x1 = Tensor(x1)
- expect = (np.reshape(np.array([0] * 16).astype(nptype), (2, 2, 2, 2)),
- np.arange(2 * 2 * 2 * 2).reshape(2, 2, 2, 2).astype(nptype))
- output = P.Unpack(axis=3)(x1)
-
- for i, exp in enumerate(expect):
- assert (output[i].asnumpy() == exp).all()
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_unpack_graph_float32():
- unpack(np.float32)
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_unpack_graph_float16():
- unpack(np.float16)
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_unpack_graph_int32():
- unpack(np.int32)
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_unpack_graph_int16():
- unpack(np.int16)
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_unpack_graph_uint8():
- unpack(np.uint8)
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_unpack_graph_bool():
- unpack(np.bool)
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_unpack_pynative_float32():
- unpack_pynative(np.float32)
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_unpack_pynative_float16():
- unpack_pynative(np.float16)
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_unpack_pynative_int32():
- unpack_pynative(np.int32)
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_unpack_pynative_int16():
- unpack_pynative(np.int16)
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_unpack_pynative_uint8():
- unpack_pynative(np.uint8)
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_unpack_pynative_bool():
- unpack_pynative(np.bool)
|