-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_model.py
More file actions
225 lines (188 loc) · 8.1 KB
/
Copy pathtest_model.py
File metadata and controls
225 lines (188 loc) · 8.1 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
import torch
import torch.nn as nn
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from models.fma_model import FMAEncoderModel
from models.baseline_model import StandardTransformerModel
import os
class ModelTester:
def __init__(self, device='cuda'):
self.device = torch.device(device if torch.cuda.is_available() else 'cpu')
print(f"Using device: {self.device}")
# Model configs (must match training)
self.vocab_size = None
self.d_model = 128
self.num_layers = 2
self.num_modes = 32
self.max_len = 128
self.num_classes = 2
self.nhead = 4
self.tokenizer = None
self.teacher = None
self.student = None
self.teacher_hf = None # For HuggingFace pretrained
def load_distilled_models(self):
"""Load models trained with train_distill.py (BERT-Tiny teacher + FMA student)"""
print("\n=== Loading Distilled Models ===")
# Load tokenizer
teacher_name = "prajjwal1/bert-tiny"
print(f"Loading tokenizer: {teacher_name}")
self.tokenizer = AutoTokenizer.from_pretrained(teacher_name)
self.vocab_size = self.tokenizer.vocab_size
# Load HuggingFace teacher
print(f"Loading Teacher (HF): {teacher_name}")
self.teacher_hf = AutoModelForSequenceClassification.from_pretrained(
teacher_name, num_labels=2
).to(self.device)
self.teacher_hf.eval()
# Load FMA student
print("Loading Student (FMA)")
self.student = FMAEncoderModel(
vocab_size=self.vocab_size,
d_model=self.d_model,
num_layers=self.num_layers,
num_modes=self.num_modes,
max_len=self.max_len,
num_classes=self.num_classes
).to(self.device)
# Load checkpoint if exists
ckpt_path = "checkpoints/student_fma_distilled.pt"
if os.path.exists(ckpt_path):
self.student.load_state_dict(torch.load(ckpt_path, map_location=self.device))
print(f"✓ Loaded checkpoint: {ckpt_path}")
else:
print(f"⚠ Checkpoint not found: {ckpt_path}")
print(" Run: conda run -n fsnn python -m training.train_distill")
self.student.eval()
def load_tiny_models(self):
"""Load models trained with train_tiny.py (synthetic data)"""
print("\n=== Loading Tiny Models ===")
self.vocab_size = 1000
max_len = 64
# Load teacher
print("Loading Teacher (Standard Transformer)")
self.teacher = StandardTransformerModel(
vocab_size=self.vocab_size,
d_model=self.d_model,
num_layers=self.num_layers,
nhead=self.nhead,
max_len=max_len,
num_classes=self.num_classes
).to(self.device)
ckpt_path = "checkpoints/teacher.pt"
if os.path.exists(ckpt_path):
self.teacher.load_state_dict(torch.load(ckpt_path, map_location=self.device))
print(f"✓ Loaded checkpoint: {ckpt_path}")
else:
print(f"⚠ Checkpoint not found: {ckpt_path}")
self.teacher.eval()
# Load student
print("Loading Student (FMA)")
self.student = FMAEncoderModel(
vocab_size=self.vocab_size,
d_model=self.d_model,
num_layers=self.num_layers,
num_modes=self.num_modes,
max_len=max_len,
num_classes=self.num_classes
).to(self.device)
ckpt_path = "checkpoints/student_kd.pt"
if os.path.exists(ckpt_path):
self.student.load_state_dict(torch.load(ckpt_path, map_location=self.device))
print(f"✓ Loaded checkpoint: {ckpt_path}")
else:
print(f"⚠ Checkpoint not found: {ckpt_path}")
self.student.eval()
def predict(self, text):
"""Run inference on text input"""
if self.tokenizer is None:
print("Error: No tokenizer loaded. Use load_distilled_models() first.")
return
# Tokenize
inputs = self.tokenizer(
text,
padding="max_length",
truncation=True,
max_length=self.max_len,
return_tensors="pt"
)
input_ids = inputs['input_ids'].to(self.device)
attention_mask = inputs['attention_mask'].to(self.device)
with torch.no_grad():
# Teacher prediction
if self.teacher_hf is not None:
teacher_outputs = self.teacher_hf(input_ids=input_ids, attention_mask=attention_mask)
teacher_logits = teacher_outputs.logits
teacher_probs = torch.softmax(teacher_logits, dim=-1)
teacher_pred = torch.argmax(teacher_logits, dim=-1).item()
else:
teacher_pred = None
teacher_probs = None
# Student prediction
student_logits = self.student(input_ids)
student_probs = torch.softmax(student_logits, dim=-1)
student_pred = torch.argmax(student_logits, dim=-1).item()
# Format results
labels = ["Negative", "Positive"]
print(f"\n{'='*60}")
print(f"Text: {text}")
print(f"{'='*60}")
if teacher_pred is not None:
print(f"\n📊 Teacher (BERT-Tiny):")
print(f" Prediction: {labels[teacher_pred]}")
print(f" Confidence: {teacher_probs[0][teacher_pred].item():.2%}")
print(f" Probs: Neg={teacher_probs[0][0].item():.2%}, Pos={teacher_probs[0][1].item():.2%}")
print(f"\n⚡ Student (FMA):")
print(f" Prediction: {labels[student_pred]}")
print(f" Confidence: {student_probs[0][student_pred].item():.2%}")
print(f" Probs: Neg={student_probs[0][0].item():.2%}, Pos={student_probs[0][1].item():.2%}")
if teacher_pred is not None:
match = "✓" if teacher_pred == student_pred else "✗"
print(f"\n{match} Agreement: {'YES' if teacher_pred == student_pred else 'NO'}")
return {
'teacher_pred': teacher_pred,
'teacher_probs': teacher_probs[0].cpu().numpy() if teacher_probs is not None else None,
'student_pred': student_pred,
'student_probs': student_probs[0].cpu().numpy()
}
def main():
tester = ModelTester()
# Check which checkpoints exist
print("\n=== Checking Available Checkpoints ===")
checkpoints = [
"checkpoints/teacher.pt",
"checkpoints/student_kd.pt",
"checkpoints/student_fma_distilled.pt"
]
available = []
for ckpt in checkpoints:
if os.path.exists(ckpt):
size = os.path.getsize(ckpt) / (1024**2) # MB
print(f"✓ {ckpt} ({size:.2f} MB)")
available.append(ckpt)
else:
print(f"✗ {ckpt}")
if not available:
print("\n⚠ No checkpoints found. Please train a model first:")
print(" conda run -n fsnn python -m training.train_distill")
return
# Load appropriate models
if "checkpoints/student_fma_distilled.pt" in available:
tester.load_distilled_models()
# Test prompts
test_texts = [
"This movie was absolutely amazing! I loved every minute of it.",
"Terrible film. Complete waste of time and money.",
"The acting was great but the plot was confusing.",
"Best movie of the year! Highly recommend!",
"Boring and predictable. Don't bother watching."
]
print("\n\n" + "="*60)
print("TESTING DISTILLED MODELS ON REAL PROMPTS")
print("="*60)
for text in test_texts:
tester.predict(text)
else:
print("\n⚠ Distilled model not found. Run full training:")
print(" conda run -n fsnn python -m training.train_distill")
if __name__ == "__main__":
main()