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# ============================================================================ 15 16import pytest 17import numpy as np 18from mindspore import Tensor 19from mindspore.ops import operations as P 20from mindspore.common.api import ms_function 21from mindspore.common.initializer import initializer 22from mindspore.common.parameter import Parameter 23import mindspore.nn as nn 24import mindspore.context as context 25 26context.set_context(mode=context.PYNATIVE_MODE, device_target='CPU') 27 28 29class Transpose(nn.Cell): 30 def __init__(self): 31 super(Transpose, self).__init__() 32 self.transpose = P.Transpose() 33 34 self.x_2D = Parameter(initializer(Tensor(np.arange(5 * 6).reshape(5, 6).astype(np.float32)), [5, 6]), 35 name='x_2D') 36 self.perm_2D = (1, 0) 37 38 self.x_3D = Parameter(initializer(Tensor(np.arange(2 * 2 * 4).reshape(2, 2, 4).astype(np.float32)), [2, 2, 4]), 39 name='x_3D') 40 self.perm_3D = (1, 0, 2) 41 42 self.x_4D = Parameter( 43 initializer(Tensor(np.arange(2 * 3 * 4 * 5).reshape(2, 44 3, 4, 5).astype(np.float32)), [2, 3, 4, 5]), 45 name='x_4D') 46 self.perm_4D = (0, 1, 2, 3) 47 48 self.x_5D = Parameter( 49 initializer(Tensor(np.arange(1 * 2 * 3 * 4 * 5).reshape(1, 2, 3, 4, 5).astype(np.float32)), 50 [1, 2, 3, 4, 5]), name='x_5D') 51 self.perm_5D = (1, 0, 3, 4, 2) 52 53 @ms_function 54 def construct(self): 55 return (self.transpose(self.x_2D, self.perm_2D), self.transpose(self.x_3D, self.perm_3D), 56 self.transpose(self.x_4D, self.perm_4D), self.transpose(self.x_5D, self.perm_5D)) 57 58 59@pytest.mark.level0 60@pytest.mark.platform_x86_cpu 61@pytest.mark.env_onecard 62def test_transpose(): 63 transpose = Transpose() 64 output = transpose() 65 66 expect0 = np.array([[[0, 6, 12, 18, 24], 67 [1, 7, 13, 19, 25], 68 [2, 8, 14, 20, 26], 69 [3, 9, 15, 21, 27], 70 [4, 10, 16, 22, 28], 71 [5, 11, 17, 23, 29]]]).astype(np.float32) 72 expect1 = np.array([[[[0, 1, 2, 3], 73 [8, 9, 10, 11]], 74 [[4, 5, 6, 7], 75 [12, 13, 14, 15]]]]).astype(np.float32) 76 expect2 = np.array([[[[[0, 1, 2, 3, 4], 77 [5, 6, 7, 8, 9], 78 [10, 11, 12, 13, 14], 79 [15, 16, 17, 18, 19]], 80 [[20, 21, 22, 23, 24], 81 [25, 26, 27, 28, 29], 82 [30, 31, 32, 33, 34], 83 [35, 36, 37, 38, 39]], 84 [[40, 41, 42, 43, 44], 85 [45, 46, 47, 48, 49], 86 [50, 51, 52, 53, 54], 87 [55, 56, 57, 58, 59]]], 88 89 [[[60, 61, 62, 63, 64], 90 [65, 66, 67, 68, 69], 91 [70, 71, 72, 73, 74], 92 [75, 76, 77, 78, 79]], 93 [[80, 81, 82, 83, 84], 94 [85, 86, 87, 88, 89], 95 [90, 91, 92, 93, 94], 96 [95, 96, 97, 98, 99]], 97 [[100, 101, 102, 103, 104], 98 [105, 106, 107, 108, 109], 99 [110, 111, 112, 113, 114], 100 [115, 116, 117, 118, 119]]]]]).astype(np.float32) 101 expect3 = np.array([[[[[[0, 20, 40], 102 [1, 21, 41], 103 [2, 22, 42], 104 [3, 23, 43], 105 [4, 24, 44]], 106 [[5, 25, 45], 107 [6, 26, 46], 108 [7, 27, 47], 109 [8, 28, 48], 110 [9, 29, 49]], 111 [[10, 30, 50], 112 [11, 31, 51], 113 [12, 32, 52], 114 [13, 33, 53], 115 [14, 34, 54]], 116 [[15, 35, 55], 117 [16, 36, 56], 118 [17, 37, 57], 119 [18, 38, 58], 120 [19, 39, 59]]]], 121 122 [[[[60, 80, 100], 123 [61, 81, 101], 124 [62, 82, 102], 125 [63, 83, 103], 126 [64, 84, 104]], 127 [[65, 85, 105], 128 [66, 86, 106], 129 [67, 87, 107], 130 [68, 88, 108], 131 [69, 89, 109]], 132 [[70, 90, 110], 133 [71, 91, 111], 134 [72, 92, 112], 135 [73, 93, 113], 136 [74, 94, 114]], 137 [[75, 95, 115], 138 [76, 96, 116], 139 [77, 97, 117], 140 [78, 98, 118], 141 [79, 99, 119]]]]]]).astype(np.float32) 142 assert (output[0].asnumpy() == expect0).all() 143 assert (output[1].asnumpy() == expect1).all() 144 assert (output[2].asnumpy() == expect2).all() 145 assert (output[3].asnumpy() == expect3).all() 146 147 148test_transpose() 149 150 151class Transpose_int64(nn.Cell): 152 def __init__(self): 153 super(Transpose_int64, self).__init__() 154 self.transpose = P.Transpose() 155 156 self.x_2D = Parameter(initializer(Tensor(np.arange(5 * 6).reshape(5, 6).astype(np.int64)), [5, 6]), 157 name='x_2D') 158 self.perm_2D = (1, 0) 159 160 self.x_3D = Parameter(initializer(Tensor(np.arange(2 * 2 * 4).reshape(2, 2, 4).astype(np.int64)), [2, 2, 4]), 161 name='x_3D') 162 self.perm_3D = (1, 0, 2) 163 164 self.x_4D = Parameter( 165 initializer(Tensor(np.arange(2 * 3 * 4 * 5).reshape(2, 166 3, 4, 5).astype(np.int64)), [2, 3, 4, 5]), 167 name='x_4D') 168 self.perm_4D = (0, 1, 2, 3) 169 170 self.x_5D = Parameter( 171 initializer(Tensor(np.arange(1 * 2 * 3 * 4 * 5).reshape(1, 2, 3, 4, 5).astype(np.int64)), 172 [1, 2, 3, 4, 5]), name='x_5D') 173 self.perm_5D = (1, 0, 3, 4, 2) 174 175 @ms_function 176 def construct(self): 177 return (self.transpose(self.x_2D, self.perm_2D), self.transpose(self.x_3D, self.perm_3D), 178 self.transpose(self.x_4D, self.perm_4D), self.transpose(self.x_5D, self.perm_5D)) 179 180 181@pytest.mark.level0 182@pytest.mark.platform_x86_cpu 183@pytest.mark.env_onecard 184def test_transpose_int64(): 185 transpose = Transpose_int64() 186 output = transpose() 187 188 expect0 = np.array([[[0, 6, 12, 18, 24], 189 [1, 7, 13, 19, 25], 190 [2, 8, 14, 20, 26], 191 [3, 9, 15, 21, 27], 192 [4, 10, 16, 22, 28], 193 [5, 11, 17, 23, 29]]]).astype(np.int64) 194 expect1 = np.array([[[[0, 1, 2, 3], 195 [8, 9, 10, 11]], 196 [[4, 5, 6, 7], 197 [12, 13, 14, 15]]]]).astype(np.int64) 198 expect2 = np.array([[[[[0, 1, 2, 3, 4], 199 [5, 6, 7, 8, 9], 200 [10, 11, 12, 13, 14], 201 [15, 16, 17, 18, 19]], 202 [[20, 21, 22, 23, 24], 203 [25, 26, 27, 28, 29], 204 [30, 31, 32, 33, 34], 205 [35, 36, 37, 38, 39]], 206 [[40, 41, 42, 43, 44], 207 [45, 46, 47, 48, 49], 208 [50, 51, 52, 53, 54], 209 [55, 56, 57, 58, 59]]], 210 211 [[[60, 61, 62, 63, 64], 212 [65, 66, 67, 68, 69], 213 [70, 71, 72, 73, 74], 214 [75, 76, 77, 78, 79]], 215 [[80, 81, 82, 83, 84], 216 [85, 86, 87, 88, 89], 217 [90, 91, 92, 93, 94], 218 [95, 96, 97, 98, 99]], 219 [[100, 101, 102, 103, 104], 220 [105, 106, 107, 108, 109], 221 [110, 111, 112, 113, 114], 222 [115, 116, 117, 118, 119]]]]]).astype(np.int64) 223 expect3 = np.array([[[[[[0, 20, 40], 224 [1, 21, 41], 225 [2, 22, 42], 226 [3, 23, 43], 227 [4, 24, 44]], 228 [[5, 25, 45], 229 [6, 26, 46], 230 [7, 27, 47], 231 [8, 28, 48], 232 [9, 29, 49]], 233 [[10, 30, 50], 234 [11, 31, 51], 235 [12, 32, 52], 236 [13, 33, 53], 237 [14, 34, 54]], 238 [[15, 35, 55], 239 [16, 36, 56], 240 [17, 37, 57], 241 [18, 38, 58], 242 [19, 39, 59]]]], 243 244 [[[[60, 80, 100], 245 [61, 81, 101], 246 [62, 82, 102], 247 [63, 83, 103], 248 [64, 84, 104]], 249 [[65, 85, 105], 250 [66, 86, 106], 251 [67, 87, 107], 252 [68, 88, 108], 253 [69, 89, 109]], 254 [[70, 90, 110], 255 [71, 91, 111], 256 [72, 92, 112], 257 [73, 93, 113], 258 [74, 94, 114]], 259 [[75, 95, 115], 260 [76, 96, 116], 261 [77, 97, 117], 262 [78, 98, 118], 263 [79, 99, 119]]]]]]).astype(np.int64) 264 assert (output[0].asnumpy() == expect0).all() 265 assert (output[1].asnumpy() == expect1).all() 266 assert (output[2].asnumpy() == expect2).all() 267 assert (output[3].asnumpy() == expect3).all() 268 269 270test_transpose_int64() 271 272 273class Transpose_uint8(nn.Cell): 274 def __init__(self): 275 super(Transpose_uint8, self).__init__() 276 self.transpose = P.Transpose() 277 278 self.x_2D = Parameter(initializer(Tensor(np.arange(5 * 6).reshape(5, 6).astype(np.uint8)), [5, 6]), 279 name='x_2D') 280 self.perm_2D = (1, 0) 281 282 self.x_3D = Parameter(initializer(Tensor(np.arange(2 * 2 * 4).reshape(2, 2, 4).astype(np.uint8)), [2, 2, 4]), 283 name='x_3D') 284 self.perm_3D = (1, 0, 2) 285 286 self.x_4D = Parameter( 287 initializer(Tensor(np.arange(2 * 3 * 4 * 5).reshape(2, 288 3, 4, 5).astype(np.uint8)), [2, 3, 4, 5]), 289 name='x_4D') 290 self.perm_4D = (0, 1, 2, 3) 291 292 self.x_5D = Parameter( 293 initializer(Tensor(np.arange(1 * 2 * 3 * 4 * 5).reshape(1, 2, 3, 4, 5).astype(np.uint8)), 294 [1, 2, 3, 4, 5]), name='x_5D') 295 self.perm_5D = (1, 0, 3, 4, 2) 296 297 @ms_function 298 def construct(self): 299 return (self.transpose(self.x_2D, self.perm_2D), self.transpose(self.x_3D, self.perm_3D), 300 self.transpose(self.x_4D, self.perm_4D), self.transpose(self.x_5D, self.perm_5D)) 301 302 303@pytest.mark.level0 304@pytest.mark.platform_x86_cpu 305@pytest.mark.env_onecard 306def test_transpose_uint8(): 307 transpose = Transpose_uint8() 308 output = transpose() 309 310 expect0 = np.array([[[0, 6, 12, 18, 24], 311 [1, 7, 13, 19, 25], 312 [2, 8, 14, 20, 26], 313 [3, 9, 15, 21, 27], 314 [4, 10, 16, 22, 28], 315 [5, 11, 17, 23, 29]]]).astype(np.uint8) 316 expect1 = np.array([[[[0, 1, 2, 3], 317 [8, 9, 10, 11]], 318 [[4, 5, 6, 7], 319 [12, 13, 14, 15]]]]).astype(np.uint8) 320 expect2 = np.array([[[[[0, 1, 2, 3, 4], 321 [5, 6, 7, 8, 9], 322 [10, 11, 12, 13, 14], 323 [15, 16, 17, 18, 19]], 324 [[20, 21, 22, 23, 24], 325 [25, 26, 27, 28, 29], 326 [30, 31, 32, 33, 34], 327 [35, 36, 37, 38, 39]], 328 [[40, 41, 42, 43, 44], 329 [45, 46, 47, 48, 49], 330 [50, 51, 52, 53, 54], 331 [55, 56, 57, 58, 59]]], 332 333 [[[60, 61, 62, 63, 64], 334 [65, 66, 67, 68, 69], 335 [70, 71, 72, 73, 74], 336 [75, 76, 77, 78, 79]], 337 [[80, 81, 82, 83, 84], 338 [85, 86, 87, 88, 89], 339 [90, 91, 92, 93, 94], 340 [95, 96, 97, 98, 99]], 341 [[100, 101, 102, 103, 104], 342 [105, 106, 107, 108, 109], 343 [110, 111, 112, 113, 114], 344 [115, 116, 117, 118, 119]]]]]).astype(np.uint8) 345 expect3 = np.array([[[[[[0, 20, 40], 346 [1, 21, 41], 347 [2, 22, 42], 348 [3, 23, 43], 349 [4, 24, 44]], 350 [[5, 25, 45], 351 [6, 26, 46], 352 [7, 27, 47], 353 [8, 28, 48], 354 [9, 29, 49]], 355 [[10, 30, 50], 356 [11, 31, 51], 357 [12, 32, 52], 358 [13, 33, 53], 359 [14, 34, 54]], 360 [[15, 35, 55], 361 [16, 36, 56], 362 [17, 37, 57], 363 [18, 38, 58], 364 [19, 39, 59]]]], 365 366 [[[[60, 80, 100], 367 [61, 81, 101], 368 [62, 82, 102], 369 [63, 83, 103], 370 [64, 84, 104]], 371 [[65, 85, 105], 372 [66, 86, 106], 373 [67, 87, 107], 374 [68, 88, 108], 375 [69, 89, 109]], 376 [[70, 90, 110], 377 [71, 91, 111], 378 [72, 92, 112], 379 [73, 93, 113], 380 [74, 94, 114]], 381 [[75, 95, 115], 382 [76, 96, 116], 383 [77, 97, 117], 384 [78, 98, 118], 385 [79, 99, 119]]]]]]).astype(np.uint8) 386 assert (output[0].asnumpy() == expect0).all() 387 assert (output[1].asnumpy() == expect1).all() 388 assert (output[2].asnumpy() == expect2).all() 389 assert (output[3].asnumpy() == expect3).all() 390 391 392test_transpose_uint8() 393