@@ -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
@@ -452,8 +450,7 @@ static void gguf_merge(const split_params & split_params) {
452450                        LLM_KV_SPLIT_COUNT);
453451                gguf_free (ctx_gguf);
454452                ggml_free (ctx_meta);
455-                 gguf_free (ctx_out);
456-                 fout.close ();
453+                 gguf_free (ctx_out);                
457454                exit (EXIT_FAILURE);
458455            }
459456
@@ -465,8 +462,7 @@ static void gguf_merge(const split_params & split_params) {
465462                        n_split);
466463                gguf_free (ctx_gguf);
467464                ggml_free (ctx_meta);
468-                 gguf_free (ctx_out);
469-                 fout.close ();
465+                 gguf_free (ctx_out);                
470466                exit (EXIT_FAILURE);
471467            }
472468
@@ -478,8 +474,7 @@ static void gguf_merge(const split_params & split_params) {
478474                        split_path, i_split, n_split);
479475                gguf_free (ctx_gguf);
480476                ggml_free (ctx_meta);
481-                 gguf_free (ctx_out);
482-                 fout.close ();
477+                 gguf_free (ctx_out);                
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 ..."  , __func__, split_path);
@@ -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- 
562-         fout.close ();
563-         gguf_free (ctx_out);
561+         fout.close ();        
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