• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 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# ============================================================================
15import numpy as np
16import pytest
17
18import mindspore.context as context
19import mindspore.nn as nn
20from mindspore import Tensor
21from mindspore.ops import operations as P
22
23class ReverseV2Net(nn.Cell):
24    def __init__(self, axis):
25        super(ReverseV2Net, self).__init__()
26        self.reverse_v2 = P.ReverseV2(axis)
27
28    def construct(self, x):
29        return self.reverse_v2(x)
30
31
32def reverse_v2(x_numpy, axis):
33    x = Tensor(x_numpy)
34    reverse_v2_net = ReverseV2Net(axis)
35    output = reverse_v2_net(x).asnumpy()
36    expected_output = np.flip(x_numpy, axis)
37    np.testing.assert_array_equal(output, expected_output)
38
39def reverse_v2_3d(nptype):
40    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
41
42    x_numpy = np.arange(60).reshape(3, 4, 5).astype(nptype)
43
44    reverse_v2(x_numpy, (0,))
45    reverse_v2(x_numpy, (1,))
46    reverse_v2(x_numpy, (2,))
47    reverse_v2(x_numpy, (2, -2))
48    reverse_v2(x_numpy, (-3, 1, 2))
49
50def reverse_v2_1d(nptype):
51    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
52
53    x_numpy = np.arange(4).astype(nptype)
54
55    reverse_v2(x_numpy, (0,))
56    reverse_v2(x_numpy, (-1,))
57
58@pytest.mark.level0
59@pytest.mark.platform_x86_gpu_training
60@pytest.mark.env_onecard
61def test_reverse_v2_float16():
62    reverse_v2_1d(np.float16)
63    reverse_v2_3d(np.float16)
64
65@pytest.mark.level0
66@pytest.mark.platform_x86_gpu_training
67@pytest.mark.env_onecard
68def test_reverse_v2_float32():
69    reverse_v2_1d(np.float32)
70    reverse_v2_3d(np.float32)
71
72@pytest.mark.level1
73@pytest.mark.platform_x86_gpu_training
74@pytest.mark.env_onecard
75def test_reverse_v2_uint8():
76    reverse_v2_1d(np.uint8)
77    reverse_v2_3d(np.uint8)
78
79@pytest.mark.level1
80@pytest.mark.platform_x86_gpu_training
81@pytest.mark.env_onecard
82def test_reverse_v2_int16():
83    reverse_v2_1d(np.int16)
84    reverse_v2_3d(np.int16)
85
86@pytest.mark.level1
87@pytest.mark.platform_x86_gpu_training
88@pytest.mark.env_onecard
89def test_reverse_v2_int32():
90    reverse_v2_1d(np.int32)
91    reverse_v2_3d(np.int32)
92
93@pytest.mark.level1
94@pytest.mark.platform_x86_gpu_training
95@pytest.mark.env_onecard
96def test_reverse_v2_int64():
97    reverse_v2_1d(np.int64)
98    reverse_v2_3d(np.int64)
99
100@pytest.mark.level0
101@pytest.mark.platform_x86_gpu_training
102@pytest.mark.env_onecard
103def test_reverse_v2_invalid_axis():
104    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
105    x = Tensor(np.arange(60).reshape(1, 2, 3, 2, 5).astype(np.int32))
106
107    with pytest.raises(ValueError) as info:
108        reverse_v2_net = ReverseV2Net((0, 1, 2, 1))
109        _ = reverse_v2_net(x)
110    assert "'axis' cannot contain duplicate dimensions" in str(info.value)
111
112    with pytest.raises(ValueError) as info:
113        reverse_v2_net = ReverseV2Net((-2, -1, 3))
114        _ = reverse_v2_net(x)
115    assert "'axis' cannot contain duplicate dimensions" in str(info.value)
116