File size: 7,782 Bytes
c8b77b5 |
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 |
"""
Refactored LoRA Knowledge Distillation Trainer using modular architecture.
This module implements a clean, testable trainer that follows the interface contracts
and provides better separation of concerns.
"""
import logging
from pathlib import Path
from typing import Any, Dict, Optional
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from ..core.base_components import BaseTrainer
from ..core.exceptions import TrainingError
from ..core.interfaces import TrainingConfig
logger = logging.getLogger(__name__)
class ModularLoRATrainer(BaseTrainer):
"""Modular LoRA trainer with clean separation of concerns."""
def __init__(
self,
model: nn.Module,
optimizer: torch.optim.Optimizer,
loss_function,
device: str = "cpu",
teacher_model: Optional[nn.Module] = None,
):
"""
Initialize the modular LoRA trainer.
Args:
model: Student model to train
optimizer: Optimizer for training
loss_function: Loss function implementing ILossFunction
device: Training device
teacher_model: Optional teacher model for distillation
"""
super().__init__(model, optimizer, device)
self.loss_function = loss_function
self.teacher_model = teacher_model
if self.teacher_model:
self.teacher_model.to(self.device)
self.teacher_model.eval()
self.custom_loss_fn = None
def set_custom_loss_fn(self, loss_fn):
"""Set custom loss function for specialized training."""
self.custom_loss_fn = loss_fn
def compute_distillation_loss(self, student_outputs, teacher_outputs, batch):
"""Compute standard distillation loss."""
return self.loss_function.compute(
student_outputs.logits,
(
teacher_outputs.logits
if hasattr(teacher_outputs, "logits")
else teacher_outputs
),
labels=batch.get("labels"),
)
def train(self, dataloader: DataLoader, config: TrainingConfig) -> Dict[str, Any]:
"""
Train the model with the given configuration.
Args:
dataloader: Training data loader
config: Training configuration
Returns:
Training results and metrics
"""
try:
self.model.train()
total_loss = 0.0
num_batches = 0
training_metrics = {}
for epoch in range(config.num_epochs):
epoch_loss = 0.0
epoch_batches = 0
for batch_idx, batch in enumerate(dataloader):
# Move batch to device
batch = self._move_batch_to_device(batch)
# Forward pass
self.optimizer.zero_grad()
# Student model forward pass
student_outputs = self.model(**batch)
# Teacher model forward pass (if available)
teacher_outputs = None
if self.teacher_model:
with torch.no_grad():
teacher_outputs = self.teacher_model(**batch)
# Compute loss
if self.custom_loss_fn:
loss = self.custom_loss_fn(
student_outputs, teacher_outputs, batch
)
else:
loss = self.loss_function.compute(
(
student_outputs.logits
if hasattr(student_outputs, "logits")
else student_outputs
),
batch.get("labels", batch.get("input_ids")),
)
# Backward pass
loss.backward()
self.optimizer.step()
# Track metrics
epoch_loss += loss.item()
epoch_batches += 1
# Log training step
if batch_idx % config.save_steps == 0:
step_metrics = self.loss_function.get_metrics()
self._log_training_step(
epoch, batch_idx, loss.item(), step_metrics
)
logger.info(
f"Epoch {epoch}, Batch {batch_idx}, Loss: {loss.item():.4f}"
)
# End of epoch
avg_epoch_loss = (
epoch_loss / epoch_batches if epoch_batches > 0 else 0.0
)
total_loss += epoch_loss
num_batches += epoch_batches
training_metrics[f"epoch_{epoch}_loss"] = avg_epoch_loss
# Save checkpoint
if epoch % config.save_steps == 0:
checkpoint_path = (
Path(config.output_dir) / f"checkpoint_epoch_{epoch}.pt"
)
self.save_checkpoint(checkpoint_path, epoch)
# Final results
avg_loss = total_loss / num_batches if num_batches > 0 else 0.0
results = {
"average_loss": avg_loss,
"total_epochs": config.num_epochs,
"total_batches": num_batches,
"training_metrics": training_metrics,
"loss_function_metrics": self.loss_function.get_metrics(),
}
logger.info(f"Training completed. Average loss: {avg_loss:.4f}")
return results
except Exception as e:
raise TrainingError(
f"Training failed: {str(e)}",
"TRAINING_FAILED",
{"epoch": getattr(self, "current_epoch", 0)},
)
def evaluate(self, dataloader: DataLoader) -> Dict[str, float]:
"""
Evaluate the model on the given dataset.
Args:
dataloader: Evaluation data loader
Returns:
Evaluation metrics
"""
try:
self.model.eval()
total_loss = 0.0
num_batches = 0
with torch.no_grad():
for batch in dataloader:
batch = self._move_batch_to_device(batch)
# Forward pass
outputs = self.model(**batch)
# Compute loss
loss = self.loss_function.compute(
outputs.logits if hasattr(outputs, "logits") else outputs,
batch.get("labels", batch.get("input_ids")),
)
total_loss += loss.item()
num_batches += 1
avg_loss = total_loss / num_batches if num_batches > 0 else 0.0
results = {"eval_loss": avg_loss, "eval_batches": num_batches}
results.update(self.loss_function.get_metrics())
logger.info(f"Evaluation completed. Average loss: {avg_loss:.4f}")
return results
except Exception as e:
raise TrainingError(f"Evaluation failed: {str(e)}", "EVALUATION_FAILED")
def _move_batch_to_device(
self, batch: Dict[str, torch.Tensor]
) -> Dict[str, torch.Tensor]:
"""Move batch tensors to the training device."""
device_batch = {}
for key, value in batch.items():
if isinstance(value, torch.Tensor):
device_batch[key] = value.to(self.device)
else:
device_batch[key] = value
return device_batch
|