diff --git a/training/scripts/train.py b/training/scripts/train.py index 423afd3..e393685 100755 --- a/training/scripts/train.py +++ b/training/scripts/train.py @@ -98,19 +98,35 @@ def train(config_path): except Exception as e: print(f"✗ Failed: {e}") - # Strategy 5: bf16 to CPU - print("\n[5/6] Trying: bf16 to CPU...") + # Strategy 5: bf16 with accelerate CPU offload + print("\n[5/6] Trying: bf16 with accelerate CPU offload...") try: - model = AutoModelForCausalLM.from_pretrained(config["base_model"], torch_dtype=torch.bfloat16, device_map="cpu", low_cpu_mem_usage=True, trust_remote_code=True) - print("✓ Success: bf16 to CPU") + from accelerate import Accelerator + model = AutoModelForCausalLM.from_pretrained( + config["base_model"], + torch_dtype=torch.bfloat16, + device_map="cpu", + low_cpu_mem_usage=True, + trust_remote_code=True, + ) + accelerator = Accelerator(cpu_offload=True) + model = accelerator.prepare(model) + print("✓ Success: bf16 with accelerate CPU offload") except Exception as e: print(f"✗ Failed: {e}") - # Strategy 6: bf16 auto - print("\n[6/6] Trying: bf16 auto...") + # Strategy 6: bf16 to CPU with gradient checkpointing + print("\n[6/6] Trying: bf16 to CPU with gradient checkpointing...") try: - model = AutoModelForCausalLM.from_pretrained(config["base_model"], torch_dtype=torch.bfloat16, device_map="auto", low_cpu_mem_usage=True, trust_remote_code=True) - print("✓ Success: bf16 auto") + model = AutoModelForCausalLM.from_pretrained( + config["base_model"], + torch_dtype=torch.bfloat16, + device_map="cpu", + low_cpu_mem_usage=True, + trust_remote_code=True, + ) + model.gradient_checkpointing_enable() + print("✓ Success: bf16 to CPU with gradient checkpointing") except Exception as e: print(f"✗ Failed: {e}") raise RuntimeError("All loading strategies failed!")