File size: 10,544 Bytes
73400c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
325
326
#!/usr/bin/env python3
"""
GRPO Training - The Reasoning Magic
Uses the trained model from stage 1
"""

import sys
import json
import torch
import torch.nn as nn
import torch.nn.functional as F
from pathlib import Path
from tqdm import tqdm

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

from src.shorekeeper import SHOREKEEPER, MemoryEfficientSHOREKEEPER
from transformers import AutoTokenizer

class GRPOTrainer:
    """Group Relative Policy Optimization Trainer"""
    
    def __init__(self, model, tokenizer, config):
        self.model = model
        self.tokenizer = tokenizer
        self.device = next(model.parameters()).device
        
        self.group_size = config.get('group_size', 2)
        self.lr = config.get('learning_rate', 1e-6)
        
        self.optimizer = torch.optim.AdamW(
            self.model.parameters(),
            lr=self.lr,
            weight_decay=0.01
        )
        
        self.step = 0
    
    def compute_reward(self, response, ground_truth):
        """Calculate reward for a response"""
        reward = 0.0
        
        # Format reward - check for reasoning tokens
        if '|special_token|' in response:
            reward += 0.5
        
        # Extract answer (look for numbers at the end)
        import re
        numbers = re.findall(r'\d+', response)
        if numbers:
            last_num = numbers[-1]
            if last_num == str(ground_truth).strip():
                reward += 2.0
        
        # Length reward - not too short
        if len(response.split()) > 10:
            reward += 0.2
        
        # No repetition penalty
        words = response.split()
        unique_ratio = len(set(words)) / max(len(words), 1)
        if unique_ratio > 0.5:
            reward += 0.3
        
        return reward
    
    def generate_response(self, prompt, max_length=128):
        """Generate a response from the model"""
        self.model.eval()
        
        try:
            inputs = self.tokenizer(prompt, return_tensors="pt", truncation=True, max_length=256)
            inputs = {k: v.to(self.device) for k, v in inputs.items()}
            
            with torch.no_grad():
                outputs = self.model.generate(
                    inputs['input_ids'],
                    max_new_tokens=max_length,
                    temperature=0.8,
                    do_sample=True,
                    pad_token_id=self.tokenizer.eos_token_id
                )
            
            response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
            return response
        except Exception as e:
            return f"Error: {e}"
    
    def train_step(self, prompt, ground_truth):
        """Single GRPO step"""
        self.model.train()
        
        # Generate group of responses
        responses = []
        rewards = []
        
        for _ in range(self.group_size):
            response = self.generate_response(prompt)
            responses.append(response)
            reward = self.compute_reward(response, ground_truth)
            rewards.append(reward)
        
        # Calculate advantages (relative to group mean)
        mean_reward = sum(rewards) / len(rewards)
        advantages = [r - mean_reward for r in rewards]
        
        # Train on responses with positive advantage
        total_loss = 0
        valid_steps = 0
        
        for i, (response, advantage) in enumerate(zip(responses, advantages)):
            if advantage <= 0:
                continue
            
            # Create training text
            text = f"{prompt}\n{response}"
            inputs = self.tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
            inputs = {k: v.to(self.device) for k, v in inputs.items()}
            
            # Forward pass
            logits = self.model(inputs['input_ids'])
            
            # Calculate language modeling loss
            shift_logits = logits[..., :-1, :].contiguous()
            shift_labels = inputs['input_ids'][..., 1:].contiguous()
            
            loss = F.cross_entropy(
                shift_logits.view(-1, shift_logits.size(-1)),
                shift_labels.view(-1),
                ignore_index=self.tokenizer.pad_token_id
            )
            
            # Weight by advantage
            total_loss = total_loss + loss * advantage
            valid_steps += 1
        
        if valid_steps > 0 and total_loss != 0:
            total_loss = total_loss / valid_steps
            self.optimizer.zero_grad()
            total_loss.backward()
            torch.nn.utils.clip_grad_norm_(self.model.parameters(), 1.0)
            self.optimizer.step()
            return {
                'loss': total_loss.item(),
                'avg_reward': sum(rewards) / len(rewards),
                'best_reward': max(rewards),
                'valid_steps': valid_steps
            }
        
        return {
            'loss': 0,
            'avg_reward': sum(rewards) / len(rewards),
            'best_reward': max(rewards),
            'valid_steps': 0
        }
    
    def train(self, dataset, num_epochs=1):
        """Full training loop"""
        print(f"\nTraining on device: {self.device}")
        
        for epoch in range(num_epochs):
            print(f"\n{'='*50}")
            print(f"Epoch {epoch + 1}/{num_epochs}")
            print(f"{'='*50}")
            
            total_loss = 0
            total_reward = 0
            steps = 0
            valid_steps = 0
            
            pbar = tqdm(dataset, desc=f"GRPO Training")
            
            for i, item in enumerate(pbar):
                prompt = item.get('prompt', '')
                answer = item.get('answer', item.get('ground_truth', ''))
                
                if not prompt or not answer:
                    continue
                
                try:
                    stats = self.train_step(prompt, str(answer))
                    
                    if stats['valid_steps'] > 0:
                        total_loss += stats['loss']
                        valid_steps += 1
                    
                    total_reward += stats['avg_reward']
                    steps += 1
                    
                    pbar.set_postfix({
                        'loss': f'{stats["loss"]:.4f}',
                        'reward': f'{stats["avg_reward"]:.2f}'
                    })
                    
                except Exception as e:
                    if i < 10:
                        print(f"\n  Error: {e}")
                    continue
            
            if steps > 0:
                avg_loss = total_loss / valid_steps if valid_steps > 0 else 0
                avg_reward = total_reward / steps
                print(f"\n  Epoch complete: Avg Loss={avg_loss:.4f}, Avg Reward={avg_reward:.2f}")
        
        return self.model

def load_training_data(data_path, limit=None):
    """Load training data for GRPO"""
    data = []
    data_path = Path(data_path)
    
    if not data_path.exists():
        print(f"Data file not found: {data_path}")
        return data
    
    with open(data_path, 'r') as f:
        for i, line in enumerate(f):
            if limit and i >= limit:
                break
            try:
                item = json.loads(line)
                data.append({
                    'prompt': item.get('prompt', ''),
                    'answer': item.get('ground_truth', item.get('response', ''))
                })
            except:
                continue
    
    return data

def main():
    print("=" * 60)
    print("SHOREKEEPER GRPO Training")
    print("The Reasoning Magic")
    print("=" * 60)
    
    # Check device
    if torch.cuda.is_available():
        device = torch.device("cuda")
        print(f"\n✓ CUDA: {torch.cuda.get_device_name(0)}")
        print(f"  Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB")
    
    # Load trained model (full precision for training)
    print("\n1. Loading trained SHOREKEEPER model...")
    model_path = Path("./outputs/shorekeeper-4b-final.pt")
    
    if not model_path.exists():
        print(f"\n❌ Model not found at {model_path}")
        print("   Run training first: python3 scripts/04_train.py")
        return
    
    model = SHOREKEEPER()  # Use full model (not memory efficient for training)
    model.load_state_dict(torch.load(model_path, map_location=device))
    model = model.to(device)
    model.train()
    print(f"   ✓ Model loaded from {model_path}")
    
    # Load tokenizer
    print("\n2. Loading tokenizer...")
    tokenizer = AutoTokenizer.from_pretrained("gpt2")
    tokenizer.pad_token = tokenizer.eos_token
    print("   ✓ Using GPT-2 tokenizer")
    
    # Load training data
    print("\n3. Loading training data...")
    data_path = Path("./data/processed/train.jsonl")
    
    if not data_path.exists():
        print(f"\n❌ No data at {data_path}")
        return
    
    print("   Options:")
    print("   [1] Quick test (20 examples)")
    print("   [2] Small training (100 examples, 3 epochs)")
    
    choice = input("\nChoose option (1/2): ").strip()
    
    if choice == "1":
        limit = 20
        epochs = 1
    else:
        limit = 100
        epochs = 3
    
    data = load_training_data(data_path, limit=limit)
    print(f"\n   Loaded {len(data)} examples")
    print(f"   Training for {epochs} epochs")
    
    # GRPO config
    config = {
        'group_size': 2,
        'learning_rate': 1e-6
    }
    
    print("\n4. Initializing GRPO Trainer...")
    trainer = GRPOTrainer(model, tokenizer, config)
    
    print("\n5. Starting GRPO training...")
    print("   (This teaches the model to reason)\n")
    
    try:
        trained_model = trainer.train(data, num_epochs=epochs)
    except KeyboardInterrupt:
        print("\n   Interrupted")
    except Exception as e:
        print(f"\n   Error: {e}")
        import traceback
        traceback.print_exc()
    
    # Save model
    print("\n6. Saving model...")
    output_dir = Path("./outputs/grpo")
    output_dir.mkdir(parents=True, exist_ok=True)
    
    torch.save(model.state_dict(), output_dir / "shorekeeper-4b-grpo.pt")
    print(f"   ✓ Saved to {output_dir / 'shorekeeper-4b-grpo.pt'}")
    
    print("\n" + "=" * 60)
    print("✅ GRPO Complete!")
    print("=" * 60)
    print("\nNow run SHOREKEEPER:")
    print("  python3 scripts/07_run_shorekeeper.py")

if __name__ == "__main__":
    main()