-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtrain_mac.py
199 lines (154 loc) · 5.49 KB
/
train_mac.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
import random
import tqdm
import gzip
import numpy as np
import torch
from torch import nn, Tensor
from torch.optim import Adam
from torch.nn import functional as F
from torch.utils.data import DataLoader, Dataset
from titans_pytorch import MemoryAsContextTransformer
# constants
NUM_BATCHES = int(1e5)
BATCH_SIZE = 4
GRADIENT_ACCUMULATE_EVERY = 4
LEARNING_RATE = 2e-4
VALIDATE_EVERY = 100
GENERATE_EVERY = 500
PRIME_LENGTH = 100
GENERATE_LENGTH = 512
SHOULD_GENERATE = True
SEQ_LEN = 512
# neural memory related
NEURAL_MEMORY_DEPTH = 2
NUM_PERSIST_MEM = 4
NUM_LONGTERM_MEM = 4
NEURAL_MEM_LAYERS = (2, 4)
NEURAL_MEM_GATE_ATTN_OUTPUT = True
WINDOW_SIZE = 32
KV_RECON_LOSS_WEIGHT = 0.
LEARNED_MEM_MODEL_WEIGHTS = True
# experiment related
PROJECT_NAME = 'titans-mac-transformer'
RUN_NAME = f'mac - {NUM_LONGTERM_MEM} longterm mems, layers {NEURAL_MEM_LAYERS}'
WANDB_ONLINE = False # turn this on to pipe experiment to cloud
# perf related
USE_ACCELERATED_SCAN = True
USE_FLEX_ATTN = True
# wandb experiment tracker
import wandb
wandb.init(project = PROJECT_NAME, mode = 'disabled' if not WANDB_ONLINE else 'online')
wandb.run.name = RUN_NAME
wandb.run.save()
# 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)))
# sampling helpers
def log(t, eps = 1e-20):
return torch.log(t.clamp(min = eps))
def gumbel_noise(t):
noise = torch.zeros_like(t).uniform_(0, 1)
return -log(-log(noise))
def gumbel_sample(t, temperature = 1., keepdim = True):
if temperature <= 0.:
return t.argmax(dim = dim, keepdim = keepdim)
return ((t / max(temperature, 1e-10)) + gumbel_noise(t)).argmax(dim = -1, keepdim = keepdim)
# min_p
# https://arxiv.org/abs/2407.01082
def min_p_filter(logits, min_p = 0.1):
probs = logits.softmax(dim = -1)
max_probs = probs.amax(dim = -1, keepdim = True)
limit = min_p * max_probs
return torch.where(probs < limit, float('-inf'), logits)
def base_decoding(
net,
prompt: Tensor,
seq_len: int,
temperature = 1.5,
min_p = 1e-1,
filter_thres = 0.9,
):
prompt_seq_len, out = prompt.shape[-1], prompt.clone()
sample_num_times = max(0, seq_len - prompt_seq_len)
for _ in tqdm.tqdm(range(sample_num_times)):
logits = net(out, disable_flex_attn = True)
logits = logits[:, -1]
logits = min_p_filter(logits, min_p = min_p)
sample = gumbel_sample(logits, temperature = temperature)
out = torch.cat((out, sample), dim = -1)
return out[..., prompt_seq_len:]
# instantiate memory-as-context transformer
model = MemoryAsContextTransformer(
num_tokens = 256,
dim = 384,
depth = 8,
segment_len = WINDOW_SIZE,
num_persist_mem_tokens = NUM_PERSIST_MEM,
num_longterm_mem_tokens = NUM_LONGTERM_MEM,
neural_memory_layers = NEURAL_MEM_LAYERS,
neural_memory_segment_len = WINDOW_SIZE // 2,
neural_mem_gate_attn_output = NEURAL_MEM_GATE_ATTN_OUTPUT,
aux_kv_recon_loss_weight = KV_RECON_LOSS_WEIGHT,
use_flex_attn = USE_FLEX_ATTN,
neural_memory_kwargs = dict(
dim_head = 64,
heads = 4,
use_accelerated_scan = USE_ACCELERATED_SCAN,
learned_mem_model_weights = LEARNED_MEM_MODEL_WEIGHTS,
default_model_kwargs = dict(
depth = NEURAL_MEMORY_DEPTH,
)
)
).cuda()
# prepare enwik8 data
with gzip.open('./data/enwik8.gz') as file:
data = np.frombuffer(file.read(int(95e6)), dtype = np.uint8).copy()
data_train, data_val = np.split(data, [int(90e6)])
data_train, data_val = map(torch.from_numpy, (data_train, data_val))
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.cuda()
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))
val_loader = cycle(DataLoader(val_dataset, batch_size = BATCH_SIZE))
# optimizer
optim = Adam(model.parameters(), lr=LEARNING_RATE)
# training
for i in tqdm.tqdm(range(NUM_BATCHES), mininterval=10., desc='training'):
model.train()
for __ in range(GRADIENT_ACCUMULATE_EVERY):
loss, (ar_loss, kv_recon_losses) = model(next(train_loader), return_loss = True, return_loss_breakdown = True)
loss.backward()
print(f'training loss: {ar_loss.item()}')
torch.nn.utils.clip_grad_norm_(model.parameters(), 0.5)
optim.step()
optim.zero_grad()
wandb.log(dict(loss = ar_loss.item()))
if i % VALIDATE_EVERY == 0:
model.eval()
with torch.no_grad():
loss, (ar_loss, _) = model(next(val_loader), return_loss = True, return_loss_breakdown = True)
print(f'validation loss: {ar_loss.item()}')
if SHOULD_GENERATE and i % GENERATE_EVERY == 0:
model.eval()
inp = random.choice(val_dataset)[:PRIME_LENGTH]
prime = decode_tokens(inp)
print(f'%s \n\n %s', (prime, '*' * 100))
sample = base_decoding(model, inp[None, ...], GENERATE_LENGTH)
output_str = decode_tokens(sample[0])
print(output_str)