1 /**
2 * Copyright 2021 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 "src/common/config_file.h"
18
19 #ifdef _MSC_VER
20 #define PATH_MAX 1024
21 #endif
22 namespace {
23 constexpr size_t kLengthOfParentheses = 2;
24 }
25
26 namespace mindspore {
27 namespace lite {
GetSectionInfoFromConfigFile(const std::string & file,const std::string & section_name,std::map<std::string,std::string> * section_info)28 int GetSectionInfoFromConfigFile(const std::string &file, const std::string §ion_name,
29 std::map<std::string, std::string> *section_info) {
30 if (file.empty()) {
31 MS_LOG(ERROR) << "file is nullptr";
32 return RET_ERROR;
33 }
34 auto resolved_path = std::make_unique<char[]>(PATH_MAX);
35 if (resolved_path == nullptr) {
36 MS_LOG(ERROR) << "new resolved_path failed";
37 return RET_ERROR;
38 }
39
40 #ifdef _WIN32
41 char *real_path = _fullpath(resolved_path.get(), file.c_str(), MAX_CONFIG_FILE_LENGTH);
42 #else
43 char *real_path = realpath(file.c_str(), resolved_path.get());
44 #endif
45 if (real_path == nullptr || strlen(real_path) == 0) {
46 MS_LOG(ERROR) << "file path is not valid : " << file;
47 return RET_ERROR;
48 }
49 std::ifstream ifs(resolved_path.get());
50 if (!ifs.good()) {
51 MS_LOG(ERROR) << "file: " << real_path << " is not exist";
52 return RET_ERROR;
53 }
54 if (!ifs.is_open()) {
55 MS_LOG(ERROR) << "file: " << real_path << "open failed";
56 return RET_ERROR;
57 }
58 std::string line;
59
60 bool find_section = false;
61 while (std::getline(ifs, line)) {
62 lite::Trim(&line);
63 if (line.empty()) {
64 continue;
65 }
66 if (line[0] == '#') {
67 continue;
68 }
69
70 if (line[0] == '[') {
71 if (find_section == true) {
72 break;
73 }
74 std::string section = line.substr(1, line.length() - kLengthOfParentheses);
75 if (section != section_name) {
76 continue;
77 }
78 find_section = true;
79 }
80
81 if (find_section == true) {
82 auto index = line.find('=');
83 if (index == std::string::npos) {
84 continue;
85 }
86 auto key = line.substr(0, index);
87 auto value = line.substr(index + 1);
88 lite::Trim(&key);
89 lite::Trim(&value);
90 section_info->insert(std::make_pair(key, value));
91 }
92 }
93
94 ifs.close();
95 return RET_OK;
96 }
97
ParserExecutionPlan(const std::map<std::string,std::string> * config_infos,std::map<std::string,TypeId> * data_type_plan)98 void ParserExecutionPlan(const std::map<std::string, std::string> *config_infos,
99 std::map<std::string, TypeId> *data_type_plan) {
100 for (auto info : *config_infos) {
101 std::string op_name = info.first;
102 std::string value = info.second;
103 if (value[0] == '"' && value[value.length() - 1] == '"') {
104 value = value.substr(1, value.length() - kLengthOfParentheses);
105 }
106 auto index = value.find(':');
107 if (index == std::string::npos) {
108 MS_LOG(WARNING) << "Invalid info in execution_plan: " << value;
109 continue;
110 }
111 auto data_type_key = value.substr(0, index);
112 auto data_type_value = value.substr(index + 1);
113 if (data_type_key != "data_type") {
114 MS_LOG(WARNING) << "Invalid key in execution_plan: " << value;
115 continue;
116 }
117 TypeId type_id = kTypeUnknown;
118 if (data_type_value == "float32") {
119 type_id = kNumberTypeFloat32;
120 } else if (data_type_value == "float16") {
121 type_id = kNumberTypeFloat16;
122 } else {
123 MS_LOG(WARNING) << "Invalid value in execution_plan: " << value;
124 continue;
125 }
126 data_type_plan->insert(std::make_pair(op_name, type_id));
127 }
128 }
129 } // namespace lite
130 } // namespace mindspore
131