-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
186 lines (156 loc) · 5.03 KB
/
train.py
File metadata and controls
186 lines (156 loc) · 5.03 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""
Phi-3-mini QLoRA Training Script
GTX 1650 4GB + 16GB RAM uchun optimallashtirilgan
"""
import os
import torch
from datasets import load_dataset
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
)
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
from trl import SFTTrainer, SFTConfig
# ============== KONFIGURATSIYA ==============
MODEL_NAME = "microsoft/Phi-3-mini-4k-instruct"
OUTPUT_DIR = "./procurement-lora"
DATA_FILE = "train_data.jsonl"
# LoRA parametrlari (3000 samples uchun optimallashtirilgan)
LORA_R = 16 # Kattaroq rank = ko'proq o'rganish qobiliyati
LORA_ALPHA = 32
LORA_DROPOUT = 0.05
# Training parametrlari
BATCH_SIZE = 1
GRADIENT_ACCUMULATION = 8 # Kattaroq effective batch
LEARNING_RATE = 1e-4 # Kichikroq LR ko'p data uchun
NUM_EPOCHS = 5 # Ko'proq epochs
MAX_SEQ_LENGTH = 1024
# ============================================
def create_prompt(example):
"""Training prompt formatlash."""
instruction = example.get("instruction", "")
input_text = example.get("input", "")
output = example.get("output", "")
if input_text:
text = f"""<|user|>
{instruction}
Ma'lumot:
{input_text}<|end|>
<|assistant|>
{output}<|end|>"""
else:
text = f"""<|user|>
{instruction}<|end|>
<|assistant|>
{output}<|end|>"""
return {"text": text}
def main():
print("=" * 50)
print("Phi-3 QLoRA Training")
print("=" * 50)
# GPU tekshirish
if torch.cuda.is_available():
print(f"GPU: {torch.cuda.get_device_name(0)}")
print(f"VRAM: {torch.cuda.get_device_properties(0).total_memory / 1024**3:.1f} GB")
else:
print("WARNING: GPU topilmadi! CPU da training juda sekin bo'ladi.")
# 4-bit quantization konfiguratsiyasi
print("\n[1/5] 4-bit quantization sozlanmoqda...")
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16, # GTX 1650 uchun float16
)
# Tokenizer yuklash
print("\n[2/5] Tokenizer yuklanmoqda...")
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "right"
# Model yuklash
print("\n[3/5] Model yuklanmoqda (4-bit)...")
model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME,
quantization_config=bnb_config,
device_map="auto",
trust_remote_code=True,
attn_implementation="eager", # Flash attention o'rniga oddiy attention
)
model.config.use_cache = False
# Gradient checkpointing
model.gradient_checkpointing_enable()
model = prepare_model_for_kbit_training(model)
# LoRA konfiguratsiyasi
print("\n[4/5] LoRA adapter sozlanmoqda...")
lora_config = LoraConfig(
r=LORA_R,
lora_alpha=LORA_ALPHA,
lora_dropout=LORA_DROPOUT,
bias="none",
task_type="CAUSAL_LM",
target_modules=[
"qkv_proj",
"o_proj",
"gate_up_proj",
"down_proj",
],
)
model = get_peft_model(model, lora_config)
# Trainable parametrlarni ko'rsatish
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
total_params = sum(p.numel() for p in model.parameters())
print(f"Trainable: {trainable_params:,} / {total_params:,} ({100 * trainable_params / total_params:.2f}%)")
# Dataset yuklash
print("\n[5/5] Dataset yuklanmoqda...")
dataset = load_dataset("json", data_files=DATA_FILE, split="train")
dataset = dataset.map(create_prompt)
print(f"Training samples: {len(dataset)}")
# SFT konfiguratsiyasi (yangi TRL versiya uchun)
sft_config = SFTConfig(
output_dir=OUTPUT_DIR,
num_train_epochs=NUM_EPOCHS,
per_device_train_batch_size=BATCH_SIZE,
gradient_accumulation_steps=GRADIENT_ACCUMULATION,
learning_rate=LEARNING_RATE,
weight_decay=0.01,
logging_steps=1,
save_strategy="epoch",
save_total_limit=2,
fp16=False, # GTX 1650 uchun FP32 ishlatamiz
optim="paged_adamw_8bit",
warmup_ratio=0.03,
lr_scheduler_type="cosine",
report_to="none",
gradient_checkpointing=True,
max_grad_norm=0.3,
max_length=MAX_SEQ_LENGTH,
dataset_text_field="text",
packing=False,
)
# Trainer
trainer = SFTTrainer(
model=model,
args=sft_config,
train_dataset=dataset,
processing_class=tokenizer,
)
# Training
print("\n" + "=" * 50)
print("TRAINING BOSHLANDI")
print("=" * 50)
trainer.train()
# Adapter saqlash
print("\n" + "=" * 50)
print("Model saqlanmoqda...")
print("=" * 50)
trainer.save_model(OUTPUT_DIR)
tokenizer.save_pretrained(OUTPUT_DIR)
print(f"\nAdapter saqlandi: {OUTPUT_DIR}/")
print("Training yakunlandi!")
# VRAM tozalash
del model
del trainer
torch.cuda.empty_cache()
if __name__ == "__main__":
main()