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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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env python3
"""
Streaming Teacher Cache with Async Background Fetching.

This module implements a producer-consumer pattern for knowledge distillation:
1. Producer: Background async task that continuously fetches teacher logits
2. Consumer: Training dataloader that reads from the cache

The cache grows during training, allowing training to start immediately
with a small initial cache while more samples are fetched in background.

Usage:
    cache = StreamingTeacherCache(tokenizer, initial_samples=10000)
    await cache.warmup()  # Pre-cache initial samples
    cache.start_background_fetching()  # Start producer

    # Training loop uses cache.get_batch()
    for batch in cache:
        train_step(batch)
"""

import asyncio
import threading
import torch
import queue
import time
from dataclasses import dataclass
from typing import Optional, Any, Iterator
from collections import deque
from concurrent.futures import ThreadPoolExecutor

from datasets import load_dataset
from transformers import AutoTokenizer

from sem_v6.training.master_teacher import create_master_teacher, MasterTeacher


@dataclass
class CachedSample:
    """A single cached sample with teacher logits."""

    input_ids: torch.Tensor  # (seq_len,)
    labels: torch.Tensor  # (seq_len,)
    teacher_logits: torch.Tensor  # (seq_len, vocab_size)


class StreamingTeacherCache:
    """
    Streaming cache with async background fetching.

    The cache uses a ring buffer that grows up to max_samples.
    A background producer continuously fetches new samples.
    The training dataloader consumes from the cache.

    Args:
        tokenizer: HuggingFace tokenizer
        max_samples: Maximum cache size (default: 50000)
        batch_size: Batch size for API calls (default: 4)
        max_length: Maximum sequence length (default: 64)
        sample_positions: Which positions to sample for teacher (default: last 8)
        temperature: Distillation temperature (default: 2.0)
        providers: Which providers to use (default: all except openrouter)
    """

    def __init__(
        self,
        tokenizer,
        max_samples: int = 50000,
        batch_size: int = 4,
        max_length: int = 64,
        sample_positions: Optional[list[int]] = None,
        temperature: float = 2.0,
        providers: Optional[list[str]] = None,
        concurrent_batches: int = 8,
        max_consecutive_errors: int = 5,
    ):
        self.tokenizer = tokenizer
        self.vocab_size = len(tokenizer)
        self.max_samples = max_samples
        self.batch_size = batch_size
        self.max_length = max_length
        self.sample_positions = sample_positions or list(
            range(max_length - 8, max_length)
        )
        self.temperature = temperature
        self.concurrent_batches = concurrent_batches
        self.max_consecutive_errors = max_consecutive_errors

        # Skip OpenRouter (rate limited on free tier)
        self.providers = providers or ["gemini", "cloudflare", "opencode"]

        # Cache storage (thread-safe deque)
        self.cache: deque[CachedSample] = deque(maxlen=max_samples)
        self.cache_lock = threading.Lock()

        # Producer control
        self._stop_event = threading.Event()
        self._producer_thread: Optional[threading.Thread] = None
        self._samples_produced = 0
        self._samples_consumed = 0

        # Data source
        self._data_iter: Optional[Iterator] = None

        # Stats
        self.stats = {
            "produced": 0,
            "consumed": 0,
            "cache_size": 0,
            "producer_errors": 0,
        }
        self._consecutive_errors = 0

    def _create_data_iterator(self) -> Iterator:
        """Create streaming iterator from OpenWebText."""
        dataset = load_dataset("openwebtext", split="train", streaming=True)

        for sample in dataset:
            text = sample["text"]
            if len(text) < 100:
                continue

            tokens = self.tokenizer.encode(
                text, max_length=self.max_length, truncation=True
            )
            if len(tokens) < 50:
                continue

            # Pad or truncate
            if len(tokens) < self.max_length:
                tokens = tokens + [self.tokenizer.eos_token_id or 0] * (
                    self.max_length - len(tokens)
                )
            else:
                tokens = tokens[: self.max_length]

            input_ids = torch.tensor(tokens)
            labels = torch.tensor(tokens[1:] + [self.tokenizer.eos_token_id or 0])

            yield input_ids, labels

    async def _fetch_batch_async(
        self,
        teacher: MasterTeacher,
        batch_inputs: list[torch.Tensor],
        batch_labels: list[torch.Tensor],
    ) -> list[CachedSample]:
        """Fetch teacher logits for a batch asynchronously."""
        x = torch.stack(batch_inputs)

        try:
            teacher_logits = await teacher.get_teacher_logits_for_batch_async(
                x,
                sample_positions=self.sample_positions,
                temperature=self.temperature,
            )

            samples = []
            for i in range(len(batch_inputs)):
                samples.append(
                    CachedSample(
                        input_ids=batch_inputs[i],
                        labels=batch_labels[i],
                        teacher_logits=teacher_logits[i],
                    )
                )
            self._consecutive_errors = 0
            return samples

        except Exception as e:
            self.stats["producer_errors"] += 1
            self._consecutive_errors += 1
            if self._consecutive_errors >= self.max_consecutive_errors:
                raise RuntimeError(
                    f"StreamingTeacherCache failed {self._consecutive_errors} consecutive fetches"
                ) from e
            return []

    async def _producer_loop_async(self, teacher: MasterTeacher):
        """Async producer loop that fetches samples."""
        if self._data_iter is None:
            self._data_iter = self._create_data_iterator()

        semaphore = asyncio.Semaphore(self.concurrent_batches)

        async def process_batch(batch_inputs, batch_labels):
            async with semaphore:
                return await self._fetch_batch_async(
                    teacher, batch_inputs, batch_labels
                )

        while not self._stop_event.is_set():
            # Collect batch
            batch_inputs = []
            batch_labels = []

            try:
                for _ in range(self.batch_size):
                    input_ids, labels = next(self._data_iter)
                    batch_inputs.append(input_ids)
                    batch_labels.append(labels)
            except StopIteration:
                # Restart iterator
                self._data_iter = self._create_data_iterator()
                continue

            # Fetch asynchronously
            samples = await process_batch(batch_inputs, batch_labels)

            # Add to cache
            if samples:
                with self.cache_lock:
                    for sample in samples:
                        self.cache.append(sample)
                    self.stats["produced"] += len(samples)
                    self.stats["cache_size"] = len(self.cache)

            # Brief yield to allow other tasks
            await asyncio.sleep(0.001)

    def _producer_thread_main(self):
        """Thread entry point for producer."""
        teacher = create_master_teacher(
            self.tokenizer,
            providers=self.providers,
        )

        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)

        try:
            loop.run_until_complete(self._producer_loop_async(teacher))
        finally:
            loop.close()

    async def warmup(self, num_samples: int = 10000) -> None:
        """
        Pre-cache initial samples before training starts.

        Args:
            num_samples: Number of samples to pre-cache (default: 10000)
        """
        print(f"[StreamingCache] Warming up with {num_samples} samples...")
        start = time.time()

        teacher = create_master_teacher(
            self.tokenizer,
            providers=self.providers,
        )

        self._data_iter = self._create_data_iterator()

        samples_fetched = 0
        batch_count = 0

        while samples_fetched < num_samples:
            # Collect batch
            batch_inputs = []
            batch_labels = []

            for _ in range(self.batch_size):
                try:
                    input_ids, labels = next(self._data_iter)
                    batch_inputs.append(input_ids)
                    batch_labels.append(labels)
                except StopIteration:
                    break

            if not batch_inputs:
                break

            # Fetch teacher logits
            samples = await self._fetch_batch_async(teacher, batch_inputs, batch_labels)

            # Add to cache
            for sample in samples:
                self.cache.append(sample)
                samples_fetched += 1

            batch_count += 1
            if batch_count % 50 == 0:
                elapsed = time.time() - start
                rate = samples_fetched / elapsed * 60
                print(f"  [{samples_fetched}/{num_samples}] {rate:.1f} samples/min")

        elapsed = time.time() - start
        self.stats["produced"] = len(self.cache)
        self.stats["cache_size"] = len(self.cache)

        print(
            f"[StreamingCache] Warmup complete: {len(self.cache)} samples in {elapsed:.1f}s"
        )
        print(f"  Provider stats: {teacher.stats()}")

    def start_background_fetching(self) -> None:
        """Start the background producer thread."""
        if self._producer_thread is not None:
            return

        self._stop_event.clear()
        self._producer_thread = threading.Thread(
            target=self._producer_thread_main,
            daemon=True,
            name="TeacherCacheProducer",
        )
        self._producer_thread.start()
        print("[StreamingCache] Background producer started")

    def stop_background_fetching(self) -> None:
        """Stop the background producer thread."""
        if self._producer_thread is None:
            return

        self._stop_event.set()
        self._producer_thread.join(timeout=5.0)
        self._producer_thread = None
        print("[StreamingCache] Background producer stopped")

    def get_batch(self, batch_size: int) -> Optional[dict[str, torch.Tensor]]:
        """
        Get a batch of samples from the cache.

        Args:
            batch_size: Number of samples to get

        Returns:
            Dict with input_ids, labels, teacher_logits or None if cache empty
        """
        with self.cache_lock:
            if len(self.cache) < batch_size:
                return None

            # Random sample from cache (not pop, so samples can be reused)
            indices = torch.randperm(len(self.cache))[:batch_size]
            samples = [self.cache[i] for i in indices]

            self.stats["consumed"] += batch_size

        return {
            "input_ids": torch.stack([s.input_ids for s in samples]),
            "labels": torch.stack([s.labels for s in samples]),
            "teacher_logits": torch.stack([s.teacher_logits for s in samples]),
        }

    def __len__(self) -> int:
        """Current cache size."""
        return len(self.cache)

    def __iter__(self):
        """Iterate over cache for DataLoader compatibility."""
        with self.cache_lock:
            for sample in self.cache:
                yield {
                    "input_ids": sample.input_ids,
                    "labels": sample.labels,
                    "teacher_logits": sample.teacher_logits,
                }

    def get_stats(self) -> dict[str, Any]:
        """Get cache statistics."""
        return {
            **self.stats,
            "cache_size": len(self.cache),
            "producer_running": self._producer_thread is not None
            and self._producer_thread.is_alive(),
        }


class StreamingCacheDataset(torch.utils.data.IterableDataset):
    """
    PyTorch IterableDataset wrapper for StreamingTeacherCache.

    This allows the cache to be used with PyTorch DataLoader.

    Args:
        cache: StreamingTeacherCache instance
        batch_size: Batch size for training
        min_cache_size: Minimum samples in cache before yielding (default: 1000)
    """

    def __init__(
        self,
        cache: StreamingTeacherCache,
        batch_size: int = 16,
        min_cache_size: int = 1000,
    ):
        self.cache = cache
        self.batch_size = batch_size
        self.min_cache_size = min_cache_size

    def __iter__(self):
        """Yield batches from cache, waiting if needed."""
        while True:
            # Wait for minimum cache size
            while len(self.cache) < self.min_cache_size:
                time.sleep(0.1)

            # Get random samples from cache
            batch = self.cache.get_batch(self.batch_size)
            if batch is not None:
                yield batch


if __name__ == "__main__":
    # Test the streaming cache
    import asyncio

    print("Testing StreamingTeacherCache...")

    tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B")

    cache = StreamingTeacherCache(
        tokenizer=tokenizer,
        max_samples=1000,
        batch_size=4,
        max_length=64,
        providers=["gemini", "cloudflare", "opencode"],  # Skip rate-limited openrouter
    )

    # Warmup with small amount
    asyncio.run(cache.warmup(num_samples=100))

    print(f"\nCache stats: {cache.get_stats()}")

    # Start background producer
    cache.start_background_fetching()

    # Simulate training loop
    print("\nSimulating training for 10 seconds...")
    start = time.time()
    step = 0

    while time.time() - start < 10:
        batch = cache.get_batch(batch_size=8)
        if batch is not None:
            step += 1
            if step % 10 == 0:
                stats = cache.get_stats()
                print(
                    f"Step {step}: cache_size={stats['cache_size']}, produced={stats['produced']}"
                )
        time.sleep(0.1)

    cache.stop_background_fetching()

    print(f"\nFinal stats: {cache.get_stats()}")
    print("✅ StreamingTeacherCache test complete!")