
'''simple docstring'''



from ...utils import is_transformers_available
from .pipeline_latent_diffusion_superresolution import LDMSuperResolutionPipeline


if is_transformers_available():
     from .pipeline_latent_diffusion import LDMBertModel, LDMTextToImagePipeline








'''simple docstring'''



from typing import List, Optional, Tuple, Union

import torch

from ...utils import randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput

class       _a (_lowerCamelCase):





       """simple docstring"""




       def __init__(							self				,							A__				,							A__					)			->	Union[str, Any]:
          super().__init__()
          self.register_modules(unet=A__				,							scheduler=A__					)






       @torch.no_grad()
       def __call__(							self				,							A__ = 1				,							A__ = None				,							A__ = 10_00				,							A__ = "pil"				,							A__ = True				,							)			->	Union[ImagePipelineOutput, Tuple]:
          # Sample gaussian noise to begin loop
          if isinstance(self.unet.config.sample_size				,							A__					):
             _SCREAMING_SNAKE_CASE          = (
                 batch_size,
                 self.unet.config.in_channels,
                 self.unet.config.sample_size,
                 self.unet.config.sample_size,
             )
          else:
             _SCREAMING_SNAKE_CASE          = (batch_size, self.unet.config.in_channels, *self.unet.config.sample_size)

          if self.device.type == "mps":
             # randn does not work reproducibly on mps
             _SCREAMING_SNAKE_CASE          = randn_tensor(A__				,							generator=A__					)
             _SCREAMING_SNAKE_CASE          = image.to(self.device					)
          else:
             _SCREAMING_SNAKE_CASE          = randn_tensor(A__				,							generator=A__				,							device=self.device					)

          # set step values
          self.scheduler.set_timesteps(A__					)

          for t in self.progress_bar(self.scheduler.timesteps					):
             # 1. predict noise model_output
             _SCREAMING_SNAKE_CASE          = self.unet(A__				,							A__					).sample

             # 2. compute previous image: x_t -> x_t-1
             _SCREAMING_SNAKE_CASE          = self.scheduler.step(A__				,							A__				,							A__				,							generator=A__					).prev_sample

          _SCREAMING_SNAKE_CASE          = (image / 2 + 0.5).clamp(0				,							1					)
          _SCREAMING_SNAKE_CASE          = image.cpu().permute(0				,							2				,							3				,							1					).numpy()
          if output_type == "pil":
             _SCREAMING_SNAKE_CASE          = self.numpy_to_pil(A__					)

          if not return_dict:
             return (image,)

          return ImagePipelineOutput(images=A__					)






