• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import numpy as np
17import pytest
18
19import mindspore.context as context
20import mindspore.nn as nn
21from mindspore import Tensor
22from mindspore.common.api import ms_function
23from mindspore.common.initializer import initializer
24from mindspore.common.parameter import Parameter
25from mindspore.ops import operations as P
26from mindspore.ops.operations import _inner_ops as inner
27
28context.set_context(device_target='GPU')
29
30class Transpose(nn.Cell):
31    def __init__(self, nptype):
32        super(Transpose, self).__init__()
33        self.transpose = P.Transpose()
34        self.x_2D = Parameter(initializer(Tensor(np.arange(5 * 6).reshape(5, 6).astype(nptype)), [5, 6]),
35                              name='x_2D')
36        self.perm_2D = (1, 0)
37        self.x_3D = Parameter(initializer(Tensor(np.arange(2 * 2 * 4).reshape(2, 2, 4).astype(nptype)), [2, 2, 4]),
38                              name='x_3D')
39        self.perm_3D = (1, 0, 2)
40        self.x_4D = Parameter(
41            initializer(Tensor(np.arange(2 * 3 * 4 * 5).reshape(2, 3, 4, 5).astype(nptype)), [2, 3, 4, 5]),
42            name='x_4D')
43        self.perm_4D = (0, 1, 2, 3)
44        self.x_5D = Parameter(
45            initializer(Tensor(np.arange(1 * 2 * 3 * 4 * 5).reshape(1, 2, 3, 4, 5).astype(nptype)),
46                        [1, 2, 3, 4, 5]), name='x_5D')
47        self.perm_5D = (1, 0, 3, 4, 2)
48
49    @ms_function
50    def construct(self):
51        return (self.transpose(self.x_2D, self.perm_2D), self.transpose(self.x_3D, self.perm_3D),
52                self.transpose(self.x_4D, self.perm_4D), self.transpose(self.x_5D, self.perm_5D))
53
54class Transpose_dynamic(nn.Cell):
55    def __init__(self, nptype):
56        super(Transpose_dynamic, self).__init__()
57        self.transpose = P.Transpose()
58        self.test_dynamic = inner.GpuConvertToDynamicShape()
59        self.x = Parameter(
60            initializer(Tensor(np.arange(1 * 2 * 3 * 4 * 5).reshape(1, 2, 3, 4, 5).astype(nptype)),
61                        [1, 2, 3, 4, 5]), name='5D')
62        self.perm = (1, 0, 3, 4, 2)
63
64    @ms_function
65    def construct(self):
66        out = self.test_dynamic(self.x)
67        return self.transpose(out, self.perm)
68
69class Transpose_dynamic2(nn.Cell):
70    def __init__(self, input_1, input_2, perm_1, perm_2):
71        super(Transpose_dynamic2, self).__init__()
72        self.transpose = P.Transpose()
73        self.test_dynamic = inner.GpuConvertToDynamicShape()
74        self.x_1 = input_1
75        self.x_2 = input_2
76        self.perm_1 = perm_1
77        self.perm_2 = perm_2
78
79    @ms_function
80    def construct(self):
81        out_1 = self.test_dynamic(self.x_1)
82        out_1 = self.transpose(out_1, self.perm_1)
83        out_2 = self.test_dynamic(self.x_2)
84        out_2 = self.transpose(out_2, self.perm_2)
85        return (out_1, out_2)
86
87def transpose1(nptype):
88    context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
89    transpose = Transpose(nptype)
90    output = transpose()
91    expect0 = np.array([[[0, 6, 12, 18, 24],
92                         [1, 7, 13, 19, 25],
93                         [2, 8, 14, 20, 26],
94                         [3, 9, 15, 21, 27],
95                         [4, 10, 16, 22, 28],
96                         [5, 11, 17, 23, 29]]]).astype(nptype)
97    expect1 = np.array([[[[0, 1, 2, 3],
98                          [8, 9, 10, 11]],
99                         [[4, 5, 6, 7],
100                          [12, 13, 14, 15]]]]).astype(nptype)
101    expect2 = np.array([[[[[0, 1, 2, 3, 4],
102                           [5, 6, 7, 8, 9],
103                           [10, 11, 12, 13, 14],
104                           [15, 16, 17, 18, 19]],
105                          [[20, 21, 22, 23, 24],
106                           [25, 26, 27, 28, 29],
107                           [30, 31, 32, 33, 34],
108                           [35, 36, 37, 38, 39]],
109                          [[40, 41, 42, 43, 44],
110                           [45, 46, 47, 48, 49],
111                           [50, 51, 52, 53, 54],
112                           [55, 56, 57, 58, 59]]],
113                         [[[60, 61, 62, 63, 64],
114                           [65, 66, 67, 68, 69],
115                           [70, 71, 72, 73, 74],
116                           [75, 76, 77, 78, 79]],
117                          [[80, 81, 82, 83, 84],
118                           [85, 86, 87, 88, 89],
119                           [90, 91, 92, 93, 94],
120                           [95, 96, 97, 98, 99]],
121                          [[100, 101, 102, 103, 104],
122                           [105, 106, 107, 108, 109],
123                           [110, 111, 112, 113, 114],
124                           [115, 116, 117, 118, 119]]]]]).astype(nptype)
125    expect3 = np.array([[[[[[0, 20, 40],
126                            [1, 21, 41],
127                            [2, 22, 42],
128                            [3, 23, 43],
129                            [4, 24, 44]],
130                           [[5, 25, 45],
131                            [6, 26, 46],
132                            [7, 27, 47],
133                            [8, 28, 48],
134                            [9, 29, 49]],
135                           [[10, 30, 50],
136                            [11, 31, 51],
137                            [12, 32, 52],
138                            [13, 33, 53],
139                            [14, 34, 54]],
140                           [[15, 35, 55],
141                            [16, 36, 56],
142                            [17, 37, 57],
143                            [18, 38, 58],
144                            [19, 39, 59]]]],
145                         [[[[60, 80, 100],
146                            [61, 81, 101],
147                            [62, 82, 102],
148                            [63, 83, 103],
149                            [64, 84, 104]],
150                           [[65, 85, 105],
151                            [66, 86, 106],
152                            [67, 87, 107],
153                            [68, 88, 108],
154                            [69, 89, 109]],
155                           [[70, 90, 110],
156                            [71, 91, 111],
157                            [72, 92, 112],
158                            [73, 93, 113],
159                            [74, 94, 114]],
160                           [[75, 95, 115],
161                            [76, 96, 116],
162                            [77, 97, 117],
163                            [78, 98, 118],
164                            [79, 99, 119]]]]]]).astype(nptype)
165    assert (output[0].asnumpy() == expect0).all()
166    assert (output[1].asnumpy() == expect1).all()
167    assert (output[2].asnumpy() == expect2).all()
168    assert (output[3].asnumpy() == expect3).all()
169
170def transpose_d(nptype):
171    context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
172    transpose = Transpose_dynamic(nptype)
173    output = transpose()
174    expect = np.array([[[[[[0, 20, 40],
175                           [1, 21, 41],
176                           [2, 22, 42],
177                           [3, 23, 43],
178                           [4, 24, 44]],
179                          [[5, 25, 45],
180                           [6, 26, 46],
181                           [7, 27, 47],
182                           [8, 28, 48],
183                           [9, 29, 49]],
184                          [[10, 30, 50],
185                           [11, 31, 51],
186                           [12, 32, 52],
187                           [13, 33, 53],
188                           [14, 34, 54]],
189                          [[15, 35, 55],
190                           [16, 36, 56],
191                           [17, 37, 57],
192                           [18, 38, 58],
193                           [19, 39, 59]]]],
194                        [[[[60, 80, 100],
195                           [61, 81, 101],
196                           [62, 82, 102],
197                           [63, 83, 103],
198                           [64, 84, 104]],
199                          [[65, 85, 105],
200                           [66, 86, 106],
201                           [67, 87, 107],
202                           [68, 88, 108],
203                           [69, 89, 109]],
204                          [[70, 90, 110],
205                           [71, 91, 111],
206                           [72, 92, 112],
207                           [73, 93, 113],
208                           [74, 94, 114]],
209                          [[75, 95, 115],
210                           [76, 96, 116],
211                           [77, 97, 117],
212                           [78, 98, 118],
213                           [79, 99, 119]]]]]]).astype(nptype)
214    assert (output.asnumpy() == expect).all()
215
216def transpose_d2(nptype):
217    context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
218    input_1 = Parameter(Tensor(np.arange(5 * 6).reshape(5, 6).astype(nptype)),
219                        name="input_1")
220    input_2 = Parameter(Tensor(np.arange(2 * 2 * 4).reshape(2, 2, 4).astype(nptype)),
221                        name="input_2")
222    perm_1 = (1, 0)
223    perm_2 = (1, 0, 2)
224    expect_1 = np.array([[[0, 6, 12, 18, 24],
225                          [1, 7, 13, 19, 25],
226                          [2, 8, 14, 20, 26],
227                          [3, 9, 15, 21, 27],
228                          [4, 10, 16, 22, 28],
229                          [5, 11, 17, 23, 29]]]).astype(nptype)
230    expect_2 = np.array([[[[0, 1, 2, 3],
231                           [8, 9, 10, 11]],
232                          [[4, 5, 6, 7],
233                           [12, 13, 14, 15]]]]).astype(nptype)
234    net = Transpose_dynamic2(input_1, input_2, perm_1, perm_2)
235    output_1, output_2 = net()
236    assert (output_1.asnumpy() == expect_1).all()
237    assert (output_2.asnumpy() == expect_2).all()
238
239@pytest.mark.level0
240@pytest.mark.platform_x86_gpu_training
241@pytest.mark.env_onecard
242def test_transpose_float32():
243    transpose1(np.float32)
244
245@pytest.mark.level0
246@pytest.mark.platform_x86_gpu_training
247@pytest.mark.env_onecard
248def test_transpose_float16():
249    transpose1(np.float16)
250
251@pytest.mark.level0
252@pytest.mark.platform_x86_gpu_training
253@pytest.mark.env_onecard
254def test_transpose_int32():
255    transpose1(np.int32)
256
257@pytest.mark.level0
258@pytest.mark.platform_x86_gpu_training
259@pytest.mark.env_onecard
260def test_transpose_int64():
261    transpose1(np.int64)
262
263@pytest.mark.level0
264@pytest.mark.platform_x86_gpu_training
265@pytest.mark.env_onecard
266def test_transpose_dynamic_int64():
267    transpose_d(np.int64)
268
269@pytest.mark.level0
270@pytest.mark.platform_x86_gpu_training
271@pytest.mark.env_onecard
272def test_transpose_dynamic_two_inputs_int64():
273    transpose_d2(np.int64)
274
275@pytest.mark.level0
276@pytest.mark.platform_x86_gpu_training
277@pytest.mark.env_onecard
278def test_transpose_dynamic_float32():
279    transpose_d(np.float32)
280
281@pytest.mark.level0
282@pytest.mark.platform_x86_gpu_training
283@pytest.mark.env_onecard
284def test_transpose_dynamic_float16():
285    transpose_d(np.float16)
286
287@pytest.mark.level0
288@pytest.mark.platform_x86_gpu_training
289@pytest.mark.env_onecard
290def test_transpose_dynamic_int32():
291    transpose_d(np.int32)
292
293@pytest.mark.level0
294@pytest.mark.platform_x86_gpu_training
295@pytest.mark.env_onecard
296def test_transpose_dynamic_two_inputs_float32():
297    transpose_d2(np.float32)
298
299@pytest.mark.level0
300@pytest.mark.platform_x86_gpu_training
301@pytest.mark.env_onecard
302def test_transpose_dynamic_two_inputs_float16():
303    transpose_d2(np.float16)
304
305@pytest.mark.level0
306@pytest.mark.platform_x86_gpu_training
307@pytest.mark.env_onecard
308def test_transpose_dynamic_two_inputs_int32():
309    transpose_d2(np.int32)
310