





import unittest

import numpy as np
import torch
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyPriorPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()
class SCREAMING_SNAKE_CASE_    (       snake_case_							,					unittest.TestCase       ):
						__magic_name__:      List[Any]			   =							KandinskyPriorPipeline
						__magic_name__:      Optional[Any]			   =							["prompt"]
						__magic_name__:      Any			   =							["prompt", "negative_prompt"]
						__magic_name__:      str			   =							[
						    "num_images_per_prompt",
						    "generator",
						    "num_inference_steps",
						    "latents",
						    "negative_prompt",
						    "guidance_scale",
						    "output_type",
						    "return_dict",
						]
						__magic_name__:      Union[str, Any]			   =							False






						@property
						def UpperCAmelCase_					(   self   :   int						)				->     List[Any]:







							"""simple docstring"""


							return 32






						@property
						def UpperCAmelCase_					(   self   :   Union[str, Any]						)				->     Any:







							"""simple docstring"""


							return 32






						@property
						def UpperCAmelCase_					(   self   :   int						)				->     Optional[Any]:







							"""simple docstring"""


							return self.time_input_dim






						@property
						def UpperCAmelCase_					(   self   :   List[Any]						)				->     Tuple:







							"""simple docstring"""


							return self.time_input_dim * 4






						@property
						def UpperCAmelCase_					(   self   :   Optional[Any]						)				->     Dict:







							"""simple docstring"""


							return 100






						@property
						def UpperCAmelCase_					(   self   :   Optional[int]						)				->     Any:







							"""simple docstring"""


							snake_case_		:      Dict           =						CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'						)
							return tokenizer






						@property
						def UpperCAmelCase_					(   self   :   Union[str, Any]						)				->     int:







							"""simple docstring"""


							torch.manual_seed(0						)
							snake_case_		:      List[Any]           =						CLIPTextConfig(
							    bos_token_id=0		, eos_token_id=2		, hidden_size=self.text_embedder_hidden_size		, projection_dim=self.text_embedder_hidden_size		, intermediate_size=37		, layer_norm_eps=1E-05		, num_attention_heads=4		, num_hidden_layers=5		, pad_token_id=1		, vocab_size=1000		, )
							return CLIPTextModelWithProjection(_A						)






						@property
						def UpperCAmelCase_					(   self   :   List[Any]						)				->     Any:







							"""simple docstring"""


							torch.manual_seed(0						)

							snake_case_		:      Any           =						{
							    'num_attention_heads': 2,
							    'attention_head_dim': 12,
							    'embedding_dim': self.text_embedder_hidden_size,
							    'num_layers': 1,
							}

							snake_case_		:      Optional[Any]           =						PriorTransformer(**_A						)
							# clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
							snake_case_		:      Union[str, Any]           =						nn.Parameter(torch.ones(model.clip_std.shape						)						)
							return model






						@property
						def UpperCAmelCase_					(   self   :   str						)				->     Union[str, Any]:







							"""simple docstring"""


							torch.manual_seed(0						)
							snake_case_		:      Optional[Any]           =						CLIPVisionConfig(
							    hidden_size=self.text_embedder_hidden_size		, image_size=224		, projection_dim=self.text_embedder_hidden_size		, intermediate_size=37		, num_attention_heads=4		, num_channels=3		, num_hidden_layers=5		, patch_size=14		, )

							snake_case_		:      Tuple           =						CLIPVisionModelWithProjection(_A						)
							return model






						@property
						def UpperCAmelCase_					(   self   :   Union[str, Any]						)				->     List[str]:







							"""simple docstring"""


							snake_case_		:      Optional[Any]           =						CLIPImageProcessor(
							    crop_size=224		, do_center_crop=_A		, do_normalize=_A		, do_resize=_A		, image_mean=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]		, image_std=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]		, resample=3		, size=224		, )

							return image_processor






						def UpperCAmelCase_					(   self   :   Any						)				->     int:







							"""simple docstring"""


							snake_case_		:      Tuple           =						self.dummy_prior
							snake_case_		:      Optional[int]           =						self.dummy_image_encoder
							snake_case_		:      List[str]           =						self.dummy_text_encoder
							snake_case_		:      List[str]           =						self.dummy_tokenizer
							snake_case_		:      List[str]           =						self.dummy_image_processor

							snake_case_		:      int           =						UnCLIPScheduler(
							    variance_type='fixed_small_log'		, prediction_type='sample'		, num_train_timesteps=1000		, clip_sample=_A		, clip_sample_range=1_0.0		, )

							snake_case_		:      List[str]           =						{
							    'prior': prior,
							    'image_encoder': image_encoder,
							    'text_encoder': text_encoder,
							    'tokenizer': tokenizer,
							    'scheduler': scheduler,
							    'image_processor': image_processor,
							}

							return components






						def UpperCAmelCase_					(   self   :   List[Any]		, _A   :   Optional[int]		, _A   :   Optional[Any]=0						)				->     List[str]:







							"""simple docstring"""


							if str(_A						).startswith('mps'						):
								snake_case_		:      List[Any]           =						torch.manual_seed(_A						)
							else:
								snake_case_		:      int           =						torch.Generator(device=_A						).manual_seed(_A						)
							snake_case_		:      Optional[int]           =						{
							    'prompt': 'horse',
							    'generator': generator,
							    'guidance_scale': 4.0,
							    'num_inference_steps': 2,
							    'output_type': 'np',
							}
							return inputs






						def UpperCAmelCase_					(   self   :   Optional[Any]						)				->     str:







							"""simple docstring"""


							snake_case_		:      Optional[Any]           =						'cpu'

							snake_case_		:      Optional[Any]           =						self.get_dummy_components()

							snake_case_		:      Union[str, Any]           =						self.pipeline_class(**_A						)
							snake_case_		:      str           =						pipe.to(_A						)

							pipe.set_progress_bar_config(disable=_A						)

							snake_case_		:      List[Any]           =						pipe(**self.get_dummy_inputs(_A						)						)
							snake_case_		:      List[str]           =						output.image_embeds

							snake_case_		:      Optional[int]           =						pipe(
							    **self.get_dummy_inputs(_A						)		, return_dict=_A		, )[0]

							snake_case_		:      Tuple           =						image[0, -10:]
							snake_case_		:      Union[str, Any]           =						image_from_tuple[0, -10:]

							assert image.shape == (1, 32)

							snake_case_		:      Optional[Any]           =						np.array(
							    [-0.0_5_3_2, 1.7_1_2_0, 0.3_6_5_6, -1.0_8_5_2, -0.8_9_4_6, -1.1_7_5_6, 0.4_3_4_8, 0.2_4_8_2, 0.5_1_4_6, -0.1_1_5_6]						)

							assert np.abs(image_slice.flatten() - expected_slice						).max() < 1E-2
							assert np.abs(image_from_tuple_slice.flatten() - expected_slice						).max() < 1E-2






						@skip_mps
						def UpperCAmelCase_					(   self   :   List[str]						)				->     Optional[int]:







							"""simple docstring"""


							snake_case_		:      Union[str, Any]           =						torch_device == 'cpu'
							snake_case_		:      Tuple           =						True
							snake_case_		:      str           =						False

							self._test_inference_batch_single_identical(
							    test_max_difference=_A		, relax_max_difference=_A		, test_mean_pixel_difference=_A		, )






						@skip_mps
						def UpperCAmelCase_					(   self   :   List[str]						)				->     Dict:







							"""simple docstring"""


							snake_case_		:      List[str]           =						torch_device == 'cpu'
							snake_case_		:      Tuple           =						False

							self._test_attention_slicing_forward_pass(
							    test_max_difference=_A		, test_mean_pixel_difference=_A		, )






'''simple docstring'''







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


lowercase__  :	str            =      logging.get_logger(__name__)

@dataclass
class    lowerCamelCase_				:






   '''simple docstring'''

   A			:  str
   A			:  str
   A			:  Optional[str]					=						None
   A			:  Optional[str]					=						None


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

@dataclass(frozen=lowerCAmelCase_	)
class    lowerCamelCase_				:






   '''simple docstring'''

   A			:  List[int]
   A			:  Optional[List[int]]					=						None
   A			:  Optional[List[int]]					=						None
   A			:  Optional[Union[int, float]]					=						None


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

class    lowerCamelCase_				:






   '''simple docstring'''

   def 						_UpperCamelCase							(	self  ,    __UpperCamelCase			):
       raise NotImplementedError()


   def 						_UpperCamelCase							(	self  ,    __UpperCamelCase			):
       raise NotImplementedError()


   def 						_UpperCamelCase							(	self  ,    __UpperCamelCase			):
       raise NotImplementedError()


   def 						_UpperCamelCase							(	self  ,    __UpperCamelCase			):
       raise NotImplementedError()


   def 						_UpperCamelCase							(	self			):
       raise NotImplementedError()


   def 						_UpperCamelCase							(	self  ,    __UpperCamelCase			):
       if len(self.get_labels()			) > 1:
           UpperCamelCase__	     =      self.get_labels()[int(example.label			)]
       return example







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

class    lowerCamelCase_				(						lowerCAmelCase_	):






   '''simple docstring'''

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


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


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


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


   @classmethod
   def 						_UpperCamelCase							(	cls  ,    __UpperCamelCase  ,    __UpperCamelCase=None  ,    **__UpperCamelCase			):
       UpperCamelCase__	     =      cls(**__UpperCamelCase			)
       processor.add_examples(__UpperCamelCase  ,    labels=__UpperCamelCase			)
       return processor


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

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


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

       # Update examples
       if overwrite_examples:
           UpperCamelCase__	     =      examples
       else:
           self.examples.extend(__UpperCamelCase			)

       # Update labels
       if overwrite_labels:
           UpperCamelCase__	     =      list(__UpperCamelCase			)
       else:
           UpperCamelCase__	     =      list(set(self.labels			).union(__UpperCamelCase			)			)

       return self.examples







   def 						_UpperCamelCase							(	self  ,    __UpperCamelCase  ,    __UpperCamelCase=None  ,    __UpperCamelCase=False  ,    __UpperCamelCase=0  ,    __UpperCamelCase=True  ,    __UpperCamelCase=None  ,    ):
       if max_length is None:
           UpperCamelCase__	     =      tokenizer.max_len

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

       UpperCamelCase__	     =      []
       for ex_index, example in enumerate(self.examples			):
           if ex_index % 1_00_00 == 0:
               logger.info(f'Tokenizing example {ex_index}'			)

           UpperCamelCase__	     =      tokenizer.encode(
               example.text_a  ,    add_special_tokens=__UpperCamelCase  ,    max_length=min(__UpperCamelCase  ,    tokenizer.max_len			)  ,    )
           all_input_ids.append(__UpperCamelCase			)

       UpperCamelCase__	     =      max(len(__UpperCamelCase			) for input_ids in all_input_ids			)

       UpperCamelCase__	     =      []
       for ex_index, (input_ids, example) in enumerate(zip(__UpperCamelCase  ,    self.examples			)			):
           if ex_index % 1_00_00 == 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.
           UpperCamelCase__	     =      [1 if mask_padding_with_zero else 0] * len(__UpperCamelCase			)

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

           if len(__UpperCamelCase			) != batch_length:
               raise ValueError(f'Error with input length {len(__UpperCamelCase			)} vs {batch_length}'			)
           if len(__UpperCamelCase			) != batch_length:
               raise ValueError(f'Error with input length {len(__UpperCamelCase			)} vs {batch_length}'			)

           if self.mode == "classification":
               UpperCamelCase__	     =      label_map[example.label]
           elif self.mode == "regression":
               UpperCamelCase__	     =      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(__UpperCamelCase			) for x in input_ids]			)}'			)
               logger.info(f'attention_mask: {" ".join([str(__UpperCamelCase			) for x in attention_mask]			)}'			)
               logger.info(f'label: {example.label} (id = {label})'			)

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

       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)

           UpperCamelCase__	     =      tf.data.Dataset.from_generator(
               __UpperCamelCase  ,    ({"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

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

           UpperCamelCase__	     =      TensorDataset(__UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase			)
           return dataset
       else:
           raise ValueError("return_tensors should be one of 'tf' or 'pt'"			)




