


'''simple docstring'''



import gc
import random
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

import diffusers
from diffusers import (
    AutoencoderKL,
    EulerDiscreteScheduler,
    StableDiffusionLatentUpscalePipeline,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.schedulers import KarrasDiffusionSchedulers
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   Optional[int]   ):
					__a			:       Optional[Any]					    =  [tensor.shape for tensor in tensor_list]
					return all(shape == shapes[0] for shape in shapes[1:]   )





class 							__UpperCamelCase				(		lowerCAmelCase_		,      lowerCAmelCase_		,      lowerCAmelCase_		,      unittest.TestCase					):
	A_				=    StableDiffusionLatentUpscalePipeline
	A_				=    TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {
	    "height",
	    "width",
	    "cross_attention_kwargs",
	    "negative_prompt_embeds",
	    "prompt_embeds",
	}
	A_				=    PipelineTesterMixin.required_optional_params - {"num_images_per_prompt"}
	A_				=    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
	A_				=    frozenset(
	    []					)  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
	A_				=    frozenset([]					)

	A_				=    True




	@property
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       int					    =  1
						__a			:       Any					    =  4
						__a			:       List[str]					    =  (16, 16)

						__a			:       List[Any]					    =  floats_tensor((batch_size, num_channels) + sizes						,				rng=random.Random(0							)							).to(__a							)
						return image




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						torch.manual_seed(0							)
						__a			:       List[Any]					    =  UNetaDConditionModel(
						    act_fn='gelu'						,				attention_head_dim=8						,				norm_num_groups=__a						,				block_out_channels=[32, 32, 64, 64]						,				time_cond_proj_dim=160						,				conv_in_kernel=1						,				conv_out_kernel=1						,				cross_attention_dim=32						,				down_block_types=(
						        'KDownBlock2D',
						        'KCrossAttnDownBlock2D',
						        'KCrossAttnDownBlock2D',
						        'KCrossAttnDownBlock2D',
						    )						,				in_channels=8						,				mid_block_type=__a						,				only_cross_attention=__a						,				out_channels=5						,				resnet_time_scale_shift='scale_shift'						,				time_embedding_type='fourier'						,				timestep_post_act='gelu'						,				up_block_types=('KCrossAttnUpBlock2D', 'KCrossAttnUpBlock2D', 'KCrossAttnUpBlock2D', 'KUpBlock2D')						,				)
						__a			:       Dict					    =  AutoencoderKL(
						    block_out_channels=[32, 32, 64, 64]						,				in_channels=3						,				out_channels=3						,				down_block_types=[
						        'DownEncoderBlock2D',
						        'DownEncoderBlock2D',
						        'DownEncoderBlock2D',
						        'DownEncoderBlock2D',
						    ]						,				up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D', 'UpDecoderBlock2D', 'UpDecoderBlock2D']						,				latent_channels=4						,				)
						__a			:       str					    =  EulerDiscreteScheduler(prediction_type='sample'							)
						__a			:       Optional[int]					    =  CLIPTextConfig(
						    bos_token_id=0						,				eos_token_id=2						,				hidden_size=32						,				intermediate_size=37						,				layer_norm_eps=1E-0_5						,				num_attention_heads=4						,				num_hidden_layers=5						,				pad_token_id=1						,				vocab_size=1000						,				hidden_act='quick_gelu'						,				projection_dim=512						,				)
						__a			:       Optional[Any]					    =  CLIPTextModel(__a							)
						__a			:       Any					    =  CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'							)

						__a			:       Any					    =  {
						    'unet': model.eval(),
						    'vae': vae.eval(),
						    'scheduler': scheduler,
						    'text_encoder': text_encoder,
						    'tokenizer': tokenizer,
						}

						return components




	def       __UpperCAmelCase							(   self						,				__a						,				__a=0							):

						'''simple docstring'''





						if str(__a							).startswith('mps'							):
											__a			:       str					    =  torch.manual_seed(__a							)
						else:
											__a			:       Tuple					    =  torch.Generator(device=__a							).manual_seed(__a							)
						__a			:       Optional[int]					    =  {
						    'prompt': 'A painting of a squirrel eating a burger',
						    'image': self.dummy_image.cpu(),
						    'generator': generator,
						    'num_inference_steps': 2,
						    'output_type': 'numpy',
						}
						return inputs




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Optional[Any]					    =  'cpu'

						__a			:       List[Any]					    =  self.get_dummy_components()
						__a			:       Optional[int]					    =  self.pipeline_class(**__a							)
						pipe.to(__a							)
						pipe.set_progress_bar_config(disable=__a							)

						__a			:       Dict					    =  self.get_dummy_inputs(__a							)
						__a			:       Tuple					    =  pipe(**__a							).images
						__a			:       List[Any]					    =  image[0, -3:, -3:, -1]

						self.assertEqual(image.shape						,				(1, 256, 256, 3)							)
						__a			:       List[str]					    =  np.array(
						    [0.47222412, 0.41921633, 0.44717434, 0.46874192, 0.42588258, 0.46150726, 0.4677534, 0.45583832, 0.48579055]							)
						__a			:       Tuple					    =  np.abs(image_slice.flatten() - expected_slice							).max()
						self.assertLessEqual(__a						,				1E-3							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						super().test_attention_slicing_forward_pass(expected_max_diff=7E-3							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						super().test_cpu_offload_forward_pass(expected_max_diff=3E-3							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						super().test_inference_batch_single_identical(expected_max_diff=7E-3							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						super().test_pt_np_pil_outputs_equivalent(expected_max_diff=3E-3							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						super().test_save_load_local(expected_max_difference=3E-3							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						super().test_save_load_optional_components(expected_max_difference=3E-3							)





	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       List[Any]					    =  [
						    'DDIMScheduler',
						    'DDPMScheduler',
						    'PNDMScheduler',
						    'HeunDiscreteScheduler',
						    'EulerAncestralDiscreteScheduler',
						    'KDPM2DiscreteScheduler',
						    'KDPM2AncestralDiscreteScheduler',
						    'DPMSolverSDEScheduler',
						]
						__a			:       Tuple					    =  self.get_dummy_components()
						__a			:       Tuple					    =  self.pipeline_class(**__a							)

						# make sure that PNDM does not need warm-up
						pipe.scheduler.register_to_config(skip_prk_steps=__a							)

						pipe.to(__a							)
						pipe.set_progress_bar_config(disable=__a							)
						__a			:       List[str]					    =  self.get_dummy_inputs(__a							)
						__a			:       Any					    =  2

						__a			:       Tuple					    =  []
						for scheduler_enum in KarrasDiffusionSchedulers:
											if scheduler_enum.name in skip_schedulers:
																# no sigma schedulers are not supported
																# no schedulers
																continue

											__a			:       Tuple					    =  getattr(__a						,				scheduler_enum.name							)
											__a			:       Optional[Any]					    =  scheduler_cls.from_config(pipe.scheduler.config							)
											__a			:       int					    =  pipe(**__a							)[0]
											outputs.append(__a							)

						assert check_same_shape(__a							)





@require_torch_gpu
@slow
class 							__UpperCamelCase				(		unittest.TestCase					):




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Dict					    =  torch.manual_seed(33							)

						__a			:       str					    =  StableDiffusionPipeline.from_pretrained('CompVis/stable-diffusion-v1-4'						,				torch_dtype=torch.floataa							)
						pipe.to('cuda'							)

						__a			:       str					    =  StableDiffusionLatentUpscalePipeline.from_pretrained(
						    'stabilityai/sd-x2-latent-upscaler'						,				torch_dtype=torch.floataa							)
						upscaler.to('cuda'							)

						__a			:       Union[str, Any]					    =  'a photo of an astronaut high resolution, unreal engine, ultra realistic'

						__a			:       int					    =  pipe(__a						,				generator=__a						,				output_type='latent'							).images

						__a			:       Union[str, Any]					    =  upscaler(
						    prompt=__a						,				image=__a						,				num_inference_steps=20						,				guidance_scale=0						,				generator=__a						,				output_type='np'						,				).images[0]

						__a			:       Optional[Any]					    =  load_numpy(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/astronaut_1024.npy'							)
						assert np.abs((expected_image - image).mean()							) < 5E-2





	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Optional[Any]					    =  torch.manual_seed(33							)

						__a			:       Union[str, Any]					    =  StableDiffusionLatentUpscalePipeline.from_pretrained(
						    'stabilityai/sd-x2-latent-upscaler'						,				torch_dtype=torch.floataa							)
						upscaler.to('cuda'							)

						__a			:       Optional[int]					    =  'the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas'

						__a			:       List[str]					    =  load_image(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png'							)

						__a			:       List[str]					    =  upscaler(
						    prompt=__a						,				image=__a						,				num_inference_steps=20						,				guidance_scale=0						,				generator=__a						,				output_type='np'						,				).images[0]

						__a			:       Tuple					    =  load_numpy(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_1024.npy'							)
						assert np.abs((expected_image - image).max()							) < 5E-2




"""simple docstring"""




import pickle
import shutil
import tempfile
import unittest

from transformers import SPIECE_UNDERLINE, XGLMTokenizer, XGLMTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin


_a      :		Optional[int]           =   get_tests_dir('fixtures/test_sentencepiece.model')
@require_sentencepiece
@require_tokenizers
class 						__A							( SCREAMING_SNAKE_CASE_    ,	unittest.TestCase		):
     _UpperCamelCase							:			List[Any]							=		XGLMTokenizer
     _UpperCamelCase							:			List[Any]							=		XGLMTokenizerFast
     _UpperCamelCase							:			Dict							=		True
     _UpperCamelCase							:			Tuple							=		True






     def     __A  (		self  ):
       super().setUp()

       # We have a SentencePiece fixture for testing
       _lowerCAmelCase						:       List[Any]  			=     XGLMTokenizer(a__				,     keep_accents=a__  )
       tokenizer.save_pretrained(self.tmpdirname  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       List[str]  			=     """<pad>"""
       _lowerCAmelCase						:       List[Any]  			=     1

       self.assertEqual(self.get_tokenizer()._convert_token_to_id(a__  )				,     a__  )
       self.assertEqual(self.get_tokenizer()._convert_id_to_token(a__  )				,     a__  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       Optional[int]  			=     list(self.get_tokenizer().get_vocab().keys()  )

       self.assertEqual(vocab_keys[0]				,     """<s>"""  )
       self.assertEqual(vocab_keys[1]				,     """<pad>"""  )
       self.assertEqual(len(a__  )				,     1008  )






     def     __A  (		self  ):
       self.assertEqual(self.get_tokenizer().vocab_size				,     1008  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       List[Any]  			=     XGLMTokenizer(a__				,     keep_accents=a__  )

       _lowerCAmelCase						:       Dict  			=     tokenizer.tokenize("""This is a test"""  )
       self.assertListEqual(a__				,     ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""]  )

       self.assertListEqual(
           tokenizer.convert_tokens_to_ids(a__  )				,     [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]]				,     )

       _lowerCAmelCase						:       Any  			=     tokenizer.tokenize("""I was born in 92000, and this is falsé."""  )
       self.assertListEqual(
           a__				,     [
               SPIECE_UNDERLINE + """I""",
               SPIECE_UNDERLINE + """was""",
               SPIECE_UNDERLINE + """b""",
               """or""",
               """n""",
               SPIECE_UNDERLINE + """in""",
               SPIECE_UNDERLINE + """""",
               """9""",
               """2""",
               """0""",
               """0""",
               """0""",
               """,""",
               SPIECE_UNDERLINE + """and""",
               SPIECE_UNDERLINE + """this""",
               SPIECE_UNDERLINE + """is""",
               SPIECE_UNDERLINE + """f""",
               """al""",
               """s""",
               """é""",
               """.""",
           ]				,     )
       _lowerCAmelCase						:       List[str]  			=     tokenizer.convert_tokens_to_ids(a__  )
       self.assertListEqual(
           a__				,     [
               value + tokenizer.fairseq_offset
               for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4]
           ]				,     )

       _lowerCAmelCase						:       Optional[int]  			=     tokenizer.convert_ids_to_tokens(a__  )
       self.assertListEqual(
           a__				,     [
               SPIECE_UNDERLINE + """I""",
               SPIECE_UNDERLINE + """was""",
               SPIECE_UNDERLINE + """b""",
               """or""",
               """n""",
               SPIECE_UNDERLINE + """in""",
               SPIECE_UNDERLINE + """""",
               """<unk>""",
               """2""",
               """0""",
               """0""",
               """0""",
               """,""",
               SPIECE_UNDERLINE + """and""",
               SPIECE_UNDERLINE + """this""",
               SPIECE_UNDERLINE + """is""",
               SPIECE_UNDERLINE + """f""",
               """al""",
               """s""",
               """<unk>""",
               """.""",
           ]				,     )






     @cached_property
     def     __A  (		self  ):
       return XGLMTokenizer.from_pretrained("""facebook/xglm-564M"""  )






     def     __A  (		self  ):
       with tempfile.NamedTemporaryFile() as f:
         shutil.copyfile(a__				,     f.name  )
         _lowerCAmelCase						:       Union[str, Any]  			=     XGLMTokenizer(f.name				,     keep_accents=a__  )
         _lowerCAmelCase						:       List[str]  			=     pickle.dumps(a__  )
       pickle.loads(a__  )






     def     __A  (		self  ):
       if not self.test_rust_tokenizer:
         return

       _lowerCAmelCase						:       List[str]  			=     self.get_tokenizer()
       _lowerCAmelCase						:       Optional[Any]  			=     self.get_rust_tokenizer()

       _lowerCAmelCase						:       Tuple  			=     """I was born in 92000, and this is falsé."""

       _lowerCAmelCase						:       List[Any]  			=     tokenizer.tokenize(a__  )
       _lowerCAmelCase						:       Tuple  			=     rust_tokenizer.tokenize(a__  )
       self.assertListEqual(a__				,     a__  )

       _lowerCAmelCase						:       Union[str, Any]  			=     tokenizer.encode(a__				,     add_special_tokens=a__  )
       _lowerCAmelCase						:       str  			=     rust_tokenizer.encode(a__				,     add_special_tokens=a__  )
       self.assertListEqual(a__				,     a__  )

       _lowerCAmelCase						:       int  			=     self.get_rust_tokenizer()
       _lowerCAmelCase						:       Dict  			=     tokenizer.encode(a__  )
       _lowerCAmelCase						:       List[Any]  			=     rust_tokenizer.encode(a__  )
       self.assertListEqual(a__				,     a__  )






     @slow
     def     __A  (		self  ):
       _lowerCAmelCase						:       int  			=     """Hello World!"""
       _lowerCAmelCase						:       Optional[int]  			=     [2, 31227, 4447, 35]

       self.assertListEqual(a__				,     self.big_tokenizer.encode(a__  )  )






     @slow
     def     __A  (		self  ):
       _lowerCAmelCase						:       Any  			=     (
           """This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will"""
           """ add words that should not exsist and be tokenized to unk, such as saoneuhaoesuth"""
       )
       # fmt: off
       _lowerCAmelCase						:       List[str]  			=     [2, 1018, 67, 11, 1988, 2617, 5631, 278, 11, 3407, 48, 71630, 28085, 4, 3234, 157, 13, 6, 5, 6, 4, 3526, 768, 15, 659, 57, 298, 3983, 864, 129, 21, 6, 5, 13675, 377, 652, 7580, 10341, 155, 2817, 422, 1666, 7, 1674, 53, 113, 202277, 17892, 33, 60, 87, 4, 3234, 157, 61, 2667, 52376, 19, 88, 23, 735]
       # fmt: on

       self.assertListEqual(a__				,     self.big_tokenizer.encode(a__  )  )






     @slow
     def     __A  (		self  ):
       # fmt: off
       _lowerCAmelCase						:       List[str]  			=     {
           """input_ids""": [[2, 108825, 1163, 15, 88010, 473, 15898, 157, 13672, 1857, 312, 8, 238021, 1163, 53, 13672, 1857, 312, 8, 53283, 182396, 8, 18566, 16, 36733, 4101, 8, 230, 244017, 122553, 7, 15, 132597, 4, 293, 12511, 7610, 4, 3414, 132597, 9, 4, 32361, 362, 4, 734, 28512, 32569, 18, 4, 32361, 26096, 14982, 73, 18715, 21433, 235261, 15, 492, 12427, 16, 53, 18715, 21433, 65454, 15, 23659, 563, 16, 278, 597, 2843, 595, 7931, 182396, 64186, 22, 886, 595, 132981, 53, 25540, 3449, 43982, 39901, 5951, 878, 330, 4, 27694, 80269, 312, 53, 6517, 11780, 611, 20408, 5], [2, 6, 132597, 67, 42897, 33, 592, 8, 163729, 25540, 361, 136997, 109514, 173230, 7, 501, 60, 102913, 196, 5631, 235, 63243, 473, 6, 231757, 74, 5277, 7905, 53, 3095, 37317, 22, 454, 183874, 5], [2, 268, 31298, 46530, 6, 132935, 43831, 7, 597, 32, 24, 3688, 9865, 5]],
           """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
       }  # noqa: E501
       # fmt: on

       self.tokenizer_integration_test_util(
           expected_encoding=a__				,     model_name="""facebook/xglm-564M"""				,     padding=a__				,     )




