



'''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 		lowerCamelCase__      :




     '''simple docstring'''



     A_						    =						BlenderbotSmallConfig
     A_						    =						{}
     A_						    =						"""gelu"""




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



      '''simple docstring'''





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

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




     def 						__UpperCAmelCase     (  self				:	int					)  ->       List[Any]:



      '''simple docstring'''





      _lowercase					:       Optional[int]       =		ids_tensor([self.batch_size, self.seq_length - 1]     ,  self.vocab_size					)
      _lowercase					:       List[Any]       =		tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size					)     ,  1					)
      _lowercase					:       Dict       =		tf.concat([input_ids, eos_tensor]     ,  axis=1					)

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

      _lowercase					:       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     ,  )
      _lowercase					:       int       =		prepare_blenderbot_small_inputs_dict(UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_					)
      return config, inputs_dict




     def 						__UpperCAmelCase     (  self				:	Tuple     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	Optional[Any]					)  ->       List[str]:



      '''simple docstring'''





      _lowercase					:       List[Any]       =		TFBlenderbotSmallModel(config=UpperCamelCase_					).get_decoder()
      _lowercase					:       str       =		inputs_dict['input_ids']

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

      # first forward pass
      _lowercase					:       Any       =		model(UpperCamelCase_     ,  attention_mask=UpperCamelCase_     ,  head_mask=UpperCamelCase_     ,  use_cache=UpperCamelCase_					)

      _lowercase					:       Tuple       =		outputs.to_tuple()

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

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

      _lowercase					:       Optional[int]       =		model(UpperCamelCase_     ,  attention_mask=UpperCamelCase_					)[0]
      _lowercase					:       Optional[int]       =		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
      _lowercase					:       Union[str, Any]       =		int(ids_tensor((1,)     ,  output_from_past.shape[-1]					)					)
      _lowercase					:       Union[str, Any]       =		output_from_no_past[:, -3:, random_slice_idx]
      _lowercase					:       Any       =		output_from_past[:, :, random_slice_idx]

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







def 						__UpperCamelCase				(							_lowercase,				_lowercase,				_lowercase,				_lowercase=None,				_lowercase=None,				_lowercase=None,				_lowercase=None,				_lowercase=None,				) ->			Tuple:
 if attention_mask is None:
  _lowercase					:       Any       =		tf.cast(tf.math.not_equal(_lowercase,				config.pad_token_id					),				tf.inta					)
 if decoder_attention_mask is None:
  _lowercase					:       Optional[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:
  _lowercase					:       str       =		tf.ones((config.encoder_layers, config.encoder_attention_heads)					)
 if decoder_head_mask is None:
  _lowercase					:       Optional[int]       =		tf.ones((config.decoder_layers, config.decoder_attention_heads)					)
 if cross_attn_head_mask is None:
  _lowercase					:       Dict       =		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__      (						A	,   A	,   unittest.TestCase     ):




     '''simple docstring'''



     A_						    =						(
         (TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel) if is_tf_available() else ()
     )
     A_						    =						(TFBlenderbotSmallForConditionalGeneration,) if is_tf_available() else ()
     A_						    =						(
         {
             """conversational""": TFBlenderbotSmallForConditionalGeneration,
             """feature-extraction""": TFBlenderbotSmallModel,
             """summarization""": TFBlenderbotSmallForConditionalGeneration,
             """text2text-generation""": TFBlenderbotSmallForConditionalGeneration,
             """translation""": TFBlenderbotSmallForConditionalGeneration,
         }
         if is_tf_available()
         else {}
     )
     A_						    =						True
     A_						    =						False
     A_						    =						False




     def 						__UpperCAmelCase     (  self				:	int					)  ->       List[str]:



      '''simple docstring'''





      _lowercase					:       Union[str, Any]       =		TFBlenderbotSmallModelTester(self					)
      _lowercase					:       Tuple       =		ConfigTester(self     ,  config_class=UpperCamelCase_					)




     def 						__UpperCAmelCase     (  self				:	Optional[int]					)  ->       Dict:



      '''simple docstring'''





      self.config_tester.run_common_tests()




     def 						__UpperCAmelCase     (  self				:	Optional[int]					)  ->       Union[str, Any]:



      '''simple docstring'''





      _lowercase					:       str       =		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     ):




     '''simple docstring'''



     A_						    =						[
         """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?"""
     ]
     A_						    =						"""facebook/blenderbot_small-90M"""




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



      '''simple docstring'''





      return BlenderbotSmallTokenizer.from_pretrained('facebook/blenderbot-90M'					)




     @cached_property
     def 						__UpperCAmelCase     (  self				:	Union[str, Any]					)  ->       List[str]:



      '''simple docstring'''





      _lowercase					:       Tuple       =		TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name					)
      return model




     @slow
     def 						__UpperCAmelCase     (  self				:	Union[str, Any]					)  ->       List[Any]:



      '''simple docstring'''





      _lowercase					:       Optional[Any]       =		self.tokenizer(self.src_text     ,  return_tensors='tf'					)
      _lowercase					:       List[Any]       =		self.model.generate(
          model_inputs.input_ids     ,  attention_mask=model_inputs.attention_mask     ,  num_beams=2     ,  use_cache=UpperCamelCase_     ,  )
      _lowercase					:       Optional[Any]       =		self.tokenizer.batch_decode(generated_ids.numpy()     ,  skip_special_tokens=UpperCamelCase_					)[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 copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from ...utils import logging
from ..auto import CONFIG_MAPPING


_A				:    int     						=logging.get_logger(__name__)

_A				:    Union[str, Any]     						={
    '''Salesforce/instruct-blip-flan-t5''': '''https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json''',
}



class 		lowerCamelCase__      (						A     ):




					'''simple docstring'''



					A_						    =						"""instructblip_vision_model"""




					def __init__(  self				:	Union[str, Any]     ,  UpperCamelCase_				:	str=1408     ,  UpperCamelCase_				:	Tuple=6144     ,  UpperCamelCase_				:	Union[str, Any]=39     ,  UpperCamelCase_				:	Optional[Any]=16     ,  UpperCamelCase_				:	str=224     ,  UpperCamelCase_				:	Dict=14     ,  UpperCamelCase_				:	Dict="gelu"     ,  UpperCamelCase_				:	int=1E-6     ,  UpperCamelCase_				:	int=0.0     ,  UpperCamelCase_				:	List[str]=1E-10     ,  UpperCamelCase_				:	str=True     ,  **UpperCamelCase_				:	Dict     ,  )  ->       Any:



						'''simple docstring'''





						super().__init__(**UpperCamelCase_					)

						_lowercase					:       Optional[Any]       =		hidden_size
						_lowercase					:       Optional[Any]       =		intermediate_size
						_lowercase					:       Optional[int]       =		num_hidden_layers
						_lowercase					:       str       =		num_attention_heads
						_lowercase					:       Tuple       =		patch_size
						_lowercase					:       Dict       =		image_size
						_lowercase					:       Optional[int]       =		initializer_range
						_lowercase					:       List[Any]       =		attention_dropout
						_lowercase					:       int       =		layer_norm_eps
						_lowercase					:       Optional[int]       =		hidden_act
						_lowercase					:       str       =		qkv_bias




					@classmethod
					def 						__UpperCAmelCase     (  cls				:	List[Any]     ,  UpperCamelCase_				:	Union[str, os.PathLike]     ,  **UpperCamelCase_				:	List[str]					)  ->       "PretrainedConfig":



						'''simple docstring'''





						cls._set_token_in_kwargs(UpperCamelCase_					)

						_lowercase  ,    _lowercase					:       Tuple       =		cls.get_config_dict(UpperCamelCase_     ,  **UpperCamelCase_					)

						# get the vision config dict if we are loading from InstructBlipConfig
						if config_dict.get('model_type'					) == "instructblip":
							_lowercase					:       Any       =		config_dict['vision_config']

						if "model_type" in config_dict and hasattr(cls     ,  'model_type'					) and config_dict["model_type"] != cls.model_type:
							logger.warning(
							    F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
							    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''					)

						return cls.from_dict(UpperCamelCase_     ,  **UpperCamelCase_					)






class 		lowerCamelCase__      (						A     ):




					'''simple docstring'''



					A_						    =						"""instructblip_qformer"""




					def __init__(  self				:	Tuple     ,  UpperCamelCase_				:	Union[str, Any]=3_0522     ,  UpperCamelCase_				:	Union[str, Any]=768     ,  UpperCamelCase_				:	Tuple=12     ,  UpperCamelCase_				:	Optional[Any]=12     ,  UpperCamelCase_				:	List[str]=3072     ,  UpperCamelCase_				:	List[str]="gelu"     ,  UpperCamelCase_				:	Union[str, Any]=0.1     ,  UpperCamelCase_				:	List[str]=0.1     ,  UpperCamelCase_				:	Any=512     ,  UpperCamelCase_				:	Union[str, Any]=0.02     ,  UpperCamelCase_				:	List[Any]=1E-12     ,  UpperCamelCase_				:	Optional[Any]=0     ,  UpperCamelCase_				:	str="absolute"     ,  UpperCamelCase_				:	List[Any]=2     ,  UpperCamelCase_				:	Any=1408     ,  **UpperCamelCase_				:	Dict     ,  )  ->       Any:



						'''simple docstring'''





						super().__init__(pad_token_id=UpperCamelCase_     ,  **UpperCamelCase_					)

						_lowercase					:       Dict       =		vocab_size
						_lowercase					:       Optional[Any]       =		hidden_size
						_lowercase					:       Any       =		num_hidden_layers
						_lowercase					:       List[Any]       =		num_attention_heads
						_lowercase					:       Optional[int]       =		hidden_act
						_lowercase					:       Union[str, Any]       =		intermediate_size
						_lowercase					:       List[Any]       =		hidden_dropout_prob
						_lowercase					:       Dict       =		attention_probs_dropout_prob
						_lowercase					:       Any       =		max_position_embeddings
						_lowercase					:       Optional[int]       =		initializer_range
						_lowercase					:       Tuple       =		layer_norm_eps
						_lowercase					:       List[str]       =		position_embedding_type
						_lowercase					:       str       =		cross_attention_frequency
						_lowercase					:       int       =		encoder_hidden_size




					@classmethod
					def 						__UpperCAmelCase     (  cls				:	List[Any]     ,  UpperCamelCase_				:	Union[str, os.PathLike]     ,  **UpperCamelCase_				:	List[str]					)  ->       "PretrainedConfig":



						'''simple docstring'''





						cls._set_token_in_kwargs(UpperCamelCase_					)

						_lowercase  ,    _lowercase					:       List[str]       =		cls.get_config_dict(UpperCamelCase_     ,  **UpperCamelCase_					)

						# get the qformer config dict if we are loading from InstructBlipConfig
						if config_dict.get('model_type'					) == "instructblip":
							_lowercase					:       Optional[int]       =		config_dict['qformer_config']

						if "model_type" in config_dict and hasattr(cls     ,  'model_type'					) and config_dict["model_type"] != cls.model_type:
							logger.warning(
							    F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
							    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''					)

						return cls.from_dict(UpperCamelCase_     ,  **UpperCamelCase_					)






class 		lowerCamelCase__      (						A     ):




					'''simple docstring'''



					A_						    =						"""instructblip"""
					A_						    =						True




					def __init__(  self				:	Any     ,  UpperCamelCase_				:	List[Any]=None     ,  UpperCamelCase_				:	Tuple=None     ,  UpperCamelCase_				:	Dict=None     ,  UpperCamelCase_				:	Union[str, Any]=32     ,  **UpperCamelCase_				:	int					)  ->       List[str]:



						'''simple docstring'''





						super().__init__(**UpperCamelCase_					)

						if vision_config is None:
							_lowercase					:       Any       =		{}
							logger.info('vision_config is None. initializing the InstructBlipVisionConfig with default values.'					)

						if qformer_config is None:
							_lowercase					:       List[Any]       =		{}
							logger.info('qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.'					)

						if text_config is None:
							_lowercase					:       List[Any]       =		{}
							logger.info('text_config is None. Initializing the text config with default values (`OPTConfig`).'					)

						_lowercase					:       List[Any]       =		InstructBlipVisionConfig(**UpperCamelCase_					)
						_lowercase					:       Union[str, Any]       =		InstructBlipQFormerConfig(**UpperCamelCase_					)
						_lowercase					:       Union[str, Any]       =		text_config['model_type'] if 'model_type' in text_config else 'opt'
						_lowercase					:       int       =		CONFIG_MAPPING[text_model_type](**UpperCamelCase_					)

						_lowercase					:       str       =		self.text_config.tie_word_embeddings
						_lowercase					:       int       =		self.text_config.is_encoder_decoder

						_lowercase					:       Tuple       =		num_query_tokens
						_lowercase					:       str       =		self.vision_config.hidden_size
						_lowercase					:       Union[str, Any]       =		self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
						_lowercase					:       List[Any]       =		1.0
						_lowercase					:       int       =		0.02




					@classmethod
					def 						__UpperCAmelCase     (  cls				:	Tuple     ,  UpperCamelCase_				:	InstructBlipVisionConfig     ,  UpperCamelCase_				:	InstructBlipQFormerConfig     ,  UpperCamelCase_				:	PretrainedConfig     ,  **UpperCamelCase_				:	Dict     ,  )  ->       List[str]:



						'''simple docstring'''





						return cls(
						    vision_config=vision_config.to_dict()     ,  qformer_config=qformer_config.to_dict()     ,  text_config=text_config.to_dict()     ,  **UpperCamelCase_     ,  )




					def 						__UpperCAmelCase     (  self				:	Dict					)  ->       List[str]:



						'''simple docstring'''





						_lowercase					:       List[Any]       =		copy.deepcopy(self.__dict__					)
						_lowercase					:       Optional[int]       =		self.vision_config.to_dict()
						_lowercase					:       Optional[Any]       =		self.qformer_config.to_dict()
						_lowercase					:       Tuple       =		self.text_config.to_dict()
						_lowercase					:       Dict       =		self.__class__.model_type
						return output
