| 1364 | |
| 1365 | template <typename writer_t> |
| 1366 | static void gguf_write_out(const struct gguf_context * ctx, writer_t & gw, bool only_meta) { |
| 1367 | const int64_t n_kv = gguf_get_n_kv(ctx); |
| 1368 | const int64_t n_tensors = gguf_get_n_tensors(ctx); |
| 1369 | |
| 1370 | // write header |
| 1371 | gw.write(GGUF_MAGIC[0]); |
| 1372 | gw.write(GGUF_MAGIC[1]); |
| 1373 | gw.write(GGUF_MAGIC[2]); |
| 1374 | gw.write(GGUF_MAGIC[3]); |
| 1375 | gw.write(ctx->version); |
| 1376 | gw.write(n_tensors); |
| 1377 | gw.write(n_kv); |
| 1378 | |
| 1379 | // write key-value pairs |
| 1380 | for (int64_t i = 0; i < n_kv; ++i) { |
| 1381 | gw.write(ctx->kv[i]); |
| 1382 | } |
| 1383 | |
| 1384 | // write tensor info |
| 1385 | for (int64_t i = 0; i < n_tensors; ++i) { |
| 1386 | gw.write_tensor_meta(ctx->info[i]); |
| 1387 | } |
| 1388 | |
| 1389 | // we require the data section to be aligned |
| 1390 | gw.pad(ctx->alignment); |
| 1391 | |
| 1392 | if (only_meta) { |
| 1393 | return; |
| 1394 | } |
| 1395 | |
| 1396 | const size_t offset_data = gw.written_bytes; |
| 1397 | |
| 1398 | // write tensor data |
| 1399 | for (int64_t i = 0; i < n_tensors; ++i) { |
| 1400 | gw.write_tensor_data(ctx->info[i], offset_data, ctx->alignment); |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | void gguf_write_to_buf(const struct gguf_context * ctx, std::vector<int8_t> & buf, bool only_meta) { |
| 1405 | gguf_writer_buf gw(buf); |
no test coverage detected