feat: detect pre-quantized model and skip re-quantization
This commit is contained in:
18
train.py
18
train.py
@@ -33,7 +33,23 @@ def train(config_path):
|
|||||||
|
|
||||||
print(f"Loading model: {config['base_model']}")
|
print(f"Loading model: {config['base_model']}")
|
||||||
|
|
||||||
# Load bf16 to CPU with BnB 4-bit quantization
|
# Check if model is quantized (has model-XXXXX-of-XXXXX files)
|
||||||
|
import glob
|
||||||
|
safetensor_files = glob.glob(f"{config['base_model']}/*.safetensors")
|
||||||
|
is_quantized = any("of-" in f for f in safetensor_files)
|
||||||
|
|
||||||
|
if is_quantized:
|
||||||
|
print(f"\n[INFO] Loading pre-quantized BnB 4-bit model (no re-quantization)...")
|
||||||
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
|
config["base_model"],
|
||||||
|
device_map="cpu",
|
||||||
|
torch_dtype=torch.float16,
|
||||||
|
trust_remote_code=True,
|
||||||
|
low_cpu_mem_usage=True,
|
||||||
|
)
|
||||||
|
print("✓ Model loaded to CPU (BnB 4-bit)")
|
||||||
|
else:
|
||||||
|
# Load bf16 with BnB 4-bit quantization
|
||||||
print(f"\n[INFO] Loading {config['base_model']} with BnB 4-bit to CPU...")
|
print(f"\n[INFO] Loading {config['base_model']} with BnB 4-bit to CPU...")
|
||||||
from transformers import BitsAndBytesConfig
|
from transformers import BitsAndBytesConfig
|
||||||
bnb_config = BitsAndBytesConfig(
|
bnb_config = BitsAndBytesConfig(
|
||||||
|
|||||||
Reference in New Issue
Block a user