




'''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


lowercase			:      Dict			=					getLogger(__name__)


lowercase			:      Union[str, Any]			=					4
lowercase			:      Tuple			=					7

lowercase			:      int			=					{
    """float16""": torch.floataa,
    """float32""": torch.floataa,
}



def 					lowerCamelCase__							(			__lowercase							):
     if isinstance(UpperCamelCase__    ,      torch.Tensor							):
          return image
     elif isinstance(UpperCamelCase__    ,      PIL.Image.Image							):
          snake_case					   = [image]

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

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

     return image







class 				_a   (a__  ):





     '''simple docstring'''



     def __init__(							self       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a = 350       ,)			->							Tuple:
          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       ,__a       ,__a       ,__a = 75       ,__a = 9.0       ,__a = 20       ,__a = None       ,__a = 1       ,__a = 0.0       ,__a = None       ,__a = None       ,__a = None       ,__a = None       ,__a = "pil"       ,__a = True       ,__a = None       ,__a = 1       ,)			->							Dict:
          self.check_inputs(__lowerCamelCase       ,__lowerCamelCase       ,__lowerCamelCase       ,__lowerCamelCase       )

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

          snake_case					   = 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.
          snake_case					   = guidance_scale > 1.0

          # 3. Encode input prompt
          snake_case					   = self._encode_prompt(
              __lowerCamelCase       ,__lowerCamelCase       ,__lowerCamelCase       ,__lowerCamelCase       ,__lowerCamelCase       ,prompt_embeds=__lowerCamelCase       ,negative_prompt_embeds=__lowerCamelCase       ,)

          snake_case					   = ORT_TO_PT_TYPE[str(text_embeddings.dtype       )]

          # 4. Preprocess image
          snake_case					   = preprocess(__lowerCamelCase       )
          snake_case					   = image.cpu()

          # 5. set timesteps
          self.scheduler.set_timesteps(__lowerCamelCase       ,device=__lowerCamelCase       )
          snake_case					   = self.scheduler.timesteps

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

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

          # 6. Prepare latent variables
          snake_case					   = image.shape[2:]
          snake_case					   = 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
          snake_case					   = 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
          snake_case					   = self.prepare_extra_step_kwargs(__lowerCamelCase       ,__lowerCamelCase       )

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

          # 9. Denoising loop
          snake_case					   = 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
                    snake_case					   = np.concatenate([latents] * 2       ) if do_classifier_free_guidance else latents

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

                    # timestep to tensor
                    snake_case					   = np.array([t]       ,dtype=__lowerCamelCase       )

                    # predict the noise residual
                    snake_case					   = 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:
                         snake_case					   = np.split(__lowerCamelCase       ,2       )
                    snake_case					   = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

                    # compute the previous noisy sample x_t -> x_t-1
                    snake_case					   = 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
          snake_case					   = self.decode_latents(latents.float()       )

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

          if not return_dict:
               return (image,)

          return ImagePipelineOutput(images=__lowerCamelCase       )







     def  snake_case_			(							self       ,__a       )			->							Any:
          snake_case					   = 1 / 0.08_333 * latents
          snake_case					   = self.vae(latent_sample=__lowerCamelCase       )[0]
          snake_case					   = np.clip(image / 2 + 0.5       ,0       ,1       )
          snake_case					   = image.transpose((0, 2, 3, 1)       )
          return image







     def  snake_case_			(							self       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a = None       ,__a = None       ,)			->							List[Any]:
          if prompt is not None and isinstance(__lowerCamelCase       ,__lowerCamelCase       ):
               snake_case					   = 1
          elif prompt is not None and isinstance(__lowerCamelCase       ,__lowerCamelCase       ):
               snake_case					   = len(__lowerCamelCase       )
          else:
               snake_case					   = prompt_embeds.shape[0]

          if prompt_embeds is None:
               snake_case					   = self.tokenizer(
                   __lowerCamelCase       ,padding="""max_length"""       ,max_length=self.tokenizer.model_max_length       ,truncation=__lowerCamelCase       ,return_tensors="""pt"""       ,)
               snake_case					   = text_inputs.input_ids
               snake_case					   = 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       ):
                    snake_case					   = 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
               snake_case					   = self.text_encoder(
                   input_ids=text_input_ids.int().to(__lowerCamelCase       )       ,)
               snake_case					   = prompt_embeds[0]

          snake_case					   = prompt_embeds.shape
          # duplicate text embeddings for each generation per prompt, using mps friendly method
          snake_case					   = prompt_embeds.repeat(1       ,__lowerCamelCase       )
          snake_case					   = 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:
               snake_case					   = 42
               if negative_prompt is None:
                    snake_case					   = [""] * 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       ):
                    snake_case					   = [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:
                    snake_case					   = negative_prompt

               snake_case					   = text_input_ids.shape[-1]
               snake_case					   = 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

               snake_case					   = self.text_encoder(
                   input_ids=uncond_input.input_ids.int().to(__lowerCamelCase       )       ,)
               snake_case					   = uncond_embeddings[0]

          if do_classifier_free_guidance:
               snake_case					   = uncond_embeddings.shape[1]
               # duplicate unconditional embeddings for each generation per prompt, using mps friendly method
               snake_case					   = uncond_embeddings.repeat(1       ,__lowerCamelCase       )
               snake_case					   = 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
               snake_case					   = np.concatenate([uncond_embeddings, prompt_embeds]       )

          return prompt_embeds









'''simple docstring'''







import warnings

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.metrics import pairwise_distances

warnings.filterwarnings("""ignore""")

lowercase			:      Any			=					"""K-MEANS-CLUST/ """



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase    ,      __lowercase=None							):
     if seed is not None:  # useful for obtaining consistent results
          np.random.seed(__lowercase							)
     snake_case :			Optional[int]					   = data.shape[0]  # number of data points

     # Pick K indices from range [0, N).
     snake_case :			Tuple					   = np.random.randint(0    ,      __lowercase    ,      __lowercase							)

     # Keep centroids as dense format, as many entries will be nonzero due to averaging.
     # As long as at least one document in a cluster contains a word,
     # it will carry a nonzero weight in the TF-IDF vector of the centroid.
     snake_case :			List[Any]					   = data[rand_indices, :]

     return centroids



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase							):
     return pairwise_distances(__lowercase    ,      __lowercase    ,      metric="""euclidean"""							)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase							):
     # Compute distances between each data point and the set of centroids:
     # Fill in the blank (RHS only)
     snake_case :			Union[str, Any]					   = centroid_pairwise_dist(__lowercase    ,      __lowercase							)

     # Compute cluster assignments for each data point:
     # Fill in the blank (RHS only)
     snake_case :			Any					   = np.argmin(__lowercase    ,      axis=1							)

     return cluster_assignment



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase    ,      __lowercase							):
     snake_case :			List[Any]					   = []
     for i in range(__lowercase							):
          # Select all data points that belong to cluster i. Fill in the blank (RHS only)
          snake_case :			List[str]					   = data[cluster_assignment == i]
          # Compute the mean of the data points. Fill in the blank (RHS only)
          snake_case :			int					   = member_data_points.mean(axis=0							)
          new_centroids.append(__lowercase							)
     snake_case :			Union[str, Any]					   = np.array(__lowercase							)

     return new_centroids



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase    ,      __lowercase    ,      __lowercase							):
     snake_case :			Optional[Any]					   = 0.0
     for i in range(__lowercase							):
          # Select all data points that belong to cluster i. Fill in the blank (RHS only)
          snake_case :			Any					   = data[cluster_assignment == i, :]

          if member_data_points.shape[0] > 0:  # check if i-th cluster is non-empty
               # Compute distances from centroid to data points (RHS only)
               snake_case :			Dict					   = pairwise_distances(
                   __lowercase    ,      [centroids[i]]    ,      metric="""euclidean"""							)
               snake_case :			Optional[int]					   = distances**2
               heterogeneity += np.sum(__lowercase							)

     return heterogeneity



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase							):
     plt.figure(figsize=(7, 4)							)
     plt.plot(__lowercase    ,      linewidth=4							)
     plt.xlabel("""# Iterations"""							)
     plt.ylabel("""Heterogeneity"""							)
     plt.title(F'''Heterogeneity of clustering over time, K={k:d}'''							)
     plt.rcParams.update({"""font.size""": 16}							)
     plt.show()



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase    ,      __lowercase    ,      __lowercase=500    ,      __lowercase=None    ,      __lowercase=False							):
     snake_case :			Tuple					   = initial_centroids[:]
     snake_case :			Dict					   = None

     for itr in range(__lowercase							):
          if verbose:
               print(__lowercase    ,      end=""""""							)

          # 1. Make cluster assignments using nearest centroids
          snake_case :			List[Any]					   = assign_clusters(__lowercase    ,      __lowercase							)

          # 2. Compute a new centroid for each of the k clusters, averaging all data
          #    points assigned to that cluster.
          snake_case :			Optional[int]					   = revise_centroids(__lowercase    ,      __lowercase    ,      __lowercase							)

          # Check for convergence: if none of the assignments changed, stop
          if (
              prev_cluster_assignment is not None
              and (prev_cluster_assignment == cluster_assignment).all()
          ):
               break

          # Print number of new assignments
          if prev_cluster_assignment is not None:
               snake_case :			Tuple					   = np.sum(prev_cluster_assignment != cluster_assignment							)
               if verbose:
                    print(
                        F'''    {num_changed:5d} elements changed their cluster assignment.'''							)

        # Record heterogeneity convergence metric
          if record_heterogeneity is not None:
               # YOUR CODE HERE
               snake_case :			Any					   = compute_heterogeneity(__lowercase    ,      __lowercase    ,      __lowercase    ,      __lowercase							)
               record_heterogeneity.append(__lowercase							)

          snake_case :			Any					   = cluster_assignment[:]

     return centroids, cluster_assignment


# Mock test below
if False:  # change to true to run this test case.
      from sklearn import datasets as ds

      lowercase			:      Optional[int]			=					ds.load_iris()
      lowercase			:      str			=					3
      lowercase			:      Any			=					[]
      lowercase			:      Dict			=					get_initial_centroids(dataset["""data"""], k, seed=0)
      lowercase,  lowercase			:      str			=					kmeans(
          dataset["""data"""],
          k,
          initial_centroids,
          maxiter=400,
          record_heterogeneity=heterogeneity,
          verbose=True,
      )
      plot_heterogeneity(heterogeneity, k)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase    ,      __lowercase=None							):
     # Fill missing values with given rules
     if fill_missing_report:
          snake_case :			List[str]					   = df.fillna(value=__lowercase							)
     snake_case :			List[Any]					   = 1
     snake_case :			Optional[int]					   = df.select_dtypes(np.number							).columns
     snake_case :			int					   = (
         df.groupby(["""Cluster"""]							)[  # construct report dataframe
             numeric_cols
         ]  # group by cluster number
         .agg(
             [
                 ("""sum""", np.sum),
                 ("""mean_with_zeros""", lambda __lowercase							: np.mean(np.nan_to_num(__lowercase							)							)),
                 ("""mean_without_zeros""", lambda __lowercase							: x.replace(0    ,      np.NaN							).mean()),
                 (
                     """mean_25-75""",
                     lambda __lowercase							: np.mean(
                         np.nan_to_num(
                             sorted(__lowercase							)[
                                 round(len(__lowercase							) * 25 / 100							) : round(len(__lowercase							) * 75 / 100							)
                             ]							)							),
                 ),
                 ("""mean_with_na""", np.mean),
                 ("""min""", lambda __lowercase							: x.min()),
                 ("""5%""", lambda __lowercase							: x.quantile(0.05							)),
                 ("""25%""", lambda __lowercase							: x.quantile(0.25							)),
                 ("""50%""", lambda __lowercase							: x.quantile(0.50							)),
                 ("""75%""", lambda __lowercase							: x.quantile(0.75							)),
                 ("""95%""", lambda __lowercase							: x.quantile(0.95							)),
                 ("""max""", lambda __lowercase							: x.max()),
                 ("""count""", lambda __lowercase							: x.count()),
                 ("""stdev""", lambda __lowercase							: x.std()),
                 ("""mode""", lambda __lowercase							: x.mode()[0]),
                 ("""median""", lambda __lowercase							: x.median()),
                 ("""# > 0""", lambda __lowercase							: (x > 0).sum()),
             ]							)
         .T.reset_index()
         .rename(index=__lowercase    ,      columns={"""level_0""": """Features""", """level_1""": """Type"""}							)
     )  # rename columns
     # calculate the size of cluster(count of clientID's)
     snake_case :			List[str]					   = report[
         (report["""Features"""] == """dummy""") & (report["""Type"""] == """count""")
     ].copy()  # avoid SettingWithCopyWarning
     snake_case :			Optional[Any]					   = (
         """ClusterSize"""  # rename created cluster df to match report column names
     )
     snake_case :			Optional[int]					   = """# of Customers"""
     snake_case :			List[str]					   = pd.DataFrame(
         clustersize.iloc[:, 2:].values
         / clustersize.iloc[:, 2:].values.sum()							)
     snake_case :			List[str]					   = """% of Customers"""  # rename created cluster df to match report column names
     snake_case :			List[Any]					   = """ClusterProportion"""
     snake_case :			Tuple					   = clusterproportion.columns.tolist()
     snake_case :			Optional[int]					   = cols[-2:] + cols[:-2]
     snake_case :			Tuple					   = clusterproportion[cols]  # rearrange columns to match report
     snake_case :			Tuple					   = report.columns
     snake_case :			List[str]					   = pd.DataFrame(
         abs(
             report[report["""Type"""] == """count"""].iloc[:, 2:].values
             - clustersize.iloc[:, 2:].values							)							)  # generating df with count of nan values
     snake_case :			Tuple					   = 0
     snake_case :			int					   = """# of nan"""
     snake_case :			int					   = report[
         report["""Type"""] == """count"""
     ].Features.tolist()  # filling values in order to match report
     snake_case :			Optional[Any]					   = a.columns.tolist()
     snake_case :			Dict					   = cols[-2:] + cols[:-2]
     snake_case :			Optional[Any]					   = a[cols]  # rearrange columns to match report
     snake_case :			Tuple					   = report.columns  # rename columns to match report
     snake_case :			Optional[Any]					   = report.drop(
         report[report.Type == """count"""].index							)  # drop count values except cluster size
     snake_case :			List[Any]					   = pd.concat(
         [report, a, clustersize, clusterproportion]    ,      axis=0							)  # concat report with clustert size and nan values
     snake_case :			Optional[int]					   = report["""Features"""].isin(__lowercase							)
     snake_case :			Any					   = report.columns.tolist()
     snake_case :			Union[str, Any]					   = cols[0:2] + cols[-1:] + cols[2:-1]
     snake_case :			List[Any]					   = report[cols]
     snake_case :			Any					   = {
         """ClusterSize""": 9,
         """ClusterProportion""": 8,
         """mean_with_zeros""": 7,
         """mean_with_na""": 6,
         """max""": 5,
         """50%""": 4,
         """min""": 3,
         """25%""": 2,
         """75%""": 1,
         """# of nan""": 0,
         """# > 0""": -1,
         """sum_with_na""": -2,
     }
     snake_case :			List[Any]					   = (
         report.assign(
             Sortera=lambda __lowercase							: x.Type.map(__lowercase							)    ,      Sortera=lambda __lowercase							: list(reversed(range(len(__lowercase							)							)							)							)    ,      )
         .sort_values(["""Sorter1""", """Mark""", """Sorter2"""]    ,      ascending=__lowercase							)
         .drop(["""Sorter1""", """Sorter2"""]    ,      axis=1							)
     )
     snake_case :			str					   = """"""
     snake_case :			Dict					   = report.reset_index()
     snake_case :			Optional[int]					   = report.drop(columns=["""index"""]							)
     return report


if __name__ == "__main__":
      import doctest

      doctest.testmod()



