File size: 6,005 Bytes
d09dc68
39c903a
d09dc68
 
 
 
 
 
 
 
 
 
 
39c903a
d09dc68
 
 
 
 
 
 
 
 
 
 
880c288
d09dc68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
880c288
 
d09dc68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
# Model for SupraMNiST-IMG-200k

from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Optional

import torch
from diffusers import UNet2DConditionModel
from transformers import PreTrainedModel
from transformers.utils import ModelOutput

from .configuration import DigitDiffusionConfig


@dataclass
class DigitDiffusionOutput(ModelOutput):
    sample: torch.FloatTensor | None = None


class DigitDiffusionModel(PreTrainedModel):
    config_class = DigitDiffusionConfig
    base_model_prefix = "unet"
    main_input_name = "noisy_images"
    all_tied_weights_keys = {}

    def __init__(self, config: DigitDiffusionConfig) -> None:
        super().__init__(config)

        block_count = len(config.block_out_channels)

        self.unet = UNet2DConditionModel(
            sample_size=config.sample_size,
            in_channels=config.in_channels,
            out_channels=config.out_channels,
            layers_per_block=config.layers_per_block,
            block_out_channels=tuple(config.block_out_channels),
            down_block_types=("DownBlock2D",) * block_count,
            up_block_types=("UpBlock2D",) * block_count,
            mid_block_type="UNetMidBlock2D",
            norm_num_groups=config.norm_num_groups,
            num_class_embeds=config.num_classes,
            cross_attention_dim=config.cross_attention_dim,
            class_embed_type=config.class_embed_type,
        )

        self.post_init()

    def _init_weights(self, module):
        # Diffusers initializes the UNet internally, so there is nothing extra
        # to initialize here.
        return

    def _make_dummy_context(
        self,
        batch_size: int,
        device: torch.device,
        dtype: torch.dtype,
    ) -> torch.Tensor:
        return torch.zeros(
            batch_size,
            1,
            self.config.cross_attention_dim,
            device=device,
            dtype=dtype,
        )

    def _normalize_inputs(
        self,
        noisy_images: Optional[torch.Tensor] = None,
        timesteps: Optional[torch.Tensor | int] = None,
        sample: Optional[torch.Tensor] = None,
        timestep: Optional[torch.Tensor | int] = None,
    ) -> tuple[torch.Tensor, torch.Tensor]:
        if noisy_images is None:
            noisy_images = sample
        if timesteps is None:
            timesteps = timestep

        if noisy_images is None:
            raise ValueError("Either `noisy_images` or `sample` must be provided.")
        if timesteps is None:
            raise ValueError("Either `timesteps` or `timestep` must be provided.")

        if not torch.is_tensor(timesteps):
            timesteps = torch.tensor(
                timesteps,
                device=noisy_images.device,
                dtype=torch.long,
            )
        if timesteps.ndim == 0:
            timesteps = timesteps.expand(noisy_images.shape[0])
        elif timesteps.shape[0] != noisy_images.shape[0]:
            timesteps = timesteps.reshape(-1)
            if timesteps.numel() == 1:
                timesteps = timesteps.expand(noisy_images.shape[0])
            elif timesteps.shape[0] != noisy_images.shape[0]:
                raise ValueError(
                    "Timesteps must be a scalar, a batch-sized tensor, or a single-value tensor."
                )

        return noisy_images, timesteps.to(device=noisy_images.device, dtype=torch.long)

    def forward(
        self,
        noisy_images: Optional[torch.Tensor] = None,
        timesteps: Optional[torch.Tensor | int] = None,
        class_labels: Optional[torch.Tensor] = None,
        sample: Optional[torch.Tensor] = None,
        timestep: Optional[torch.Tensor | int] = None,
        encoder_hidden_states: Optional[torch.Tensor] = None,
        return_dict: bool = True,
        **kwargs: Any,
    ):
        noisy_images, timesteps = self._normalize_inputs(
            noisy_images=noisy_images,
            timesteps=timesteps,
            sample=sample,
            timestep=timestep,
        )

        batch_size = noisy_images.shape[0]
        if class_labels is None:
            class_labels = torch.zeros(
                batch_size,
                device=noisy_images.device,
                dtype=torch.long,
            )
        else:
            class_labels = class_labels.to(device=noisy_images.device, dtype=torch.long)

        if encoder_hidden_states is None:
            encoder_hidden_states = self._make_dummy_context(
                batch_size=batch_size,
                device=noisy_images.device,
                dtype=noisy_images.dtype,
            )

        noise_pred = self.unet(
            sample=noisy_images,
            timestep=timesteps,
            encoder_hidden_states=encoder_hidden_states,
            class_labels=class_labels,
            return_dict=True,
            **kwargs,
        ).sample

        if return_dict:
            return DigitDiffusionOutput(sample=noise_pred)
        return (noise_pred,)

    def load_state_dict(self, state_dict, strict: bool = True, assign: bool = False):
        if state_dict:
            keys = list(state_dict.keys())
            has_prefixed = any(k.startswith("unet.") for k in keys)
            has_plain_unet = any(
                k.startswith(
                    (
                        "conv_in.",
                        "conv_norm_out.",
                        "conv_out.",
                        "time_embedding.",
                        "class_embedding.",
                        "down_blocks.",
                        "up_blocks.",
                        "mid_block.",
                    )
                )
                for k in keys
            )

            if has_plain_unet and not has_prefixed:
                state_dict = {f"unet.{k}": v for k, v in state_dict.items()}

        return super().load_state_dict(state_dict, strict=strict, assign=assign)


DigitDiffusionModel.register_for_auto_class("AutoModel")