| 74 | void put64(std::vector<uint8_t>& o, uint64_t v) { o.insert(o.end(), (uint8_t*) &v, (uint8_t*) &v + 8); } |
| 75 | |
| 76 | bool write_gguf(const std::string& path, const std::vector<Spec>& ts) { |
| 77 | std::vector<uint8_t> o; |
| 78 | put32(o, 0x46554747u); |
| 79 | put32(o, 3); |
| 80 | put64(o, ts.size()); |
| 81 | put64(o, 0); // no metadata |
| 82 | uint64_t off = 0; |
| 83 | for (const Spec& t : ts) { |
| 84 | put64(o, t.name.size()); |
| 85 | o.insert(o.end(), t.name.begin(), t.name.end()); |
| 86 | put32(o, (uint32_t) t.ne.size()); |
| 87 | for (uint64_t e : t.ne) put64(o, e); |
| 88 | put32(o, t.type); |
| 89 | put64(o, off); |
| 90 | off += (t.data.size() + 31) / 32 * 32; |
| 91 | } |
| 92 | while (o.size() % 32) o.push_back(0); |
| 93 | for (const Spec& t : ts) { |
| 94 | o.insert(o.end(), t.data.begin(), t.data.end()); |
| 95 | while (o.size() % 32) o.push_back(0); |
| 96 | } |
| 97 | std::FILE* f = std::fopen(path.c_str(), "wb"); |
| 98 | if (!f) return false; |
| 99 | const bool ok = std::fwrite(o.data(), 1, o.size(), f) == o.size(); |
| 100 | std::fclose(f); |
| 101 | return ok; |
| 102 | } |
| 103 | |
| 104 | } // namespace |
| 105 | |