-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathtransformer.py
More file actions
210 lines (164 loc) · 7.25 KB
/
transformer.py
File metadata and controls
210 lines (164 loc) · 7.25 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
205
206
207
208
209
210
"""Demo of a simple transformer language model.
Code is adapted from the PyTorch examples at
https://github.com/pytorch/examples/blob/main/word_language_model
"""
import math
import os
from pathlib import Path
from typing import Optional
import torch
import torch.nn as nn
import torch.nn.functional as F
from lightning_utilities.core.imports import RequirementCache
from torch import Tensor
from torch.nn.modules import MultiheadAttention
from torch.utils.data import DataLoader, Dataset
from lightning.pytorch import LightningModule
_REQUESTS_AVAILABLE = RequirementCache("requests")
if hasattr(MultiheadAttention, "_reset_parameters") and not hasattr(MultiheadAttention, "reset_parameters"):
# See https://github.com/pytorch/pytorch/issues/107909
MultiheadAttention.reset_parameters = MultiheadAttention._reset_parameters
class Transformer(nn.Module):
def __init__(
self,
vocab_size: int = 33278, # default for WikiText2
ninp: int = 200,
nhead: int = 2,
nhid: int = 200,
nlayers: int = 2,
dropout: float = 0.2,
) -> None:
super().__init__()
self.pos_encoder = PositionalEncoding(ninp, dropout)
self.embedding = nn.Embedding(vocab_size, ninp)
self.transformer = nn.Transformer(
d_model=ninp,
nhead=nhead,
num_encoder_layers=nlayers,
num_decoder_layers=nlayers,
dim_feedforward=nhid,
dropout=dropout,
batch_first=True,
)
self.decoder = nn.Linear(ninp, vocab_size)
self.ninp = ninp
self.vocab_size = vocab_size
self.src_mask: Optional[Tensor] = None
def generate_square_subsequent_mask(self, size: int) -> Tensor:
"""Generate a square mask for the sequence to prevent future tokens from being seen."""
mask = torch.triu(torch.ones(size, size), diagonal=1)
return mask.float().masked_fill(mask == 1, float("-inf")).masked_fill(mask == 0, 0.0)
def forward(self, inputs: Tensor, target: Tensor, mask: Optional[Tensor] = None) -> Tensor:
_, t = inputs.shape
# Generate source mask to prevent future token leakage
if self.src_mask is None or self.src_mask.size(0) != t:
self.src_mask = self.generate_square_subsequent_mask(t).to(inputs.device)
# Generate target mask if not provided
if mask is None:
mask = self.generate_square_subsequent_mask(t).to(inputs.device)
src = self.pos_encoder(self.embedding(inputs) * math.sqrt(self.ninp))
target = self.pos_encoder(self.embedding(target) * math.sqrt(self.ninp))
output = self.transformer(src, target, tgt_mask=mask)
output = self.decoder(output)
output = F.log_softmax(output, dim=-1)
return output.view(-1, self.vocab_size)
class PositionalEncoding(nn.Module):
def __init__(self, dim: int, dropout: float = 0.1, max_len: int = 5000) -> None:
super().__init__()
self.dropout = nn.Dropout(p=dropout)
self.dim = dim
self.max_len = max_len
self.pe: Optional[Tensor] = None
def forward(self, x: Tensor) -> Tensor:
if self.pe is None:
# 1) can't use buffer, see https://github.com/pytorch/pytorch/issues/68407
# 2) can't use parameter because pe gets sliced and DDP requires all params to participate in forward
# TODO: Could make this a `nn.Parameter` with `requires_grad=False`
self.pe = self._init_pos_encoding(device=x.device)
x = x + self.pe[:, x.size(1)]
return self.dropout(x)
def _init_pos_encoding(self, device: torch.device) -> Tensor:
pe = torch.zeros(self.max_len, self.dim, device=device)
position = torch.arange(0, self.max_len, dtype=torch.float, device=device).unsqueeze(1)
div_term = torch.exp(torch.arange(0, self.dim, 2, device=device).float() * (-math.log(10000.0) / self.dim))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
return pe.unsqueeze(0)
class WikiText2(Dataset):
"""Mini version of WikiText2."""
def __init__(self, data_dir: Path = Path("./data"), block_size: int = 35, download: bool = True) -> None:
super().__init__()
self.path = data_dir / "wikitext-2.txt"
if download:
self.download(self.path)
self.data, self.dictionary = tokenize(self.path)
self.block_size = block_size
@property
def vocab_size(self) -> int:
return len(self.dictionary)
def __len__(self) -> int:
return len(self.data) // self.block_size - 1
def __getitem__(self, index: int) -> tuple[Tensor, Tensor]:
start = index * self.block_size
end = start + self.block_size
inputs = self.data[start:end]
target = self.data[(start + 1) : (end + 1)]
return inputs, target
@staticmethod
def download(destination: Path) -> None:
if not _REQUESTS_AVAILABLE:
raise ModuleNotFoundError(str(_REQUESTS_AVAILABLE))
import requests
os.makedirs(destination.parent, exist_ok=True)
url = "https://raw.githubusercontent.com/pytorch/examples/main/word_language_model/data/wikitext-2/train.txt"
if os.path.exists(destination):
return
with open(destination, "w") as f:
f.write(requests.get(url).text)
class Dictionary:
def __init__(self) -> None:
self.word2idx: dict[str, int] = {}
self.idx2word: list[str] = []
def add_word(self, word: str) -> int:
if word not in self.word2idx:
self.idx2word.append(word)
self.word2idx[word] = len(self.idx2word) - 1
return self.word2idx[word]
def __len__(self) -> int:
return len(self.idx2word)
def tokenize(path: Path) -> tuple[Tensor, Dictionary]:
dictionary = Dictionary()
assert os.path.exists(path)
# Add words to the dictionary
with open(path, encoding="utf8") as f:
for line in f:
words = line.split() + ["<eos>"]
for word in words:
dictionary.add_word(word)
# Tokenize file content
with open(path, encoding="utf8") as f:
idss: list[Tensor] = []
for line in f:
words = line.split() + ["<eos>"]
ids: list[int] = []
for word in words:
ids.append(dictionary.word2idx[word])
idss.append(torch.tensor(ids).type(torch.int64))
return torch.cat(idss), dictionary
class LightningTransformer(LightningModule):
def __init__(self, vocab_size: int = 33278) -> None:
super().__init__()
self.model = Transformer(vocab_size=vocab_size)
def forward(self, inputs: Tensor, target: Tensor) -> Tensor:
return self.model(inputs, target)
def training_step(self, batch: tuple[Tensor, Tensor], batch_idx: int) -> Tensor:
inputs, target = batch
output = self(inputs, target)
return torch.nn.functional.nll_loss(output, target.view(-1))
def configure_optimizers(self) -> torch.optim.Optimizer:
return torch.optim.SGD(self.model.parameters(), lr=0.1)
def prepare_data(self) -> None:
WikiText2(download=True)
def train_dataloader(self) -> DataLoader:
dataset = WikiText2()
return DataLoader(dataset)