File size: 13,380 Bytes
54c5666
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python3
"""

Inference Script for Trained Models

Supports interactive chat, batch generation, and API serving

"""

import os
import sys
import json
import argparse
import torch
from pathlib import Path

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'src'))

from models.architecture import AdvancedGPTModel, ModelConfig
from utils.generation import AdvancedGenerator, ControllableGenerator, GenerationConfig, create_generation_configs

try:
    import tiktoken
    TIKTOKEN_AVAILABLE = True
except ImportError:
    TIKTOKEN_AVAILABLE = False


class SimpleTokenizer:
    """Fallback tokenizer"""
    def __init__(self, vocab_file=None):
        if vocab_file and os.path.exists(vocab_file):
            with open(vocab_file, 'r') as f:
                data = json.load(f)
                self.chars = data.get('chars', list('abcdefghijklmnopqrstuvwxyz'))
        else:
            self.chars = list('abcdefghijklmnopqrstuvwxyz')
        
        self.stoi = {c: i for i, c in enumerate(self.chars)}
        self.itos = {i: c for i, c in enumerate(self.chars)}
        self.vocab_size = len(self.chars)

    def encode(self, text):
        return [self.stoi.get(c, 0) for c in text]

    def decode(self, tokens):
        return ''.join([self.itos.get(t, '') for t in tokens])


def load_model_and_tokenizer(checkpoint_path, device='cuda'):
    """Load trained model and tokenizer"""
    print(f"Loading model from {checkpoint_path}")
    
    # Load checkpoint
    if os.path.isdir(checkpoint_path):
        model_path = os.path.join(checkpoint_path, "pytorch_model.bin")
        tokenizer_path = os.path.join(checkpoint_path, "tokenizer.json")
    else:
        model_path = checkpoint_path
        tokenizer_path = None
    
    checkpoint = torch.load(model_path, map_location=device)
    
    # Get model config
    config_dict = checkpoint.get('config', {})
    model_config_dict = config_dict.get('model_config_dict', {})
    
    if not model_config_dict:
        # Fallback config
        model_config_dict = {
            'vocab_size': 50304,
            'n_positions': 2048,
            'n_embd': 768,
            'n_layer': 12,
            'n_head': 12,
            'n_kv_head': 4,
            'rotary_dim': 64,
            'intermediate_size': 3072,
            'activation': 'swiglu',
            'norm_type': 'rmsnorm',
            'norm_eps': 1e-5,
            'dropout': 0.0,
            'attention_dropout': 0.0,
            'residual_dropout': 0.1,
            'embed_dropout': 0.1,
            'tie_word_embeddings': True,
            'use_cache': True,
            'attention_bias': False,
            'mlp_bias': False,
            'flash_attention': True,
            'gradient_checkpointing': False,
            'max_position_embeddings': 2048
        }
    
    # Create model
    model_config = ModelConfig(**model_config_dict)
    model = AdvancedGPTModel(model_config)
    
    # Load state dict
    model.load_state_dict(checkpoint['model_state_dict'])
    model = model.to(device)
    model.eval()
    
    # Load tokenizer
    if tokenizer_path and os.path.exists(tokenizer_path):
        with open(tokenizer_path, 'r') as f:
            tokenizer_info = json.load(f)
        
        if tokenizer_info.get('type') == 'tiktoken' and TIKTOKEN_AVAILABLE:
            tokenizer = tiktoken.get_encoding('gpt2')
        else:
            tokenizer = SimpleTokenizer()
    else:
        # Try tiktoken
        if TIKTOKEN_AVAILABLE:
            tokenizer = tiktoken.get_encoding('gpt2')
        else:
            tokenizer = SimpleTokenizer()
    
    print(f"Model loaded: {sum(p.numel() for p in model.parameters()):,} parameters")
    # Support tiktoken Encoding (n_vocab) and SimpleTokenizer (vocab_size/chars)
    tok_vs = None
    for attr in ('vocab_size', 'n_vocab'):
        if hasattr(tokenizer, attr):
            tok_vs = getattr(tokenizer, attr)
            break
    if tok_vs is None:
        tok_vs = len(getattr(tokenizer, 'chars', [])) or 'unknown'
    print(f"Tokenizer: {tok_vs} vocab size")
    
    return model, tokenizer


def interactive_chat(model, tokenizer, device, overrides=None):
    """Interactive chat interface"""
    generator = AdvancedGenerator(model, tokenizer, device)
    controllable = ControllableGenerator(model, tokenizer, device)
    configs = create_generation_configs()
    
    print("\n" + "="*50)
    print("πŸ€– CLAUDE OPUS 4 SCALE MODEL - INTERACTIVE CHAT")
    print("="*50)
    print("Commands:")
    print("  /help - Show this help")
    print("  /config <name> - Change generation config (creative, balanced, focused, etc.)")
    print("  /style <style> - Set style (formal, casual, creative, technical, etc.)")
    print("  /clear - Clear conversation history")
    print("  /quit - Exit chat")
    print("="*50)
    
    current_config = configs['balanced']
    # Apply CLI overrides if provided
    if overrides:
        if 'max_new_tokens' in overrides and overrides['max_new_tokens'] is not None:
            current_config.max_new_tokens = int(overrides['max_new_tokens'])
        if 'temperature' in overrides and overrides['temperature'] is not None:
            current_config.temperature = float(overrides['temperature'])
        if 'top_k' in overrides and overrides['top_k'] is not None:
            current_config.top_k = int(overrides['top_k'])
        if 'top_p' in overrides and overrides['top_p'] is not None:
            current_config.top_p = float(overrides['top_p'])
        if 'do_sample' in overrides and overrides['do_sample'] is not None:
            current_config.do_sample = bool(overrides['do_sample'])
    current_style = None
    conversation_history = ""
    
    while True:
        try:
            user_input = input("\nπŸ§‘ You: ").strip()
            
            if not user_input:
                continue
            
            # Handle commands
            if user_input.startswith('/'):
                cmd_parts = user_input[1:].split()
                cmd = cmd_parts[0].lower()
                
                if cmd == 'quit':
                    print("πŸ‘‹ Goodbye!")
                    break
                elif cmd == 'help':
                    print("\nAvailable configs:", list(configs.keys()))
                    print("Available styles: formal, casual, creative, technical, humorous, serious")
                    continue
                elif cmd == 'config' and len(cmd_parts) > 1:
                    config_name = cmd_parts[1]
                    if config_name in configs:
                        current_config = configs[config_name]
                        print(f"βœ… Config changed to: {config_name}")
                    else:
                        print(f"❌ Unknown config: {config_name}")
                    continue
                elif cmd == 'style' and len(cmd_parts) > 1:
                    current_style = cmd_parts[1]
                    print(f"βœ… Style set to: {current_style}")
                    continue
                elif cmd == 'clear':
                    conversation_history = ""
                    print("πŸ—‘οΈ Conversation history cleared")
                    continue
                else:
                    print("❌ Unknown command. Type /help for available commands.")
                    continue
            
            # Prepare prompt
            conversation_history += f"\nHuman: {user_input}\nAssistant: "
            
            # Generate response
            print("πŸ€– Assistant: ", end="", flush=True)
            
            def stream_callback(text):
                print(text, end="", flush=True)
            
            if current_style:
                response = controllable.generate_with_style(
                    conversation_history, current_style, current_config
                )
            else:
                response = generator.generate(
                    conversation_history, current_config, stream=True, callback=stream_callback
                )
            
            if not current_config.do_sample or current_style:
                print(response, end="", flush=True)
            
            # Update conversation history
            conversation_history += response
            print()  # New line
            
        except KeyboardInterrupt:
            print("\nπŸ‘‹ Chat interrupted. Goodbye!")
            break
        except Exception as e:
            print(f"\n❌ Error: {e}")


def batch_generation(model, tokenizer, device, prompts_file, output_file, config_name='balanced'):
    """Batch generation from file"""
    generator = AdvancedGenerator(model, tokenizer, device)
    configs = create_generation_configs()
    config = configs.get(config_name, configs['balanced'])
    
    # Load prompts
    with open(prompts_file, 'r', encoding='utf-8') as f:
        prompts = [line.strip() for line in f if line.strip()]
    
    print(f"Generating responses for {len(prompts)} prompts...")
    
    results = []
    for i, prompt in enumerate(prompts):
        print(f"Processing {i+1}/{len(prompts)}: {prompt[:50]}...")
        
        response = generator.generate(prompt, config)
        results.append({
            'prompt': prompt,
            'response': response,
            'config': config_name
        })
    
    # Save results
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(results, f, indent=2, ensure_ascii=False)
    
    print(f"Results saved to {output_file}")


def benchmark_model(model, tokenizer, device):
    """Run model benchmarks"""
    from utils.generation import BenchmarkGenerator
    from utils.evaluation import run_evaluation
    
    print("πŸ”¬ Running model benchmarks...")
    
    # Speed benchmark
    benchmark_gen = BenchmarkGenerator(model, tokenizer, device)
    test_prompts = [
        "Explain quantum computing",
        "Write a short story about AI",
        "Describe the benefits of renewable energy",
        "What is the future of space exploration?",
        "How does machine learning work?"
    ]
    
    speed_results = benchmark_gen.speed_benchmark(test_prompts)
    print("\nπŸ“Š Speed Benchmark Results:")
    print(f"  Tokens/second: {speed_results['tokens_per_second']:.2f}")
    print(f"  Average time per batch: {speed_results['avg_time_per_batch']:.2f}s")
    
    # Quality evaluation
    print("\n🎯 Running quality evaluation...")
    eval_results = run_evaluation(model, tokenizer, device)
    
    return {**speed_results, **eval_results}


def main():
    parser = argparse.ArgumentParser(description="Model Inference")
    parser.add_argument("--checkpoint", type=str, required=True, help="Path to model checkpoint")
    parser.add_argument("--mode", type=str, choices=['chat', 'batch', 'benchmark'], default='chat')
    parser.add_argument("--prompts", type=str, help="Input prompts file for batch mode")
    parser.add_argument("--output", type=str, help="Output file for batch mode")
    parser.add_argument("--config", type=str, default='balanced', help="Generation config")
    parser.add_argument("--device", type=str, default='cuda', help="Device to use")
    # Optional generation overrides
    parser.add_argument("--max_new_tokens", type=int, default=None, help="Max new tokens to generate")
    parser.add_argument("--temperature", type=float, default=None, help="Sampling temperature")
    parser.add_argument("--top_k", type=int, default=None, help="Top-k sampling")
    parser.add_argument("--top_p", type=float, default=None, help="Top-p sampling")
    parser.add_argument("--do_sample", type=int, default=None, help="1 to enable sampling, 0 to disable")
    
    args = parser.parse_args()
    
    # Setup device
    if args.device == 'cuda' and not torch.cuda.is_available():
        print("⚠️ CUDA not available, using CPU")
        device = 'cpu'
    else:
        device = args.device
    
    # Load model
    model, tokenizer = load_model_and_tokenizer(args.checkpoint, device)
    
    # Run inference
    if args.mode == 'chat':
        overrides = {
            'max_new_tokens': args.max_new_tokens,
            'temperature': args.temperature,
            'top_k': args.top_k,
            'top_p': args.top_p,
            'do_sample': (None if args.do_sample is None else bool(args.do_sample)),
        }
        interactive_chat(model, tokenizer, device, overrides)
    elif args.mode == 'batch':
        if not args.prompts or not args.output:
            print("❌ Batch mode requires --prompts and --output arguments")
            return
        batch_generation(model, tokenizer, device, args.prompts, args.output, args.config)
    elif args.mode == 'benchmark':
        results = benchmark_model(model, tokenizer, device)
        print("\nπŸ“‹ Benchmark Summary:")
        for key, value in results.items():
            if isinstance(value, (int, float)):
                print(f"  {key}: {value:.3f}")


if __name__ == "__main__":
    main()