-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_train_v2.py
More file actions
320 lines (255 loc) · 11.6 KB
/
06_train_v2.py
File metadata and controls
320 lines (255 loc) · 11.6 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"""
v2 Training: Raw hidden state keys + gated query projectors.
Key differences from v1:
1. No key projectors — use base model's raw chunk-pooled hidden states
2. Gated residual query projectors (start near identity)
3. Only 4 routing layers instead of all 18
4. Lower learning rate (1e-4 vs 5e-4)
RESULT: Peaked at ~50% recall@16 at epoch 4, then degraded.
See README.md for analysis.
"""
import json
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import AdamW
from torch.optim.lr_scheduler import CosineAnnealingLR
from transformers import AutoModelForCausalLM, AutoTokenizer
from pathlib import Path
from tqdm import tqdm
import random
import time
import gc
# === ADAPT THESE PATHS ===
PROJECT_DIR = Path(__file__).parent
DOCS_FILE = PROJECT_DIR / "dataset" / "documents.json"
PAIRS_FILE = PROJECT_DIR / "dataset" / "training_pairs.jsonl"
OUTPUT_DIR = PROJECT_DIR / "checkpoints_v2"
CACHE_DIR = PROJECT_DIR / "cache_v2"
# Training config
MODEL_NAME = "Qwen/Qwen3-4B"
CHUNK_SIZE = 64
EPOCHS = 10
BATCH_SIZE = 8
LEARNING_RATE = 1e-4 # Lower LR — refining, not learning from scratch
TEMPERATURE = 0.07
DEVICE = "cuda"
DTYPE = torch.bfloat16
MAX_POSITIVES = 4
MAX_NEGATIVES = 16
NUM_ROUTING_LAYERS = 4 # Evenly-spaced from latter half
class GatedQueryProjector(nn.Module):
"""Gated residual bottleneck — starts near identity."""
def __init__(self, hidden_dim):
super().__init__()
self.down = nn.Linear(hidden_dim, hidden_dim // 4, bias=False)
self.up = nn.Linear(hidden_dim // 4, hidden_dim, bias=False)
self.gate = nn.Linear(hidden_dim, hidden_dim, bias=False)
nn.init.xavier_normal_(self.down.weight)
nn.init.xavier_normal_(self.up.weight)
nn.init.xavier_normal_(self.gate.weight)
def forward(self, x):
projected = self.up(F.silu(self.down(x)))
gate = torch.sigmoid(self.gate(x))
return x + gate * projected
def chunk_pool(tensor, chunk_size=64):
seq_len, dim = tensor.shape
pad_len = (chunk_size - seq_len % chunk_size) % chunk_size
if pad_len > 0:
tensor = F.pad(tensor, (0, 0, 0, pad_len))
num_chunks = (seq_len + pad_len) // chunk_size
return tensor.view(num_chunks, chunk_size, dim).mean(dim=1)
def get_routing_layers(msa_start_layer, num_layers, num_routing_layers):
msa_layers = list(range(msa_start_layer, num_layers))
if len(msa_layers) <= num_routing_layers:
return msa_layers
step = len(msa_layers) / num_routing_layers
return [msa_layers[int(i * step)] for i in range(num_routing_layers)]
def load_data():
print("Loading documents...")
with open(DOCS_FILE, 'r', encoding='utf-8') as f:
documents = json.load(f)['documents']
print("Loading training pairs...")
pairs = []
with open(PAIRS_FILE, 'r', encoding='utf-8') as f:
for line in f:
pairs.append(json.loads(line))
print(f" {len(documents)} documents, {len(pairs)} training pairs")
return documents, pairs
def pre_encode_documents(base_model, tokenizer, documents, routing_layers,
cache_dir, device, dtype, chunk_size):
"""Extract chunk-pooled RAW hidden states. No projection — just the
pretrained model's representations. These are already semantically rich."""
cache_file = Path(cache_dir) / "doc_keys_raw.pt"
if cache_file.exists():
print(f"Loading cached document keys from {cache_file}")
return torch.load(cache_file, map_location='cpu', weights_only=True)
print(f"Pre-encoding {len(documents)} documents (raw hidden states)...")
cached = {}
for i, doc in enumerate(tqdm(documents, desc="Encoding")):
tokens = tokenizer(
doc['text'], return_tensors="pt", truncation=True, max_length=2048
).to(device)
with torch.no_grad():
outputs = base_model(
input_ids=tokens.input_ids, attention_mask=tokens.attention_mask,
output_hidden_states=True,
)
doc_keys = {}
for layer_idx in routing_layers:
hs = outputs.hidden_states[layer_idx + 1].squeeze(0)
pooled = chunk_pool(hs, chunk_size)
pooled = F.normalize(pooled.float(), dim=-1).half()
doc_keys[layer_idx] = pooled.cpu()
cached[doc['doc_id']] = doc_keys
if (i + 1) % 200 == 0:
torch.cuda.empty_cache()
torch.save(cached, cache_file)
print(f"Cache saved: {cache_file.stat().st_size / (1024**2):.0f} MB")
return cached
def train_epoch(query_projectors, base_model, tokenizer, pairs, doc_keys,
optimizer, routing_layers, epoch, total_epochs, device, dtype):
for qp in query_projectors.values():
qp.train()
random.shuffle(pairs)
total_loss = 0.0
num_steps = 0
optimizer.zero_grad()
for i in range(0, len(pairs), BATCH_SIZE):
batch_loss = torch.tensor(0.0, device=device, requires_grad=True)
valid = 0
for pair in pairs[i:i + BATCH_SIZE]:
pos_ids = [pid for pid in pair['positive_doc_ids'][:MAX_POSITIVES] if pid in doc_keys]
neg_ids = [nid for nid in pair['negative_doc_ids'][:MAX_NEGATIVES] if nid in doc_keys]
if not pos_ids or not neg_ids:
continue
tokens = tokenizer(
pair['query'], return_tensors="pt", truncation=True, max_length=512
).to(device)
with torch.no_grad():
outputs = base_model(
input_ids=tokens.input_ids, attention_mask=tokens.attention_mask,
output_hidden_states=True,
)
layer_losses = []
for layer_idx in routing_layers:
q_hs = outputs.hidden_states[layer_idx + 1].squeeze(0).to(dtype)
q_projected = query_projectors[str(layer_idx)](q_hs)
q_vec = F.normalize(q_projected.max(dim=0).values.float(), dim=-1)
pos_sims = [torch.matmul(
doc_keys[pid][layer_idx].to(device=device).float(), q_vec
).max() for pid in pos_ids]
neg_sims = torch.stack([torch.matmul(
doc_keys[nid][layer_idx].to(device=device).float(), q_vec
).max() for nid in neg_ids])
for ps in pos_sims:
logits = torch.cat([ps.unsqueeze(0), neg_sims]) / TEMPERATURE
labels = torch.zeros(1, dtype=torch.long, device=device)
layer_losses.append(F.cross_entropy(logits.unsqueeze(0), labels))
if layer_losses:
batch_loss = batch_loss + torch.stack(layer_losses).mean()
valid += 1
if valid > 0:
(batch_loss / valid).backward()
torch.nn.utils.clip_grad_norm_(
[p for qp in query_projectors.values() for p in qp.parameters()], 1.0
)
optimizer.step()
optimizer.zero_grad()
total_loss += (batch_loss / valid).item()
num_steps += 1
if (i // BATCH_SIZE + 1) % 200 == 0:
print(f" Epoch {epoch+1}/{total_epochs}, {(i+BATCH_SIZE)/len(pairs)*100:.0f}%, loss: {total_loss/max(num_steps,1):.4f}")
if (i // BATCH_SIZE + 1) % 50 == 0:
torch.cuda.empty_cache()
return total_loss / max(num_steps, 1)
def evaluate(query_projectors, base_model, tokenizer, pairs, doc_keys,
routing_layers, device, dtype, num_eval=100):
for qp in query_projectors.values():
qp.eval()
eval_pairs = random.sample(pairs, min(num_eval, len(pairs)))
doc_id_list = sorted(doc_keys.keys())
hits = 0
for pair in tqdm(eval_pairs, desc="Evaluating", leave=False):
tokens = tokenizer(
pair['query'], return_tensors="pt", truncation=True, max_length=512
).to(device)
with torch.no_grad():
outputs = base_model(
input_ids=tokens.input_ids, attention_mask=tokens.attention_mask,
output_hidden_states=True,
)
scores = torch.zeros(len(doc_id_list), device=device)
for layer_idx in routing_layers:
q_hs = outputs.hidden_states[layer_idx + 1].squeeze(0).to(dtype)
with torch.no_grad():
q_vec = query_projectors[str(layer_idx)](q_hs)
q_vec = F.normalize(q_vec.max(dim=0).values.float(), dim=-1)
for doc_i, doc_id in enumerate(doc_id_list):
dk = doc_keys[doc_id][layer_idx].to(device=device).float()
scores[doc_i] += torch.matmul(dk, q_vec).max()
retrieved = set(doc_id_list[idx.item()] for idx in torch.topk(scores, min(16, len(scores))).indices)
if set(pair['positive_doc_ids']) & retrieved:
hits += 1
recall = hits / len(eval_pairs)
print(f"Recall@16: {recall:.3f} ({hits}/{len(eval_pairs)})")
return recall
def main():
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
CACHE_DIR.mkdir(parents=True, exist_ok=True)
documents, pairs = load_data()
print(f"\nLoading base model: {MODEL_NAME}")
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
base_model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME, torch_dtype=DTYPE, device_map=DEVICE, trust_remote_code=True,
)
for param in base_model.parameters():
param.requires_grad = False
config = base_model.config
hidden_dim = config.hidden_size
num_layers = config.num_hidden_layers
msa_start_layer = num_layers // 2
routing_layers = get_routing_layers(msa_start_layer, num_layers, NUM_ROUTING_LAYERS)
print(f"Routing layers: {routing_layers}")
doc_keys = pre_encode_documents(
base_model, tokenizer, documents, routing_layers,
CACHE_DIR, DEVICE, DTYPE, CHUNK_SIZE
)
query_projectors = nn.ModuleDict({
str(l): GatedQueryProjector(hidden_dim) for l in routing_layers
}).to(device=DEVICE, dtype=DTYPE)
print(f"Query projector params: {sum(p.numel() for p in query_projectors.parameters()):,}")
# Baseline eval (before training)
print("\nBaseline evaluation (untrained)...")
baseline = evaluate(query_projectors, base_model, tokenizer, pairs, doc_keys,
routing_layers, DEVICE, DTYPE, num_eval=50)
optimizer = AdamW(query_projectors.parameters(), lr=LEARNING_RATE, weight_decay=0.01)
scheduler = CosineAnnealingLR(optimizer, T_max=EPOCHS)
best_recall = baseline
for epoch in range(EPOCHS):
avg_loss = train_epoch(
query_projectors, base_model, tokenizer, pairs, doc_keys,
optimizer, routing_layers, epoch, EPOCHS, DEVICE, DTYPE
)
scheduler.step()
print(f"Epoch {epoch+1}/{EPOCHS}: loss={avg_loss:.4f}")
if (epoch + 1) % 2 == 0:
checkpoint = {
'query_projectors': query_projectors.state_dict(),
'epoch': epoch + 1,
'config': {
'model_name': MODEL_NAME, 'hidden_dim': hidden_dim,
'num_layers': num_layers, 'msa_start_layer': msa_start_layer,
'routing_layers': routing_layers, 'chunk_size': CHUNK_SIZE,
}
}
torch.save(checkpoint, OUTPUT_DIR / f"router_v2_epoch{epoch+1}.pt")
recall = evaluate(query_projectors, base_model, tokenizer, pairs, doc_keys,
routing_layers, DEVICE, DTYPE, num_eval=50)
if recall > best_recall:
best_recall = recall
torch.save(checkpoint, OUTPUT_DIR / "router_v2_best.pt")
print(f" New best: {best_recall:.3f}")
print(f"\nBest recall@16: {best_recall:.3f}")
if __name__ == '__main__':
main()