File size: 8,199 Bytes
ec8f374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
"""
Custom Training Loop Module

Provides custom training loop implementation with fine-grained control over training.
"""

from dataclasses import dataclass, field
from typing import Optional, Dict, Any, Callable
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm


@dataclass
class TrainingConfig:
    """Configuration for custom training loop."""
    num_epochs: int = 3
    learning_rate: float = 2e-4
    batch_size: int = 4
    gradient_accumulation_steps: int = 4
    max_grad_norm: float = 1.0
    warmup_steps: int = 100
    logging_steps: int = 10
    eval_steps: int = 500
    save_steps: int = 500
    output_dir: str = "./models/output"
    device: str = "cuda" if torch.cuda.is_available() else "cpu"


class TrainingLoop:
    """
    Custom training loop for fine-grained control over the training process.

    Provides manual control over:
    - Forward/backward passes
    - Gradient accumulation
    - Learning rate scheduling
    - Logging and evaluation
    - Checkpointing
    """

    def __init__(
        self,
        model: torch.nn.Module,
        train_dataloader: DataLoader,
        eval_dataloader: Optional[DataLoader] = None,
        config: Optional[TrainingConfig] = None
    ):
        """
        Initialize custom training loop.

        Args:
            model: PyTorch model to train
            train_dataloader: Training data loader
            eval_dataloader: Optional evaluation data loader
            config: Training configuration
        """
        self.model = model
        self.train_dataloader = train_dataloader
        self.eval_dataloader = eval_dataloader
        self.config = config or TrainingConfig()

        self.optimizer = None
        self.scheduler = None
        self.global_step = 0
        self.current_epoch = 0

    def setup_optimizer(self, optimizer_class=torch.optim.AdamW, **optimizer_kwargs):
        """
        Setup optimizer and learning rate scheduler.

        Args:
            optimizer_class: Optimizer class to use
            **optimizer_kwargs: Additional optimizer arguments
        """
        self.optimizer = optimizer_class(
            self.model.parameters(),
            lr=self.config.learning_rate,
            **optimizer_kwargs
        )

        # Linear warmup scheduler
        def lr_lambda(current_step: int):
            if current_step < self.config.warmup_steps:
                return float(current_step) / float(max(1, self.config.warmup_steps))
            return 1.0

        self.scheduler = torch.optim.lr_scheduler.LambdaLR(
            self.optimizer,
            lr_lambda
        )

    def train_step(self, batch: Dict[str, torch.Tensor]) -> float:
        """
        Perform a single training step.

        Args:
            batch: Batch of training data

        Returns:
            Loss value
        """
        # Move batch to device
        batch = {k: v.to(self.config.device) for k, v in batch.items()}

        # Forward pass
        outputs = self.model(**batch)
        loss = outputs.loss

        # Scale loss for gradient accumulation
        loss = loss / self.config.gradient_accumulation_steps

        # Backward pass
        loss.backward()

        return loss.item()

    def train_epoch(self) -> Dict[str, float]:
        """
        Train for one epoch.

        Returns:
            Training metrics
        """
        self.model.train()
        total_loss = 0
        num_batches = 0

        progress_bar = tqdm(
            self.train_dataloader,
            desc=f"Epoch {self.current_epoch + 1}/{self.config.num_epochs}"
        )

        for step, batch in enumerate(progress_bar):
            # Training step
            loss = self.train_step(batch)
            total_loss += loss

            # Gradient accumulation
            if (step + 1) % self.config.gradient_accumulation_steps == 0:
                # Clip gradients
                torch.nn.utils.clip_grad_norm_(
                    self.model.parameters(),
                    self.config.max_grad_norm
                )

                # Optimizer step
                self.optimizer.step()
                self.scheduler.step()
                self.optimizer.zero_grad()

                self.global_step += 1
                num_batches += 1

                # Update progress bar
                progress_bar.set_postfix({
                    "loss": total_loss / num_batches,
                    "lr": self.scheduler.get_last_lr()[0]
                })

                # Logging
                if self.global_step % self.config.logging_steps == 0:
                    avg_loss = total_loss / num_batches
                    print(f"Step {self.global_step}: loss={avg_loss:.4f}")

                # Evaluation
                if self.eval_dataloader and self.global_step % self.config.eval_steps == 0:
                    eval_metrics = self.evaluate()
                    print(f"Evaluation: {eval_metrics}")
                    self.model.train()

        return {
            "loss": total_loss / max(num_batches, 1),
            "epoch": self.current_epoch
        }

    def evaluate(self) -> Dict[str, float]:
        """
        Evaluate model on validation set.

        Returns:
            Evaluation metrics
        """
        if self.eval_dataloader is None:
            return {}

        self.model.eval()
        total_loss = 0
        num_batches = 0

        with torch.no_grad():
            for batch in tqdm(self.eval_dataloader, desc="Evaluating"):
                batch = {k: v.to(self.config.device) for k, v in batch.items()}
                outputs = self.model(**batch)
                total_loss += outputs.loss.item()
                num_batches += 1

        return {
            "eval_loss": total_loss / max(num_batches, 1)
        }

    def train(self, callback: Optional[Callable] = None) -> Dict[str, Any]:
        """
        Run full training loop.

        Args:
            callback: Optional callback function called after each epoch

        Returns:
            Training history
        """
        if self.optimizer is None:
            self.setup_optimizer()

        print(f"Starting training for {self.config.num_epochs} epochs")
        print(f"Device: {self.config.device}")
        print(f"Batch size: {self.config.batch_size}")
        print(f"Gradient accumulation: {self.config.gradient_accumulation_steps}")

        history = {
            "train_loss": [],
            "eval_loss": []
        }

        for epoch in range(self.config.num_epochs):
            self.current_epoch = epoch

            # Train epoch
            train_metrics = self.train_epoch()
            history["train_loss"].append(train_metrics["loss"])

            # Evaluate
            if self.eval_dataloader:
                eval_metrics = self.evaluate()
                history["eval_loss"].append(eval_metrics.get("eval_loss", 0))

            # Callback
            if callback:
                callback(epoch, train_metrics)

        print("✅ Training complete!")
        return history

    def save_checkpoint(self, path: str) -> None:
        """
        Save training checkpoint.

        Args:
            path: Path to save checkpoint
        """
        checkpoint = {
            "model_state_dict": self.model.state_dict(),
            "optimizer_state_dict": self.optimizer.state_dict(),
            "scheduler_state_dict": self.scheduler.state_dict(),
            "global_step": self.global_step,
            "epoch": self.current_epoch
        }
        torch.save(checkpoint, path)
        print(f"Checkpoint saved to: {path}")

    def load_checkpoint(self, path: str) -> None:
        """
        Load training checkpoint.

        Args:
            path: Path to checkpoint
        """
        checkpoint = torch.load(path)
        self.model.load_state_dict(checkpoint["model_state_dict"])
        self.optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
        self.scheduler.load_state_dict(checkpoint["scheduler_state_dict"])
        self.global_step = checkpoint["global_step"]
        self.current_epoch = checkpoint["epoch"]
        print(f"Checkpoint loaded from: {path}")