
"""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_      (lowercase__							,unittest.TestCase ):




  '''simple docstring'''
  SCREAMING_SNAKE_CASE__     :		str    	=						KandinskyPriorPipeline
  SCREAMING_SNAKE_CASE__     :		Any    	=						["""prompt"""]
  SCREAMING_SNAKE_CASE__     :		List[str]    	=						["""prompt""", """negative_prompt"""]
  SCREAMING_SNAKE_CASE__     :		int    	=						[
      """num_images_per_prompt""",
      """generator""",
      """num_inference_steps""",
      """latents""",
      """negative_prompt""",
      """guidance_scale""",
      """output_type""",
      """return_dict""",
  ]
  SCREAMING_SNAKE_CASE__     :		Tuple    	=						False







  @property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     return 32







  @property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     return 32







  @property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     return self.time_input_dim







  @property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     return self.time_input_dim * 4







  @property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     return 100







  @property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   List[str]     	=       CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"    )
     return tokenizer







  @property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     torch.manual_seed(0    )
     UpperCAmelCase_   :   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-0_5							,  num_attention_heads=4							,  num_hidden_layers=5							,  pad_token_id=1							,  vocab_size=1000							,  )
     return CLIPTextModelWithProjection(lowercase_    )







  @property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     torch.manual_seed(0    )

     UpperCAmelCase_   :   Dict     	=       {
         "num_attention_heads": 2,
         "attention_head_dim": 12,
         "embedding_dim": self.text_embedder_hidden_size,
         "num_layers": 1,
     }

     UpperCAmelCase_   :   Dict     	=       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
     UpperCAmelCase_   :   List[Any]     	=       nn.Parameter(torch.ones(model.clip_std.shape    )    )
     return model







  @property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     torch.manual_seed(0    )
     UpperCAmelCase_   :   Tuple     	=       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							,  )

     UpperCAmelCase_   :   List[Any]     	=       CLIPVisionModelWithProjection(lowercase_    )
     return model







  @property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   str     	=       CLIPImageProcessor(
         crop_size=224							,  do_center_crop=lowercase_							,  do_normalize=lowercase_							,  do_resize=lowercase_							,  image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]							,  image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]							,  resample=3							,  size=224							,  )

     return image_processor







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Optional[Any]     	=       self.dummy_prior
     UpperCAmelCase_   :   int     	=       self.dummy_image_encoder
     UpperCAmelCase_   :   Union[str, Any]     	=       self.dummy_text_encoder
     UpperCAmelCase_   :   List[Any]     	=       self.dummy_tokenizer
     UpperCAmelCase_   :   List[Any]     	=       self.dummy_image_processor

     UpperCAmelCase_   :   Dict     	=       UnCLIPScheduler(
         variance_type="fixed_small_log"							,  prediction_type="sample"							,  num_train_timesteps=1000							,  clip_sample=lowercase_							,  clip_sample_range=10.0							,  )

     UpperCAmelCase_   :   Optional[int]     	=       {
         "prior": prior,
         "image_encoder": image_encoder,
         "text_encoder": text_encoder,
         "tokenizer": tokenizer,
         "scheduler": scheduler,
         "image_processor": image_processor,
     }

     return components







  def 			UpperCamelCase__   (						self							,  lowercase_							,  lowercase_=0    ):

     """simple docstring"""
     if str(lowercase_    ).startswith("mps"    ):
        UpperCAmelCase_   :   List[str]     	=       torch.manual_seed(lowercase_    )
     else:
        UpperCAmelCase_   :   Dict     	=       torch.Generator(device=lowercase_    ).manual_seed(lowercase_    )
     UpperCAmelCase_   :   Any     	=       {
         "prompt": "horse",
         "generator": generator,
         "guidance_scale": 4.0,
         "num_inference_steps": 2,
         "output_type": "np",
     }
     return inputs







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Optional[int]     	=       "cpu"

     UpperCAmelCase_   :   str     	=       self.get_dummy_components()

     UpperCAmelCase_   :   Optional[int]     	=       self.pipeline_class(**lowercase_    )
     UpperCAmelCase_   :   Union[str, Any]     	=       pipe.to(lowercase_    )

     pipe.set_progress_bar_config(disable=lowercase_    )

     UpperCAmelCase_   :   Optional[int]     	=       pipe(**self.get_dummy_inputs(lowercase_    )    )
     UpperCAmelCase_   :   int     	=       output.image_embeds

     UpperCAmelCase_   :   Union[str, Any]     	=       pipe(
         **self.get_dummy_inputs(lowercase_    )							,  return_dict=lowercase_							,  )[0]

     UpperCAmelCase_   :   Optional[Any]     	=       image[0, -10:]
     UpperCAmelCase_   :   List[str]     	=       image_from_tuple[0, -10:]

     assert image.shape == (1, 32)

     UpperCAmelCase_   :   Optional[int]     	=       np.array(
         [-0.05_32, 1.71_20, 0.36_56, -1.08_52, -0.89_46, -1.17_56, 0.43_48, 0.24_82, 0.51_46, -0.11_56]    )

     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    ):

     """simple docstring"""
     UpperCAmelCase_   :   Union[str, Any]     	=       torch_device == "cpu"
     UpperCAmelCase_   :   Union[str, Any]     	=       True
     UpperCAmelCase_   :   Optional[Any]     	=       False

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







  @skip_mps
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Optional[int]     	=       torch_device == "cpu"
     UpperCAmelCase_   :   str     	=       False

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







import json
import os
import shutil
import tempfile
import unittest

from transformers import is_speech_available, is_torch_available
from transformers.models.speechta import SpeechTaTokenizer
from transformers.testing_utils import get_tests_dir, require_torch
from transformers.utils import FEATURE_EXTRACTOR_NAME


if is_speech_available() and is_torch_available():
	from transformers import SpeechTaFeatureExtractor, SpeechTaProcessor

	from .test_feature_extraction_speechta import floats_list


A__       =			get_tests_dir("""fixtures/test_sentencepiece_bpe_char.model""")





@require_torch
class    __lowerCAmelCase					(	unittest.TestCase			):




				def 				snake_case  (				self						):



									"""simple docstring"""





									_lowerCAmelCase         =       tempfile.mkdtemp()

									_lowerCAmelCase         =       SpeechTaTokenizer(_snake_case						)
									tokenizer.save_pretrained(self.tmpdirname						)

									_lowerCAmelCase         =       {
									    """feature_size""": 1,
									    """padding_value""": 0.0,
									    """sampling_rate""": 16000,
									    """do_normalize""": False,
									    """num_mel_bins""": 80,
									    """hop_length""": 16,
									    """win_length""": 64,
									    """win_function""": """hann_window""",
									    """fmin""": 80,
									    """fmax""": 7600,
									    """mel_floor""": 1e-10,
									    """reduction_factor""": 2,
									    """return_attention_mask""": True,
									}

									_lowerCAmelCase         =       os.path.join(self.tmpdirname		,							_snake_case						)
									with open(self.feature_extraction_file		,							"""w"""		,							encoding="""utf-8"""						) as fp:
														fp.write(json.dumps(_snake_case						) + """\n"""						)




				def 				snake_case  (				self		,							**_snake_case						):



									"""simple docstring"""





									return SpeechTaTokenizer.from_pretrained(self.tmpdirname		,							**_snake_case						)




				def 				snake_case  (				self		,							**_snake_case						):



									"""simple docstring"""





									return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname		,							**_snake_case						)




				def 				snake_case  (				self						):



									"""simple docstring"""





									shutil.rmtree(self.tmpdirname						)




				def 				snake_case  (				self						):



									"""simple docstring"""





									_lowerCAmelCase         =       self.get_tokenizer()
									_lowerCAmelCase         =       self.get_feature_extractor()

									_lowerCAmelCase         =       SpeechTaProcessor(tokenizer=_snake_case		,							feature_extractor=_snake_case						)

									processor.save_pretrained(self.tmpdirname						)
									_lowerCAmelCase         =       SpeechTaProcessor.from_pretrained(self.tmpdirname						)

									self.assertEqual(processor.tokenizer.get_vocab()		,							tokenizer.get_vocab()						)
									self.assertIsInstance(processor.tokenizer		,							_snake_case						)

									self.assertEqual(processor.feature_extractor.to_json_string()		,							feature_extractor.to_json_string()						)
									self.assertIsInstance(processor.feature_extractor		,							_snake_case						)




				def 				snake_case  (				self						):



									"""simple docstring"""





									_lowerCAmelCase         =       SpeechTaProcessor(tokenizer=self.get_tokenizer()		,							feature_extractor=self.get_feature_extractor()						)
									processor.save_pretrained(self.tmpdirname						)

									_lowerCAmelCase         =       self.get_tokenizer(bos_token="""(BOS)"""		,							eos_token="""(EOS)"""						)
									_lowerCAmelCase         =       self.get_feature_extractor(do_normalize=_snake_case		,							padding_value=1.0						)

									_lowerCAmelCase         =       SpeechTaProcessor.from_pretrained(
									    self.tmpdirname		,							bos_token="""(BOS)"""		,							eos_token="""(EOS)"""		,							do_normalize=_snake_case		,							padding_value=1.0						)

									self.assertEqual(processor.tokenizer.get_vocab()		,							tokenizer_add_kwargs.get_vocab()						)
									self.assertIsInstance(processor.tokenizer		,							_snake_case						)

									self.assertEqual(processor.feature_extractor.to_json_string()		,							feature_extractor_add_kwargs.to_json_string()						)
									self.assertIsInstance(processor.feature_extractor		,							_snake_case						)




				def 				snake_case  (				self						):



									"""simple docstring"""





									_lowerCAmelCase         =       self.get_feature_extractor()
									_lowerCAmelCase         =       self.get_tokenizer()

									_lowerCAmelCase         =       SpeechTaProcessor(tokenizer=_snake_case		,							feature_extractor=_snake_case						)

									_lowerCAmelCase         =       floats_list((3, 1000)						)

									_lowerCAmelCase         =       feature_extractor(audio=_snake_case		,							return_tensors="""np"""						)
									_lowerCAmelCase         =       processor(audio=_snake_case		,							return_tensors="""np"""						)

									for key in input_feat_extract.keys():
														self.assertAlmostEqual(input_feat_extract[key].sum()		,							input_processor[key].sum()		,							delta=1e-2						)




				def 				snake_case  (				self						):



									"""simple docstring"""





									_lowerCAmelCase         =       self.get_feature_extractor()
									_lowerCAmelCase         =       self.get_tokenizer()

									_lowerCAmelCase         =       SpeechTaProcessor(tokenizer=_snake_case		,							feature_extractor=_snake_case						)

									_lowerCAmelCase         =       floats_list((3, 1000)						)

									_lowerCAmelCase         =       feature_extractor(audio_target=_snake_case		,							return_tensors="""np"""						)
									_lowerCAmelCase         =       processor(audio_target=_snake_case		,							return_tensors="""np"""						)

									for key in input_feat_extract.keys():
														self.assertAlmostEqual(input_feat_extract[key].sum()		,							input_processor[key].sum()		,							delta=1e-2						)




				def 				snake_case  (				self						):



									"""simple docstring"""





									_lowerCAmelCase         =       self.get_feature_extractor()
									_lowerCAmelCase         =       self.get_tokenizer()

									_lowerCAmelCase         =       SpeechTaProcessor(tokenizer=_snake_case		,							feature_extractor=_snake_case						)

									_lowerCAmelCase         =       """This is a test string"""

									_lowerCAmelCase         =       processor(text=_snake_case						)
									_lowerCAmelCase         =       tokenizer(_snake_case						)

									for key in encoded_tok.keys():
														self.assertListEqual(encoded_tok[key]		,							encoded_processor[key]						)




				def 				snake_case  (				self						):



									"""simple docstring"""





									_lowerCAmelCase         =       self.get_feature_extractor()
									_lowerCAmelCase         =       self.get_tokenizer()

									_lowerCAmelCase         =       SpeechTaProcessor(tokenizer=_snake_case		,							feature_extractor=_snake_case						)

									_lowerCAmelCase         =       """This is a test string"""

									_lowerCAmelCase         =       processor(text_target=_snake_case						)
									_lowerCAmelCase         =       tokenizer(_snake_case						)

									for key in encoded_tok.keys():
														self.assertListEqual(encoded_tok[key]		,							encoded_processor[key]						)




				def 				snake_case  (				self						):



									"""simple docstring"""





									_lowerCAmelCase         =       self.get_feature_extractor()
									_lowerCAmelCase         =       self.get_tokenizer()

									_lowerCAmelCase         =       SpeechTaProcessor(tokenizer=_snake_case		,							feature_extractor=_snake_case						)

									_lowerCAmelCase         =       [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

									_lowerCAmelCase         =       processor.batch_decode(_snake_case						)
									_lowerCAmelCase         =       tokenizer.batch_decode(_snake_case						)

									self.assertListEqual(_snake_case		,							_snake_case						)




				def 				snake_case  (				self						):



									"""simple docstring"""





									_lowerCAmelCase         =       self.get_feature_extractor()
									_lowerCAmelCase         =       self.get_tokenizer()

									_lowerCAmelCase         =       SpeechTaProcessor(tokenizer=_snake_case		,							feature_extractor=_snake_case						)

									self.assertListEqual(
									    processor.model_input_names		,							feature_extractor.model_input_names		,							msg="""`processor` and `feature_extractor` model input names do not match"""		,							)



