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']}")
|
print(f"Loading model: {config['base_model']}")
|
||||||
|
|
||||||
# Load bf16 to CPU first
|
# Load bf16 to CPU with BnB 4-bit quantization
|
||||||
print(f"\n[INFO] Loading {config['base_model']} bf16 to CPU...")
|
print(f"\n[INFO] Loading {config['base_model']} with BnB 4-bit 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...")
|
|
||||||
from transformers import BitsAndBytesConfig
|
from transformers import BitsAndBytesConfig
|
||||||
bnb_config = BitsAndBytesConfig(
|
bnb_config = BitsAndBytesConfig(
|
||||||
load_in_4bit=True,
|
load_in_4bit=True,
|
||||||
@@ -53,12 +42,6 @@ def train(config_path):
|
|||||||
bnb_4bit_compute_dtype=torch.float16,
|
bnb_4bit_compute_dtype=torch.float16,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Reload with quantization
|
|
||||||
del model
|
|
||||||
import gc
|
|
||||||
gc.collect()
|
|
||||||
torch.cuda.empty_cache()
|
|
||||||
|
|
||||||
model = AutoModelForCausalLM.from_pretrained(
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
config["base_model"],
|
config["base_model"],
|
||||||
quantization_config=bnb_config,
|
quantization_config=bnb_config,
|
||||||
@@ -67,7 +50,7 @@ def train(config_path):
|
|||||||
trust_remote_code=True,
|
trust_remote_code=True,
|
||||||
low_cpu_mem_usage=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
|
# Move to GPU
|
||||||
print(" Moving to GPU 0...")
|
print(" Moving to GPU 0...")
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
# LoRA Training Configuration for Ornith-1.0-35B
|
# LoRA Training Configuration for Ornith-1.0-35B
|
||||||
# Dataset: cyron_summary_lora_dataset (20k examples)
|
# Dataset: cyron_summary_lora_dataset (20k examples)
|
||||||
|
|
||||||
base_model: /data/models/Ornith-1.0-35B-bnb-4bit
|
base_model: /data/models/Ornith-1.0-35B
|
||||||
model_type: Qwen3_5MoeForCausalLM
|
model_type: Qwen3_5MoeForCausalLM
|
||||||
tokenizer_type: AutoTokenizer
|
tokenizer_type: AutoTokenizer
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user