


import copy
import os
from typing import Union

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


lowerCAmelCase__    :Tuple       =				logging.get_logger(__name__)

lowerCAmelCase__    :Dict       =				{
    '''BAAI/AltCLIP''': '''https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json''',
    # See all AltCLIP models at https://huggingface.co/models?filter=altclip
}

class 						__a (							UpperCAmelCase     ):
		_a      :    int           =      'altclip_text_model'
		def __init__(     self     ,       _SCREAMING_SNAKE_CASE=250002     ,       _SCREAMING_SNAKE_CASE=1024     ,       _SCREAMING_SNAKE_CASE=24     ,       _SCREAMING_SNAKE_CASE=16     ,       _SCREAMING_SNAKE_CASE=4096     ,       _SCREAMING_SNAKE_CASE="gelu"     ,       _SCREAMING_SNAKE_CASE=0.1     ,       _SCREAMING_SNAKE_CASE=0.1     ,       _SCREAMING_SNAKE_CASE=514     ,       _SCREAMING_SNAKE_CASE=1     ,       _SCREAMING_SNAKE_CASE=0.02     ,       _SCREAMING_SNAKE_CASE=0.02     ,       _SCREAMING_SNAKE_CASE=1e-0_5     ,       _SCREAMING_SNAKE_CASE=1     ,       _SCREAMING_SNAKE_CASE=0     ,       _SCREAMING_SNAKE_CASE=2     ,       _SCREAMING_SNAKE_CASE="absolute"     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=768     ,       **_SCREAMING_SNAKE_CASE     ,       )					->			List[Any]:




									"""simple docstring"""





									super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE     ,       bos_token_id=_SCREAMING_SNAKE_CASE     ,       eos_token_id=_SCREAMING_SNAKE_CASE     ,       **_SCREAMING_SNAKE_CASE		)

									_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											=     type_vocab_size
									_UpperCAmelCase											=     initializer_range
									_UpperCAmelCase											=     initializer_factor
									_UpperCAmelCase											=     layer_norm_eps
									_UpperCAmelCase											=     position_embedding_type
									_UpperCAmelCase											=     use_cache
									_UpperCAmelCase											=     project_dim



class 						__a (							UpperCAmelCase     ):
		_a      :    List[str]           =      'altclip_vision_model'
		def __init__(     self     ,       _SCREAMING_SNAKE_CASE=768     ,       _SCREAMING_SNAKE_CASE=3072     ,       _SCREAMING_SNAKE_CASE=512     ,       _SCREAMING_SNAKE_CASE=12     ,       _SCREAMING_SNAKE_CASE=12     ,       _SCREAMING_SNAKE_CASE=3     ,       _SCREAMING_SNAKE_CASE=224     ,       _SCREAMING_SNAKE_CASE=32     ,       _SCREAMING_SNAKE_CASE="quick_gelu"     ,       _SCREAMING_SNAKE_CASE=1e-5     ,       _SCREAMING_SNAKE_CASE=0.0     ,       _SCREAMING_SNAKE_CASE=0.02     ,       _SCREAMING_SNAKE_CASE=1.0     ,       **_SCREAMING_SNAKE_CASE     ,       )					->			Tuple:




									"""simple docstring"""





									super().__init__(**_SCREAMING_SNAKE_CASE		)

									_UpperCAmelCase											=     hidden_size
									_UpperCAmelCase											=     intermediate_size
									_UpperCAmelCase											=     projection_dim
									_UpperCAmelCase											=     num_hidden_layers
									_UpperCAmelCase											=     num_attention_heads
									_UpperCAmelCase											=     num_channels
									_UpperCAmelCase											=     patch_size
									_UpperCAmelCase											=     image_size
									_UpperCAmelCase											=     initializer_range
									_UpperCAmelCase											=     initializer_factor
									_UpperCAmelCase											=     attention_dropout
									_UpperCAmelCase											=     layer_norm_eps
									_UpperCAmelCase											=     hidden_act






		@classmethod
		def       UpperCAmelCase__							(     cls     ,       _SCREAMING_SNAKE_CASE     ,       **_SCREAMING_SNAKE_CASE		)					->			"PretrainedConfig":




									"""simple docstring"""





									cls._set_token_in_kwargs(_SCREAMING_SNAKE_CASE		)

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

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



class 						__a (							UpperCAmelCase     ):
		_a      :    List[Any]           =      'altclip'
		_a      :    Optional[Any]           =      True
		def __init__(     self     ,       _SCREAMING_SNAKE_CASE=None     ,       _SCREAMING_SNAKE_CASE=None     ,       _SCREAMING_SNAKE_CASE=768     ,       _SCREAMING_SNAKE_CASE=2.6592     ,       **_SCREAMING_SNAKE_CASE		)					->			Dict:




									"""simple docstring"""





									_UpperCAmelCase											=     kwargs.pop('text_config_dict'     ,       _SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     kwargs.pop('vision_config_dict'     ,       _SCREAMING_SNAKE_CASE		)

									super().__init__(**_SCREAMING_SNAKE_CASE		)

									# Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in
									# `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most
									# cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`.
									if text_config_dict is not None:
																if text_config is None:
																							_UpperCAmelCase											=     {}

																# This is the complete result when using `text_config_dict`.
																_UpperCAmelCase											=     AltCLIPTextConfig(**_SCREAMING_SNAKE_CASE		).to_dict()

																# Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different.
																for key, value in _text_config_dict.items():
																							if key in text_config and value != text_config[key] and key not in ["transformers_version"]:
																														# If specified in `text_config_dict`
																														if key in text_config_dict:
																																					_UpperCAmelCase											=     (
																																					    f'''`{key}` is found in both `text_config_dict` and `text_config` but with different values. '''
																																					    f'''The value `text_config_dict["{key}"]` will be used instead.'''
																																					)
																														# If inferred from default argument values (just to be super careful)
																														else:
																																					_UpperCAmelCase											=     (
																																					    f'''`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The '''
																																					    f'''value `text_config["{key}"]` will be overriden.'''
																																					)
																														logger.warning(_SCREAMING_SNAKE_CASE		)

            # Update all values in `text_config` with the ones in `_text_config_dict`.
																text_config.update(_text_config_dict		)

									if vision_config_dict is not None:
																if vision_config is None:
																							_UpperCAmelCase											=     {}

																# This is the complete result when using `vision_config_dict`.
																_UpperCAmelCase											=     AltCLIPVisionConfig(**_SCREAMING_SNAKE_CASE		).to_dict()
																# convert keys to string instead of integer
																if "id2label" in _vision_config_dict:
																							_UpperCAmelCase											=     {
																							    str(_SCREAMING_SNAKE_CASE		): value for key, value in _vision_config_dict['id2label'].items()
																							}

																# Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different.
																for key, value in _vision_config_dict.items():
																							if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]:
																														# If specified in `vision_config_dict`
																														if key in vision_config_dict:
																																					_UpperCAmelCase											=     (
																																					    f'''`{key}` is found in both `vision_config_dict` and `vision_config` but with different '''
																																					    f'''values. The value `vision_config_dict["{key}"]` will be used instead.'''
																																					)
																														# If inferred from default argument values (just to be super careful)
																														else:
																																					_UpperCAmelCase											=     (
																																					    f'''`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. '''
																																					    f'''The value `vision_config["{key}"]` will be overriden.'''
																																					)
																														logger.warning(_SCREAMING_SNAKE_CASE		)

            # Update all values in `vision_config` with the ones in `_vision_config_dict`.
																vision_config.update(_vision_config_dict		)

									if text_config is None:
																_UpperCAmelCase											=     {}
																logger.info('`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values.'		)

									if vision_config is None:
																_UpperCAmelCase											=     {}
																logger.info('`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values.'		)

									_UpperCAmelCase											=     AltCLIPTextConfig(**_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     AltCLIPVisionConfig(**_SCREAMING_SNAKE_CASE		)

									_UpperCAmelCase											=     projection_dim
									_UpperCAmelCase											=     logit_scale_init_value
									_UpperCAmelCase											=     1.0
		@classmethod
		def       UpperCAmelCase__							(     cls     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       **_SCREAMING_SNAKE_CASE		)					->			List[str]:




									"""simple docstring"""





									return cls(text_config=text_config.to_dict()     ,       vision_config=vision_config.to_dict()     ,       **_SCREAMING_SNAKE_CASE		)






		def       UpperCAmelCase__							(     self		)					->			Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase											=     copy.deepcopy(self.__dict__		)
									_UpperCAmelCase											=     self.text_config.to_dict()
									_UpperCAmelCase											=     self.vision_config.to_dict()
									_UpperCAmelCase											=     self.__class__.model_type
									return output




import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
		import tensorflow as tf

		from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments

@require_tf
class 						__a (							unittest.TestCase     ):
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE		)					->			List[str]:




									"""simple docstring"""





									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(_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       eager_mode=_SCREAMING_SNAKE_CASE     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
									_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		)					->			int:




									"""simple docstring"""





									_UpperCAmelCase											=     'sgugger/tiny-distilbert-classification'
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       only_pretrain_model=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
									_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[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
									_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[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       eager_mode=_SCREAMING_SNAKE_CASE     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE     ,       [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[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE     ,       [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[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
									_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		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE     ,       [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:




									"""simple docstring"""





									_UpperCAmelCase											=     'patrickvonplaten/t5-tiny-random'
									_UpperCAmelCase											=     AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE     ,       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		)
		@unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices('GPU'		)		) == 0     ,       'Cannot do xla on CPU.'		)
		def       UpperCAmelCase__							(     self		)					->			int:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       use_xla=_SCREAMING_SNAKE_CASE     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
									_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		)					->			str:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									with tempfile.TemporaryDirectory() as tmp_dir:
																_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
																    models=[MODEL_ID]     ,       inference=_SCREAMING_SNAKE_CASE     ,       save_to_csv=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       inference_time_csv_file=os.path.join(_SCREAMING_SNAKE_CASE     ,       'inf_time.csv'		)     ,       inference_memory_csv_file=os.path.join(_SCREAMING_SNAKE_CASE     ,       'inf_mem.csv'		)     ,       env_info_csv_file=os.path.join(_SCREAMING_SNAKE_CASE     ,       'env.csv'		)     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
																_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
																benchmark.run()
																self.assertTrue(Path(os.path.join(_SCREAMING_SNAKE_CASE     ,       'inf_time.csv'		)		).exists()		)
																self.assertTrue(Path(os.path.join(_SCREAMING_SNAKE_CASE     ,       'inf_mem.csv'		)		).exists()		)
																self.assertTrue(Path(os.path.join(_SCREAMING_SNAKE_CASE     ,       'env.csv'		)		).exists()		)






		def       UpperCAmelCase__							(     self		)					->			Tuple:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'

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

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