| 239 | } |
| 240 | |
| 241 | bool WeightTable::load(const std::string& pack_dir, void* arena_base, uint64_t arena_bytes, std::string& err, |
| 242 | const std::set<std::string>* skip) { |
| 243 | const std::string path = pack_dir + "/index.txt"; |
| 244 | std::FILE* idx = std::fopen(path.c_str(), "rb"); |
| 245 | if (!idx) { err = "cannot open " + path + " (run tools/pack_index.py)"; return false; } |
| 246 | |
| 247 | uint64_t pool = 0; |
| 248 | int align = 0; |
| 249 | // The index's own version, from its first line. v4 adds ne2 after ne1; a v3 file has no such column and its |
| 250 | // tensors are all two-dimensional. A version this engine does not know may have columns it would misread, |
| 251 | // so it is refused rather than parsed as far as the known ones go - the same rule native_experts.txt uses. |
| 252 | int version = 3; |
| 253 | std::vector<IndexRow> rows; |
| 254 | char line[1024]; |
| 255 | while (std::fgets(line, sizeof line, idx)) { |
| 256 | if (line[0] == '#') { |
| 257 | unsigned long long p = 0; |
| 258 | int a = 0, t = 0, v = 0; |
| 259 | if (std::sscanf(line, "# strata pack index v%d", &v) == 1) { |
| 260 | if (v < 3 || v > kPackIndexVersion) { |
| 261 | err = "index.txt is v" + std::to_string(v) + "; this engine reads v3 to v" + |
| 262 | std::to_string(kPackIndexVersion) + " (repack with tools/iq_pack.py)"; |
| 263 | std::fclose(idx); |
| 264 | return false; |
| 265 | } |
| 266 | version = v; |
| 267 | } |
| 268 | if (std::sscanf(line, "# align %d pool %llu tensors %d", &a, &p, &t) == 3) { pool = p; align = a; } |
| 269 | continue; |
| 270 | } |
| 271 | IndexRow r; |
| 272 | char name[256] = {0}; |
| 273 | int kind = 0; |
| 274 | // The format is written by tools/pack_index.py and is space-separated with no quoting; a name with a |
| 275 | // space in it would break the parse, which is why the parser REFUSES rather than taking what it got. |
| 276 | const int want = version >= 4 ? 20 : 19; |
| 277 | const int n = version >= 4 |
| 278 | ? std::sscanf(line, |
| 279 | "%255s %d %d %" SCNu64 " %" SCNu64 " %" SCNu64 " %" SCNu64 " %" SCNd64 " %" SCNd64 " %" SCNd64 " %d %d %d %d %d %" SCNu64 " %" SCNu64 " %" SCNu64 " %d %d", |
| 280 | name, &r.file, &kind, &r.src_off, &r.src_bytes, &r.dst_off, &r.dst_bytes, |
| 281 | &r.ne0, &r.ne1, &r.ne2, &r.code_bits, &r.code_bias, &r.group_elems, &r.codebook, |
| 282 | &r.has_offset, &r.codes_bytes, &r.scales_bytes, &r.offset_bytes, |
| 283 | &r.scales_fp16, &r.act_kind) |
| 284 | : std::sscanf(line, |
| 285 | "%255s %d %d %" SCNu64 " %" SCNu64 " %" SCNu64 " %" SCNu64 " %" SCNd64 " %" SCNd64 " %d %d %d %d %d %" SCNu64 " %" SCNu64 " %" SCNu64 " %d %d", |
| 286 | name, &r.file, &kind, &r.src_off, &r.src_bytes, &r.dst_off, &r.dst_bytes, |
| 287 | &r.ne0, &r.ne1, &r.code_bits, &r.code_bias, &r.group_elems, &r.codebook, |
| 288 | &r.has_offset, &r.codes_bytes, &r.scales_bytes, &r.offset_bytes, |
| 289 | &r.scales_fp16, &r.act_kind); |
| 290 | if (n != want) { |
| 291 | // A malformed index line is a WRONG BYTE OFFSET waiting to happen, and a wrong offset decodes to |
| 292 | // a plausible weight. Refuse loudly instead of loading the rows that happened to parse. |
| 293 | err = "index.txt: could not parse a row (" + std::to_string(n) + " of " + std::to_string(want) + |
| 294 | " fields, index v" + std::to_string(version) + ")"; |
| 295 | std::fclose(idx); |
| 296 | return false; |
| 297 | } |
| 298 | r.name = name; |
nothing calls this directly
no test coverage detected