


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       :
       _lowercase											=			BlenderbotConfig
       _lowercase											=			{}
       _lowercase											=			'''gelu'''







       def __init__(       self:      List[str],A_:      Union[str, Any],A_:      int=13,A_:      List[Any]=7,A_:      int=True,A_:      Dict=False,A_:      Optional[int]=99,A_:      Optional[int]=32,A_:      List[Any]=2,A_:      List[str]=4,A_:      List[str]=37,A_:      Any=0.1,A_:      str=0.1,A_:      List[Any]=20,A_:      Any=2,A_:      Tuple=1,A_:      Dict=0,):






            '''simple docstring'''




            __UpperCamelCase      =       parent
            __UpperCamelCase      =       batch_size
            __UpperCamelCase      =       seq_length
            __UpperCamelCase      =       is_training
            __UpperCamelCase      =       use_labels
            __UpperCamelCase      =       vocab_size
            __UpperCamelCase      =       hidden_size
            __UpperCamelCase      =       num_hidden_layers
            __UpperCamelCase      =       num_attention_heads
            __UpperCamelCase      =       intermediate_size

            __UpperCamelCase      =       hidden_dropout_prob
            __UpperCamelCase      =       attention_probs_dropout_prob
            __UpperCamelCase      =       max_position_embeddings
            __UpperCamelCase      =       eos_token_id
            __UpperCamelCase      =       pad_token_id
            __UpperCamelCase      =       bos_token_id







       def 						snake_case_     (       self:      Union[str, Any] ):






            '''simple docstring'''




            __UpperCamelCase      =       ids_tensor([self.batch_size, self.seq_length - 1],self.vocab_size )
            __UpperCamelCase      =       tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ),1 )
            __UpperCamelCase      =       tf.concat([input_ids, eos_tensor],axis=1 )

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

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







       def 						snake_case_     (       self:      Tuple,A_:      Any,A_:      Any ):






            '''simple docstring'''




            __UpperCamelCase      =       TFBlenderbotModel(config=__lowerCAmelCase ).get_decoder()
            __UpperCamelCase      =       inputs_dict['input_ids']

            __UpperCamelCase      =       input_ids[:1, :]
            __UpperCamelCase      =       inputs_dict['attention_mask'][:1, :]
            __UpperCamelCase      =       inputs_dict['head_mask']
            __UpperCamelCase      =       1

            # first forward pass
            __UpperCamelCase      =       model(__lowerCAmelCase,attention_mask=__lowerCAmelCase,head_mask=__lowerCAmelCase,use_cache=__lowerCAmelCase )

            __UpperCamelCase,  __UpperCamelCase      =       outputs.to_tuple()

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

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

            __UpperCamelCase      =       model(__lowerCAmelCase,attention_mask=__lowerCAmelCase )[0]
            __UpperCamelCase      =       model(__lowerCAmelCase,attention_mask=__lowerCAmelCase,past_key_values=__lowerCAmelCase )[0]

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

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

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






def 						_A						(						_lowercase ,   _lowercase ,   _lowercase ,   _lowercase=None ,   _lowercase=None ,   _lowercase=None ,   _lowercase=None ,   _lowercase=None ,   ) ->      Optional[Any]:


     """simple docstring"""


     if attention_mask is None:
          __UpperCamelCase      =       tf.cast(tf.math.not_equal(_A ,   config.pad_token_id      ) ,   tf.inta      )
     if decoder_attention_mask is None:
          __UpperCamelCase      =       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      =       tf.ones((config.encoder_layers, config.encoder_attention_heads)      )
     if decoder_head_mask is None:
          __UpperCamelCase      =       tf.ones((config.decoder_layers, config.decoder_attention_heads)      )
     if cross_attn_head_mask is None:
          __UpperCamelCase      =       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		):
       _lowercase											=			(TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else ()
       _lowercase											=			(TFBlenderbotForConditionalGeneration,) if is_tf_available() else ()
       _lowercase											=			(
           {
               '''conversational''': TFBlenderbotForConditionalGeneration,
               '''feature-extraction''': TFBlenderbotModel,
               '''summarization''': TFBlenderbotForConditionalGeneration,
               '''text2text-generation''': TFBlenderbotForConditionalGeneration,
               '''translation''': TFBlenderbotForConditionalGeneration,
           }
           if is_tf_available()
           else {}
       )
       _lowercase											=			True
       _lowercase											=			False
       _lowercase											=			False







       def 						snake_case_     (       self:      Any ):






            '''simple docstring'''




            __UpperCamelCase      =       TFBlenderbotModelTester(self )
            __UpperCamelCase      =       ConfigTester(self,config_class=__lowerCAmelCase )







       def 						snake_case_     (       self:      Any ):






            '''simple docstring'''




            self.config_tester.run_common_tests()







       def 						snake_case_     (       self:      Tuple ):






            '''simple docstring'''




            __UpperCamelCase      =       self.model_tester.prepare_config_and_inputs_for_common()
            self.model_tester.check_decoder_model_past_large_inputs(*__lowerCAmelCase )



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







       @cached_property
       def 						snake_case_     (       self:      List[Any] ):






            '''simple docstring'''




            return BlenderbotTokenizer.from_pretrained(self.model_name )







       @cached_property
       def 						snake_case_     (       self:      Optional[Any] ):






            '''simple docstring'''




            __UpperCamelCase      =       TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name )
            return model







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






            '''simple docstring'''




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



'''simple docstring'''




import os
import string
import sys


__lowercase          =  1 << 8

__lowercase          =  {
    '''tab''': ord('''\t'''),
    '''newline''': ord('''\r'''),
    '''esc''': 2_7,
    '''up''': 6_5 + ARROW_KEY_FLAG,
    '''down''': 6_6 + ARROW_KEY_FLAG,
    '''right''': 6_7 + ARROW_KEY_FLAG,
    '''left''': 6_8 + ARROW_KEY_FLAG,
    '''mod_int''': 9_1,
    '''undefined''': sys.maxsize,
    '''interrupt''': 3,
    '''insert''': 5_0,
    '''delete''': 5_1,
    '''pg_up''': 5_3,
    '''pg_down''': 5_4,
}

__lowercase          =  KEYMAP['''up''']
__lowercase          =  KEYMAP['''left''']

if sys.platform == "win32":
						__lowercase          =  []
						__lowercase          =  {
						    B'''\xe0H''': KEYMAP['''up'''] - ARROW_KEY_FLAG,
						    B'''\x00H''': KEYMAP['''up'''] - ARROW_KEY_FLAG,
						    B'''\xe0P''': KEYMAP['''down'''] - ARROW_KEY_FLAG,
						    B'''\x00P''': KEYMAP['''down'''] - ARROW_KEY_FLAG,
						    B'''\xe0M''': KEYMAP['''right'''] - ARROW_KEY_FLAG,
						    B'''\x00M''': KEYMAP['''right'''] - ARROW_KEY_FLAG,
						    B'''\xe0K''': KEYMAP['''left'''] - ARROW_KEY_FLAG,
						    B'''\x00K''': KEYMAP['''left'''] - ARROW_KEY_FLAG,
						}

for i in range(1_0):
						__lowercase          =  ord(str(i))






def 				snake_case__							(			)    ->	List[Any]:

		'''simple docstring'''


		if os.name == "nt":
				import msvcrt

				lowerCAmelCase   					=  """mbcs"""
				# Flush the keyboard buffer
				while msvcrt.kbhit():
						msvcrt.getch()
				if len(_A ) == 0:
						# Read the keystroke
						lowerCAmelCase   					=  msvcrt.getch()

						# If it is a prefix char, get second part
						if ch in (b"\x00", b"\xe0"):
								lowerCAmelCase   					=  ch + msvcrt.getch()
								# Translate actual Win chars to bullet char types
								try:
										lowerCAmelCase   					=  chr(WIN_KEYMAP[cha] )
										WIN_CH_BUFFER.append(chr(KEYMAP["""mod_int"""] ) )
										WIN_CH_BUFFER.append(_A )
										if ord(_A ) in (
										    KEYMAP["insert"] - 1 << 9,
										    KEYMAP["delete"] - 1 << 9,
										    KEYMAP["pg_up"] - 1 << 9,
										    KEYMAP["pg_down"] - 1 << 9,
										):
												WIN_CH_BUFFER.append(chr(126 ) )
										lowerCAmelCase   					=  chr(KEYMAP["""esc"""] )
								except KeyError:
										lowerCAmelCase   					=  cha[1]
						else:
								lowerCAmelCase   					=  ch.decode(_A )
				else:
						lowerCAmelCase   					=  WIN_CH_BUFFER.pop(0 )
		elif os.name == "posix":
				import termios
				import tty

				lowerCAmelCase   					=  sys.stdin.fileno()
				lowerCAmelCase   					=  termios.tcgetattr(_A )
				try:
						tty.setraw(_A )
						lowerCAmelCase   					=  sys.stdin.read(1 )
				finally:
						termios.tcsetattr(_A      ,   termios.TCSADRAIN      ,   _A )
		return ch






def 				snake_case__							(			)    ->	Tuple:

		'''simple docstring'''


		lowerCAmelCase   					=  get_raw_chars()
		if ord(_A ) in [KEYMAP["interrupt"], KEYMAP["newline"]]:
				return char

		elif ord(_A ) == KEYMAP["esc"]:
				lowerCAmelCase   					=  get_raw_chars()
				if ord(_A ) == KEYMAP["mod_int"]:
						lowerCAmelCase   					=  get_raw_chars()
						if ord(_A ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(_A ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG:
								return chr(ord(_A ) + ARROW_KEY_FLAG )
						else:
								return KEYMAP["undefined"]
				else:
						return get_raw_chars()

		else:
				if char in string.printable:
						return char
				else:
						return KEYMAP["undefined"]




