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
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
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
#!/usr/bin/env python3
"""
BitTransformerLM Full Bi-Directional Attention Training Script
===============================================================

This script implements the breakthrough Fixed RL Adafactor training configuration
for production-scale BitTransformerLM training with FULL BI-DIRECTIONAL UNCHUNKED ATTENTION.

Configuration:
- Model: 16M parameters (d_model=512, nhead=16, num_layers=8)  
- Attention: FULL BI-DIRECTIONAL UNCHUNKED (chunk_size=None)
- Optimizer: Fixed LR Adafactor (identical to breakthrough config)
- Features: Reversible layers, ACT, QAT, compression
- Data: HuggingFace WCNegentropy/BitTransformerLM dataset
- Checkpointing: After every training cycle for continuous training
"""

import sys
import os
import json
import time
import logging
from datetime import datetime
from pathlib import Path
from typing import Optional, Dict, Any

import torch
import torch.nn.functional as F
from datasets import load_dataset
from huggingface_hub import login

# Add paths for imports
sys.path.append('/data')
sys.path.append('/data/BitTransformerLM')

from bit_transformer import (
    BitTransformerLM,
    text_to_bits,
    bits_to_text,
    save_model,
    load_model,
    set_dropout
)
from BTLM_Extensions import configure_adafactor_optimizer

# Setup logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('full_attention_training.log'),
        logging.StreamHandler()
    ]
)
logger = logging.getLogger(__name__)

class ProductionTrainer:
    """Production-grade BitTransformerLM trainer with breakthrough configuration."""
    
    def __init__(self, config: Dict[str, Any]):
        self.config = config
        self.device = torch.device('cpu')  # CPU training as per breakthrough
        self.model = None
        self.optimizer = None
        self.scheduler = None
        self.dataset = None
        self.checkpoint_dir = Path(config['checkpoint_dir'])
        self.checkpoint_dir.mkdir(parents=True, exist_ok=True)
        
        # Training state
        self.current_epoch = 0
        self.total_steps = 0
        self.best_loss = float('inf')
        self.training_history = []
        
    def setup_model(self):
        """Create the breakthrough 16M parameter BitTransformerLM model with full bi-directional attention."""
        logger.info("Setting up breakthrough BitTransformerLM with FULL BI-DIRECTIONAL UNCHUNKED ATTENTION...")
        
        self.model = BitTransformerLM(
            d_model=512,                    # Breakthrough config
            nhead=16,                       # 16 attention heads
            num_layers=8,                   # 8 layers for ~16M params
            dim_feedforward=1024,           # 2x d_model for optimal params
            max_seq_len=512,               # Reasonable sequence length
            reversible=True,                # Memory efficiency
            use_checkpoint=True,            # Gradient checkpointing
            use_autocast=True,             # CPU mixed precision
            use_act=True,                  # Adaptive Computation Time
            act_threshold=0.9,             # ACT threshold
            lambda_K=0.05,                 # Safety telemetry weights
            lambda_C=0.05,
            lambda_S=0.05,
            chunk_size=None,               # FULL ATTENTION - no chunking
            overlap=0,                     # No overlap needed for full attention
            full_attn_logging=True         # Enable full attention logging
        ).to(self.device)
        
        # Calculate actual parameter count
        total_params = sum(p.numel() for p in self.model.parameters())
        trainable_params = sum(p.numel() for p in self.model.parameters() if p.requires_grad)
        
        logger.info(f"Model created: {total_params:,} total parameters ({trainable_params:,} trainable)")
        logger.info(f"Target: ~16M parameters - {'✓' if 15_000_000 <= total_params <= 17_000_000 else '✗'}")
        
        return self.model
    
    def setup_optimizer(self):
        """Setup Fixed RL Adafactor optimizer (the breakthrough secret sauce)."""
        logger.info("Setting up Fixed RL Adafactor optimizer...")
        
        # CRITICAL: Use fixed LR, not auto-LR (lr=None)
        self.optimizer, self.scheduler = configure_adafactor_optimizer(
            self.model,
            lr=self.config['learning_rate'],  # Fixed LR - the key to breakthrough!
            weight_decay=self.config['weight_decay'],
            total_steps=self.config['total_steps']
        )
        
        logger.info(f"Fixed RL Adafactor configured with LR={self.config['learning_rate']}")
        return self.optimizer, self.scheduler
    
    def setup_dataset(self):
        """Load and prepare the WCNegentropy/BitTransformerLM dataset."""
        logger.info("Loading WCNegentropy/BitTransformerLM dataset...")
        
        # Login to HuggingFace
        login(token=self.config['hf_token'])
        
        # Load dataset
        try:
            dataset = load_dataset("WCNegentropy/BitTransformerLM")
            logger.info(f"Dataset loaded: {dataset}")
            
            # Use train split
            train_data = dataset['train'] if 'train' in dataset else dataset
            logger.info(f"Training samples: {len(train_data)}")
            
            # Process dataset - convert to bits using the ACTUAL text_to_bits function
            bit_sequences = []
            for i, sample in enumerate(train_data):
                if i % 1000 == 0:
                    logger.info(f"Processing sample {i}/{len(train_data)}")
                
                # Try to get text from various fields
                text = None
                if 'original_text' in sample and sample['original_text']:
                    text = sample['original_text']
                elif 'text' in sample and sample['text']:
                    text = sample['text']
                
                if text and text.strip():
                    # Use ACTUAL text_to_bits function
                    bits = text_to_bits(text)
                    if len(bits) >= self.config['sequence_length']:
                        bit_sequences.append(bits)
            
            logger.info(f"Processed {len(bit_sequences)} valid bit sequences")
            
            # Create training sequences with proper length
            seq_len = self.config['sequence_length']
            training_sequences = []
            
            for bits in bit_sequences:
                # Create overlapping chunks
                for i in range(0, len(bits) - seq_len + 1, seq_len // 2):
                    chunk = bits[i:i + seq_len]
                    if len(chunk) == seq_len:
                        training_sequences.append(chunk)
            
            # Convert to tensor with proper dtype
            self.dataset = torch.tensor(training_sequences, dtype=torch.long)
            logger.info(f"Created training dataset: {self.dataset.shape}")
            
        except Exception as e:
            logger.error(f"Failed to load dataset: {e}")
            # Fallback to synthetic data for testing
            logger.info("Falling back to synthetic bit data...")
            synthetic_bits = torch.randint(0, 2, (1000, self.config['sequence_length']))
            self.dataset = synthetic_bits
            logger.warning("Using synthetic data - replace with real dataset for production")
        
        return self.dataset
    
    def save_checkpoint(self, epoch: int, loss: float, is_best: bool = False):
        """Save model checkpoint with all training state."""
        checkpoint_data = {
            'epoch': epoch,
            'total_steps': self.total_steps,
            'model_state_dict': self.model.state_dict(),
            'optimizer_state_dict': self.optimizer.state_dict(),
            'scheduler_state_dict': self.scheduler.state_dict() if self.scheduler else None,
            'loss': loss,
            'best_loss': self.best_loss,
            'config': self.config,
            'training_history': self.training_history,
            'timestamp': datetime.now().isoformat()
        }
        
        # Save latest checkpoint
        latest_path = self.checkpoint_dir / 'checkpoint_latest.pt'
        torch.save(checkpoint_data, latest_path)
        logger.info(f"Saved checkpoint: {latest_path}")
        
        # Save epoch-specific checkpoint
        epoch_path = self.checkpoint_dir / f'checkpoint_epoch_{epoch:04d}.pt'
        torch.save(checkpoint_data, epoch_path)
        
        # Save best model if this is the best loss
        if is_best:
            best_path = self.checkpoint_dir / 'checkpoint_best.pt'
            torch.save(checkpoint_data, best_path)
            logger.info(f"NEW BEST MODEL! Loss: {loss:.6f} -> {best_path}")
        
        # Save training config for reference
        config_path = self.checkpoint_dir / 'training_config.json'
        with open(config_path, 'w') as f:
            json.dump(self.config, f, indent=2)
    
    def load_checkpoint(self, checkpoint_path: Optional[str] = None) -> bool:
        """Load model weights from latest checkpoint but restart training from epoch 1."""
        if checkpoint_path is None:
            checkpoint_path = self.checkpoint_dir / 'checkpoint_latest.pt'
        
        checkpoint_path = Path(checkpoint_path)
        if not checkpoint_path.exists():
            logger.info("No checkpoint found - starting fresh training")
            return False
        
        logger.info(f"Loading model weights from: {checkpoint_path}")
        try:
            checkpoint = torch.load(checkpoint_path, map_location=self.device)
            
            # Load ONLY model weights
            self.model.load_state_dict(checkpoint['model_state_dict'])
            
            # RESET all training state to start from epoch 1
            self.current_epoch = 1
            self.total_steps = 0
            self.best_loss = float('inf')
            self.training_history = []
            
            # DO NOT load optimizer/scheduler state - fresh start
            
            logger.info(f"Loaded model weights, restarting training from epoch 1, step 0")
            return True
            
        except Exception as e:
            logger.error(f"Failed to load checkpoint: {e}")
            return False
    
    def training_step(self, batch: torch.Tensor) -> Dict[str, float]:
        """Single training step with telemetry."""
        self.model.train()
        set_dropout(self.model, self.config['dropout'])
        
        batch = batch.to(self.device)
        
        # Zero gradients
        self.optimizer.zero_grad()
        
        # Forward pass with telemetry
        with torch.autocast(device_type='cpu', dtype=torch.bfloat16):
            logits, telemetry = self.model(batch)
            
            # Compute loss (next bit prediction)
            if logits.dim() == 3:  # (batch, seq, 2)
                targets = batch[:, 1:]  # Next bit prediction
                logits = logits[:, :-1]  # Remove last prediction
                loss = F.cross_entropy(logits.reshape(-1, 2), targets.reshape(-1))
            else:
                loss = F.cross_entropy(logits, batch)
            
            # Add telemetry regularization (safety metrics)
            if self.model.lambda_K > 0 and 'negentropy_logits' in telemetry:
                k_term = self.model.lambda_K * (1 - telemetry['negentropy_logits'])
                if k_term.dim() == 0:  # scalar
                    loss = loss + k_term
                else:
                    loss = loss + k_term.mean()
            if self.model.lambda_C > 0 and 'lz_complexity_logits' in telemetry:
                c_term = self.model.lambda_C * (1 - telemetry['lz_complexity_logits'])
                if c_term.dim() == 0:  # scalar
                    loss = loss + c_term
                else:
                    loss = loss + c_term.mean()
            if self.model.lambda_S > 0 and 'symbiosis_score' in telemetry:
                s_term = self.model.lambda_S * (1 - telemetry['symbiosis_score'])
                if s_term.dim() == 0:  # scalar
                    loss = loss + s_term
                else:
                    loss = loss + s_term.mean()
        
        # Backward pass
        loss.backward()
        
        # Gradient clipping
        torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.config['max_grad_norm'])
        
        # Optimizer step
        self.optimizer.step()
        if self.scheduler:
            self.scheduler.step()
        
        self.total_steps += 1
        
        return {
            'loss': loss.item(),
            'K': telemetry.get('negentropy_logits', torch.tensor(0.0)).mean().item() if torch.is_tensor(telemetry.get('negentropy_logits', 0.0)) else telemetry.get('negentropy_logits', 0.0),
            'C': telemetry.get('lz_complexity_logits', torch.tensor(0.0)).mean().item() if torch.is_tensor(telemetry.get('lz_complexity_logits', 0.0)) else telemetry.get('lz_complexity_logits', 0.0),
            'S': telemetry.get('symbiosis_score', torch.tensor(0.0)).mean().item() if torch.is_tensor(telemetry.get('symbiosis_score', 0.0)) else telemetry.get('symbiosis_score', 0.0),
            'lr': self.optimizer.param_groups[0]['lr']
        }
    
    def train_epoch(self) -> Dict[str, float]:
        """Train for one epoch."""
        logger.info(f"Starting epoch {self.current_epoch + 1}")
        
        # Create data loader
        from torch.utils.data import DataLoader
        dataloader = DataLoader(
            self.dataset, 
            batch_size=self.config['batch_size'], 
            shuffle=True,
            drop_last=True
        )
        
        epoch_losses = []
        epoch_metrics = {'K': [], 'C': [], 'S': []}
        
        start_time = time.time()
        
        for step, batch in enumerate(dataloader):
            metrics = self.training_step(batch)
            
            epoch_losses.append(metrics['loss'])
            epoch_metrics['K'].append(metrics['K'])
            epoch_metrics['C'].append(metrics['C'])
            epoch_metrics['S'].append(metrics['S'])
            
            # Log progress
            if step % self.config['log_interval'] == 0:
                logger.info(
                    f"Epoch {self.current_epoch + 1}, Step {step}/{len(dataloader)}: "
                    f"Loss={metrics['loss']:.6f}, K={metrics['K']:.3f}, "
                    f"C={metrics['C']:.3f}, S={metrics['S']:.3f}, LR={metrics['lr']:.2e}"
                )
        
        # Calculate epoch metrics
        epoch_time = time.time() - start_time
        avg_loss = sum(epoch_losses) / len(epoch_losses)
        avg_metrics = {k: sum(v) / len(v) for k, v in epoch_metrics.items()}
        
        epoch_summary = {
            'epoch': self.current_epoch + 1,
            'avg_loss': avg_loss,
            'time': epoch_time,
            **avg_metrics
        }
        
        self.training_history.append(epoch_summary)
        
        logger.info(
            f"Epoch {self.current_epoch + 1} completed in {epoch_time:.1f}s: "
            f"Avg Loss={avg_loss:.6f}, K={avg_metrics['K']:.3f}, "
            f"C={avg_metrics['C']:.3f}, S={avg_metrics['S']:.3f}"
        )
        
        return epoch_summary
    
    def train(self, num_epochs: int):
        """Main training loop."""
        logger.info(f"Starting production training for {num_epochs} epochs...")
        logger.info(f"Breakthrough configuration: Fixed RL Adafactor + 16M BitTransformerLM")
        
        for epoch in range(num_epochs):
            try:
                # Train epoch
                epoch_metrics = self.train_epoch()
                avg_loss = epoch_metrics['avg_loss']
                
                # Check if this is the best model
                is_best = avg_loss < self.best_loss
                if is_best:
                    self.best_loss = avg_loss
                
                # Save checkpoint after each epoch
                self.save_checkpoint(self.current_epoch + 1, avg_loss, is_best)
                
                self.current_epoch += 1
                
                # Log progress
                logger.info(f"=== EPOCH {self.current_epoch} COMPLETE ===")
                logger.info(f"Loss: {avg_loss:.6f} (best: {self.best_loss:.6f})")
                
                # Check for breakthrough performance (loss < 3.0)
                if avg_loss < 3.0:
                    logger.info("🚀 BREAKTHROUGH PERFORMANCE ACHIEVED! Loss < 3.0!")
                
            except KeyboardInterrupt:
                logger.info("Training interrupted by user")
                break
            except Exception as e:
                logger.error(f"Error in epoch {self.current_epoch + 1}: {e}")
                # Save emergency checkpoint
                try:
                    self.save_checkpoint(self.current_epoch, float('inf'), False)
                except:
                    pass
                raise


def main():
    """Main function to run production training."""
    
    # Production training configuration
    config = {
        # Model parameters (breakthrough configuration)
        'model_params': {
            'd_model': 512,
            'nhead': 16,
            'num_layers': 8,
            'dim_feedforward': 1024,
        },
        
        # Training parameters
        'learning_rate': 1e-3,        # FIXED LR - key to breakthrough!
        'weight_decay': 0.01,
        'batch_size': 4,              # Adjust based on memory
        'sequence_length': 256,       # Bit sequence length
        'num_epochs': 50,             # Long training run
        'max_grad_norm': 1.0,
        'dropout': 0.1,
        'total_steps': 10000,         # For scheduler
        
        # Data parameters
        'hf_token': None,  # Set via environment variable HF_TOKEN
        
        # Logging and checkpointing
        'log_interval': 10,
        'checkpoint_dir': '/data/BitTransformerLM/checkpoints',
    }
    
    # Create trainer
    trainer = ProductionTrainer(config)
    
    # Setup components
    trainer.setup_model()
    trainer.setup_optimizer()  
    trainer.setup_dataset()
    
    # Try to resume from checkpoint
    trainer.load_checkpoint()
    
    # Start training
    logger.info("🚀 STARTING BREAKTHROUGH BITRANSFORMERLM TRAINING!")
    logger.info("Configuration: Fixed RL Adafactor + 16M parameters + CPU training")
    
    trainer.train(config['num_epochs'])
    
    logger.info("Training completed!")
    logger.info(f"Best loss achieved: {trainer.best_loss:.6f}")
    logger.info(f"Checkpoints saved to: {trainer.checkpoint_dir}")


if __name__ == "__main__":
    main()