





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


__A       : List[str]						    = getLogger(__name__)


__A       : Any						    = 4
__A       : Tuple						    = 7

__A       : Any						    = {
    "float16": torch.floataa,
    "float32": torch.floataa,
}





def 							__lowerCAmelCase(					_SCREAMING_SNAKE_CASE							)       ->   Tuple:




    """simple docstring"""




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

    if isinstance(image[0]       ,    PIL.Image.Image							):
        _A, _A    =       image[0].size
        _A, _A    =       (x - x % 64 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(_SCREAMING_SNAKE_CASE       ,    axis=0							)
        _A    =       np.array(_SCREAMING_SNAKE_CASE							).astype(np.floataa							) / 255.0
        _A    =       image.transpose(0       ,    3       ,    1       ,    2							)
        _A    =       2.0 * image - 1.0
        _A    =       torch.from_numpy(_SCREAMING_SNAKE_CASE							)
    elif isinstance(image[0]       ,    torch.Tensor							):
        _A    =       torch.cat(_SCREAMING_SNAKE_CASE       ,    dim=0							)

    return image







class     lowerCamelCase(	lowerCAmelCase__  ):
     '''simple docstring'''


     def __init__(						self							,    snake_case_							,    snake_case_							,    snake_case_							,    snake_case_							,    snake_case_							,    snake_case_							,    snake_case_ = 350							,    ):
         super().__init__(
             vae=lowerCAmelCase_							,    text_encoder=lowerCAmelCase_							,    tokenizer=lowerCAmelCase_							,    unet=lowerCAmelCase_							,    low_res_scheduler=lowerCAmelCase_							,    scheduler=lowerCAmelCase_							,    safety_checker=lowerCAmelCase_							,    feature_extractor=lowerCAmelCase_							,    watermarker=lowerCAmelCase_							,    max_noise_level=lowerCAmelCase_							,    )




     def __call__(						self							,    snake_case_							,    snake_case_							,    snake_case_ = 75							,    snake_case_ = 9.0							,    snake_case_ = 20							,    snake_case_ = None							,    snake_case_ = 1							,    snake_case_ = 0.0							,    snake_case_ = None							,    snake_case_ = None							,    snake_case_ = None							,    snake_case_ = None							,    snake_case_ = "pil"							,    snake_case_ = True							,    snake_case_ = None							,    snake_case_ = 1							,    ):
         self.check_inputs(lowerCAmelCase_							,    lowerCAmelCase_							,    lowerCAmelCase_							,    lowerCAmelCase_       )

         # 2. Define call parameters
         if prompt is not None and isinstance(lowerCAmelCase_							,    lowerCAmelCase_       ):
             _A    =       1
         elif prompt is not None and isinstance(lowerCAmelCase_							,    lowerCAmelCase_       ):
             _A    =       len(lowerCAmelCase_       )
         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(
             lowerCAmelCase_							,    lowerCAmelCase_							,    lowerCAmelCase_							,    lowerCAmelCase_							,    lowerCAmelCase_							,    prompt_embeds=lowerCAmelCase_							,    negative_prompt_embeds=lowerCAmelCase_							,    )

         _A    =       ORT_TO_PT_TYPE[str(text_embeddings.dtype       )]

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

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

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

         _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							,    lowerCAmelCase_							,    lowerCAmelCase_							,    lowerCAmelCase_							,    lowerCAmelCase_							,    lowerCAmelCase_							,    lowerCAmelCase_							,    lowerCAmelCase_							,    )

         # 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(lowerCAmelCase_							,    lowerCAmelCase_       )

         _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(lowerCAmelCase_       ) - num_inference_steps * self.scheduler.order
         with self.progress_bar(total=lowerCAmelCase_       ) as progress_bar:
             for i, t in enumerate(lowerCAmelCase_       ):
                 # 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(lowerCAmelCase_							,    lowerCAmelCase_       )
                 _A    =       np.concatenate([latent_model_input, image]							,    axis=1       )

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

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

                 # perform guidance
                 if do_classifier_free_guidance:
                     _A, _A    =       np.split(lowerCAmelCase_							,    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(lowerCAmelCase_       )							,    lowerCAmelCase_							,    lowerCAmelCase_							,    **lowerCAmelCase_       ).prev_sample

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

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

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

         if not return_dict:
             return (image,)

         return ImagePipelineOutput(images=lowerCAmelCase_       )




     def 					lowerCAmelCase__		(						self							,    snake_case_       ):
         _A    =       1 / 0.0_8333 * latents
         _A    =       self.vae(latent_sample=lowerCAmelCase_       )[0]
         _A    =       np.clip(image / 2 + 0.5							,    0							,    1       )
         _A    =       image.transpose((0, 2, 3, 1)       )
         return image




     def 					lowerCAmelCase__		(						self							,    snake_case_							,    snake_case_							,    snake_case_							,    snake_case_							,    snake_case_							,    snake_case_ = None							,    snake_case_ = None							,    ):






         if prompt is not None and isinstance(lowerCAmelCase_							,    lowerCAmelCase_       ):
             _A    =       1
         elif prompt is not None and isinstance(lowerCAmelCase_							,    lowerCAmelCase_       ):
             _A    =       len(lowerCAmelCase_       )
         else:
             _A    =       prompt_embeds.shape[0]

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

             if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(
                 lowerCAmelCase_							,    lowerCAmelCase_       ):
                 _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(lowerCAmelCase_       )							,    )
             _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							,    lowerCAmelCase_       )
         _A    =       prompt_embeds.reshape(bs_embed * num_images_per_prompt							,    lowerCAmelCase_							,    -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(lowerCAmelCase_       ) is not type(lowerCAmelCase_       ):
                 raise TypeError(
                     F"`negative_prompt` should be the same type to `prompt`, but got {type(lowerCAmelCase_       )} !="
                     F" {type(lowerCAmelCase_       )}."       )
             elif isinstance(lowerCAmelCase_							,    lowerCAmelCase_       ):
                 _A    =       [negative_prompt]
             elif batch_size != len(lowerCAmelCase_       ):
                 raise ValueError(
                     F"`negative_prompt`: {negative_prompt} has batch size {len(lowerCAmelCase_       )}, 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(
                 lowerCAmelCase_							,    padding='max_length'							,    max_length=lowerCAmelCase_							,    truncation=lowerCAmelCase_							,    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(lowerCAmelCase_       )							,    )
             _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							,    lowerCAmelCase_       )
             _A    =       uncond_embeddings.reshape(batch_size * num_images_per_prompt							,    lowerCAmelCase_							,    -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










import csv
import dataclasses
import json
from dataclasses import dataclass
from typing import List, Optional, Union

from ...utils import is_tf_available, is_torch_available, logging


__A       : Optional[int]						    = logging.get_logger(__name__)







@dataclass
class     lowerCamelCase:
     '''simple docstring'''


     __magic_name__        = 42
     __magic_name__        = 42
     __magic_name__        = None
     __magic_name__        = None




     def 					lowerCAmelCase__		(						self       ):
         return json.dumps(dataclasses.asdict(self       )							,    indent=2       ) + "\n"







@dataclass(frozen=__snake_case  )
class     lowerCamelCase:
     '''simple docstring'''


     __magic_name__        = 42
     __magic_name__        = None
     __magic_name__        = None
     __magic_name__        = None




     def 					lowerCAmelCase__		(						self       ):
         return json.dumps(dataclasses.asdict(self       )       ) + "\n"







class     lowerCamelCase:
     '''simple docstring'''


     def 					lowerCAmelCase__		(						self							,    snake_case_       ):
         raise NotImplementedError()




     def 					lowerCAmelCase__		(						self							,    snake_case_       ):
         raise NotImplementedError()




     def 					lowerCAmelCase__		(						self							,    snake_case_       ):
         raise NotImplementedError()




     def 					lowerCAmelCase__		(						self							,    snake_case_       ):
         raise NotImplementedError()




     def 					lowerCAmelCase__		(						self       ):
         raise NotImplementedError()




     def 					lowerCAmelCase__		(						self							,    snake_case_       ):
         if len(self.get_labels()       ) > 1:
             _A    =       self.get_labels()[int(example.label       )]
         return example




     @classmethod
     def 					lowerCAmelCase__		(						cls							,    snake_case_							,    snake_case_=None       ):
         with open(snake_case_							,    'r'							,    encoding='utf-8-sig'       ) as f:
             return list(csv.reader(snake_case_							,    delimiter='\t'							,    quotechar=snake_case_       )       )







class     lowerCamelCase(	__snake_case  ):
     '''simple docstring'''


     def __init__(						self							,    snake_case_=None							,    snake_case_=None							,    snake_case_="classification"							,    snake_case_=False       ):
         _A    =       [] if labels is None else labels
         _A    =       [] if examples is None else examples
         _A    =       mode
         _A    =       verbose




     def __len__(						self       ):
         return len(self.examples       )




     def __getitem__(						self							,    snake_case_       ):
         if isinstance(snake_case_							,    snake_case_       ):
             return SingleSentenceClassificationProcessor(labels=self.labels							,    examples=self.examples[idx]       )
         return self.examples[idx]




     @classmethod
     def 					lowerCAmelCase__		(						cls							,    snake_case_							,    snake_case_=""							,    snake_case_=0							,    snake_case_=1							,    snake_case_=None							,    snake_case_=False							,    **snake_case_       ):
         _A    =       cls(**snake_case_       )
         processor.add_examples_from_csv(
             snake_case_							,    split_name=snake_case_							,    column_label=snake_case_							,    column_text=snake_case_							,    column_id=snake_case_							,    skip_first_row=snake_case_							,    overwrite_labels=snake_case_							,    overwrite_examples=snake_case_							,    )
         return processor




     @classmethod
     def 					lowerCAmelCase__		(						cls							,    snake_case_							,    snake_case_=None							,    **snake_case_       ):
         _A    =       cls(**snake_case_       )
         processor.add_examples(snake_case_							,    labels=snake_case_       )
         return processor




     def 					lowerCAmelCase__		(						self							,    snake_case_							,    snake_case_=""							,    snake_case_=0							,    snake_case_=1							,    snake_case_=None							,    snake_case_=False							,    snake_case_=False							,    snake_case_=False							,    ):
         _A    =       self._read_tsv(snake_case_       )
         if skip_first_row:
             _A    =       lines[1:]
         _A    =       []
         _A    =       []
         _A    =       []
         for i, line in enumerate(snake_case_       ):
             texts.append(line[column_text]       )
             labels.append(line[column_label]       )
             if column_id is not None:
                 ids.append(line[column_id]       )
             else:
                 _A    =       F"{split_name}-{i}" if split_name else str(snake_case_       )
                 ids.append(snake_case_       )

         return self.add_examples(
             snake_case_							,    snake_case_							,    snake_case_							,    overwrite_labels=snake_case_							,    overwrite_examples=snake_case_       )




     def 					lowerCAmelCase__		(						self							,    snake_case_							,    snake_case_=None							,    snake_case_=None							,    snake_case_=False							,    snake_case_=False       ):
         if labels is not None and len(snake_case_       ) != len(snake_case_       ):
             raise ValueError(
                 F"Text and labels have mismatched lengths {len(snake_case_       )} and {len(snake_case_       )}"       )
         if ids is not None and len(snake_case_       ) != len(snake_case_       ):
             raise ValueError(F"Text and ids have mismatched lengths {len(snake_case_       )} and {len(snake_case_       )}"       )
         if ids is None:
             _A    =       [None] * len(snake_case_       )
         if labels is None:
             _A    =       [None] * len(snake_case_       )
         _A    =       []
         _A    =       set()
         for text_or_text_and_label, label, guid in zip(snake_case_							,    snake_case_							,    snake_case_       ):
             if isinstance(snake_case_							,    (tuple, list)       ) and label is None:
                 _A, _A    =       text_or_text_and_label
             else:
                 _A    =       text_or_text_and_label
             added_labels.add(snake_case_       )
             examples.append(InputExample(guid=snake_case_							,    text_a=snake_case_							,    text_b=snake_case_							,    label=snake_case_       )       )

         # Update examples
         if overwrite_examples:
             _A    =       examples
         else:
             self.examples.extend(snake_case_       )

         # Update labels
         if overwrite_labels:
             _A    =       list(snake_case_       )
         else:
             _A    =       list(set(self.labels       ).union(snake_case_       )       )

         return self.examples




     def 					lowerCAmelCase__		(						self							,    snake_case_							,    snake_case_=None							,    snake_case_=False							,    snake_case_=0							,    snake_case_=True							,    snake_case_=None							,    ):
         if max_length is None:
             _A    =       tokenizer.max_len

         _A    =       {label: i for i, label in enumerate(self.labels       )}

         _A    =       []
         for ex_index, example in enumerate(self.examples       ):
             if ex_index % 1_0000 == 0:
                 logger.info(F"Tokenizing example {ex_index}"       )

             _A    =       tokenizer.encode(
                 example.text_a							,    add_special_tokens=snake_case_							,    max_length=min(snake_case_							,    tokenizer.max_len       )							,    )
             all_input_ids.append(snake_case_       )

         _A    =       max(len(snake_case_       ) for input_ids in all_input_ids       )

         _A    =       []
         for ex_index, (input_ids, example) in enumerate(zip(snake_case_							,    self.examples       )       ):
             if ex_index % 1_0000 == 0:
                 logger.info(F"Writing example {ex_index}/{len(self.examples       )}"       )
             # The mask has 1 for real tokens and 0 for padding tokens. Only real
             # tokens are attended to.
             _A    =       [1 if mask_padding_with_zero else 0] * len(snake_case_       )

             # Zero-pad up to the sequence length.
             _A    =       batch_length - len(snake_case_       )
             if pad_on_left:
                 _A    =       ([pad_token] * padding_length) + input_ids
                 _A    =       ([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
             else:
                 _A    =       input_ids + ([pad_token] * padding_length)
                 _A    =       attention_mask + ([0 if mask_padding_with_zero else 1] * padding_length)

             if len(snake_case_       ) != batch_length:
                 raise ValueError(F"Error with input length {len(snake_case_       )} vs {batch_length}"       )
             if len(snake_case_       ) != batch_length:
                 raise ValueError(F"Error with input length {len(snake_case_       )} vs {batch_length}"       )

             if self.mode == "classification":
                 _A    =       label_map[example.label]
             elif self.mode == "regression":
                 _A    =       float(example.label       )
             else:
                 raise ValueError(self.mode       )

             if ex_index < 5 and self.verbose:
                 logger.info('*** Example ***'       )
                 logger.info(F"guid: {example.guid}"       )
                 logger.info(F"input_ids: {' '.join([str(snake_case_       ) for x in input_ids]       )}"       )
                 logger.info(F"attention_mask: {' '.join([str(snake_case_       ) for x in attention_mask]       )}"       )
                 logger.info(F"label: {example.label} (id = {label})"       )

             features.append(InputFeatures(input_ids=snake_case_							,    attention_mask=snake_case_							,    label=snake_case_       )       )

         if return_tensors is None:
             return features
         elif return_tensors == "tf":
             if not is_tf_available():
                 raise RuntimeError('return_tensors set to \'tf\' but TensorFlow 2.0 can\'t be imported'       )
             import tensorflow as tf

             def gen():
                 for ex in features:
                     yield ({"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label)

             _A    =       tf.data.Dataset.from_generator(
                 snake_case_							,    ({'input_ids': tf.intaa, 'attention_mask': tf.intaa}, tf.intaa)							,    ({'input_ids': tf.TensorShape([None]       ), 'attention_mask': tf.TensorShape([None]       )}, tf.TensorShape([]       ))							,    )
             return dataset
         elif return_tensors == "pt":
             if not is_torch_available():
                 raise RuntimeError('return_tensors set to \'pt\' but PyTorch can\'t be imported'       )
             import torch
             from torch.utils.data import TensorDataset

             _A    =       torch.tensor([f.input_ids for f in features]							,    dtype=torch.long       )
             _A    =       torch.tensor([f.attention_mask for f in features]							,    dtype=torch.long       )
             if self.mode == "classification":
                 _A    =       torch.tensor([f.label for f in features]							,    dtype=torch.long       )
             elif self.mode == "regression":
                 _A    =       torch.tensor([f.label for f in features]							,    dtype=torch.float       )

             _A    =       TensorDataset(snake_case_							,    snake_case_							,    snake_case_       )
             return dataset
         else:
             raise ValueError('return_tensors should be one of \'tf\' or \'pt\''       )





