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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
#!/usr/bin/env python3
"""
LOOP 4 → RSI CEILING BREAKTHROUGH TEST (MEMORY-OPTIMIZED)
==========================================================
The critical experiment: Does tokenization co-evolution break the RSI ceiling?

Key insight: Adaptation training must EXERCISE THE NEW TOKENS.
We generate text dense with merged patterns, re-encode with new tokenizer,
and train the model to predict the merged tokens.

MEMORY OPTIMIZED:
- 4-bit quantization (~5GB model)
- Batch size 1 with gradient accumulation
- Reduced sequence lengths
- Aggressive memory cleanup

Expected VRAM: ~12-14GB
Expected runtime: 45-75 minutes

Pipeline:
1. Load Loop 4 results (merge candidates)
2. Resize embeddings for new tokens
3. TARGETED fine-tuning on text dense with merged patterns
4. Run extended RSI (aim for >5 iterations)
5. Measure if ceiling has moved

Author: Logan Napolitano
Date: January 2026
"""

import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import json
import os
import gc
import re
from pathlib import Path
from dataclasses import dataclass
from typing import List, Dict, Tuple, Optional
from transformers import (
    AutoModelForCausalLM, 
    AutoTokenizer, 
    TrainingArguments,
    Trainer,
    DataCollatorForLanguageModeling,
    BitsAndBytesConfig,
)
from datasets import Dataset
from peft import LoraConfig, get_peft_model, TaskType, prepare_model_for_kbit_training
from tqdm import tqdm
import warnings
warnings.filterwarnings("ignore")


@dataclass
class BreakthroughConfig:
    model_path: str = "."
    output_dir: str = "loop4_breakthrough"
    device: str = "cuda"
    
    # Tokenizer modification
    loop4_results_path: str = "loop4_full_results/loop4_full_results.json"
    top_k_merges: int = 10  # Reduced
    
    # Adaptation fine-tuning (targeted)
    adaptation_samples: int = 150  # Reduced
    adaptation_steps: int = 100  # Reduced
    adaptation_lr: float = 2e-5
    adaptation_batch_size: int = 1  # Reduced
    gradient_accumulation: int = 8  # Increased to compensate
    min_pattern_density: float = 0.08
    
    # RSI settings
    max_rsi_iterations: int = 10
    rsi_micro_steps: int = 15  # Reduced
    rsi_lr: float = 1e-5
    quality_threshold: float = 0.92
    
    # Evaluation
    eval_samples: int = 10  # Reduced


class EmbeddingResizer:
    """Handles resizing model embeddings for new tokens."""
    
    def __init__(self, model, tokenizer):
        self.model = model
        self.tokenizer = tokenizer
        self.original_vocab_size = len(tokenizer)
        self.original_tokenizer = AutoTokenizer.from_pretrained(
            tokenizer.name_or_path,
            local_files_only=True
        )
    
    def add_tokens_and_resize(self, new_tokens: List[str]) -> Tuple[int, List[str]]:
        """
        Add new tokens and resize embeddings.
        Returns: (num_added, list of added tokens)
        """
        existing_vocab = set(self.tokenizer.get_vocab().keys())
        tokens_to_add = [t for t in new_tokens if t not in existing_vocab]
        
        if not tokens_to_add:
            print("All tokens already in vocabulary")
            return 0, []
        
        print(f"Adding {len(tokens_to_add)} new tokens...")
        for t in tokens_to_add:
            print(f"  + '{repr(t)}'")
        
        num_added = self.tokenizer.add_tokens(tokens_to_add)
        print(f"Vocabulary: {self.original_vocab_size}{len(self.tokenizer)}")
        
        self.model.resize_token_embeddings(len(self.tokenizer))
        self._initialize_new_embeddings(tokens_to_add)
        
        return num_added, tokens_to_add
    
    def _initialize_new_embeddings(self, new_tokens: List[str]):
        """Initialize new embeddings as average of component tokens."""
        embed_weight = self.model.get_input_embeddings().weight
        
        for token in new_tokens:
            new_id = self.tokenizer.convert_tokens_to_ids(token)
            
            # Get component IDs from original tokenizer
            component_ids = self.original_tokenizer.encode(token, add_special_tokens=False)
            
            if component_ids:
                with torch.no_grad():
                    component_embeds = embed_weight[component_ids]
                    embed_weight[new_id] = component_embeds.mean(dim=0)
                print(f"  '{token}' initialized from {len(component_ids)} components")


class TargetedDataGenerator:
    """
    Generates training data DENSE with the merged token patterns.
    This is the key insight: train on text that exercises the new vocabulary.
    """
    
    def __init__(self, model, tokenizer, merged_tokens: List[str], config: BreakthroughConfig):
        self.model = model
        self.tokenizer = tokenizer
        self.merged_tokens = merged_tokens
        self.config = config
        
        # Build pattern matchers for each merged token
        self.patterns = []
        for token in merged_tokens:
            # Escape special regex chars
            escaped = re.escape(token)
            self.patterns.append(escaped)
        
        # Combined pattern
        if self.patterns:
            self.combined_pattern = re.compile('|'.join(self.patterns))
        else:
            self.combined_pattern = None
    
    def count_pattern_matches(self, text: str) -> int:
        """Count how many merged token patterns appear in text."""
        if not self.combined_pattern:
            return 0
        return len(self.combined_pattern.findall(text))
    
    def compute_pattern_density(self, text: str) -> float:
        """Compute what fraction of text is covered by merged patterns."""
        if not text:
            return 0.0
        matches = self.count_pattern_matches(text)
        # Rough estimate: each match covers ~5 chars on average
        coverage = (matches * 5) / len(text)
        return min(coverage, 1.0)
    
    def generate_dense_text(self, n_samples: int) -> List[str]:
        """
        Generate text that naturally contains many merged token patterns.
        
        Strategy:
        1. Use prompts that encourage the patterns (sentence starters, connectives)
        2. Filter for high pattern density
        3. Augment with explicit pattern injection if needed
        """
        print("Generating pattern-dense training data...")
        
        # Prompts designed to elicit the patterns (. The, , and, . In, . It, , the, etc.)
        dense_prompts = [
            # These encourage ". The" pattern
            "The experiment showed remarkable results. The data clearly indicated",
            "She opened the door carefully. The room inside was dark. The air felt cold. The silence was complete.",
            "Scientists made a breakthrough. The discovery changes everything. The implications are vast.",
            
            # These encourage ", and" and ", the" patterns
            "The team worked hard, and the results showed improvement, and the project succeeded.",
            "He studied mathematics, physics, and chemistry, and he excelled in all subjects.",
            "We need food, water, and shelter, and the supplies must arrive soon.",
            
            # These encourage ". In" pattern
            "The city grew rapidly. In the downtown area, new buildings appeared. In the suburbs, families settled.",
            "Change happens slowly. In the beginning, few noticed. In time, everyone understood.",
            
            # These encourage ". It" pattern
            "The machine hummed quietly. It processed data continuously. It never stopped working.",
            "The algorithm converged. It found the optimal solution. It exceeded expectations.",
            
            # These encourage ". This" pattern
            "The theory was revolutionary. This changed how scientists thought. This led to new discoveries.",
            "The problem seemed impossible. This made the solution more remarkable. This proved the method worked.",
            
            # Dense combinations
            "The research began in January. The team collected data, and the analysis revealed patterns. In the first phase, the results were promising. It became clear that the hypothesis was correct. This validated the entire approach, and the project moved forward.",
            
            "The algorithm processes input. The output depends on parameters, and the system optimizes continuously. In each iteration, the model improves. It learns from errors. This creates a feedback loop, and the performance increases.",
            
            "The forest stretched endlessly. The trees stood tall, and the leaves rustled softly. In the clearing, sunlight streamed down. It illuminated the path. This was the way forward, and the journey continued.",
        ]
        
        # Additional templates that force patterns
        templates = [
            "The {noun} was {adj}. The {noun2} seemed {adj2}. In the {place}, {event}. It was {description}. This meant {conclusion}, and the {outcome}.",
            "{Statement}. The evidence was clear. In retrospect, {reflection}. It all made sense. This {insight}, and {result}.",
            "The process began. The first step involved {action}. In this phase, {detail}. It required {requirement}. This ensured {guarantee}, and the {final}.",
        ]
        
        nouns = ["system", "approach", "method", "solution", "discovery", "pattern", "structure", "concept"]
        adjs = ["remarkable", "significant", "important", "crucial", "fundamental", "essential"]
        places = ["beginning", "end", "middle", "process", "analysis", "study"]
        
        texts = []
        attempts = 0
        max_attempts = n_samples * 10
        
        # First, use the dense prompts and generate continuations
        for prompt in dense_prompts:
            if len(texts) >= n_samples:
                break
                
            try:
                inputs = self.tokenizer(prompt, return_tensors="pt").to(self.config.device)
                
                with torch.no_grad():
                    outputs = self.model.generate(
                        inputs.input_ids,
                        max_new_tokens=80,  # Reduced
                        temperature=0.8,
                        do_sample=True,
                        pad_token_id=self.tokenizer.eos_token_id,
                        repetition_penalty=1.1,
                    )
                
                text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
                density = self.compute_pattern_density(text)
                
                if density >= self.config.min_pattern_density:
                    texts.append(text)
                    
            except Exception as e:
                continue
        
        # Memory cleanup
        torch.cuda.empty_cache()
        gc.collect()
        
        # Generate more with varied prompts
        while len(texts) < n_samples and attempts < max_attempts:
            attempts += 1
            
            # Random dense prompt
            base = np.random.choice(dense_prompts)
            
            try:
                inputs = self.tokenizer(base, return_tensors="pt").to(self.config.device)
                
                with torch.no_grad():
                    outputs = self.model.generate(
                        inputs.input_ids,
                        max_new_tokens=60,  # Reduced
                        temperature=0.9,
                        do_sample=True,
                        top_p=0.95,
                        pad_token_id=self.tokenizer.eos_token_id,
                        repetition_penalty=1.1,
                    )
                
                text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
                density = self.compute_pattern_density(text)
                
                if density >= self.config.min_pattern_density * 0.8:  # Slightly relaxed
                    texts.append(text)
                    
            except:
                continue
            
            if attempts % 50 == 0:
                print(f"  Generated {len(texts)}/{n_samples} samples ({attempts} attempts)")
        
        # If we still need more, inject patterns into generated text
        if len(texts) < n_samples:
            print(f"  Augmenting with pattern injection...")
            texts.extend(self._inject_patterns(n_samples - len(texts)))
        
        return texts[:n_samples]
    
    def _inject_patterns(self, n_samples: int) -> List[str]:
        """Inject merged patterns into template text."""
        templates = [
            "The {0} was significant. The {1} followed naturally. In the {2}, everything changed. It was clear that {3}. This meant {4}, and the outcome was {5}.",
            "Research showed {0}. The findings were {1}. In particular, the {2} demonstrated {3}. It proved that {4}. This {5}, and {6}.",
            "The system processed {0}. The algorithm computed {1}, and the results showed {2}. In each iteration, the {3} improved. It optimized {4}. This created {5}, and the {6}.",
        ]
        
        fillers = [
            "the data", "the results", "the analysis", "the model", "the approach",
            "important", "significant", "remarkable", "essential", "fundamental",
            "process", "method", "system", "framework", "structure",
            "the hypothesis held", "the theory worked", "the method succeeded",
            "improvement", "progress", "advancement", "efficiency", "performance",
            "a breakthrough", "new understanding", "better outcomes", "clear benefits",
            "conclusion validated the approach", "study confirmed expectations",
        ]
        
        texts = []
        for _ in range(n_samples):
            template = np.random.choice(templates)
            fills = np.random.choice(fillers, size=10, replace=True)
            try:
                text = template.format(*fills)
                texts.append(text)
            except:
                continue
        
        return texts
    
    def create_dataset(self, n_samples: int) -> Dataset:
        """Create dataset with pattern-dense text."""
        texts = self.generate_dense_text(n_samples)
        
        # Report statistics
        total_patterns = sum(self.count_pattern_matches(t) for t in texts)
        avg_density = np.mean([self.compute_pattern_density(t) for t in texts])
        
        print(f"\nDataset statistics:")
        print(f"  Samples: {len(texts)}")
        print(f"  Total pattern matches: {total_patterns}")
        print(f"  Avg pattern density: {avg_density:.2%}")
        print(f"  Patterns per sample: {total_patterns/len(texts):.1f}")
        
        return Dataset.from_dict({"text": texts})


class TargetedAdaptationTrainer:
    """
    Fine-tuning that specifically teaches the model to USE the new tokens.
    """
    
    def __init__(self, model, tokenizer, merged_tokens: List[str], config: BreakthroughConfig):
        self.model = model
        self.tokenizer = tokenizer
        self.merged_tokens = merged_tokens
        self.config = config
    
    def train(self) -> nn.Module:
        print("\n" + "="*60)
        print("TARGETED ADAPTATION TRAINING")
        print("="*60)
        print(f"Teaching model to use {len(self.merged_tokens)} new tokens")
        print(f"Merged tokens: {self.merged_tokens[:5]}...")
        
        # Generate pattern-dense data
        generator = TargetedDataGenerator(
            self.model, self.tokenizer, self.merged_tokens, self.config
        )
        dataset = generator.create_dataset(self.config.adaptation_samples)
        
        # Tokenize - this is where merged tokens get used
        def tokenize_fn(examples):
            tokenized = self.tokenizer(
                examples["text"],
                truncation=True,
                max_length=128,  # Reduced
                padding="max_length",
            )
            return tokenized
        
        tokenized = dataset.map(tokenize_fn, batched=True, remove_columns=["text"])
        
        # Verify merged tokens appear in tokenized data
        sample_ids = tokenized[0]["input_ids"]
        new_token_ids = set(
            self.tokenizer.convert_tokens_to_ids(t) for t in self.merged_tokens
        )
        found = sum(1 for id in sample_ids if id in new_token_ids)
        print(f"  New tokens in sample: {found}")
        
        # LoRA setup
        lora_config = LoraConfig(
            task_type=TaskType.CAUSAL_LM,
            r=16,  # Reduced for memory
            lora_alpha=32,
            lora_dropout=0.05,
            target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
        )
        
        # Prepare for quantized training
        self.model = prepare_model_for_kbit_training(self.model)
        peft_model = get_peft_model(self.model, lora_config)
        peft_model.print_trainable_parameters()
        
        # Training
        training_args = TrainingArguments(
            output_dir=f"{self.config.output_dir}/adaptation",
            max_steps=self.config.adaptation_steps,
            per_device_train_batch_size=self.config.adaptation_batch_size,
            gradient_accumulation_steps=self.config.gradient_accumulation,
            learning_rate=self.config.adaptation_lr,
            warmup_steps=15,
            logging_steps=10,
            save_strategy="no",
            fp16=True,
            report_to="none",
            dataloader_pin_memory=False,
        )
        
        data_collator = DataCollatorForLanguageModeling(
            tokenizer=self.tokenizer,
            mlm=False,
        )
        
        trainer = Trainer(
            model=peft_model,
            args=training_args,
            train_dataset=tokenized,
            data_collator=data_collator,
        )
        
        print("\nTraining on pattern-dense data...")
        trainer.train()
        
        # Merge weights
        print("Merging LoRA weights...")
        merged_model = peft_model.merge_and_unload()
        
        # Quick verification
        print("\nVerifying adaptation...")
        self._verify_adaptation(merged_model)
        
        return merged_model
    
    def _verify_adaptation(self, model):
        """Verify the model uses new tokens correctly."""
        test_prompt = "The research showed results. The"
        
        inputs = self.tokenizer(test_prompt, return_tensors="pt").to(self.config.device)
        
        with torch.no_grad():
            outputs = model.generate(
                inputs.input_ids,
                max_new_tokens=30,
                temperature=0.7,
                do_sample=True,
                pad_token_id=self.tokenizer.eos_token_id,
            )
        
        response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
        print(f"  Test generation: '{response[:100]}...'")


class ExtendedRSI:
    """Extended RSI to test ceiling breakthrough."""
    
    def __init__(self, model, tokenizer, config: BreakthroughConfig):
        self.model = model
        self.tokenizer = tokenizer
        self.config = config
        self.iteration_history = []
        self.baseline_quality = None
    
    def evaluate_quality(self, n_samples: int = None) -> Dict:
        if n_samples is None:
            n_samples = self.config.eval_samples
        
        prompts = [
            "Explain the concept of recursion in programming:",
            "What are the key principles of effective communication?",
            "Describe the process of photosynthesis:",
            "How do neural networks learn from data?",
            "What is the scientific method and why is it important?",
            "Explain the relationship between supply and demand:",
            "What are the main challenges in renewable energy?",
            "How does memory work in the human brain?",
            "Describe the structure of an atom:",
            "What factors influence climate patterns?",
        ]
        
        metrics = {
            "coherence": [],
            "completeness": [],
            "repetition_rate": [],
            "avg_length": [],
        }
        
        for prompt in prompts[:n_samples]:
            try:
                inputs = self.tokenizer(prompt, return_tensors="pt").to(self.config.device)
                
                with torch.no_grad():
                    outputs = self.model.generate(
                        inputs.input_ids,
                        max_new_tokens=100,
                        temperature=0.7,
                        do_sample=True,
                        pad_token_id=self.tokenizer.eos_token_id,
                        repetition_penalty=1.1,
                    )
                
                response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
                response = response[len(prompt):].strip()
                
                tokens = response.split()
                
                # Repetition
                if len(tokens) > 0:
                    unique_ratio = len(set(tokens)) / len(tokens)
                    metrics["repetition_rate"].append(1 - unique_ratio)
                else:
                    metrics["repetition_rate"].append(1.0)
                
                metrics["avg_length"].append(len(tokens))
                metrics["coherence"].append(1.0 if len(tokens) > 10 else 0.5)
                metrics["completeness"].append(1.0 if response and response[-1] in '.!?' else 0.7)
                
            except:
                continue
        
        result = {
            "coherence": np.mean(metrics["coherence"]) if metrics["coherence"] else 0,
            "completeness": np.mean(metrics["completeness"]) if metrics["completeness"] else 0,
            "repetition_rate": np.mean(metrics["repetition_rate"]) if metrics["repetition_rate"] else 1,
            "avg_length": np.mean(metrics["avg_length"]) if metrics["avg_length"] else 0,
        }
        
        result["quality_score"] = (
            result["coherence"] * 0.3 +
            result["completeness"] * 0.3 +
            (1 - result["repetition_rate"]) * 0.4
        )
        
        return result
    
    def generate_rsi_data(self, n_samples: int = 30) -> Dataset:  # Reduced default
        prompts = [
            "Write a clear explanation of",
            "Describe in detail how",
            "Explain the relationship between", 
            "What are the key aspects of",
            "Provide an analysis of",
        ]
        
        topics = [
            "machine learning algorithms", "climate systems", "economic markets",
            "cognitive processes", "technological change", "scientific methods",
            "social structures", "mathematical proofs", "biological evolution",
            "energy systems",
        ]
        
        texts = []
        
        for prompt in prompts:
            for topic in topics:
                full_prompt = f"{prompt} {topic}:"
                
                try:
                    inputs = self.tokenizer(full_prompt, return_tensors="pt").to(self.config.device)
                    
                    with torch.no_grad():
                        outputs = self.model.generate(
                            inputs.input_ids,
                            max_new_tokens=60,  # Reduced
                            temperature=0.7,
                            do_sample=True,
                            pad_token_id=self.tokenizer.eos_token_id,
                            repetition_penalty=1.1,
                        )
                    
                    text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
                    texts.append(text)
                    
                except:
                    continue
                
                if len(texts) >= n_samples:
                    break
            if len(texts) >= n_samples:
                break
        
        return Dataset.from_dict({"text": texts})
    
    def micro_train(self, dataset: Dataset):
        def tokenize_fn(examples):
            return self.tokenizer(
                examples["text"],
                truncation=True,
                max_length=128,  # Reduced
                padding="max_length",
            )
        
        tokenized = dataset.map(tokenize_fn, batched=True, remove_columns=["text"])
        
        lora_config = LoraConfig(
            task_type=TaskType.CAUSAL_LM,
            r=8,
            lora_alpha=16,
            lora_dropout=0.05,
            target_modules=["q_proj", "v_proj"],
        )
        
        # Prepare model for quantized training
        model_for_training = prepare_model_for_kbit_training(self.model)
        peft_model = get_peft_model(model_for_training, lora_config)
        
        training_args = TrainingArguments(
            output_dir=f"{self.config.output_dir}/rsi_temp",
            max_steps=self.config.rsi_micro_steps,
            per_device_train_batch_size=1,  # Reduced
            gradient_accumulation_steps=4,  # Increased
            learning_rate=self.config.rsi_lr,
            warmup_steps=2,
            logging_steps=100,
            fp16=True,
            report_to="none",
            save_strategy="no",
            dataloader_pin_memory=False,
        )
        
        data_collator = DataCollatorForLanguageModeling(
            tokenizer=self.tokenizer,
            mlm=False,
        )
        
        trainer = Trainer(
            model=peft_model,
            args=training_args,
            train_dataset=tokenized,
            data_collator=data_collator,
        )
        
        trainer.train()
        self.model = peft_model.merge_and_unload()
    
    def run(self) -> Dict:
        print("\n" + "="*60)
        print("EXTENDED RSI - CEILING BREAKTHROUGH TEST")
        print("="*60)
        print(f"Previous ceiling: 3-5 iterations")
        print(f"Target: >5 successful iterations")
        print(f"Max attempts: {self.config.max_rsi_iterations}")
        
        # Baseline
        print("\nEstablishing baseline...")
        self.baseline_quality = self.evaluate_quality()
        print(f"Baseline: {self.baseline_quality['quality_score']:.4f}")
        
        successful = 0
        consecutive_failures = 0
        
        # Store original model reference (quantized base - don't modify)
        # RSI works by: train LoRA -> evaluate -> if good, keep merged; if bad, skip merge
        
        for iteration in range(1, self.config.max_rsi_iterations + 1):
            print(f"\n--- RSI Iteration {iteration} ---")
            
            # Self-generate data BEFORE any training
            print("  Generating data...")
            rsi_data = self.generate_rsi_data(25)
            
            # Setup fresh LoRA for this iteration
            def tokenize_fn(examples):
                return self.tokenizer(
                    examples["text"],
                    truncation=True,
                    max_length=128,
                    padding="max_length",
                )
            
            tokenized = rsi_data.map(tokenize_fn, batched=True, remove_columns=["text"])
            
            lora_config = LoraConfig(
                task_type=TaskType.CAUSAL_LM,
                r=8,
                lora_alpha=16,
                lora_dropout=0.05,
                target_modules=["q_proj", "v_proj"],
            )
            
            # Apply LoRA (don't prepare_for_kbit_training again if already done)
            try:
                peft_model = get_peft_model(self.model, lora_config)
            except:
                # Model might already be a PEFT model from previous iteration
                self.model = self.model.merge_and_unload() if hasattr(self.model, 'merge_and_unload') else self.model
                peft_model = get_peft_model(self.model, lora_config)
            
            training_args = TrainingArguments(
                output_dir=f"{self.config.output_dir}/rsi_temp",
                max_steps=self.config.rsi_micro_steps,
                per_device_train_batch_size=1,
                gradient_accumulation_steps=4,
                learning_rate=self.config.rsi_lr,
                warmup_steps=2,
                logging_steps=100,
                fp16=True,
                report_to="none",
                save_strategy="no",
                dataloader_pin_memory=False,
            )
            
            data_collator = DataCollatorForLanguageModeling(
                tokenizer=self.tokenizer,
                mlm=False,
            )
            
            print("  Training...")
            trainer = Trainer(
                model=peft_model,
                args=training_args,
                train_dataset=tokenized,
                data_collator=data_collator,
            )
            trainer.train()
            
            # Merge to evaluate
            merged_model = peft_model.merge_and_unload()
            
            # Evaluate
            print("  Evaluating...")
            old_model = self.model
            self.model = merged_model
            quality = self.evaluate_quality()
            relative = quality["quality_score"] / self.baseline_quality["quality_score"]
            
            print(f"  Quality: {quality['quality_score']:.4f} ({relative:.1%} of baseline)")
            
            if relative >= self.config.quality_threshold:
                successful += 1
                consecutive_failures = 0
                
                self.iteration_history.append({
                    "iteration": iteration,
                    "status": "success",
                    "quality": quality["quality_score"],
                    "relative": relative,
                })
                
                print(f"  ✅ SUCCESS (total: {successful})")
                
                # Keep merged model
                if quality["quality_score"] > self.baseline_quality["quality_score"]:
                    self.baseline_quality = quality
                    
            else:
                consecutive_failures += 1
                
                self.iteration_history.append({
                    "iteration": iteration,
                    "status": "rollback",
                    "quality": quality["quality_score"],
                    "relative": relative,
                })
                
                print(f"  ⚠️ ROLLBACK (discarding LoRA)")
                # Don't keep the merged model, go back to old
                self.model = old_model
                del merged_model
                
                if consecutive_failures >= 3:
                    print(f"\n🛑 CEILING HIT at iteration {iteration}")
                    break
            
            torch.cuda.empty_cache()
            gc.collect()
        
        return {
            "successful_iterations": successful,
            "total_attempts": len(self.iteration_history),
            "ceiling_broken": successful > 5,
            "history": self.iteration_history,
            "final_quality": self.evaluate_quality(),
        }


class BreakthroughExperiment:
    """Complete Loop 4 → RSI breakthrough experiment."""
    
    def __init__(self, config: BreakthroughConfig):
        self.config = config
        self.results = {}
    
    def run(self):
        print("="*70)
        print("LOOP 4 → RSI CEILING BREAKTHROUGH TEST")
        print("="*70)
        print("\nIf RSI goes past 5 iterations, the ceiling is broken.\n")
        
        # Load Loop 4 results
        print("Step 1: Loading Loop 4 results...")
        loop4_path = Path(self.config.loop4_results_path)
        
        if not loop4_path.exists():
            print(f"ERROR: Not found: {loop4_path}")
            return None
        
        with open(loop4_path) as f:
            loop4_results = json.load(f)
        
        # Extract merges
        merges = []
        if "all_improvements" in loop4_results:
            for imp in loop4_results["all_improvements"][:self.config.top_k_merges]:
                merges.append(imp["merged"])
        elif "top_stressed_pairs" in loop4_results:
            for pair in loop4_results["top_stressed_pairs"][:self.config.top_k_merges]:
                # Parse "'. ' + 'The'" format
                tokens = pair["tokens"].replace("'", "").split(" + ")
                if len(tokens) == 2:
                    merges.append(tokens[0] + tokens[1])
        
        print(f"Merge candidates: {merges}")
        
        # Load model with 4-bit quantization
        print("\nStep 2: Loading model (4-bit quantized)...")
        tokenizer = AutoTokenizer.from_pretrained(
            self.config.model_path,
            local_files_only=True
        )
        tokenizer.pad_token = tokenizer.eos_token
        
        bnb_config = BitsAndBytesConfig(
            load_in_4bit=True,
            bnb_4bit_quant_type="nf4",
            bnb_4bit_compute_dtype=torch.bfloat16,
            bnb_4bit_use_double_quant=True,
        )
        
        model = AutoModelForCausalLM.from_pretrained(
            self.config.model_path,
            quantization_config=bnb_config,
            device_map="auto",
            torch_dtype=torch.bfloat16,
            local_files_only=True,
        )
        print(f"Loaded: {model.config.hidden_size}d, {model.config.num_hidden_layers}L")
        print(f"VRAM: {torch.cuda.memory_allocated()/1024**3:.1f}GB")
        
        # Resize embeddings
        print("\nStep 3: Resizing embeddings...")
        resizer = EmbeddingResizer(model, tokenizer)
        n_added, added_tokens = resizer.add_tokens_and_resize(merges)
        self.results["tokens_added"] = n_added
        
        # Targeted adaptation
        print("\nStep 4: Targeted adaptation training...")
        adapter = TargetedAdaptationTrainer(model, tokenizer, added_tokens, self.config)
        model = adapter.train()
        
        # Extended RSI
        print("\nStep 5: Extended RSI...")
        rsi = ExtendedRSI(model, tokenizer, self.config)
        rsi_results = rsi.run()
        self.results["rsi"] = rsi_results
        
        # Report
        print("\n" + "="*70)
        print("RESULTS")
        print("="*70)
        
        print(f"\nTokens added: {n_added}")
        print(f"RSI successful: {rsi_results['successful_iterations']}")
        print(f"RSI total: {rsi_results['total_attempts']}")
        
        if rsi_results["ceiling_broken"]:
            print("\n" + "🎯"*25)
            print("       THE CEILING IS BROKEN")
            print("🎯"*25)
            print(f"\nRSI: {rsi_results['successful_iterations']} iterations (>5)")
            print("Loop 4 tokenization co-evolution WORKS")
            print("The ladder extends.")
        else:
            print(f"\n⚠️ Ceiling at {rsi_results['successful_iterations']} iterations")
            if rsi_results['successful_iterations'] >= 5:
                print("   Matched previous ceiling - more refinement needed")
            else:
                print("   Below previous ceiling - investigate")
        
        # Save
        output_dir = Path(self.config.output_dir)
        output_dir.mkdir(exist_ok=True)
        
        save_data = {
            "tokens_added": n_added,
            "merges": merges,
            "rsi_successful": rsi_results["successful_iterations"],
            "ceiling_broken": rsi_results["ceiling_broken"],
            "history": rsi_results["history"],
        }
        
        with open(output_dir / "breakthrough_results.json", "w") as f:
            json.dump(save_data, f, indent=2)
        
        print(f"\nSaved to {output_dir}/breakthrough_results.json")
        
        return self.results


def main():
    config = BreakthroughConfig(
        model_path=".",
        loop4_results_path="loop4_full_results/loop4_full_results.json",
        top_k_merges=15,
        adaptation_samples=300,
        adaptation_steps=150,
        adaptation_lr=2e-5,
        max_rsi_iterations=10,
        rsi_micro_steps=20,
        quality_threshold=0.92,
    )
    
    experiment = BreakthroughExperiment(config)
    return experiment.run()


if __name__ == "__main__":
    main()