fix: load model first then quantize with BnB
This commit is contained in:
@@ -32,7 +32,7 @@ def train(config_path):
|
|||||||
|
|
||||||
print(f"Loading model: {config['base_model']}")
|
print(f"Loading model: {config['base_model']}")
|
||||||
|
|
||||||
# Load model with QLoRA (4-bit quantization)
|
# Load model (skip broken quantization config)
|
||||||
print(f"Loading model: {config['base_model']}")
|
print(f"Loading model: {config['base_model']}")
|
||||||
from transformers import BitsAndBytesConfig
|
from transformers import BitsAndBytesConfig
|
||||||
|
|
||||||
@@ -43,14 +43,17 @@ def train(config_path):
|
|||||||
bnb_4bit_use_double_quant=True,
|
bnb_4bit_use_double_quant=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Load without quantization config, then apply BnB
|
||||||
model = AutoModelForCausalLM.from_pretrained(
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
config["base_model"],
|
config["base_model"],
|
||||||
quantization_config=bnb_config,
|
torch_dtype=torch.bfloat16,
|
||||||
dtype=torch.bfloat16,
|
device_map="auto",
|
||||||
device_map="auto", # Load directly to GPU
|
|
||||||
trust_remote_code=True,
|
trust_remote_code=True,
|
||||||
)
|
)
|
||||||
print("Model loaded with QLoRA (4-bit).")
|
# Apply 4-bit quantization after loading
|
||||||
|
from peft import prepare_model_for_kbit_training
|
||||||
|
model = prepare_model_for_kbit_training(model, use_gradient_checkpointing=True)
|
||||||
|
print("Model loaded and quantized.")
|
||||||
|
|
||||||
# Prepare model for k-bit training
|
# Prepare model for k-bit training
|
||||||
from peft import prepare_model_for_kbit_training
|
from peft import prepare_model_for_kbit_training
|
||||||
|
|||||||
Reference in New Issue
Block a user