-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_enwik_torch.py
More file actions
204 lines (165 loc) · 6.59 KB
/
train_enwik_torch.py
File metadata and controls
204 lines (165 loc) · 6.59 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Copyright 2024 DeepMind Technologies Limited (converted to PyTorch)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Trains a language model on the Enwik8 dataset (PyTorch version)."""
from torch.utils.data import DataLoader
from typing import Tuple
import itertools
from typing import Tuple
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torchinfo import summary
from torch.utils.data import DataLoader, Dataset
from tqdm import tqdm
import constants
import data_loaders
import argparse
from transformer_torch import TransformerConfig, TransformerDecoder
parser = argparse.ArgumentParser(description='Train Transformer Decoder on Enwik8 dataset.')
parser.add_argument('-e', '--epochs', type=int, default=1, help='Number of training epochs')
parser.add_argument('-b', '--batch_size', type=int, default=1, help='Batch size')
parser.add_argument('--drill', action='store_true', default=False, help='drill run')
args = parser.parse_args()
# ===========================================================
# 数据定义
# ===========================================================
class Enwik8Dataset(Dataset):
"""Dataset for Enwik8 data."""
def __init__(self, data_chunks) -> None:
self.dataset = data_chunks
def __len__(self) -> int:
return len(self.dataset)
def __getitem__(self, idx: int) -> torch.Tensor:
seq = self.dataset[idx]
seq_ascii = np.frombuffer(seq, dtype=np.uint8)
# 依然回傳 uint8;模型內會轉為 long
return torch.tensor(seq_ascii, dtype=torch.uint8)
# ===========================================================
# 训练流程
# ===========================================================
# ----------------------------
# 自定义句子级 NLLoss
# ----------------------------
class SentenceLevelNLoss(nn.Module):
def __init__(self):
super(SentenceLevelNLoss, self).__init__()
def forward(self, logits, targets):
log_probs = torch.log_softmax(logits, dim=-1)
true_predictions = torch.gather(
log_probs, 2, targets.long().unsqueeze(2)).squeeze(2)
sentence_loss = -torch.mean(torch.sum(true_predictions, dim=1))
return sentence_loss
def train_transformer_decoder(
model: nn.Module,
data_loader: DataLoader,
training_steps: int,
log_every: int,
use_tqdm: bool = True,
device: str = 'cuda',
) -> Tuple[nn.Module, float]:
model.to(device)
model.train()
# Optimizer
optimizer = optim.Adam(model.parameters(), lr=1e-4)
loss_fn = SentenceLevelNLoss()
print('Initialization done, starting training...')
last_loss = 0.0
data_iter = itertools.cycle(data_loader)
for step in tqdm(range(training_steps), disable=not use_tqdm):
batch = next(data_iter).to(device)
optimizer.zero_grad()
logits = model(batch) # [B, T, V] logits
loss = loss_fn(logits, batch)
loss.backward()
optimizer.step()
if log_every > 0 and step % log_every == 0:
print(f'Step {step}, Loss {loss.item()}')
last_loss = loss.item()
return model, last_loss
def train_transformer_decoder_by_epoch(
model: nn.Module,
data_loader: DataLoader,
num_epochs: int,
log_every: int,
use_tqdm: bool = False,
device: str = 'cuda',
) -> Tuple[nn.Module, float]:
"""
按轮次训练Transformer解码器的函数。
参数:
model (nn.Module): 要训练的模型
data_loader (DataLoader): 数据加载器
num_epochs (int): 训练的轮数
log_every (int): 每隔多少步打印一次日志
use_tqdm (bool): 是否使用tqdm显示进度条,默认为True
device (str): 使用的设备,默认为'cuda'
返回:
Tuple[nn.Module, float]: 训练后的模型和最后一轮的最后一个损失值
"""
model.to(device)
model.train()
# 优化器
optimizer = optim.Adam(model.parameters(), lr=1e-4)
loss_fn = SentenceLevelNLoss()
print('Initialization done, starting training...')
last_loss = 0.0
for epoch in tqdm(range(num_epochs), disable=not use_tqdm):
print(f"Epoch {epoch} starts!")
for step, batch in enumerate(data_loader):
if args.drill and step > 10:
break
batch = batch.to(device)
optimizer.zero_grad()
logits = model(batch) # [B, T, V] logits
loss = loss_fn(logits, batch)
loss.backward()
optimizer.step()
if log_every > 0 and step % log_every == 0:
print(f'Epoch {epoch}, Step {step}, Loss {loss.item()}')
last_loss = loss.item()
return model, last_loss
if __name__ == '__main__':
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Create config and model
config = TransformerConfig(vocab_size=constants.ALPHABET_SIZE)
print(config.__dict__)
model = TransformerDecoder(config)
print(summary(model, input_size=(
2, constants.CHUNK_SIZE_BYTES), dtypes=[torch.long]))
# Prepare data loader
sequence_length = constants.CHUNK_SIZE_BYTES
enwik8_data_generator = data_loaders.get_enwik9_iterator(
# 只拿了 10% 用于训练,EnWik9 包含了 8,前10%是 8
num_chunks=constants.NUM_CHUNKS // 10,
sequence_length=sequence_length,
)
enwik8_chunks = list(enwik8_data_generator)
enwik8Dataset = Enwik8Dataset(enwik8_chunks)
batch_size = 8 if args.drill else args.batch_size
enwik8DataLoader = DataLoader(enwik8Dataset, batch_size=batch_size, shuffle=True)
# Start training
model, loss = train_transformer_decoder_by_epoch(
model=model,
data_loader=enwik8DataLoader,
num_epochs=args.epochs,
log_every=500,
device=device
)
print(f'Final loss: {loss}')
# Save model
model_filename = 'params_drill.pth' if args.drill else f'params_epoch_{args.epochs}.pth'
torch.save(model.state_dict(), model_filename)
print(f'Parameters saved in file {model_filename}')