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_transpose_op.py 6.0 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Copyright 2019 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. from mindspore import Tensor
  20. from mindspore.common.api import ms_function
  21. from mindspore.common.initializer import initializer
  22. from mindspore.common.parameter import Parameter
  23. from mindspore.ops import operations as P
  24. context.set_context(device_target='GPU')
  25. class Transpose(nn.Cell):
  26. def __init__(self):
  27. super(Transpose, self).__init__()
  28. self.transpose = P.Transpose()
  29. self.x_2D = Parameter(initializer(Tensor(np.arange(5 * 6).reshape(5, 6).astype(np.float32)), [5, 6]),
  30. name='x_2D')
  31. self.perm_2D = (1, 0)
  32. self.x_3D = Parameter(initializer(Tensor(np.arange(2 * 2 * 4).reshape(2, 2, 4).astype(np.float32)), [2, 2, 4]),
  33. name='x_3D')
  34. self.perm_3D = (1, 0, 2)
  35. self.x_4D = Parameter(
  36. initializer(Tensor(np.arange(2 * 3 * 4 * 5).reshape(2, 3, 4, 5).astype(np.float32)), [2, 3, 4, 5]),
  37. name='x_4D')
  38. self.perm_4D = (0, 1, 2, 3)
  39. self.x_5D = Parameter(
  40. initializer(Tensor(np.arange(1 * 2 * 3 * 4 * 5).reshape(1, 2, 3, 4, 5).astype(np.float32)),
  41. [1, 2, 3, 4, 5]), name='x_5D')
  42. self.perm_5D = (1, 0, 3, 4, 2)
  43. @ms_function
  44. def construct(self):
  45. return (self.transpose(self.x_2D, self.perm_2D), self.transpose(self.x_3D, self.perm_3D),
  46. self.transpose(self.x_4D, self.perm_4D), self.transpose(self.x_5D, self.perm_5D))
  47. @pytest.mark.level0
  48. @pytest.mark.platform_x86_gpu_training
  49. @pytest.mark.env_onecard
  50. def test_transpose():
  51. transpose = Transpose()
  52. output = transpose()
  53. expect0 = np.array([[[0, 6, 12, 18, 24],
  54. [1, 7, 13, 19, 25],
  55. [2, 8, 14, 20, 26],
  56. [3, 9, 15, 21, 27],
  57. [4, 10, 16, 22, 28],
  58. [5, 11, 17, 23, 29]]]).astype(np.float32)
  59. expect1 = np.array([[[[0, 1, 2, 3],
  60. [8, 9, 10, 11]],
  61. [[4, 5, 6, 7],
  62. [12, 13, 14, 15]]]]).astype(np.float32)
  63. expect2 = np.array([[[[[0, 1, 2, 3, 4],
  64. [5, 6, 7, 8, 9],
  65. [10, 11, 12, 13, 14],
  66. [15, 16, 17, 18, 19]],
  67. [[20, 21, 22, 23, 24],
  68. [25, 26, 27, 28, 29],
  69. [30, 31, 32, 33, 34],
  70. [35, 36, 37, 38, 39]],
  71. [[40, 41, 42, 43, 44],
  72. [45, 46, 47, 48, 49],
  73. [50, 51, 52, 53, 54],
  74. [55, 56, 57, 58, 59]]],
  75. [[[60, 61, 62, 63, 64],
  76. [65, 66, 67, 68, 69],
  77. [70, 71, 72, 73, 74],
  78. [75, 76, 77, 78, 79]],
  79. [[80, 81, 82, 83, 84],
  80. [85, 86, 87, 88, 89],
  81. [90, 91, 92, 93, 94],
  82. [95, 96, 97, 98, 99]],
  83. [[100, 101, 102, 103, 104],
  84. [105, 106, 107, 108, 109],
  85. [110, 111, 112, 113, 114],
  86. [115, 116, 117, 118, 119]]]]]).astype(np.float32)
  87. expect3 = np.array([[[[[[0, 20, 40],
  88. [1, 21, 41],
  89. [2, 22, 42],
  90. [3, 23, 43],
  91. [4, 24, 44]],
  92. [[5, 25, 45],
  93. [6, 26, 46],
  94. [7, 27, 47],
  95. [8, 28, 48],
  96. [9, 29, 49]],
  97. [[10, 30, 50],
  98. [11, 31, 51],
  99. [12, 32, 52],
  100. [13, 33, 53],
  101. [14, 34, 54]],
  102. [[15, 35, 55],
  103. [16, 36, 56],
  104. [17, 37, 57],
  105. [18, 38, 58],
  106. [19, 39, 59]]]],
  107. [[[[60, 80, 100],
  108. [61, 81, 101],
  109. [62, 82, 102],
  110. [63, 83, 103],
  111. [64, 84, 104]],
  112. [[65, 85, 105],
  113. [66, 86, 106],
  114. [67, 87, 107],
  115. [68, 88, 108],
  116. [69, 89, 109]],
  117. [[70, 90, 110],
  118. [71, 91, 111],
  119. [72, 92, 112],
  120. [73, 93, 113],
  121. [74, 94, 114]],
  122. [[75, 95, 115],
  123. [76, 96, 116],
  124. [77, 97, 117],
  125. [78, 98, 118],
  126. [79, 99, 119]]]]]]).astype(np.float32)
  127. assert (output[0].asnumpy() == expect0).all()
  128. assert (output[1].asnumpy() == expect1).all()
  129. assert (output[2].asnumpy() == expect2).all()
  130. assert (output[3].asnumpy() == expect3).all()