



"""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        __snake_case       :
  snake_case__				:   List[Any]  			=						BlenderbotConfig
  snake_case__				:   List[str]  			=						{}
  snake_case__				:   Optional[int]  			=						'gelu'




  def __init__( self     :						str						,					__lowerCAmelCase     :						Optional[Any]						,					__lowerCAmelCase     :						List[Any]=1_3						,					__lowerCAmelCase     :						Optional[int]=7						,					__lowerCAmelCase     :						Union[str, Any]=True						,					__lowerCAmelCase     :						Tuple=False						,					__lowerCAmelCase     :						str=9_9						,					__lowerCAmelCase     :						Optional[Any]=3_2						,					__lowerCAmelCase     :						int=2						,					__lowerCAmelCase     :						int=4						,					__lowerCAmelCase     :						Tuple=3_7						,					__lowerCAmelCase     :						Tuple=0.1						,					__lowerCAmelCase     :						Optional[int]=0.1						,					__lowerCAmelCase     :						Any=2_0						,					__lowerCAmelCase     :						List[Any]=2						,					__lowerCAmelCase     :						List[str]=1						,					__lowerCAmelCase     :						Union[str, Any]=0						,					):






         """simple docstring"""






         _lowerCamelCase						:				str    			= parent
         _lowerCamelCase						:				List[str]    			= batch_size
         _lowerCamelCase						:				Tuple    			= seq_length
         _lowerCamelCase						:				Any    			= is_training
         _lowerCamelCase						:				Union[str, Any]    			= use_labels
         _lowerCamelCase						:				List[str]    			= vocab_size
         _lowerCamelCase						:				Union[str, Any]    			= hidden_size
         _lowerCamelCase						:				Dict    			= num_hidden_layers
         _lowerCamelCase						:				Optional[Any]    			= num_attention_heads
         _lowerCamelCase						:				Union[str, Any]    			= intermediate_size

         _lowerCamelCase						:				Optional[Any]    			= hidden_dropout_prob
         _lowerCamelCase						:				int    			= attention_probs_dropout_prob
         _lowerCamelCase						:				List[Any]    			= max_position_embeddings
         _lowerCamelCase						:				Union[str, Any]    			= eos_token_id
         _lowerCamelCase						:				List[Any]    			= pad_token_id
         _lowerCamelCase						:				List[str]    			= bos_token_id




  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase						:				Tuple    			= ids_tensor([self.batch_size, self.seq_length - 1]						,					self.vocab_size		)
         _lowerCamelCase						:				str    			= tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size		)						,					1		)
         _lowerCamelCase						:				Optional[int]    			= tf.concat([input_ids, eos_tensor]						,					axis=1		)

         _lowerCamelCase						:				List[Any]    			= ids_tensor([self.batch_size, self.seq_length]						,					self.vocab_size		)

         _lowerCamelCase						:				Union[str, Any]    			= 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						:				int    			= prepare_blenderbot_inputs_dict(SCREAMING_SNAKE_CASE_						,					SCREAMING_SNAKE_CASE_						,					SCREAMING_SNAKE_CASE_		)
         return config, inputs_dict




  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[int]						,					__lowerCAmelCase     :						Tuple						,					__lowerCAmelCase     :						List[Any]		):






         """simple docstring"""






         _lowerCamelCase						:				int    			= TFBlenderbotModel(config=SCREAMING_SNAKE_CASE_		).get_decoder()
         _lowerCamelCase						:				Any    			= inputs_dict['input_ids']

         _lowerCamelCase						:				Union[str, Any]    			= input_ids[:1, :]
         _lowerCamelCase						:				Dict    			= inputs_dict['attention_mask'][:1, :]
         _lowerCamelCase						:				Any    			= inputs_dict['head_mask']
         _lowerCamelCase						:				Optional[Any]    			= 1

         # first forward pass
         _lowerCamelCase						:				Any    			= model(SCREAMING_SNAKE_CASE_						,					attention_mask=SCREAMING_SNAKE_CASE_						,					head_mask=SCREAMING_SNAKE_CASE_						,					use_cache=SCREAMING_SNAKE_CASE_		)

         _lowerCamelCase						:				Optional[int]    			= 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						:				str    			= tf.cast(ids_tensor((self.batch_size, 3)						,					2		)						,					tf.inta		)

         # append to next input_ids and
         _lowerCamelCase						:				Dict    			= tf.concat([input_ids, next_tokens]						,					axis=-1		)
         _lowerCamelCase						:				List[str]    			= tf.concat([attention_mask, next_attn_mask]						,					axis=-1		)

         _lowerCamelCase						:				Union[str, Any]    			= model(SCREAMING_SNAKE_CASE_						,					attention_mask=SCREAMING_SNAKE_CASE_		)[0]
         _lowerCamelCase						:				str    			= model(SCREAMING_SNAKE_CASE_						,					attention_mask=SCREAMING_SNAKE_CASE_						,					past_key_values=SCREAMING_SNAKE_CASE_		)[0]

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

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

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






def        snake_case_  (      A_					:		Tuple,				A_					:		List[Any],				A_					:		Tuple,				A_					:		List[str]=None,				A_					:		Tuple=None,				A_					:		Any=None,				A_					:		str=None,				A_					:		Optional[int]=None,				):


       '''simple docstring'''





       if attention_mask is None:
              _lowerCamelCase						:				Tuple    			= tf.cast(tf.math.not_equal(UpperCAmelCase_,				config.pad_token_id						),				tf.inta						)
       if decoder_attention_mask is None:
              _lowerCamelCase						:				Dict    			= 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						:				List[Any]    			= tf.ones((config.encoder_layers, config.encoder_attention_heads)						)
       if decoder_head_mask is None:
              _lowerCamelCase						:				Union[str, Any]    			= tf.ones((config.decoder_layers, config.decoder_attention_heads)						)
       if cross_attn_head_mask is None:
              _lowerCamelCase						:				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        __snake_case       (	_UpperCAmelCase  ,     _UpperCAmelCase  ,     unittest.TestCase):
  snake_case__				:   int  			=						(TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else ()
  snake_case__				:   Optional[Any]  			=						(TFBlenderbotForConditionalGeneration,) if is_tf_available() else ()
  snake_case__				:   Dict  			=						(
      {
          'conversational': TFBlenderbotForConditionalGeneration,
          'feature-extraction': TFBlenderbotModel,
          'summarization': TFBlenderbotForConditionalGeneration,
          'text2text-generation': TFBlenderbotForConditionalGeneration,
          'translation': TFBlenderbotForConditionalGeneration,
      }
      if is_tf_available()
      else {}
  )
  snake_case__				:   List[str]  			=						True
  snake_case__				:   Any  			=						False
  snake_case__				:   Any  			=						False




  def 						SCREAMING_SNAKE_CASE				( self     :						Dict		):






         """simple docstring"""






         _lowerCamelCase						:				Optional[Any]    			= TFBlenderbotModelTester(self		)
         _lowerCamelCase						:				Optional[int]    			= ConfigTester(self						,					config_class=SCREAMING_SNAKE_CASE_		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Tuple		):






         """simple docstring"""






         self.config_tester.run_common_tests()




  def 						SCREAMING_SNAKE_CASE				( self     :						List[str]		):






         """simple docstring"""






         _lowerCamelCase						:				str    			= self.model_tester.prepare_config_and_inputs_for_common()
         self.model_tester.check_decoder_model_past_large_inputs(*SCREAMING_SNAKE_CASE_		)






@require_tokenizers
@require_tf
class        __snake_case       (	unittest.TestCase):
  snake_case__				:   Optional[int]  			=						['My friends are cool but they eat too many carbs.']
  snake_case__				:   Optional[Any]  			=						'facebook/blenderbot-400M-distill'




  @cached_property
  def 						SCREAMING_SNAKE_CASE				( self     :						int		):






         """simple docstring"""






         return BlenderbotTokenizer.from_pretrained(self.model_name		)




  @cached_property
  def 						SCREAMING_SNAKE_CASE				( self     :						Tuple		):






         """simple docstring"""






         _lowerCamelCase						:				List[Any]    			= TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name		)
         return model




  @slow
  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[int]		):






         """simple docstring"""






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






'''simple docstring'''



from importlib import import_module

from .logging import get_logger


A__		:					str												=  get_logger(__name__)





class    UpperCAmelCase_  :





    """simple docstring"""






    def __init__(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_=None	)				->     List[Any]:
         __lowerCamelCase   :	List[str]								=		attrs or []
         if module is not None:
              for key in module.__dict__:
                   if key in attrs or not key.startswith('__'	):
                        setattr(self			,     SCREAMING_SNAKE_CASE_			,     getattr(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)	)
         __lowerCamelCase   :	Optional[int]								=		module._original_module if isinstance(SCREAMING_SNAKE_CASE_			,     _PatchedModuleObj	) else module







class    UpperCAmelCase_  :





    """simple docstring"""






    lowerCamelCase  :					Tuple							     =			[]







    def __init__(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_=None	)				->     Union[str, Any]:
         __lowerCamelCase   :	Optional[int]								=		obj
         __lowerCamelCase   :	List[Any]								=		target
         __lowerCamelCase   :	Union[str, Any]								=		new
         __lowerCamelCase   :	Union[str, Any]								=		target.split('.'	)[0]
         __lowerCamelCase   :	Dict								=		{}
         __lowerCamelCase   :	Dict								=		attrs or []







    def __enter__(							self	)				->     Optional[int]:
         *__lowerCamelCase				,       __lowerCamelCase   :	int								=		self.target.split('.'	)

         # Patch modules:
         # it's used to patch attributes of submodules like "os.path.join";
         # in this case we need to patch "os" and "os.path"

         for i in range(len(SCREAMING_SNAKE_CASE_	)	):
              try:
                   __lowerCamelCase   :	Optional[int]								=		import_module('.'.join(submodules[: i + 1]	)	)
              except ModuleNotFoundError:
                   continue
              # We iterate over all the globals in self.obj in case we find "os" or "os.path"
              for attr in self.obj.__dir__():
                   __lowerCamelCase   :	List[Any]								=		getattr(self.obj			,     SCREAMING_SNAKE_CASE_	)
                   # We don't check for the name of the global, but rather if its value *is* "os" or "os.path".
                   # This allows to patch renamed modules like "from os import path as ospath".
                   if obj_attr is submodule or (
                       (isinstance(SCREAMING_SNAKE_CASE_			,     _PatchedModuleObj	) and obj_attr._original_module is submodule)
                   ):
                        __lowerCamelCase   :	Optional[Any]								=		obj_attr
                        # patch at top level
                        setattr(self.obj			,     SCREAMING_SNAKE_CASE_			,     _PatchedModuleObj(SCREAMING_SNAKE_CASE_			,     attrs=self.attrs	)	)
                        __lowerCamelCase   :	str								=		getattr(self.obj			,     SCREAMING_SNAKE_CASE_	)
                        # construct lower levels patches
                        for key in submodules[i + 1 :]:
                             setattr(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     _PatchedModuleObj(getattr(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)			,     attrs=self.attrs	)	)
                             __lowerCamelCase   :	List[Any]								=		getattr(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)
                        # finally set the target attribute
                        setattr(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     self.new	)

        # Patch attribute itself:
        # it's used for builtins like "open",
        # and also to patch "os.path.join" we may also need to patch "join"
        # itself if it was imported as "from os.path import join".

         if submodules:  # if it's an attribute of a submodule like "os.path.join"
              try:
                   __lowerCamelCase   :	Union[str, Any]								=		getattr(import_module('.'.join(SCREAMING_SNAKE_CASE_	)	)			,     SCREAMING_SNAKE_CASE_	)
              except (AttributeError, ModuleNotFoundError):
                   return
              # We iterate over all the globals in self.obj in case we find "os.path.join"
              for attr in self.obj.__dir__():
                   # We don't check for the name of the global, but rather if its value *is* "os.path.join".
                   # This allows to patch renamed attributes like "from os.path import join as pjoin".
                   if getattr(self.obj			,     SCREAMING_SNAKE_CASE_	) is attr_value:
                        __lowerCamelCase   :	Optional[int]								=		getattr(self.obj			,     SCREAMING_SNAKE_CASE_	)
                        setattr(self.obj			,     SCREAMING_SNAKE_CASE_			,     self.new	)
         elif target_attr in globals()["__builtins__"]:  # if it'a s builtin like "open"
              __lowerCamelCase   :	List[Any]								=		globals()['__builtins__'][target_attr]
              setattr(self.obj			,     SCREAMING_SNAKE_CASE_			,     self.new	)
         else:
              raise RuntimeError(f'Tried to patch attribute {target_attr} instead of a submodule.'	)







    def __exit__(							self			,     *SCREAMING_SNAKE_CASE_	)				->     Optional[Any]:
         for attr in list(self.original	):
              setattr(self.obj			,     SCREAMING_SNAKE_CASE_			,     self.original.pop(SCREAMING_SNAKE_CASE_	)	)







    def       lowercase_				(							self	)				->     Optional[int]:
         self.__enter__()
         self._active_patches.append(self	)







    def       lowercase_				(							self	)				->     str:
         try:
              self._active_patches.remove(self	)
         except ValueError:
              # If the patch hasn't been started this will fail
              return None

         return self.__exit__()






