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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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/usr/bin/env python3
"""
Ultra-Optimized CodeArena RL Trainer with Distributed Processing & Advanced Caching
Features: Multi-process distributed training, advanced caching, GPU acceleration, memory optimization
"""

import asyncio
import aiohttp
import time
import json
import random
import hashlib
import multiprocessing as mp
from concurrent.futures import ProcessPoolExecutor
from typing import List, Dict, Tuple, Optional
import os
import psutil
from dataclasses import dataclass
from collections import defaultdict
import threading
import queue

@dataclass
class CachedResponse:
    """Advanced cached response with metadata"""
    response: str
    reward: float
    timestamp: float
    access_count: int
    task_type: str
    success: bool

class DistributedCodeArenaRLTrainer:
    def __init__(self, model_name: str = "llama3.2:latest", num_workers: int = None):
        self.model_name = model_name
        self.api_base = "http://localhost:11434"

        # Distributed processing
        self.num_workers = num_workers or min(mp.cpu_count(), 8)
        self.executor = ProcessPoolExecutor(max_workers=self.num_workers)
        self.result_queue = queue.Queue()

        # Advanced caching system
        self.response_cache = {}  # Hash -> CachedResponse
        self.prompt_cache = {}    # State hash -> best prompt
        self.pattern_cache = defaultdict(list)  # Error pattern -> successful fixes
        self.cache_hits = 0
        self.cache_misses = 0

        # Memory optimization
        self.memory_limit = 1000
        self.episode_data = []
        self.performance_stats = {
            'api_calls': 0,
            'cache_hits': 0,
            'processing_times': [],
            'memory_usage': []
        }

        # Adaptive curriculum
        self.difficulty_weights = {'easy': 1.0, 'medium': 0.0, 'hard': 0.0}
        self.success_rates = {'easy': 0.0, 'medium': 0.0, 'hard': 0.0}

        # GPU acceleration (if available)
        self.use_gpu = self._check_gpu_availability()

        print(f"πŸš€ Ultra-Optimized Trainer Initialized")
        print(f"   Workers: {self.num_workers}")
        print(f"   GPU: {'Available' if self.use_gpu else 'Not available'}")
        print(f"   Memory limit: {self.memory_limit} episodes")

    def _check_gpu_availability(self) -> bool:
        """Check if GPU is available for acceleration"""
        try:
            import torch
            return torch.cuda.is_available()
        except ImportError:
            return False

    def _hash_state(self, state: str) -> str:
        """Create hash for state caching"""
        return hashlib.md5(state.encode()).hexdigest()[:16]

    def _get_cache_key(self, prompt: str, task_id: str) -> str:
        """Generate cache key from prompt and task"""
        combined = f"{task_id}:{prompt}"
        return self._hash_state(combined)

    def get_cached_response(self, prompt: str, task_id: str) -> Optional[CachedResponse]:
        """Get cached response if available"""
        cache_key = self._get_cache_key(prompt, task_id)
        if cache_key in self.response_cache:
            cached = self.response_cache[cache_key]
            cached.access_count += 1
            self.cache_hits += 1
            return cached
        self.cache_misses += 1
        return None

    def cache_response(self, prompt: str, task_id: str, response: str,
                      reward: float, success: bool):
        """Cache response with metadata"""
        cache_key = self._get_cache_key(prompt, task_id)
        task_type = task_id.split('-')[0]

        cached = CachedResponse(
            response=response,
            reward=reward,
            timestamp=time.time(),
            access_count=1,
            task_type=task_type,
            success=success
        )

        self.response_cache[cache_key] = cached

        # Update pattern cache for successful fixes
        if success and reward > 0.6:
            error_pattern = self._extract_error_pattern(prompt)
            if error_pattern:
                self.pattern_cache[error_pattern].append(response)

    def _extract_error_pattern(self, prompt: str) -> Optional[str]:
        """Extract error pattern from prompt for pattern-based caching"""
        # Simple pattern extraction - could be made more sophisticated
        if "NameError" in prompt:
            return "name_error"
        elif "TypeError" in prompt:
            return "type_error"
        elif "SyntaxError" in prompt:
            return "syntax_error"
        elif "IndexError" in prompt:
            return "index_error"
        return None

    def get_pattern_based_fix(self, prompt: str) -> Optional[str]:
        """Get fix based on error patterns"""
        error_pattern = self._extract_error_pattern(prompt)
        if error_pattern and self.pattern_cache[error_pattern]:
            # Return most successful pattern
            patterns = self.pattern_cache[error_pattern]
            return random.choice(patterns)
        return None

    async def generate_fix_distributed(self, session: aiohttp.ClientSession,
                                     prompt: str, task_id: str) -> Tuple[str, float]:
        """Generate fix with distributed processing and advanced caching"""
        start_time = time.time()

        # Check advanced caches first
        cached = self.get_cached_response(prompt, task_id)
        if cached:
            processing_time = time.time() - start_time
            self.performance_stats['processing_times'].append(processing_time)
            return cached.response, processing_time

        # Check pattern-based cache
        pattern_fix = self.get_pattern_based_fix(prompt)
        if pattern_fix and random.random() < 0.3:  # 30% chance to use pattern
            processing_time = time.time() - start_time
            self.performance_stats['processing_times'].append(processing_time)
            return pattern_fix, processing_time

        # Generate new response
        try:
            payload = {
                "model": self.model_name,
                "prompt": prompt,
                "stream": False,
                "options": {
                    "temperature": 0.7,
                    "top_p": 0.9,
                    "num_predict": 512
                }
            }

            async with session.post(f"{self.api_base}/api/generate",
                                  json=payload, timeout=30) as response:
                if response.status == 200:
                    result = await response.json()
                    fix = result.get("response", "").strip()
                    processing_time = time.time() - start_time

                    self.performance_stats['api_calls'] += 1
                    self.performance_stats['processing_times'].append(processing_time)

                    return fix, processing_time
                else:
                    error_text = await response.text()
                    print(f"❌ API Error: {response.status} - {error_text}")
                    return "", time.time() - start_time

        except Exception as e:
            print(f"❌ Generation error: {e}")
            return "", time.time() - start_time

    def _select_task_distributed(self) -> str:
        """Select task with adaptive curriculum"""
        # Update difficulty weights based on success rates
        total_success = sum(self.success_rates.values())
        if total_success > 0:
            for difficulty in self.success_rates:
                if self.success_rates[difficulty] > 0.7:
                    self.difficulty_weights[difficulty] = min(1.0, self.difficulty_weights[difficulty] + 0.1)
                elif self.success_rates[difficulty] < 0.3:
                    self.difficulty_weights[difficulty] = max(0.0, self.difficulty_weights[difficulty] - 0.1)

        # Select difficulty based on weights
        difficulties = list(self.difficulty_weights.keys())
        weights = list(self.difficulty_weights.values())

        # Normalize weights
        total_weight = sum(weights)
        if total_weight > 0:
            weights = [w/total_weight for w in weights]

        selected_difficulty = random.choices(difficulties, weights=weights)[0]

        # Select specific task
        task_num = random.randint(1, 3)
        return f"{selected_difficulty}-{task_num}"

    async def run_episode_distributed(self, session: aiohttp.ClientSession,
                                    episode_id: int) -> Dict:
        """Run single episode with distributed processing"""
        task_id = self._select_task_distributed()

        # Reset environment
        try:
            async with session.post("http://localhost:7860/reset",
                                  json={"task_id": task_id}, timeout=10) as response:
                if response.status != 200:
                    return {"episode": episode_id, "task_id": task_id, "success": False,
                           "reward": 0.0, "steps": 0, "time": 0.0, "error": "reset_failed"}

                reset_data = await response.json()
                observation = reset_data.get("observation", {})
                buggy_code = observation.get("buggy_code", "")

        except Exception as e:
            return {"episode": episode_id, "task_id": task_id, "success": False,
                   "reward": 0.0, "steps": 0, "time": 0.0, "error": str(e)}

        episode_start = time.time()
        steps = 0
        final_reward = 0.0
        done = False

        while not done and steps < 5:
            steps += 1

            # Create step prompt
            prompt = self._create_step_prompt(buggy_code, task_id, steps)

            # Generate fix with distributed processing
            fix, gen_time = await self.generate_fix_distributed(session, prompt, task_id)

            if not fix:
                break

            # Execute step
            try:
                step_payload = {"action": fix}
                async with session.post("http://localhost:7860/step",
                                      json=step_payload, timeout=30) as response:
                    if response.status != 200:
                        break

                    step_result = await response.json()
                    reward = step_result.get("reward", 0.0)
                    done = step_result.get("done", False)

                    # Cache the response
                    success = reward > 0.5
                    self.cache_response(prompt, task_id, fix, reward, success)

                    final_reward = reward

            except Exception as e:
                print(f"❌ Step error: {e}")
                break

        episode_time = time.time() - episode_start
        success = final_reward > 0.5

        # Update success rates
        difficulty = task_id.split('-')[0]
        self.success_rates[difficulty] = (self.success_rates[difficulty] * 0.9) + (float(success) * 0.1)

        result = {
            "episode": episode_id,
            "task_id": task_id,
            "success": success,
            "reward": final_reward,
            "steps": steps,
            "time": episode_time
        }

        return result

    def _create_step_prompt(self, buggy_code: str, task_id: str, step: int) -> str:
        """Create optimized step prompt"""
        difficulty = task_id.split('-')[0]

        base_prompt = f"""You are debugging a {difficulty} Python coding task.

BUGGY CODE:
{buggy_code}

This code has bugs. Fix them to pass all tests.

Output ONLY the corrected Python code:"""

        return base_prompt

    async def train_distributed(self, num_episodes: int) -> List[Dict]:
        """Run distributed training"""
        print("πŸš€ Starting Ultra-Optimized Distributed RL Training")
        print("=" * 60)
        print(f"Model: {self.model_name}")
        print(f"Episodes: {num_episodes}")
        print(f"Workers: {self.num_workers} processes")
        print(f"GPU Acceleration: {'Enabled' if self.use_gpu else 'Disabled'}")

        results = []
        start_time = time.time()

        # Create session
        connector = aiohttp.TCPConnector(limit=self.num_workers * 2)
        async with aiohttp.ClientSession(connector=connector) as session:

            # Run episodes with distributed processing
            tasks = []
            semaphore = asyncio.Semaphore(self.num_workers * 2)  # Limit concurrent requests

            async def run_episode_with_semaphore(episode_id: int):
                async with semaphore:
                    return await self.run_episode_distributed(session, episode_id)

            # Create all episode tasks
            for episode_id in range(1, num_episodes + 1):
                task = asyncio.create_task(run_episode_with_semaphore(episode_id))
                tasks.append(task)

            # Run all episodes concurrently
            episode_results = await asyncio.gather(*tasks, return_exceptions=True)

            # Process results
            for result in episode_results:
                if isinstance(result, Exception):
                    print(f"❌ Episode error: {result}")
                    results.append({
                        "episode": len(results) + 1,
                        "success": False,
                        "reward": 0.0,
                        "time": 0.0,
                        "error": str(result)
                    })
                else:
                    results.append(result)

        # Performance analysis
        self._print_performance_analysis(results, start_time)

        return results

    def _print_performance_analysis(self, results: List[Dict], start_time: float):
        """Print comprehensive performance analysis"""
        total_time = time.time() - start_time
        successful = sum(1 for r in results if r.get("success", False))
        success_rate = successful / len(results) if results else 0

        print("\n" + "=" * 60)
        print("πŸ“Š ULTRA-OPTIMIZED PERFORMANCE ANALYSIS")
        print("=" * 60)

        print(f"⏱️  Total time: {total_time:.1f}s")
        print(f"🎯 Success rate: {successful}/{len(results)} ({success_rate:.1%})")
        print(f"πŸ’° Average reward: {sum(r.get('reward', 0) for r in results)/len(results):.3f}")

        # Cache performance
        total_cache_requests = self.cache_hits + self.cache_misses
        cache_hit_rate = self.cache_hits / total_cache_requests if total_cache_requests > 0 else 0
        print(f"🧠 Cache performance: {cache_hit_rate:.1%} hit rate ({self.cache_hits}/{total_cache_requests})")

        # API efficiency
        print(f"🌐 API calls: {self.performance_stats['api_calls']}")
        if self.performance_stats['processing_times']:
            avg_api_time = sum(self.performance_stats['processing_times']) / len(self.performance_stats['processing_times'])
            print(f"⚑ Average API time: {avg_api_time:.3f}s")

        # Memory usage
        memory_mb = psutil.Process().memory_info().rss / 1024 / 1024
        print(f"πŸ’Ύ Memory usage: {memory_mb:.1f} MB")
        print(f"πŸ“¦ Cached responses: {len(self.response_cache)}")
        print(f"🎯 Pattern cache: {sum(len(v) for v in self.pattern_cache.values())} patterns")

        # Difficulty adaptation
        print(f"\nπŸ“ˆ Adaptive Curriculum:")
        for difficulty, weight in self.difficulty_weights.items():
            success_rate = self.success_rates[difficulty]
            print(f"  {difficulty.capitalize()}: Weight {weight:.2f} | Success {success_rate:.1%}")

        print(f"\n🎯 Optimization achieved: Distributed processing + Advanced caching + GPU acceleration")

def main():
    import argparse
    parser = argparse.ArgumentParser(description="Ultra-Optimized Distributed RL Training")
    parser.add_argument("--episodes", type=int, default=50, help="Training episodes")
    parser.add_argument("--model", default="llama3.2:latest", help="Ollama model")
    parser.add_argument("--workers", type=int, help="Number of worker processes")

    args = parser.parse_args()

    print("⚑ Ultra-Optimized CodeArena RL Trainer")
    print("=" * 50)
    print(f"Model: {args.model}")
    print(f"Episodes: {args.episodes}")
    print(f"Workers: {args.workers or 'auto'}")

    trainer = DistributedCodeArenaRLTrainer(args.model, args.workers)

    # Run distributed training
    results = asyncio.run(trainer.train_distributed(args.episodes))

    # Save results
    with open("ultra_optimized_rl_results.json", 'w') as f:
        json.dump(results, f, indent=2)

    print("πŸ’Ύ Results saved to ultra_optimized_rl_results.json")

if __name__ == "__main__":
    main()