@@ -408,8 +408,6 @@ static void gguf_merge(const split_params & split_params) {
408
408
exit (EXIT_FAILURE);
409
409
}
410
410
411
- std::ofstream fout (split_params.output .c_str (), std::ios::binary);
412
- fout.exceptions (std::ofstream::failbit); // fail fast on write errors
413
411
414
412
auto * ctx_out = gguf_init_empty ();
415
413
@@ -453,7 +451,6 @@ static void gguf_merge(const split_params & split_params) {
453
451
gguf_free (ctx_gguf);
454
452
ggml_free (ctx_meta);
455
453
gguf_free (ctx_out);
456
- fout.close ();
457
454
exit (EXIT_FAILURE);
458
455
}
459
456
@@ -466,7 +463,6 @@ static void gguf_merge(const split_params & split_params) {
466
463
gguf_free (ctx_gguf);
467
464
ggml_free (ctx_meta);
468
465
gguf_free (ctx_out);
469
- fout.close ();
470
466
exit (EXIT_FAILURE);
471
467
}
472
468
@@ -479,7 +475,6 @@ static void gguf_merge(const split_params & split_params) {
479
475
gguf_free (ctx_gguf);
480
476
ggml_free (ctx_meta);
481
477
gguf_free (ctx_out);
482
- fout.close ();
483
478
exit (EXIT_FAILURE);
484
479
}
485
480
@@ -500,9 +495,11 @@ static void gguf_merge(const split_params & split_params) {
500
495
501
496
fprintf (stderr, " \033 [3Ddone\n " );
502
497
}
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
506
503
auto meta_size = gguf_get_meta_size (ctx_out);
507
504
::zeros (fout, meta_size);
508
505
}
@@ -518,7 +515,9 @@ static void gguf_merge(const split_params & split_params) {
518
515
ggml_free (ctx_metas[i]);
519
516
}
520
517
gguf_free (ctx_out);
521
- fout.close ();
518
+ if (!split_params.dry_run ) {
519
+ fout.close ();
520
+ }
522
521
exit (EXIT_FAILURE);
523
522
}
524
523
fprintf (stderr, " %s: writing tensors %s ..." , __func__, split_path);
@@ -540,10 +539,11 @@ static void gguf_merge(const split_params & split_params) {
540
539
auto offset = gguf_get_data_offset (ctx_gguf) + gguf_get_tensor_offset (ctx_gguf, i_tensor);
541
540
f_input.seekg (offset);
542
541
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
+ }
547
547
}
548
548
549
549
gguf_free (ctx_gguf);
@@ -552,16 +552,15 @@ static void gguf_merge(const split_params & split_params) {
552
552
fprintf (stderr, " \033 [3Ddone\n " );
553
553
}
554
554
555
- {
555
+ if (!split_params. dry_run ) {
556
556
// go back to beginning of file and write the updated metadata
557
557
fout.seekp (0 );
558
558
std::vector<uint8_t > data (gguf_get_meta_size (ctx_out));
559
559
gguf_get_meta_data (ctx_out, data.data ());
560
560
fout.write ((const char *)data.data (), data.size ());
561
-
562
561
fout.close ();
563
- gguf_free (ctx_out);
564
562
}
563
+ gguf_free (ctx_out);
565
564
566
565
fprintf (stderr, " %s: %s merged from %d split with %d tensors.\n " ,
567
566
__func__, split_params.output .c_str (), n_split, total_tensors);
0 commit comments