fix: load bf16 model directly with BnB 4-bit (single load)
This commit is contained in:
23
train.py
23
train.py
@@ -33,19 +33,8 @@ def train(config_path):
|
||||
|
||||
print(f"Loading model: {config['base_model']}")
|
||||
|
||||
# Load bf16 to CPU first
|
||||
print(f"\n[INFO] Loading {config['base_model']} bf16 to CPU...")
|
||||
model = AutoModelForCausalLM.from_pretrained(
|
||||
config["base_model"],
|
||||
device_map="cpu",
|
||||
torch_dtype=torch.bfloat16,
|
||||
trust_remote_code=True,
|
||||
low_cpu_mem_usage=True,
|
||||
)
|
||||
print("✓ Model loaded to CPU (~70GB bf16)")
|
||||
|
||||
# Quantize with BnB on CPU
|
||||
print(" Quantizing with BnB 4-bit on CPU...")
|
||||
# Load bf16 to CPU with BnB 4-bit quantization
|
||||
print(f"\n[INFO] Loading {config['base_model']} with BnB 4-bit to CPU...")
|
||||
from transformers import BitsAndBytesConfig
|
||||
bnb_config = BitsAndBytesConfig(
|
||||
load_in_4bit=True,
|
||||
@@ -53,12 +42,6 @@ def train(config_path):
|
||||
bnb_4bit_compute_dtype=torch.float16,
|
||||
)
|
||||
|
||||
# Reload with quantization
|
||||
del model
|
||||
import gc
|
||||
gc.collect()
|
||||
torch.cuda.empty_cache()
|
||||
|
||||
model = AutoModelForCausalLM.from_pretrained(
|
||||
config["base_model"],
|
||||
quantization_config=bnb_config,
|
||||
@@ -67,7 +50,7 @@ def train(config_path):
|
||||
trust_remote_code=True,
|
||||
low_cpu_mem_usage=True,
|
||||
)
|
||||
print("✓ Model quantized to 4-bit on CPU")
|
||||
print("✓ Model loaded to CPU with BnB 4-bit (~17.5GB)")
|
||||
|
||||
# Move to GPU
|
||||
print(" Moving to GPU 0...")
|
||||
|
||||
Reference in New Issue
Block a user