forked from lucidrains/speculative-decoding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_prophet.py
162 lines (119 loc) · 4.4 KB
/
train_prophet.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
import gzip
import random
import tqdm
import numpy as np
import time
from functools import wraps, partial
import torch
from torch.optim import Adam
from torch.nn import functional as F
from torch.cuda import synchronize, Event
from torch.utils.data import DataLoader, Dataset
timer = partial(Event, enable_timing = True)
from speculative_decoding.speculative_decoding_with_prophet import (
Decoder,
ModelWithProphetWrapper,
base_decoding,
speculative_decoding_with_prophet_model
)
# constants
NUM_BATCHES = int(1e5)
BATCH_SIZE = 4
GRAD_ACCUM_EVERY = 4
LEARNING_RATE = 1e-4
PRIME_LENGTH = 128
GENERATE_EVERY = 100
GENERATE_LENGTH = 512
SEQ_LEN = 512
GAMMA = 5
TRAIN_PROPHET = True
DEVICE_STR = 'cuda' if torch.cuda.is_available() else 'cpu'
# helpers
def cycle(loader):
while True:
for data in loader:
yield data
def decode_token(token):
return str(chr(max(32, token)))
def decode_tokens(tokens):
return "".join(list(map(decode_token, tokens)))
def benchmark(fn):
@wraps(fn)
def inner(*args, **kwargs):
start_event = timer()
end_event = timer()
start_event.record()
out = fn(*args, **kwargs)
end_event.record()
torch.cuda.synchronize()
elapsed_time_ms = start_event.elapsed_time(end_event)
return out, elapsed_time_ms
return inner
# instantiate transformer
device = torch.device(DEVICE_STR)
model = Decoder(
num_tokens = 256,
dim = 512,
depth = 10
)
prophet = Decoder(
num_tokens = 256,
dim = 512,
depth = 2
)
model_and_prophet = ModelWithProphetWrapper(
model,
prophet,
prophet_train_length = GAMMA + 2,
num_leading_start_tokens = 2,
detach_model_embed_for_prophet = False # train end to end, shouldn't hurt (although benefits is dubious) given ProphetNet paper - of course, trying to get to the bottom of the benefits in spec decoding setting here
).to(device)
# prepare enwik8 data
with gzip.open("./data/enwik8.gz") as file:
data = np.frombuffer(file.read(int(95e6)), dtype=np.uint8).copy()
np_train, np_valid = np.split(data, [int(90e6)])
data_train, data_val = torch.from_numpy(np_train), torch.from_numpy(np_valid)
class TextSamplerDataset(Dataset):
def __init__(self, data, seq_len):
super().__init__()
self.data = data
self.seq_len = seq_len
def __getitem__(self, index):
rand_start = torch.randint(0, self.data.size(0) - self.seq_len, (1,))
full_seq = self.data[rand_start : rand_start + self.seq_len + 1].long()
return full_seq.to(device)
def __len__(self):
return self.data.size(0) // self.seq_len
train_dataset = TextSamplerDataset(data_train, SEQ_LEN)
val_dataset = TextSamplerDataset(data_val, SEQ_LEN)
train_loader = cycle(DataLoader(train_dataset, batch_size=BATCH_SIZE))
# optimizer
params = model_and_prophet.parameters() if TRAIN_PROPHET else model.parameters()
optim = Adam(params, lr = LEARNING_RATE)
# training
for i in tqdm.tqdm(range(NUM_BATCHES), mininterval = 10.0, desc = "training"):
model_and_prophet.train()
for _ in range(GRAD_ACCUM_EVERY):
data = next(train_loader)
total_loss, (loss, prophet_loss) = model_and_prophet(data)
(total_loss / GRAD_ACCUM_EVERY).backward()
print(f"training loss: {loss.item():.3f}")
print(f"training prophet loss: {prophet_loss.item():.3f}")
torch.nn.utils.clip_grad_norm_(model_and_prophet.parameters(), 0.5)
optim.step()
optim.zero_grad()
if i % GENERATE_EVERY == 0:
model_and_prophet.eval()
inp = random.choice(val_dataset)[:PRIME_LENGTH]
prime = decode_tokens(inp)
print(f"%s \n\n %s", (prime, "*" * 100))
prompt = inp[None, ...]
sampled, base_decode_elapsed = benchmark(base_decoding)(model, prompt, GENERATE_LENGTH)
(spec_decode_sampled, num_accepted), spec_decode_elapsed = benchmark(speculative_decoding_with_prophet_model)(model_and_prophet, prompt, GENERATE_LENGTH, GAMMA)
base_decode_output = decode_tokens(sampled[0])
spec_decode_output = decode_tokens(spec_decode_sampled[0])
print("\nbase decoding:\n\n", base_decode_output, "\n")
print("\nspec decoding:\n\n", spec_decode_output, "\n")
print(f'base decoding in: {base_decode_elapsed:.3f}ms\n')
print(f'spec decoding in: {spec_decode_elapsed:.3f}ms\n')
print(f'average num accepted: {num_accepted:.1f} / {GAMMA}\n')