






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 						_a       (  unittest.TestCase    ):






  """simple docstring"""





  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase )			-> List[Any]:
     for model_result in results.values():
        for batch_size, sequence_length in zip(model_result['bs']		,		model_result['ss'] ):
           UpperCamelCase_									=     model_result['result'][batch_size][sequence_length]
           self.assertIsNotNone(_UpperCAmelCase )




  def    _UpperCAmelCase     (     self )			-> Tuple:
     UpperCamelCase_									=     'sshleifer/tiny-gpt2'
     UpperCamelCase_									=     PyTorchBenchmarkArguments(
         models=[MODEL_ID]		,		training=_UpperCAmelCase		,		inference=_UpperCAmelCase		,		sequence_lengths=[8]		,		batch_sizes=[1]		,		multi_process=_UpperCAmelCase		,		)
     UpperCamelCase_									=     PyTorchBenchmark(_UpperCAmelCase )
     UpperCamelCase_									=     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 )			-> List[str]:
     UpperCamelCase_									=     'sgugger/tiny-distilbert-classification'
     UpperCamelCase_									=     PyTorchBenchmarkArguments(
         models=[MODEL_ID]		,		training=_UpperCAmelCase		,		inference=_UpperCAmelCase		,		sequence_lengths=[8]		,		batch_sizes=[1]		,		multi_process=_UpperCAmelCase		,		only_pretrain_model=_UpperCAmelCase		,		)
     UpperCamelCase_									=     PyTorchBenchmark(_UpperCAmelCase )
     UpperCamelCase_									=     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 )			-> Optional[Any]:
     UpperCamelCase_									=     'sshleifer/tiny-gpt2'
     UpperCamelCase_									=     PyTorchBenchmarkArguments(
         models=[MODEL_ID]		,		training=_UpperCAmelCase		,		inference=_UpperCAmelCase		,		torchscript=_UpperCAmelCase		,		sequence_lengths=[8]		,		batch_sizes=[1]		,		multi_process=_UpperCAmelCase		,		)
     UpperCamelCase_									=     PyTorchBenchmark(_UpperCAmelCase )
     UpperCamelCase_									=     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 )			-> Dict:
     UpperCamelCase_									=     'sshleifer/tiny-gpt2'
     UpperCamelCase_									=     PyTorchBenchmarkArguments(
         models=[MODEL_ID]		,		training=_UpperCAmelCase		,		inference=_UpperCAmelCase		,		fpaa=_UpperCAmelCase		,		sequence_lengths=[8]		,		batch_sizes=[1]		,		multi_process=_UpperCAmelCase		,		)
     UpperCamelCase_									=     PyTorchBenchmark(_UpperCAmelCase )
     UpperCamelCase_									=     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 )			-> Tuple:
     UpperCamelCase_									=     'sshleifer/tiny-gpt2'
     UpperCamelCase_									=     AutoConfig.from_pretrained(_UpperCAmelCase )
     # set architectures equal to `None`
     UpperCamelCase_									=     None
     UpperCamelCase_									=     PyTorchBenchmarkArguments(
         models=[MODEL_ID]		,		training=_UpperCAmelCase		,		inference=_UpperCAmelCase		,		sequence_lengths=[8]		,		batch_sizes=[1]		,		multi_process=_UpperCAmelCase		,		)
     UpperCamelCase_									=     PyTorchBenchmark(_UpperCAmelCase		,		configs=[config] )
     UpperCamelCase_									=     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 )			-> List[str]:
     UpperCamelCase_									=     'sshleifer/tiny-gpt2'
     UpperCamelCase_									=     PyTorchBenchmarkArguments(
         models=[MODEL_ID]		,		training=_UpperCAmelCase		,		inference=_UpperCAmelCase		,		sequence_lengths=[8]		,		batch_sizes=[1]		,		multi_process=_UpperCAmelCase		,		)
     UpperCamelCase_									=     PyTorchBenchmark(_UpperCAmelCase )
     UpperCamelCase_									=     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 )			-> List[Any]:
     UpperCamelCase_									=     'sshleifer/tiny-gpt2'
     UpperCamelCase_									=     PyTorchBenchmarkArguments(
         models=[MODEL_ID]		,		training=_UpperCAmelCase		,		inference=_UpperCAmelCase		,		sequence_lengths=[8]		,		batch_sizes=[1]		,		fpaa=_UpperCAmelCase		,		multi_process=_UpperCAmelCase		,		)
     UpperCamelCase_									=     PyTorchBenchmark(_UpperCAmelCase )
     UpperCamelCase_									=     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 )			-> Tuple:
     UpperCamelCase_									=     'sshleifer/tiny-gpt2'
     UpperCamelCase_									=     AutoConfig.from_pretrained(_UpperCAmelCase )
     UpperCamelCase_									=     PyTorchBenchmarkArguments(
         models=[MODEL_ID]		,		training=_UpperCAmelCase		,		inference=_UpperCAmelCase		,		sequence_lengths=[8]		,		batch_sizes=[1]		,		multi_process=_UpperCAmelCase		,		)
     UpperCamelCase_									=     PyTorchBenchmark(_UpperCAmelCase		,		configs=[config] )
     UpperCamelCase_									=     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 )			-> Tuple:
     UpperCamelCase_									=     'sshleifer/tinier_bart'
     UpperCamelCase_									=     AutoConfig.from_pretrained(_UpperCAmelCase )
     UpperCamelCase_									=     PyTorchBenchmarkArguments(
         models=[MODEL_ID]		,		training=_UpperCAmelCase		,		inference=_UpperCAmelCase		,		sequence_lengths=[8]		,		batch_sizes=[1]		,		multi_process=_UpperCAmelCase		,		)
     UpperCamelCase_									=     PyTorchBenchmark(_UpperCAmelCase		,		configs=[config] )
     UpperCamelCase_									=     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 )			-> Optional[Any]:
     UpperCamelCase_									=     'sshleifer/tiny-gpt2'
     UpperCamelCase_									=     AutoConfig.from_pretrained(_UpperCAmelCase )
     UpperCamelCase_									=     PyTorchBenchmarkArguments(
         models=[MODEL_ID]		,		training=_UpperCAmelCase		,		inference=_UpperCAmelCase		,		sequence_lengths=[8]		,		batch_sizes=[1]		,		multi_process=_UpperCAmelCase		,		)
     UpperCamelCase_									=     PyTorchBenchmark(_UpperCAmelCase		,		configs=[config] )
     UpperCamelCase_									=     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 )			-> Optional[int]:
     UpperCamelCase_									=     'sshleifer/tinier_bart'
     UpperCamelCase_									=     AutoConfig.from_pretrained(_UpperCAmelCase )
     UpperCamelCase_									=     PyTorchBenchmarkArguments(
         models=[MODEL_ID]		,		training=_UpperCAmelCase		,		inference=_UpperCAmelCase		,		sequence_lengths=[8]		,		batch_sizes=[1]		,		multi_process=_UpperCAmelCase		,		)
     UpperCamelCase_									=     PyTorchBenchmark(_UpperCAmelCase		,		configs=[config] )
     UpperCamelCase_									=     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 )			-> Any:
     UpperCamelCase_									=     'sshleifer/tiny-gpt2'
     with tempfile.TemporaryDirectory() as tmp_dir:
        UpperCamelCase_									=     PyTorchBenchmarkArguments(
            models=[MODEL_ID]		,		training=_UpperCAmelCase		,		inference=_UpperCAmelCase		,		save_to_csv=_UpperCAmelCase		,		sequence_lengths=[8]		,		batch_sizes=[1]		,		inference_time_csv_file=os.path.join(_UpperCAmelCase		,		'inf_time.csv' )		,		train_memory_csv_file=os.path.join(_UpperCAmelCase		,		'train_mem.csv' )		,		inference_memory_csv_file=os.path.join(_UpperCAmelCase		,		'inf_mem.csv' )		,		train_time_csv_file=os.path.join(_UpperCAmelCase		,		'train_time.csv' )		,		env_info_csv_file=os.path.join(_UpperCAmelCase		,		'env.csv' )		,		multi_process=_UpperCAmelCase		,		)
        UpperCamelCase_									=     PyTorchBenchmark(_UpperCAmelCase )
        benchmark.run()
        self.assertTrue(Path(os.path.join(_UpperCAmelCase		,		'inf_time.csv' ) ).exists() )
        self.assertTrue(Path(os.path.join(_UpperCAmelCase		,		'train_time.csv' ) ).exists() )
        self.assertTrue(Path(os.path.join(_UpperCAmelCase		,		'inf_mem.csv' ) ).exists() )
        self.assertTrue(Path(os.path.join(_UpperCAmelCase		,		'train_mem.csv' ) ).exists() )
        self.assertTrue(Path(os.path.join(_UpperCAmelCase		,		'env.csv' ) ).exists() )




  def    _UpperCAmelCase     (     self )			-> List[Any]:
     UpperCamelCase_									=     'sshleifer/tiny-gpt2'

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

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













import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


snake_case__      :	Optional[int]       	=      logging.get_logger(__name__)

snake_case__      :	List[Any]       	=      {
    """microsoft/git-base""": """https://huggingface.co/microsoft/git-base/resolve/main/config.json""",
}


class 						_a       (  UpperCAmelCase__    ):






  """simple docstring"""





  A_						=   """git_vision_model"""




  def __init__(     self		,		_UpperCAmelCase=768		,		_UpperCAmelCase=3072		,		_UpperCAmelCase=12		,		_UpperCAmelCase=12		,		_UpperCAmelCase=3		,		_UpperCAmelCase=224		,		_UpperCAmelCase=16		,		_UpperCAmelCase="quick_gelu"		,		_UpperCAmelCase=1e-5		,		_UpperCAmelCase=0.0		,		_UpperCAmelCase=0.0_2		,		**_UpperCAmelCase		,		)			-> Optional[Any]:
     super().__init__(**_UpperCAmelCase )

     UpperCamelCase_									=     hidden_size
     UpperCamelCase_									=     intermediate_size
     UpperCamelCase_									=     num_hidden_layers
     UpperCamelCase_									=     num_attention_heads
     UpperCamelCase_									=     num_channels
     UpperCamelCase_									=     patch_size
     UpperCamelCase_									=     image_size
     UpperCamelCase_									=     initializer_range
     UpperCamelCase_									=     attention_dropout
     UpperCamelCase_									=     layer_norm_eps
     UpperCamelCase_									=     hidden_act




  @classmethod
  def    _UpperCAmelCase     (     cls		,		_UpperCAmelCase		,		**_UpperCAmelCase )			-> "PretrainedConfig":
     cls._set_token_in_kwargs(_UpperCAmelCase )

     UpperCamelCase_		,      UpperCamelCase_									=     cls.get_config_dict(_UpperCAmelCase		,		**_UpperCAmelCase )

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




class 						_a       (  UpperCAmelCase__    ):






  """simple docstring"""





  A_						=   """git"""




  def __init__(     self		,		_UpperCAmelCase=None		,		_UpperCAmelCase=30522		,		_UpperCAmelCase=768		,		_UpperCAmelCase=6		,		_UpperCAmelCase=12		,		_UpperCAmelCase=3072		,		_UpperCAmelCase="gelu"		,		_UpperCAmelCase=0.1		,		_UpperCAmelCase=0.1		,		_UpperCAmelCase=1024		,		_UpperCAmelCase=0.0_2		,		_UpperCAmelCase=1e-12		,		_UpperCAmelCase=0		,		_UpperCAmelCase="absolute"		,		_UpperCAmelCase=True		,		_UpperCAmelCase=False		,		_UpperCAmelCase=101		,		_UpperCAmelCase=102		,		_UpperCAmelCase=None		,		**_UpperCAmelCase		,		)			-> Optional[int]:
     super().__init__(bos_token_id=_UpperCAmelCase		,		eos_token_id=_UpperCAmelCase		,		pad_token_id=_UpperCAmelCase		,		**_UpperCAmelCase )

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

     UpperCamelCase_									=     GitVisionConfig(**_UpperCAmelCase )
     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_									=     use_cache
     UpperCamelCase_									=     tie_word_embeddings
     UpperCamelCase_									=     num_image_with_embedding

     UpperCamelCase_									=     bos_token_id
     UpperCamelCase_									=     eos_token_id




  def    _UpperCAmelCase     (     self )			-> str:
     UpperCamelCase_									=     copy.deepcopy(self.__dict__ )
     UpperCamelCase_									=     self.vision_config.to_dict()
     UpperCamelCase_									=     self.__class__.model_type
     return output





