




'''simple docstring'''







import unittest
from pathlib import Path
from tempfile import NamedTemporaryFile, TemporaryDirectory

from transformers import BertConfig, BertTokenizerFast, FeatureExtractionPipeline
from transformers.convert_graph_to_onnx import (
    convert,
    ensure_valid_input,
    generate_identified_filename,
    infer_shapes,
    quantize,
)
from transformers.testing_utils import require_tf, require_tokenizers, require_torch, slow

class 	_snake_case:






 def 						_UpperCamelCase (self       :  int    ,		a       :  Dict    ,		a       :  Union[str, Any]    ,		a       :  Optional[Any]    )       ->   int:






    """simple docstring"""


    return None




class 	_snake_case:






 def 						_UpperCamelCase (self       :  Tuple    ,		a       :  int    ,		a       :  str    ,		a       :  Union[str, Any]    ,		a       :  Any    )       ->   str:






    """simple docstring"""


    return None




class 	_snake_case(			unittest.TestCase      ):
 __snake_case:							Optional[Any]						=					[
     # (model_name, model_kwargs)
     ('bert-base-cased', {}),
     ('gpt2', {'use_cache': False}),  # We don't support exporting GPT2 past keys anymore
 ]






 @require_tf
 @slow
 def 						_UpperCamelCase (self       :  List[Any]    )       ->   List[Any]:






    """simple docstring"""


    for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
       self._test_export(_SCREAMING_SNAKE_CASE    ,		'tf'    ,		12    ,		**_SCREAMING_SNAKE_CASE    )






 @require_torch
 @slow
 def 						_UpperCamelCase (self       :  Dict    )       ->   List[str]:






    """simple docstring"""


    for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
       self._test_export(_SCREAMING_SNAKE_CASE    ,		'pt'    ,		12    ,		**_SCREAMING_SNAKE_CASE    )






 @require_torch
 @slow
 def 						_UpperCamelCase (self       :  Dict    )       ->   List[str]:






    """simple docstring"""


    from transformers import BertModel

    A__       =     ['[UNK]', '[SEP]', '[CLS]', '[PAD]', '[MASK]', 'some', 'other', 'words']
    with NamedTemporaryFile(mode='w+t'    ) as vocab_file:
       vocab_file.write('\n'.join(_SCREAMING_SNAKE_CASE    )    )
       vocab_file.flush()
       A__       =     BertTokenizerFast(vocab_file.name    )

    with TemporaryDirectory() as bert_save_dir:
       A__       =     BertModel(BertConfig(vocab_size=len(_SCREAMING_SNAKE_CASE    )    )    )
       model.save_pretrained(_SCREAMING_SNAKE_CASE    )
       self._test_export(_SCREAMING_SNAKE_CASE    ,		'pt'    ,		12    ,		_SCREAMING_SNAKE_CASE    )






 @require_tf
 @slow
 def 						_UpperCamelCase (self       :  Dict    )       ->   List[Any]:






    """simple docstring"""


    for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
       A__       =     self._test_export(_SCREAMING_SNAKE_CASE    ,		'tf'    ,		12    ,		**_SCREAMING_SNAKE_CASE    )
       A__       =     quantize(Path(_SCREAMING_SNAKE_CASE    )    )

       # Ensure the actual quantized model is not bigger than the original one
       if quantized_path.stat().st_size >= Path(_SCREAMING_SNAKE_CASE    ).stat().st_size:
          self.fail('Quantized model is bigger than initial ONNX model'    )






 @require_torch
 @slow
 def 						_UpperCamelCase (self       :  Tuple    )       ->   Union[str, Any]:






    """simple docstring"""


    for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
       A__       =     self._test_export(_SCREAMING_SNAKE_CASE    ,		'pt'    ,		12    ,		**_SCREAMING_SNAKE_CASE    )
       A__       =     quantize(_SCREAMING_SNAKE_CASE    )

       # Ensure the actual quantized model is not bigger than the original one
       if quantized_path.stat().st_size >= Path(_SCREAMING_SNAKE_CASE    ).stat().st_size:
          self.fail('Quantized model is bigger than initial ONNX model'    )






 def 						_UpperCamelCase (self       :  Optional[Any]    ,		a       :  Optional[Any]    ,		a       :  Tuple    ,		a       :  Union[str, Any]    ,		a       :  int=None    ,		**a       :  List[str]    )       ->   List[Any]:






    """simple docstring"""


    try:
       # Compute path
       with TemporaryDirectory() as tempdir:
          A__       =     Path(_SCREAMING_SNAKE_CASE    ).joinpath('model.onnx'    )

       # Remove folder if exists
       if path.parent.exists():
          path.parent.rmdir()

       # Export
       convert(_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    ,		**_SCREAMING_SNAKE_CASE    )

       return path
    except Exception as e:
       self.fail(_SCREAMING_SNAKE_CASE    )






 @require_torch
 @require_tokenizers
 @slow
 def 						_UpperCamelCase (self       :  Optional[Any]    )       ->   int:






    """simple docstring"""


    from transformers import BertModel

    A__       =     BertModel(BertConfig.from_pretrained('lysandre/tiny-bert-random'    )    )
    A__       =     BertTokenizerFast.from_pretrained('lysandre/tiny-bert-random'    )
    self._test_infer_dynamic_axis(_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    ,		'pt'    )






 @require_tf
 @require_tokenizers
 @slow
 def 						_UpperCamelCase (self       :  str    )       ->   Union[str, Any]:






    """simple docstring"""


    from transformers import TFBertModel

    A__       =     TFBertModel(BertConfig.from_pretrained('lysandre/tiny-bert-random'    )    )
    A__       =     BertTokenizerFast.from_pretrained('lysandre/tiny-bert-random'    )
    self._test_infer_dynamic_axis(_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    ,		'tf'    )






 def 						_UpperCamelCase (self       :  List[Any]    ,		a       :  Union[str, Any]    ,		a       :  int    ,		a       :  Optional[int]    )       ->   str:






    """simple docstring"""


    A__       =     FeatureExtractionPipeline(_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    )

    A__       =     ['input_ids', 'token_type_ids', 'attention_mask', 'output_0', 'output_1']
    A__  ,			A__  ,			A__  ,			A__       =     infer_shapes(_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    )

    # Assert all variables are present
    self.assertEqual(len(_SCREAMING_SNAKE_CASE    )    ,		len(_SCREAMING_SNAKE_CASE    )    )
    self.assertTrue(all(var_name in shapes for var_name in variable_names    )    )
    self.assertSequenceEqual(variable_names[:3]    ,		_SCREAMING_SNAKE_CASE    )
    self.assertSequenceEqual(variable_names[3:]    ,		_SCREAMING_SNAKE_CASE    )

    # Assert inputs are {0: batch, 1: sequence}
    for var_name in ["input_ids", "token_type_ids", "attention_mask"]:
       self.assertDictEqual(shapes[var_name]    ,		{0: 'batch', 1: 'sequence'}    )

    # Assert outputs are {0: batch, 1: sequence} and {0: batch}
    self.assertDictEqual(shapes['output_0']    ,		{0: 'batch', 1: 'sequence'}    )
    self.assertDictEqual(shapes['output_1']    ,		{0: 'batch'}    )






 def 						_UpperCamelCase (self       :  List[str]    )       ->   str:






    """simple docstring"""


    A__       =     ['input_ids', 'attention_mask', 'token_type_ids']
    A__       =     {'input_ids': [1, 2, 3, 4], 'attention_mask': [0, 0, 0, 0], 'token_type_ids': [1, 1, 1, 1]}
    A__  ,			A__       =     ensure_valid_input(FuncContiguousArgs()    ,		_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    )

    # Should have exactly the same number of args (all are valid)
    self.assertEqual(len(_SCREAMING_SNAKE_CASE    )    ,		3    )

    # Should have exactly the same input names
    self.assertEqual(set(_SCREAMING_SNAKE_CASE    )    ,		set(_SCREAMING_SNAKE_CASE    )    )

    # Parameter should be reordered according to their respective place in the function:
    # (input_ids, token_type_ids, attention_mask)
    self.assertEqual(_SCREAMING_SNAKE_CASE    ,		(tokens['input_ids'], tokens['token_type_ids'], tokens['attention_mask'])    )

    # Generated args are interleaved with another args (for instance parameter "past" in GPT2)
    A__  ,			A__       =     ensure_valid_input(FuncNonContiguousArgs()    ,		_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    )

    # Should have exactly the one arg (all before the one not provided "some_other_args")
    self.assertEqual(len(_SCREAMING_SNAKE_CASE    )    ,		1    )
    self.assertEqual(len(_SCREAMING_SNAKE_CASE    )    ,		1    )

    # Should have only "input_ids"
    self.assertEqual(inputs_args[0]    ,		tokens['input_ids']    )
    self.assertEqual(ordered_input_names[0]    ,		'input_ids'    )






 def 						_UpperCamelCase (self       :  str    )       ->   Optional[Any]:






    """simple docstring"""


    A__       =     generate_identified_filename(Path('/home/something/my_fake_model.onnx'    )    ,		'-test'    )
    self.assertEqual('/home/something/my_fake_model-test.onnx'    ,		generated.as_posix()    )






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__    :Dict       =				logging.get_logger(__name__)

lowerCAmelCase__    :Optional[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 (							UpperCAmelCase     ):
		_a      :    Tuple           =      'perceiver'
		def __init__(     self     ,       _SCREAMING_SNAKE_CASE=256     ,       _SCREAMING_SNAKE_CASE=1280     ,       _SCREAMING_SNAKE_CASE=768     ,       _SCREAMING_SNAKE_CASE=1     ,       _SCREAMING_SNAKE_CASE=26     ,       _SCREAMING_SNAKE_CASE=8     ,       _SCREAMING_SNAKE_CASE=8     ,       _SCREAMING_SNAKE_CASE=None     ,       _SCREAMING_SNAKE_CASE=None     ,       _SCREAMING_SNAKE_CASE="kv"     ,       _SCREAMING_SNAKE_CASE=1     ,       _SCREAMING_SNAKE_CASE=1     ,       _SCREAMING_SNAKE_CASE="gelu"     ,       _SCREAMING_SNAKE_CASE=0.1     ,       _SCREAMING_SNAKE_CASE=0.02     ,       _SCREAMING_SNAKE_CASE=1e-1_2     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=262     ,       _SCREAMING_SNAKE_CASE=2048     ,       _SCREAMING_SNAKE_CASE=56     ,       _SCREAMING_SNAKE_CASE=[368, 496]     ,       _SCREAMING_SNAKE_CASE=16     ,       _SCREAMING_SNAKE_CASE=1920     ,       _SCREAMING_SNAKE_CASE=16     ,       _SCREAMING_SNAKE_CASE=[1, 16, 224, 224]     ,       **_SCREAMING_SNAKE_CASE     ,       )					->			Any:




									"""simple docstring"""





									super().__init__(**_SCREAMING_SNAKE_CASE		)

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



class 						__a (							UpperCAmelCase     ):
		@property
		def       UpperCAmelCase__							(     self		)					->			Mapping[str, Mapping[int, str]]:




									"""simple docstring"""





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




									"""simple docstring"""





									return 1e-4






		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE = -1     ,       _SCREAMING_SNAKE_CASE = -1     ,       _SCREAMING_SNAKE_CASE = -1     ,       _SCREAMING_SNAKE_CASE = False     ,       _SCREAMING_SNAKE_CASE = None     ,       _SCREAMING_SNAKE_CASE = 3     ,       _SCREAMING_SNAKE_CASE = 40     ,       _SCREAMING_SNAKE_CASE = 40     ,       )					->			Mapping[str, Any]:




									"""simple docstring"""





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