


'''simple docstring'''

import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Union

import tqdm
from filelock import FileLock

from transformers import (
    BartTokenizer,
    BartTokenizerFast,
    DataProcessor,
    PreTrainedTokenizer,
    RobertaTokenizer,
    RobertaTokenizerFast,
    XLMRobertaTokenizer,
    is_tf_available,
    is_torch_available,
)


lowerCamelCase							:   Optional[Any]  						=	logging.getLogger(__name__)
@dataclass(frozen=lowercase_     )
class   __lowerCAmelCase						:





      '''simple docstring'''






      lowerCAmelCase__					:     str
      lowerCAmelCase__					:     str
      lowerCAmelCase__					:     Optional[str]  					=							None
      lowerCAmelCase__					:     Optional[str]  					=							None
      lowerCAmelCase__					:     Optional[str]  					=							None






@dataclass(frozen=lowercase_     )
class   __lowerCAmelCase						:





      '''simple docstring'''






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


if is_torch_available():
 import torch
 from torch.utils.data import Dataset
 class   __lowerCAmelCase						(lowercase_     ):





       '''simple docstring'''






       lowerCAmelCase__					:     List[InputFeatures]



       def __init__(self    :    Optional[int]   ,						UpperCamelCase    :    str   ,						UpperCamelCase    :    PreTrainedTokenizer   ,						UpperCamelCase    :    str   ,						UpperCamelCase    :    Optional[int] = None   ,						UpperCamelCase    :    List[Any]=False   ,						UpperCamelCase    :    bool = False   ,						):

              '''simple docstring'''







              lowercase__      			= hans_processors[task]()

              lowercase__      			= os.path.join(
                  UpperCamelCase   ,						'''cached_{}_{}_{}_{}'''.format(
                      '''dev''' if evaluate else '''train'''   ,						tokenizer.__class__.__name__   ,						str(UpperCamelCase )   ,						UpperCamelCase   ,						)   ,						)
              lowercase__      			= processor.get_labels()
              if tokenizer.__class__ in (
                  RobertaTokenizer,
                  RobertaTokenizerFast,
                  XLMRobertaTokenizer,
                  BartTokenizer,
                  BartTokenizerFast,
              ):
                     # HACK(label indices are swapped in RoBERTa pretrained model)
                     lowercase__			,lowercase__      			= label_list[2], label_list[1]
              lowercase__      			= label_list

              # Make sure only the first process in distributed training processes the dataset,
              # and the others will use the cache.
              lowercase__      			= cached_features_file + '''.lock'''
              with FileLock(UpperCamelCase ):
                     if os.path.exists(UpperCamelCase ) and not overwrite_cache:
                            logger.info(f"Loading features from cached file {cached_features_file}" )
                            lowercase__      			= torch.load(UpperCamelCase )
                     else:
                            logger.info(f"Creating features from dataset file at {data_dir}" )

                            lowercase__      			= (
                                processor.get_dev_examples(UpperCamelCase ) if evaluate else processor.get_train_examples(UpperCamelCase )
                            )

                            logger.info('''Training examples: %s'''   ,						len(UpperCamelCase ) )
                            lowercase__      			= hans_convert_examples_to_features(UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase )
                            logger.info('''Saving features into cached file %s'''   ,						UpperCamelCase )
                            torch.save(self.features   ,						UpperCamelCase )



       def __len__(self    :    Optional[int] ):

              '''simple docstring'''







              return len(self.features )



       def __getitem__(self    :    Tuple   ,						UpperCamelCase    :    str ):

              '''simple docstring'''







              return self.features[i]






       def       UpperCamelCase__		(self    :    str ):

              '''simple docstring'''







              return self.label_list


if is_tf_available():
 import tensorflow as tf
 class   __lowerCAmelCase						:





       '''simple docstring'''






       lowerCAmelCase__					:     List[InputFeatures]



       def __init__(self    :    List[Any]   ,						UpperCamelCase    :    str   ,						UpperCamelCase    :    PreTrainedTokenizer   ,						UpperCamelCase    :    str   ,						UpperCamelCase    :    Optional[int] = 128   ,						UpperCamelCase    :    Union[str, Any]=False   ,						UpperCamelCase    :    bool = False   ,						):

              '''simple docstring'''







              lowercase__      			= hans_processors[task]()
              lowercase__      			= processor.get_labels()
              if tokenizer.__class__ in (
                  RobertaTokenizer,
                  RobertaTokenizerFast,
                  XLMRobertaTokenizer,
                  BartTokenizer,
                  BartTokenizerFast,
              ):
                     # HACK(label indices are swapped in RoBERTa pretrained model)
                     lowercase__			,lowercase__      			= label_list[2], label_list[1]
              lowercase__      			= label_list

              lowercase__      			= processor.get_dev_examples(UpperCamelCase ) if evaluate else processor.get_train_examples(UpperCamelCase )
              lowercase__      			= hans_convert_examples_to_features(UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase )

              def gen():
                     for ex_index, ex in tqdm.tqdm(enumerate(self.features )   ,						desc='''convert examples to features''' ):
                            if ex_index % 10000 == 0:
                                   logger.info('''Writing example %d of %d''' % (ex_index, len(UpperCamelCase )) )

                            yield (
                                {
                                    "example_id": 0,
                                    "input_ids": ex.input_ids,
                                    "attention_mask": ex.attention_mask,
                                    "token_type_ids": ex.token_type_ids,
                                },
                                ex.label,
                            )

              lowercase__      			= tf.data.Dataset.from_generator(
                  UpperCamelCase   ,						(
                      {
                          '''example_id''': tf.intaa,
                          '''input_ids''': tf.intaa,
                          '''attention_mask''': tf.intaa,
                          '''token_type_ids''': tf.intaa,
                      },
                      tf.intaa,
                  )   ,						(
                      {
                          '''example_id''': tf.TensorShape([] ),
                          '''input_ids''': tf.TensorShape([None, None] ),
                          '''attention_mask''': tf.TensorShape([None, None] ),
                          '''token_type_ids''': tf.TensorShape([None, None] ),
                      },
                      tf.TensorShape([] ),
                  )   ,						)



       def       UpperCamelCase__		(self    :    Optional[int] ):

              '''simple docstring'''







              return self.dataset



       def __len__(self    :    List[Any] ):

              '''simple docstring'''







              return len(self.features )



       def __getitem__(self    :    Union[str, Any]   ,						UpperCamelCase    :    Tuple ):

              '''simple docstring'''







              return self.features[i]






       def       UpperCamelCase__		(self    :    Union[str, Any] ):

              '''simple docstring'''







              return self.label_list
class   __lowerCAmelCase						(lowercase_     ):





      '''simple docstring'''






      def       UpperCamelCase__		(self    :    Optional[int]   ,						UpperCamelCase    :    Any ):

             '''simple docstring'''







             return self._create_examples(self._read_tsv(os.path.join(UpperCamelCase   ,						'''heuristics_train_set.txt''' ) )   ,						'''train''' )



      def       UpperCamelCase__		(self    :    Optional[int]   ,						UpperCamelCase    :    Optional[Any] ):

             '''simple docstring'''







             return self._create_examples(self._read_tsv(os.path.join(UpperCamelCase   ,						'''heuristics_evaluation_set.txt''' ) )   ,						'''dev''' )



      def       UpperCamelCase__		(self    :    Dict ):

             '''simple docstring'''







             return ["contradiction", "entailment", "neutral"]






      def       UpperCamelCase__		(self    :    Optional[Any]   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    Optional[int] ):

             '''simple docstring'''







             lowercase__      			= []
             for i, line in enumerate(UpperCamelCase ):
                    if i == 0:
                           continue
                    lowercase__      			= '''%s-%s''' % (set_type, line[0])
                    lowercase__      			= line[5]
                    lowercase__      			= line[6]
                    lowercase__      			= line[7][2:] if line[7].startswith('''ex''' ) else line[7]
                    lowercase__      			= line[0]
                    examples.append(InputExample(guid=UpperCamelCase   ,						text_a=UpperCamelCase   ,						text_b=UpperCamelCase   ,						label=UpperCamelCase   ,						pairID=UpperCamelCase ) )
             return examples






def _SCREAMING_SNAKE_CASE      (A  , A  , A  , A  , )       ->       int:






       """simple docstring"""







       lowercase__      			= {label: i for i, label in enumerate(A       )}

       lowercase__      			= []
       for ex_index, example in tqdm.tqdm(enumerate(A       )  , desc='''convert examples to features'''       ):
              if ex_index % 10_000 == 0:
                     logger.info('''Writing example %d''' % (ex_index)       )

              lowercase__      			= tokenizer(
                  example.text_a  , example.text_b  , add_special_tokens=A  , max_length=A  , padding='''max_length'''  , truncation=A  , return_overflowing_tokens=A  , )

              lowercase__      			= label_map[example.label] if example.label in label_map else 0

              lowercase__      			= int(example.pairID       )

              features.append(InputFeatures(**A  , label=A  , pairID=A       )       )

       for i, example in enumerate(examples[:5]       ):
              logger.info('''*** Example ***'''       )
              logger.info(f"guid: {example}"       )
              logger.info(f"features: {features[i]}"       )

       return features


lowerCamelCase							:   Tuple  						=	{
    'hans': 3,
}

lowerCamelCase							:   str  						=	{
    'hans': HansProcessor,
}






import unittest

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

from diffusers import KandinskyVaaPriorPipeline, 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 				lowercase		(					SCREAMING_SNAKE_CASE__       ,  unittest.TestCase		):
							lowercase_       :       Any       					=KandinskyVaaPriorPipeline
							lowercase_       :       str       					=['''prompt''']
							lowercase_       :       Union[str, Any]       					=['''prompt''', '''negative_prompt''']
							lowercase_       :       Dict       					=[
							    '''num_images_per_prompt''',
							    '''generator''',
							    '''num_inference_steps''',
							    '''latents''',
							    '''negative_prompt''',
							    '''guidance_scale''',
							    '''output_type''',
							    '''return_dict''',
							]
							lowercase_       :       List[str]       					=False




							@property
							def  A__      (				self):
												return 3_2




							@property
							def  A__      (				self):
												return 3_2




							@property
							def  A__      (				self):
												return self.time_input_dim




							@property
							def  A__      (				self):
												return self.time_input_dim * 4




							@property
							def  A__      (				self):
												return 1_0_0




							@property
							def  A__      (				self):
												lowercase							=						CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''')
												return tokenizer




							@property
							def  A__      (				self):
												torch.manual_seed(0)
												lowercase							=						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=3_7   ,layer_norm_eps=1E-05   ,num_attention_heads=4   ,num_hidden_layers=5   ,pad_token_id=1   ,vocab_size=1_0_0_0   ,)
												return CLIPTextModelWithProjection(A__)




							@property
							def  A__      (				self):
												torch.manual_seed(0)

												lowercase							=						{
												    '''num_attention_heads''': 2,
												    '''attention_head_dim''': 1_2,
												    '''embedding_dim''': self.text_embedder_hidden_size,
												    '''num_layers''': 1,
												}

												lowercase							=						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
												lowercase							=						nn.Parameter(torch.ones(model.clip_std.shape))
												return model




							@property
							def  A__      (				self):
												torch.manual_seed(0)
												lowercase							=						CLIPVisionConfig(
												    hidden_size=self.text_embedder_hidden_size   ,image_size=2_2_4   ,projection_dim=self.text_embedder_hidden_size   ,intermediate_size=3_7   ,num_attention_heads=4   ,num_channels=3   ,num_hidden_layers=5   ,patch_size=1_4   ,)

												lowercase							=						CLIPVisionModelWithProjection(A__)
												return model




							@property
							def  A__      (				self):
												lowercase							=						CLIPImageProcessor(
												    crop_size=2_2_4   ,do_center_crop=A__   ,do_normalize=A__   ,do_resize=A__   ,image_mean=[0.48145466, 0.4578275, 0.40821073]   ,image_std=[0.26862954, 0.26130258, 0.27577711]   ,resample=3   ,size=2_2_4   ,)

												return image_processor




							def  A__      (				self):
												lowercase							=						self.dummy_prior
												lowercase							=						self.dummy_image_encoder
												lowercase							=						self.dummy_text_encoder
												lowercase							=						self.dummy_tokenizer
												lowercase							=						self.dummy_image_processor

												lowercase							=						UnCLIPScheduler(
												    variance_type='''fixed_small_log'''   ,prediction_type='''sample'''   ,num_train_timesteps=1_0_0_0   ,clip_sample=A__   ,clip_sample_range=10.0   ,)

												lowercase							=						{
												    '''prior''': prior,
												    '''image_encoder''': image_encoder,
												    '''text_encoder''': text_encoder,
												    '''tokenizer''': tokenizer,
												    '''scheduler''': scheduler,
												    '''image_processor''': image_processor,
												}

												return components




							def  A__      (				self   ,A__   ,A__=0):
												if str(A__).startswith('''mps'''):
																	lowercase							=						torch.manual_seed(A__)
												else:
																	lowercase							=						torch.Generator(device=A__).manual_seed(A__)
												lowercase							=						{
												    '''prompt''': '''horse''',
												    '''generator''': generator,
												    '''guidance_scale''': 4.0,
												    '''num_inference_steps''': 2,
												    '''output_type''': '''np''',
												}
												return inputs




							def  A__      (				self):
												lowercase							=						'''cpu'''

												lowercase							=						self.get_dummy_components()

												lowercase							=						self.pipeline_class(**A__)
												lowercase							=						pipe.to(A__)

												pipe.set_progress_bar_config(disable=A__)

												lowercase							=						pipe(**self.get_dummy_inputs(A__))
												lowercase							=						output.image_embeds

												lowercase							=						pipe(
												    **self.get_dummy_inputs(A__)   ,return_dict=A__   ,)[0]

												lowercase							=						image[0, -1_0:]
												lowercase							=						image_from_tuple[0, -1_0:]

												assert image.shape == (1, 3_2)

												lowercase							=						np.array(
												    [-0.0532, 1.7120, 0.3656, -1.0852, -0.8946, -1.1756, 0.4348, 0.2482, 0.5146, -0.1156])

												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  A__      (				self):
												lowercase							=						torch_device == '''cpu'''
												lowercase							=						True
												lowercase							=						False

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





							@skip_mps
							def  A__      (				self):
												lowercase							=						torch_device == '''cpu'''
												lowercase							=						False

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




