fix: add accelerate CPU offload to bf16 strategies
This commit is contained in:
@@ -98,19 +98,35 @@ def train(config_path):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"✗ Failed: {e}")
|
print(f"✗ Failed: {e}")
|
||||||
|
|
||||||
# Strategy 5: bf16 to CPU
|
# Strategy 5: bf16 with accelerate CPU offload
|
||||||
print("\n[5/6] Trying: bf16 to CPU...")
|
print("\n[5/6] Trying: bf16 with accelerate CPU offload...")
|
||||||
try:
|
try:
|
||||||
model = AutoModelForCausalLM.from_pretrained(config["base_model"], torch_dtype=torch.bfloat16, device_map="cpu", low_cpu_mem_usage=True, trust_remote_code=True)
|
from accelerate import Accelerator
|
||||||
print("✓ Success: bf16 to CPU")
|
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:
|
except Exception as e:
|
||||||
print(f"✗ Failed: {e}")
|
print(f"✗ Failed: {e}")
|
||||||
|
|
||||||
# Strategy 6: bf16 auto
|
# Strategy 6: bf16 to CPU with gradient checkpointing
|
||||||
print("\n[6/6] Trying: bf16 auto...")
|
print("\n[6/6] Trying: bf16 to CPU with gradient checkpointing...")
|
||||||
try:
|
try:
|
||||||
model = AutoModelForCausalLM.from_pretrained(config["base_model"], torch_dtype=torch.bfloat16, device_map="auto", low_cpu_mem_usage=True, trust_remote_code=True)
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
print("✓ Success: bf16 auto")
|
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:
|
except Exception as e:
|
||||||
print(f"✗ Failed: {e}")
|
print(f"✗ Failed: {e}")
|
||||||
raise RuntimeError("All loading strategies failed!")
|
raise RuntimeError("All loading strategies failed!")
|
||||||
|
|||||||
Reference in New Issue
Block a user