@@ -408,8 +408,6 @@ static void gguf_merge(const split_params & split_params) {
408408        exit (EXIT_FAILURE);
409409    }
410410
411-     std::ofstream fout (split_params.output .c_str (), std::ios::binary);
412-     fout.exceptions (std::ofstream::failbit); //  fail fast on write errors
413411
414412    auto  * ctx_out = gguf_init_empty ();
415413
@@ -453,7 +451,6 @@ static void gguf_merge(const split_params & split_params) {
453451                gguf_free (ctx_gguf);
454452                ggml_free (ctx_meta);
455453                gguf_free (ctx_out);
456-                 fout.close ();
457454                exit (EXIT_FAILURE);
458455            }
459456
@@ -466,7 +463,6 @@ static void gguf_merge(const split_params & split_params) {
466463                gguf_free (ctx_gguf);
467464                ggml_free (ctx_meta);
468465                gguf_free (ctx_out);
469-                 fout.close ();
470466                exit (EXIT_FAILURE);
471467            }
472468
@@ -479,7 +475,6 @@ static void gguf_merge(const split_params & split_params) {
479475                gguf_free (ctx_gguf);
480476                ggml_free (ctx_meta);
481477                gguf_free (ctx_out);
482-                 fout.close ();
483478                exit (EXIT_FAILURE);
484479            }
485480
@@ -500,9 +495,11 @@ static void gguf_merge(const split_params & split_params) {
500495
501496        fprintf (stderr, " \033 [3Ddone\n " 
502497    }
503- 
504-     //  placeholder for the meta data
505-     {
498+     std::ofstream fout;
499+     if  (!split_params.dry_run ) {
500+         fout.open (split_params.output .c_str (), std::ios::binary);
501+         fout.exceptions (std::ofstream::failbit); //  fail fast on write errors
502+         //  placeholder for the meta data
506503        auto  meta_size = gguf_get_meta_size (ctx_out);
507504        ::zeros (fout, meta_size);
508505    }
@@ -518,7 +515,9 @@ static void gguf_merge(const split_params & split_params) {
518515                ggml_free (ctx_metas[i]);
519516            }
520517            gguf_free (ctx_out);
521-             fout.close ();
518+             if  (!split_params.dry_run ) {
519+                 fout.close ();
520+             }
522521            exit (EXIT_FAILURE);
523522        }
524523        fprintf (stderr, " %s: writing tensors %s ..." 
@@ -540,10 +539,11 @@ static void gguf_merge(const split_params & split_params) {
540539            auto  offset = gguf_get_data_offset (ctx_gguf) + gguf_get_tensor_offset (ctx_gguf, i_tensor);
541540            f_input.seekg (offset);
542541            f_input.read ((char  *)read_data.data (), n_bytes);
543- 
544-             //  write tensor data + padding
545-             fout.write ((const  char  *)read_data.data (), n_bytes);
546-             zeros (fout, GGML_PAD (n_bytes, GGUF_DEFAULT_ALIGNMENT) - n_bytes);
542+             if  (!split_params.dry_run ) {
543+                 //  write tensor data + padding
544+                 fout.write ((const  char  *)read_data.data (), n_bytes);
545+                 zeros (fout, GGML_PAD (n_bytes, GGUF_DEFAULT_ALIGNMENT) - n_bytes);
546+             }
547547        }
548548
549549        gguf_free (ctx_gguf);
@@ -552,16 +552,15 @@ static void gguf_merge(const split_params & split_params) {
552552        fprintf (stderr, " \033 [3Ddone\n " 
553553    }
554554
555-     {
555+     if  (!split_params. dry_run )  {
556556        //  go back to beginning of file and write the updated metadata
557557        fout.seekp (0 );
558558        std::vector<uint8_t > data (gguf_get_meta_size (ctx_out));
559559        gguf_get_meta_data (ctx_out, data.data ());
560560        fout.write ((const  char  *)data.data (), data.size ());
561- 
562561        fout.close ();
563-         gguf_free (ctx_out);
564562    }
563+     gguf_free (ctx_out);
565564
566565    fprintf (stderr, " %s: %s merged from %d split with %d tensors.\n " 
567566            __func__, split_params.output .c_str (), n_split, total_tensors);
0 commit comments