
"""simple docstring"""


from __future__ import annotations

import unittest

from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available
from transformers.testing_utils import require_tf, require_tokenizers, slow
from transformers.utils import cached_property

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 TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel


@require_tf
class 			A__	:


     """simple docstring"""
     snake_case__	:					Optional[Any]													=BlenderbotConfig
     snake_case__	:					List[Any]													={}
     snake_case__	:					int													='''gelu'''

     def __init__(	self:						Any     ,		__a:						str     ,		__a:						Any=13     ,		__a:						List[str]=7     ,		__a:						Any=True     ,		__a:						Optional[Any]=False     ,		__a:						Tuple=99     ,		__a:						Optional[int]=32     ,		__a:						str=2     ,		__a:						Dict=4     ,		__a:						Dict=37     ,		__a:						Tuple=0.1     ,		__a:						Optional[int]=0.1     ,		__a:						Optional[Any]=20     ,		__a:						Optional[int]=2     ,		__a:						Union[str, Any]=1     ,		__a:						int=0     ,		)->    Tuple:
           lowerCamelCase					:					Union[str, Any]        =	parent
           lowerCamelCase					:					str        =	batch_size
           lowerCamelCase					:					Optional[Any]        =	seq_length
           lowerCamelCase					:					str        =	is_training
           lowerCamelCase					:					Any        =	use_labels
           lowerCamelCase					:					List[str]        =	vocab_size
           lowerCamelCase					:					Any        =	hidden_size
           lowerCamelCase					:					Union[str, Any]        =	num_hidden_layers
           lowerCamelCase					:					Optional[Any]        =	num_attention_heads
           lowerCamelCase					:					Union[str, Any]        =	intermediate_size

           lowerCamelCase					:					int        =	hidden_dropout_prob
           lowerCamelCase					:					List[Any]        =	attention_probs_dropout_prob
           lowerCamelCase					:					Dict        =	max_position_embeddings
           lowerCamelCase					:					Any        =	eos_token_id
           lowerCamelCase					:					str        =	pad_token_id
           lowerCamelCase					:					Optional[Any]        =	bos_token_id

     def 		a__						(	self:						Any	)->    Optional[Any]:
           lowerCamelCase					:					str        =	ids_tensor([self.batch_size, self.seq_length - 1]     ,		self.vocab_size	)
           lowerCamelCase					:					Optional[Any]        =	tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size	)     ,		1	)
           lowerCamelCase					:					str        =	tf.concat([input_ids, eos_tensor]     ,		axis=1	)

           lowerCamelCase					:					Optional[Any]        =	ids_tensor([self.batch_size, self.seq_length]     ,		self.vocab_size	)

           lowerCamelCase					:					List[str]        =	self.config_cls(
               vocab_size=self.vocab_size     ,		d_model=self.hidden_size     ,		encoder_layers=self.num_hidden_layers     ,		decoder_layers=self.num_hidden_layers     ,		encoder_attention_heads=self.num_attention_heads     ,		decoder_attention_heads=self.num_attention_heads     ,		encoder_ffn_dim=self.intermediate_size     ,		decoder_ffn_dim=self.intermediate_size     ,		dropout=self.hidden_dropout_prob     ,		attention_dropout=self.attention_probs_dropout_prob     ,		max_position_embeddings=self.max_position_embeddings     ,		eos_token_ids=[2]     ,		bos_token_id=self.bos_token_id     ,		pad_token_id=self.pad_token_id     ,		decoder_start_token_id=self.pad_token_id     ,		**self.config_updates     ,		)
           lowerCamelCase					:					str        =	prepare_blenderbot_inputs_dict(A_     ,		A_     ,		A_	)
           return config, inputs_dict







     def 		a__						(	self:						Any     ,		__a:						int     ,		__a:						Tuple	)->    List[Any]:
           lowerCamelCase					:					Dict        =	TFBlenderbotModel(config=A_	).get_decoder()
           lowerCamelCase					:					List[str]        =	inputs_dict["""input_ids"""]

           lowerCamelCase					:					Optional[int]        =	input_ids[:1, :]
           lowerCamelCase					:					Any        =	inputs_dict["""attention_mask"""][:1, :]
           lowerCamelCase					:					Union[str, Any]        =	inputs_dict["""head_mask"""]
           lowerCamelCase					:					List[Any]        =	1

           # first forward pass
           lowerCamelCase					:					int        =	model(A_     ,		attention_mask=A_     ,		head_mask=A_     ,		use_cache=A_	)

           lowerCamelCase     ,			lowerCamelCase					:					Union[str, Any]        =	outputs.to_tuple()

           # create hypothetical next token and extent to next_input_ids
           lowerCamelCase					:					Any        =	ids_tensor((self.batch_size, 3)     ,		config.vocab_size	)
           lowerCamelCase					:					List[Any]        =	tf.cast(ids_tensor((self.batch_size, 3)     ,		2	)     ,		tf.inta	)

           # append to next input_ids and
           lowerCamelCase					:					int        =	tf.concat([input_ids, next_tokens]     ,		axis=-1	)
           lowerCamelCase					:					Optional[Any]        =	tf.concat([attention_mask, next_attn_mask]     ,		axis=-1	)

           lowerCamelCase					:					Optional[int]        =	model(A_     ,		attention_mask=A_	)[0]
           lowerCamelCase					:					Dict        =	model(A_     ,		attention_mask=A_     ,		past_key_values=A_	)[0]

           self.parent.assertEqual(next_tokens.shape[1]     ,		output_from_past.shape[1]	)

           # select random slice
           lowerCamelCase					:					Union[str, Any]        =	int(ids_tensor((1,)     ,		output_from_past.shape[-1]	)	)
           lowerCamelCase					:					Tuple        =	output_from_no_past[:, -3:, random_slice_idx]
           lowerCamelCase					:					List[Any]        =	output_from_past[:, :, random_slice_idx]

           # test that outputs are equal for slice
           tf.debugging.assert_near(A_     ,		A_     ,		rtol=1e-3	)




def   snake_case		(     UpperCamelCase__       :						List[Any]			,				UpperCamelCase__       :						List[str]			,				UpperCamelCase__       :						List[str]			,				UpperCamelCase__       :						Optional[Any]=None			,				UpperCamelCase__       :						Union[str, Any]=None			,				UpperCamelCase__       :						int=None			,				UpperCamelCase__       :						Tuple=None			,				UpperCamelCase__       :						List[Any]=None			,				)   ->      List[Any]:
      if attention_mask is None:
            lowerCamelCase					:					Any        =	tf.cast(tf.math.not_equal(UpperCamelCase__			,				config.pad_token_id    )			,				tf.inta    )
      if decoder_attention_mask is None:
            lowerCamelCase					:					Any        =	tf.concat(
                [
                    tf.ones(decoder_input_ids[:, :1].shape			,				dtype=tf.inta    ),
                    tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]			,				config.pad_token_id    )			,				tf.inta    ),
                ]			,				axis=-1			,				)
      if head_mask is None:
            lowerCamelCase					:					Dict        =	tf.ones((config.encoder_layers, config.encoder_attention_heads)    )
      if decoder_head_mask is None:
            lowerCamelCase					:					Tuple        =	tf.ones((config.decoder_layers, config.decoder_attention_heads)    )
      if cross_attn_head_mask is None:
            lowerCamelCase					:					Union[str, Any]        =	tf.ones((config.decoder_layers, config.decoder_attention_heads)    )
      return {
          "input_ids": input_ids,
          "decoder_input_ids": decoder_input_ids,
          "attention_mask": attention_mask,
          "decoder_attention_mask": decoder_attention_mask,
          "head_mask": head_mask,
          "decoder_head_mask": decoder_head_mask,
          "cross_attn_head_mask": cross_attn_head_mask,
      }


@require_tf
class 			A__	(						_UpperCAmelCase ,     _UpperCAmelCase ,     unittest.TestCase):


     """simple docstring"""
     snake_case__	:					List[str]													=(TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else ()
     snake_case__	:					Optional[int]													=(TFBlenderbotForConditionalGeneration,) if is_tf_available() else ()
     snake_case__	:					Optional[Any]													=(
         {
             '''conversational''': TFBlenderbotForConditionalGeneration,
             '''feature-extraction''': TFBlenderbotModel,
             '''summarization''': TFBlenderbotForConditionalGeneration,
             '''text2text-generation''': TFBlenderbotForConditionalGeneration,
             '''translation''': TFBlenderbotForConditionalGeneration,
         }
         if is_tf_available()
         else {}
     )
     snake_case__	:					Optional[Any]													=True
     snake_case__	:					Any													=False
     snake_case__	:					Optional[Any]													=False

     def 		a__						(	self:						str	)->    List[str]:
           lowerCamelCase					:					Tuple        =	TFBlenderbotModelTester(self	)
           lowerCamelCase					:					Union[str, Any]        =	ConfigTester(self     ,		config_class=A_	)

     def 		a__						(	self:						List[Any]	)->    str:
           self.config_tester.run_common_tests()







     def 		a__						(	self:						List[str]	)->    Tuple:
           lowerCamelCase					:					Union[str, Any]        =	self.model_tester.prepare_config_and_inputs_for_common()
           self.model_tester.check_decoder_model_past_large_inputs(*A_	)


@require_tokenizers
@require_tf
class 			A__	(						unittest.TestCase):


     """simple docstring"""
     snake_case__	:					Optional[Any]													=['''My friends are cool but they eat too many carbs.''']
     snake_case__	:					Any													='''facebook/blenderbot-400M-distill'''

     @cached_property
     def 		a__						(	self:						str	)->    Optional[int]:
           return BlenderbotTokenizer.from_pretrained(self.model_name	)

     @cached_property
     def 		a__						(	self:						Tuple	)->    List[str]:
           lowerCamelCase					:					Any        =	TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name	)
           return model







     @slow
     def 		a__						(	self:						List[Any]	)->    str:
           lowerCamelCase					:					List[Any]        =	self.tokenizer(self.src_text     ,		return_tensors="""tf"""	)
           lowerCamelCase					:					int        =	self.model.generate(
               model_inputs.input_ids     ,		)
           lowerCamelCase					:					List[Any]        =	self.tokenizer.batch_decode(generated_ids.numpy()     ,		skip_special_tokens=A_	)[0]
           assert (
               generated_words
               == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?"
           )








"""simple docstring"""


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


__lowerCamelCase      :Any	    =		False


@skip_mps
class 			A__	(						__lowercase ,     __lowercase ,     __lowercase ,     unittest.TestCase):


     """simple docstring"""
     snake_case__	:					Optional[Any]													=StableDiffusionAttendAndExcitePipeline
     snake_case__	:					Any													=False
     snake_case__	:					Dict													=TEXT_TO_IMAGE_PARAMS
     snake_case__	:					Any													=TEXT_TO_IMAGE_BATCH_PARAMS.union({'''token_indices'''})
     snake_case__	:					Dict													=TEXT_TO_IMAGE_IMAGE_PARAMS
     snake_case__	:					str													=TEXT_TO_IMAGE_IMAGE_PARAMS

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

     @classmethod
     def 		a__						(	cls:						Union[str, Any]	)->    Any:
           super().tearDownClass()
           torch.use_deterministic_algorithms(__a	)

     def 		a__						(	self:						Tuple	)->    Union[str, Any]:
           torch.manual_seed(0	)
           lowerCamelCase					:					str        =	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     ,		)
           lowerCamelCase					:					Union[str, Any]        =	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	)
           lowerCamelCase					:					Union[str, Any]        =	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	)
           lowerCamelCase					:					str        =	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=1_000     ,		hidden_act="""gelu"""     ,		projection_dim=512     ,		)
           lowerCamelCase					:					Optional[int]        =	CLIPTextModel(__a	)
           lowerCamelCase					:					str        =	CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""	)

           lowerCamelCase					:					List[str]        =	{
               """unet""": unet,
               """scheduler""": scheduler,
               """vae""": vae,
               """text_encoder""": text_encoder,
               """tokenizer""": tokenizer,
               """safety_checker""": None,
               """feature_extractor""": None,
           }

           return components

     def 		a__						(	self:						Tuple     ,		__a:						int     ,		__a:						Union[str, Any]=0	)->    Optional[Any]:
           if str(__a	).startswith("""mps"""	):
                 lowerCamelCase					:					Tuple        =	torch.manual_seed(__a	)
           else:
                 lowerCamelCase					:					str        =	torch.Generator(device=__a	).manual_seed(__a	)
           lowerCamelCase					:					Dict        =	{
               """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 		a__						(	self:						Dict	)->    str:
           lowerCamelCase					:					Tuple        =	"""cpu"""

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

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

           self.assertEqual(image.shape     ,		(1, 64, 64, 3)	)
           lowerCamelCase					:					Optional[Any]        =	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]	)
           lowerCamelCase					:					Optional[Any]        =	np.abs(image_slice.flatten() - expected_slice	).max()
           self.assertLessEqual(__a     ,		1e-3	)

     def 		a__						(	self:						int	)->    Optional[Any]:
           super().test_cpu_offload_forward_pass(expected_max_diff=5e-4	)

     def 		a__						(	self:						Union[str, Any]	)->    Optional[int]:
           # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
           self._test_inference_batch_consistent(batch_sizes=[1, 2]	)

     def 		a__						(	self:						Tuple	)->    int:
           self._test_inference_batch_single_identical(batch_size=2     ,		expected_max_diff=7e-4	)

     def 		a__						(	self:						Dict	)->    List[Any]:
           super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3	)

     def 		a__						(	self:						Optional[int]	)->    Dict:
           super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5e-4	)

     def 		a__						(	self:						Any	)->    Tuple:
           super().test_save_load_local(expected_max_difference=5e-4	)







     def 		a__						(	self:						str	)->    str:
           super().test_save_load_optional_components(expected_max_difference=4e-4	)


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


     """simple docstring"""

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

     @classmethod
     def 		a__						(	cls:						Dict	)->    Optional[int]:
           super().tearDownClass()
           torch.use_deterministic_algorithms(__a	)

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







     def 		a__						(	self:						int	)->    Optional[Any]:
           lowerCamelCase					:					List[Any]        =	torch.manual_seed(51	)

           lowerCamelCase					:					List[str]        =	StableDiffusionAttendAndExcitePipeline.from_pretrained(
               """CompVis/stable-diffusion-v1-4"""     ,		safety_checker=__a     ,		torch_dtype=torch.floataa	)
           pipe.to("""cuda"""	)

           lowerCamelCase					:					Dict        =	"""a painting of an elephant with glasses"""
           lowerCamelCase					:					Any        =	[5, 7]

           lowerCamelCase					:					Tuple        =	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]

           lowerCamelCase					:					Union[str, Any]        =	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




