File size: 5,520 Bytes
fa2d87b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import json
import os
from pathlib import Path
from typing import Any

import torch


SPENT_PREDICTIONS = frozenset({"noise_pred", "audio_noise_pred"})


class DenoiseCheckpointStop(RuntimeError):
    def __init__(self, *, completed_steps: int, total_steps: int):
        self.completed_steps = completed_steps
        self.total_steps = total_steps
        super().__init__(f"stopped after durable checkpoint {completed_steps} of {total_steps}")


def _partial_path(path: Path) -> Path:
    return path.with_name(f"{path.name}.partial-{os.getpid()}")


def _sync_and_replace(partial: Path, target: Path) -> None:
    with partial.open("rb") as handle:
        os.fsync(handle.fileno())
    os.replace(partial, target)
    directory_fd = os.open(target.parent, os.O_RDONLY)
    try:
        os.fsync(directory_fd)
    finally:
        os.close(directory_fd)


def atomic_torch_save(payload: Any, path: Path) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    partial = _partial_path(path)
    try:
        torch.save(payload, partial)
        _sync_and_replace(partial, path)
    finally:
        partial.unlink(missing_ok=True)


def atomic_json_write(payload: Any, path: Path) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    partial = _partial_path(path)
    try:
        with partial.open("w", encoding="utf-8") as handle:
            json.dump(payload, handle, indent=2, ensure_ascii=False)
            handle.write("\n")
            handle.flush()
            os.fsync(handle.fileno())
        os.replace(partial, path)
        directory_fd = os.open(path.parent, os.O_RDONLY)
        try:
            os.fsync(directory_fd)
        finally:
            os.close(directory_fd)
    finally:
        partial.unlink(missing_ok=True)


def snapshot_to_cpu(value: Any) -> Any:
    if isinstance(value, torch.Tensor):
        return value.detach().to(device="cpu").contiguous().clone()
    if isinstance(value, dict):
        return {key: snapshot_to_cpu(item) for key, item in value.items()}
    if isinstance(value, list):
        return [snapshot_to_cpu(item) for item in value]
    if isinstance(value, tuple):
        return tuple(snapshot_to_cpu(item) for item in value)
    return value


def write_step_checkpoint(
    directory: Path,
    *,
    step_index: int,
    total_steps: int,
    block_state,
    metadata: dict[str, Any],
) -> tuple[Path, Path]:
    if not 0 <= step_index < total_steps:
        raise ValueError(f"invalid checkpoint step {step_index} for total {total_steps}")
    completed_step = step_index + 1
    stem = f"step-{completed_step:03d}-of-{total_steps:03d}"
    state = snapshot_to_cpu(block_state.as_dict())
    for name in SPENT_PREDICTIONS:
        state.pop(name, None)
    payload = {
        "schema_version": 1,
        "completed_step": completed_step,
        "resume_step_index": completed_step,
        "total_steps": total_steps,
        "metadata": dict(metadata),
        "state": state,
    }
    checkpoint = directory / f"{stem}.pt"
    manifest = directory / f"{stem}.json"
    atomic_torch_save(payload, checkpoint)
    atomic_json_write(
        {
            "status": "checkpointed",
            "completed_step": completed_step,
            "resume_step_index": completed_step,
            "total_steps": total_steps,
            "checkpoint": checkpoint.name,
            **metadata,
        },
        manifest,
    )
    atomic_json_write(
        {
            "status": "checkpointed",
            "completed_step": completed_step,
            "resume_step_index": completed_step,
            "total_steps": total_steps,
            "checkpoint": checkpoint.name,
            "manifest": manifest.name,
            **metadata,
        },
        directory / "latest.json",
    )
    return checkpoint, manifest


def install_loop_checkpointing(
    loop_wrapper_cls,
    directory: Path,
    *,
    metadata: dict[str, Any],
    stop_after_steps: int | None = None,
):
    if stop_after_steps is not None and stop_after_steps < 1:
        raise ValueError("stop_after_steps must be positive")
    original_call = loop_wrapper_cls.__call__

    @torch.no_grad()
    def checkpointed_call(self, components, state):
        block_state = self.get_block_state(state)
        total_steps = len(block_state.timesteps)
        with self.progress_bar(total=total_steps) as progress_bar:
            for step_index, timestep in enumerate(block_state.timesteps):
                components, block_state = self.loop_step(
                    components,
                    block_state,
                    i=step_index,
                    t=timestep,
                )
                write_step_checkpoint(
                    directory,
                    step_index=step_index,
                    total_steps=total_steps,
                    block_state=block_state,
                    metadata=metadata,
                )
                progress_bar.update()
                completed_steps = step_index + 1
                if stop_after_steps is not None and completed_steps >= stop_after_steps:
                    self.set_block_state(state, block_state)
                    raise DenoiseCheckpointStop(
                        completed_steps=completed_steps,
                        total_steps=total_steps,
                    )
        self.set_block_state(state, block_state)
        return components, state

    loop_wrapper_cls.__call__ = checkpointed_call
    return original_call