


'''simple docstring'''






from __future__ import annotations

import unittest

from transformers import BlenderbotSmallConfig, BlenderbotSmallTokenizer, 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, TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel






@require_tf
class    __magic_name__				:
       UpperCAmelCase		 =BlenderbotSmallConfig
       UpperCAmelCase		 ={}
       UpperCAmelCase		 ="gelu"






       def __init__(     self    ,				snake_case    ,				snake_case=1_3    ,				snake_case=7    ,				snake_case=True    ,				snake_case=False    ,				snake_case=9_9    ,				snake_case=3_2    ,				snake_case=2    ,				snake_case=4    ,				snake_case=3_7    ,				snake_case=0.1    ,				snake_case=0.1    ,				snake_case=2_0    ,				snake_case=2    ,				snake_case=1    ,				snake_case=0    ,				)		->    Tuple:




          '''simple docstring'''
          _UpperCAmelCase  :  List[str]				       =parent
          _UpperCAmelCase  :  Union[str, Any]				       =batch_size
          _UpperCAmelCase  :  int				       =seq_length
          _UpperCAmelCase  :  List[Any]				       =is_training
          _UpperCAmelCase  :  str				       =use_labels
          _UpperCAmelCase  :  List[Any]				       =vocab_size
          _UpperCAmelCase  :  Dict				       =hidden_size
          _UpperCAmelCase  :  str				       =num_hidden_layers
          _UpperCAmelCase  :  Any				       =num_attention_heads
          _UpperCAmelCase  :  Tuple				       =intermediate_size

          _UpperCAmelCase  :  List[Any]				       =hidden_dropout_prob
          _UpperCAmelCase  :  List[str]				       =attention_probs_dropout_prob
          _UpperCAmelCase  :  List[Any]				       =max_position_embeddings
          _UpperCAmelCase  :  Optional[Any]				       =eos_token_id
          _UpperCAmelCase  :  Optional[Any]				       =pad_token_id
          _UpperCAmelCase  :  Tuple				       =bos_token_id






       def      lowerCAmelCase						(     self)		->    Tuple:




          '''simple docstring'''
          _UpperCAmelCase  :  Tuple				       =ids_tensor([self.batch_size, self.seq_length - 1]    ,				self.vocab_size)
          _UpperCAmelCase  :  Optional[Any]				       =tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size)    ,				1)
          _UpperCAmelCase  :  List[Any]				       =tf.concat([input_ids, eos_tensor]    ,				axis=1)

          _UpperCAmelCase  :  List[str]				       =ids_tensor([self.batch_size, self.seq_length]    ,				self.vocab_size)

          _UpperCAmelCase  :  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    ,				)
          _UpperCAmelCase  :  List[str]				       =prepare_blenderbot_small_inputs_dict(snake_case    ,				snake_case    ,				snake_case)
          return config, inputs_dict






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case)		->    str:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[int]				       =TFBlenderbotSmallModel(config=snake_case).get_decoder()
          _UpperCAmelCase  :  Optional[Any]				       =inputs_dict['input_ids']

          _UpperCAmelCase  :  Dict				       =input_ids[:1, :]
          _UpperCAmelCase  :  List[str]				       =inputs_dict['attention_mask'][:1, :]
          _UpperCAmelCase  :  Any				       =inputs_dict['head_mask']
          _UpperCAmelCase  :  int				       =1

          # first forward pass
          _UpperCAmelCase  :  Any				       =model(snake_case    ,				attention_mask=snake_case    ,				head_mask=snake_case    ,				use_cache=snake_case)

          _UpperCAmelCase       ,			_UpperCAmelCase  :  Tuple				       =outputs.to_tuple()

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

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

          _UpperCAmelCase  :  List[str]				       =model(snake_case    ,				attention_mask=snake_case)[0]
          _UpperCAmelCase  :  Optional[Any]				       =model(snake_case    ,				attention_mask=snake_case    ,				past_key_values=snake_case)[0]

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

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

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






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







   '''simple docstring'''




   if attention_mask is None:
      _UpperCAmelCase  :  int				       =tf.cast(tf.math.not_equal(__lowerCamelCase			,       config.pad_token_id				)			,       tf.inta				)
   if decoder_attention_mask is None:
      _UpperCAmelCase  :  Tuple				       =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:
      _UpperCAmelCase  :  str				       =tf.ones((config.encoder_layers, config.encoder_attention_heads)				)
   if decoder_head_mask is None:
      _UpperCAmelCase  :  List[Any]				       =tf.ones((config.decoder_layers, config.decoder_attention_heads)				)
   if cross_attn_head_mask is None:
      _UpperCAmelCase  :  List[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    __magic_name__				(						lowerCAmelCase   ,lowerCAmelCase   ,unittest.TestCase							):
       UpperCAmelCase		 =(
           (TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel) if is_tf_available() else ()
       )
       UpperCAmelCase		 =(TFBlenderbotSmallForConditionalGeneration,) if is_tf_available() else ()
       UpperCAmelCase		 =(
           {
               "conversational": TFBlenderbotSmallForConditionalGeneration,
               "feature-extraction": TFBlenderbotSmallModel,
               "summarization": TFBlenderbotSmallForConditionalGeneration,
               "text2text-generation": TFBlenderbotSmallForConditionalGeneration,
               "translation": TFBlenderbotSmallForConditionalGeneration,
           }
           if is_tf_available()
           else {}
       )
       UpperCAmelCase		 =True
       UpperCAmelCase		 =False
       UpperCAmelCase		 =False






       def      lowerCAmelCase						(     self)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  List[str]				       =TFBlenderbotSmallModelTester(self)
          _UpperCAmelCase  :  List[str]				       =ConfigTester(self    ,				config_class=snake_case)






       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          self.config_tester.run_common_tests()






       def      lowerCAmelCase						(     self)		->    List[Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  List[str]				       =self.model_tester.prepare_config_and_inputs_for_common()
          self.model_tester.check_decoder_model_past_large_inputs(*snake_case)






@require_tokenizers
@require_tf
class    __magic_name__				(						unittest.TestCase							):
       UpperCAmelCase		 =[
           "Social anxiety\nWow, I am never shy. Do you have anxiety?\nYes. I end up sweating and blushing and feel like "
           "  i'm going to throw up.\nand why is that?"
       ]
       UpperCAmelCase		 ="facebook/blenderbot_small-90M"






       @cached_property
       def      lowerCAmelCase						(     self)		->    Dict:




          '''simple docstring'''
          # use "old" tokenizer here because of bug when downloading new tokenizer
          return BlenderbotSmallTokenizer.from_pretrained('facebook/blenderbot-90M')






       @cached_property
       def      lowerCAmelCase						(     self)		->    Optional[Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  List[str]				       =TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name)
          return model






       @slow
       def      lowerCAmelCase						(     self)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  List[str]				       =self.tokenizer(self.src_text    ,				return_tensors='tf')
          _UpperCAmelCase  :  Tuple				       =self.model.generate(
              model_inputs.input_ids    ,				attention_mask=model_inputs.attention_mask    ,				num_beams=2    ,				use_cache=snake_case    ,				)
          _UpperCAmelCase  :  Optional[int]				       =self.tokenizer.batch_decode(generated_ids.numpy()    ,				skip_special_tokens=snake_case)[0]
          assert generated_words in (
              "i don't know. i just feel like i'm going to throw up. it's not fun.",
              "i'm not sure. i just feel like i've been feeling like i have to be in a certain place",
              "i'm not sure. i just feel like i've been in a bad situation.",
          )





'''simple docstring'''






import unittest

from diffusers.pipelines.pipeline_utils import is_safetensors_compatible






class    __magic_name__				(						unittest.TestCase							):






       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  Any				       =[
              'safety_checker/pytorch_model.bin',
              'safety_checker/model.safetensors',
              'vae/diffusion_pytorch_model.bin',
              'vae/diffusion_pytorch_model.safetensors',
              'text_encoder/pytorch_model.bin',
              'text_encoder/model.safetensors',
              'unet/diffusion_pytorch_model.bin',
              'unet/diffusion_pytorch_model.safetensors',
          ]
          self.assertTrue(is_safetensors_compatible(snake_case))






       def      lowerCAmelCase						(     self)		->    List[str]:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[int]				       =[
              'unet/diffusion_pytorch_model.bin',
              'unet/diffusion_pytorch_model.safetensors',
          ]
          self.assertTrue(is_safetensors_compatible(snake_case))






       def      lowerCAmelCase						(     self)		->    Any:




          '''simple docstring'''
          _UpperCAmelCase  :  Union[str, Any]				       =[
              'safety_checker/pytorch_model.bin',
              'safety_checker/model.safetensors',
              'vae/diffusion_pytorch_model.bin',
              'vae/diffusion_pytorch_model.safetensors',
              'text_encoder/pytorch_model.bin',
              'text_encoder/model.safetensors',
              'unet/diffusion_pytorch_model.bin',
              # Removed: 'unet/diffusion_pytorch_model.safetensors',
          ]
          self.assertFalse(is_safetensors_compatible(snake_case))






       def      lowerCAmelCase						(     self)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  int				       =[
              'text_encoder/pytorch_model.bin',
              'text_encoder/model.safetensors',
          ]
          self.assertTrue(is_safetensors_compatible(snake_case))






       def      lowerCAmelCase						(     self)		->    Any:




          '''simple docstring'''
          _UpperCAmelCase  :  Any				       =[
              'safety_checker/pytorch_model.bin',
              'safety_checker/model.safetensors',
              'vae/diffusion_pytorch_model.bin',
              'vae/diffusion_pytorch_model.safetensors',
              'text_encoder/pytorch_model.bin',
              # Removed: 'text_encoder/model.safetensors',
              'unet/diffusion_pytorch_model.bin',
              'unet/diffusion_pytorch_model.safetensors',
          ]
          self.assertFalse(is_safetensors_compatible(snake_case))






       def      lowerCAmelCase						(     self)		->    List[Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  Union[str, Any]				       =[
              'safety_checker/pytorch_model.fp16.bin',
              'safety_checker/model.fp16.safetensors',
              'vae/diffusion_pytorch_model.fp16.bin',
              'vae/diffusion_pytorch_model.fp16.safetensors',
              'text_encoder/pytorch_model.fp16.bin',
              'text_encoder/model.fp16.safetensors',
              'unet/diffusion_pytorch_model.fp16.bin',
              'unet/diffusion_pytorch_model.fp16.safetensors',
          ]
          _UpperCAmelCase  :  str				       ='fp16'
          self.assertTrue(is_safetensors_compatible(snake_case    ,				variant=snake_case))






       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  Tuple				       =[
              'unet/diffusion_pytorch_model.fp16.bin',
              'unet/diffusion_pytorch_model.fp16.safetensors',
          ]
          _UpperCAmelCase  :  int				       ='fp16'
          self.assertTrue(is_safetensors_compatible(snake_case    ,				variant=snake_case))






       def      lowerCAmelCase						(     self)		->    Optional[int]:




          '''simple docstring'''
          # pass variant but use the non-variant filenames
          _UpperCAmelCase  :  Optional[int]				       =[
              'unet/diffusion_pytorch_model.bin',
              'unet/diffusion_pytorch_model.safetensors',
          ]
          _UpperCAmelCase  :  Optional[Any]				       ='fp16'
          self.assertTrue(is_safetensors_compatible(snake_case    ,				variant=snake_case))






       def      lowerCAmelCase						(     self)		->    int:




          '''simple docstring'''
          _UpperCAmelCase  :  str				       =[
              'safety_checker/pytorch_model.fp16.bin',
              'safety_checker/model.fp16.safetensors',
              'vae/diffusion_pytorch_model.fp16.bin',
              'vae/diffusion_pytorch_model.fp16.safetensors',
              'text_encoder/pytorch_model.fp16.bin',
              'text_encoder/model.fp16.safetensors',
              'unet/diffusion_pytorch_model.fp16.bin',
              # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors',
          ]
          _UpperCAmelCase  :  List[str]				       ='fp16'
          self.assertFalse(is_safetensors_compatible(snake_case    ,				variant=snake_case))






       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  Dict				       =[
              'text_encoder/pytorch_model.fp16.bin',
              'text_encoder/model.fp16.safetensors',
          ]
          _UpperCAmelCase  :  Optional[Any]				       ='fp16'
          self.assertTrue(is_safetensors_compatible(snake_case    ,				variant=snake_case))






       def      lowerCAmelCase						(     self)		->    Any:




          '''simple docstring'''
          # pass variant but use the non-variant filenames
          _UpperCAmelCase  :  Optional[int]				       =[
              'text_encoder/pytorch_model.bin',
              'text_encoder/model.safetensors',
          ]
          _UpperCAmelCase  :  List[Any]				       ='fp16'
          self.assertTrue(is_safetensors_compatible(snake_case    ,				variant=snake_case))






       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  str				       =[
              'safety_checker/pytorch_model.fp16.bin',
              'safety_checker/model.fp16.safetensors',
              'vae/diffusion_pytorch_model.fp16.bin',
              'vae/diffusion_pytorch_model.fp16.safetensors',
              'text_encoder/pytorch_model.fp16.bin',
              # 'text_encoder/model.fp16.safetensors',
              'unet/diffusion_pytorch_model.fp16.bin',
              'unet/diffusion_pytorch_model.fp16.safetensors',
          ]
          _UpperCAmelCase  :  Tuple				       ='fp16'
          self.assertFalse(is_safetensors_compatible(snake_case    ,				variant=snake_case))

