
"""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								=						logging.get_logger(__name__)

_a								=						{
    """Salesforce/instruct-blip-flan-t5""": """https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json""",
}






class        _UpperCAmelCase(       lowerCamelCase      ):
		lowercase__         = 'instructblip_vision_model'



		def __init__(   self							,						__a=14_08							,						__a=61_44							,						__a=39							,						__a=16							,						__a=2_24							,						__a=14							,						__a="gelu"							,						__a=1e-6							,						__a=0.0							,						__a=1e-10							,						__a=True							,						**__a							,						)			->			int:






							'''simple docstring'''
							super().__init__(**__a)

							_UpperCamelCase									=    hidden_size
							_UpperCamelCase									=    intermediate_size
							_UpperCamelCase									=    num_hidden_layers
							_UpperCamelCase									=    num_attention_heads
							_UpperCamelCase									=    patch_size
							_UpperCamelCase									=    image_size
							_UpperCamelCase									=    initializer_range
							_UpperCamelCase									=    attention_dropout
							_UpperCamelCase									=    layer_norm_eps
							_UpperCamelCase									=    hidden_act
							_UpperCamelCase									=    qkv_bias




		@classmethod
		def 		UpperCAmelCase				(   cls							,						__a							,						**__a)			->			"PretrainedConfig":






							'''simple docstring'''
							cls._set_token_in_kwargs(__a)

							_UpperCamelCase					,    _UpperCamelCase									=    cls.get_config_dict(__a							,						**__a)

							# get the vision config dict if we are loading from InstructBlipConfig
							if config_dict.get('''model_type''') == "instructblip":
												_UpperCamelCase									=    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(__a							,						**__a)






class        _UpperCAmelCase(       lowerCamelCase      ):
		lowercase__         = 'instructblip_qformer'



		def __init__(   self							,						__a=3_05_22							,						__a=7_68							,						__a=12							,						__a=12							,						__a=30_72							,						__a="gelu"							,						__a=0.1							,						__a=0.1							,						__a=5_12							,						__a=0.02							,						__a=1e-12							,						__a=0							,						__a="absolute"							,						__a=2							,						__a=14_08							,						**__a							,						)			->			Dict:






							'''simple docstring'''
							super().__init__(pad_token_id=__a							,						**__a)

							_UpperCamelCase									=    vocab_size
							_UpperCamelCase									=    hidden_size
							_UpperCamelCase									=    num_hidden_layers
							_UpperCamelCase									=    num_attention_heads
							_UpperCamelCase									=    hidden_act
							_UpperCamelCase									=    intermediate_size
							_UpperCamelCase									=    hidden_dropout_prob
							_UpperCamelCase									=    attention_probs_dropout_prob
							_UpperCamelCase									=    max_position_embeddings
							_UpperCamelCase									=    initializer_range
							_UpperCamelCase									=    layer_norm_eps
							_UpperCamelCase									=    position_embedding_type
							_UpperCamelCase									=    cross_attention_frequency
							_UpperCamelCase									=    encoder_hidden_size




		@classmethod
		def 		UpperCAmelCase				(   cls							,						__a							,						**__a)			->			"PretrainedConfig":






							'''simple docstring'''
							cls._set_token_in_kwargs(__a)

							_UpperCamelCase					,    _UpperCamelCase									=    cls.get_config_dict(__a							,						**__a)

							# get the qformer config dict if we are loading from InstructBlipConfig
							if config_dict.get('''model_type''') == "instructblip":
												_UpperCamelCase									=    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(__a							,						**__a)






class        _UpperCAmelCase(       lowerCamelCase      ):
		lowercase__         = 'instructblip'
		lowercase__         = True



		def __init__(   self							,						__a=None							,						__a=None							,						__a=None							,						__a=32							,						**__a)			->			List[str]:






							'''simple docstring'''
							super().__init__(**__a)

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

							if qformer_config is None:
												_UpperCamelCase									=    {}
												logger.info('''qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.''')

							if text_config is None:
												_UpperCamelCase									=    {}
												logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).''')

							_UpperCamelCase									=    InstructBlipVisionConfig(**__a)
							_UpperCamelCase									=    InstructBlipQFormerConfig(**__a)
							_UpperCamelCase									=    text_config['''model_type'''] if '''model_type''' in text_config else '''opt'''
							_UpperCamelCase									=    CONFIG_MAPPING[text_model_type](**__a)

							_UpperCamelCase									=    self.text_config.tie_word_embeddings
							_UpperCamelCase									=    self.text_config.is_encoder_decoder

							_UpperCamelCase									=    num_query_tokens
							_UpperCamelCase									=    self.vision_config.hidden_size
							_UpperCamelCase									=    self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
							_UpperCamelCase									=    1.0
							_UpperCamelCase									=    0.02



		@classmethod
		def 		UpperCAmelCase				(   cls							,						__a							,						__a							,						__a							,						**__a							,						)			->			int:






							'''simple docstring'''
							return cls(
							    vision_config=vision_config.to_dict()							,						qformer_config=qformer_config.to_dict()							,						text_config=text_config.to_dict()							,						**__a							,						)




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






							'''simple docstring'''
							_UpperCamelCase									=    copy.deepcopy(self.__dict__)
							_UpperCamelCase									=    self.vision_config.to_dict()
							_UpperCamelCase									=    self.qformer_config.to_dict()
							_UpperCamelCase									=    self.text_config.to_dict()
							_UpperCamelCase									=    self.__class__.model_type
							return output







'''simple docstring'''

import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_torch_available
from transformers.testing_utils import require_torch, torch_device


if is_torch_available():
 from transformers import PyTorchBenchmark, PyTorchBenchmarkArguments


@require_torch
class     __UpperCAmelCase       ( unittest.TestCase		):





  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase ):
     for model_result in results.values():
        for batch_size, sequence_length in zip(model_result['''bs''']    ,     model_result['''ss'''] ):
           lowerCAmelCase_			=      model_result['''result'''][batch_size][sequence_length]
           self.assertIsNotNone(_lowerCamelCase )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      '''sshleifer/tiny-gpt2'''
     lowerCAmelCase_			=      PyTorchBenchmarkArguments(
         models=[MODEL_ID]    ,     training=_lowerCamelCase    ,     inference=_lowerCamelCase    ,     sequence_lengths=[8]    ,     batch_sizes=[1]    ,     multi_process=_lowerCamelCase    ,     )
     lowerCAmelCase_			=      PyTorchBenchmark(_lowerCamelCase )
     lowerCAmelCase_			=      benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result )
     self.check_results_dict_not_empty(results.memory_inference_result )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      '''sgugger/tiny-distilbert-classification'''
     lowerCAmelCase_			=      PyTorchBenchmarkArguments(
         models=[MODEL_ID]    ,     training=_lowerCamelCase    ,     inference=_lowerCamelCase    ,     sequence_lengths=[8]    ,     batch_sizes=[1]    ,     multi_process=_lowerCamelCase    ,     only_pretrain_model=_lowerCamelCase    ,     )
     lowerCAmelCase_			=      PyTorchBenchmark(_lowerCamelCase )
     lowerCAmelCase_			=      benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result )
     self.check_results_dict_not_empty(results.memory_inference_result )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      '''sshleifer/tiny-gpt2'''
     lowerCAmelCase_			=      PyTorchBenchmarkArguments(
         models=[MODEL_ID]    ,     training=_lowerCamelCase    ,     inference=_lowerCamelCase    ,     torchscript=_lowerCamelCase    ,     sequence_lengths=[8]    ,     batch_sizes=[1]    ,     multi_process=_lowerCamelCase    ,     )
     lowerCAmelCase_			=      PyTorchBenchmark(_lowerCamelCase )
     lowerCAmelCase_			=      benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result )
     self.check_results_dict_not_empty(results.memory_inference_result )





  @unittest.skipIf(torch_device == '''cpu'''    ,     '''Cant do half precision''' )
  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      '''sshleifer/tiny-gpt2'''
     lowerCAmelCase_			=      PyTorchBenchmarkArguments(
         models=[MODEL_ID]    ,     training=_lowerCamelCase    ,     inference=_lowerCamelCase    ,     fpaa=_lowerCamelCase    ,     sequence_lengths=[8]    ,     batch_sizes=[1]    ,     multi_process=_lowerCamelCase    ,     )
     lowerCAmelCase_			=      PyTorchBenchmark(_lowerCamelCase )
     lowerCAmelCase_			=      benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result )
     self.check_results_dict_not_empty(results.memory_inference_result )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      '''sshleifer/tiny-gpt2'''
     lowerCAmelCase_			=      AutoConfig.from_pretrained(_lowerCamelCase )
     # set architectures equal to `None`
     lowerCAmelCase_			=      None
     lowerCAmelCase_			=      PyTorchBenchmarkArguments(
         models=[MODEL_ID]    ,     training=_lowerCamelCase    ,     inference=_lowerCamelCase    ,     sequence_lengths=[8]    ,     batch_sizes=[1]    ,     multi_process=_lowerCamelCase    ,     )
     lowerCAmelCase_			=      PyTorchBenchmark(_lowerCamelCase    ,     configs=[config] )
     lowerCAmelCase_			=      benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result )
     self.check_results_dict_not_empty(results.memory_inference_result )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      '''sshleifer/tiny-gpt2'''
     lowerCAmelCase_			=      PyTorchBenchmarkArguments(
         models=[MODEL_ID]    ,     training=_lowerCamelCase    ,     inference=_lowerCamelCase    ,     sequence_lengths=[8]    ,     batch_sizes=[1]    ,     multi_process=_lowerCamelCase    ,     )
     lowerCAmelCase_			=      PyTorchBenchmark(_lowerCamelCase )
     lowerCAmelCase_			=      benchmark.run()
     self.check_results_dict_not_empty(results.time_train_result )
     self.check_results_dict_not_empty(results.memory_train_result )





  @unittest.skipIf(torch_device == '''cpu'''    ,     '''Can\'t do half precision''' )
  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      '''sshleifer/tiny-gpt2'''
     lowerCAmelCase_			=      PyTorchBenchmarkArguments(
         models=[MODEL_ID]    ,     training=_lowerCamelCase    ,     inference=_lowerCamelCase    ,     sequence_lengths=[8]    ,     batch_sizes=[1]    ,     fpaa=_lowerCamelCase    ,     multi_process=_lowerCamelCase    ,     )
     lowerCAmelCase_			=      PyTorchBenchmark(_lowerCamelCase )
     lowerCAmelCase_			=      benchmark.run()
     self.check_results_dict_not_empty(results.time_train_result )
     self.check_results_dict_not_empty(results.memory_train_result )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      '''sshleifer/tiny-gpt2'''
     lowerCAmelCase_			=      AutoConfig.from_pretrained(_lowerCamelCase )
     lowerCAmelCase_			=      PyTorchBenchmarkArguments(
         models=[MODEL_ID]    ,     training=_lowerCamelCase    ,     inference=_lowerCamelCase    ,     sequence_lengths=[8]    ,     batch_sizes=[1]    ,     multi_process=_lowerCamelCase    ,     )
     lowerCAmelCase_			=      PyTorchBenchmark(_lowerCamelCase    ,     configs=[config] )
     lowerCAmelCase_			=      benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result )
     self.check_results_dict_not_empty(results.memory_inference_result )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      '''sshleifer/tinier_bart'''
     lowerCAmelCase_			=      AutoConfig.from_pretrained(_lowerCamelCase )
     lowerCAmelCase_			=      PyTorchBenchmarkArguments(
         models=[MODEL_ID]    ,     training=_lowerCamelCase    ,     inference=_lowerCamelCase    ,     sequence_lengths=[8]    ,     batch_sizes=[1]    ,     multi_process=_lowerCamelCase    ,     )
     lowerCAmelCase_			=      PyTorchBenchmark(_lowerCamelCase    ,     configs=[config] )
     lowerCAmelCase_			=      benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result )
     self.check_results_dict_not_empty(results.memory_inference_result )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      '''sshleifer/tiny-gpt2'''
     lowerCAmelCase_			=      AutoConfig.from_pretrained(_lowerCamelCase )
     lowerCAmelCase_			=      PyTorchBenchmarkArguments(
         models=[MODEL_ID]    ,     training=_lowerCamelCase    ,     inference=_lowerCamelCase    ,     sequence_lengths=[8]    ,     batch_sizes=[1]    ,     multi_process=_lowerCamelCase    ,     )
     lowerCAmelCase_			=      PyTorchBenchmark(_lowerCamelCase    ,     configs=[config] )
     lowerCAmelCase_			=      benchmark.run()
     self.check_results_dict_not_empty(results.time_train_result )
     self.check_results_dict_not_empty(results.memory_train_result )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      '''sshleifer/tinier_bart'''
     lowerCAmelCase_			=      AutoConfig.from_pretrained(_lowerCamelCase )
     lowerCAmelCase_			=      PyTorchBenchmarkArguments(
         models=[MODEL_ID]    ,     training=_lowerCamelCase    ,     inference=_lowerCamelCase    ,     sequence_lengths=[8]    ,     batch_sizes=[1]    ,     multi_process=_lowerCamelCase    ,     )
     lowerCAmelCase_			=      PyTorchBenchmark(_lowerCamelCase    ,     configs=[config] )
     lowerCAmelCase_			=      benchmark.run()
     self.check_results_dict_not_empty(results.time_train_result )
     self.check_results_dict_not_empty(results.memory_train_result )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      '''sshleifer/tiny-gpt2'''
     with tempfile.TemporaryDirectory() as tmp_dir:
        lowerCAmelCase_			=      PyTorchBenchmarkArguments(
            models=[MODEL_ID]    ,     training=_lowerCamelCase    ,     inference=_lowerCamelCase    ,     save_to_csv=_lowerCamelCase    ,     sequence_lengths=[8]    ,     batch_sizes=[1]    ,     inference_time_csv_file=os.path.join(_lowerCamelCase    ,     '''inf_time.csv''' )    ,     train_memory_csv_file=os.path.join(_lowerCamelCase    ,     '''train_mem.csv''' )    ,     inference_memory_csv_file=os.path.join(_lowerCamelCase    ,     '''inf_mem.csv''' )    ,     train_time_csv_file=os.path.join(_lowerCamelCase    ,     '''train_time.csv''' )    ,     env_info_csv_file=os.path.join(_lowerCamelCase    ,     '''env.csv''' )    ,     multi_process=_lowerCamelCase    ,     )
        lowerCAmelCase_			=      PyTorchBenchmark(_lowerCamelCase )
        benchmark.run()
        self.assertTrue(Path(os.path.join(_lowerCamelCase    ,     '''inf_time.csv''' ) ).exists() )
        self.assertTrue(Path(os.path.join(_lowerCamelCase    ,     '''train_time.csv''' ) ).exists() )
        self.assertTrue(Path(os.path.join(_lowerCamelCase    ,     '''inf_mem.csv''' ) ).exists() )
        self.assertTrue(Path(os.path.join(_lowerCamelCase    ,     '''train_mem.csv''' ) ).exists() )
        self.assertTrue(Path(os.path.join(_lowerCamelCase    ,     '''env.csv''' ) ).exists() )







  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      '''sshleifer/tiny-gpt2'''

     def _check_summary_is_not_empty(_lowerCamelCase ):
        self.assertTrue(hasattr(_lowerCamelCase    ,     '''sequential''' ) )
        self.assertTrue(hasattr(_lowerCamelCase    ,     '''cumulative''' ) )
        self.assertTrue(hasattr(_lowerCamelCase    ,     '''current''' ) )
        self.assertTrue(hasattr(_lowerCamelCase    ,     '''total''' ) )

     with tempfile.TemporaryDirectory() as tmp_dir:
        lowerCAmelCase_			=      PyTorchBenchmarkArguments(
            models=[MODEL_ID]    ,     training=_lowerCamelCase    ,     inference=_lowerCamelCase    ,     sequence_lengths=[8]    ,     batch_sizes=[1]    ,     log_filename=os.path.join(_lowerCamelCase    ,     '''log.txt''' )    ,     log_print=_lowerCamelCase    ,     trace_memory_line_by_line=_lowerCamelCase    ,     multi_process=_lowerCamelCase    ,     )
        lowerCAmelCase_			=      PyTorchBenchmark(_lowerCamelCase )
        lowerCAmelCase_			=      benchmark.run()
        _check_summary_is_not_empty(result.inference_summary )
        _check_summary_is_not_empty(result.train_summary )
        self.assertTrue(Path(os.path.join(_lowerCamelCase    ,     '''log.txt''' ) ).exists() )





