-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_translation.py
203 lines (168 loc) · 6.48 KB
/
train_translation.py
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import torch
from transformers import MT5ForConditionalGeneration, T5Tokenizer
from datasets import load_dataset
from torch.utils.data import DataLoader
from torch.optim import AdamW
from tqdm import tqdm
import wandb
import os
from datetime import datetime
def setup_wandb():
"""Initialize wandb run"""
# Set up wandb API key
wandb.login(key="6f56843a9fda5209d74d64d2c778e60eddbf7ef2")
return wandb.init(
project="mt5-translation",
config={
"model_name": "google/mt5-large",
"batch_size": 8,
"learning_rate": 1e-4,
"num_epochs": 20,
"max_length": 64,
"dataset": "wmt14",
"dataset_size": 100000
}
)
def create_model_dir():
"""Create directory for saving models"""
# Create base models directory
base_dir = "models"
os.makedirs(base_dir, exist_ok=True)
# Create timestamped run directory
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
run_dir = os.path.join(base_dir, f"run_{timestamp}")
os.makedirs(run_dir, exist_ok=True)
return run_dir
def collate_fn(batch, tokenizer):
input_ids = [item['input_ids'] for item in batch]
attention_mask = [item['attention_mask'] for item in batch]
labels = [item['labels'] for item in batch]
input_ids = torch.nn.utils.rnn.pad_sequence([torch.tensor(x) for x in input_ids], batch_first=True, padding_value=tokenizer.pad_token_id)
attention_mask = torch.nn.utils.rnn.pad_sequence([torch.tensor(x) for x in attention_mask], batch_first=True, padding_value=0)
labels = torch.nn.utils.rnn.pad_sequence([torch.tensor(x) for x in labels], batch_first=True, padding_value=-100)
return {
'input_ids': input_ids,
'attention_mask': attention_mask,
'labels': labels
}
def main():
# Set up wandb API key
wandb.login(key="6f56843a9fda5209d74d64d2c778e60eddbf7ef2")
# Initialize wandb
run = setup_wandb()
# Create model directory
model_dir = create_model_dir()
print(f"Saving models to: {model_dir}")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
# Load model and tokenizer
model_name = wandb.config.model_name
model = MT5ForConditionalGeneration.from_pretrained(model_name).to(device)
tokenizer = T5Tokenizer.from_pretrained(model_name)
# Load dataset
dataset = load_dataset("wmt14", "de-en", split=f"train[:{wandb.config.dataset_size}]")
def preprocess_data(examples):
inputs = [ex['en'] for ex in examples['translation']]
targets = [ex['de'] for ex in examples['translation']]
model_inputs = tokenizer(
inputs,
padding=True,
truncation=True,
max_length=wandb.config.max_length,
return_tensors=None
)
with tokenizer.as_target_tokenizer():
labels = tokenizer(
targets,
padding=True,
truncation=True,
max_length=wandb.config.max_length,
return_tensors=None
)
return {
'input_ids': model_inputs['input_ids'],
'attention_mask': model_inputs['attention_mask'],
'labels': labels['input_ids']
}
# Process dataset
dataset = dataset.map(preprocess_data, batched=True, batch_size=wandb.config.batch_size)
dataset.set_format(type='torch')
dataloader = DataLoader(
dataset,
batch_size=wandb.config.batch_size,
shuffle=True,
collate_fn=lambda batch: collate_fn(batch, tokenizer)
)
# Training setup
optimizer = AdamW(model.parameters(), lr=wandb.config.learning_rate)
best_loss = float('inf')
# Training loop
model.train()
for epoch in range(wandb.config.num_epochs):
total_loss = 0
num_batches = 0
progress_bar = tqdm(dataloader, desc=f"Epoch {epoch + 1}")
for batch in progress_bar:
optimizer.zero_grad()
input_ids = batch['input_ids'].to(device)
attention_mask = batch['attention_mask'].to(device)
labels = batch['labels'].to(device)
outputs = model(
input_ids=input_ids,
attention_mask=attention_mask,
labels=labels
)
loss = outputs.loss
loss.backward()
optimizer.step()
# Update metrics
loss_value = loss.item()
total_loss += loss_value
num_batches += 1
# Update progress bar and wandb
current_loss = total_loss / num_batches
progress_bar.set_postfix({'loss': f'{current_loss:.4f}'})
wandb.log({
"batch_loss": loss_value,
"running_loss": current_loss,
"epoch": epoch + 1
})
# Calculate epoch statistics
avg_loss = total_loss / num_batches
print(f"\nEpoch {epoch + 1} - Average loss: {avg_loss:.4f}")
# Log epoch metrics to wandb
wandb.log({
"epoch_loss": avg_loss,
"epoch": epoch + 1
})
# Save model if it's the best so far
if avg_loss < best_loss:
best_loss = avg_loss
print(f"New best loss! Saving model...")
model_path = os.path.join(model_dir, f"best_model_epoch_{epoch + 1}")
model.save_pretrained(model_path)
tokenizer.save_pretrained(model_path)
# Log model to wandb
artifact = wandb.Artifact(
name=f"model-epoch-{epoch + 1}",
type="model",
description=f"Model checkpoint from epoch {epoch + 1} with loss {avg_loss:.4f}"
)
artifact.add_dir(model_path)
run.log_artifact(artifact)
# Save final model
final_model_path = os.path.join(model_dir, "final_model")
model.save_pretrained(final_model_path)
tokenizer.save_pretrained(final_model_path)
# Log final model to wandb
final_artifact = wandb.Artifact(
name="final_model",
type="model",
description=f"Final model after training with best loss {best_loss:.4f}"
)
final_artifact.add_dir(final_model_path)
run.log_artifact(final_artifact)
# Finish wandb run
wandb.finish()
if __name__ == "__main__":
main()