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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
import contextlib
import inspect
from typing import Any, Dict, List, Optional, Union, get_args

import numpy as np
import torch
import torch.nn.functional as F
import torchvision.transforms.functional as TF
from diffusers.image_processor import PipelineImageInput, VaeImageProcessor
from diffusers.loaders import FromSingleFileMixin
from diffusers.models.transformers import Transformer2DModel
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import (
    rescale_noise_cfg,
    retrieve_timesteps,
)
from diffusers.schedulers import KarrasDiffusionSchedulers
from diffusers.utils import (
    BaseOutput,
    deprecate,
    logging,
)
from diffusers.utils.torch_utils import randn_tensor
from PIL import (
    Image,
    Jpeg2KImagePlugin,
    JpegImagePlugin,
    PngImagePlugin,
    TiffImagePlugin,
)
from transformers import (
    CLIPImageProcessor,
    CLIPTextModel,
    CLIPTokenizer,
    CLIPVisionModel,
)

from diffusers import AutoencoderKL, DiffusionPipeline, UNet2DConditionModel

logger = logging.get_logger(__name__)  # pylint: disable=invalid-name
from dataclasses import dataclass

ImageInput = Union[
    PipelineImageInput,
    JpegImagePlugin.JpegImageFile,
    Jpeg2KImagePlugin.Jpeg2KImageFile,
    PngImagePlugin.PngImageFile,
    TiffImagePlugin.TiffImageFile,
]

import math


def postprocess(

    image: torch.FloatTensor,

    output_type: str = "pil",

):
    """

    Postprocess the image output from tensor to `output_type`.



    Args:

        image (`torch.FloatTensor`):

            The image input, should be a pytorch tensor with shape `B x C x H x W`.

        output_type (`str`, *optional*, defaults to `pil`):

            The output type of the image, can be one of `pil`, `np`, `pt`, `latent`.



    Returns:

        `PIL.Image.Image`, `np.ndarray` or `torch.FloatTensor`:

            The postprocessed image.

    """
    if not isinstance(image, torch.Tensor):
        raise ValueError(
            f"Input for postprocessing is in incorrect format: {type(image)}. We only support pytorch tensor"
        )
    if output_type not in ["latent", "pt", "np", "pil"]:
        deprecation_message = (
            f"the output_type {output_type} is outdated and has been set to `np`. Please make sure to set it to one of these instead: "
            "`pil`, `np`, `pt`, `latent`"
        )
        deprecate(
            "Unsupported output_type", "1.0.0", deprecation_message, standard_warn=False
        )
        output_type = "np"

    image = image.detach().cpu()
    image = image.to(torch.float32)

    if output_type == "latent":
        return image

    # denormalize the image
    image = image * 0.5 + 0.5  # .clamp(0, 1)

    materials = []
    for i in range(image.shape[0]):

        material = StableMaterialsMaterial()
        material.init_from_tensor(image[i], mode=output_type)

        materials.append(material)

    return materials


@dataclass
class StableMaterialsMaterial:
    basecolor: torch.FloatTensor
    normal: torch.FloatTensor
    height: torch.FloatTensor
    roughness: torch.FloatTensor
    metallic: torch.FloatTensor
    _mode: str = "tensor"  # Default mode is tensor

    def __init__(

        self,

        basecolor: Optional[Union[Image.Image, np.ndarray, torch.FloatTensor]] = None,

        normal: Optional[Union[Image.Image, np.ndarray, torch.FloatTensor]] = None,

        height: Optional[Union[Image.Image, np.ndarray, torch.FloatTensor]] = None,

        roughness: Optional[Union[Image.Image, np.ndarray, torch.FloatTensor]] = None,

        metallic: Optional[Union[Image.Image, np.ndarray, torch.FloatTensor]] = None,

        mode: str = "tensor",

    ):
        self._basecolor = self._to_pt(basecolor)
        self._normal = self._to_pt(normal)
        self._height = self._to_pt(height)
        self._roughness = self._to_pt(roughness)
        self._metallic = self._to_pt(metallic)
        self._mode = mode

    def init_from_tensor(self, image: torch.FloatTensor, mode: str = "tensor"):
        assert image.shape[0] >= 8, "Input tensor should have at least 8 channels"
        self._basecolor = image[:3].clamp(0, 1)
        self._normal = self.compute_normal_map_z_component(image[3:5])
        self._height = image[5:6].clamp(0, 1)
        self._roughness = image[6:7].clamp(0, 1)
        self._metallic = image[7:8].clamp(0, 1)
        self._mode = mode

    def resize(self, size, antialias=True):
        self._basecolor = TF.resize(self._basecolor, size, antialias=antialias)
        self._normal = TF.resize(self._normal, size, antialias=antialias)
        self._height = TF.resize(self._height, size, antialias=antialias)
        self._roughness = TF.resize(self._roughness, size, antialias=antialias)
        self._metallic = TF.resize(self._metallic, size, antialias=antialias)
        return self

    def tile(self, num_tiles):
        self._basecolor = self._basecolor.repeat(1, num_tiles, num_tiles)
        self._normal = self._normal.repeat(1, num_tiles, num_tiles)
        self._height = self._height.repeat(1, num_tiles, num_tiles)
        self._roughness = self._roughness.repeat(1, num_tiles, num_tiles)
        self._metallic = self._metallic.repeat(1, num_tiles, num_tiles)
        return self

    def _to_numpy(self, image: torch.FloatTensor):
        if image is None:
            return None
        return image.numpy()

    def _to_pil(self, image: torch.FloatTensor, mode: str = "RGB"):
        if image is None:
            return None
        return TF.to_pil_image(image).convert(mode)

    def _to_pt(self, image):
        if image is None:
            return None
        if isinstance(image, np.ndarray):
            image = torch.from_numpy(image)
        elif isinstance(image, Image.Image):
            image = TF.to_tensor(image)
        return image.cpu()

    def compute_normal_map_z_component(self, normal: torch.FloatTensor):
        normal = normal * 2 - 1
        sum_sq = (normal**2).sum(dim=0, keepdim=True)[0]
        z = torch.zeros_like(sum_sq)
        mask = sum_sq <= 1
        z[mask] = torch.sqrt(1 - sum_sq[mask])
        mask_outlier = sum_sq > 1
        scale_factor = torch.sqrt(sum_sq[mask_outlier])
        normal[:, mask_outlier] = normal[:, mask_outlier] / scale_factor
        normal = torch.cat([normal, z.unsqueeze(0)], dim=0)
        normal = normal * 0.5 + 0.5
        return normal.clamp(0, 1)

    def _convert(self, image, mode="RGB"):
        if self._mode == "numpy":
            return self._to_numpy(image)
        elif self._mode == "pil":
            return self._to_pil(image, mode)
        return image

    @property
    def size(self):
        return list(self._basecolor.shape[-2:])

    @property
    def basecolor(self):
        return self._convert(self._basecolor, mode="RGB")

    @property
    def normal(self):
        return self._convert(self._normal, mode="RGB")

    @property
    def height(self):
        return self._convert(self._height, mode="L")

    @property
    def roughness(self):
        return self._convert(self._roughness, mode="L")

    @property
    def metallic(self):
        return self._convert(self._metallic, mode="L")

    def as_dict(self):
        return {
            "basecolor": self.basecolor,
            "normal": self.normal,
            "height": self.height,
            "roughness": self.roughness,
            "metallic": self.metallic,
        }

    def as_list(self):
        return [
            self.basecolor,
            self.normal,
            self.height,
            self.roughness,
            self.metallic,
        ]

    def as_tensor(self):
        return torch.cat(
            [
                self._basecolor,
                self._normal[:2],
                self._height,
                self._roughness,
                self._metallic,
            ],
            dim=0,
        )


@dataclass
class StableMaterialsPipelineOutput(BaseOutput):
    """

    Output class for Stable Diffusion pipelines.



    Args:

        images (`List[PIL.Image.Image]` or `np.ndarray`)

            List of denoised PIL images of length `batch_size` or NumPy array of shape `(batch_size, height, width,

            num_channels)`.

    """

    images: List[StableMaterialsMaterial]


def patch(x, patch_factor=2):
    if isinstance(x, (list, tuple)):
        pass

    b, c, h, w = x.shape
    patch_size = h // patch_factor

    x = x.unfold(2, patch_size, patch_size).unfold(3, patch_size, patch_size)
    x = x.permute(0, 2, 3, 1, 4, 5).contiguous().view(-1, c, patch_size, patch_size)

    n_patches = x.shape[0] // b

    return x, (b, h), n_patches, patch_size


def unpatch(x, b, h, n_patches, patch_size=32):
    if isinstance(x, (list, tuple)):
        if len(x) == 1:
            x = x[0]
        else:
            pass

    factor = patch_size / x.shape[-1]
    h, w = int(h / factor), int(h / factor)

    c, patch_size = x.shape[1], x.shape[2]
    n_patches = x.shape[0] // b

    x = x.reshape(b, n_patches, c, patch_size, patch_size)
    x = x.permute(0, 2, 3, 4, 1).contiguous().view(b, c * patch_size * patch_size, -1)

    x = F.fold(
        x,
        output_size=(h, w),
        kernel_size=patch_size,
        stride=patch_size,
    )

    return x


def roll(x):
    roll_h = torch.randint(0, 256, (1,)).item() // 2 * 2
    roll_w = torch.randint(0, 256, (1,)).item() // 2 * 2

    x = torch.roll(x, shifts=(roll_h, roll_w), dims=(2, 3))

    return x, (roll_h, roll_w)


def unroll(x, roll_h, roll_w, factor=1.0):
    roll_h = int(roll_h * factor)
    roll_w = int(roll_w * factor)
    x = torch.roll(x, shifts=(-roll_h, -roll_w), dims=(2, 3))
    return x


@contextlib.contextmanager
def rolled_conv(enabled=True):
    conv = torch.nn.Conv2d

    if enabled:
        # Save the original conv's constructor
        orig_forward = conv.forward

        def forward(self, x, *args, **kwargs):
            x, (roll_h, roll_w) = roll(x)

            pad = 4
            x = F.pad(x, (pad, pad, pad, pad), mode="circular")
            h = x.shape[-2]

            x = orig_forward(self, x, *args, **kwargs)
            h1 = x.shape[-2]
            factor = h1 / h

            pad = int(pad * factor)
            x = x[..., pad:-pad, pad:-pad]
            x = unroll(x, roll_h, roll_w, factor)

            return x

        # Patch conv's constructor
        conv.forward = forward
        # conv.__init__ = __init__
        yield conv

        # Restore the original conv's constructor
        conv.forward = orig_forward
    else:
        # Use the original conv
        yield conv


@contextlib.contextmanager
def tiled_attn(enabled=True, scale_multiplier=4):
    conv = Transformer2DModel

    if enabled:
        # Save the original conv's constructor
        orig_forward = conv.forward
        # mult = scale_multiplier

        def forward(self, hidden_states, encoder_hidden_states, *args, **kwargs):
            hidden_states, (roll_h, roll_w) = roll(hidden_states)
            hidden_states, (b, h), n_patches, patch_size = patch(
                hidden_states, self.scale_multiplier
            )
            encoder_hidden_states = encoder_hidden_states.repeat_interleave(
                n_patches, dim=0
            )
            chunks = math.ceil(len(hidden_states) / 8)
            hidden_states = hidden_states.chunk(chunks, dim=0)
            encoder_hidden_states = encoder_hidden_states.chunk(chunks, dim=0)
            result = []
            for i in range(chunks):
                result.append(
                    orig_forward(
                        self,
                        hidden_states[i],
                        encoder_hidden_states[i],
                        *args,
                        **kwargs,
                    )[0]
                )
            hidden_states = torch.cat(result, dim=0)
            hidden_states = unpatch(hidden_states, b, h, n_patches, patch_size)
            hidden_states = unroll(hidden_states, roll_h, roll_w)
            return (hidden_states,)

        # Patch conv's constructor
        conv.scale_multiplier = scale_multiplier
        conv.forward = forward
        yield conv

        # Restore the original conv's constructor
        conv.forward = orig_forward
    else:
        # Use the original conv
        yield conv


class StableMaterialsPipeline(DiffusionPipeline, FromSingleFileMixin):

    model_cpu_offload_seq = "prompt_encoder->unet->vae"

    def __init__(

        self,

        vae: AutoencoderKL,

        unet: UNet2DConditionModel,

        # prompt_encoder: nn.Module,

        scheduler: KarrasDiffusionSchedulers,

        text_encoder: CLIPTextModel,

        tokenizer: CLIPTokenizer,

        vision_encoder: CLIPVisionModel,

        processor: CLIPImageProcessor,

    ):
        super().__init__()

        self.register_modules(
            vae=vae,
            unet=unet,
            # prompt_encoder=prompt_encoder,
            scheduler=scheduler,
            # Conditioning modules
            tokenizer=tokenizer,
            processor=processor,
            text_encoder=text_encoder,
            vision_encoder=vision_encoder,
        )

        self.vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1)
        self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor)

    def enable_vae_slicing(self):
        r"""

        Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to

        compute decoding in several steps. This is useful to save some memory and allow larger batch sizes.

        """
        self.vae.enable_slicing()

    def disable_vae_slicing(self):
        r"""

        Disable sliced VAE decoding. If `enable_vae_slicing` was previously enabled, this method will go back to

        computing decoding in one step.

        """
        self.vae.disable_slicing()

    def enable_vae_tiling(self):
        r"""

        Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to

        compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow

        processing larger images.

        """
        self.vae.enable_tiling()

    def disable_vae_tiling(self):
        r"""

        Disable tiled VAE decoding. If `enable_vae_tiling` was previously enabled, this method will go back to

        computing decoding in one step.

        """
        self.vae.disable_tiling()

    def __encode_text(self, text):
        inputs = self.tokenizer(text, padding=True, return_tensors="pt")
        inputs["input_ids"] = inputs["input_ids"].to(self.device)
        inputs["attention_mask"] = inputs["attention_mask"].to(self.device)
        outputs = self.text_encoder(**inputs)
        return outputs.text_embeds.unsqueeze(1)

    def __encode_image(self, image):
        inputs = self.processor(images=image, return_tensors="pt")
        inputs["pixel_values"] = inputs["pixel_values"].to(self.device)
        outputs = self.vision_encoder(**inputs)
        return outputs.image_embeds.unsqueeze(1)

    def __encode_prompt(

        self,

        prompt,

    ):
        if type(prompt) != list:
            prompt = [prompt]

        embs = []
        for prompt in prompt:
            if isinstance(prompt, str):
                embs.append(self.__encode_text(prompt))
            elif type(prompt) in get_args(ImageInput):
                embs.append(self.__encode_image(prompt))
            else:
                raise NotImplementedError

        return torch.cat(embs, dim=0)

    def encode_prompt(

        self,

        prompt,

        device,

        num_images_per_prompt,

        do_classifier_free_guidance,

        negative_prompt=None,

        prompt_embeds: Optional[torch.FloatTensor] = None,

        negative_prompt_embeds: Optional[torch.FloatTensor] = None,

    ):
        r"""

        Encodes the prompt into text encoder hidden states.



        Args:

            prompt (`str` or `List[str]`, *optional*):

                prompt to be encoded

            device: (`torch.device`):

                torch device

            num_images_per_prompt (`int`):

                number of images that should be generated per prompt

            do_classifier_free_guidance (`bool`):

                whether to use classifier free guidance or not

            negative_prompt (`str` or `List[str]`, *optional*):

                The prompt or prompts not to guide the image generation. If not defined, one has to pass

                `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is

                less than `1`).

            prompt_embeds (`torch.FloatTensor`, *optional*):

                Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not

                provided, text embeddings will be generated from `prompt` input argument.

            negative_prompt_embeds (`torch.FloatTensor`, *optional*):

                Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt

                weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input

                argument.

        """
        if (
            prompt is not None
            and isinstance(prompt, str)
            or isinstance(prompt, Image.Image)
        ):
            batch_size = 1
        elif prompt is not None and isinstance(prompt, list):
            batch_size = len(prompt)
        else:
            batch_size = prompt_embeds.shape[0]

        if prompt_embeds is None:
            prompt_embeds = self.__encode_prompt(prompt)

        if self.unet is not None:
            prompt_embeds_dtype = self.unet.dtype
        else:
            prompt_embeds_dtype = prompt_embeds.dtype

        prompt_embeds = prompt_embeds.to(dtype=prompt_embeds_dtype, device=device)

        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
        )

        if do_classifier_free_guidance and negative_prompt_embeds is None:
            uncond_tokens: List[str]
            if negative_prompt is None:
                # uncond_tokens = [""] * batch_size
                uncond_tokens = [Image.new("RGB", (512, 512), (0, 0, 0))] * batch_size
            elif isinstance(negative_prompt, str):
                uncond_tokens = [negative_prompt] * batch_size
            elif len(negative_prompt) != batch_size:
                raise ValueError(
                    "The `negative_prompt` must be a string, a list of strings of length `batch_size`, or `None`."
                )
            else:
                uncond_tokens = negative_prompt

            negative_prompt_embeds = self.__encode_prompt(uncond_tokens)

        # get unconditional embeddings for classifier free guidance
        if do_classifier_free_guidance:
            # duplicate unconditional embeddings for each generation per prompt, using mps friendly method
            seq_len = negative_prompt_embeds.shape[1]

            negative_prompt_embeds = negative_prompt_embeds.to(
                dtype=prompt_embeds_dtype, device=device
            )

            negative_prompt_embeds = negative_prompt_embeds.repeat(
                1, num_images_per_prompt, 1
            )
            negative_prompt_embeds = negative_prompt_embeds.view(
                batch_size * num_images_per_prompt, seq_len, -1
            )

        return prompt_embeds, negative_prompt_embeds

    def decode_latents(self, latents):
        deprecation_message = "The decode_latents method is deprecated and will be removed in 1.0.0. Please use VaeImageProcessor.postprocess(...) instead"
        deprecate("decode_latents", "1.0.0", deprecation_message, standard_warn=False)

        latents = 1 / self.vae.config.scaling_factor * latents
        image = self.vae.decode(latents, return_dict=False)[0]
        image = (image / 2 + 0.5).clamp(0, 1)
        # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
        image = image.cpu().permute(0, 2, 3, 1).float().numpy()
        return image

    def prepare_extra_step_kwargs(self, generator, eta):
        # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
        # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
        # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
        # and should be between [0, 1]

        accepts_eta = "eta" in set(
            inspect.signature(self.scheduler.step).parameters.keys()
        )
        extra_step_kwargs = {}
        if accepts_eta:
            extra_step_kwargs["eta"] = eta

        # check if the scheduler accepts generator
        accepts_generator = "generator" in set(
            inspect.signature(self.scheduler.step).parameters.keys()
        )
        if accepts_generator:
            extra_step_kwargs["generator"] = generator
        return extra_step_kwargs

    def check_inputs(

        self,

        prompt,

        height,

        width,

        negative_prompt=None,

        prompt_embeds=None,

        negative_prompt_embeds=None,

    ):
        if height % 8 != 0 or width % 8 != 0:
            raise ValueError(
                f"`height` and `width` have to be divisible by 8 but are {height} and {width}."
            )

        if prompt is not None and prompt_embeds is not None:
            raise ValueError(
                f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to"
                " only forward one of the two."
            )
        elif prompt is None and prompt_embeds is None:
            raise ValueError(
                "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined."
            )
        elif prompt is not None and (not isinstance(prompt, (str, list, Image.Image))):
            raise ValueError(
                f"`prompt` has to be of type `str` or `list` but is {type(prompt)}"
            )

        if negative_prompt is not None and negative_prompt_embeds is not None:
            raise ValueError(
                f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:"
                f" {negative_prompt_embeds}. Please make sure to only forward one of the two."
            )

        if prompt_embeds is not None and negative_prompt_embeds is not None:
            if prompt_embeds.shape != negative_prompt_embeds.shape:
                raise ValueError(
                    "`prompt_embeds` and `negative_prompt_embeds` must have the same shape when passed directly, but"
                    f" got: `prompt_embeds` {prompt_embeds.shape} != `negative_prompt_embeds`"
                    f" {negative_prompt_embeds.shape}."
                )

    def prepare_latents(

        self,

        batch_size,

        num_channels_latents,

        height,

        width,

        dtype,

        device,

        generator,

        latents=None,

    ):
        shape = (
            batch_size,
            num_channels_latents,
            height // self.vae_scale_factor,
            width // self.vae_scale_factor,
        )
        if isinstance(generator, list) and len(generator) != batch_size:
            raise ValueError(
                f"You have passed a list of generators of length {len(generator)}, but requested an effective batch"
                f" size of {batch_size}. Make sure the batch size matches the length of the generators."
            )

        if latents is None:
            latents = randn_tensor(
                shape, generator=generator, device=device, dtype=dtype
            )
        else:
            latents = latents.to(device)

        # scale the initial noise by the standard deviation required by the scheduler
        latents = latents * self.scheduler.init_noise_sigma
        return latents

    def enable_freeu(self, s1: float, s2: float, b1: float, b2: float):
        r"""Enables the FreeU mechanism as in https://arxiv.org/abs/2309.11497.



        The suffixes after the scaling factors represent the stages where they are being applied.



        Please refer to the [official repository](https://github.com/ChenyangSi/FreeU) for combinations of the values

        that are known to work well for different pipelines such as Stable Diffusion v1, v2, and Stable Diffusion XL.



        Args:

            s1 (`float`):

                Scaling factor for stage 1 to attenuate the contributions of the skip features. This is done to

                mitigate "oversmoothing effect" in the enhanced denoising process.

            s2 (`float`):

                Scaling factor for stage 2 to attenuate the contributions of the skip features. This is done to

                mitigate "oversmoothing effect" in the enhanced denoising process.

            b1 (`float`): Scaling factor for stage 1 to amplify the contributions of backbone features.

            b2 (`float`): Scaling factor for stage 2 to amplify the contributions of backbone features.

        """
        if not hasattr(self, "unet"):
            raise ValueError("The pipeline must have `unet` for using FreeU.")
        self.unet.enable_freeu(s1=s1, s2=s2, b1=b1, b2=b2)

    def disable_freeu(self):
        """Disables the FreeU mechanism if enabled."""
        self.unet.disable_freeu()

    # Copied from diffusers.pipelines.latent_consistency_models.pipeline_latent_consistency_text2img.LatentConsistencyModelPipeline.get_guidance_scale_embedding
    def get_guidance_scale_embedding(self, w, embedding_dim=512, dtype=torch.float32):
        """

        See https://github.com/google-research/vdm/blob/dc27b98a554f65cdc654b800da5aa1846545d41b/model_vdm.py#L298



        Args:

            timesteps (`torch.Tensor`):

                generate embedding vectors at these timesteps

            embedding_dim (`int`, *optional*, defaults to 512):

                dimension of the embeddings to generate

            dtype:

                data type of the generated embeddings



        Returns:

            `torch.FloatTensor`: Embedding vectors with shape `(len(timesteps), embedding_dim)`

        """
        assert len(w.shape) == 1
        w = w * 1000.0

        half_dim = embedding_dim // 2
        emb = torch.log(torch.tensor(10000.0)) / (half_dim - 1)
        emb = torch.exp(torch.arange(half_dim, dtype=dtype) * -emb)
        emb = w.to(dtype)[:, None] * emb[None, :]
        emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1)
        if embedding_dim % 2 == 1:  # zero pad
            emb = torch.nn.functional.pad(emb, (0, 1))
        assert emb.shape == (w.shape[0], embedding_dim)
        return emb

    @property
    def guidance_scale(self):
        return self._guidance_scale

    @property
    def guidance_rescale(self):
        return self._guidance_rescale

    # 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.
    @property
    def do_classifier_free_guidance(self):
        return self._guidance_scale > 1 and self.unet.config.time_cond_proj_dim is None

    @property
    def cross_attention_kwargs(self):
        return self._cross_attention_kwargs

    @property
    def num_timesteps(self):
        return self._num_timesteps

    @property
    def interrupt(self):
        return self._interrupt

    @torch.no_grad()
    # @replace_example_docstring(EXAMPLE_DOC_STRING)
    def __call__(

        self,

        prompt: Union[

            str, List[str], PipelineImageInput, List[PipelineImageInput]

        ] = None,

        height: Optional[int] = None,

        width: Optional[int] = None,

        tileable: bool = False,

        patched: bool = False,

        num_inference_steps: int = 50,

        timesteps: List[int] = None,

        guidance_scale: float = 7.5,

        negative_prompt: 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,

        latents: Optional[torch.FloatTensor] = None,

        prompt_embeds: Optional[torch.FloatTensor] = None,

        negative_prompt_embeds: Optional[torch.FloatTensor] = None,

        output_type: Optional[str] = "pil",

        return_dict: bool = True,

        cross_attention_kwargs: Optional[Dict[str, Any]] = None,

        guidance_rescale: float = 0.0,

        **kwargs,

    ):

        # 0. Default height and width to unet
        height = height or self.unet.config.sample_size * self.vae_scale_factor
        width = width or self.unet.config.sample_size * self.vae_scale_factor

        # 1. Check inputs. Raise error if not correct
        self.check_inputs(
            prompt,
            height,
            width,
            negative_prompt,
            prompt_embeds,
            negative_prompt_embeds,
        )

        self._guidance_scale = guidance_scale
        self._guidance_rescale = guidance_rescale
        self._cross_attention_kwargs = cross_attention_kwargs
        self._interrupt = False

        # 2. Define call parameters
        if prompt is not None and (
            isinstance(prompt, str) or isinstance(prompt, Image.Image)
        ):
            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

        # 3. Encode input prompt
        prompt_embeds, negative_prompt_embeds = self.encode_prompt(
            prompt,
            device,
            num_images_per_prompt,
            self.do_classifier_free_guidance,
            negative_prompt,
            prompt_embeds=prompt_embeds,
            negative_prompt_embeds=negative_prompt_embeds,
        )

        # For classifier free guidance, we need to do two forward passes.
        # Here we concatenate the unconditional and text embeddings into a single batch
        # to avoid doing two forward passes
        if self.do_classifier_free_guidance:
            prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds])

        # 4. Prepare timesteps
        timesteps, num_inference_steps = retrieve_timesteps(
            self.scheduler, num_inference_steps, device, timesteps
        )

        # 5. 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,
        )

        # 6. Prepare extra step kwargs.
        extra_step_kwargs = self.prepare_extra_step_kwargs(generator, eta)

        # 6.2 Optionally get Guidance Scale Embedding
        timestep_cond = None
        if self.unet.config.time_cond_proj_dim is not None:
            guidance_scale_tensor = torch.tensor(self.guidance_scale - 1).repeat(
                batch_size * num_images_per_prompt
            )
            timestep_cond = self.get_guidance_scale_embedding(
                guidance_scale_tensor, embedding_dim=self.unet.config.time_cond_proj_dim
            ).to(device=device, dtype=latents.dtype)

        # 7. Denoising loop
        self._num_timesteps = len(timesteps)
        with self.progress_bar(total=num_inference_steps) as progress_bar:
            for i, t in enumerate(timesteps):
                if self.interrupt:
                    continue

                # expand the latents if we are doing classifier free guidance
                latent_model_input = (
                    torch.cat([latents] * 2)
                    if self.do_classifier_free_guidance
                    else latents
                )
                latent_model_input = self.scheduler.scale_model_input(
                    latent_model_input, t
                )

                scale_multiplier = (
                    latent_model_input.shape[-1]
                ) // self.unet.config.sample_size

                past_mid = i >= len(timesteps) // 4
                # predict the noise residual
                with rolled_conv(enabled=(tileable & past_mid)):
                    with tiled_attn(enabled=patched, scale_multiplier=scale_multiplier):
                        noise_pred = self.unet(
                            latent_model_input,
                            t,
                            encoder_hidden_states=prompt_embeds,
                            timestep_cond=timestep_cond,
                            cross_attention_kwargs=self.cross_attention_kwargs,
                            return_dict=False,
                        )[0]

                # perform guidance
                if self.do_classifier_free_guidance:
                    noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
                    noise_pred = noise_pred_uncond + self.guidance_scale * (
                        noise_pred_text - noise_pred_uncond
                    )

                if self.do_classifier_free_guidance and self.guidance_rescale > 0.0:
                    # Based on 3.4. in https://arxiv.org/pdf/2305.08891.pdf
                    noise_pred = rescale_noise_cfg(
                        noise_pred,
                        noise_pred_text,
                        guidance_rescale=self.guidance_rescale,
                    )

                # 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]

                # call the callback, if provided
                if i == len(timesteps) - 1 or (i + 1) % self.scheduler.order == 0:
                    progress_bar.update()

        if not output_type == "latent":
            if tileable:
                # decode padded latent to preserve tileability
                l_height = height // self.vae_scale_factor
                l_width = width // self.vae_scale_factor
                pad = l_height // 4
                latents = TF.center_crop(
                    latents.repeat(1, 1, 3, 3), (l_height + pad, l_width + pad)
                )

            # decode the latents
            image = self.vae.decode(
                latents / self.vae.config.scaling_factor,
                return_dict=False,
                generator=generator,
            )[0]

            # crop to original size
            image = TF.center_crop(image, (height, width))
        else:
            image = latents

        image = postprocess(image, output_type=output_type)

        # Offload all models
        self.maybe_free_model_hooks()

        if not return_dict:
            return image

        return StableMaterialsPipelineOutput(images=image)