File size: 7,464 Bytes
6daffc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Load the dflow_2b denoiser and text adapter weights from a release directory.

A release directory holds ``model.safetensors`` (every weight, public names,
see ``precision``), ``config.json`` (architecture constants) and
``release.json`` (version and counts). Loading checks, before any weight
reaches a module: the config against this package's architecture, the
version, the tensor and parameter counts, every name (none missing, none
extra), every shape and every storage dtype. The denoiser then loads with
``strict=True``.

Public names: ``denoiser.<module path>`` for the denoiser (``DFlowModel``'s
own ``state_dict`` names, e.g. ``denoiser.prefix_layers.0.attn.qkv.weight``)
and ``text_adapter.<key>`` for the text adapter (its keys come from the text
adapter module, ``text_conditioning.TEXT_ADAPTER_KEY_MAP``).
"""

from __future__ import annotations

import json
from pathlib import Path

import torch
from safetensors.torch import load_file
from torch import Tensor

from .configuration import DFLOW_2B_CONFIG, DFlowConfig, read_denoiser_config
from .modeling import DFlowModel
from .precision import (
    DENOISER_PREFIX,
    TEXT_ADAPTER_PREFIX,
    StoredDType,
    denoiser_storage_dtype,
    text_adapter_storage_dtypes,
)
from .version import MODEL_VERSION

MODEL_FILENAME = "model.safetensors"
CONFIG_FILENAME = "config.json"
RELEASE_FILENAME = "release.json"


def expected_denoiser_shapes(config: DFlowConfig) -> dict[str, tuple[int, ...]]:
    """Public name -> shape of every denoiser tensor of ``config``."""

    with torch.device("meta"):
        model = DFlowModel(config)
    return {
        DENOISER_PREFIX + name: tuple(tensor.shape)
        for name, tensor in model.state_dict().items()
    }


def expected_storage(config: DFlowConfig) -> dict[str, StoredDType]:
    """Public name -> storage dtype of every tensor in ``model.safetensors``."""

    storage = {
        name: denoiser_storage_dtype(name) for name in expected_denoiser_shapes(config)
    }
    storage.update(text_adapter_storage_dtypes())
    return storage


def validate_public_tensors(tensors: dict[str, Tensor], config: DFlowConfig) -> None:
    """Check names, denoiser shapes and storage dtypes of a public weight file.

    Raises:
        ValueError: If a name is missing or unexpected, a denoiser shape
            differs from ``config``'s, or a dtype differs from the policy.
    """

    storage = expected_storage(config)
    missing = sorted(set(storage) - set(tensors))
    unexpected = sorted(set(tensors) - set(storage))
    if missing or unexpected:
        raise ValueError(
            f"{MODEL_FILENAME} does not match this package: missing={missing}, "
            f"unexpected={unexpected}"
        )
    for name, shape in expected_denoiser_shapes(config).items():
        if tuple(tensors[name].shape) != shape:
            raise ValueError(
                f"{name} has shape {tuple(tensors[name].shape)}; expected {shape}"
            )
    for name, stored in storage.items():
        if tensors[name].dtype != stored.torch_dtype:
            raise ValueError(
                f"{name} is stored as {tensors[name].dtype}; the storage policy "
                f"requires {stored.torch_dtype}"
            )


def _validate_release_metadata(path: Path, tensors: dict[str, Tensor]) -> None:
    """Check ``release.json``'s version and counts against the weight file.

    Raises:
        FileNotFoundError: If ``release.json`` is missing.
        ValueError: If the version or a count differs.
    """

    if not path.is_file():
        raise FileNotFoundError(f"Missing release metadata: {path}")
    match json.loads(path.read_text(encoding="utf-8")):
        case {
            "version": str() as version,
            "tensor_count": int() as tensor_count,
            "parameter_count": int() as parameter_count,
        }:
            pass
        case _:
            raise ValueError(
                f"{path} must hold version, tensor_count and parameter_count"
            )
    if version != MODEL_VERSION:
        raise ValueError(
            f"{path} is release {version}; this package loads {MODEL_VERSION}"
        )
    found_parameters = sum(tensor.numel() for tensor in tensors.values())
    if (tensor_count, parameter_count) != (len(tensors), found_parameters):
        raise ValueError(
            f"{MODEL_FILENAME} holds {len(tensors)} tensors and {found_parameters} "
            f"parameters; {RELEASE_FILENAME} declares {tensor_count} and "
            f"{parameter_count}"
        )


def read_release_tensors(directory: Path, config: DFlowConfig) -> dict[str, Tensor]:
    """Read and validate every tensor of a release directory (on the CPU).

    Raises:
        FileNotFoundError: If a release file is missing.
        ValueError: If the config, metadata or tensors do not match.
    """

    read_denoiser_config(directory / CONFIG_FILENAME, config)
    weights = directory / MODEL_FILENAME
    if not weights.is_file():
        raise FileNotFoundError(f"Missing weights: {weights}")
    tensors = load_file(weights, device="cpu")
    _validate_release_metadata(directory / RELEASE_FILENAME, tensors)
    validate_public_tensors(tensors, config)
    return tensors


def _with_prefix(tensors: dict[str, Tensor], prefix: str) -> dict[str, Tensor]:
    """The entries of ``tensors`` under ``prefix``, with the prefix removed."""

    return {
        name.removeprefix(prefix): tensor
        for name, tensor in tensors.items()
        if name.startswith(prefix)
    }


def load_dflow_denoiser(
    directory: Path,
    *,
    device: torch.device,
    compile_model: bool,
    config: DFlowConfig = DFLOW_2B_CONFIG,
) -> DFlowModel:
    """Build the denoiser and load a release directory's weights.

    Weights keep their storage dtypes on the device (bfloat16 with FP32
    islands). With ``compile_model`` every block is compiled
    (``DFlowModel.compile_blocks(dynamic=True)``); graphs build on first use.

    Args:
        directory: Release directory (``model.safetensors``, ``config.json``,
            ``release.json``).
        device: CUDA device for the model.
        compile_model: Compile the blocks, or run eagerly.
        config: The architecture to expect (the released model's by default).

    Returns:
        The eval-mode denoiser without gradients.

    Raises:
        FileNotFoundError: If a release file is missing.
        ValueError: If ``device`` is not CUDA or the release does not match.
    """

    if device.type != "cuda":
        raise ValueError(f"dflow runs on CUDA (bfloat16 autocast), got {device}")
    tensors = read_release_tensors(directory, config)
    with torch.device("meta"):
        model = DFlowModel(config)
    model.load_state_dict(
        _with_prefix(tensors, DENOISER_PREFIX), strict=True, assign=True
    )
    model.to(device=device)
    model.eval()
    model.requires_grad_(False)
    if compile_model:
        model.compile_blocks(dynamic=True)
    return model


def load_text_adapter_tensors(
    directory: Path, *, config: DFlowConfig = DFLOW_2B_CONFIG
) -> dict[str, Tensor]:
    """The text adapter's validated tensors, keyed relative to the adapter
    module (the ``text_adapter.`` prefix removed), on the CPU.

    Raises:
        FileNotFoundError: If a release file is missing.
        ValueError: If the release does not match.
    """

    return _with_prefix(read_release_tensors(directory, config), TEXT_ADAPTER_PREFIX)