


from __future__ import annotations

import random
import unittest

from transformers import TransfoXLConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import tensorflow as tf

				from transformers import (
				    TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFTransfoXLForSequenceClassification,
				    TFTransfoXLLMHeadModel,
				    TFTransfoXLModel,
				)






class    _UpperCamelCase     :

				"""simple docstring"""







				def __init__(    self					, a__					, )						->					Optional[int]:
					A             =					parent
					A             =					13
					A             =					7
					A             =					30
					A             =					self.seq_length + self.mem_len
					A             =					15
					A             =					True
					A             =					True
					A             =					99
					A             =					[10, 50, 80]
					A             =					32
					A             =					32
					A             =					4
					A             =					8
					A             =					128
					A             =					2
					A             =					2
					A             =					None
					A             =					1
					A             =					0
					A             =					3
					A             =					self.vocab_size - 1
					A             =					0.01

				def   _UpperCAmelCase       (    self )						->					Union[str, Any]:
					A             =					ids_tensor([self.batch_size, self.seq_length]					, self.vocab_size )
					A             =					ids_tensor([self.batch_size, self.seq_length]					, self.vocab_size )

					A             =					None
					if self.use_labels:
						A             =					ids_tensor([self.batch_size, self.seq_length]					, self.vocab_size )

					A             =					TransfoXLConfig(
					    vocab_size=self.vocab_size					, mem_len=self.mem_len					, clamp_len=self.clamp_len					, cutoffs=self.cutoffs					, d_model=self.hidden_size					, d_embed=self.d_embed					, n_head=self.num_attention_heads					, d_head=self.d_head					, d_inner=self.d_inner					, div_val=self.div_val					, n_layer=self.num_hidden_layers					, eos_token_id=self.eos_token_id					, pad_token_id=self.vocab_size - 1					, init_range=self.init_range					, num_labels=self.num_labels					, )

					return (config, input_ids_a, input_ids_a, lm_labels)

				def   _UpperCAmelCase       (    self )						->					Tuple:
					random.seed(self.seed )
					tf.random.set_seed(self.seed )

				def   _UpperCAmelCase       (    self					, a__					, a__					, a__					, a__ )						->					List[Any]:
					A             =					TFTransfoXLModel(a__ )

					A       ,      A             =					model(a__ ).to_tuple()

					A             =					{"""input_ids""": input_ids_a, """mems""": mems_a}

					A       ,      A             =					model(a__ ).to_tuple()

					self.parent.assertEqual(hidden_states_a.shape					, (self.batch_size, self.seq_length, self.hidden_size) )
					self.parent.assertEqual(hidden_states_a.shape					, (self.batch_size, self.seq_length, self.hidden_size) )
					self.parent.assertListEqual(
					    [mem.shape for mem in mems_a]					, [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers					, )
					self.parent.assertListEqual(
					    [mem.shape for mem in mems_a]					, [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers					, )

				def   _UpperCAmelCase       (    self					, a__					, a__					, a__					, a__ )						->					List[str]:
					A             =					TFTransfoXLLMHeadModel(a__ )

					A       ,      A             =					model(a__ ).to_tuple()

					A             =					{"""input_ids""": input_ids_a, """labels""": lm_labels}
					A       ,      A             =					model(a__ ).to_tuple()

					A       ,      A             =					model([input_ids_a, mems_a] ).to_tuple()

					A             =					{"""input_ids""": input_ids_a, """mems""": mems_a, """labels""": lm_labels}

					A       ,      A             =					model(a__ ).to_tuple()

					self.parent.assertEqual(lm_logits_a.shape					, (self.batch_size, self.seq_length, self.vocab_size) )
					self.parent.assertListEqual(
					    [mem.shape for mem in mems_a]					, [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers					, )

					self.parent.assertEqual(lm_logits_a.shape					, (self.batch_size, self.seq_length, self.vocab_size) )
					self.parent.assertListEqual(
					    [mem.shape for mem in mems_a]					, [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers					, )

				def   _UpperCAmelCase       (    self					, a__					, a__					, a__					, a__ )						->					int:
					A             =					TFTransfoXLForSequenceClassification(a__ )
					A             =					model(a__ )
					self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.num_labels) )

				def   _UpperCAmelCase       (    self )						->					Optional[Any]:
					A             =					self.prepare_config_and_inputs()
					((A)       ,      (A)       ,      (A)       ,      (A))             =					config_and_inputs
					A             =					{"""input_ids""": input_ids_a}
					return config, inputs_dict






@require_tf
class    _UpperCamelCase     (       __snake_case     ,		__snake_case     ,		unittest.TestCase					):

				"""simple docstring"""







				lowerCAmelCase					=			(
				    (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else ()
				)
				lowerCAmelCase					=			() if is_tf_available() else ()
				lowerCAmelCase					=			(
				    {
				        'feature-extraction': TFTransfoXLModel,
				        'text-classification': TFTransfoXLForSequenceClassification,
				        'text-generation': TFTransfoXLLMHeadModel,
				        'zero-shot': TFTransfoXLForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				# TODO: add this test when TFTransfoXLLMHead has a linear output layer implemented
				lowerCAmelCase					=			False
				lowerCAmelCase					=			False
				lowerCAmelCase					=			False
				lowerCAmelCase					=			False

				def   _UpperCAmelCase       (    self					, a__					, a__					, a__					, a__					, a__ )						->					Union[str, Any]:
					if pipeline_test_casse_name == "TextGenerationPipelineTests":
						# Get `ValueError: AttributeError: 'NoneType' object has no attribute 'new_ones'` or `AssertionError`.
						# `TransfoXLConfig` was never used in pipeline tests: cannot create a simple
						# tokenizer.
						return True

					return False

				def   _UpperCAmelCase       (    self )						->					Optional[Any]:
					A             =					TFTransfoXLModelTester(self )
					A             =					ConfigTester(self					, config_class=a__					, d_embed=37 )

				def   _UpperCAmelCase       (    self )						->					int:
					self.config_tester.run_common_tests()

				def   _UpperCAmelCase       (    self )						->					Dict:
					self.model_tester.set_seed()
					A             =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_transfo_xl_model(*a__ )

				def   _UpperCAmelCase       (    self )						->					Tuple:
					self.model_tester.set_seed()
					A             =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_transfo_xl_lm_head(*a__ )

				def   _UpperCAmelCase       (    self )						->					Any:
					A             =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*a__ )

				def   _UpperCAmelCase       (    self )						->					Dict:
					A       ,      A             =					self.model_tester.prepare_config_and_inputs_for_common()
					A             =					[TFTransfoXLForSequenceClassification]

					for model_class in self.all_model_classes:
						A             =					model_class(a__ )
						assert isinstance(model.get_input_embeddings()					, tf.keras.layers.Layer )
						if model_class in list_other_models_with_output_ebd:
							A             =					model.get_output_embeddings()
							assert isinstance(a__					, tf.keras.layers.Layer )
							A             =					model.get_bias()
							assert name is None
						else:
							A             =					model.get_output_embeddings()
							assert x is None
							A             =					model.get_bias()
							assert name is None

				def   _UpperCAmelCase       (    self )						->					Tuple:
					# TODO JP: Make TransfoXL XLA compliant
					pass

				@slow
				def   _UpperCAmelCase       (    self )						->					str:
					for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						A             =					TFTransfoXLModel.from_pretrained(a__ )
						self.assertIsNotNone(a__ )

				@unittest.skip(reason="""This model doesn't play well with fit() due to not returning a single loss.""" )
				def   _UpperCAmelCase       (    self )						->					str:
					pass






@require_tf
class    _UpperCamelCase     (       unittest.TestCase					):

				"""simple docstring"""







				@unittest.skip("""Skip test until #12651 is resolved.""" )
				@slow
				def   _UpperCAmelCase       (    self )						->					List[str]:
					A             =					TFTransfoXLLMHeadModel.from_pretrained("""transfo-xl-wt103""" )
					# fmt: off
					A             =					tf.convert_to_tensor([[33,1297,2,1,1009,4,1109,1_1739,4762,358,5,25,245,22,1706,17,2_0098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,7_1477,2_0098,10_4447,2,2_0961,1,2604,4,1,329,3,6224,831,1_6002,2,8,603,7_8967,2_9546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,2_9546,54,8,3609,5,5_7211,49,4,1,277,18,8,1755,1_5691,3,341,25,416,693,4_2573,71,17,401,94,31,1_7919,2,2_9546,7873,18,1,435,23,1_1011,755,5,5167,3,7983,98,84,2,2_9546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,2_9546,824,1400,1868,2,19,160,2,311,8,5496,2,2_0920,17,25,1_5097,3,24,24,0]]					, dtype=tf.intaa )  # noqa: E231
					# fmt: on
					#  In 1991 , the remains of Russian Tsar Nicholas II and his family
					#  ( except for Alexei and Maria ) are discovered .
					#  The voice of Nicholas's young son , Tsarevich Alexei Nikolaevich , narrates the
					#  remainder of the story . 1883 Western Siberia ,
					#  a young Grigori Rasputin is asked by his father and a group of men to perform magic .
					#  Rasputin has a vision and denounces one of the men as a horse thief . Although his
					#  father initially slaps him for making such an accusation , Rasputin watches as the
					#  man is chased outside and beaten . Twenty years later , Rasputin sees a vision of
					#  the Virgin Mary , prompting him to become a priest . Rasputin quickly becomes famous ,
					#  with people , even a bishop , begging for his blessing . <eod> </s> <eos>

					# fmt: off
					A             =					[33,1297,2,1,1009,4,1109,1_1739,4762,358,5,25,245,22,1706,17,2_0098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,7_1477,2_0098,10_4447,2,2_0961,1,2604,4,1,329,3,6224,831,1_6002,2,8,603,7_8967,2_9546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,2_9546,54,8,3609,5,5_7211,49,4,1,277,18,8,1755,1_5691,3,341,25,416,693,4_2573,71,17,401,94,31,1_7919,2,2_9546,7873,18,1,435,23,1_1011,755,5,5167,3,7983,98,84,2,2_9546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,2_9546,824,1400,1868,2,19,160,2,311,8,5496,2,2_0920,17,25,1_5097,3,24,24,0,33,1,1857,2,1,1009,4,1109,1_1739,4762,358,5,25,245,28,1110,3,13,1041,4,24,603,490,2,7_1477,2_0098,10_4447,2,2_0961,1,2604,4,1,329,3,0]  # noqa: E231
					# fmt: on
					#  In 1991, the remains of Russian Tsar Nicholas II and his family (
					#  except for Alexei and Maria ) are discovered. The voice of young son,
					#  Tsarevich Alexei Nikolaevich, narrates the remainder of the story.
					#  1883 Western Siberia, a young Grigori Rasputin is asked by his father
					#  and a group of men to perform magic. Rasputin has a vision and
					#  denounces one of the men as a horse thief. Although his father initially
					#  slaps him for making such an accusation, Rasputin watches as the man
					#  is chased outside and beaten. Twenty years later, Rasputin sees a vision
					#  of the Virgin Mary, prompting him to become a priest.
					#  Rasputin quickly becomes famous, with people, even a bishop, begging for
					#  his blessing. <unk> <unk> <eos> In the 1990s, the remains of Russian Tsar
					# Nicholas II and his family were discovered. The voice of <unk> young son,
					# Tsarevich Alexei Nikolaevich, narrates the remainder of the story.<eos>

					A             =					model.generate(a__					, max_length=200					, do_sample=a__ )
					self.assertListEqual(output_ids[0].numpy().tolist()					, a__ )







import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionAttendAndExcitePipeline,
    UNetaDConditionModel,
)
from diffusers.utils import load_numpy, skip_mps, slow
from diffusers.utils.testing_utils import require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


_lowercase							:   Optional[int]						=						False






@skip_mps
class    _UpperCamelCase     (       __snake_case     ,		__snake_case     ,		__snake_case     ,		unittest.TestCase					):

				"""simple docstring"""







				lowerCAmelCase					=			StableDiffusionAttendAndExcitePipeline
				lowerCAmelCase					=			False
				lowerCAmelCase					=			TEXT_TO_IMAGE_PARAMS
				lowerCAmelCase					=			TEXT_TO_IMAGE_BATCH_PARAMS.union({'token_indices'}					)
				lowerCAmelCase					=			TEXT_TO_IMAGE_IMAGE_PARAMS
				lowerCAmelCase					=			TEXT_TO_IMAGE_IMAGE_PARAMS

				@classmethod
				def   _UpperCAmelCase       (    cls )						->					List[Any]:
					super().setUpClass()
					torch.use_deterministic_algorithms(a__ )

				@classmethod
				def   _UpperCAmelCase       (    cls )						->					Tuple:
					super().tearDownClass()
					torch.use_deterministic_algorithms(a__ )

				def   _UpperCAmelCase       (    self )						->					Dict:
					torch.manual_seed(0 )
					A             =					UNetaDConditionModel(
					    block_out_channels=(32, 64)					, layers_per_block=1					, sample_size=32					, in_channels=4					, out_channels=4					, down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")					, up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")					, cross_attention_dim=32					, attention_head_dim=(2, 4)					, use_linear_projection=a__					, )
					A             =					DDIMScheduler(
					    beta_start=0.0_00_85					, beta_end=0.0_12					, beta_schedule="""scaled_linear"""					, clip_sample=a__					, set_alpha_to_one=a__					, )
					torch.manual_seed(0 )
					A             =					AutoencoderKL(
					    block_out_channels=[32, 64]					, in_channels=3					, out_channels=3					, down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]					, up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]					, latent_channels=4					, sample_size=128					, )
					torch.manual_seed(0 )
					A             =					CLIPTextConfig(
					    bos_token_id=0					, eos_token_id=2					, hidden_size=32					, intermediate_size=37					, layer_norm_eps=1e-05					, num_attention_heads=4					, num_hidden_layers=5					, pad_token_id=1					, vocab_size=1000					, hidden_act="""gelu"""					, projection_dim=512					, )
					A             =					CLIPTextModel(a__ )
					A             =					CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" )

					A             =					{
					    """unet""": unet,
					    """scheduler""": scheduler,
					    """vae""": vae,
					    """text_encoder""": text_encoder,
					    """tokenizer""": tokenizer,
					    """safety_checker""": None,
					    """feature_extractor""": None,
					}

					return components

				def   _UpperCAmelCase       (    self					, a__					, a__=0 )						->					Optional[Any]:
					if str(a__ ).startswith("""mps""" ):
						A             =					torch.manual_seed(a__ )
					else:
						A             =					torch.Generator(device=a__ ).manual_seed(a__ )
					A             =					A             =					{
					    """prompt""": """a cat and a frog""",
					    """token_indices""": [2, 5],
					    """generator""": generator,
					    """num_inference_steps""": 1,
					    """guidance_scale""": 6.0,
					    """output_type""": """numpy""",
					    """max_iter_to_alter""": 2,
					    """thresholds""": {0: 0.7},
					}
					return inputs

				def   _UpperCAmelCase       (    self )						->					Union[str, Any]:
					A             =					"""cpu"""

					A             =					self.get_dummy_components()
					A             =					self.pipeline_class(**a__ )
					pipe.to(a__ )
					pipe.set_progress_bar_config(disable=a__ )

					A             =					self.get_dummy_inputs(a__ )
					A             =					pipe(**a__ ).images
					A             =					image[0, -3:, -3:, -1]

					self.assertEqual(image.shape					, (1, 64, 64, 3) )
					A             =					np.array(
					    [0.63_90_53_64, 0.62_89_73_07, 0.48_59_90_17, 0.5_13_36_24, 0.5_55_00_48, 0.45_76_95_16, 0.50_32_69_73, 0.5_02_31_39, 0.45_38_44_96] )
					A             =					np.abs(image_slice.flatten() - expected_slice ).max()
					self.assertLessEqual(a__					, 1e-3 )

				def   _UpperCAmelCase       (    self )						->					List[Any]:
					super().test_cpu_offload_forward_pass(expected_max_diff=5e-4 )

				def   _UpperCAmelCase       (    self )						->					Dict:
					# NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
					self._test_inference_batch_consistent(batch_sizes=[1, 2] )

				def   _UpperCAmelCase       (    self )						->					Any:
					self._test_inference_batch_single_identical(batch_size=2					, expected_max_diff=7e-4 )

				def   _UpperCAmelCase       (    self )						->					Optional[Any]:
					super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 )

				def   _UpperCAmelCase       (    self )						->					str:
					super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5e-4 )

				def   _UpperCAmelCase       (    self )						->					int:
					super().test_save_load_local(expected_max_difference=5e-4 )

				def   _UpperCAmelCase       (    self )						->					Optional[Any]:
					super().test_save_load_optional_components(expected_max_difference=4e-4 )






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

				"""simple docstring"""







				@classmethod
				def   _UpperCAmelCase       (    cls )						->					Tuple:
					super().setUpClass()
					torch.use_deterministic_algorithms(a__ )

				@classmethod
				def   _UpperCAmelCase       (    cls )						->					Dict:
					super().tearDownClass()
					torch.use_deterministic_algorithms(a__ )

				def   _UpperCAmelCase       (    self )						->					Optional[Any]:
					super().tearDown()
					gc.collect()
					torch.cuda.empty_cache()

				def   _UpperCAmelCase       (    self )						->					int:
					A             =					torch.manual_seed(51 )

					A             =					StableDiffusionAttendAndExcitePipeline.from_pretrained(
					    """CompVis/stable-diffusion-v1-4"""					, safety_checker=a__					, torch_dtype=torch.floataa )
					pipe.to("""cuda""" )

					A             =					"""a painting of an elephant with glasses"""
					A             =					[5, 7]

					A             =					pipe(
					    prompt=a__					, token_indices=a__					, guidance_scale=7.5					, generator=a__					, num_inference_steps=5					, max_iter_to_alter=5					, output_type="""numpy"""					, ).images[0]

					A             =					load_numpy(
					    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/attend-and-excite/elephant_glasses.npy""" )
					assert np.abs((expected_image - image).max() ) < 5e-1



