feat: add Strategy 1 - 4-bit QLoRA with device_map=auto (distributed across GPUs)
This commit is contained in:
41
train.py
41
train.py
@@ -38,9 +38,32 @@ def train(config_path):
|
|||||||
errors = []
|
errors = []
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
# Strategy 1: QLoRA 4-bit with FSDP (load to CPU, FSDP shards across GPUs)
|
# Strategy 1: QLoRA 4-bit with device_map="auto" (distributed across GPUs, no FSDP)
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
print("\n[1/4] Trying: 4-bit QLoRA (FSDP, load to CPU)...")
|
print("\n[1/4] Trying: 4-bit QLoRA (distributed across GPUs, no FSDP)...")
|
||||||
|
try:
|
||||||
|
bnb_config = BitsAndBytesConfig(
|
||||||
|
load_in_4bit=True,
|
||||||
|
bnb_4bit_quant_type="nf4",
|
||||||
|
bnb_4bit_compute_dtype=torch.bfloat16,
|
||||||
|
bnb_4bit_use_double_quant=True,
|
||||||
|
)
|
||||||
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
|
config["base_model"],
|
||||||
|
quantization_config=bnb_config,
|
||||||
|
device_map="auto", # Distribute layers across GPUs automatically
|
||||||
|
trust_remote_code=True,
|
||||||
|
low_cpu_mem_usage=True,
|
||||||
|
)
|
||||||
|
print("✓ Success: QLoRA 4-bit distributed across GPUs (no FSDP)")
|
||||||
|
except Exception as e:
|
||||||
|
errors.append(("QLoRA 4-bit", e))
|
||||||
|
print(f"✗ Failed: {e}")
|
||||||
|
|
||||||
|
# --------------------------------------------------------------
|
||||||
|
# Strategy 2: QLoRA 4-bit with FSDP (load to CPU, FSDP shards across GPUs)
|
||||||
|
# --------------------------------------------------------------
|
||||||
|
print("\n[2/4] Trying: 4-bit QLoRA (FSDP, load to CPU)...")
|
||||||
try:
|
try:
|
||||||
bnb_config = BitsAndBytesConfig(
|
bnb_config = BitsAndBytesConfig(
|
||||||
load_in_4bit=True,
|
load_in_4bit=True,
|
||||||
@@ -58,13 +81,13 @@ def train(config_path):
|
|||||||
)
|
)
|
||||||
print("✓ Success: QLoRA 4-bit loaded to CPU (FSDP will shard across GPUs)")
|
print("✓ Success: QLoRA 4-bit loaded to CPU (FSDP will shard across GPUs)")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
errors.append(("QLoRA 4-bit", e))
|
errors.append(("QLoRA 4-bit FSDP", e))
|
||||||
print(f"✗ Failed: {e}")
|
print(f"✗ Failed: {e}")
|
||||||
|
|
||||||
# --------------------------------------------------------------
|
# --------------------------------------------------------------
|
||||||
# Strategy 2: BF16 CPU (model too large for single GPU)
|
# Strategy 3: BF16 CPU (model too large for single GPU)
|
||||||
# --------------------------------------------------------------
|
# --------------------------------------------------------------
|
||||||
print("\n[2/4] Trying: bf16 CPU...")
|
print("\n[3/4] Trying: bf16 CPU...")
|
||||||
try:
|
try:
|
||||||
model = AutoModelForCausalLM.from_pretrained(
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
config["base_model"],
|
config["base_model"],
|
||||||
@@ -79,9 +102,9 @@ def train(config_path):
|
|||||||
print(f"✗ Failed: {e}")
|
print(f"✗ Failed: {e}")
|
||||||
|
|
||||||
# ----------------------------------------------------------
|
# ----------------------------------------------------------
|
||||||
# Strategy 3: FP16 CPU (fallback)
|
# Strategy 4: FP16 CPU (fallback)
|
||||||
# ----------------------------------------------------------
|
# ----------------------------------------------------------
|
||||||
print("\n[3/4] Trying: fp16 CPU...")
|
print("\n[4/4] Trying: fp16 CPU...")
|
||||||
try:
|
try:
|
||||||
model = AutoModelForCausalLM.from_pretrained(
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
config["base_model"],
|
config["base_model"],
|
||||||
@@ -96,9 +119,9 @@ def train(config_path):
|
|||||||
print(f"✗ Failed: {e}")
|
print(f"✗ Failed: {e}")
|
||||||
|
|
||||||
# ------------------------------------------------------
|
# ------------------------------------------------------
|
||||||
# Strategy 4: 4-bit AS-IS (already quantized)
|
# Strategy 5: 4-bit AS-IS (already quantized)
|
||||||
# ----------------------------------------------------------
|
# ----------------------------------------------------------
|
||||||
print("\n[4/4] Trying: 4-bit AS-IS...")
|
print("\n[5/5] Trying: 4-bit AS-IS...")
|
||||||
try:
|
try:
|
||||||
model = AutoModelForCausalLM.from_pretrained(
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
config["base_model"],
|
config["base_model"],
|
||||||
|
|||||||
Reference in New Issue
Block a user