| 181 | } |
| 182 | |
| 183 | void DataParser::parse_string(const std::string& name, const std::string& str) { |
| 184 | //! parse shape |
| 185 | if ('{' == str[0]) { |
| 186 | parse_shape(name, str); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | // data type |
| 191 | megdnn::DType data_type = mgb::dtype::Int32(); |
| 192 | if (str.find(".") != std::string::npos or str.find(".") != std::string::npos) { |
| 193 | data_type = mgb::dtype::Float32(); |
| 194 | } |
| 195 | // shape |
| 196 | size_t number_cnt = 0; |
| 197 | |
| 198 | std::shared_ptr<Brace> brace_root = std::make_shared<Brace>(); |
| 199 | std::shared_ptr<Brace> cur = brace_root; |
| 200 | for (size_t i = 0; i < str.size(); ++i) { |
| 201 | char c = str[i]; |
| 202 | if (c == '[') { |
| 203 | std::shared_ptr<Brace> child = std::make_shared<Brace>(); |
| 204 | child->parent = cur; |
| 205 | cur->chidren.emplace_back(child); |
| 206 | cur = child; |
| 207 | } else if (c == ']') { |
| 208 | cur = cur->parent.lock(); |
| 209 | } else if (c == ',') { |
| 210 | number_cnt++; |
| 211 | } |
| 212 | continue; |
| 213 | } |
| 214 | ++number_cnt; |
| 215 | |
| 216 | mgb_assert(cur == brace_root, "braces not closed for --input"); |
| 217 | megdnn::SmallVector<size_t> shape; |
| 218 | cur = brace_root; |
| 219 | while (not cur->chidren.empty()) { |
| 220 | shape.append({cur->chidren.size()}); |
| 221 | number_cnt /= cur->chidren.size(); |
| 222 | cur = cur->chidren[0]; |
| 223 | } |
| 224 | mgb_assert(number_cnt > 0); |
| 225 | shape.append({number_cnt}); |
| 226 | |
| 227 | // data |
| 228 | std::string json_arr; |
| 229 | for (size_t i = 0; i < str.size(); ++i) { |
| 230 | char c = str[i]; |
| 231 | if (c != '[' and c != ']') { |
| 232 | json_arr += c; |
| 233 | } |
| 234 | } |
| 235 | json_arr = "[" + json_arr + "]"; |
| 236 | |
| 237 | // reuse json parser to resolve raw data |
| 238 | mgb::JsonLoader json; |
| 239 | std::shared_ptr<mgb::JsonLoader::Value> json_root = |
| 240 | json.load(json_arr.data(), json_arr.size()); |
nothing calls this directly
no test coverage detected