

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  ):
							"""simple docstring"""







							def 							__lowercase							(     self			,				_a			)       ->       str:
												for model_result in results.values():
																	for batch_size, sequence_length in zip(model_result['''bs''']			,				model_result['''ss''']			):
																						_a      :       Optional[Any]   =						model_result['''result'''][batch_size][sequence_length]
																						self.assertIsNotNone(_a			)


							def 							__lowercase							(     self			)       ->       Tuple:
												_a      :       Tuple   =						'''sshleifer/tiny-gpt2'''
												_a      :       Dict   =						PyTorchBenchmarkArguments(
												    models=[MODEL_ID]			,				training=_a			,				inference=_a			,				sequence_lengths=[8]			,				batch_sizes=[1]			,				multi_process=_a			,				)
												_a      :       int   =						PyTorchBenchmark(_a			)
												_a      :       str   =						benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result			)
												self.check_results_dict_not_empty(results.memory_inference_result			)


							def 							__lowercase							(     self			)       ->       str:
												_a      :       str   =						'''sgugger/tiny-distilbert-classification'''
												_a      :       str   =						PyTorchBenchmarkArguments(
												    models=[MODEL_ID]			,				training=_a			,				inference=_a			,				sequence_lengths=[8]			,				batch_sizes=[1]			,				multi_process=_a			,				only_pretrain_model=_a			,				)
												_a      :       Any   =						PyTorchBenchmark(_a			)
												_a      :       Dict   =						benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result			)
												self.check_results_dict_not_empty(results.memory_inference_result			)


							def 							__lowercase							(     self			)       ->       int:
												_a      :       Optional[Any]   =						'''sshleifer/tiny-gpt2'''
												_a      :       Optional[int]   =						PyTorchBenchmarkArguments(
												    models=[MODEL_ID]			,				training=_a			,				inference=_a			,				torchscript=_a			,				sequence_lengths=[8]			,				batch_sizes=[1]			,				multi_process=_a			,				)
												_a      :       int   =						PyTorchBenchmark(_a			)
												_a      :       Any   =						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 							__lowercase							(     self			)       ->       int:
												_a      :       Optional[int]   =						'''sshleifer/tiny-gpt2'''
												_a      :       Any   =						PyTorchBenchmarkArguments(
												    models=[MODEL_ID]			,				training=_a			,				inference=_a			,				fpaa=_a			,				sequence_lengths=[8]			,				batch_sizes=[1]			,				multi_process=_a			,				)
												_a      :       int   =						PyTorchBenchmark(_a			)
												_a      :       Any   =						benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result			)
												self.check_results_dict_not_empty(results.memory_inference_result			)


							def 							__lowercase							(     self			)       ->       List[Any]:
												_a      :       Union[str, Any]   =						'''sshleifer/tiny-gpt2'''
												_a      :       Dict   =						AutoConfig.from_pretrained(_a			)
												# set architectures equal to `None`
												_a      :       List[Any]   =						None
												_a      :       Tuple   =						PyTorchBenchmarkArguments(
												    models=[MODEL_ID]			,				training=_a			,				inference=_a			,				sequence_lengths=[8]			,				batch_sizes=[1]			,				multi_process=_a			,				)
												_a      :       List[Any]   =						PyTorchBenchmark(_a			,				configs=[config]			)
												_a      :       int   =						benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result			)
												self.check_results_dict_not_empty(results.memory_inference_result			)


							def 							__lowercase							(     self			)       ->       Dict:
												_a      :       Tuple   =						'''sshleifer/tiny-gpt2'''
												_a      :       int   =						PyTorchBenchmarkArguments(
												    models=[MODEL_ID]			,				training=_a			,				inference=_a			,				sequence_lengths=[8]			,				batch_sizes=[1]			,				multi_process=_a			,				)
												_a      :       Dict   =						PyTorchBenchmark(_a			)
												_a      :       str   =						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 							__lowercase							(     self			)       ->       Optional[Any]:
												_a      :       Any   =						'''sshleifer/tiny-gpt2'''
												_a      :       Tuple   =						PyTorchBenchmarkArguments(
												    models=[MODEL_ID]			,				training=_a			,				inference=_a			,				sequence_lengths=[8]			,				batch_sizes=[1]			,				fpaa=_a			,				multi_process=_a			,				)
												_a      :       Tuple   =						PyTorchBenchmark(_a			)
												_a      :       List[str]   =						benchmark.run()
												self.check_results_dict_not_empty(results.time_train_result			)
												self.check_results_dict_not_empty(results.memory_train_result			)


							def 							__lowercase							(     self			)       ->       Any:
												_a      :       int   =						'''sshleifer/tiny-gpt2'''
												_a      :       List[str]   =						AutoConfig.from_pretrained(_a			)
												_a      :       List[str]   =						PyTorchBenchmarkArguments(
												    models=[MODEL_ID]			,				training=_a			,				inference=_a			,				sequence_lengths=[8]			,				batch_sizes=[1]			,				multi_process=_a			,				)
												_a      :       Tuple   =						PyTorchBenchmark(_a			,				configs=[config]			)
												_a      :       List[str]   =						benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result			)
												self.check_results_dict_not_empty(results.memory_inference_result			)


							def 							__lowercase							(     self			)       ->       Union[str, Any]:
												_a      :       Union[str, Any]   =						'''sshleifer/tinier_bart'''
												_a      :       Optional[int]   =						AutoConfig.from_pretrained(_a			)
												_a      :       Optional[int]   =						PyTorchBenchmarkArguments(
												    models=[MODEL_ID]			,				training=_a			,				inference=_a			,				sequence_lengths=[8]			,				batch_sizes=[1]			,				multi_process=_a			,				)
												_a      :       List[str]   =						PyTorchBenchmark(_a			,				configs=[config]			)
												_a      :       Any   =						benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result			)
												self.check_results_dict_not_empty(results.memory_inference_result			)


							def 							__lowercase							(     self			)       ->       int:
												_a      :       Optional[int]   =						'''sshleifer/tiny-gpt2'''
												_a      :       int   =						AutoConfig.from_pretrained(_a			)
												_a      :       Union[str, Any]   =						PyTorchBenchmarkArguments(
												    models=[MODEL_ID]			,				training=_a			,				inference=_a			,				sequence_lengths=[8]			,				batch_sizes=[1]			,				multi_process=_a			,				)
												_a      :       Tuple   =						PyTorchBenchmark(_a			,				configs=[config]			)
												_a      :       str   =						benchmark.run()
												self.check_results_dict_not_empty(results.time_train_result			)
												self.check_results_dict_not_empty(results.memory_train_result			)


							def 							__lowercase							(     self			)       ->       List[Any]:
												_a      :       Dict   =						'''sshleifer/tinier_bart'''
												_a      :       Dict   =						AutoConfig.from_pretrained(_a			)
												_a      :       List[Any]   =						PyTorchBenchmarkArguments(
												    models=[MODEL_ID]			,				training=_a			,				inference=_a			,				sequence_lengths=[8]			,				batch_sizes=[1]			,				multi_process=_a			,				)
												_a      :       int   =						PyTorchBenchmark(_a			,				configs=[config]			)
												_a      :       Any   =						benchmark.run()
												self.check_results_dict_not_empty(results.time_train_result			)
												self.check_results_dict_not_empty(results.memory_train_result			)


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




							def 							__lowercase							(     self			)       ->       Optional[Any]:
												_a      :       Optional[int]   =						'''sshleifer/tiny-gpt2'''

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

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






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


a__												=						logging.get_logger(__name__)

a__												=						{
    '''funnel-transformer/small''': '''https://huggingface.co/funnel-transformer/small/resolve/main/config.json''',
    '''funnel-transformer/small-base''': '''https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json''',
    '''funnel-transformer/medium''': '''https://huggingface.co/funnel-transformer/medium/resolve/main/config.json''',
    '''funnel-transformer/medium-base''': '''https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json''',
    '''funnel-transformer/intermediate''': (
        '''https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json'''
    ),
    '''funnel-transformer/intermediate-base''': (
        '''https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json'''
    ),
    '''funnel-transformer/large''': '''https://huggingface.co/funnel-transformer/large/resolve/main/config.json''',
    '''funnel-transformer/large-base''': '''https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json''',
    '''funnel-transformer/xlarge''': '''https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json''',
    '''funnel-transformer/xlarge-base''': '''https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json''',
}

class 							UpperCAmelCase_						(     __lowercase  ):
							"""simple docstring"""







							UpperCAmelCase__			:		Optional[int]             =       "funnel"
							UpperCAmelCase__			:		Tuple             =       {
							    "hidden_size": "d_model",
							    "num_attention_heads": "n_head",
							}


							def __init__(     self			,				_a=3_0_5_2_2			,				_a=[4, 4, 4]			,				_a=None			,				_a=2			,				_a=7_6_8			,				_a=1_2			,				_a=6_4			,				_a=3_0_7_2			,				_a="gelu_new"			,				_a=0.1			,				_a=0.1			,				_a=0.0			,				_a=0.1			,				_a=None			,				_a=1e-9			,				_a="mean"			,				_a="relative_shift"			,				_a=True			,				_a=True			,				_a=True			,				**_a			,				)       ->       List[Any]:
												_a      :       Optional[int]   =						vocab_size
												_a      :       Dict   =						block_sizes
												_a      :       Optional[int]   =						[1] * len(_a			) if block_repeats is None else block_repeats
												assert len(_a			) == len(
												    self.block_repeats			), "`block_sizes` and `block_repeats` should have the same length."
												_a      :       int   =						num_decoder_layers
												_a      :       List[str]   =						d_model
												_a      :       Optional[Any]   =						n_head
												_a      :       Tuple   =						d_head
												_a      :       Dict   =						d_inner
												_a      :       List[str]   =						hidden_act
												_a      :       int   =						hidden_dropout
												_a      :       Union[str, Any]   =						attention_dropout
												_a      :       Tuple   =						activation_dropout
												_a      :       Optional[Any]   =						initializer_range
												_a      :       Dict   =						initializer_std
												_a      :       Union[str, Any]   =						layer_norm_eps
												assert pooling_type in [
												    "mean",
												    "max",
												], F"""Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported."""
												_a      :       Any   =						pooling_type
												assert attention_type in [
												    "relative_shift",
												    "factorized",
												], F"""Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported."""
												_a      :       Optional[Any]   =						attention_type
												_a      :       int   =						separate_cls
												_a      :       Tuple   =						truncate_seq
												_a      :       List[Any]   =						pool_q_only

												super().__init__(**_a			)


							@property
							def 							__lowercase							(     self			)       ->       Tuple:
												return sum(self.block_sizes			)


							@num_hidden_layers.setter
							def 							__lowercase							(     self			,				_a			)       ->       List[str]:
												raise NotImplementedError(
												    '''This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.'''			)


							@property
							def 							__lowercase							(     self			)       ->       Optional[int]:
												return len(self.block_sizes			)




							@num_blocks.setter
							def 							__lowercase							(     self			,				_a			)       ->       Dict:
												raise NotImplementedError('''This model does not support the setting of `num_blocks`. Please set `block_sizes`.'''			)



