'''simple docstring'''






from logging import getLogger
from typing import Any, Callable, List, Optional, Union

import numpy as np
import PIL
import torch

from ...schedulers import DDPMScheduler
from ..onnx_utils import ORT_TO_NP_TYPE, OnnxRuntimeModel
from ..pipeline_utils import ImagePipelineOutput
from . import StableDiffusionUpscalePipeline


_UpperCAmelCase						:    str     					=					getLogger(__name__)


_UpperCAmelCase						:    Dict     					=					4
_UpperCAmelCase						:    Optional[Any]     					=					7

_UpperCAmelCase						:    Any     					=					{
    '''float16''': torch.floataa,
    '''float32''': torch.floataa,
}





def    _SCREAMING_SNAKE_CASE		(			__snake_case       : List[str]							):
     if isinstance(__snake_case      ,		torch.Tensor							):
          return image
     elif isinstance(__snake_case      ,		PIL.Image.Image							):
          _A								=     [image]

     if isinstance(image[0]      ,		PIL.Image.Image							):
          _A		,					_A								=     image[0].size
          _A		,					_A								=     (x - x % 6_4 for x in (w, h))  # resize to integer multiple of 32

          _A								=     [np.array(i.resize((w, h)							)							)[None, :] for i in image]
          _A								=     np.concatenate(__snake_case      ,		axis=0							)
          _A								=     np.array(__snake_case							).astype(np.floataa							) / 2_5_5.0
          _A								=     image.transpose(0      ,		3      ,		1      ,		2							)
          _A								=     2.0 * image - 1.0
          _A								=     torch.from_numpy(__snake_case							)
     elif isinstance(image[0]      ,		torch.Tensor							):
          _A								=     torch.cat(__snake_case      ,		dim=0							)

     return image







class lowercase_					(		_UpperCamelCase ):







   """simple docstring"""

   def __init__(			self			: Optional[Any],   UpperCamelCase__			: OnnxRuntimeModel,   UpperCamelCase__			: OnnxRuntimeModel,   UpperCamelCase__			: Any,   UpperCamelCase__			: OnnxRuntimeModel,   UpperCamelCase__			: DDPMScheduler,   UpperCamelCase__			: Any,   UpperCamelCase__			: int = 3_50,   )     -> Any:
        super().__init__(
            vae=UpperCamelCase__,   text_encoder=UpperCamelCase__,   tokenizer=UpperCamelCase__,   unet=UpperCamelCase__,   low_res_scheduler=UpperCamelCase__,   scheduler=UpperCamelCase__,   safety_checker=UpperCamelCase__,   feature_extractor=UpperCamelCase__,   watermarker=UpperCamelCase__,   max_noise_level=UpperCamelCase__,   )
   def __call__(			self			: List[str],   UpperCamelCase__			: Union[str, List[str]],   UpperCamelCase__			: Union[torch.FloatTensor, PIL.Image.Image, List[PIL.Image.Image]],   UpperCamelCase__			: int = 75,   UpperCamelCase__			: float = 9.0,   UpperCamelCase__			: int = 20,   UpperCamelCase__			: Optional[Union[str, List[str]]] = None,   UpperCamelCase__			: Optional[int] = 1,   UpperCamelCase__			: float = 0.0,   UpperCamelCase__			: Optional[Union[torch.Generator, List[torch.Generator]]] = None,   UpperCamelCase__			: Optional[torch.FloatTensor] = None,   UpperCamelCase__			: Optional[np.ndarray] = None,   UpperCamelCase__			: Optional[np.ndarray] = None,   UpperCamelCase__			: Optional[str] = "pil",   UpperCamelCase__			: bool = True,   UpperCamelCase__			: Optional[Callable[[int, int, torch.FloatTensor], None]] = None,   UpperCamelCase__			: Optional[int] = 1,   )     -> Any:
        self.check_inputs(UpperCamelCase__,   UpperCamelCase__,   UpperCamelCase__,   UpperCamelCase__    )

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

        _A								=     self._execution_device
        # 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.
        _A								=     guidance_scale > 1.0

        # 3. Encode input prompt
        _A								=     self._encode_prompt(
            UpperCamelCase__,   UpperCamelCase__,   UpperCamelCase__,   UpperCamelCase__,   UpperCamelCase__,   prompt_embeds=UpperCamelCase__,   negative_prompt_embeds=UpperCamelCase__,   )

        _A								=     ORT_TO_PT_TYPE[str(text_embeddings.dtype    )]

        # 4. Preprocess image
        _A								=     preprocess(UpperCamelCase__    )
        _A								=     image.cpu()

        # 5. set timesteps
        self.scheduler.set_timesteps(UpperCamelCase__,   device=UpperCamelCase__    )
        _A								=     self.scheduler.timesteps

        # 5. Add noise to image
        _A								=     torch.tensor([noise_level],   dtype=torch.long,   device=UpperCamelCase__    )
        _A								=     torch.randn(image.shape,   generator=UpperCamelCase__,   device=UpperCamelCase__,   dtype=UpperCamelCase__    )
        _A								=     self.low_res_scheduler.add_noise(UpperCamelCase__,   UpperCamelCase__,   UpperCamelCase__    )

        _A								=     2 if do_classifier_free_guidance else 1
        _A								=     np.concatenate([image] * batch_multiplier * num_images_per_prompt    )
        _A								=     np.concatenate([noise_level] * image.shape[0]    )

        # 6. Prepare latent variables
        _A		,					_A								=     image.shape[2:]
        _A								=     self.prepare_latents(
            batch_size * num_images_per_prompt,   UpperCamelCase__,   UpperCamelCase__,   UpperCamelCase__,   UpperCamelCase__,   UpperCamelCase__,   UpperCamelCase__,   UpperCamelCase__,   )

        # 7. Check that sizes of image and latents match
        _A								=     image.shape[1]
        if NUM_LATENT_CHANNELS + num_channels_image != NUM_UNET_INPUT_CHANNELS:
             raise ValueError(
                 'Incorrect configuration settings! The config of `pipeline.unet` expects'
                 f' {NUM_UNET_INPUT_CHANNELS} but received `num_channels_latents`: {NUM_LATENT_CHANNELS} +'
                 f' `num_channels_image`: {num_channels_image} '
                 f' = {NUM_LATENT_CHANNELS+num_channels_image}. Please verify the config of'
                 ' `pipeline.unet` or your `image` input.'    )

        # 8. Prepare extra step kwargs. TODO: Logic should ideally just be moved out of the pipeline
        _A								=     self.prepare_extra_step_kwargs(UpperCamelCase__,   UpperCamelCase__    )

        _A								=     next(
            (input.type for input in self.unet.model.get_inputs() if input.name == 'timestep'),   'tensor(float)'    )
        _A								=     ORT_TO_NP_TYPE[timestep_dtype]

        # 9. Denoising loop
        _A								=     len(UpperCamelCase__    ) - num_inference_steps * self.scheduler.order
        with self.progress_bar(total=UpperCamelCase__    ) as progress_bar:
             for i, t in enumerate(UpperCamelCase__    ):
                  # expand the latents if we are doing classifier free guidance
                  _A								=     np.concatenate([latents] * 2    ) if do_classifier_free_guidance else latents

                  # concat latents, mask, masked_image_latents in the channel dimension
                  _A								=     self.scheduler.scale_model_input(UpperCamelCase__,   UpperCamelCase__    )
                  _A								=     np.concatenate([latent_model_input, image],   axis=1    )

                  # timestep to tensor
                  _A								=     np.array([t],   dtype=UpperCamelCase__    )

                  # predict the noise residual
                  _A								=     self.unet(
                      sample=UpperCamelCase__,   timestep=UpperCamelCase__,   encoder_hidden_states=UpperCamelCase__,   class_labels=noise_level.astype(np.intaa    ),   )[0]

                  # perform guidance
                  if do_classifier_free_guidance:
                       _A		,					_A								=     np.split(UpperCamelCase__,   2    )
                  _A								=     noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

                  # compute the previous noisy sample x_t -> x_t-1
                  _A								=     self.scheduler.step(
                      torch.from_numpy(UpperCamelCase__    ),   UpperCamelCase__,   UpperCamelCase__,   **UpperCamelCase__    ).prev_sample

                  # call the callback, if provided
                  if i == len(UpperCamelCase__    ) - 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:
                            callback(UpperCamelCase__,   UpperCamelCase__,   UpperCamelCase__    )

        # 10. Post-processing
        _A								=     self.decode_latents(latents.float()    )

        # 11. Convert to PIL
        if output_type == "pil":
             _A								=     self.numpy_to_pil(UpperCamelCase__    )

        if not return_dict:
             return (image,)

        return ImagePipelineOutput(images=UpperCamelCase__    )
   def   __UpperCAmelCase      (			self			: int,   UpperCamelCase__			: Tuple    )     -> Any:
        _A								=     1 / 0.08_333 * latents
        _A								=     self.vae(latent_sample=UpperCamelCase__    )[0]
        _A								=     np.clip(image / 2 + 0.5,   0,   1    )
        _A								=     image.transpose((0, 2, 3, 1)    )
        return image

   def   __UpperCAmelCase      (			self			: str,   UpperCamelCase__			: Union[str, List[str]],   UpperCamelCase__			: Optional[Any],   UpperCamelCase__			: Optional[int],   UpperCamelCase__			: bool,   UpperCamelCase__			: Optional[str],   UpperCamelCase__			: Optional[torch.FloatTensor] = None,   UpperCamelCase__			: Optional[torch.FloatTensor] = None,   )     -> Dict:
        if prompt is not None and isinstance(UpperCamelCase__,   UpperCamelCase__    ):
             _A								=     1
        elif prompt is not None and isinstance(UpperCamelCase__,   UpperCamelCase__    ):
             _A								=     len(UpperCamelCase__    )
        else:
             _A								=     prompt_embeds.shape[0]

        if prompt_embeds is None:
             _A								=     self.tokenizer(
                 UpperCamelCase__,   padding='max_length',   max_length=self.tokenizer.model_max_length,   truncation=UpperCamelCase__,   return_tensors='pt',   )
             _A								=     text_inputs.input_ids
             _A								=     self.tokenizer(UpperCamelCase__,   padding='longest',   return_tensors='pt'    ).input_ids

             if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(
                 UpperCamelCase__,   UpperCamelCase__    ):
                  _A								=     self.tokenizer.batch_decode(
                      untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]    )
                  logger.warning(
                      'The following part of your input was truncated because CLIP can only handle sequences up to'
                      f' {self.tokenizer.model_max_length} tokens: {removed_text}'    )

             # no positional arguments to text_encoder
             _A								=     self.text_encoder(
                 input_ids=text_input_ids.int().to(UpperCamelCase__    ),   )
             _A								=     prompt_embeds[0]

        _A		,					_A		,					_A								=     prompt_embeds.shape
        # duplicate text embeddings for each generation per prompt, using mps friendly method
        _A								=     prompt_embeds.repeat(1,   UpperCamelCase__    )
        _A								=     prompt_embeds.reshape(bs_embed * num_images_per_prompt,   UpperCamelCase__,   -1    )

        # get unconditional embeddings for classifier free guidance
        if do_classifier_free_guidance and negative_prompt_embeds is None:
             _A								=     42
             if negative_prompt is None:
                  _A								=     [''] * batch_size
             elif type(UpperCamelCase__    ) is not type(UpperCamelCase__    ):
                  raise TypeError(
                      f'`negative_prompt` should be the same type to `prompt`, but got {type(UpperCamelCase__    )} !='
                      f' {type(UpperCamelCase__    )}.'    )
             elif isinstance(UpperCamelCase__,   UpperCamelCase__    ):
                  _A								=     [negative_prompt]
             elif batch_size != len(UpperCamelCase__    ):
                  raise ValueError(
                      f'`negative_prompt`: {negative_prompt} has batch size {len(UpperCamelCase__    )}, but `prompt`:'
                      f' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches'
                      ' the batch size of `prompt`.'    )
             else:
                  _A								=     negative_prompt

             _A								=     text_input_ids.shape[-1]
             _A								=     self.tokenizer(
                 UpperCamelCase__,   padding='max_length',   max_length=UpperCamelCase__,   truncation=UpperCamelCase__,   return_tensors='pt',   )

             # if hasattr(uncond_input, "attention_mask"):
             #     attention_mask = uncond_input.attention_mask.to(device)
             # else:
             #     attention_mask = None

             _A								=     self.text_encoder(
                 input_ids=uncond_input.input_ids.int().to(UpperCamelCase__    ),   )
             _A								=     uncond_embeddings[0]

        if do_classifier_free_guidance:
             _A								=     uncond_embeddings.shape[1]
             # duplicate unconditional embeddings for each generation per prompt, using mps friendly method
             _A								=     uncond_embeddings.repeat(1,   UpperCamelCase__    )
             _A								=     uncond_embeddings.reshape(batch_size * num_images_per_prompt,   UpperCamelCase__,   -1    )

             # 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
             _A								=     np.concatenate([uncond_embeddings, prompt_embeds]    )

        return prompt_embeds




'''simple docstring'''






import multiprocessing
import os
from typing import BinaryIO, Optional, Union

from .. import Dataset, Features, NamedSplit, config
from ..formatting import query_table
from ..packaged_modules.csv.csv import Csv
from ..utils import logging
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader







class lowercase_					(		_UpperCamelCase ):







   """simple docstring"""

   def __init__(			self			: Any,   UpperCamelCase__			: NestedDataStructureLike[PathLike],   UpperCamelCase__			: Optional[NamedSplit] = None,   UpperCamelCase__			: Optional[Features] = None,   UpperCamelCase__			: str = None,   UpperCamelCase__			: bool = False,   UpperCamelCase__			: bool = False,   UpperCamelCase__			: Optional[int] = None,   **UpperCamelCase__			: Optional[Any],   )     -> Tuple:
        super().__init__(
            UpperCamelCase__,   split=UpperCamelCase__,   features=UpperCamelCase__,   cache_dir=UpperCamelCase__,   keep_in_memory=UpperCamelCase__,   streaming=UpperCamelCase__,   num_proc=UpperCamelCase__,   **UpperCamelCase__,   )
        _A								=     path_or_paths if isinstance(UpperCamelCase__,   UpperCamelCase__    ) else {self.split: path_or_paths}
        _A								=     Csv(
            cache_dir=UpperCamelCase__,   data_files=UpperCamelCase__,   features=UpperCamelCase__,   **UpperCamelCase__,   )

   def   __UpperCAmelCase      (			self			: List[Any]    )     -> int:
        # Build iterable dataset
        if self.streaming:
             _A								=     self.builder.as_streaming_dataset(split=self.split    )
        # Build regular (map-style) dataset
        else:
             _A								=     None
             _A								=     None
             _A								=     None
             _A								=     None

             self.builder.download_and_prepare(
                 download_config=UpperCamelCase__,   download_mode=UpperCamelCase__,   verification_mode=UpperCamelCase__,   base_path=UpperCamelCase__,   num_proc=self.num_proc,   )
             _A								=     self.builder.as_dataset(
                 split=self.split,   verification_mode=UpperCamelCase__,   in_memory=self.keep_in_memory    )
        return dataset







class lowercase_					:







   """simple docstring"""

   def __init__(			self			: Optional[Any],   UpperCamelCase__			: Dataset,   UpperCamelCase__			: Union[PathLike, BinaryIO],   UpperCamelCase__			: Optional[int] = None,   UpperCamelCase__			: Optional[int] = None,   **UpperCamelCase__			: Dict,   )     -> Any:
        if num_proc is not None and num_proc <= 0:
             raise ValueError(f'num_proc {num_proc} must be an integer > 0.'    )

        _A								=     dataset
        _A								=     path_or_buf
        _A								=     batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
        _A								=     num_proc
        _A								=     'utf-8'
        _A								=     to_csv_kwargs
   def   __UpperCAmelCase      (			self			: List[Any]    )     -> int:
        _A								=     self.to_csv_kwargs.pop('path_or_buf',   UpperCamelCase__    )
        _A								=     self.to_csv_kwargs.pop('header',   UpperCamelCase__    )
        _A								=     self.to_csv_kwargs.pop('index',   UpperCamelCase__    )

        if isinstance(self.path_or_buf,   (str, bytes, os.PathLike)    ):
             with open(self.path_or_buf,   'wb+'    ) as buffer:
                  _A								=     self._write(file_obj=UpperCamelCase__,   header=UpperCamelCase__,   index=UpperCamelCase__,   **self.to_csv_kwargs    )
        else:
             _A								=     self._write(file_obj=self.path_or_buf,   header=UpperCamelCase__,   index=UpperCamelCase__,   **self.to_csv_kwargs    )
        return written
   def   __UpperCAmelCase      (			self			: str,   UpperCamelCase__			: Optional[int]    )     -> str:
        _A		,					_A		,					_A		,					_A								=     args

        _A								=     query_table(
            table=self.dataset.data,   key=slice(UpperCamelCase__,   offset + self.batch_size    ),   indices=self.dataset._indices,   )
        _A								=     batch.to_pandas().to_csv(
            path_or_buf=UpperCamelCase__,   header=header if (offset == 0) else False,   index=UpperCamelCase__,   **UpperCamelCase__    )
        return csv_str.encode(self.encoding    )

   def   __UpperCAmelCase      (			self			: Union[str, Any],   UpperCamelCase__			: BinaryIO,   UpperCamelCase__			: List[str],   UpperCamelCase__			: Dict,   **UpperCamelCase__			: Optional[int]    )     -> int:
        _A								=     0

        if self.num_proc is None or self.num_proc == 1:
             for offset in logging.tqdm(
                 range(0,   len(self.dataset    ),   self.batch_size    ),   unit='ba',   disable=not logging.is_progress_bar_enabled(),   desc='Creating CSV from Arrow format',   ):
                  _A								=     self._batch_csv((offset, header, index, to_csv_kwargs)    )
                  written += file_obj.write(UpperCamelCase__    )

        else:
             _A		,					_A								=     len(self.dataset    ), self.batch_size
             with multiprocessing.Pool(self.num_proc    ) as pool:
                  for csv_str in logging.tqdm(
                      pool.imap(
                          self._batch_csv,   [(offset, header, index, to_csv_kwargs) for offset in range(0,   UpperCamelCase__,   UpperCamelCase__    )],   ),   total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size,   unit='ba',   disable=not logging.is_progress_bar_enabled(),   desc='Creating CSV from Arrow format',   ):
                       written += file_obj.write(UpperCamelCase__    )

        return written



