| 499 | #endif |
| 500 | |
| 501 | bool expert_layout_load(const std::string& pack_dir, int64_t n_layers, int64_t n_expert, std::string& err) { |
| 502 | ExpertLayout L; |
| 503 | L.n_layers = n_layers; |
| 504 | L.n_expert = n_expert; |
| 505 | std::ifstream in(pack_dir + "/native_experts.txt"); |
| 506 | if (!in) { |
| 507 | L.total = (uint64_t) n_layers * (uint64_t) n_expert * (uint64_t) BLOB; |
| 508 | hadamard_set({}); |
| 509 | g_layout = L; |
| 510 | return true; |
| 511 | } |
| 512 | #if !defined(STRATA_NATIVE_EXPERTS) |
| 513 | err = "this pack has native (IQ) experts but the engine was built without STRATA_NATIVE_EXPERTS"; |
| 514 | return false; |
| 515 | #else |
| 516 | L.native = true; |
| 517 | L.fmt.resize((size_t) n_layers); |
| 518 | L.offset.assign((size_t) n_layers, ~0ull); |
| 519 | L.bytes.assign((size_t) n_layers, 0); |
| 520 | L.max_blob = 0; |
| 521 | std::string line; |
| 522 | while (std::getline(in, line)) { |
| 523 | if (line.empty() || line[0] == '#') { |
| 524 | if (!line.empty() && line[0] == '#') { |
| 525 | // "# strata native experts vN: ...". A version this engine does not know may mean columns it |
| 526 | // would misread, so it is refused rather than parsed as far as the known columns go. |
| 527 | static const char tag[] = "# strata native experts v"; |
| 528 | if (line.compare(0, sizeof tag - 1, tag) == 0) { |
| 529 | L.version = std::atoi(line.c_str() + sizeof tag - 1); |
| 530 | if (L.version > kExpertLayoutVersion) { |
| 531 | err = "native_experts.txt is v" + std::to_string(L.version) + "; this engine reads up to v" + |
| 532 | std::to_string(kExpertLayoutVersion) + " (the pack was written by a newer tools/" |
| 533 | "iq_pack.py: update the engine, or repack with this one)"; |
| 534 | return false; |
| 535 | } |
| 536 | } |
| 537 | // v3 packs record their expert count in the header; a pruned model (GSQ-RCO Coder) ships |
| 538 | // fewer experts than the canonical geometry the caller passes, which is a compile-time |
| 539 | // default, so the header wins. |
| 540 | const size_t at = line.find("(n_expert "); |
| 541 | if (at != std::string::npos) L.n_expert = std::atoll(line.c_str() + at + 10); |
| 542 | } |
| 543 | continue; |
| 544 | } |
| 545 | std::istringstream ss(line); |
| 546 | long long l = -1, gt = -1, dt = -1; |
| 547 | unsigned long long off = 0, blob = 0, go = 0, uo = 0, dox = 0; |
| 548 | if (!(ss >> l >> gt >> dt >> off >> blob) || l < 0 || l >= n_layers) { |
| 549 | err = "native_experts.txt: a malformed line: " + line; |
| 550 | return false; |
| 551 | } |
| 552 | NativeFmt f; |
| 553 | if (!native_fmt((int) gt, (int) dt, H, FF, f, err)) return false; |
| 554 | if (f.bytes != blob) { |
| 555 | err = "native_experts.txt: layer " + std::to_string(l) + " blob is " + std::to_string(blob) + |
| 556 | " B but its formats make " + std::to_string(f.bytes); |
| 557 | return false; |
| 558 | } |