pipeline.py 20.5 KB
Newer Older
chenpangpang's avatar
chenpangpang committed
1
2
3
from typing import Any, Callable, Dict, List, Optional, Union, Tuple
from collections import OrderedDict
import os
4
import PIL
chenpangpang's avatar
chenpangpang committed
5
import numpy as np 
6
7

import torch
chenpangpang's avatar
chenpangpang committed
8
from torchvision import transforms as T
9
10
11

from safetensors import safe_open
from huggingface_hub.utils import validate_hf_hub_args
chenpangpang's avatar
chenpangpang committed
12
from transformers import CLIPImageProcessor, CLIPTokenizer
13
14
15
16
from diffusers import StableDiffusionXLPipeline
from diffusers.pipelines.stable_diffusion_xl import StableDiffusionXLPipelineOutput
from diffusers.utils import (
    _get_model_file,
chenpangpang's avatar
chenpangpang committed
17
18
    is_transformers_available,
    logging,
19
20
)

chenpangpang's avatar
chenpangpang committed
21
from model import PhotoMakerIDEncoder
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

PipelineImageInput = Union[
    PIL.Image.Image,
    torch.FloatTensor,
    List[PIL.Image.Image],
    List[torch.FloatTensor],
]


class PhotoMakerStableDiffusionXLPipeline(StableDiffusionXLPipeline):
    @validate_hf_hub_args
    def load_photomaker_adapter(
        self,
        pretrained_model_name_or_path_or_dict: Union[str, Dict[str, torch.Tensor]],
        weight_name: str,
        subfolder: str = '',
        trigger_word: str = 'img',
        **kwargs,
    ):
        """
chenpangpang's avatar
chenpangpang committed
42
        #TODO
43
44
45
46
47
48
49
50
51
52
        Parameters:
            pretrained_model_name_or_path_or_dict (`str` or `os.PathLike` or `dict`):
                Can be either:
                    - A string, the *model id* (for example `google/ddpm-celebahq-256`) of a pretrained model hosted on
                      the Hub.
                    - A path to a *directory* (for example `./my_model_directory`) containing the model weights saved
                      with [`ModelMixin.save_pretrained`].
                    - A [torch state
                      dict](https://pytorch.org/tutorials/beginner/saving_loading_models.html#what-is-a-state-dict).
            weight_name (`str`):
chenpangpang's avatar
chenpangpang committed
53
                The subfolder location of a model file within a larger model repository on the Hub or locally.
54
55
56
            subfolder (`str`, defaults to `""`):
                The subfolder location of a model file within a larger model repository on the Hub or locally.
            trigger_word (`str`, *optional*, defaults to `"img"`):
chenpangpang's avatar
chenpangpang committed
57
                The subfolder location of a model file within a larger model repository on the Hub or locally.            
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
        """

        # Load the main state dict first.
        cache_dir = kwargs.pop("cache_dir", None)
        force_download = kwargs.pop("force_download", False)
        resume_download = kwargs.pop("resume_download", False)
        proxies = kwargs.pop("proxies", None)
        local_files_only = kwargs.pop("local_files_only", None)
        token = kwargs.pop("token", None)
        revision = kwargs.pop("revision", None)

        user_agent = {
            "file_type": "attn_procs_weights",
            "framework": "pytorch",
        }

        if not isinstance(pretrained_model_name_or_path_or_dict, dict):
            model_file = _get_model_file(
                pretrained_model_name_or_path_or_dict,
                weights_name=weight_name,
                cache_dir=cache_dir,
                force_download=force_download,
                resume_download=resume_download,
                proxies=proxies,
                local_files_only=local_files_only,
                token=token,
                revision=revision,
                subfolder=subfolder,
                user_agent=user_agent,
            )
            if weight_name.endswith(".safetensors"):
                state_dict = {"id_encoder": {}, "lora_weights": {}}
                with safe_open(model_file, framework="pt", device="cpu") as f:
                    for key in f.keys():
                        if key.startswith("id_encoder."):
                            state_dict["id_encoder"][key.replace("id_encoder.", "")] = f.get_tensor(key)
                        elif key.startswith("lora_weights."):
                            state_dict["lora_weights"][key.replace("lora_weights.", "")] = f.get_tensor(key)
            else:
                state_dict = torch.load(model_file, map_location="cpu")
        else:
            state_dict = pretrained_model_name_or_path_or_dict

        keys = list(state_dict.keys())
        if keys != ["id_encoder", "lora_weights"]:
            raise ValueError("Required keys are (`id_encoder` and `lora_weights`) missing from the state dict.")

        self.trigger_word = trigger_word
        # load finetuned CLIP image encoder and fuse module here if it has not been registered to the pipeline yet
chenpangpang's avatar
chenpangpang committed
107
108
        print(f"Loading PhotoMaker components [1] id_encoder from [{pretrained_model_name_or_path_or_dict}]...")
        id_encoder = PhotoMakerIDEncoder()
109
110
111
        id_encoder.load_state_dict(state_dict["id_encoder"], strict=True)
        id_encoder = id_encoder.to(self.device, dtype=self.unet.dtype)    
        self.id_encoder = id_encoder
chenpangpang's avatar
chenpangpang committed
112
        self.id_image_processor = CLIPImageProcessor()
113
114

        # load lora into models
chenpangpang's avatar
chenpangpang committed
115
        print(f"Loading PhotoMaker components [2] lora_weights from [{pretrained_model_name_or_path_or_dict}]")
116
117
118
119
120
121
122
123
124
125
126
127
128
        self.load_lora_weights(state_dict["lora_weights"], adapter_name="photomaker")

        # Add trigger word token
        if self.tokenizer is not None: 
            self.tokenizer.add_tokens([self.trigger_word], special_tokens=True)
        
        self.tokenizer_2.add_tokens([self.trigger_word], special_tokens=True)
        

    def encode_prompt_with_trigger_word(
        self,
        prompt: str,
        prompt_2: Optional[str] = None,
Paper99's avatar
Paper99 committed
129
        num_id_images: int = 1,
chenpangpang's avatar
chenpangpang committed
130
131
132
        device: Optional[torch.device] = None,
        prompt_embeds: Optional[torch.FloatTensor] = None,
        pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
133
134
135
136
        class_tokens_mask: Optional[torch.LongTensor] = None,
    ):
        device = device or self._execution_device

chenpangpang's avatar
chenpangpang committed
137
138
139
        if prompt is not None and isinstance(prompt, str):
            batch_size = 1
        elif prompt is not None and isinstance(prompt, list):
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
            batch_size = len(prompt)
        else:
            batch_size = prompt_embeds.shape[0]

        # Find the token id of the trigger word
        image_token_id = self.tokenizer_2.convert_tokens_to_ids(self.trigger_word)

        # Define tokenizers and text encoders
        tokenizers = [self.tokenizer, self.tokenizer_2] if self.tokenizer is not None else [self.tokenizer_2]
        text_encoders = (
            [self.text_encoder, self.text_encoder_2] if self.text_encoder is not None else [self.text_encoder_2]
        )

        if prompt_embeds is None:
            prompt_2 = prompt_2 or prompt
            prompt_embeds_list = []
            prompts = [prompt, prompt_2]
            for prompt, tokenizer, text_encoder in zip(prompts, tokenizers, text_encoders):
chenpangpang's avatar
chenpangpang committed
158
                input_ids = tokenizer.encode(prompt) # TODO: batch encode
159
160
161
                clean_index = 0
                clean_input_ids = []
                class_token_index = []
chenpangpang's avatar
chenpangpang committed
162
163
                # Find out the corrresponding class word token based on the newly added trigger word token
                for i, token_id in enumerate(input_ids):
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
                    if token_id == image_token_id:
                        class_token_index.append(clean_index - 1)
                    else:
                        clean_input_ids.append(token_id)
                        clean_index += 1

                if len(class_token_index) != 1:
                    raise ValueError(
                        f"PhotoMaker currently does not support multiple trigger words in a single prompt.\
                            Trigger word: {self.trigger_word}, Prompt: {prompt}."
                    )
                class_token_index = class_token_index[0]

                # Expand the class word token and corresponding mask
                class_token = clean_input_ids[class_token_index]
chenpangpang's avatar
chenpangpang committed
179
                clean_input_ids = clean_input_ids[:class_token_index] + [class_token] * num_id_images + \
180
181
182
183
184
185
186
187
188
189
190
                    clean_input_ids[class_token_index+1:]                
                    
                # Truncation or padding
                max_len = tokenizer.model_max_length
                if len(clean_input_ids) > max_len:
                    clean_input_ids = clean_input_ids[:max_len]
                else:
                    clean_input_ids = clean_input_ids + [tokenizer.pad_token_id] * (
                        max_len - len(clean_input_ids)
                    )

chenpangpang's avatar
chenpangpang committed
191
                class_tokens_mask = [True if class_token_index <= i < class_token_index+num_id_images else False \
192
193
194
195
                     for i in range(len(clean_input_ids))]
                
                clean_input_ids = torch.tensor(clean_input_ids, dtype=torch.long).unsqueeze(0)
                class_tokens_mask = torch.tensor(class_tokens_mask, dtype=torch.bool).unsqueeze(0)
chenpangpang's avatar
chenpangpang committed
196
197
198
199
200
                
                prompt_embeds = text_encoder(
                    clean_input_ids.to(device),
                    output_hidden_states=True,
                )
201
202
203

                # We are only ALWAYS interested in the pooled output of the final text encoder
                pooled_prompt_embeds = prompt_embeds[0]
chenpangpang's avatar
chenpangpang committed
204
                prompt_embeds = prompt_embeds.hidden_states[-2]
205
206
207
208
209
210
                prompt_embeds_list.append(prompt_embeds)

            prompt_embeds = torch.concat(prompt_embeds_list, dim=-1)

        prompt_embeds = prompt_embeds.to(dtype=self.text_encoder_2.dtype, device=device)
        class_tokens_mask = class_tokens_mask.to(device=device) # TODO: ignoring two-prompt case
Paper99's avatar
Paper99 committed
211

chenpangpang's avatar
chenpangpang committed
212
        return prompt_embeds, pooled_prompt_embeds, class_tokens_mask
Paper99's avatar
Paper99 committed
213

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

    @torch.no_grad()
    def __call__(
        self,
        prompt: Union[str, List[str]] = None,
        prompt_2: Optional[Union[str, List[str]]] = None,
        height: Optional[int] = None,
        width: Optional[int] = None,
        num_inference_steps: int = 50,
        denoising_end: Optional[float] = None,
        guidance_scale: float = 5.0,
        negative_prompt: Optional[Union[str, List[str]]] = None,
        negative_prompt_2: Optional[Union[str, List[str]]] = None,
        num_images_per_prompt: Optional[int] = 1,
        eta: float = 0.0,
        generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
chenpangpang's avatar
chenpangpang committed
230
231
232
233
234
        latents: Optional[torch.FloatTensor] = None,
        prompt_embeds: Optional[torch.FloatTensor] = None,
        negative_prompt_embeds: Optional[torch.FloatTensor] = None,
        pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
        negative_pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
235
236
237
238
239
240
241
        output_type: Optional[str] = "pil",
        return_dict: bool = True,
        cross_attention_kwargs: Optional[Dict[str, Any]] = None,
        guidance_rescale: float = 0.0,
        original_size: Optional[Tuple[int, int]] = None,
        crops_coords_top_left: Tuple[int, int] = (0, 0),
        target_size: Optional[Tuple[int, int]] = None,
chenpangpang's avatar
chenpangpang committed
242
243
        callback: Optional[Callable[[int, int, torch.FloatTensor], None]] = None,
        callback_steps: int = 1,
244
245
246
247
248
        # Added parameters (for PhotoMaker)
        input_id_images: PipelineImageInput = None,
        class_tokens_mask: Optional[torch.LongTensor] = None,
        prompt_embeds_text_only: Optional[torch.FloatTensor] = None,
        pooled_prompt_embeds_text_only: Optional[torch.FloatTensor] = None,
chenpangpang's avatar
chenpangpang committed
249
        start_merge_step: int = 0,
250
    ):
chenpangpang's avatar
chenpangpang committed
251
        # TODO: doc
252
        # 0. Default height and width to unet
chenpangpang's avatar
chenpangpang committed
253
254
        height = height or self.unet.config.sample_size * self.vae_scale_factor
        width = width or self.unet.config.sample_size * self.vae_scale_factor
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

        original_size = original_size or (height, width)
        target_size = target_size or (height, width)

        # 1. Check inputs. Raise error if not correct
        self.check_inputs(
            prompt,
            prompt_2,
            height,
            width,
            callback_steps,
            negative_prompt,
            negative_prompt_2,
            prompt_embeds,
            negative_prompt_embeds,
            pooled_prompt_embeds,
            negative_pooled_prompt_embeds,
        )
        #        
        if prompt_embeds is not None and class_tokens_mask is None:
            raise ValueError(
                "If `prompt_embeds` are provided, `class_tokens_mask` also have to be passed. Make sure to generate `class_tokens_mask` from the same tokenizer that was used to generate `prompt_embeds`."
            )
        # check the input id images
        if input_id_images is None:
            raise ValueError(
                "Provide `input_id_images`. Cannot leave `input_id_images` undefined for PhotoMaker pipeline."
            )
        if not isinstance(input_id_images, list):
            input_id_images = [input_id_images]

        # 2. Define call parameters
        if prompt is not None and isinstance(prompt, str):
            batch_size = 1
        elif prompt is not None and isinstance(prompt, list):
            batch_size = len(prompt)
        else:
            batch_size = prompt_embeds.shape[0]

        device = self._execution_device

chenpangpang's avatar
chenpangpang committed
296
297
298
299
300
301
302
        # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
        # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
        # corresponds to doing no classifier free guidance.
        do_classifier_free_guidance = guidance_scale > 1.0

        assert do_classifier_free_guidance

303
        # 3. Encode input prompt
Paper99's avatar
Paper99 committed
304
        num_id_images = len(input_id_images)
chenpangpang's avatar
chenpangpang committed
305
        
306
        (
chenpangpang's avatar
chenpangpang committed
307
            prompt_embeds,
308
309
310
311
312
313
314
315
316
            pooled_prompt_embeds,
            class_tokens_mask,
        ) = self.encode_prompt_with_trigger_word(
            prompt=prompt,
            prompt_2=prompt_2,
            device=device,
            num_id_images=num_id_images,
            prompt_embeds=prompt_embeds,
            pooled_prompt_embeds=pooled_prompt_embeds,
chenpangpang's avatar
chenpangpang committed
317
            class_tokens_mask=class_tokens_mask,
318
        )
chenpangpang's avatar
chenpangpang committed
319
        
320
        # 4. Encode input prompt without the trigger word for delayed conditioning
chenpangpang's avatar
chenpangpang committed
321
        prompt_text_only = prompt.replace(" "+self.trigger_word, "") # sensitive to white space
322
323
324
325
326
327
328
329
330
331
        (
            prompt_embeds_text_only,
            negative_prompt_embeds,
            pooled_prompt_embeds_text_only, # TODO: replace the pooled_prompt_embeds with text only prompt
            negative_pooled_prompt_embeds,
        ) = self.encode_prompt(
            prompt=prompt_text_only,
            prompt_2=prompt_2,
            device=device,
            num_images_per_prompt=num_images_per_prompt,
chenpangpang's avatar
chenpangpang committed
332
            do_classifier_free_guidance=do_classifier_free_guidance,
333
334
335
336
337
338
            negative_prompt=negative_prompt,
            negative_prompt_2=negative_prompt_2,
            prompt_embeds=prompt_embeds_text_only,
            negative_prompt_embeds=negative_prompt_embeds,
            pooled_prompt_embeds=pooled_prompt_embeds_text_only,
            negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
Paper99's avatar
Paper99 committed
339
340
        )

chenpangpang's avatar
chenpangpang committed
341
        # 5. Prepare the input ID images
342
343
344
345
346
347
        dtype = next(self.id_encoder.parameters()).dtype
        if not isinstance(input_id_images[0], torch.Tensor):
            id_pixel_values = self.id_image_processor(input_id_images, return_tensors="pt").pixel_values

        id_pixel_values = id_pixel_values.unsqueeze(0).to(device=device, dtype=dtype) # TODO: multiple prompts

chenpangpang's avatar
chenpangpang committed
348
349
        # 6. Get the update text embedding with the stacked ID embedding
        prompt_embeds = self.id_encoder(id_pixel_values, prompt_embeds, class_tokens_mask)
350
351
352
353
354
        
        bs_embed, seq_len, _ = prompt_embeds.shape
        # duplicate text embeddings for each generation per prompt, using mps friendly method
        prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1)
        prompt_embeds = prompt_embeds.view(bs_embed * num_images_per_prompt, seq_len, -1)
chenpangpang's avatar
chenpangpang committed
355
356
357
358
359
360
361
        pooled_prompt_embeds = pooled_prompt_embeds.repeat(1, num_images_per_prompt).view(
            bs_embed * num_images_per_prompt, -1
        )

        # 7. Prepare timesteps
        self.scheduler.set_timesteps(num_inference_steps, device=device)
        timesteps = self.scheduler.timesteps
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
387
388
389
390
391

        # 8. Prepare latent variables
        num_channels_latents = self.unet.config.in_channels
        latents = self.prepare_latents(
            batch_size * num_images_per_prompt,
            num_channels_latents,
            height,
            width,
            prompt_embeds.dtype,
            device,
            generator,
            latents,
        )

        # 9. Prepare extra step kwargs. TODO: Logic should ideally just be moved out of the pipeline
        extra_step_kwargs = self.prepare_extra_step_kwargs(generator, eta)

        # 10. Prepare added time ids & embeddings
        if self.text_encoder_2 is None:
            text_encoder_projection_dim = int(pooled_prompt_embeds.shape[-1])
        else:
            text_encoder_projection_dim = self.text_encoder_2.config.projection_dim

        add_time_ids = self._get_add_time_ids(
            original_size,
            crops_coords_top_left,
            target_size,
            dtype=prompt_embeds.dtype,
            text_encoder_projection_dim=text_encoder_projection_dim,
        )
chenpangpang's avatar
chenpangpang committed
392
        add_time_ids = torch.cat([add_time_ids, add_time_ids], dim=0)
393
394
395
        add_time_ids = add_time_ids.to(device).repeat(batch_size * num_images_per_prompt, 1)

        # 11. Denoising loop
chenpangpang's avatar
chenpangpang committed
396
        num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order
397
398
        with self.progress_bar(total=num_inference_steps) as progress_bar:
            for i, t in enumerate(timesteps):
chenpangpang's avatar
chenpangpang committed
399
400
401
                latent_model_input = (
                    torch.cat([latents] * 2) if do_classifier_free_guidance else latents
                )
402
403
404
405
406
                latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)

                if i <= start_merge_step:
                    current_prompt_embeds = torch.cat(
                        [negative_prompt_embeds, prompt_embeds_text_only], dim=0
chenpangpang's avatar
chenpangpang committed
407
408
                    )
                    add_text_embeds = torch.cat([negative_pooled_prompt_embeds, pooled_prompt_embeds_text_only], dim=0)
409
410
411
                else:
                    current_prompt_embeds = torch.cat(
                        [negative_prompt_embeds, prompt_embeds], dim=0
chenpangpang's avatar
chenpangpang committed
412
413
                    )
                    add_text_embeds = torch.cat([negative_pooled_prompt_embeds, pooled_prompt_embeds], dim=0)
Paper99's avatar
Paper99 committed
414
                # predict the noise residual
chenpangpang's avatar
chenpangpang committed
415
                added_cond_kwargs = {"text_embeds": add_text_embeds, "time_ids": add_time_ids}
416
417
418
419
                noise_pred = self.unet(
                    latent_model_input,
                    t,
                    encoder_hidden_states=current_prompt_embeds,
chenpangpang's avatar
chenpangpang committed
420
                    cross_attention_kwargs=cross_attention_kwargs,
421
422
423
424
425
                    added_cond_kwargs=added_cond_kwargs,
                    return_dict=False,
                )[0]

                # perform guidance
chenpangpang's avatar
chenpangpang committed
426
                if do_classifier_free_guidance:
427
                    noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
chenpangpang's avatar
chenpangpang committed
428
                    noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
429

chenpangpang's avatar
chenpangpang committed
430
                if do_classifier_free_guidance and guidance_rescale > 0.0:
431
                    # Based on 3.4. in https://arxiv.org/pdf/2305.08891.pdf
chenpangpang's avatar
chenpangpang committed
432
                    noise_pred = rescale_noise_cfg(noise_pred, noise_pred_text, guidance_rescale=guidance_rescale)
433
434
435

                # compute the previous noisy sample x_t -> x_t-1
                latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs, return_dict=False)[0]
436

437
438
439
440
                # call the callback, if provided
                if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
                    progress_bar.update()
                    if callback is not None and i % callback_steps == 0:
chenpangpang's avatar
chenpangpang committed
441
                        callback(i, t, latents)
442

chenpangpang's avatar
chenpangpang committed
443
444
445
446
        # make sure the VAE is in float32 mode, as it overflows in float16
        if self.vae.dtype == torch.float16 and self.vae.config.force_upcast:
            self.upcast_vae()
            latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)
447
448

        if not output_type == "latent":
chenpangpang's avatar
chenpangpang committed
449
            image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False)[0]
450
451
452
453
454
455
456
457
458
459
        else:
            image = latents
            return StableDiffusionXLPipelineOutput(images=image)

        # apply watermark if available
        # if self.watermark is not None:
        #     image = self.watermark.apply_watermark(image)

        image = self.image_processor.postprocess(image, output_type=output_type)

chenpangpang's avatar
chenpangpang committed
460
461
462
        # Offload last model to CPU
        if hasattr(self, "final_offload_hook") and self.final_offload_hook is not None:
            self.final_offload_hook.offload()
463
464
465
466

        if not return_dict:
            return (image,)

chenpangpang's avatar
chenpangpang committed
467
        return StableDiffusionXLPipelineOutput(images=image)