Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,352 +1,431 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
# No need for notebook_login on Hugging Face platform
|
10 |
-
# Authentication is handled automatically
|
11 |
-
|
12 |
import torch
|
13 |
-
from transformers import AutoModelForCausalLM, BitsAndBytesConfig
|
14 |
-
from peft import LoraConfig, TaskType, get_peft_model, prepare_model_for_kbit_training
|
15 |
import gc
|
16 |
-
import
|
17 |
from datasets import Dataset
|
18 |
from huggingface_hub import snapshot_download
|
19 |
-
import
|
20 |
-
from
|
21 |
-
|
22 |
-
# --- Configuration ---
|
23 |
-
YOUR_HF_USERNAME = "Twelve2five"
|
24 |
-
MODEL_REPO_NAME = "llama-3-8b-rvq-resized"
|
25 |
-
DATASET_REPO_NAME = "podcast-dialogue-rvq-pairs-3items"
|
26 |
-
|
27 |
-
hf_model_repo_id = f"{YOUR_HF_USERNAME}/{MODEL_REPO_NAME}"
|
28 |
-
hf_dataset_repo_id = f"{YOUR_HF_USERNAME}/{DATASET_REPO_NAME}"
|
29 |
-
|
30 |
-
# Check if running on multiple GPUs
|
31 |
-
n_gpus = torch.cuda.device_count()
|
32 |
-
print(f"Number of GPUs available: {n_gpus}")
|
33 |
-
|
34 |
-
# --- Quantization Configuration ---
|
35 |
-
bnb_config = BitsAndBytesConfig(
|
36 |
-
load_in_4bit=True,
|
37 |
-
bnb_4bit_quant_type="nf4",
|
38 |
-
bnb_4bit_compute_dtype=torch.bfloat16,
|
39 |
-
bnb_4bit_use_double_quant=True,
|
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 |
-
try:
|
81 |
-
downloaded_repo_root = snapshot_download(
|
82 |
-
repo_id=hf_dataset_repo_id,
|
83 |
-
repo_type="dataset",
|
84 |
-
local_dir=local_download_path,
|
85 |
-
local_dir_use_symlinks=False
|
86 |
)
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
#
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
print(f"Found {len(all_pair_files)} RVQ pair files.")
|
102 |
-
|
103 |
-
# --- Load data from .pt files ---
|
104 |
-
all_data_pairs = []
|
105 |
-
for file_path in tqdm(all_pair_files, desc="Loading pair files"):
|
106 |
try:
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
109 |
except Exception as e:
|
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 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
#
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
"
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
"
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
"offload_optimizer": {
|
290 |
-
"device": "cpu",
|
291 |
-
"pin_memory": True
|
292 |
},
|
293 |
-
"
|
294 |
-
"
|
295 |
-
"pin_memory": True
|
296 |
},
|
297 |
-
"
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
#
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
import sys
|
4 |
+
import os
|
5 |
+
import glob
|
6 |
+
import json
|
7 |
+
import math
|
|
|
|
|
|
|
|
|
8 |
import torch
|
|
|
|
|
9 |
import gc
|
10 |
+
from tqdm import tqdm
|
11 |
from datasets import Dataset
|
12 |
from huggingface_hub import snapshot_download
|
13 |
+
from transformers import AutoModelForCausalLM, BitsAndBytesConfig, TrainingArguments, Trainer
|
14 |
+
from peft import LoraConfig, TaskType, get_peft_model, prepare_model_for_kbit_training
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# Function to run the training process
|
17 |
+
def train_model(
|
18 |
+
hf_username,
|
19 |
+
model_repo_name,
|
20 |
+
dataset_repo_name,
|
21 |
+
epochs=1,
|
22 |
+
batch_size=1,
|
23 |
+
grad_accum_steps=4,
|
24 |
+
learning_rate=1e-4,
|
25 |
+
progress=gr.Progress()
|
26 |
+
):
|
27 |
+
progress(0, desc="Installing dependencies...")
|
28 |
+
# Install required packages if needed
|
29 |
+
try:
|
30 |
+
import transformers
|
31 |
+
import accelerate
|
32 |
+
import bitsandbytes
|
33 |
+
import peft
|
34 |
+
import deepspeed
|
35 |
+
except ImportError:
|
36 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "-U",
|
37 |
+
"transformers", "accelerate", "bitsandbytes", "peft",
|
38 |
+
"torch", "datasets", "huggingface_hub", "deepspeed"])
|
39 |
+
|
40 |
+
# --- Configuration ---
|
41 |
+
progress(0.05, desc="Setting up configuration...")
|
42 |
+
hf_model_repo_id = f"{hf_username}/{model_repo_name}"
|
43 |
+
hf_dataset_repo_id = f"{hf_username}/{dataset_repo_name}"
|
44 |
+
|
45 |
+
log = []
|
46 |
+
log.append(f"Model repo: {hf_model_repo_id}")
|
47 |
+
log.append(f"Dataset repo: {hf_dataset_repo_id}")
|
48 |
+
|
49 |
+
# Check if running on multiple GPUs
|
50 |
+
n_gpus = torch.cuda.device_count()
|
51 |
+
log.append(f"Number of GPUs available: {n_gpus}")
|
52 |
+
|
53 |
+
# --- Quantization Configuration ---
|
54 |
+
bnb_config = BitsAndBytesConfig(
|
55 |
+
load_in_4bit=True,
|
56 |
+
bnb_4bit_quant_type="nf4",
|
57 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
58 |
+
bnb_4bit_use_double_quant=True,
|
59 |
)
|
60 |
+
|
61 |
+
# --- Load Base Model (with quantization) ---
|
62 |
+
progress(0.1, desc="Loading base model...")
|
63 |
+
try:
|
64 |
+
model = AutoModelForCausalLM.from_pretrained(
|
65 |
+
hf_model_repo_id,
|
66 |
+
quantization_config=bnb_config,
|
67 |
+
device_map="auto",
|
68 |
+
trust_remote_code=True
|
69 |
+
)
|
70 |
+
log.append(f"Loaded model vocab size: {model.config.vocab_size}")
|
71 |
+
log.append(f"Input embedding shape: {model.get_input_embeddings().weight.shape}")
|
72 |
+
except Exception as e:
|
73 |
+
error_msg = f"Error loading model from Hub: {e}"
|
74 |
+
log.append(error_msg)
|
75 |
+
return "\n".join(log)
|
76 |
+
|
77 |
+
# --- Prepare for K-bit Training & Apply LoRA ---
|
78 |
+
progress(0.15, desc="Preparing model for fine-tuning...")
|
79 |
+
model = prepare_model_for_kbit_training(model)
|
80 |
+
|
81 |
+
lora_config = LoraConfig(
|
82 |
+
task_type=TaskType.CAUSAL_LM,
|
83 |
+
r=16,
|
84 |
+
lora_alpha=32,
|
85 |
+
lora_dropout=0.05,
|
86 |
+
bias="none",
|
87 |
+
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
)
|
89 |
+
peft_model = get_peft_model(model, lora_config)
|
90 |
+
trainable_params = peft_model.print_trainable_parameters()
|
91 |
+
log.append(f"Trainable parameters: {trainable_params}")
|
92 |
+
model_to_train = peft_model
|
93 |
+
|
94 |
+
# Cleanup
|
95 |
+
gc.collect()
|
96 |
+
if torch.cuda.is_available():
|
97 |
+
torch.cuda.empty_cache()
|
98 |
+
|
99 |
+
# --- Load Dataset from Hub ---
|
100 |
+
progress(0.2, desc="Downloading dataset...")
|
101 |
+
local_download_path = "./downloaded_dataset_files"
|
102 |
+
|
|
|
|
|
|
|
|
|
|
|
103 |
try:
|
104 |
+
downloaded_repo_root = snapshot_download(
|
105 |
+
repo_id=hf_dataset_repo_id,
|
106 |
+
repo_type="dataset",
|
107 |
+
local_dir=local_download_path,
|
108 |
+
local_dir_use_symlinks=False
|
109 |
+
)
|
110 |
+
log.append(f"Dataset repository content downloaded to: {downloaded_repo_root}")
|
111 |
except Exception as e:
|
112 |
+
error_msg = f"Error downloading dataset repository from Hub: {e}"
|
113 |
+
log.append(error_msg)
|
114 |
+
return "\n".join(log)
|
115 |
+
|
116 |
+
# --- Find and load the .pt files ---
|
117 |
+
progress(0.25, desc="Finding dataset files...")
|
118 |
+
pairs_dir = os.path.join(downloaded_repo_root, "final_rvq_pairs")
|
119 |
+
all_pair_files = glob.glob(os.path.join(pairs_dir, "*_rvq_pairs.pt"))
|
120 |
+
|
121 |
+
if not all_pair_files:
|
122 |
+
all_pair_files = glob.glob(os.path.join(downloaded_repo_root, "*_rvq_pairs.pt"))
|
123 |
+
if not all_pair_files:
|
124 |
+
error_msg = "No RVQ pair files found in expected directories"
|
125 |
+
log.append(error_msg)
|
126 |
+
return "\n".join(log)
|
127 |
+
|
128 |
+
log.append(f"Found {len(all_pair_files)} RVQ pair files.")
|
129 |
+
|
130 |
+
# --- Load data from .pt files ---
|
131 |
+
progress(0.3, desc="Loading dataset files...")
|
132 |
+
all_data_pairs = []
|
133 |
+
for i, file_path in enumerate(all_pair_files):
|
134 |
+
progress(0.3 + (0.1 * i / len(all_pair_files)), desc=f"Loading file {i+1}/{len(all_pair_files)}")
|
135 |
+
try:
|
136 |
+
episode_pairs = torch.load(file_path, map_location='cpu')
|
137 |
+
all_data_pairs.extend(episode_pairs)
|
138 |
+
except Exception as e:
|
139 |
+
log.append(f"Warning: Could not load file {file_path}: {e}")
|
140 |
+
|
141 |
+
if not all_data_pairs:
|
142 |
+
error_msg = "No valid data pairs were loaded"
|
143 |
+
log.append(error_msg)
|
144 |
+
return "\n".join(log)
|
145 |
+
|
146 |
+
log.append(f"Loaded a total of {len(all_data_pairs)} training pairs into memory.")
|
147 |
+
|
148 |
+
# --- Convert to HF Dataset ---
|
149 |
+
progress(0.45, desc="Converting to Hugging Face Dataset...")
|
150 |
+
def prepare_for_dataset(batch):
|
151 |
+
output = {'input_ids': [], 'labels': []}
|
152 |
+
for item in batch:
|
153 |
+
output['input_ids'].append(item['input_ids'].cpu().tolist())
|
154 |
+
output['labels'].append(item['labels'].cpu().tolist())
|
155 |
+
return output
|
156 |
+
|
157 |
+
chunk_size = 1000
|
158 |
+
processed_data = {'input_ids': [], 'labels': []}
|
159 |
+
|
160 |
+
total_chunks = len(range(0, len(all_data_pairs), chunk_size))
|
161 |
+
for i in range(0, len(all_data_pairs), chunk_size):
|
162 |
+
chunk_idx = i // chunk_size
|
163 |
+
progress(0.45 + (0.1 * chunk_idx / total_chunks),
|
164 |
+
desc=f"Processing chunk {chunk_idx+1}/{total_chunks}")
|
165 |
+
batch = all_data_pairs[i:i + chunk_size]
|
166 |
+
prepared_batch = prepare_for_dataset(batch)
|
167 |
+
processed_data['input_ids'].extend(prepared_batch['input_ids'])
|
168 |
+
processed_data['labels'].extend(prepared_batch['labels'])
|
169 |
+
|
170 |
+
hf_dataset = Dataset.from_dict(processed_data)
|
171 |
+
|
172 |
+
# Transform to get tensors back
|
173 |
+
hf_dataset.set_transform(lambda batch: {
|
174 |
+
'input_ids': [torch.tensor(ids, dtype=torch.long) for ids in batch['input_ids']],
|
175 |
+
'labels': [torch.tensor(lbls, dtype=torch.long) for lbls in batch['labels']]
|
176 |
+
})
|
177 |
+
|
178 |
+
train_dataset = hf_dataset
|
179 |
+
|
180 |
+
# Cleanup
|
181 |
+
del all_data_pairs
|
182 |
+
del processed_data
|
183 |
+
gc.collect()
|
184 |
+
|
185 |
+
# --- Define Data Collator ---
|
186 |
+
progress(0.55, desc="Defining data collator...")
|
187 |
+
def seq2seq_causal_collator(features):
|
188 |
+
batch = {}
|
189 |
+
concatenated_input_ids = []
|
190 |
+
concatenated_labels = []
|
191 |
+
max_len = 0
|
192 |
+
|
193 |
+
# First pass: Concatenate, create masked labels, find max length
|
194 |
+
for feature in features:
|
195 |
+
input_ids = feature['input_ids']
|
196 |
+
labels = feature['labels']
|
197 |
+
|
198 |
+
if input_ids.dim() > 1: input_ids = input_ids.squeeze()
|
199 |
+
if labels.dim() > 1: labels = labels.squeeze()
|
200 |
+
|
201 |
+
context_len = input_ids.shape[0]
|
202 |
+
target_len = labels.shape[0]
|
203 |
+
|
204 |
+
combined_ids = torch.cat([input_ids, labels], dim=0)
|
205 |
+
concatenated_input_ids.append(combined_ids)
|
206 |
+
|
207 |
+
masked_labels = torch.cat([
|
208 |
+
torch.full((context_len,), -100, dtype=torch.long, device=input_ids.device),
|
209 |
+
labels
|
210 |
+
], dim=0)
|
211 |
+
concatenated_labels.append(masked_labels)
|
212 |
+
|
213 |
+
if combined_ids.shape[0] > max_len:
|
214 |
+
max_len = combined_ids.shape[0]
|
215 |
+
|
216 |
+
# Second pass: Pad to max length
|
217 |
+
padded_input_ids = []
|
218 |
+
padded_labels = []
|
219 |
+
input_pad_token_id = 0
|
220 |
+
label_pad_token_id = -100
|
221 |
+
|
222 |
+
for i in range(len(features)):
|
223 |
+
ids = concatenated_input_ids[i]
|
224 |
+
lbls = concatenated_labels[i]
|
225 |
+
|
226 |
+
padding_len = max_len - ids.shape[0]
|
227 |
+
|
228 |
+
padded_input_ids.append(torch.nn.functional.pad(
|
229 |
+
ids, (0, padding_len), value=input_pad_token_id
|
230 |
+
))
|
231 |
+
padded_labels.append(torch.nn.functional.pad(
|
232 |
+
lbls, (0, padding_len), value=label_pad_token_id
|
233 |
+
))
|
234 |
+
|
235 |
+
# Stack and create final batch
|
236 |
+
batch['input_ids'] = torch.stack(padded_input_ids)
|
237 |
+
batch['labels'] = torch.stack(padded_labels)
|
238 |
+
batch['attention_mask'] = batch['input_ids'].ne(input_pad_token_id).long()
|
239 |
+
|
240 |
+
return batch
|
241 |
+
|
242 |
+
data_collator = seq2seq_causal_collator
|
243 |
+
|
244 |
+
# --- Define Training Arguments and Initialize Trainer ---
|
245 |
+
progress(0.65, desc="Setting up training configuration...")
|
246 |
+
|
247 |
+
# Output directories
|
248 |
+
OUTPUT_TRAINING_DIR = "./llama3-8b-rvq-qlora-finetuned-run"
|
249 |
+
LOGGING_DIR = "./llama3-8b-rvq-qlora-logs-run"
|
250 |
+
|
251 |
+
# Training parameters - adjusted for 4x T4 GPUs
|
252 |
+
NUM_EPOCHS = int(epochs)
|
253 |
+
BATCH_SIZE_PER_DEVICE = int(batch_size) # Smaller per-device batch size to avoid OOM
|
254 |
+
GRAD_ACCUMULATION_STEPS = int(grad_accum_steps)
|
255 |
+
LEARNING_RATE = float(learning_rate)
|
256 |
+
WEIGHT_DECAY = 0.01
|
257 |
+
WARMUP_RATIO = 0.03
|
258 |
+
LR_SCHEDULER = "cosine"
|
259 |
+
OPTIMIZER = "paged_adamw_8bit"
|
260 |
+
|
261 |
+
# Calculate total steps and warmup steps
|
262 |
+
# Total batch size is now batch_size × num_gpus × grad_accum_steps
|
263 |
+
total_train_batch_size = BATCH_SIZE_PER_DEVICE * n_gpus * GRAD_ACCUMULATION_STEPS
|
264 |
+
num_training_steps = math.ceil((len(train_dataset) * NUM_EPOCHS) / total_train_batch_size)
|
265 |
+
num_warmup_steps = int(num_training_steps * WARMUP_RATIO)
|
266 |
+
|
267 |
+
# Logging/Saving frequency
|
268 |
+
steps_per_epoch = math.ceil(len(train_dataset) / total_train_batch_size)
|
269 |
+
LOGGING_STEPS = max(10, steps_per_epoch // 15)
|
270 |
+
SAVE_STEPS = max(50, steps_per_epoch // 10)
|
271 |
+
|
272 |
+
log.append(f"Dataset size: {len(train_dataset)}")
|
273 |
+
log.append(f"Number of GPUs: {n_gpus}")
|
274 |
+
log.append(f"Batch size per device: {BATCH_SIZE_PER_DEVICE}")
|
275 |
+
log.append(f"Gradient Accumulation steps: {GRAD_ACCUMULATION_STEPS}")
|
276 |
+
log.append(f"Total train batch size (effective): {total_train_batch_size}")
|
277 |
+
log.append(f"Total optimization steps: {num_training_steps}")
|
278 |
+
log.append(f"Warmup steps: {num_warmup_steps}")
|
279 |
+
|
280 |
+
# --- Create DeepSpeed configuration file ---
|
281 |
+
progress(0.7, desc="Creating DeepSpeed configuration...")
|
282 |
+
# DeepSpeed ZeRO-3 config optimized for T4 GPUs
|
283 |
+
ds_config = {
|
284 |
+
"fp16": {
|
285 |
+
"enabled": "auto",
|
286 |
+
"loss_scale": 0,
|
287 |
+
"loss_scale_window": 1000,
|
288 |
+
"initial_scale_power": 16,
|
289 |
+
"hysteresis": 2,
|
290 |
+
"min_loss_scale": 1
|
|
|
|
|
|
|
291 |
},
|
292 |
+
"bf16": {
|
293 |
+
"enabled": "auto"
|
|
|
294 |
},
|
295 |
+
"zero_optimization": {
|
296 |
+
"stage": 3,
|
297 |
+
"offload_optimizer": {
|
298 |
+
"device": "cpu",
|
299 |
+
"pin_memory": True
|
300 |
+
},
|
301 |
+
"offload_param": {
|
302 |
+
"device": "cpu",
|
303 |
+
"pin_memory": True
|
304 |
+
},
|
305 |
+
"overlap_comm": True,
|
306 |
+
"contiguous_gradients": True,
|
307 |
+
"reduce_bucket_size": "auto",
|
308 |
+
"stage3_prefetch_bucket_size": "auto",
|
309 |
+
"stage3_param_persistence_threshold": "auto",
|
310 |
+
"gather_16bit_weights_on_model_save": True,
|
311 |
+
"stage3_max_live_parameters": 1e9,
|
312 |
+
"stage3_max_reuse_distance": 1e9
|
313 |
+
},
|
314 |
+
"gradient_accumulation_steps": GRAD_ACCUMULATION_STEPS,
|
315 |
+
"gradient_clipping": "auto",
|
316 |
+
"steps_per_print": 10,
|
317 |
+
"train_batch_size": "auto",
|
318 |
+
"train_micro_batch_size_per_gpu": "auto",
|
319 |
+
"wall_clock_breakdown": False
|
320 |
+
}
|
321 |
+
|
322 |
+
with open("ds_config.json", "w") as f:
|
323 |
+
json.dump(ds_config, f, indent=4)
|
324 |
+
|
325 |
+
# Configure for multi-GPU training using DeepSpeed
|
326 |
+
progress(0.75, desc="Setting up training arguments...")
|
327 |
+
training_args = TrainingArguments(
|
328 |
+
output_dir=OUTPUT_TRAINING_DIR,
|
329 |
+
num_train_epochs=NUM_EPOCHS,
|
330 |
+
per_device_train_batch_size=BATCH_SIZE_PER_DEVICE,
|
331 |
+
gradient_accumulation_steps=GRAD_ACCUMULATION_STEPS,
|
332 |
+
optim=OPTIMIZER,
|
333 |
+
logging_dir=LOGGING_DIR,
|
334 |
+
logging_strategy="steps",
|
335 |
+
logging_steps=LOGGING_STEPS,
|
336 |
+
save_strategy="steps",
|
337 |
+
save_steps=SAVE_STEPS,
|
338 |
+
save_total_limit=2,
|
339 |
+
learning_rate=LEARNING_RATE,
|
340 |
+
weight_decay=WEIGHT_DECAY,
|
341 |
+
warmup_steps=num_warmup_steps,
|
342 |
+
lr_scheduler_type=LR_SCHEDULER,
|
343 |
+
report_to="tensorboard",
|
344 |
+
bf16=True if torch.cuda.is_available() and torch.cuda.is_bf16_supported() else False,
|
345 |
+
gradient_checkpointing=True,
|
346 |
+
gradient_checkpointing_kwargs={'use_reentrant': False},
|
347 |
+
|
348 |
+
# Multi-GPU specific settings
|
349 |
+
deepspeed="ds_config.json",
|
350 |
+
ddp_find_unused_parameters=False,
|
351 |
+
)
|
352 |
+
|
353 |
+
# --- Initialize Trainer ---
|
354 |
+
progress(0.8, desc="Initializing trainer...")
|
355 |
+
trainer = Trainer(
|
356 |
+
model=model_to_train,
|
357 |
+
args=training_args,
|
358 |
+
train_dataset=train_dataset,
|
359 |
+
data_collator=data_collator,
|
360 |
+
)
|
361 |
+
|
362 |
+
log.append("Trainer initialized with DeepSpeed for multi-GPU training.")
|
363 |
+
|
364 |
+
# --- Start Training ---
|
365 |
+
# Clear cache before starting
|
366 |
+
gc.collect()
|
367 |
+
if torch.cuda.is_available():
|
368 |
+
torch.cuda.empty_cache()
|
369 |
+
|
370 |
+
try:
|
371 |
+
progress(0.85, desc="Starting training...")
|
372 |
+
log.append("Starting distributed training on multiple GPUs...")
|
373 |
+
train_result = trainer.train()
|
374 |
+
progress(0.95, desc="Saving model...")
|
375 |
+
|
376 |
+
# Save final model (adapter weights) and training state
|
377 |
+
final_save_path = os.path.join(training_args.output_dir, "final_checkpoint")
|
378 |
+
log.append(f"Saving final model checkpoint to {final_save_path}...")
|
379 |
+
trainer.save_model(final_save_path)
|
380 |
+
trainer.save_state()
|
381 |
+
|
382 |
+
# Log metrics
|
383 |
+
metrics = train_result.metrics
|
384 |
+
trainer.log_metrics("train", metrics)
|
385 |
+
trainer.save_metrics("train", metrics)
|
386 |
+
|
387 |
+
for key, value in metrics.items():
|
388 |
+
log.append(f"{key}: {value}")
|
389 |
+
|
390 |
+
except Exception as e:
|
391 |
+
error_msg = f"An error occurred during training: {e}"
|
392 |
+
log.append(error_msg)
|
393 |
+
return "\n".join(log)
|
394 |
+
|
395 |
+
progress(1.0, desc="Training complete!")
|
396 |
+
log.append("Multi-GPU training process complete.")
|
397 |
+
return "\n".join(log)
|
398 |
+
|
399 |
+
# Define the Gradio interface
|
400 |
+
def create_interface():
|
401 |
+
with gr.Blocks(title="Llama 3 8B RVQ Fine-tuning") as demo:
|
402 |
+
gr.Markdown("# Llama 3 8B RVQ LoRA Fine-tuning")
|
403 |
+
gr.Markdown("Fine-tune a Llama 3 8B model with RVQ token embeddings using LoRA on multiple GPUs")
|
404 |
+
|
405 |
+
with gr.Row():
|
406 |
+
with gr.Column():
|
407 |
+
hf_username = gr.Textbox(label="HuggingFace Username", value="Twelve2five")
|
408 |
+
model_repo = gr.Textbox(label="Model Repository Name", value="llama-3-8b-rvq-resized")
|
409 |
+
dataset_repo = gr.Textbox(label="Dataset Repository Name", value="podcast-dialogue-rvq-pairs-3items")
|
410 |
+
|
411 |
+
with gr.Column():
|
412 |
+
epochs = gr.Number(label="Number of Epochs", value=1, minimum=1, maximum=10)
|
413 |
+
batch_size = gr.Number(label="Batch Size per Device", value=1, minimum=1, maximum=8)
|
414 |
+
grad_accum = gr.Number(label="Gradient Accumulation Steps", value=4, minimum=1, maximum=16)
|
415 |
+
lr = gr.Number(label="Learning Rate", value=1e-4)
|
416 |
+
|
417 |
+
start_btn = gr.Button("Start Training")
|
418 |
+
output = gr.Textbox(label="Training Log", lines=20)
|
419 |
+
|
420 |
+
start_btn.click(
|
421 |
+
fn=train_model,
|
422 |
+
inputs=[hf_username, model_repo, dataset_repo, epochs, batch_size, grad_accum, lr],
|
423 |
+
outputs=output
|
424 |
+
)
|
425 |
+
|
426 |
+
return demo
|
427 |
+
|
428 |
+
# Create and launch the interface
|
429 |
+
demo = create_interface()
|
430 |
+
if __name__ == "__main__":
|
431 |
+
demo.launch()
|