1 /**
2 * Copyright 2023 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 "kernel/format_utils.h"
18 #include <algorithm>
19 #include <map>
20 #include "utils/log_adapter.h"
21
22 namespace mindspore {
23 namespace kernel {
24 const mindspore::HashMap<std::string, Format> format_relation_map = {
25 {"DefaultFormat", Format::DEFAULT_FORMAT},
26 {"NCHW", Format::NCHW},
27 {"NHWC", Format::NHWC},
28 {"NHWC4", Format::NHWC4},
29 {"HWKC", Format::HWKC},
30 {"HWCK", Format::HWCK},
31 {"KCHW", Format::KCHW},
32 {"CKHW", Format::CKHW},
33 {"KHWC", Format::KHWC},
34 {"CHWK", Format::CHWK},
35 {"HW", Format::HW},
36 {"HW4", Format::HW4},
37 {"NC", Format::NC},
38 {"NC4", Format::NC4},
39 {"NC4HW4", Format::NC4HW4},
40 {"NCDHW", Format::NCDHW},
41 {"NWC", Format::NWC},
42 {"NCW", Format::NCW},
43 {"NDHWC", Format::NDHWC},
44 {"NC8HW8", Format::NC8HW8},
45 {"FRACTAL_NZ", Format::FRACTAL_NZ},
46 {"ND", Format::ND},
47 {"NC1HWC0", Format::NC1HWC0},
48 {"FRACTAL_Z", Format::FRACTAL_Z},
49 {"NC1C0HWPAD", Format::NC1C0HWPAD},
50 {"NHWC1C0", Format::NHWC1C0},
51 {"FSR_NCHW", Format::FSR_NCHW},
52 {"FRACTAL_DECONV", Format::FRACTAL_DECONV},
53 {"C1HWNC0", Format::C1HWNC0},
54 {"FRACTAL_DECONV_TRANSPOSE", Format::FRACTAL_DECONV_TRANSPOSE},
55 {"FRACTAL_DECONV_SP_STRIDE_TRANS", Format::FRACTAL_DECONV_SP_STRIDE_TRANS},
56 {"NC1HWC0_C04", Format::NC1HWC0_C04},
57 {"FRACTAL_Z_C04", Format::FRACTAL_Z_C04},
58 {"CHWN", Format::CHWN},
59 {"FRACTAL_DECONV_SP_STRIDE8_TRANS", Format::FRACTAL_DECONV_SP_STRIDE8_TRANS},
60 {"HWCN", Format::HWCN},
61 {"NC1KHKWHWC0", Format::NC1KHKWHWC0},
62 {"BN_WEIGHT", Format::BN_WEIGHT},
63 {"FILTER_HWCK", Format::FILTER_HWCK},
64 {"LOOKUP_LOOKUPS", Format::LOOKUP_LOOKUPS},
65 {"LOOKUP_KEYS", Format::LOOKUP_KEYS},
66 {"LOOKUP_VALUE", Format::LOOKUP_VALUE},
67 {"LOOKUP_OUTPUT", Format::LOOKUP_OUTPUT},
68 {"LOOKUP_HITS", Format::LOOKUP_HITS},
69 {"C1HWNCoC0", Format::C1HWNCoC0},
70 {"MD", Format::MD},
71 {"FRACTAL_ZZ", Format::FRACTAL_ZZ},
72 {"DHWCN", Format::DHWCN},
73 {"NDC1HWC0", Format::NDC1HWC0},
74 {"FRACTAL_Z_3D", Format::FRACTAL_Z_3D},
75 {"CN", Format::CN},
76 {"DHWNC", Format::DHWNC},
77 {"FRACTAL_Z_3D_TRANSPOSE", Format::FRACTAL_Z_3D_TRANSPOSE},
78 {"FRACTAL_ZN_LSTM", Format::FRACTAL_ZN_LSTM},
79 {"FRACTAL_Z_G", Format::FRACTAL_Z_G},
80 {"ND_RNN_BIAS", Format::ND_RNN_BIAS},
81 {"FRACTAL_ZN_RNN", Format::FRACTAL_ZN_RNN},
82 {"NYUV", Format::NYUV},
83 {"NYUV_A", Format::NYUV_A},
84 {"NCL", Format::NCL}};
85
GetFormatFromStrToEnum(const std::string & format_str)86 Format GetFormatFromStrToEnum(const std::string &format_str) {
87 auto iter = format_relation_map.find(format_str);
88 if (iter != format_relation_map.end()) {
89 return iter->second;
90 }
91 MS_LOG(DEBUG) << "The data format " << format_str << " can not be converted to enum.";
92 return Format::DEFAULT_FORMAT;
93 }
94
GetFormatFromEnumToStr(Format format)95 std::string GetFormatFromEnumToStr(Format format) {
96 const std::string format_str = "DefaultFormat";
97 auto iter = std::find_if(format_relation_map.begin(), format_relation_map.end(),
98 [format](auto item) { return item.second == format; });
99 if (iter != format_relation_map.end()) {
100 return iter->first;
101 }
102 MS_LOG(WARNING) << "The data format " << format << " can not be converted to string.";
103 return format_str;
104 }
105 } // namespace kernel
106 } // namespace mindspore
107