from collections import OrderedDict
from typing import Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...feature_extraction_utils import FeatureExtractionMixin
from ...onnx import OnnxConfig
from ...onnx.utils import compute_effective_axis_dimension
from ...tokenization_utils_base import PreTrainedTokenizerBase
from ...utils import TensorType, logging


lowerCamelCase_    :		Optional[Any]							=	logging.get_logger(__name__)

lowerCamelCase_    :		int							=	{
    """deepmind/language-perceiver""": """https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json""",
    # See all Perceiver models at https://huggingface.co/models?filter=perceiver
}


class 						a__							(       __snake_case		):
			A__    :       Dict        =      'perceiver'



			def __init__(						self				,						UpperCAmelCase=2_5_6				,						UpperCAmelCase=1_2_8_0				,						UpperCAmelCase=7_6_8				,						UpperCAmelCase=1				,						UpperCAmelCase=2_6				,						UpperCAmelCase=8				,						UpperCAmelCase=8				,						UpperCAmelCase=None				,						UpperCAmelCase=None				,						UpperCAmelCase="kv"				,						UpperCAmelCase=1				,						UpperCAmelCase=1				,						UpperCAmelCase="gelu"				,						UpperCAmelCase=0.1				,						UpperCAmelCase=0.02				,						UpperCAmelCase=1e-12				,						UpperCAmelCase=True				,						UpperCAmelCase=2_6_2				,						UpperCAmelCase=2_0_4_8				,						UpperCAmelCase=5_6				,						UpperCAmelCase=[3_6_8, 4_9_6]				,						UpperCAmelCase=1_6				,						UpperCAmelCase=1_9_2_0				,						UpperCAmelCase=1_6				,						UpperCAmelCase=[1, 1_6, 2_2_4, 2_2_4]				,						**UpperCAmelCase				,						)			->   Optional[int]:
								super().__init__(**UpperCAmelCase   )

								__a		 =       num_latents
								__a		 =       d_latents
								__a		 =       d_model
								__a		 =       num_blocks
								__a		 =       num_self_attends_per_block
								__a		 =       num_self_attention_heads
								__a		 =       num_cross_attention_heads
								__a		 =       qk_channels
								__a		 =       v_channels
								__a		 =       cross_attention_shape_for_attention
								__a		 =       self_attention_widening_factor
								__a		 =       cross_attention_widening_factor
								__a		 =       hidden_act
								__a		 =       attention_probs_dropout_prob
								__a		 =       initializer_range
								__a		 =       layer_norm_eps
								__a		 =       use_query_residual
								# masked language modeling attributes
								__a		 =       vocab_size
								__a		 =       max_position_embeddings
								# image classification attributes
								__a		 =       image_size
								# flow attributes
								__a		 =       train_size
								# multimodal autoencoding attributes
								__a		 =       num_frames
								__a		 =       audio_samples_per_frame
								__a		 =       samples_per_patch
								__a		 =       output_shape






class 						a__							(       __snake_case		):



			@property
			def __SCREAMING_SNAKE_CASE							(						self   )			->   Mapping[str, Mapping[int, str]]:
								if self.task == "multiple-choice":
													__a		 =       {0: 'batch', 1: 'choice', 2: 'sequence'}
								else:
													__a		 =       {0: 'batch', 1: 'sequence'}
								return OrderedDict(
								    [
								        ('inputs', dynamic_axis),
								        ('attention_mask', dynamic_axis),
								    ]   )



			@property
			def __SCREAMING_SNAKE_CASE							(						self   )			->   float:
								return 1e-4







			def __SCREAMING_SNAKE_CASE							(						self				,						UpperCAmelCase				,						UpperCAmelCase = -1				,						UpperCAmelCase = -1				,						UpperCAmelCase = -1				,						UpperCAmelCase = False				,						UpperCAmelCase = None				,						UpperCAmelCase = 3				,						UpperCAmelCase = 4_0				,						UpperCAmelCase = 4_0				,						)			->   Mapping[str, Any]:
								# copied from `transformers.onnx.config.OnnxConfig` and slightly altered/simplified

								if isinstance(UpperCAmelCase				,						UpperCAmelCase   ):
													# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
													__a		 =       compute_effective_axis_dimension(
													    UpperCAmelCase				,						fixed_dimension=OnnxConfig.default_fixed_batch				,						num_token_to_add=0   )
													# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
													__a		 =       preprocessor.num_special_tokens_to_add(UpperCAmelCase   )
													__a		 =       compute_effective_axis_dimension(
													    UpperCAmelCase				,						fixed_dimension=OnnxConfig.default_fixed_sequence				,						num_token_to_add=UpperCAmelCase   )
													# Generate dummy inputs according to compute batch and sequence
													__a		 =       [' '.join(['a']   ) * seq_length] * batch_size
													__a		 =       dict(preprocessor(UpperCAmelCase				,						return_tensors=UpperCAmelCase   )   )
													__a		 =       inputs.pop('input_ids'   )
													return inputs
								elif isinstance(UpperCAmelCase				,						UpperCAmelCase   ) and preprocessor.model_input_names[0] == "pixel_values":
													# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
													__a		 =       compute_effective_axis_dimension(UpperCAmelCase				,						fixed_dimension=OnnxConfig.default_fixed_batch   )
													__a		 =       self._generate_dummy_images(UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase   )
													__a		 =       dict(preprocessor(images=UpperCAmelCase				,						return_tensors=UpperCAmelCase   )   )
													__a		 =       inputs.pop('pixel_values'   )
													return inputs
								else:
													raise ValueError(
													    'Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor.'   )





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 __SCREAMING_SNAKE_CASE							(						self				,						UpperCAmelCase   )			->   Optional[Any]:
								for model_result in results.values():
													for batch_size, sequence_length in zip(model_result['bs']				,						model_result['ss']   ):
																		__a		 =       model_result['result'][batch_size][sequence_length]
																		self.assertIsNotNone(UpperCAmelCase   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[Any]:
								__a		 =       'sshleifer/tiny-gpt2'
								__a		 =       TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]				,						training=UpperCAmelCase				,						inference=UpperCAmelCase				,						sequence_lengths=[8]				,						batch_sizes=[1]				,						eager_mode=UpperCAmelCase				,						multi_process=UpperCAmelCase				,						)
								__a		 =       TensorFlowBenchmark(UpperCAmelCase   )
								__a		 =       benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result   )
								self.check_results_dict_not_empty(results.memory_inference_result   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[int]:
								__a		 =       'sgugger/tiny-distilbert-classification'
								__a		 =       TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]				,						training=UpperCAmelCase				,						inference=UpperCAmelCase				,						sequence_lengths=[8]				,						batch_sizes=[1]				,						multi_process=UpperCAmelCase				,						only_pretrain_model=UpperCAmelCase				,						)
								__a		 =       TensorFlowBenchmark(UpperCAmelCase   )
								__a		 =       benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result   )
								self.check_results_dict_not_empty(results.memory_inference_result   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Any:
								__a		 =       'sshleifer/tiny-gpt2'
								__a		 =       TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]				,						training=UpperCAmelCase				,						inference=UpperCAmelCase				,						sequence_lengths=[8]				,						batch_sizes=[1]				,						multi_process=UpperCAmelCase				,						)
								__a		 =       TensorFlowBenchmark(UpperCAmelCase   )
								__a		 =       benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result   )
								self.check_results_dict_not_empty(results.memory_inference_result   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[int]:
								__a		 =       'sshleifer/tiny-gpt2'
								__a		 =       AutoConfig.from_pretrained(UpperCAmelCase   )
								__a		 =       TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]				,						training=UpperCAmelCase				,						inference=UpperCAmelCase				,						sequence_lengths=[8]				,						batch_sizes=[1]				,						eager_mode=UpperCAmelCase				,						multi_process=UpperCAmelCase				,						)
								__a		 =       TensorFlowBenchmark(UpperCAmelCase				,						[config]   )
								__a		 =       benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result   )
								self.check_results_dict_not_empty(results.memory_inference_result   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Any:
								__a		 =       'sshleifer/tiny-gpt2'
								__a		 =       AutoConfig.from_pretrained(UpperCAmelCase   )
								__a		 =       TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]				,						training=UpperCAmelCase				,						inference=UpperCAmelCase				,						sequence_lengths=[8]				,						batch_sizes=[1]				,						multi_process=UpperCAmelCase				,						)
								__a		 =       TensorFlowBenchmark(UpperCAmelCase				,						[config]   )
								__a		 =       benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result   )
								self.check_results_dict_not_empty(results.memory_inference_result   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[int]:
								__a		 =       'sshleifer/tiny-gpt2'
								__a		 =       TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]				,						training=UpperCAmelCase				,						inference=UpperCAmelCase				,						sequence_lengths=[8]				,						batch_sizes=[1]				,						multi_process=UpperCAmelCase				,						)
								__a		 =       TensorFlowBenchmark(UpperCAmelCase   )
								__a		 =       benchmark.run()
								self.check_results_dict_not_empty(results.time_train_result   )
								self.check_results_dict_not_empty(results.memory_train_result   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[Any]:
								__a		 =       'sshleifer/tiny-gpt2'
								__a		 =       AutoConfig.from_pretrained(UpperCAmelCase   )
								__a		 =       TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]				,						training=UpperCAmelCase				,						inference=UpperCAmelCase				,						sequence_lengths=[8]				,						batch_sizes=[1]				,						multi_process=UpperCAmelCase				,						)
								__a		 =       TensorFlowBenchmark(UpperCAmelCase				,						[config]   )
								__a		 =       benchmark.run()
								self.check_results_dict_not_empty(results.time_train_result   )
								self.check_results_dict_not_empty(results.memory_train_result   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   List[str]:
								__a		 =       'patrickvonplaten/t5-tiny-random'
								__a		 =       AutoConfig.from_pretrained(UpperCAmelCase   )
								__a		 =       TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]				,						training=UpperCAmelCase				,						inference=UpperCAmelCase				,						sequence_lengths=[8]				,						batch_sizes=[1]				,						multi_process=UpperCAmelCase				,						)
								__a		 =       TensorFlowBenchmark(UpperCAmelCase				,						configs=[config]   )
								__a		 =       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 __SCREAMING_SNAKE_CASE							(						self   )			->   str:
								__a		 =       'sshleifer/tiny-gpt2'
								__a		 =       TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]				,						training=UpperCAmelCase				,						inference=UpperCAmelCase				,						sequence_lengths=[8]				,						batch_sizes=[1]				,						use_xla=UpperCAmelCase				,						multi_process=UpperCAmelCase				,						)
								__a		 =       TensorFlowBenchmark(UpperCAmelCase   )
								__a		 =       benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result   )
								self.check_results_dict_not_empty(results.memory_inference_result   )



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







			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								__a		 =       '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:
													__a		 =       TensorFlowBenchmarkArguments(
													    models=[MODEL_ID]				,						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				,						eager_mode=UpperCAmelCase				,						multi_process=UpperCAmelCase				,						)
													__a		 =       TensorFlowBenchmark(UpperCAmelCase   )
													__a		 =       benchmark.run()
													_check_summary_is_not_empty(result.inference_summary   )
													self.assertTrue(Path(os.path.join(UpperCAmelCase				,						'log.txt'   )   ).exists()   )




