@@ -1310,45 +1310,6 @@ struct gguf_writer_buf final : public gguf_writer_base {
13101310 }
13111311};
13121312
1313- template <typename Writer>
1314- static void gguf_write_out (const struct gguf_context * ctx, Writer & gw, bool only_meta) {
1315- const int64_t n_kv = gguf_get_n_kv (ctx);
1316- const int64_t n_tensors = gguf_get_n_tensors (ctx);
1317-
1318- // write header
1319- gw.write (GGUF_MAGIC[0 ]);
1320- gw.write (GGUF_MAGIC[1 ]);
1321- gw.write (GGUF_MAGIC[2 ]);
1322- gw.write (GGUF_MAGIC[3 ]);
1323- gw.write (ctx->version );
1324- gw.write (n_tensors);
1325- gw.write (n_kv);
1326-
1327- // write key-value pairs
1328- for (int64_t i = 0 ; i < n_kv; ++i) {
1329- gw.write (ctx->kv [i]);
1330- }
1331-
1332- // write tensor info
1333- for (int64_t i = 0 ; i < n_tensors; ++i) {
1334- gw.write_tensor_meta (ctx->info [i]);
1335- }
1336-
1337- // we require the data section to be aligned
1338- gw.pad (ctx->alignment );
1339-
1340- if (only_meta) {
1341- return ;
1342- }
1343-
1344- const size_t offset_data = gw.written_bytes ;
1345-
1346- // write tensor data
1347- for (int64_t i = 0 ; i < n_tensors; ++i) {
1348- gw.write_tensor_data (ctx->info [i], offset_data, ctx->alignment );
1349- }
1350- }
1351-
13521313// file based writer
13531314struct gguf_writer_file final : public gguf_writer_base {
13541315 FILE * file;
@@ -1359,18 +1320,21 @@ struct gguf_writer_file final : public gguf_writer_base {
13591320 using gguf_writer_base::write;
13601321
13611322 void write (const int8_t val) override {
1323+ if (!ok) return ;
13621324 const auto ret = fputc (val, file);
13631325 written_bytes++;
13641326 ok = ok && ret == val;
13651327 }
13661328
13671329 void write (const std::vector<int8_t > & val) override {
1330+ if (!ok) return ;
13681331 const auto ret = fwrite (val.data (), 1 , val.size (), file);
13691332 written_bytes += val.size ();
13701333 ok = ok && ret == val.size ();
13711334 }
13721335
13731336 void write_tensor_data (const struct gguf_tensor_info & info, const size_t offset_data, const size_t alignment) override {
1337+ if (!ok) return ;
13741338 GGML_ASSERT (written_bytes - offset_data == info.offset );
13751339
13761340 GGML_ASSERT (ggml_is_contiguous (&info.t ));
@@ -1389,6 +1353,45 @@ struct gguf_writer_file final : public gguf_writer_base {
13891353 }
13901354};
13911355
1356+ template <typename writer_t >
1357+ static void gguf_write_out (const struct gguf_context * ctx, writer_t & gw, bool only_meta) {
1358+ const int64_t n_kv = gguf_get_n_kv (ctx);
1359+ const int64_t n_tensors = gguf_get_n_tensors (ctx);
1360+
1361+ // write header
1362+ gw.write (GGUF_MAGIC[0 ]);
1363+ gw.write (GGUF_MAGIC[1 ]);
1364+ gw.write (GGUF_MAGIC[2 ]);
1365+ gw.write (GGUF_MAGIC[3 ]);
1366+ gw.write (ctx->version );
1367+ gw.write (n_tensors);
1368+ gw.write (n_kv);
1369+
1370+ // write key-value pairs
1371+ for (int64_t i = 0 ; i < n_kv; ++i) {
1372+ gw.write (ctx->kv [i]);
1373+ }
1374+
1375+ // write tensor info
1376+ for (int64_t i = 0 ; i < n_tensors; ++i) {
1377+ gw.write_tensor_meta (ctx->info [i]);
1378+ }
1379+
1380+ // we require the data section to be aligned
1381+ gw.pad (ctx->alignment );
1382+
1383+ if (only_meta) {
1384+ return ;
1385+ }
1386+
1387+ const size_t offset_data = gw.written_bytes ;
1388+
1389+ // write tensor data
1390+ for (int64_t i = 0 ; i < n_tensors; ++i) {
1391+ gw.write_tensor_data (ctx->info [i], offset_data, ctx->alignment );
1392+ }
1393+ }
1394+
13921395void gguf_write_to_buf (const struct gguf_context * ctx, std::vector<int8_t > & buf, bool only_meta) {
13931396 gguf_writer_buf gw (buf);
13941397 gguf_write_out (ctx, gw, only_meta);
0 commit comments