@@ -1167,7 +1167,6 @@ void gguf_set_tensor_data(struct gguf_context * ctx, const char * name, const vo
11671167}
11681168
11691169struct gguf_writer_base {
1170- bool ok {true };
11711170 size_t written_bytes {0u };
11721171
11731172 ~gguf_writer_base (void ) {}
@@ -1267,7 +1266,6 @@ struct gguf_writer_base {
12671266
12681267 void pad (const size_t alignment) {
12691268 while (written_bytes % alignment != 0 ) {
1270- if (!ok) return ;
12711269 const int8_t zero = 0 ;
12721270 write (zero);
12731271 }
@@ -1321,21 +1319,23 @@ struct gguf_writer_file final : public gguf_writer_base {
13211319 using gguf_writer_base::write;
13221320
13231321 void write (const int8_t val) override {
1324- if (!ok) return ;
1325- const auto ret = fputc (val , file);
1322+ const auto real_val = static_cast < uint8_t >(val) ;
1323+ const auto ret = fputc (real_val , file);
13261324 written_bytes++;
1327- ok = ok && ret == val;
1325+ if (ret != real_val) {
1326+ throw std::runtime_error (" unexpected fputc result '" + std::to_string (ret) + " ' instead of '" + std::to_string ((int )real_val) + " '" );
1327+ }
13281328 }
13291329
13301330 void write (const std::vector<int8_t > & val) override {
1331- if (!ok) return ;
13321331 const auto ret = fwrite (val.data (), 1 , val.size (), file);
13331332 written_bytes += val.size ();
1334- ok = ok && ret == val.size ();
1333+ if (ret != val.size ()) {
1334+ throw std::runtime_error (" unexpected fwrite number of bytes written, '" + std::to_string (ret) + " ' instead of '" + std::to_string (val.size ()) + " '" );
1335+ }
13351336 }
13361337
13371338 void write_tensor_data (const struct gguf_tensor_info & info, const size_t offset_data, const size_t alignment) override {
1338- if (!ok) return ;
13391339 GGML_ASSERT (written_bytes - offset_data == info.offset );
13401340
13411341 GGML_ASSERT (ggml_is_contiguous (&info.t ));
@@ -1368,8 +1368,6 @@ static void gguf_write_out(const struct gguf_context * ctx, writer_t & gw, bool
13681368 gw.write (n_tensors);
13691369 gw.write (n_kv);
13701370
1371- if (!gw.ok ) return ;
1372-
13731371 // write key-value pairs
13741372 for (int64_t i = 0 ; i < n_kv; ++i) {
13751373 gw.write (ctx->kv [i]);
@@ -1380,8 +1378,6 @@ static void gguf_write_out(const struct gguf_context * ctx, writer_t & gw, bool
13801378 gw.write_tensor_meta (ctx->info [i]);
13811379 }
13821380
1383- if (!gw.ok ) return ;
1384-
13851381 // we require the data section to be aligned
13861382 gw.pad (ctx->alignment );
13871383
@@ -1410,14 +1406,17 @@ bool gguf_write_to_file(const struct gguf_context * ctx, const char * fname, boo
14101406 return false ;
14111407 }
14121408
1413- gguf_writer_file gw (file);
1414- gguf_write_out (ctx, gw, only_meta);
1415- if (!gw.ok ) {
1416- GGML_LOG_ERROR (" %s: failed to write GGUF data into '%s'\n " , __func__, fname);
1409+ try {
1410+ gguf_writer_file gw (file);
1411+ gguf_write_out (ctx, gw, only_meta);
1412+ } catch (const std::runtime_error& ex) {
1413+ GGML_LOG_ERROR (" %s: failed to write GGUF data into '%s': %s\n " , __func__, fname, ex.what ());
1414+ fclose (file);
1415+ return false ;
14171416 }
14181417
14191418 fclose (file);
1420- return gw. ok ;
1419+ return true ;
14211420}
14221421
14231422size_t gguf_get_meta_size (const struct gguf_context * ctx) {
0 commit comments