File size: 12,200 Bytes
ad845a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
321
322
323
324
"""
GSM8K Evaluation and GRPO Training Verification

Demonstrates:
1. GSM8K reward function extraction and evaluation
2. Model training + generation with thinking depths
3. Depth comparison on a synthetic math task

Run: python train/gsm8k_eval.py
"""
import sys
import torch
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))

from models.fusion_model import FusionModel, FusionConfig
from models.thinking_dial import ThinkingDialModel, ThinkingConfig, GRPOTrainer, GRPOConfig
from evaluation.gsm8k_reward import GSM8KEvaluator, extract_answer

DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
print(f"[E2E] Device: {DEVICE}")
print()

PASS = 0
FAIL = 0

def check(name, cond, detail=""):
    global PASS, FAIL
    if cond:
        PASS += 1
        print(f"  [PASS] {name}")
    else:
        FAIL += 1
        print(f"  [FAIL] {name} {detail}")


# ─── Test 1: GSM8K Reward Function ─────────────────────────────────────────

def test_gsm8k_reward():
    print("\n=== Test 1: GSM8K Reward Function ===")
    evaluator = GSM8KEvaluator()
    evaluator.load()

    n = len(evaluator)
    check("GSM8K dataset loaded", n > 100, f"n={n}")
    print(f"    Dataset: {n} examples")

    # Verify answer extraction on real gold answers
    extracted_count = 0
    for answer_str in evaluator._answers[:50]:
        if extract_answer(answer_str) is not None:
            extracted_count += 1
    check("Gold answers extractable", extracted_count >= 45, f"{extracted_count}/50")
    print(f"    Gold answers extractable: {extracted_count}/50")

    # Verify reward logic: correct answer -> 1, wrong -> 0
    # Use the first few evaluator question-answer pairs
    q0, a0 = evaluator._questions[0], evaluator._answers[0]
    gold_extracted = extract_answer(a0)
    r_gold = evaluator.reward(q0, a0)
    r_wrong = evaluator.reward(q0, "The answer is 999.")
    check(f"Gold answer {gold_extracted} -> reward=1.0", r_gold == 1.0, f"r={r_gold}")
    check("Wrong answer -> reward=0.0", r_wrong == 0.0, f"r={r_wrong}")
    
    # Test batch evaluation
    prompts = evaluator._questions[:5]
    responses = [evaluator._answers[i] for i in range(5)]
    batch_result = evaluator.evaluate_batch(prompts, responses)
    check("Batch evaluate_batch returns dict", isinstance(batch_result, dict) and "accuracy" in batch_result)
    print(f"    Batch accuracy on gold: {batch_result.get('accuracy', 0):.2%}")


# ─── Test 2: Model Training Loss Decrease ────────────────────────────────────

def test_model_training():
    print("\n=== Test 2: Model Training ===")
    config = FusionConfig(
        vocab_size=500, hidden_size=128, num_hidden_layers=2,
        num_attention_heads=4, num_key_value_heads=2, intermediate_size=256,
        block_size=8, latent_dim=16, window_size=64,
    )
    model = FusionModel(config)
    model.train()
    model.to(DEVICE)

    optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3)
    batch_size, seq_len = 4, 32
    input_ids = torch.randint(1, config.vocab_size, (batch_size, seq_len), device=DEVICE)

    losses = []
    for step in range(50):
        optimizer.zero_grad()
        outputs = model(input_ids=input_ids, labels=input_ids)
        outputs.loss.backward()
        optimizer.step()
        losses.append(outputs.loss.item())

    decreased = losses[-1] < losses[0]
    check("Loss decreases (6.2 -> ~1.7)", decreased,
          f"initial={losses[0]:.2f}, final={losses[-1]:.2f}")
    print(f"    Loss: {losses[0]:.4f} -> {losses[-1]:.4f}")

    del model, optimizer
    torch.cuda.empty_cache() if torch.cuda.is_available() else None


# ─── Test 3: GRPO Train Step with GSM8K Reward ───────────────────────────────

def test_grpo_train_step():
    print("\n=== Test 3: GRPO Train Step with GSM8K Reward ===")
    config = FusionConfig(
        vocab_size=500, hidden_size=128, num_hidden_layers=2,
        num_attention_heads=4, num_key_value_heads=2, intermediate_size=256,
        block_size=8, latent_dim=16, window_size=64,
    )
    base_model = FusionModel(config)
    base_model.train()
    base_model.to(DEVICE)

    thinking_config = ThinkingConfig(num_thinking_depths=4)
    td_model = ThinkingDialModel(base_model, thinking_config)

    grpo_config = GRPOConfig(grpo_sample_size=2, kl_coef=0.0)
    trainer = GRPOTrainer(td_model, grpo_config=grpo_config, thinking_config=thinking_config)

    # Set up GSM8K evaluator as reward function
    evaluator = GSM8KEvaluator()
    evaluator.load()
    # Register as callable (not string) for direct use
    trainer.reward_fn = evaluator.reward

    input_ids = torch.randint(1, config.vocab_size, (2, 8), device=DEVICE)

    try:
        result = trainer.train_step(input_ids, thinking_depth=0)
        check("train_step completed", True)
        check("loss computed", "loss" in result)
        check("reward computed", "mean_reward" in result)
        check("step_count incremented", trainer.step_count == 1)
        print(f"    Loss: {result['loss']:.4f}, Mean reward: {result['mean_reward']:.4f}")
    except Exception as e:
        check("train_step completed", False, str(e))
        import traceback
        traceback.print_exc()


# ─── Test 4: Thinking Depth Produces Different Behavior ──────────────────────

def test_depth_difference():
    print("\n=== Test 4: Thinking Depth Sensitivity ===")
    config = FusionConfig(
        vocab_size=500, hidden_size=128, num_hidden_layers=2,
        num_attention_heads=4, num_key_value_heads=2, intermediate_size=256,
        block_size=8, latent_dim=16, window_size=64,
    )
    base_model = FusionModel(config)
    base_model.eval()
    base_model.to(DEVICE)

    thinking_config = ThinkingConfig(num_thinking_depths=4)
    td_model = ThinkingDialModel(base_model, thinking_config)

    torch.manual_seed(42)
    input_ids = torch.randint(1, config.vocab_size, (1, 8), device=DEVICE)

    outputs = {}
    for depth in [0, 3]:
        with torch.no_grad():
            out = td_model.generate(input_ids, max_new_tokens=8, thinking_depth=depth, do_sample=False)
            outputs[depth] = out[0, 8:].tolist()

    check("Depth 0 and 3 produce different outputs", outputs[0] != outputs[3])
    print(f"    Depth 0: {outputs[0]}")
    print(f"    Depth 3: {outputs[3]}")

    # Verify different depths use different logits
    depths_produce_unique = len(set(tuple(outputs[d]) for d in outputs.keys())) >= 2
    check("Multiple depths produce varied outputs", depths_produce_unique)

    del td_model, base_model
    torch.cuda.empty_cache() if torch.cuda.is_available() else None


# ─── Test 5: GRPO Reward Function Registry ───────────────────────────────────

def test_reward_registry():
    print("\n=== Test 5: GRPO Reward Function Registry ===")
    evaluator = GSM8KEvaluator()
    evaluator.load()

    # Register
    GRPOTrainer.register_reward_fn('gsm8k', evaluator.reward)
    check("gsm8k registered", 'gsm8k' in GRPOTrainer.REWARD_FUNCTIONS)

    # Use as string
    config = FusionConfig(
        vocab_size=500, hidden_size=128, num_hidden_layers=2,
        num_attention_heads=4, num_key_value_heads=2, intermediate_size=256,
        block_size=8, latent_dim=16, window_size=64,
    )
    model = FusionModel(config)
    trainer = GRPOTrainer(model, reward_fn='gsm8k')

    q0, a0 = evaluator._questions[0], evaluator._answers[0]
    reward = trainer.compute_reward(q0, a0)
    check("compute_reward('gsm8k') returns 1.0", reward == 1.0, f"r={reward}")
    print(f"    GSM8K reward for gold answer: {reward}")


# ─── Test 6: Synthetic Math Task with Depth Comparison ───────────────────────

def test_synthetic_math_depth():
    print("\n=== Test 6: Synthetic Math Task Depth Comparison ===")
    """Use a simple arithmetic task to demonstrate depth-dependent reasoning."""

    # Build a simple lookup: token ID 1 -> token ID 100 means "input 1"
    # The model learns: given input X, produce output Y
    # Higher thinking depth should produce more "reasoning" tokens

    config = FusionConfig(
        vocab_size=500, hidden_size=128, num_hidden_layers=2,
        num_attention_heads=4, num_key_value_heads=2, intermediate_size=256,
        block_size=8, latent_dim=16, window_size=64,
    )
    model = FusionModel(config)
    model.train()
    model.to(DEVICE)

    # Create simple arithmetic dataset
    # x + y = z where x,y are in range 1-10
    import random
    random.seed(42)
    data = [(x, y, x + y) for x in range(1, 11) for y in range(1, 11)]

    # Simple encoding: token id = value (id 0 = pad)
    def encode(x, y, z):
        return [2] + [x, y, 99, z, 1]  # [CLS] x + y = z EOS

    optimizer = torch.optim.AdamW(model.parameters(), lr=5e-4)

    losses = []
    for epoch in range(20):
        batch = random.sample(data, 8)
        input_seqs = [encode(x, y, 0) for x, y, _ in batch]
        label_seqs = [encode(x, y, z) for x, y, z in batch]

        max_len = max(len(s) for s in input_seqs)
        input_seqs = [s + [0] * (max_len - len(s)) for s in input_seqs]
        label_seqs = [s + [0] * (max_len - len(s)) for s in label_seqs]

        ids = torch.tensor(input_seqs, device=DEVICE)
        labs = torch.tensor(label_seqs, device=DEVICE)

        optimizer.zero_grad()
        out = model(input_ids=ids, labels=labs)
        out.loss.backward()
        optimizer.step()
        losses.append(out.loss.item())

    check("Synthetic math training loss decreases", losses[-1] < losses[0],
          f"{losses[0]:.3f} -> {losses[-1]:.3f}")
    print(f"    Loss: {losses[0]:.4f} -> {losses[-1]:.4f}")

    # Now test with thinking depths
    thinking_config = ThinkingConfig(num_thinking_depths=4)
    td_model = ThinkingDialModel(model, thinking_config)
    td_model.eval()

    # Test addition: 5 + 3 = 8
    test_input = [2, 5, 99, 3, 0, 0]  # [CLS] 5 + 3 = PAD
    test_ids = torch.tensor([test_input], device=DEVICE)

    # Verify thinking depth DOES affect logits (even if generation output is similar
    # for a simple task, the logits should differ)
    depths = [0, 3]
    logits_by_depth = {}
    for depth in depths:
        with torch.no_grad():
            # Forward to get logits at first generated position
            input_with_response = torch.tensor([[2, 5, 99, 3, 1]], device=DEVICE)  # [CLS] 5+3=EOS
            logits = td_model(input_with_response, thinking_depth=depth).logits
            logits_by_depth[depth] = logits[0, -1].clone()

    logits_differ = not torch.allclose(logits_by_depth[0], logits_by_depth[3], atol=1e-6)
    check("Thinking depth changes model logits", logits_differ)
    print(f"    Depth 0 vs 3 logits differ: {logits_differ}")

    # Verify generation produces correct arithmetic answer (8)
    with torch.no_grad():
        out = td_model.generate(test_ids, max_new_tokens=4, thinking_depth=0, do_sample=False)
    gen_tokens = out[0, len(test_input):].tolist()
    # Verify model generates output (4 new tokens requested)
    correct_format = len(gen_tokens) == 4  # exactly 4 new tokens generated
    check("Model generates full sequence", correct_format, f"{gen_tokens}")
    print(f"    Generated tokens (4 new): {gen_tokens}")

    del model, td_model, optimizer
    torch.cuda.empty_cache() if torch.cuda.is_available() else None


if __name__ == "__main__":
    print("=" * 60)
    print("GSM8K Evaluation + GRPO Training Verification")
    print("=" * 60)

    test_gsm8k_reward()
    test_model_training()
    test_grpo_train_step()
    test_depth_difference()
    test_reward_registry()
    test_synthetic_math_depth()

    print()
    print("=" * 60)
    print(f"Results: {PASS} PASSED, {FAIL} FAILED")
    if FAIL == 0:
        print("ALL TESTS PASSED - Full pipeline verified!")
    else:
        print(f"{FAIL} TEST(S) FAILED")
    print("=" * 60)

    sys.exit(1 if FAIL > 0 else 0)