td-builder commited on
Commit
a268cc1
·
verified ·
1 Parent(s): 1be0c1f

Upload 148 files

Browse files
Files changed (1) hide show
  1. hugging/td_fuse/reasoning_heal.py +40 -1
hugging/td_fuse/reasoning_heal.py CHANGED
@@ -413,11 +413,50 @@ def run_reasoning_heal():
413
  output_subdir = Path(OUTPUT_DIR)
414
  output_subdir.mkdir(parents=True, exist_ok=True)
415
 
416
- for phase_name, phase_config in PHASES.items():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417
  model = run_phase(model, tokenizer, phase_name, phase_config, str(output_subdir))
418
  gc.collect()
419
  torch.cuda.empty_cache()
420
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
421
  # Save final model
422
  print(f"\n[heal] Saving final reasoning-healed model to {OUTPUT_DIR}...")
423
  sys.stdout.flush()
 
413
  output_subdir = Path(OUTPUT_DIR)
414
  output_subdir.mkdir(parents=True, exist_ok=True)
415
 
416
+ phase_names = list(PHASES.keys())
417
+ for i, phase_name in enumerate(phase_names):
418
+ phase_config = PHASES[phase_name]
419
+ phase_save_dir = Path(f"td_fuse_outputs/heal_after_{phase_name}")
420
+
421
+ # Check if this phase was already completed (resume support)
422
+ phase_safetensors = list(phase_save_dir.glob("*.safetensors")) if phase_save_dir.exists() else []
423
+ phase_size = sum(f.stat().st_size for f in phase_safetensors) if phase_safetensors else 0
424
+ if phase_size > 1_000_000_000:
425
+ print(f"\n[heal] Phase {i+1}/3 ({phase_name}) already saved ({phase_size/1e9:.1f} GB) — SKIPPING!")
426
+ print(f"[heal] Loading saved model from {phase_save_dir}...")
427
+ sys.stdout.flush()
428
+ # Free current model and load saved one
429
+ del model
430
+ gc.collect()
431
+ torch.cuda.empty_cache()
432
+ model, tokenizer = load_model_smart(str(phase_save_dir))
433
+ continue
434
+
435
+ # Run the phase
436
  model = run_phase(model, tokenizer, phase_name, phase_config, str(output_subdir))
437
  gc.collect()
438
  torch.cuda.empty_cache()
439
 
440
+ # Save after EVERY phase so nothing is lost
441
+ print(f"\n[heal] Saving model after {phase_name} to {phase_save_dir}...")
442
+ sys.stdout.flush()
443
+ t = time.time()
444
+ phase_save_dir.mkdir(parents=True, exist_ok=True)
445
+ model.save_pretrained(str(phase_save_dir), safe_serialization=True)
446
+ tokenizer.save_pretrained(str(phase_save_dir))
447
+ saved_files = list(phase_save_dir.glob("*.safetensors"))
448
+ total_size = sum(f.stat().st_size for f in saved_files) if saved_files else 0
449
+ print(f"[heal] Saved: {len(saved_files)} files, {total_size/1e9:.1f} GB in {time.time()-t:.0f}s")
450
+ sys.stdout.flush()
451
+
452
+ # Delete PREVIOUS phase save to free disk (keep only latest)
453
+ if i > 0:
454
+ prev_save = Path(f"td_fuse_outputs/heal_after_{phase_names[i-1]}")
455
+ if prev_save.exists() and prev_save != phase_save_dir:
456
+ import shutil
457
+ shutil.rmtree(str(prev_save), ignore_errors=True)
458
+ print(f"[heal] Freed disk: deleted {prev_save}")
459
+
460
  # Save final model
461
  print(f"\n[heal] Saving final reasoning-healed model to {OUTPUT_DIR}...")
462
  sys.stdout.flush()