File size: 8,421 Bytes
88b5dc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import logging
import time
from dataclasses import KW_ONLY, dataclass
from pathlib import Path
from typing import Protocol

import torch
from torch import Tensor
from torch.utils.data import DataLoader

from .control import non_blocking_input
from .distributed import is_global_leader
from .engine import Engine
from .utils import tree_map

logger = logging.getLogger(__name__)


class EvalFn(Protocol):
    def __call__(self, engine: Engine, eval_dir: Path) -> None:
        ...


class EngineLoader(Protocol):
    def __call__(self, run_dir: Path) -> Engine:
        ...


class GenFeeder(Protocol):
    def __call__(self, engine: Engine, batch: dict[str, Tensor]) -> tuple[Tensor, dict[str, Tensor]]:
        ...


class DisFeeder(Protocol):
    def __call__(self, engine: Engine, batch: dict[str, Tensor] | None, fake: Tensor) -> dict[str, Tensor]:
        ...


@dataclass
class TrainLoop:
    _ = KW_ONLY

    run_dir: Path
    train_dl: DataLoader

    load_G: EngineLoader
    feed_G: GenFeeder
    load_D: EngineLoader | None = None
    feed_D: DisFeeder | None = None

    update_every: int = 5_000
    eval_every: int = 5_000
    backup_steps: tuple[int, ...] = (5_000, 100_000, 500_000)

    device: str = "cuda"
    eval_fn: EvalFn | None = None
    gan_training_start_step: int | None = None

    @property
    def global_step(self):
        return self.engine_G.global_step  # How many steps have been completed?

    @property
    def eval_dir(self) -> Path | None:
        if self.eval_every != 0:
            eval_dir = self.run_dir.joinpath("eval")
            eval_dir.mkdir(exist_ok=True)
        else:
            eval_dir = None
        return eval_dir

    @property
    def viz_dir(self) -> Path:
        return Path(self.run_dir / "viz")

    def make_current_step_viz_path(self, name: str, suffix: str) -> Path:
        path = (self.viz_dir / name / f"{self.global_step}").with_suffix(suffix)
        path.parent.mkdir(exist_ok=True, parents=True)
        return path

    def __post_init__(self):
        engine_G = self.load_G(self.run_dir)
        if self.load_D is None:
            engine_D = None
        else:
            engine_D = self.load_D(self.run_dir)
        self.engine_G = engine_G
        self.engine_D = engine_D

    @property
    def model_G(self):
        return self.engine_G.module

    @property
    def model_D(self):
        if self.engine_D is None:
            return None
        return self.engine_D.module

    def save_checkpoint(self, tag="default"):
        engine_G = self.engine_G
        engine_D = self.engine_D
        engine_G.save_checkpoint(tag=tag)
        if engine_D is not None:
            engine_D.save_checkpoint(tag=tag)

    def run(self, max_steps: int = -1):
        self.set_running_loop_(self)

        train_dl = self.train_dl
        update_every = self.update_every
        eval_every = self.eval_every
        device = self.device
        eval_fn = self.eval_fn

        engine_G = self.engine_G
        engine_D = self.engine_D
        eval_dir = self.eval_dir

        init_step = self.global_step

        logger.info(f"\nTraining from step {init_step} to step {max_steps}")
        warmup_steps = {init_step + x for x in [50, 100, 500]}

        engine_G.train()

        if engine_D is not None:
            engine_D.train()

        gan_start_step = self.gan_training_start_step

        while True:
            loss_G = loss_D = 0

            for batch in train_dl:
                torch.cuda.synchronize()
                start_time = time.time()

                # What's the step after this batch?
                step = self.global_step + 1

                # Send data to the GPU
                batch = tree_map(lambda x: x.to(device) if isinstance(x, Tensor) else x, batch)

                stats = {"step": step}

                # Include step == 1 for sanity check
                gan_started = gan_start_step is not None and (step >= gan_start_step or step == 1)
                gan_started &= engine_D is not None

                # Generator step
                fake, losses = self.feed_G(engine=engine_G, batch=batch)

                # Train generator
                if gan_started:
                    assert engine_D is not None
                    assert self.feed_D is not None

                    # Freeze the discriminator to let gradient go through fake
                    engine_D.freeze_()
                    losses |= self.feed_D(engine=engine_D, batch=None, fake=fake)

                loss_G = sum(losses.values())
                stats |= {f"G/{k}": v.item() for k, v in losses.items()}
                stats |= {f"G/{k}": v for k, v in engine_G.gather_attribute("stats").items()}
                del losses

                assert isinstance(loss_G, Tensor)
                stats["G/loss"] = loss_G.item()
                stats["G/lr"] = engine_G.get_lr()[0]
                stats["G/grad_norm"] = engine_G.get_grad_norm() or 0

                if loss_G.isnan().item():
                    logger.error("Generator loss is NaN, skipping step")
                    continue

                engine_G.backward(loss_G)
                engine_G.step()

                # Discriminator step
                if gan_started:
                    assert engine_D is not None
                    assert self.feed_D is not None

                    engine_D.unfreeze_()
                    losses = self.feed_D(engine=engine_D, batch=batch, fake=fake.detach())
                    del fake

                    assert isinstance(losses, dict)
                    loss_D = sum(losses.values())
                    assert isinstance(loss_D, Tensor)

                    stats |= {f"D/{k}": v.item() for k, v in losses.items()}
                    stats |= {f"D/{k}": v for k, v in engine_D.gather_attribute("stats").items()}
                    del losses

                    if loss_D.isnan().item():
                        logger.error("Discriminator loss is NaN, skipping step")
                        continue

                    engine_D.backward(loss_D)
                    engine_D.step()

                    stats["D/loss"] = loss_D.item()
                    stats["D/lr"] = engine_D.get_lr()[0]
                    stats["D/grad_norm"] = engine_D.get_grad_norm() or 0

                torch.cuda.synchronize()
                stats["elapsed_time"] = time.time() - start_time
                stats = tree_map(lambda x: float(f"{x:.4g}") if isinstance(x, float) else x, stats)
                logger.info(json.dumps(stats, indent=0))

                command = non_blocking_input()

                evaling = step % eval_every == 0 or step in warmup_steps or command.strip() == "eval"
                if eval_fn is not None and is_global_leader() and eval_dir is not None and evaling:
                    engine_G.eval()
                    eval_fn(engine_G, eval_dir=eval_dir)
                    engine_G.train()

                if command.strip() == "quit":
                    logger.info("Training paused")
                    self.save_checkpoint("default")
                    return

                if command.strip() == "backup" or step in self.backup_steps:
                    logger.info("Backing up")
                    self.save_checkpoint(tag=f"backup_{step:07d}")

                if step % update_every == 0 or command.strip() == "save":
                    self.save_checkpoint(tag="default")

                if step == max_steps:
                    logger.info("Training finished")
                    self.save_checkpoint(tag="default")
                    return

    @classmethod
    def set_running_loop_(cls, loop):
        assert isinstance(loop, cls), f"Expected {cls}, got {type(loop)}"
        cls._running_loop: cls = loop

    @classmethod
    def get_running_loop(cls) -> "TrainLoop | None":
        if hasattr(cls, "_running_loop"):
            assert isinstance(cls._running_loop, cls)
            return cls._running_loop
        return None

    @classmethod
    def get_running_loop_global_step(cls) -> int | None:
        if loop := cls.get_running_loop():
            return loop.global_step
        return None

    @classmethod
    def get_running_loop_viz_path(cls, name: str, suffix: str) -> Path | None:
        if loop := cls.get_running_loop():
            return loop.make_current_step_viz_path(name, suffix)
        return None