File size: 5,195 Bytes
9a6b9f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17ff807
 
 
4d66a08
17ff807
 
9a6b9f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17ff807
 
9a6b9f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python3
"""LoRA training script — runs on RunPod GPU pod.

Downloaded from HF dataset repo, executed on pod startup."""
import os, sys, json, time, subprocess
from pathlib import Path

# Args passed via environment
ADAPTER_NAME = os.environ["ADAPTER_NAME"]
HF_DATA_REPO = os.environ["HF_DATA_REPO"]
HF_MODEL_REPO = os.environ["HF_MODEL_REPO"]
HF_TOKEN = os.environ["HF_TOKEN"]
LORA_RANK = int(os.environ.get("LORA_RANK", "16"))
LORA_ALPHA = LORA_RANK * 2
BASE_MODEL = "Qwen/Qwen2.5-7B-Instruct"

print(f"=== Training {ADAPTER_NAME} LoRA (r={LORA_RANK}, alpha={LORA_ALPHA}) ===")
print(f"Data: {HF_DATA_REPO}")
print(f"Output: {HF_MODEL_REPO}")
start_time = time.time()

# Install dependencies — pin exact compatible versions for torch 2.4.x (RunPod image)
# transformers<4.46 avoids set_submodule (needs torch 2.5+)
# trl<0.12 avoids processing_class kwarg (needs transformers 4.46+)
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q",
    "transformers==4.45.2", "peft==0.12.0", "datasets",
    "accelerate", "bitsandbytes", "huggingface_hub", "trl==0.11.4", "runpod"])

from datasets import load_dataset
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, BitsAndBytesConfig
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
from trl import SFTTrainer, SFTConfig
from huggingface_hub import HfApi, login
import torch

login(token=HF_TOKEN)

# Load dataset
print(f"Loading dataset from {HF_DATA_REPO}...")
dataset = load_dataset(HF_DATA_REPO, split="train")
print(f"  {len(dataset)} training examples")

# Load model in 4-bit
print(f"Loading {BASE_MODEL} in 4-bit...")
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(
    BASE_MODEL,
    quantization_config=bnb_config,
    device_map="auto",
    trust_remote_code=True,
    torch_dtype=torch.bfloat16,
)
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token

model = prepare_model_for_kbit_training(model)

# LoRA config
lora_config = LoraConfig(
    r=LORA_RANK,
    lora_alpha=LORA_ALPHA,
    target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM",
)
model = get_peft_model(model, lora_config)
model.print_trainable_parameters()

# Training args
output_dir = f"/workspace/{ADAPTER_NAME}-lora"
num_examples = len(dataset)
batch_size = 2
grad_accum = 8  # effective batch = 16
num_epochs = 3 if num_examples < 5000 else (2 if num_examples < 20000 else 1)
warmup = min(100, num_examples // (batch_size * grad_accum))

print(f"Config: epochs={num_epochs}, batch={batch_size}, grad_accum={grad_accum}, warmup={warmup}")

# Format function
def format_messages(example):
    messages = example.get("messages", [])
    text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False)
    return {"text": text}

formatted = dataset.map(format_messages, remove_columns=dataset.column_names)

# Train
print(f"Training for {num_epochs} epochs, {num_examples} examples...")
training_args = SFTConfig(
    output_dir=output_dir,
    num_train_epochs=num_epochs,
    per_device_train_batch_size=batch_size,
    gradient_accumulation_steps=grad_accum,
    learning_rate=2e-4,
    warmup_steps=warmup,
    logging_steps=10,
    save_strategy="epoch",
    bf16=True,
    optim="paged_adamw_8bit",
    lr_scheduler_type="cosine",
    gradient_checkpointing=True,
    max_grad_norm=0.3,
    report_to="none",
    max_seq_length=2048,
    packing=True,
    dataset_text_field="text",
)

trainer = SFTTrainer(
    model=model,
    train_dataset=formatted,
    args=training_args,
)
trainer.train()

# Save adapter
print("Saving adapter...")
model.save_pretrained(output_dir)
tokenizer.save_pretrained(output_dir)

# Upload to HuggingFace
print(f"Uploading to {HF_MODEL_REPO}...")
api = HfApi(token=HF_TOKEN)
api.create_repo(HF_MODEL_REPO, repo_type="model", exist_ok=True)
api.upload_folder(
    folder_path=output_dir,
    repo_id=HF_MODEL_REPO,
    repo_type="model",
)

elapsed = time.time() - start_time
print(f"\n=== {ADAPTER_NAME} COMPLETE === ({elapsed/60:.1f} min)")
print(f"Adapter uploaded to: {HF_MODEL_REPO}")

# Signal completion
with open("/workspace/TRAINING_COMPLETE", "w") as f:
    json.dump({
        "adapter": ADAPTER_NAME,
        "elapsed_min": round(elapsed / 60, 1),
        "examples": num_examples,
        "epochs": num_epochs,
        "rank": LORA_RANK,
        "model_repo": HF_MODEL_REPO,
    }, f, indent=2)

# Self-terminate pod
print("Self-terminating pod...")
try:
    import runpod
    runpod.api_key = os.environ.get("RUNPOD_API_KEY", "")
    pod_id = os.environ.get("RUNPOD_POD_ID", "")
    if pod_id and runpod.api_key:
        runpod.terminate_pod(pod_id)
except Exception as e:
    print(f"  Self-terminate failed: {e} (pod will idle-timeout)")