File size: 10,883 Bytes
e80c177
e4e13db
e80c177
 
 
b4f7900
e80c177
 
 
 
 
 
a8cf307
e80c177
 
 
 
0b0d11b
 
e80c177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4e13db
 
 
 
 
 
 
1732305
 
6a798c0
e4e13db
 
 
 
 
 
 
a8cf307
6a798c0
e4e13db
 
 
 
 
 
 
 
 
 
 
 
 
 
e80c177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b0d11b
e80c177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b0d11b
e80c177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4e13db
e80c177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89b9678
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import random
from tqdm import tqdm
from typing import Callable, Dict, List, Optional

import torch
from diffusers import DiffusionPipeline
from diffusers.configuration_utils import ConfigMixin


class SuperDiffPipeline(DiffusionPipeline, ConfigMixin):
    """SuperDiffPipeline."""

    def __init__(self, unet: Callable, vae: Callable, text_encoder: Callable, scheduler: Callable, tokenizer: Callable) -> None:
        """__init__.

        Parameters
        ----------
        unet : Callable
            unet
        vae : Callable
            vae
        text_encoder : Callable
            text_encoder
        scheduler : Callable
            scheduler
        tokenizer : Callable
            tokenizer
        kwargs :
            kwargs

        Returns
        -------
        None

        """
        super().__init__()
        # Register additional parameters for flexibility
        # Explicitly assign required components
        #self.unet = unet
        #self.vae = vae
        #self.text_encoder = text_encoder
        #self.tokenizer = tokenizer
        #self.scheduler = scheduler

        device = "cuda" if torch.cuda.is_available() else "cpu"

        vae.to(device)
        unet.to(device)
        text_encoder.to(device)
        self.register_modules(unet=unet, 
                              scheduler=scheduler, 
                              vae=vae, 
                              text_encoder=text_encoder,
                              tokenizer=tokenizer,)


        #self.register_to_config(
        #    vae=vae.__class__.__name__,
        #    scheduler=scheduler.__class__.__name__,
        #    tokenizer=tokenizer.__class__.__name__,
        #    unet=unet.__class__.__name__,
        #    text_encoder=text_encoder.__class__.__name__,
        #    device=device,
        #    batch_size=None,
        #    num_inference_steps=None,
        #    guidance_scale=None,
        #    lift=None,
        #    seed=None,
        #)

    @torch.no_grad
    def get_batch(self, latents: Callable, nrow: int, ncol: int) -> Callable:
        """get_batch.

        Parameters
        ----------
        latents : Callable
            latents
        nrow : int
            nrow
        ncol : int
            ncol

        Returns
        -------
        Callable

        """
        image = self.vae.decode(
            latents / self.vae.config.scaling_factor, return_dict=False
        )[0]
        image = (image / 2 + 0.5).clamp(0, 1).squeeze()
        if len(image.shape) < 4:
            image = image.unsqueeze(0)
        image = (image.permute(0, 2, 3, 1) * 255).to(torch.uint8)
        return image

    @torch.no_grad
    def get_text_embedding(self, prompt: str) -> Callable:
        """get_text_embedding.

        Parameters
        ----------
        prompt : str
            prompt

        Returns
        -------
        Callable

        """
        text_input = self.tokenizer(
            prompt,
            padding="max_length",
            max_length=self.tokenizer.model_max_length,
            truncation=True,
            return_tensors="pt",
        )
        return self.text_encoder(text_input.input_ids.to(self.device))[0]

    @torch.no_grad
    def get_vel(self, t: float, sigma: float, latents: Callable, embeddings: Callable):
        """get_vel.

        Parameters
        ----------
        t : float
            t
        sigma : float
            sigma
        latents : Callable
            latents
        embeddings : Callable
            embeddings
        """
        def v(_x, _e): return self.unet(
            _x / ((sigma**2 + 1) ** 0.5), t, encoder_hidden_states=_e
        ).sample
        embeds = torch.cat(embeddings)
        latent_input = latents
        vel = v(latent_input, embeds)
        return vel

    def preprocess(
        self,
        prompt_1: str,
        prompt_2: str,
        seed: int = None,
        num_inference_steps: int = 1000,
        batch_size: int = 1,
        lift: int = 0.0,
        height: int = 512,
        width: int = 512,
        guidance_scale: int = 7.5,
    ) -> Callable:
        """preprocess.

        Parameters
        ----------
        prompt_1 : str
            prompt_1
        prompt_2 : str
            prompt_2
        seed : int
            seed
        num_inference_steps : int
            num_inference_steps
        batch_size : int
            batch_size
        lift : int
            lift
        height : int
            height
        width : int
            width
        guidance_scale : int
            guidance_scale

        Returns
        -------
        Callable

        """
        # Tokenize the input
        self.batch_size = batch_size
        self.num_inference_steps = num_inference_steps
        self.guidance_scale = guidance_scale
        self.lift = lift
        self.seed = seed
        if self.seed is None:
            self.seed = random.randint(0, 2**32 - 1)
        obj_prompt = [prompt_1]
        bg_prompt = [prompt_2]
        obj_embeddings = self.get_text_embedding(obj_prompt * batch_size)
        bg_embeddings = self.get_text_embedding(bg_prompt * batch_size)

        uncond_embeddings = self.get_text_embedding([""] * batch_size)

        generator = torch.cuda.manual_seed(
            self.seed
        )  # Seed generator to create the initial latent noise
        latents = torch.randn(
            (batch_size, self.unet.config.in_channels, height // 8, width // 8),
            generator=generator,
            device=self.device,
        )

        latents_og = latents.clone().detach()
        latents_uncond_og = latents.clone().detach()

        self.scheduler.set_timesteps(num_inference_steps)
        latents = latents * self.scheduler.init_noise_sigma

        latents_uncond = latents.clone().detach()
        return {
            "latents": latents,
            "obj_embeddings": obj_embeddings,
            "uncond_embeddings": uncond_embeddings,
            "bg_embeddings": bg_embeddings,
        }

    def _forward(self, model_inputs: Dict) -> Callable:
        """_forward.

        Parameters
        ----------
        model_inputs : Dict
            model_inputs

        Returns
        -------
        Callable

        """
        latents = model_inputs["latents"]
        obj_embeddings = model_inputs["obj_embeddings"]
        uncond_embeddings = model_inputs["uncond_embeddings"]
        bg_embeddings = model_inputs["bg_embeddings"]

        kappa = 0.5 * torch.ones(
            (self.num_inference_steps + 1, self.batch_size), device=self.device
        )
        ll_obj = torch.ones(
            (self.num_inference_steps + 1, self.batch_size), device=self.device
        )
        ll_bg = torch.ones(
            (self.num_inference_steps + 1, self.batch_size), device=self.device
        )
        ll_uncond = torch.ones(
            (self.num_inference_steps + 1, self.batch_size), device=self.device
        )
        with torch.no_grad():
            for i, t in tqdm(enumerate(self.scheduler.timesteps)):
                dsigma = self.scheduler.sigmas[i +
                                               1] - self.scheduler.sigmas[i]
                sigma = self.scheduler.sigmas[i]
                vel_obj = self.get_vel(t, sigma, latents, [obj_embeddings])
                vel_uncond = self.get_vel(
                    t, sigma, latents, [uncond_embeddings])

                vel_bg = self.get_vel(t, sigma, latents, [bg_embeddings])
                noise = torch.sqrt(2 * torch.abs(dsigma) * sigma) * torch.randn_like(
                    latents
                )

                dx_ind = (
                    2
                    * dsigma
                    * (vel_uncond + self.guidance_scale * (vel_bg - vel_uncond))
                    + noise
                )
                kappa[i + 1] = (
                    (torch.abs(dsigma) * (vel_bg - vel_obj) * (vel_bg + vel_obj)).sum(
                        (1, 2, 3)
                    )
                    - (dx_ind * ((vel_obj - vel_bg))).sum((1, 2, 3))
                    + sigma * self.lift / self.num_inference_steps
                )
                kappa[i + 1] /= (
                    2
                    * dsigma
                    * self.guidance_scale
                    * ((vel_obj - vel_bg) ** 2).sum((1, 2, 3))
                )

                vf = vel_uncond + self.guidance_scale * (
                    (vel_bg - vel_uncond)
                    + kappa[i + 1][:, None, None, None] * (vel_obj - vel_bg)
                )
                dx = 2 * dsigma * vf + noise
                latents += dx

                ll_obj[i + 1] = ll_obj[i] + (
                    -torch.abs(dsigma) / sigma * (vel_obj) ** 2
                    - (dx * (vel_obj / sigma))
                ).sum((1, 2, 3))
                ll_bg[i + 1] = ll_bg[i] + (
                    -torch.abs(dsigma) / sigma * (vel_bg) ** 2 -
                    (dx * (vel_bg / sigma))
                ).sum((1, 2, 3))

        return latents

    def postprocess(self, latents: Callable) -> Callable:
        """postprocess.

        Parameters
        ----------
        latents : Callable
            latents

        Returns
        -------
        Callable

        """
        image = self.get_batch(latents, 1, self.batch_size)
        # Ensure the shape is (height, width, 3)
        assert image.shape[-1] == 3  # Handle grayscale or invalid shapes

        # Convert to uint8 if not already
        image = image.to(torch.uint8)  # Ensure it's uint8 for PIL

        return image

    def __call__(
        self,
        prompt_1: str,
        prompt_2: str,
        seed: int = None,
        num_inference_steps: int = 1000,
        batch_size: int = 1,
        lift: int = 0.0,
        height: int = 512,
        width: int = 512,
        guidance_scale: int = 7.5,
    ) -> Callable:
        """__call__.

        Parameters
        ----------
        prompt_1 : str
            prompt_1
        prompt_2 : str
            prompt_2
        seed : int
            seed
        num_inference_steps : int
            num_inference_steps
        batch_size : int
            batch_size
        lift : int
            lift
        height : int
            height
        width : int
            width
        guidance_scale : int
            guidance_scale

        Returns
        -------
        Callable

        """
        # Preprocess inputs
        model_inputs = self.preprocess(
            prompt_1,
            prompt_2,
            seed,
            num_inference_steps,
            batch_size,
            lift,
            height,
            width,
            guidance_scale,
        )

        # Forward pass through the pipeline
        latents = self._forward(model_inputs)

        # Postprocess to generate the final output
        images = self.postprocess(latents)
        return images