File size: 6,997 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
211
"""The dinac3_96 autoencoder that maps between images and dflow latents.

dflow_2b generates whitened dinac3_96 latents: 96 channels at 1/16 of the
image resolution. The autoencoder is a separate repository; it is
downloaded at a pinned commit and its own Python package (``dinac3/`` in that
repository) is imported from the download under a private module name, so it
never clashes with an installed copy.
"""

from __future__ import annotations

import importlib.util
import sys
import threading
from pathlib import Path
from types import ModuleType
from typing import Protocol, cast

import torch
from huggingface_hub import snapshot_download
from torch import Tensor

VAE_REPOSITORY = "data-archetype/dinac3_96"
# dinac3_96 v1.0 with its final documentation commit.
VAE_REVISION = "d0ca36daa3e429a510ba465a5f7f8139c66d2f82"
LATENT_CHANNELS = 96
LATENT_STRIDE = 16
_PACKAGE_DIR = "dinac3"
_PRIVATE_MODULE = "_dflow_dinac3"
_DOWNLOAD_PATTERNS = ("config.json", "model.safetensors", f"{_PACKAGE_DIR}/*.py")
_IMPORT_LOCK = threading.Lock()


class _Dinac3Model(Protocol):
    """The part of ``dinac3.Dinac3`` dflow uses."""

    def encode(self, images: Tensor) -> Tensor:
        """Whitened float32 latents of ``[-1, 1]`` images."""
        ...

    def decode(self, latents: Tensor, height: int, width: int) -> Tensor:
        """Float32 RGB (unclamped, about ``[-1, 1]``) of whitened latents."""
        ...


class _Dinac3Factory(Protocol):
    """``dinac3.Dinac3.from_pretrained``."""

    def from_pretrained(
        self,
        path_or_repo: str | Path,
        *,
        device: torch.device | str,
        dtype: torch.dtype,
        compile_encoder: bool,
        compile_decoder: bool,
        revision: str | None,
    ) -> _Dinac3Model:
        """Load and prepare the autoencoder."""
        ...


class DFlowVae:
    """Encode images to and decode them from dflow latents.

    Images are ``[B, 3, H, W]`` float32 RGB in ``[-1, 1]`` with H and W
    multiples of 16; latents are ``[B, 96, H/16, W/16]`` float32, whitened.
    """

    def __init__(self, model: _Dinac3Model, *, revision: str) -> None:
        """Wrap a loaded autoencoder; ``revision`` records the commit it came from."""

        self.model = model
        self.revision = revision

    @classmethod
    def from_pretrained(
        cls,
        *,
        device: torch.device,
        compile: bool,
        local_dir: Path | None = None,
        revision: str = VAE_REVISION,
        cache_dir: Path | None = None,
    ) -> DFlowVae:
        """Download (or reuse) the pinned dinac3_96 release and load it in bf16.

        Args:
            device: CUDA device.
            compile: ``torch.compile`` the encoder and decoder (dynamic shapes).
            local_dir: An existing local copy of the repository to load from
                instead of the Hub (offline use).
            revision: Hub commit to download; the pinned release by default.
            cache_dir: Hugging Face cache directory, or None for the default.

        Raises:
            FileNotFoundError: If ``local_dir`` lacks the package or weights.
            ValueError: On a non-CUDA device.
        """

        if device.type != "cuda":
            raise ValueError(f"dflow inference needs a CUDA device, got {device}.")
        root = (
            local_dir
            if local_dir is not None
            else Path(
                snapshot_download(
                    VAE_REPOSITORY,
                    revision=revision,
                    allow_patterns=list(_DOWNLOAD_PATTERNS),
                    cache_dir=None if cache_dir is None else str(cache_dir),
                )
            )
        )
        factory = _import_factory(root)
        model = factory.from_pretrained(
            root,
            device=device,
            dtype=torch.bfloat16,
            compile_encoder=compile,
            compile_decoder=compile,
            revision=None,
        )
        return cls(model, revision=revision)

    def decode(self, latents: Tensor) -> Tensor:
        """Decode whitened latents to float32 RGB ``[B, 3, 16h, 16w]``, unclamped."""

        height = int(latents.shape[-2]) * LATENT_STRIDE
        width = int(latents.shape[-1]) * LATENT_STRIDE
        return self.model.decode(latents.float(), height, width)

    def encode(self, images: Tensor) -> Tensor:
        """Encode ``[-1, 1]`` RGB images to whitened float32 latents.

        Raises:
            ValueError: If the image height or width is not a multiple of 16.
        """

        height, width = int(images.shape[-2]), int(images.shape[-1])
        if height % LATENT_STRIDE or width % LATENT_STRIDE:
            raise ValueError(
                f"Image size {width}x{height} must be a multiple of {LATENT_STRIDE} "
                "on both sides."
            )
        return self.model.encode(images.float())


def _import_factory(root: Path) -> _Dinac3Factory:
    """Import ``root/dinac3`` under a private module name and return ``Dinac3``.

    Raises:
        FileNotFoundError: If the package or weights are missing under ``root``.
    """

    package_init = root / _PACKAGE_DIR / "__init__.py"
    missing = [
        path
        for path in (package_init, root / "config.json", root / "model.safetensors")
        if not path.is_file()
    ]
    if missing:
        raise FileNotFoundError(
            f"Incomplete {VAE_REPOSITORY} download at {root}: missing {missing}."
        )
    with _IMPORT_LOCK:
        module = sys.modules.get(_PRIVATE_MODULE)
        if module is None or Path(module.__file__ or "") != package_init:
            module = _load_package(package_init)
    return cast("_Dinac3Factory", module.Dinac3)


def _load_package(package_init: Path) -> ModuleType:
    """Execute a package from its ``__init__.py`` under the private name,
    replacing any earlier copy and its submodules.

    Raises:
        ImportError: If Python cannot build a module spec for the file, or the
            package fails to import (a missing dependency). The partly
            imported modules are removed first, so a retry imports afresh.
    """

    spec = importlib.util.spec_from_file_location(
        _PRIVATE_MODULE,
        package_init,
        submodule_search_locations=[str(package_init.parent)],
    )
    if spec is None or spec.loader is None:
        raise ImportError(f"Cannot import the autoencoder package at {package_init}.")
    _drop_private_modules()
    module = importlib.util.module_from_spec(spec)
    sys.modules[_PRIVATE_MODULE] = module
    try:
        spec.loader.exec_module(module)
    except ImportError:
        _drop_private_modules()
        raise
    return module


def _drop_private_modules() -> None:
    """Remove the private autoencoder package and its submodules from
    ``sys.modules``."""

    for name in [
        name
        for name in sys.modules
        if name == _PRIVATE_MODULE or name.startswith(f"{_PRIVATE_MODULE}.")
    ]:
        del sys.modules[name]