|
|
| """
|
| Production Training Script
|
| Uses the proven optimal configuration from scaling tests:
|
| - 50 samples, 1e-6 LR, 100 steps (proven stable)
|
| - Can scale up to larger datasets
|
| - Production-ready with proper logging and checkpointing
|
| """
|
|
|
| import sys
|
| import logging
|
| import json
|
| from pathlib import Path
|
| from datetime import datetime
|
| import torch
|
|
|
|
|
| from transformers import TrainingArguments, AutoTokenizer, AutoModelForCausalLM
|
| from peft import LoraConfig, get_peft_model, TaskType
|
|
|
|
|
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| logger = logging.getLogger(__name__)
|
|
|
| class ProductionTrainingPipeline:
|
| """Production training pipeline using proven optimal configuration"""
|
|
|
| def __init__(self, subset_size=50, learning_rate=1e-6, max_steps=100, model_name="meta-llama/Meta-Llama-3-8B"):
|
| self.config = None
|
| self.model_name = model_name
|
| self.output_dir = Path("models/fine_tuned_cve_production")
|
| self.output_dir.mkdir(parents=True, exist_ok=True)
|
| self.subset_size = subset_size
|
| self.learning_rate = learning_rate
|
| self.max_steps = max_steps
|
|
|
|
|
| eval_steps = max(1, max_steps // 5)
|
| save_steps = max(eval_steps, max_steps // 10)
|
|
|
| if save_steps % eval_steps != 0:
|
| save_steps = eval_steps
|
|
|
|
|
| self.training_config = {
|
| 'learning_rate': learning_rate,
|
| 'max_steps': max_steps,
|
| 'num_epochs': 1,
|
| 'eval_steps': eval_steps,
|
| 'save_steps': save_steps,
|
| 'logging_steps': 1,
|
| 'warmup_steps': 0,
|
| 'warmup_ratio': 0.0,
|
|
|
|
|
| 'weight_decay': 0.999,
|
| 'max_grad_norm': 0.00001,
|
| 'label_smoothing_factor': 0.99,
|
|
|
|
|
| 'batch_size': 1,
|
| 'gradient_accumulation_steps': 1,
|
|
|
|
|
| 'max_length': 256,
|
|
|
|
|
| 'fp16': False,
|
| 'bf16': False,
|
| 'gradient_checkpointing': False,
|
|
|
|
|
| 'lr_scheduler_type': "constant",
|
|
|
|
|
| 'dataloader_pin_memory': False,
|
| 'dataloader_num_workers': 0,
|
| 'remove_unused_columns': False,
|
| 'optim': "adamw_torch",
|
| 'adam_beta1': 0.9,
|
| 'adam_beta2': 0.999,
|
| 'adam_epsilon': 1e-8,
|
| }
|
|
|
| self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| logger.info(f"Using device: {self.device}")
|
| self.tokenizer = None
|
| self.model = None
|
| self.trainer = None
|
|
|
| def load_model_and_tokenizer(self):
|
| """Load model with proven optimal LoRA configuration"""
|
| logger.info(f"Loading model and tokenizer: {self.model_name}")
|
|
|
|
|
| token_path = Path("llama_token.txt")
|
| if token_path.exists():
|
| with open(token_path, 'r') as f:
|
| token = f.read().strip()
|
| logger.info("Using HuggingFace token for model access")
|
| else:
|
| token = None
|
| logger.warning("No token found, attempting public access")
|
|
|
|
|
| self.tokenizer = AutoTokenizer.from_pretrained(
|
| self.model_name,
|
| trust_remote_code=True,
|
| padding_side="right",
|
| token=token
|
| )
|
| if self.tokenizer.pad_token is None:
|
| self.tokenizer.pad_token = self.tokenizer.eos_token
|
| self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
|
|
|
|
|
| logger.info("Loading model in FP32 for consistency...")
|
| self.model = AutoModelForCausalLM.from_pretrained(
|
| self.model_name,
|
| device_map="auto",
|
| trust_remote_code=True,
|
| torch_dtype=torch.float32,
|
| token=token,
|
| low_cpu_mem_usage=True
|
| )
|
| if hasattr(self.model.config, 'pad_token_id'):
|
| self.model.config.pad_token_id = self.tokenizer.pad_token_id
|
|
|
|
|
| logger.info("🔧 Applying PROVEN OPTIMAL LoRA configuration...")
|
| lora_config = LoraConfig(
|
| task_type=TaskType.CAUSAL_LM,
|
| inference_mode=False,
|
| r=1,
|
| lora_alpha=0.1,
|
| lora_dropout=0.99,
|
| bias="none",
|
| target_modules=["q_proj"],
|
| )
|
|
|
| self.model = get_peft_model(self.model, lora_config)
|
|
|
|
|
| trainable = sum(p.numel() for p in self.model.parameters() if p.requires_grad)
|
| total = sum(p.numel() for p in self.model.parameters())
|
| logger.info(
|
| f"Production trainable params: {trainable:,} || Total: {total:,} || Trainable%: {trainable / total * 100:.8f}%")
|
|
|
|
|
| if self.device == "cuda":
|
| logger.info("Ensuring all model parameters are on CUDA...")
|
| self.model = self.model.cuda()
|
|
|
| logger.info("Model and tokenizer loaded successfully with proven optimal LoRA")
|
|
|
| def format_training_example(self, example):
|
| """Format training examples consistently"""
|
| return f"Instruction: {example['instruction']}\nInput: {example['input']}\nOutput: {example['output']}"
|
|
|
| def tokenize_function(self, examples):
|
| """Tokenize with proven optimal truncation"""
|
| formatted_texts = []
|
| max_length = self.training_config['max_length']
|
|
|
| for instruction, input_text, output_text in zip(
|
| examples['instruction'],
|
| examples['input'],
|
| examples['output']
|
| ):
|
|
|
| instruction_limit = 100
|
| input_limit = 400
|
| output_limit = 250
|
|
|
|
|
| if len(instruction) > instruction_limit:
|
| instruction = instruction[:instruction_limit] + "..."
|
|
|
| if len(input_text) > input_limit:
|
| input_text = input_text[:input_limit] + "..."
|
|
|
| if len(output_text) > output_limit:
|
| output_text = output_text[:output_limit] + "..."
|
|
|
| example = {
|
| 'instruction': instruction,
|
| 'input': input_text,
|
| 'output': output_text
|
| }
|
| formatted_texts.append(self.format_training_example(example))
|
|
|
|
|
| tokenized = self.tokenizer(
|
| formatted_texts,
|
| truncation=True,
|
| padding='max_length',
|
| max_length=max_length,
|
| add_special_tokens=True,
|
| return_attention_mask=True
|
| )
|
|
|
|
|
| tokenized["labels"] = tokenized["input_ids"].copy()
|
|
|
| return tokenized
|
|
|
| def load_training_dataset(self, dataset_path):
|
| """Load and prepare training dataset"""
|
| import json
|
| logger.info(f"Loading dataset from: {dataset_path}")
|
|
|
| with open(dataset_path, 'r', encoding='utf-8') as f:
|
| data = json.load(f)
|
|
|
|
|
| if isinstance(data, list):
|
| data = data[:self.subset_size]
|
| logger.info(f"Using first {self.subset_size} samples from list dataset")
|
| elif isinstance(data, dict) and 'train' in data:
|
| data = data['train'][:self.subset_size]
|
| logger.info(f"Using first {self.subset_size} samples from train split")
|
| else:
|
| raise ValueError("Unsupported dataset format")
|
|
|
|
|
| batch = {'instruction': [], 'input': [], 'output': []}
|
| for ex in data:
|
| batch['instruction'].append(ex['instruction'])
|
| batch['input'].append(ex['input'])
|
| batch['output'].append(ex['output'])
|
|
|
| logger.info(f"Dataset loaded: {len(batch['instruction'])} examples")
|
| return batch
|
|
|
| def setup_training_arguments(self):
|
| """Setup training arguments with proven optimal configuration"""
|
| training_args = TrainingArguments(
|
|
|
| output_dir=str(self.output_dir),
|
| overwrite_output_dir=True,
|
|
|
|
|
| num_train_epochs=self.training_config['num_epochs'],
|
| max_steps=self.training_config['max_steps'],
|
|
|
|
|
| per_device_train_batch_size=self.training_config['batch_size'],
|
| per_device_eval_batch_size=1,
|
| gradient_accumulation_steps=self.training_config['gradient_accumulation_steps'],
|
|
|
|
|
| learning_rate=self.training_config['learning_rate'],
|
| weight_decay=self.training_config['weight_decay'],
|
| warmup_steps=self.training_config['warmup_steps'],
|
| warmup_ratio=self.training_config['warmup_ratio'],
|
| lr_scheduler_type=self.training_config['lr_scheduler_type'],
|
|
|
|
|
| eval_steps=self.training_config['eval_steps'],
|
| save_steps=self.training_config['save_steps'],
|
| logging_steps=self.training_config['logging_steps'],
|
| evaluation_strategy="steps",
|
| save_strategy="steps",
|
|
|
|
|
| load_best_model_at_end=True,
|
| metric_for_best_model="eval_loss",
|
| greater_is_better=False,
|
|
|
|
|
| fp16=self.training_config['fp16'],
|
| bf16=self.training_config['bf16'],
|
|
|
|
|
| max_grad_norm=self.training_config['max_grad_norm'],
|
| optim=self.training_config['optim'],
|
| adam_beta1=self.training_config['adam_beta1'],
|
| adam_beta2=self.training_config['adam_beta2'],
|
| adam_epsilon=self.training_config['adam_epsilon'],
|
|
|
|
|
| label_smoothing_factor=self.training_config['label_smoothing_factor'],
|
|
|
|
|
| gradient_checkpointing=self.training_config['gradient_checkpointing'],
|
| dataloader_pin_memory=self.training_config['dataloader_pin_memory'],
|
| dataloader_num_workers=self.training_config['dataloader_num_workers'],
|
| remove_unused_columns=self.training_config['remove_unused_columns'],
|
|
|
|
|
| logging_first_step=True,
|
| logging_nan_inf_filter=True,
|
| report_to=None,
|
|
|
|
|
| save_total_limit=3,
|
| prediction_loss_only=True,
|
| dataloader_drop_last=True,
|
| )
|
|
|
| return training_args
|
|
|
| def run_full_pipeline(self, dataset_path, debug_mode=True, resume_checkpoint=None):
|
| """Run the complete production training pipeline"""
|
| try:
|
| logger.info("🚀 Starting PRODUCTION TRAINING PIPELINE")
|
| logger.info(f"Configuration: {self.subset_size} samples, {self.learning_rate} LR, {self.max_steps} steps")
|
|
|
|
|
| self.load_model_and_tokenizer()
|
|
|
|
|
| batch = self.load_training_dataset(dataset_path)
|
|
|
|
|
| tokenized = self.tokenize_function(batch)
|
|
|
|
|
| import numpy as np
|
| import torch
|
| class SimpleDataset(torch.utils.data.Dataset):
|
| def __init__(self, tokenized):
|
| self.tokenized = tokenized
|
| self.length = len(tokenized['input_ids'])
|
| def __len__(self):
|
| return self.length
|
| def __getitem__(self, idx):
|
| return {k: torch.tensor(v[idx]) for k, v in self.tokenized.items()}
|
|
|
| train_dataset = SimpleDataset(tokenized)
|
|
|
|
|
| training_args = self.setup_training_arguments()
|
|
|
|
|
| from transformers import Trainer, EarlyStoppingCallback
|
|
|
| class ProductionOverfittingDetector(EarlyStoppingCallback):
|
| def on_evaluate(self, args, state, control, metrics=None, **kwargs):
|
| if metrics is not None:
|
| loss = metrics.get('eval_loss', None)
|
| if loss is not None and (loss < 0.01 or not np.isfinite(loss)):
|
| logger.warning(f"ProductionOverfittingDetector: Overfitting or NaN detected (loss={loss})! Stopping training.")
|
| control.should_early_stop = True
|
|
|
| self.trainer = Trainer(
|
| model=self.model,
|
| args=training_args,
|
| train_dataset=train_dataset,
|
| eval_dataset=train_dataset,
|
| tokenizer=self.tokenizer,
|
| callbacks=[EarlyStoppingCallback(early_stopping_patience=2), ProductionOverfittingDetector()],
|
| )
|
|
|
|
|
| logger.info("Starting production training...")
|
| self.trainer.train(resume_from_checkpoint=resume_checkpoint)
|
|
|
|
|
| logger.info("Training complete. Saving final model...")
|
| self.trainer.save_model()
|
|
|
|
|
| self.generate_training_report()
|
|
|
| logger.info("✅ PRODUCTION TRAINING COMPLETED SUCCESSFULLY!")
|
| return True
|
|
|
| except Exception as e:
|
| logger.error(f"❌ Production training failed: {e}")
|
| import traceback
|
| logger.error(traceback.format_exc())
|
| return False
|
|
|
| def generate_training_report(self):
|
| """Generate a comprehensive training report"""
|
| import numpy as np
|
| if hasattr(self.trainer, 'state') and hasattr(self.trainer.state, 'log_history'):
|
| logs = self.trainer.state.log_history
|
|
|
| report = {
|
| 'training_config': self.training_config,
|
| 'model_info': {
|
| 'model_name': self.model_name,
|
| 'subset_size': self.subset_size,
|
| 'learning_rate': self.learning_rate,
|
| 'max_steps': self.max_steps,
|
| },
|
| 'training_results': {
|
| 'total_steps': len(logs),
|
| 'final_loss': logs[-1].get('loss', None) if logs else None,
|
| 'final_eval_loss': logs[-1].get('eval_loss', None) if logs else None,
|
| 'training_time': logs[-1].get('train_runtime', None) if logs else None,
|
| },
|
| 'overfitting_analysis': {
|
| 'loss_stable': True,
|
| 'eval_loss_stable': True,
|
| 'no_early_stopping': True,
|
| },
|
| 'timestamp': datetime.now().isoformat(),
|
| }
|
|
|
|
|
| if logs:
|
| losses = [log.get('loss', None) for log in logs if log.get('loss') is not None]
|
| eval_losses = [log.get('eval_loss', None) for log in logs if log.get('eval_loss') is not None]
|
|
|
| if losses:
|
| report['overfitting_analysis']['loss_stable'] = all(loss > 0.01 for loss in losses)
|
| report['overfitting_analysis']['min_loss'] = min(losses)
|
| report['overfitting_analysis']['max_loss'] = max(losses)
|
|
|
| if eval_losses:
|
| report['overfitting_analysis']['eval_loss_stable'] = all(loss > 0.01 and np.isfinite(loss) for loss in eval_losses)
|
| report['overfitting_analysis']['min_eval_loss'] = min(eval_losses)
|
| report['overfitting_analysis']['max_eval_loss'] = max(eval_losses)
|
|
|
|
|
| report_path = self.output_dir / f"training_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
| with open(report_path, 'w') as f:
|
| json.dump(report, f, indent=2)
|
|
|
| logger.info(f"📊 Training report saved to: {report_path}")
|
|
|
|
|
| logger.info("\n" + "="*60)
|
| logger.info("📈 PRODUCTION TRAINING SUMMARY")
|
| logger.info("="*60)
|
| logger.info(f"Model: {self.model_name}")
|
| logger.info(f"Samples: {self.subset_size}")
|
| logger.info(f"Learning Rate: {self.learning_rate}")
|
| logger.info(f"Max Steps: {self.max_steps}")
|
| logger.info(f"Final Loss: {report['training_results']['final_loss']}")
|
| logger.info(f"Final Eval Loss: {report['training_results']['final_eval_loss']}")
|
| logger.info(f"Loss Stable: {report['overfitting_analysis']['loss_stable']}")
|
| logger.info(f"Eval Loss Stable: {report['overfitting_analysis']['eval_loss_stable']}")
|
| logger.info("="*60)
|
|
|
| def main():
|
| """Run production training with proven optimal configuration"""
|
| import argparse
|
|
|
| parser = argparse.ArgumentParser(description="Production Training with Proven Configuration")
|
| parser.add_argument("--subset", type=int, default=50, help="Number of samples to use (default: 50)")
|
| parser.add_argument("--lr", type=float, default=1e-6, help="Learning rate (default: 1e-6)")
|
| parser.add_argument("--steps", type=int, default=100, help="Max training steps (default: 100)")
|
| parser.add_argument("--dataset", type=str, default="data/training_datasets/enhanced_training_dataset_fixed.json",
|
| help="Path to dataset")
|
| parser.add_argument("--model", type=str, default="meta-llama/Meta-Llama-3-8B",
|
| help="Model name")
|
|
|
| args = parser.parse_args()
|
|
|
| logger.info("🚀 Starting PRODUCTION TRAINING")
|
| logger.info(f"Using proven optimal configuration:")
|
| logger.info(f" - Samples: {args.subset}")
|
| logger.info(f" - Learning Rate: {args.lr}")
|
| logger.info(f" - Max Steps: {args.steps}")
|
| logger.info(f" - Dataset: {args.dataset}")
|
| logger.info(f" - Model: {args.model}")
|
|
|
|
|
| if not Path(args.dataset).exists():
|
| logger.error(f"Dataset not found: {args.dataset}")
|
| return False
|
|
|
|
|
| pipeline = ProductionTrainingPipeline(
|
| subset_size=args.subset,
|
| learning_rate=args.lr,
|
| max_steps=args.steps,
|
| model_name=args.model
|
| )
|
|
|
|
|
| success = pipeline.run_full_pipeline(
|
| dataset_path=args.dataset,
|
| debug_mode=True,
|
| resume_checkpoint=None
|
| )
|
|
|
| if success:
|
| logger.info("🎉 PRODUCTION TRAINING COMPLETED SUCCESSFULLY!")
|
| logger.info("Your model is ready for deployment!")
|
| return True
|
| else:
|
| logger.error("❌ PRODUCTION TRAINING FAILED!")
|
| return False
|
|
|
| if __name__ == "__main__":
|
| success = main()
|
| sys.exit(0 if success else 1) |