File size: 9,660 Bytes
8d18b7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Checkpoint Management for Training"""

import json
import logging
import shutil
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Dict, List, Optional

import torch

logger = logging.getLogger(__name__)


@dataclass
class CheckpointMetadata:
    """Metadata for a checkpoint."""
    step: int
    epoch: int
    global_step: int
    metrics: Dict[str, float] = field(default_factory=dict)
    config: Dict[str, Any] = field(default_factory=dict)
    model_name: str = "zenith"
    timestamp: str = ""

    def to_dict(self) -> Dict[str, Any]:
        return {
            "step": self.step,
            "epoch": self.epoch,
            "global_step": self.global_step,
            "metrics": self.metrics,
            "config": self.config,
            "model_name": self.model_name,
            "timestamp": self.timestamp,
        }

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "CheckpointMetadata":
        return cls(**data)


class CheckpointManager:
    """Manages saving and loading of checkpoints."""

    def __init__(

        self,

        checkpoint_dir: str,

        save_total_limit: int = 5,

        save_best_only: bool = False,

        metric_for_best: str = "eval_loss",

        greater_is_better: bool = False,

    ):
        self.checkpoint_dir = Path(checkpoint_dir)
        self.checkpoint_dir.mkdir(parents=True, exist_ok=True)
        self.save_total_limit = save_total_limit
        self.save_best_only = save_best_only
        self.metric_for_best = metric_for_best
        self.greater_is_better = greater_is_better

        self.best_metric = None
        self.checkpoints: List[Path] = []

        # Load existing checkpoints
        self._scan_checkpoints()

    def _scan_checkpoints(self):
        """Scan checkpoint directory for existing checkpoints."""
        for path in self.checkpoint_dir.glob("checkpoint-*"):
            if path.is_dir():
                self.checkpoints.append(path)
        self.checkpoints.sort(key=lambda p: int(p.name.split("-")[1]))

    def save_checkpoint(

        self,

        state: Dict[str, Any],

        name: str,

        metrics: Optional[Dict[str, float]] = None,

    ) -> Path:
        """Save checkpoint to disk."""
        checkpoint_path = self.checkpoint_dir / f"checkpoint-{name}"
        checkpoint_path.mkdir(exist_ok=True)

        # Save model state
        torch.save(state["model_state_dict"], checkpoint_path / "pytorch_model.bin")

        # Save optimizer and scheduler states
        if "optimizer_state_dict" in state:
            torch.save(state["optimizer_state_dict"], checkpoint_path / "optimizer.pt")
        if "scheduler_state_dict" in state and state["scheduler_state_dict"]:
            torch.save(state["scheduler_state_dict"], checkpoint_path / "scheduler.pt")
        if "scaler_state_dict" in state and state["scaler_state_dict"]:
            torch.save(state["scaler_state_dict"], checkpoint_path / "scaler.pt")

        # Save metadata
        metadata = CheckpointMetadata(
            step=state.get("step", 0),
            epoch=state.get("epoch", 0),
            global_step=state.get("global_step", 0),
            metrics=metrics or {},
            config=state.get("config", {}),
            timestamp=state.get("timestamp", ""),
        )
        with open(checkpoint_path / "metadata.json", "w") as f:
            json.dump(metadata.to_dict(), f, indent=2)

        logger.info(f"Checkpoint saved: {checkpoint_path}")

        # Update checkpoint list
        if checkpoint_path not in self.checkpoints:
            self.checkpoints.append(checkpoint_path)
            self.checkpoints.sort(key=lambda p: int(p.name.split("-")[1]))

        # Enforce limit
        if self.save_total_limit > 0 and len(self.checkpoints) > self.save_total_limit:
            self._remove_oldest_checkpoint()

        return checkpoint_path

    def load_checkpoint(

        self,

        checkpoint_path: Union[str, Path],

        model: torch.nn.Module,

        optimizer: Optional[torch.optim.Optimizer] = None,

        scheduler: Optional[Any] = None,

        scaler: Optional[torch.cuda.amp.GradScaler] = None,

    ) -> CheckpointMetadata:
        """Load checkpoint from disk."""
        checkpoint_path = Path(checkpoint_path)

        if not checkpoint_path.exists():
            raise FileNotFoundError(f"Checkpoint not found: {checkpoint_path}")

        # Load model
        model_path = checkpoint_path / "pytorch_model.bin"
        if model_path.exists():
            state_dict = torch.load(model_path, map_location="cpu")
            model.load_state_dict(state_dict)
            logger.info(f"Loaded model from {model_path}")
        else:
            logger.warning(f"Model weights not found at {model_path}")

        # Load optimizer
        if optimizer is not None:
            opt_path = checkpoint_path / "optimizer.pt"
            if opt_path.exists():
                optimizer.load_state_dict(torch.load(opt_path, map_location="cpu"))
                logger.info(f"Loaded optimizer from {opt_path}")

        # Load scheduler
        if scheduler is not None:
            sched_path = checkpoint_path / "scheduler.pt"
            if sched_path.exists():
                scheduler.load_state_dict(torch.load(sched_path, map_location="cpu"))
                logger.info(f"Loaded scheduler from {sched_path}")

        # Load scaler
        if scaler is not None:
            scaler_path = checkpoint_path / "scaler.pt"
            if scaler_path.exists():
                scaler.load_state_dict(torch.load(scaler_path, map_location="cpu"))
                logger.info(f"Loaded scaler from {scaler_path}")

        # Load metadata
        meta_path = checkpoint_path / "metadata.json"
        if meta_path.exists():
            with open(meta_path, "r") as f:
                metadata = CheckpointMetadata.from_dict(json.load(f))
            logger.info(f"Loaded metadata: epoch={metadata.epoch}, step={metadata.step}")
        else:
            metadata = CheckpointMetadata(step=0, epoch=0, global_step=0)

        return metadata

    def get_latest_checkpoint(self) -> Optional[Path]:
        """Get the most recent checkpoint."""
        if self.checkpoints:
            return self.checkpoints[-1]
        return None

    def get_best_checkpoint(self) -> Optional[Path]:
        """Get the best checkpoint based on metric."""
        if not self.checkpoints:
            return None

        best_path = None
        best_value = None

        for path in self.checkpoints:
            meta_path = path / "metadata.json"
            if meta_path.exists():
                with open(meta_path, "r") as f:
                    meta = CheckpointMetadata.from_dict(json.load(f))

                if self.metric_for_best in meta.metrics:
                    value = meta.metrics[self.metric_for_best]
                    if best_value is None or (
                        self.greater_is_better and value > best_value
                    ) or (not self.greater_is_better and value < best_value):
                        best_value = value
                        best_path = path

        return best_path

    def _remove_oldest_checkpoint(self):
        """Remove the oldest checkpoint to maintain limit."""
        if len(self.checkpoints) > self.save_total_limit:
            oldest = self.checkpoints.pop(0)
            if oldest.exists():
                shutil.rmtree(oldest)
                logger.info(f"Removed old checkpoint: {oldest}")

    def cleanup(self, keep: Optional[List[Path]] = None):
        """Clean up checkpoints, optionally keeping specific ones."""
        if keep is None:
            keep = []

        for path in self.checkpoints:
            if path not in keep:
                if path.exists():
                    shutil.rmtree(path)
                    logger.info(f"Removed checkpoint: {path}")

        self._scan_checkpoints()


def save_checkpoint(

    model: torch.nn.Module,

    optimizer: torch.optim.Optimizer,

    scheduler: Optional[Any],

    scaler: Optional[torch.cuda.amp.GradScaler],

    checkpoint_dir: str,

    epoch: int,

    global_step: int,

    metrics: Optional[Dict[str, float]] = None,

    config: Optional[Dict[str, Any]] = None,

    save_optimizer: bool = True,

    save_scheduler: bool = True,

):
    """Convenience function to save a checkpoint."""
    manager = CheckpointManager(checkpoint_dir, save_total_limit=0)
    state = {
        "model_state_dict": model.state_dict(),
        "global_step": global_step,
        "epoch": epoch,
        "config": config or {},
        "timestamp": "",
    }

    if save_optimizer:
        state["optimizer_state_dict"] = optimizer.state_dict()
    if save_scheduler and scheduler is not None:
        state["scheduler_state_dict"] = scheduler.state_dict()

    manager.save_checkpoint(state, f"step-{global_step}", metrics)


def load_checkpoint(

    checkpoint_path: str,

    model: torch.nn.Module,

    optimizer: Optional[torch.optim.Optimizer] = None,

    scheduler: Optional[Any] = None,

    scaler: Optional[torch.cuda.amp.GradScaler] = None,

) -> int:
    """Convenience function to load a checkpoint."""
    manager = CheckpointManager(Path(checkpoint_path).parent)
    metadata = manager.load_checkpoint(checkpoint_path, model, optimizer, scheduler, scaler)
    return metadata.global_step