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.

trans_test.cc 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <vector>
  17. #include "common/common_test.h"
  18. #include "common/trans.h"
  19. #include "utils/utils.h"
  20. using namespace std;
  21. namespace mindspore {
  22. namespace trans {
  23. class FormatTransTest : public UT::Common {
  24. public:
  25. FormatTransTest() = default;
  26. void SetUp() override {}
  27. void TearDown() override {}
  28. };
  29. TEST_F(FormatTransTest, nchw_to_hwcn) {
  30. uint16_t data[2*2*2*2] = {12581,14220,14937,14302,
  31. 15004,14951,14694,14564,
  32. 14069,14554,10507,14787,
  33. 13016,15263,14872,10838};
  34. uint16_t res[2*2*2*2] = {12581,14069,15004,13016,
  35. 14220,14554,14951,15263,
  36. 14937,10507,14694,14872,
  37. 14302,14787,14564,10838};
  38. size_t device_size = 32;
  39. auto trans_tmp = std::vector<uint8_t>(device_size);
  40. FormatArgs format_args{data, device_size, kOpFormat_NCHW, kOpFormat_HWCN,
  41. {2, 2, 2, 2}, {2, 2, 2, 2}, kNumberTypeFloat16};
  42. EXPECT_EQ(trans::TransFormat(format_args, trans_tmp.data()), true);
  43. for (size_t i = 0; i < sizeof(res) / sizeof(res[0]); i++) {
  44. EXPECT_EQ((reinterpret_cast<uint16_t *>(trans_tmp.data()))[i], res[i]);
  45. }
  46. }
  47. TEST_F(FormatTransTest, hwcn_to_nchw) {
  48. uint16_t data[2*2*2*2] = {12581,14069,15004,13016,
  49. 14220,14554,14951,15263,
  50. 14937,10507,14694,14872,
  51. 14302,14787,14564,10838};
  52. uint16_t res[2*2*2*2] = {12581,14220,14937,14302,
  53. 15004,14951,14694,14564,
  54. 14069,14554,10507,14787,
  55. 13016,15263,14872,10838};
  56. size_t device_size = 32;
  57. auto trans_tmp = std::vector<uint8_t>(device_size);
  58. FormatArgs format_args{data, device_size, kOpFormat_NCHW, kOpFormat_HWCN,
  59. {2, 2, 2, 2}, {2, 2, 2, 2}, kNumberTypeFloat16};
  60. EXPECT_EQ(trans::TransFormatFromDeviceToHost(format_args, trans_tmp.data()), true);
  61. for (size_t i = 0; i < sizeof(res) / sizeof(res[0]); i++) {
  62. EXPECT_EQ((reinterpret_cast<uint16_t *>(trans_tmp.data()))[i], res[i]);
  63. }
  64. }
  65. TEST_F(FormatTransTest, nchw_to_nhwc) {
  66. uint16_t data[2*2*2*2] = {11750,13778,15007,15321,
  67. 15163,13446,15063,14467,
  68. 15056,13284,15219,14797,
  69. 12684,14288,14855,14799};
  70. uint16_t res[2*2*2*2] = {11750,15163,13778,13446,
  71. 15007,15063,15321,14467,
  72. 15056,12684,13284,14288,
  73. 15219,14855,14797,14799};
  74. size_t device_size = 32;
  75. auto trans_tmp = std::vector<uint8_t>(device_size);
  76. FormatArgs format_args{data, device_size, kOpFormat_NCHW, kOpFormat_NHWC,
  77. {2, 2, 2, 2}, {2, 2, 2, 2}, kNumberTypeFloat16};
  78. EXPECT_EQ(trans::TransFormat(format_args, trans_tmp.data()), true);
  79. for (size_t i = 0; i < sizeof(res) / sizeof(res[0]); i++) {
  80. EXPECT_EQ((reinterpret_cast<uint16_t *>(trans_tmp.data()))[i], res[i]);
  81. }
  82. }
  83. TEST_F(FormatTransTest, nhwc_to_nchw) {
  84. uint16_t data[2*2*2*2] = {11750,15163,13778,13446,
  85. 15007,15063,15321,14467,
  86. 15056,12684,13284,14288,
  87. 15219,14855,14797,14799};
  88. uint16_t res[2*2*2*2] = {11750,13778,15007,15321,
  89. 15163,13446,15063,14467,
  90. 15056,13284,15219,14797,
  91. 12684,14288,14855,14799};
  92. size_t device_size = 32;
  93. auto trans_tmp = std::vector<uint8_t>(device_size);
  94. FormatArgs format_args{data, device_size, kOpFormat_NCHW, kOpFormat_NHWC,
  95. {2, 2, 2, 2}, {2, 2, 2, 2}, kNumberTypeFloat16};
  96. EXPECT_EQ(trans::TransFormatFromDeviceToHost(format_args, trans_tmp.data()), true);
  97. for (size_t i = 0; i < sizeof(res) / sizeof(res[0]); i++) {
  98. EXPECT_EQ((reinterpret_cast<uint16_t *>(trans_tmp.data()))[i], res[i]);
  99. }
  100. }
  101. } // namespace trans
  102. } // namespace mindspore