




'''simple docstring'''







import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


sys.path.append(str(Path(__file__).parent.parent / '''utils'''))

from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


lowerCAmelCase_     						=				get_tests_dir('''fixtures''')

class 	_snake_case(			unittest.TestCase      ):






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






    """simple docstring"""


    A__       =     mock.Mock()
    A__       =     5_00
    A__       =     {}
    A__       =     HTTPError
    A__       =     {}

    # Download this model to make sure it's in the cache.
    A__       =     WavaVecaFeatureExtractor.from_pretrained('hf-internal-testing/tiny-random-wav2vec2'    )
    # Under the mock environment we get a 500 error when trying to reach the model.
    with mock.patch('requests.Session.request'    ,		return_value=_SCREAMING_SNAKE_CASE    ) as mock_head:
       A__       =     WavaVecaFeatureExtractor.from_pretrained('hf-internal-testing/tiny-random-wav2vec2'    )
       # This check we did call the fake head request
       mock_head.assert_called()






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






    """simple docstring"""


    A__       =     WavaVecaFeatureExtractor.from_pretrained(
        'https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json'    )




@is_staging_test
class 	_snake_case(			unittest.TestCase      ):






 @classmethod
 def 						_UpperCamelCase (cls       :  str    )       ->   Tuple:






    """simple docstring"""


    A__       =     TOKEN
    HfFolder.save_token(_SCREAMING_SNAKE_CASE    )






 @classmethod
 def 						_UpperCamelCase (cls       :  Optional[int]    )       ->   Tuple:






    """simple docstring"""


    try:
       delete_repo(token=cls._token    ,		repo_id='test-feature-extractor'    )
    except HTTPError:
       pass

    try:
       delete_repo(token=cls._token    ,		repo_id='valid_org/test-feature-extractor-org'    )
    except HTTPError:
       pass

    try:
       delete_repo(token=cls._token    ,		repo_id='test-dynamic-feature-extractor'    )
    except HTTPError:
       pass






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






    """simple docstring"""


    A__       =     WavaVecaFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE    )
    feature_extractor.push_to_hub('test-feature-extractor'    ,		use_auth_token=self._token    )

    A__       =     WavaVecaFeatureExtractor.from_pretrained(f"""{USER}/test-feature-extractor"""    )
    for k, v in feature_extractor.__dict__.items():
       self.assertEqual(_SCREAMING_SNAKE_CASE    ,		getattr(_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    )    )

    # Reset repo
    delete_repo(token=self._token    ,		repo_id='test-feature-extractor'    )

    # Push to hub via save_pretrained
    with tempfile.TemporaryDirectory() as tmp_dir:
       feature_extractor.save_pretrained(
           _SCREAMING_SNAKE_CASE    ,		repo_id='test-feature-extractor'    ,		push_to_hub=_SCREAMING_SNAKE_CASE    ,		use_auth_token=self._token    )

    A__       =     WavaVecaFeatureExtractor.from_pretrained(f"""{USER}/test-feature-extractor"""    )
    for k, v in feature_extractor.__dict__.items():
       self.assertEqual(_SCREAMING_SNAKE_CASE    ,		getattr(_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    )    )






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






    """simple docstring"""


    A__       =     WavaVecaFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE    )
    feature_extractor.push_to_hub('valid_org/test-feature-extractor'    ,		use_auth_token=self._token    )

    A__       =     WavaVecaFeatureExtractor.from_pretrained('valid_org/test-feature-extractor'    )
    for k, v in feature_extractor.__dict__.items():
       self.assertEqual(_SCREAMING_SNAKE_CASE    ,		getattr(_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    )    )

    # Reset repo
    delete_repo(token=self._token    ,		repo_id='valid_org/test-feature-extractor'    )

    # Push to hub via save_pretrained
    with tempfile.TemporaryDirectory() as tmp_dir:
       feature_extractor.save_pretrained(
           _SCREAMING_SNAKE_CASE    ,		repo_id='valid_org/test-feature-extractor-org'    ,		push_to_hub=_SCREAMING_SNAKE_CASE    ,		use_auth_token=self._token    )

    A__       =     WavaVecaFeatureExtractor.from_pretrained('valid_org/test-feature-extractor-org'    )
    for k, v in feature_extractor.__dict__.items():
       self.assertEqual(_SCREAMING_SNAKE_CASE    ,		getattr(_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    )    )






 def 						_UpperCamelCase (self       :  Tuple    )       ->   Tuple:






    """simple docstring"""


    CustomFeatureExtractor.register_for_auto_class()
    A__       =     CustomFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE    )

    feature_extractor.push_to_hub('test-dynamic-feature-extractor'    ,		use_auth_token=self._token    )

    # This has added the proper auto_map field to the config
    self.assertDictEqual(
        feature_extractor.auto_map    ,		{'AutoFeatureExtractor': 'custom_feature_extraction.CustomFeatureExtractor'}    ,		)

    A__       =     AutoFeatureExtractor.from_pretrained(
        f"""{USER}/test-dynamic-feature-extractor"""    ,		trust_remote_code=_SCREAMING_SNAKE_CASE    )
    # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module
    self.assertEqual(new_feature_extractor.__class__.__name__    ,		'CustomFeatureExtractor'    )






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.'		)
