




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     lowerCamelCase__:
      UpperCAmelCase__      :	Union[str, Any]    		=   BlenderbotConfig
      UpperCAmelCase__      :	Tuple    		=   {}
      UpperCAmelCase__      :	Any    		=   'gelu'
      def __init__(				self: Optional[int]    ,       UpperCamelCase_: Union[str, Any]    ,       UpperCamelCase_: Optional[int]=13    ,       UpperCamelCase_: Optional[int]=7    ,       UpperCamelCase_: int=True    ,       UpperCamelCase_: Union[str, Any]=False    ,       UpperCamelCase_: Any=99    ,       UpperCamelCase_: Optional[Any]=32    ,       UpperCamelCase_: Any=2    ,       UpperCamelCase_: Tuple=4    ,       UpperCamelCase_: List[str]=37    ,       UpperCamelCase_: Tuple=0.1    ,       UpperCamelCase_: Union[str, Any]=0.1    ,       UpperCamelCase_: List[str]=20    ,       UpperCamelCase_: int=2    ,       UpperCamelCase_: Union[str, Any]=1    ,       UpperCamelCase_: List[Any]=0    ,       ):
             __lowerCamelCase   =						parent
             __lowerCamelCase   =						batch_size
             __lowerCamelCase   =						seq_length
             __lowerCamelCase   =						is_training
             __lowerCamelCase   =						use_labels
             __lowerCamelCase   =						vocab_size
             __lowerCamelCase   =						hidden_size
             __lowerCamelCase   =						num_hidden_layers
             __lowerCamelCase   =						num_attention_heads
             __lowerCamelCase   =						intermediate_size

             __lowerCamelCase   =						hidden_dropout_prob
             __lowerCamelCase   =						attention_probs_dropout_prob
             __lowerCamelCase   =						max_position_embeddings
             __lowerCamelCase   =						eos_token_id
             __lowerCamelCase   =						pad_token_id
             __lowerCamelCase   =						bos_token_id
      def 			lowerCAmelCase__						(				self: List[Any]   ):
             __lowerCamelCase   =						ids_tensor([self.batch_size, self.seq_length - 1]    ,       self.vocab_size   )
             __lowerCamelCase   =						tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size   )    ,       1   )
             __lowerCamelCase   =						tf.concat([input_ids, eos_tensor]    ,       axis=1   )

             __lowerCamelCase   =						ids_tensor([self.batch_size, self.seq_length]    ,       self.vocab_size   )

             __lowerCamelCase   =						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   =						prepare_blenderbot_inputs_dict(UpperCamelCase_    ,       UpperCamelCase_    ,       UpperCamelCase_   )
             return config, inputs_dict




      def 			lowerCAmelCase__						(				self: int    ,       UpperCamelCase_: Optional[Any]    ,       UpperCamelCase_: str   ):
             __lowerCamelCase   =						TFBlenderbotModel(config=UpperCamelCase_   ).get_decoder()
             __lowerCamelCase   =						inputs_dict["""input_ids"""]

             __lowerCamelCase   =						input_ids[:1, :]
             __lowerCamelCase   =						inputs_dict["""attention_mask"""][:1, :]
             __lowerCamelCase   =						inputs_dict["""head_mask"""]
             __lowerCamelCase   =						1

             # first forward pass
             __lowerCamelCase   =						model(UpperCamelCase_    ,       attention_mask=UpperCamelCase_    ,       head_mask=UpperCamelCase_    ,       use_cache=UpperCamelCase_   )

             __lowerCamelCase,   __lowerCamelCase   =						outputs.to_tuple()

             # create hypothetical next token and extent to next_input_ids
             __lowerCamelCase   =						ids_tensor((self.batch_size, 3)    ,       config.vocab_size   )
             __lowerCamelCase   =						tf.cast(ids_tensor((self.batch_size, 3)    ,       2   )    ,       tf.inta   )

             # append to next input_ids and
             __lowerCamelCase   =						tf.concat([input_ids, next_tokens]    ,       axis=-1   )
             __lowerCamelCase   =						tf.concat([attention_mask, next_attn_mask]    ,       axis=-1   )

             __lowerCamelCase   =						model(UpperCamelCase_    ,       attention_mask=UpperCamelCase_   )[0]
             __lowerCamelCase   =						model(UpperCamelCase_    ,       attention_mask=UpperCamelCase_    ,       past_key_values=UpperCamelCase_   )[0]

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

             # select random slice
             __lowerCamelCase   =						int(ids_tensor((1,)    ,       output_from_past.shape[-1]   )   )
             __lowerCamelCase   =						output_from_no_past[:, -3:, random_slice_idx]
             __lowerCamelCase   =						output_from_past[:, :, random_slice_idx]

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




def 					lowerCamelCase__       (						A__       :					List[str]  ,			A__       :					Dict  ,			A__       :					Any  ,			A__       :					int=None  ,			A__       :					str=None  ,			A__       :					List[str]=None  ,			A__       :					Any=None  ,			A__       :					int=None  ,			):





       '''simple docstring'''



       if attention_mask is None:
              __lowerCamelCase   =						tf.cast(tf.math.not_equal(A__  ,			config.pad_token_id	)  ,			tf.inta	)
       if decoder_attention_mask is None:
              __lowerCamelCase   =						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   =						tf.ones((config.encoder_layers, config.encoder_attention_heads)	)
       if decoder_head_mask is None:
              __lowerCamelCase   =						tf.ones((config.decoder_layers, config.decoder_attention_heads)	)
       if cross_attn_head_mask is None:
              __lowerCamelCase   =						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     lowerCamelCase__(					__lowerCamelCase ,     __lowerCamelCase ,     unittest.TestCase):
      UpperCAmelCase__      :	Union[str, Any]    		=   (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else ()
      UpperCAmelCase__      :	str    		=   (TFBlenderbotForConditionalGeneration,) if is_tf_available() else ()
      UpperCAmelCase__      :	Optional[Any]    		=   (
          {
              'conversational': TFBlenderbotForConditionalGeneration,
              'feature-extraction': TFBlenderbotModel,
              'summarization': TFBlenderbotForConditionalGeneration,
              'text2text-generation': TFBlenderbotForConditionalGeneration,
              'translation': TFBlenderbotForConditionalGeneration,
          }
          if is_tf_available()
          else {}
      )
      UpperCAmelCase__      :	Optional[Any]    		=   True
      UpperCAmelCase__      :	int    		=   False
      UpperCAmelCase__      :	Tuple    		=   False
      def 			lowerCAmelCase__						(				self: Tuple   ):
             __lowerCamelCase   =						TFBlenderbotModelTester(self   )
             __lowerCamelCase   =						ConfigTester(self    ,       config_class=UpperCamelCase_   )
      def 			lowerCAmelCase__						(				self: int   ):
             self.config_tester.run_common_tests()




      def 			lowerCAmelCase__						(				self: Optional[int]   ):
             __lowerCamelCase   =						self.model_tester.prepare_config_and_inputs_for_common()
             self.model_tester.check_decoder_model_past_large_inputs(*UpperCamelCase_   )





@require_tokenizers
@require_tf
class     lowerCamelCase__(					unittest.TestCase):
      UpperCAmelCase__      :	Optional[Any]    		=   ['My friends are cool but they eat too many carbs.']
      UpperCAmelCase__      :	Tuple    		=   'facebook/blenderbot-400M-distill'
      @cached_property
      def 			lowerCAmelCase__						(				self: Any   ):
             return BlenderbotTokenizer.from_pretrained(self.model_name   )
      @cached_property
      def 			lowerCAmelCase__						(				self: Tuple   ):
             __lowerCamelCase   =						TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name   )
             return model




      @slow
      def 			lowerCAmelCase__						(				self: Optional[int]   ):
             __lowerCamelCase   =						self.tokenizer(self.src_text    ,       return_tensors="""tf"""   )
             __lowerCamelCase   =						self.model.generate(
                 model_inputs.input_ids    ,       )
             __lowerCamelCase   =						self.tokenizer.batch_decode(generated_ids.numpy()    ,       skip_special_tokens=UpperCamelCase_   )[0]
             assert (
                 generated_words
                 == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?"
             )







import gc
import random
import unittest

import numpy as np
import torch
from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModel

from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEImgaImgPipeline
from diffusers.pipelines.shap_e import ShapERenderer
from diffusers.utils import floats_tensor, load_image, load_numpy, slow
from diffusers.utils.testing_utils import require_torch_gpu, torch_device

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference





class     lowerCamelCase__(					__lowerCamelCase ,     unittest.TestCase):
      UpperCAmelCase__      :	Tuple    		=   ShapEImgaImgPipeline
      UpperCAmelCase__      :	Optional[Any]    		=   ['image']
      UpperCAmelCase__      :	int    		=   ['image']
      UpperCAmelCase__      :	Any    		=   [
          'num_images_per_prompt',
          'num_inference_steps',
          'generator',
          'latents',
          'guidance_scale',
          'frame_size',
          'output_type',
          'return_dict',
      ]
      UpperCAmelCase__      :	int    		=   False
      @property
      def 			lowerCAmelCase__						(				self: int   ):
             return 32
      @property
      def 			lowerCAmelCase__						(				self: List[str]   ):
             return 32
      @property
      def 			lowerCAmelCase__						(				self: Any   ):
             return self.time_input_dim * 4
      @property
      def 			lowerCAmelCase__						(				self: Dict   ):
             return 8
      @property
      def 			lowerCAmelCase__						(				self: int   ):
             torch.manual_seed(0   )
             __lowerCamelCase   =						CLIPVisionConfig(
                 hidden_size=self.text_embedder_hidden_size    ,       image_size=64    ,       projection_dim=self.text_embedder_hidden_size    ,       intermediate_size=37    ,       num_attention_heads=4    ,       num_channels=3    ,       num_hidden_layers=5    ,       patch_size=1    ,       )

             __lowerCamelCase   =						CLIPVisionModel(UpperCamelCase_   )
             return model
      @property
      def 			lowerCAmelCase__						(				self: Dict   ):
             __lowerCamelCase   =						CLIPImageProcessor(
                 crop_size=2_24    ,       do_center_crop=UpperCamelCase_    ,       do_normalize=UpperCamelCase_    ,       do_resize=UpperCamelCase_    ,       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=2_24    ,       )

             return image_processor
      @property
      def 			lowerCAmelCase__						(				self: Tuple   ):
             torch.manual_seed(0   )

             __lowerCamelCase   =						{
                 """num_attention_heads""": 2,
                 """attention_head_dim""": 16,
                 """embedding_dim""": self.time_input_dim,
                 """num_embeddings""": 32,
                 """embedding_proj_dim""": self.text_embedder_hidden_size,
                 """time_embed_dim""": self.time_embed_dim,
                 """num_layers""": 1,
                 """clip_embed_dim""": self.time_input_dim * 2,
                 """additional_embeddings""": 0,
                 """time_embed_act_fn""": """gelu""",
                 """norm_in_type""": """layer""",
                 """embedding_proj_norm_type""": """layer""",
                 """encoder_hid_proj_type""": None,
                 """added_emb_type""": None,
             }

             __lowerCamelCase   =						PriorTransformer(**UpperCamelCase_   )
             return model
      @property
      def 			lowerCAmelCase__						(				self: List[Any]   ):
             torch.manual_seed(0   )

             __lowerCamelCase   =						{
                 """param_shapes""": (
                     (self.renderer_dim, 93),
                     (self.renderer_dim, 8),
                     (self.renderer_dim, 8),
                     (self.renderer_dim, 8),
                 ),
                 """d_latent""": self.time_input_dim,
                 """d_hidden""": self.renderer_dim,
                 """n_output""": 12,
                 """background""": (
                     0.1,
                     0.1,
                     0.1,
                 ),
             }
             __lowerCamelCase   =						ShapERenderer(**UpperCamelCase_   )
             return model
      def 			lowerCAmelCase__						(				self: List[str]   ):
             __lowerCamelCase   =						self.dummy_prior
             __lowerCamelCase   =						self.dummy_image_encoder
             __lowerCamelCase   =						self.dummy_image_processor
             __lowerCamelCase   =						self.dummy_renderer

             __lowerCamelCase   =						HeunDiscreteScheduler(
                 beta_schedule="""exp"""    ,       num_train_timesteps=10_24    ,       prediction_type="""sample"""    ,       use_karras_sigmas=UpperCamelCase_    ,       clip_sample=UpperCamelCase_    ,       clip_sample_range=1.0    ,       )
             __lowerCamelCase   =						{
                 """prior""": prior,
                 """image_encoder""": image_encoder,
                 """image_processor""": image_processor,
                 """renderer""": renderer,
                 """scheduler""": scheduler,
             }

             return components
      def 			lowerCAmelCase__						(				self: int    ,       UpperCamelCase_: List[Any]    ,       UpperCamelCase_: Dict=0   ):
             __lowerCamelCase   =						floats_tensor((1, 3, 64, 64)    ,       rng=random.Random(UpperCamelCase_   )   ).to(UpperCamelCase_   )

             if str(UpperCamelCase_   ).startswith("""mps"""   ):
                    __lowerCamelCase   =						torch.manual_seed(UpperCamelCase_   )
             else:
                    __lowerCamelCase   =						torch.Generator(device=UpperCamelCase_   ).manual_seed(UpperCamelCase_   )
             __lowerCamelCase   =						{
                 """image""": input_image,
                 """generator""": generator,
                 """num_inference_steps""": 1,
                 """frame_size""": 32,
                 """output_type""": """np""",
             }
             return inputs
      def 			lowerCAmelCase__						(				self: Tuple   ):
             __lowerCamelCase   =						"""cpu"""

             __lowerCamelCase   =						self.get_dummy_components()

             __lowerCamelCase   =						self.pipeline_class(**UpperCamelCase_   )
             __lowerCamelCase   =						pipe.to(UpperCamelCase_   )

             pipe.set_progress_bar_config(disable=UpperCamelCase_   )

             __lowerCamelCase   =						pipe(**self.get_dummy_inputs(UpperCamelCase_   )   )
             __lowerCamelCase   =						output.images[0]
             __lowerCamelCase   =						image[0, -3:, -3:, -1]

             assert image.shape == (20, 32, 32, 3)

             __lowerCamelCase   =						np.array(
                 [
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                 ]   )

             assert np.abs(image_slice.flatten() - expected_slice   ).max() < 1E-2
      def 			lowerCAmelCase__						(				self: List[str]   ):
             # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
             self._test_inference_batch_consistent(batch_sizes=[1, 2]   )
      def 			lowerCAmelCase__						(				self: Any   ):
             __lowerCamelCase   =						torch_device == """cpu"""
             __lowerCamelCase   =						True
             self._test_inference_batch_single_identical(
                 batch_size=2    ,       test_max_difference=UpperCamelCase_    ,       relax_max_difference=UpperCamelCase_    ,       )




      def 			lowerCAmelCase__						(				self: Any   ):
             __lowerCamelCase   =						self.get_dummy_components()
             __lowerCamelCase   =						self.pipeline_class(**UpperCamelCase_   )
             __lowerCamelCase   =						pipe.to(UpperCamelCase_   )
             pipe.set_progress_bar_config(disable=UpperCamelCase_   )

             __lowerCamelCase   =						1
             __lowerCamelCase   =						2

             __lowerCamelCase   =						self.get_dummy_inputs(UpperCamelCase_   )

             for key in inputs.keys():
                    if key in self.batch_params:
                           __lowerCamelCase   =						batch_size * [inputs[key]]

             __lowerCamelCase   =						pipe(**UpperCamelCase_    ,       num_images_per_prompt=UpperCamelCase_   )[0]

             assert images.shape[0] == batch_size * num_images_per_prompt





@slow
@require_torch_gpu
class     lowerCamelCase__(					unittest.TestCase):
      def 			lowerCAmelCase__						(				self: Union[str, Any]   ):
             # clean up the VRAM after each test
             super().tearDown()
             gc.collect()
             torch.cuda.empty_cache()




      def 			lowerCAmelCase__						(				self: Any   ):
             __lowerCamelCase   =						load_image(
                 """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/shap_e/corgi.png"""   )
             __lowerCamelCase   =						load_numpy(
                 """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
                 """/shap_e/test_shap_e_img2img_out.npy"""   )
             __lowerCamelCase   =						ShapEImgaImgPipeline.from_pretrained("""openai/shap-e-img2img"""   )
             __lowerCamelCase   =						pipe.to(UpperCamelCase_   )
             pipe.set_progress_bar_config(disable=UpperCamelCase_   )

             __lowerCamelCase   =						torch.Generator(device=UpperCamelCase_   ).manual_seed(0   )

             __lowerCamelCase   =						pipe(
                 UpperCamelCase_    ,       generator=UpperCamelCase_    ,       guidance_scale=3.0    ,       num_inference_steps=64    ,       frame_size=64    ,       output_type="""np"""    ,       ).images[0]

             assert images.shape == (20, 64, 64, 3)

             assert_mean_pixel_difference(UpperCamelCase_    ,       UpperCamelCase_   )

