File size: 11,880 Bytes
03a7eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Optimized RL Trainer for CodeArena with speed and efficiency improvements.
"""

import asyncio
import aiohttp
import time
import json
import random
from typing import List, Dict, Tuple
from collections import deque
import numpy as np
from concurrent.futures import ThreadPoolExecutor
import threading

class OptimizedCodeArenaRLTrainer:
    def __init__(self, model_name: str = "llama3.2:latest", memory_size: int = 2000):
        self.model_name = model_name
        self.api_base = "http://localhost:11434"

        # Optimized memory management
        self.memory = deque(maxlen=memory_size)
        self.trajectories = []
        self.successful_trajectories = []

        # Performance optimizations
        self.executor = ThreadPoolExecutor(max_workers=4)
        self.session = None  # For async HTTP
        self.response_cache = {}
        self.prompt_cache = {}

        # RL parameters (optimized)
        self.learning_rate = 0.001
        self.gamma = 0.95
        self.epsilon = 1.0
        self.epsilon_min = 0.05  # Lower minimum for more exploitation
        self.epsilon_decay = 0.997  # Slower decay
        self.batch_size = 64  # Larger batches

        # Performance tracking
        self.start_time = time.time()
        self.episode_times = []
        self.api_call_times = []

        # Adaptive difficulty
        self.current_difficulty = "easy"
        self.task_performance = {"easy": [], "medium": [], "hard": []}

    async def init_session(self):
        """Initialize async HTTP session"""
        if self.session is None:
            self.session = aiohttp.ClientSession()

    async def close_session(self):
        """Close async session"""
        if self.session:
            await self.session.close()
            self.session = None

    async def generate_fix_optimized(self, prompt: str) -> str:
        """Optimized fix generation with caching and async"""
        # Check cache first
        cache_key = hash(prompt)
        if cache_key in self.response_cache:
            return self.response_cache[cache_key]

        start_time = time.time()

        try:
            payload = {
                "model": self.model_name,
                "prompt": prompt,
                "stream": False,
                "options": {
                    "temperature": max(0.1, self.epsilon),
                    "num_predict": 600,  # Shorter for speed
                    "top_p": 0.9,
                    "num_thread": 4  # Use multiple threads
                }
            }

            async with self.session.post(f"{self.api_base}/api/generate",
                                       json=payload, timeout=15) as response:
                result = await response.json()
                fix = result.get("response", "").strip()

                # Clean response
                if fix.startswith("```python"):
                    fix = fix[9:]
                if fix.startswith("```"):
                    fix = fix[3:]
                if fix.endswith("```"):
                    fix = fix[:-3]
                fix = fix.strip()

                # Cache successful responses
                if fix and len(fix) > 10:
                    self.response_cache[cache_key] = fix

                api_time = time.time() - start_time
                self.api_call_times.append(api_time)

                return fix

        except Exception as e:
            print(f"API Error: {e}")
            return "def placeholder():\n    pass"

    def get_optimized_prompt(self, buggy_code: str, error_log: str,
                           test_results: str, step_count: int,
                           previous_attempts: List[str]) -> str:
        """Generate optimized prompt with caching"""

        # Create cache key
        state_key = f"{hash(buggy_code)}|{hash(error_log)}|{hash(test_results)}|{step_count}"
        if state_key in self.prompt_cache:
            return self.prompt_cache[state_key]

        # Optimized prompt template
        prompt = f"""Fix Python code - Step {step_count}:

CODE:
{buggy_code}

ERRORS:
{error_log}

TESTS:
{test_results}

Requirements: Compile, pass tests, fix root cause. Return only code."""

        self.prompt_cache[state_key] = prompt
        return prompt

    async def run_episode_async(self, task_id: str, episode_num: int) -> Dict:
        """Run episode with async optimizations"""
        episode_start = time.time()

        try:
            # Async reset
            async with self.session.post("http://localhost:7860/reset",
                                       json={"task_id": task_id}, timeout=10) as response:
                obs = await response.json()

        except Exception as e:
            print(f"Episode {episode_num} reset failed: {e}")
            return {"success": False, "reward": 0, "steps": 0, "time": time.time() - episode_start}

        rewards = []
        previous_attempts = []
        done = False
        step_count = 0

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

            # Generate optimized prompt
            prompt = self.get_optimized_prompt(
                obs.get('buggy_code', ''),
                obs.get('error_log', ''),
                obs.get('test_results', ''),
                step_count,
                previous_attempts
            )

            # Async fix generation
            fix = await self.generate_fix_optimized(prompt)

            try:
                # Async step execution
                async with self.session.post("http://localhost:7860/step",
                                           json={"proposed_fix": fix}, timeout=20) as response:
                    result = await response.json()

                    reward = result.get('reward', 0)
                    done = result.get('done', False)
                    obs = result.get('observation', {})

                    rewards.append(reward)
                    previous_attempts.append(fix)

            except Exception as e:
                print(f"Episode {episode_num} step {step_count} failed: {e}")
                break

        episode_time = time.time() - episode_start
        self.episode_times.append(episode_time)

        final_reward = rewards[-1] if rewards else 0
        success = final_reward > 0.5

        return {
            "episode": episode_num,
            "task_id": task_id,
            "success": success,
            "reward": final_reward,
            "steps": step_count,
            "time": episode_time
        }

    async def train_async(self, episodes: int = 50):
        """Async training loop for maximum speed"""
        await self.init_session()

        print("πŸš€ Starting Optimized Async RL Training")
        print("=" * 60)
        print(f"Model: {self.model_name}")
        print(f"Episodes: {episodes}")
        print(f"Async: Enabled")
        print(f"Workers: 4 threads")

        results = []
        batch_size = 5  # Run 5 episodes concurrently

        for batch_start in range(0, episodes, batch_size):
            batch_end = min(batch_start + batch_size, episodes)
            batch_tasks = []

            # Create batch of concurrent episodes
            for i in range(batch_start, batch_end):
                task_id = f"{self.current_difficulty}-{random.randint(1, 3)}"
                task = self.run_episode_async(task_id, i + 1)
                batch_tasks.append(task)

            # Execute batch concurrently
            batch_start_time = time.time()
            batch_results = await asyncio.gather(*batch_tasks, return_exceptions=True)
            batch_time = time.time() - batch_start_time

            # Process results
            for result in batch_results:
                if isinstance(result, Exception):
                    print(f"Batch error: {result}")
                    continue

                results.append(result)

                # Update difficulty if needed
                if result["success"] and result["reward"] > 0.7:
                    self.task_performance[self.current_difficulty].append(result["reward"])

                # Progress tracking
                if len(results) % 10 == 0:
                    recent = results[-10:]
                    success_rate = sum(1 for r in recent if r["success"]) / len(recent)
                    avg_reward = sum(r["reward"] for r in recent) / len(recent)
                    avg_time = sum(r["time"] for r in recent) / len(recent)

                    print(f"Ep {len(results):3d} | Success: {success_rate:.1%} | Reward: {avg_reward:.3f} | Time: {avg_time:.2f}s")
            print(f"πŸ“¦ Batch {batch_start//batch_size + 1} completed in {batch_time:.1f}s")

        await self.close_session()
        return results

    def print_performance_stats(self, results: List[Dict]):
        """Print detailed performance statistics"""
        print("\n" + "=" * 60)
        print("πŸ“Š PERFORMANCE STATISTICS")
        print("=" * 60)

        total_time = time.time() - self.start_time
        total_episodes = len(results)
        successful = sum(1 for r in results if r["success"])

        print(f"⏱️  Total time: {total_time:.1f}s")
        print(f"🎯 Success rate: {successful}/{total_episodes} ({successful/total_episodes:.1%})")
        print(f"πŸ’° Average reward: {sum(r['reward'] for r in results)/len(results):.3f}")
        if self.episode_times:
            print(f"⚑ Average episode time: {sum(self.episode_times)/len(self.episode_times):.3f}s")
            print(f"🐌 Slowest episode: {max(self.episode_times):.3f}s")
            print(f"πŸš€ Fastest episode: {min(self.episode_times):.3f}s")
        if self.api_call_times:
            print(f"🌐 Average API call: {sum(self.api_call_times)/len(self.api_call_times):.3f}s")
            print(f"πŸ“‘ Slowest API call: {max(self.api_call_times):.3f}s")
            print(f"πŸ’¨ Fastest API call: {min(self.api_call_times):.3f}s")
        print(f"πŸ’Ύ Memory usage: {len(self.memory)} experiences")
        print(f"🧠 Cache hits: {len(self.response_cache)} responses cached")
        print(f"πŸ“ Prompts cached: {len(self.prompt_cache)} states")

        # Success rate over time
        print(f"\nπŸ“ˆ Learning Progress:")
        for i in range(0, len(results), 10):
            batch = results[i:i+10]
            if batch:
                success_rate = sum(1 for r in batch if r["success"]) / len(batch)
                avg_reward = sum(r["reward"] for r in batch) / len(batch)
                print(f"Ep {i+1:2d}-{min(i+10, len(results)):2d}: Success {success_rate:.1%} | Reward {avg_reward:.3f}")
def main():
    import argparse
    parser = argparse.ArgumentParser(description="Optimized Async 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("--use_async", action="store_true", default=True, help="Use async training")

    args = parser.parse_args()

    print("⚑ Optimized CodeArena RL Trainer")
    print("=" * 50)
    print(f"Model: {args.model}")
    print(f"Episodes: {args.episodes}")
    print(f"Async: {args.use_async}")

    trainer = OptimizedCodeArenaRLTrainer(args.model)

    if args.use_async:
        # Run async training
        results = asyncio.run(trainer.train_async(args.episodes))
    else:
        # Fallback to sync (not implemented in this optimized version)
        print("⚠️  Async training required for optimal performance")
        return

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

    trainer.print_performance_stats(results)

    print("\nπŸ’Ύ Results saved to optimized_rl_results.json")
    print("🎯 Optimization achieved: Async processing + caching + batching")

if __name__ == "__main__":
    main()