






"""simple docstring"""


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 __a   (   snake_case_ ,unittest.TestCase				):
  __lowercase    :    List[Any]						=					KandinskyPriorPipeline
  __lowercase    :    List[str]						=					['prompt']
  __lowercase    :    int						=					['prompt', 'negative_prompt']
  __lowercase    :    Any						=					[
      'num_images_per_prompt',
      'generator',
      'num_inference_steps',
      'latents',
      'negative_prompt',
      'guidance_scale',
      'output_type',
      'return_dict',
  ]
  __lowercase    :    Optional[Any]						=					False



  @property
  def 						A_				(self  :		Optional[int]   )		->							List[Any]:
   return 32



  @property
  def 						A_				(self  :		List[str]   )		->							Optional[int]:
   return 32



  @property
  def 						A_				(self  :		Union[str, Any]   )		->							Optional[int]:
   return self.time_input_dim



  @property
  def 						A_				(self  :		Dict   )		->							Any:
   return self.time_input_dim * 4



  @property
  def 						A_				(self  :		int   )		->							Optional[int]:
   return 100



  @property
  def 						A_				(self  :		Optional[int]   )		->							Optional[int]:
   __A	:       Tuple       =	CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'   )
   return tokenizer



  @property
  def 						A_				(self  :		int   )		->							Tuple:
   torch.manual_seed(0   )
   __A	:       Optional[int]       =	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=1_000				,			)
   return CLIPTextModelWithProjection(lowercase_   )



  @property
  def 						A_				(self  :		Dict   )		->							Optional[int]:
   torch.manual_seed(0   )

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

   __A	:       Tuple       =	PriorTransformer(**lowercase_   )
   # 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
   __A	:       Optional[int]       =	nn.Parameter(torch.ones(model.clip_std.shape   )   )
   return model



  @property
  def 						A_				(self  :		List[str]   )		->							Dict:
   torch.manual_seed(0   )
   __A	:       List[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				,			)

   __A	:       Tuple       =	CLIPVisionModelWithProjection(lowercase_   )
   return model



  @property
  def 						A_				(self  :		Optional[Any]   )		->							int:
   __A	:       Dict       =	CLIPImageProcessor(
       crop_size=224				,			do_center_crop=lowercase_				,			do_normalize=lowercase_				,			do_resize=lowercase_				,			image_mean=[0.4814_5466, 0.457_8275, 0.4082_1073]				,			image_std=[0.2686_2954, 0.2613_0258, 0.2757_7711]				,			resample=3				,			size=224				,			)

   return image_processor



  def 						A_				(self  :		List[Any]   )		->							Dict:
   __A	:       Dict       =	self.dummy_prior
   __A	:       str       =	self.dummy_image_encoder
   __A	:       Optional[Any]       =	self.dummy_text_encoder
   __A	:       Union[str, Any]       =	self.dummy_tokenizer
   __A	:       Tuple       =	self.dummy_image_processor

   __A	:       List[str]       =	UnCLIPScheduler(
       variance_type='fixed_small_log'				,			prediction_type='sample'				,			num_train_timesteps=1_000				,			clip_sample=lowercase_				,			clip_sample_range=10.0				,			)

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

   return components



  def 						A_				(self  :		Optional[Any]				,			lowercase_  :		Optional[int]				,			lowercase_  :		str=0   )		->							List[str]:
   if str(lowercase_   ).startswith('mps'   ):
    __A	:       Optional[Any]       =	torch.manual_seed(lowercase_   )
   else:
    __A	:       Dict       =	torch.Generator(device=lowercase_   ).manual_seed(lowercase_   )
   __A	:       List[str]       =	{
       'prompt': 'horse',
       'generator': generator,
       'guidance_scale': 4.0,
       'num_inference_steps': 2,
       'output_type': 'np',
   }
   return inputs



  def 						A_				(self  :		str   )		->							Dict:
   __A	:       List[str]       =	'cpu'

   __A	:       Tuple       =	self.get_dummy_components()

   __A	:       int       =	self.pipeline_class(**lowercase_   )
   __A	:       Optional[int]       =	pipe.to(lowercase_   )

   pipe.set_progress_bar_config(disable=lowercase_   )

   __A	:       Tuple       =	pipe(**self.get_dummy_inputs(lowercase_   )   )
   __A	:       Union[str, Any]       =	output.image_embeds

   __A	:       List[Any]       =	pipe(
       **self.get_dummy_inputs(lowercase_   )				,			return_dict=lowercase_				,			)[0]

   __A	:       Optional[Any]       =	image[0, -10:]
   __A	:       Optional[Any]       =	image_from_tuple[0, -10:]

   assert image.shape == (1, 32)

   __A	:       Union[str, Any]       =	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  :		str   )		->							Optional[Any]:
   __A	:       Union[str, Any]       =	torch_device == 'cpu'
   __A	:       int       =	True
   __A	:       Union[str, Any]       =	False

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



  @skip_mps
  def 						A_				(self  :		Optional[int]   )		->							int:
   __A	:       str       =	torch_device == 'cpu'
   __A	:       Optional[int]       =	False

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













"""simple docstring"""


import csv
import json
import os

import datasets


# TODO: Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_UpperCamelCase   :						List[Any]     						= """\
@InProceedings{huggingface:dataset,
title = {A great new dataset},
author={huggingface, Inc.
},
year={2020}
}
"""

# TODO: Add description of the dataset here
# You can copy an official description
_UpperCamelCase   :						Any     						= """\
This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
"""

# TODO: Add a link to an official homepage for the dataset here
_UpperCamelCase   :						Dict     						= """"""

# TODO: Add the licence for the dataset here if you can find it
_UpperCamelCase   :						Tuple     						= """"""

# TODO: Add link to the official dataset URLs here
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
_UpperCamelCase   :						str     						= {
    """first_domain""": """https://huggingface.co/great-new-dataset-first_domain.zip""",
    """second_domain""": """https://huggingface.co/great-new-dataset-second_domain.zip""",
}






class __a   (   datasets.GeneratorBasedBuilder				):
  __lowercase    :    int						=					datasets.Version('1.1.0'				)

  # This is an example of a dataset with multiple configurations.
  # If you don't want/need to define several sub-sets in your dataset,
  # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.

  # If you need to make complex sub-parts in the datasets with configurable options
  # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
  # BUILDER_CONFIG_CLASS = MyBuilderConfig

  # You will be able to load one or the other configurations in the following list with
  # data = datasets.load_dataset('my_dataset', 'first_domain')
  # data = datasets.load_dataset('my_dataset', 'second_domain')
  __lowercase    :    Optional[Any]						=					[
      datasets.BuilderConfig(name='first_domain' ,version=snake_case_ ,description='This part of my dataset covers a first domain'				),
      datasets.BuilderConfig(name='second_domain' ,version=snake_case_ ,description='This part of my dataset covers a second domain'				),
  ]

  __lowercase    :    Any						=					'first_domain'  # It's not mandatory to have a default configuration. Just use one if it make sense.



  def 						A_				(self  :		Optional[Any]   )		->							List[Any]:
   # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
   if self.config.name == "first_domain":  # This is the name of the configuration selected in BUILDER_CONFIGS above
    __A	:       Dict       =	datasets.Features(
        {
            'sentence': datasets.Value('string'   ),
            'option1': datasets.Value('string'   ),
            'answer': datasets.Value('string'   )
            # These are the features of your dataset like images, labels ...
        }   )
   else:  # This is an example to show how to have different features for "first_domain" and "second_domain"
    __A	:       Optional[int]       =	datasets.Features(
        {
            'sentence': datasets.Value('string'   ),
            'option2': datasets.Value('string'   ),
            'second_domain_answer': datasets.Value('string'   )
            # These are the features of your dataset like images, labels ...
        }   )
   return datasets.DatasetInfo(
       # This is the description that will appear on the datasets page.
       description=_DESCRIPTION				,			features=lowercase_				,			homepage=_HOMEPAGE				,			license=_LICENSE				,			citation=_CITATION				,			)



  def 						A_				(self  :		Dict				,			lowercase_  :		Union[str, Any]   )		->							Union[str, Any]:
   # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
   # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name

   # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
   # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
   # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
   __A	:       Optional[int]       =	_URLS[self.config.name]
   __A	:       Any       =	dl_manager.download_and_extract(lowercase_   )
   return [
       datasets.SplitGenerator(
           name=datasets.Split.TRAIN				,			gen_kwargs={
               'filepath': os.path.join(lowercase_				,			'train.jsonl'   ),
               'split': 'train',
           }				,			),
       datasets.SplitGenerator(
           name=datasets.Split.VALIDATION				,			gen_kwargs={
               'filepath': os.path.join(lowercase_				,			'dev.jsonl'   ),
               'split': 'dev',
           }				,			),
       datasets.SplitGenerator(
           name=datasets.Split.TEST				,			gen_kwargs={
               'filepath': os.path.join(lowercase_				,			'test.jsonl'   ),
               'split': 'test'
           }				,			),
   ]



  def 						A_				(self  :		Tuple				,			lowercase_  :		Dict				,			lowercase_  :		Optional[Any]   )		->							Tuple:
   # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
   # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
   with open(lowercase_				,			encoding='utf-8'   ) as f:
    for key, row in enumerate(lowercase_   ):
     __A	:       int       =	json.loads(lowercase_   )
     if self.config.name == "first_domain":
      # Yields examples as (key, example) tuples
      yield key, {
          "sentence": data["sentence"],
          "option1": data["option1"],
          "answer": "" if split == "test" else data["answer"],
      }
     else:
      yield key, {
          "sentence": data["sentence"],
          "option2": data["option2"],
          "second_domain_answer": "" if split == "test" else data["second_domain_answer"],
      }





