* @brief Function to parse a line of test.info file * Examples: * - "input, in/placeholder_32:0, TF_INT32, [3, 4, 2, 3]" * - "output, result:0, TF_FLOAT, []" */
| 141 | * - "output, result:0, TF_FLOAT, []" |
| 142 | */ |
| 143 | std::unique_ptr<ParsedTensor> parse_line(std::string &line) |
| 144 | { |
| 145 | // parsed data |
| 146 | ParsedTensor::Kind kind; |
| 147 | std::string name; |
| 148 | TF_DataType dtype; |
| 149 | std::vector<int32_t> shape; |
| 150 | |
| 151 | remove_comment(line); |
| 152 | |
| 153 | if (line.length() == 0) // empty line or line with comment |
| 154 | return nullptr; |
| 155 | |
| 156 | std::string tok, trimmed, dim; |
| 157 | |
| 158 | std::istringstream line_stream(line); |
| 159 | |
| 160 | CHECK_NOT_NULL(std::getline(line_stream, tok, ',')); // kind |
| 161 | kind = get_kind(trim(tok)); |
| 162 | |
| 163 | CHECK_NOT_NULL(std::getline(line_stream, tok, ',')); // tensor name |
| 164 | trimmed = trim(tok); |
| 165 | if (!validate_name(trimmed)) |
| 166 | throw oops::UserExn("Tensor name in wrong format", "name", tok); |
| 167 | name.assign(trimmed); |
| 168 | |
| 169 | CHECK_NOT_NULL(std::getline(line_stream, tok, ',')); // data type |
| 170 | dtype = get_dtype(trim(tok)); |
| 171 | |
| 172 | CHECK_NOT_NULL(std::getline(line_stream, tok, '[')); // start of shape |
| 173 | trimmed = trim(tok); |
| 174 | if (trimmed.length()) |
| 175 | throw oops::UserExn("Unknown token between data type and shape", "token", tok); |
| 176 | |
| 177 | CHECK_NOT_NULL(std::getline(line_stream, tok, ']')); |
| 178 | |
| 179 | std::istringstream shape_stream(tok); |
| 180 | |
| 181 | bool first = true; |
| 182 | while (std::getline(shape_stream, dim, ',')) // each dim |
| 183 | { |
| 184 | dim = trim(dim); |
| 185 | |
| 186 | if (first && dim.length() == 0) |
| 187 | continue; // scalar |
| 188 | first = false; |
| 189 | |
| 190 | if (dim.length() == 0) |
| 191 | throw oops::UserExn("Empty dim in shape", "shape", tok); |
| 192 | |
| 193 | if (!validate_num(dim)) |
| 194 | throw oops::UserExn("Dim in shape must be a number", "dim", dim); |
| 195 | |
| 196 | shape.emplace_back(std::stoi(dim)); |
| 197 | } |
| 198 | |
| 199 | return std::make_unique<ParsedTensor>(kind, name, dtype, shape); |
| 200 | } |