feat: simple NF4 quantization with device_map=auto (proven method)
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Quantize Ornith-1.0-35B to 4-bit NF4 using bitsandbytes (recommended way)."""
|
||||
"""Simple NF4 quantization using BnB with device_map auto-distribution."""
|
||||
|
||||
import torch
|
||||
from transformers import AutoModelForCausalLM, BitsAndBytesConfig, AutoTokenizer
|
||||
|
||||
|
||||
def quantize_model(model_path, output_path):
|
||||
print(f"Quantizing model from: {model_path}")
|
||||
print("This will use bitsandbytes NF4 quantization with double quantization.\n")
|
||||
|
||||
bnb_config = BitsAndBytesConfig(
|
||||
load_in_4bit=True,
|
||||
@@ -15,27 +15,35 @@ def quantize_model(model_path, output_path):
|
||||
bnb_4bit_use_double_quant=True,
|
||||
)
|
||||
|
||||
print("Loading model with 4-bit quantization (this may take a while)...")
|
||||
print("Loading model with 4-bit quantization...")
|
||||
print(" This will distribute across both GPUs automatically\n")
|
||||
|
||||
model = AutoModelForCausalLM.from_pretrained(
|
||||
model_path,
|
||||
quantization_config=bnb_config,
|
||||
device_map="auto",
|
||||
max_memory={0: "28GiB", 1: "28GiB"}, # Leave room for training
|
||||
low_cpu_mem_usage=True,
|
||||
trust_remote_code=True,
|
||||
)
|
||||
|
||||
print("\nSaving quantized model...")
|
||||
print("\n✓ Model loaded and quantized")
|
||||
print(f" GPU 0: {torch.cuda.memory_allocated(0) / 1e9:.2f} GB")
|
||||
print(f" GPU 1: {torch.cuda.memory_allocated(1) / 1e9:.2f} GB")
|
||||
|
||||
print(f"\nSaving quantized model to: {output_path}")
|
||||
model.save_pretrained(output_path)
|
||||
|
||||
# Also save tokenizer if it exists
|
||||
|
||||
# Save tokenizer
|
||||
try:
|
||||
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
||||
tokenizer.save_pretrained(output_path)
|
||||
print("✓ Tokenizer saved")
|
||||
except:
|
||||
pass
|
||||
print("⚠ No tokenizer found")
|
||||
|
||||
print(f"\n✅ Quantized model saved to: {output_path}")
|
||||
print("You can now load it with: AutoModelForCausalLM.from_pretrained(..., load_in_4bit=True)")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import argparse
|
||||
@@ -43,5 +51,5 @@ if __name__ == "__main__":
|
||||
parser.add_argument("--model-path", type=str, default="/data/models/Ornith-1.0-35B")
|
||||
parser.add_argument("--output-path", type=str, default="/data/models/Ornith-1.0-35B-nf4")
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
quantize_model(args.model_path, args.output_path)
|
||||
|
||||
Reference in New Issue
Block a user