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 17 #include <vector> 18 #include "common/common_test.h" 19 #include "common/trans.h" 20 #include "utils/utils.h" 21 22 using namespace std; 23 namespace mindspore { 24 namespace trans { 25 class FormatTransTest : public UT::Common { 26 public: 27 FormatTransTest() = default; 28 void SetUp() override {} 29 void TearDown() override {} 30 }; 31 32 TEST_F(FormatTransTest, nchw_to_hwcn) { 33 uint16_t data[2 * 2 * 2 * 2] = {12581, 14220, 14937, 14302, 15004, 14951, 14694, 14564, 34 14069, 14554, 10507, 14787, 13016, 15263, 14872, 10838}; 35 uint16_t res[2 * 2 * 2 * 2] = {12581, 14069, 15004, 13016, 14220, 14554, 14951, 15263, 36 14937, 10507, 14694, 14872, 14302, 14787, 14564, 10838}; 37 size_t device_size = 32; 38 auto trans_tmp = std::vector<uint8_t>(device_size); 39 FormatArgs format_args{data, device_size, kOpFormat_NCHW, kOpFormat_HWCN, 40 {2, 2, 2, 2}, {2, 2, 2, 2}, kNumberTypeFloat16}; 41 EXPECT_EQ(trans::TransFormat(format_args, trans_tmp.data()), true); 42 for (size_t i = 0; i < sizeof(res) / sizeof(res[0]); i++) { 43 EXPECT_EQ((reinterpret_cast<uint16_t *>(trans_tmp.data()))[i], res[i]); 44 } 45 } 46 47 TEST_F(FormatTransTest, hwcn_to_nchw) { 48 uint16_t data[2 * 2 * 2 * 2] = {12581, 14069, 15004, 13016, 14220, 14554, 14951, 15263, 49 14937, 10507, 14694, 14872, 14302, 14787, 14564, 10838}; 50 51 uint16_t res[2 * 2 * 2 * 2] = {12581, 14220, 14937, 14302, 15004, 14951, 14694, 14564, 52 14069, 14554, 10507, 14787, 13016, 15263, 14872, 10838}; 53 54 size_t device_size = 32; 55 auto trans_tmp = std::vector<uint8_t>(device_size); 56 FormatArgs format_args{data, device_size, kOpFormat_NCHW, kOpFormat_HWCN, 57 {2, 2, 2, 2}, {2, 2, 2, 2}, kNumberTypeFloat16}; 58 EXPECT_EQ(trans::TransFormatFromDeviceToHost(format_args, trans_tmp.data()), true); 59 for (size_t i = 0; i < sizeof(res) / sizeof(res[0]); i++) { 60 EXPECT_EQ((reinterpret_cast<uint16_t *>(trans_tmp.data()))[i], res[i]); 61 } 62 } 63 64 TEST_F(FormatTransTest, nchw_to_nhwc) { 65 uint16_t data[2 * 2 * 2 * 2] = {11750, 13778, 15007, 15321, 15163, 13446, 15063, 14467, 66 15056, 13284, 15219, 14797, 12684, 14288, 14855, 14799}; 67 uint16_t res[2 * 2 * 2 * 2] = {11750, 15163, 13778, 13446, 15007, 15063, 15321, 14467, 68 15056, 12684, 13284, 14288, 15219, 14855, 14797, 14799}; 69 size_t device_size = 32; 70 auto trans_tmp = std::vector<uint8_t>(device_size); 71 FormatArgs format_args{data, device_size, kOpFormat_NCHW, kOpFormat_NHWC, 72 {2, 2, 2, 2}, {2, 2, 2, 2}, kNumberTypeFloat16}; 73 EXPECT_EQ(trans::TransFormat(format_args, trans_tmp.data()), true); 74 for (size_t i = 0; i < sizeof(res) / sizeof(res[0]); i++) { 75 EXPECT_EQ((reinterpret_cast<uint16_t *>(trans_tmp.data()))[i], res[i]); 76 } 77 } 78 79 TEST_F(FormatTransTest, nhwc_to_nchw) { 80 uint16_t data[2 * 2 * 2 * 2] = {11750, 15163, 13778, 13446, 15007, 15063, 15321, 14467, 81 15056, 12684, 13284, 14288, 15219, 14855, 14797, 14799}; 82 uint16_t res[2 * 2 * 2 * 2] = {11750, 13778, 15007, 15321, 15163, 13446, 15063, 14467, 83 15056, 13284, 15219, 14797, 12684, 14288, 14855, 14799}; 84 85 size_t device_size = 32; 86 auto trans_tmp = std::vector<uint8_t>(device_size); 87 FormatArgs format_args{data, device_size, kOpFormat_NCHW, kOpFormat_NHWC, 88 {2, 2, 2, 2}, {2, 2, 2, 2}, kNumberTypeFloat16}; 89 EXPECT_EQ(trans::TransFormatFromDeviceToHost(format_args, trans_tmp.data()), true); 90 for (size_t i = 0; i < sizeof(res) / sizeof(res[0]); i++) { 91 EXPECT_EQ((reinterpret_cast<uint16_t *>(trans_tmp.data()))[i], res[i]); 92 } 93 } 94 95 class ShapeTransTest : public UT::Common { 96 public: 97 ShapeTransTest() = default; 98 void SetUp() override {} 99 void TearDown() override {} 100 }; 101 102 TEST_F(ShapeTransTest, fraczn_rnn_device_shape) { 103 std::vector<size_t> host_shape = {43, 120}; 104 std::string format = kOpFormat_FRACTAL_ZN_RNN; 105 std::vector<int64_t> input_hidden_size = {13, 30}; 106 auto trans_shape = trans::TransShapeToDevice(host_shape, format, 1, input_hidden_size); 107 const std::vector<size_t> expect_shape = {3, 8, 16, 16}; 108 EXPECT_EQ(trans_shape.size(), expect_shape.size()); 109 for (size_t i = 0; i < expect_shape.size(); i++) { 110 EXPECT_EQ(trans_shape[i], expect_shape[i]); 111 } 112 } 113 114 TEST_F(ShapeTransTest, nd_rnn_bias_device_shape) { 115 std::vector<size_t> host_shape = {120}; 116 std::string format = kOpFormat_ND_RNN_BIAS; 117 std::vector<int64_t> input_hidden_size = {13, 30}; 118 auto trans_shape = trans::TransShapeToDevice(host_shape, format, 1, input_hidden_size); 119 std::vector<size_t> expect_shape = {128}; 120 EXPECT_EQ(trans_shape.size(), expect_shape.size()); 121 for (size_t i = 0; i < expect_shape.size(); i++) { 122 EXPECT_EQ(trans_shape[i], expect_shape[i]); 123 } 124 } 125 126 TEST_F(ShapeTransTest, fraczn_rnn_dynamic_device_shape) { 127 std::vector<int64_t> host_shape = {-1, -1}; 128 std::string format = kOpFormat_FRACTAL_ZN_RNN; 129 std::vector<int64_t> input_hidden_size = {13, 30}; 130 auto trans_shape = trans::TransShapeToDevice(host_shape, format, 1, input_hidden_size); 131 const std::vector<int64_t> expect_shape = {-1, -1, 16, 16}; 132 EXPECT_EQ(trans_shape.size(), expect_shape.size()); 133 for (size_t i = 0; i < expect_shape.size(); i++) { 134 EXPECT_EQ(trans_shape[i], expect_shape[i]); 135 } 136 } 137 138 TEST_F(ShapeTransTest, nd_rnn_bias_dynamic_device_shape) { 139 std::vector<int64_t> host_shape = {-1}; 140 std::string format = kOpFormat_ND_RNN_BIAS; 141 std::vector<int64_t> input_hidden_size = {13, 30}; 142 auto trans_shape = trans::TransShapeToDevice(host_shape, format, 1, input_hidden_size); 143 std::vector<int64_t> expect_shape = {-1}; 144 EXPECT_EQ(trans_shape.size(), expect_shape.size()); 145 for (size_t i = 0; i < expect_shape.size(); i++) { 146 EXPECT_EQ(trans_shape[i], expect_shape[i]); 147 } 148 } 149 } // namespace trans 150 } // namespace mindspore 151