| 1470 | |
| 1471 | template <typename writer_t> |
| 1472 | static void gguf_write_out(const struct gguf_context * ctx, writer_t & gw, bool only_meta) { |
| 1473 | const int64_t n_kv = gguf_get_n_kv(ctx); |
| 1474 | const int64_t n_tensors = gguf_get_n_tensors(ctx); |
| 1475 | |
| 1476 | // write header |
| 1477 | gw.write(GGUF_MAGIC[0]); |
| 1478 | gw.write(GGUF_MAGIC[1]); |
| 1479 | gw.write(GGUF_MAGIC[2]); |
| 1480 | gw.write(GGUF_MAGIC[3]); |
| 1481 | gw.write(ctx->version); |
| 1482 | gw.write(n_tensors); |
| 1483 | gw.write(n_kv); |
| 1484 | |
| 1485 | // write key-value pairs |
| 1486 | for (int64_t i = 0; i < n_kv; ++i) { |
| 1487 | gw.write(ctx->kv[i]); |
| 1488 | } |
| 1489 | |
| 1490 | // write tensor info |
| 1491 | for (int64_t i = 0; i < n_tensors; ++i) { |
| 1492 | gw.write_tensor_meta(ctx->info[i]); |
| 1493 | } |
| 1494 | |
| 1495 | // we require the data section to be aligned |
| 1496 | gw.pad(ctx->alignment); |
| 1497 | |
| 1498 | if (only_meta) { |
| 1499 | return; |
| 1500 | } |
| 1501 | |
| 1502 | const size_t offset_data = gw.written_bytes; |
| 1503 | |
| 1504 | // write tensor data |
| 1505 | for (int64_t i = 0; i < n_tensors; ++i) { |
| 1506 | gw.write_tensor_data(ctx->info[i], offset_data, ctx->alignment); |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | void gguf_write_to_buf(const struct gguf_context * ctx, std::vector<int8_t> & buf, bool only_meta) { |
| 1511 | gguf_writer_buf gw(buf); |
no test coverage detected