




'''simple docstring'''







import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


lowerCAmelCase_     						=				random.Random()

if is_torch_available():
    import torch







def 						_A      ( UpperCAmelCase      ,UpperCAmelCase=1.0      ,UpperCAmelCase=None      ,UpperCAmelCase=None						):






   '''simple docstring'''







   if rng is None:
      A__       =     global_rng

   A__       =     []
   for batch_idx in range(shape[0]						):
      values.append([]						)
      for _ in range(shape[1]						):
         values[-1].append(rng.random() * scale						)

   return values

class 	_snake_case(			unittest.TestCase      ):






 def __init__(self       :  List[Any]    ,		a       :  List[str]    ,		a       :  List[str]=7    ,		a       :  Any=4_00    ,		a       :  Dict=20_00    ,		a       :  Optional[int]=1    ,		a       :  Optional[Any]=0.0    ,		a       :  List[str]=1_60_00    ,		a       :  Dict=True    ,		a       :  List[str]=True    ,		)       ->   Union[str, Any]:






    """simple docstring"""


    A__       =     parent
    A__       =     batch_size
    A__       =     min_seq_length
    A__       =     max_seq_length
    A__       =     (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
    A__       =     feature_size
    A__       =     padding_value
    A__       =     sampling_rate
    A__       =     return_attention_mask
    A__       =     do_normalize






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






    """simple docstring"""


    return {
        "feature_size": self.feature_size,
        "padding_value": self.padding_value,
        "sampling_rate": self.sampling_rate,
        "return_attention_mask": self.return_attention_mask,
        "do_normalize": self.do_normalize,
    }






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






    """simple docstring"""


    def _flatten(a       :  List[Any]    ):
       return list(itertools.chain(*_SCREAMING_SNAKE_CASE    )    )

    if equal_length:
       A__       =     floats_list((self.batch_size, self.max_seq_length)    )
    else:
       # make sure that inputs increase in size
       A__       =     [
           _flatten(floats_list((x, self.feature_size)    )    )
           for x in range(self.min_seq_length    ,		self.max_seq_length    ,		self.seq_length_diff    )
       ]

    if numpify:
       A__       =     [np.asarray(_SCREAMING_SNAKE_CASE    ) for x in speech_inputs]

    return speech_inputs




@require_torch
@require_torchaudio
class 	_snake_case(			UpperCAmelCase       , unittest.TestCase      ):
 __snake_case:							Dict						=					ASTFeatureExtractor






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






    """simple docstring"""


    A__       =     ASTFeatureExtractionTester(self    )






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






    """simple docstring"""


    A__       =     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
    # create three inputs of length 800, 1000, and 1200
    A__       =     [floats_list((1, x)    )[0] for x in range(8_00    ,		14_00    ,		2_00    )]
    A__       =     [np.asarray(_SCREAMING_SNAKE_CASE    ) for speech_input in speech_inputs]

    # Test not batched input
    A__       =     feat_extract(speech_inputs[0]    ,		return_tensors='np'    ).input_values
    A__       =     feat_extract(np_speech_inputs[0]    ,		return_tensors='np'    ).input_values
    self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    ,		atol=1e-3    )    )

    # Test batched
    A__       =     feat_extract(_SCREAMING_SNAKE_CASE    ,		padding=_SCREAMING_SNAKE_CASE    ,		return_tensors='np'    ).input_values
    A__       =     feat_extract(_SCREAMING_SNAKE_CASE    ,		padding=_SCREAMING_SNAKE_CASE    ,		return_tensors='np'    ).input_values
    for enc_seq_a, enc_seq_a in zip(_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    ):
       self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    ,		atol=1e-3    )    )

    # Test 2-D numpy arrays are batched.
    A__       =     [floats_list((1, x)    )[0] for x in (8_00, 8_00, 8_00)]
    A__       =     np.asarray(_SCREAMING_SNAKE_CASE    )
    A__       =     feat_extract(_SCREAMING_SNAKE_CASE    ,		return_tensors='np'    ).input_values
    A__       =     feat_extract(_SCREAMING_SNAKE_CASE    ,		return_tensors='np'    ).input_values
    for enc_seq_a, enc_seq_a in zip(_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    ):
       self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    ,		atol=1e-3    )    )






 @require_torch
 def 						_UpperCamelCase (self       :  Any    )       ->   int:






    """simple docstring"""


    import torch

    A__       =     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
    A__       =     np.random.rand(1_00    ).astype(np.floataa    )
    A__       =     np_speech_inputs.tolist()

    for inputs in [py_speech_inputs, np_speech_inputs]:
       A__       =     feature_extractor.pad([{'input_values': inputs}]    ,		return_tensors='np'    )
       self.assertTrue(np_processed.input_values.dtype == np.floataa    )
       A__       =     feature_extractor.pad([{'input_values': inputs}]    ,		return_tensors='pt'    )
       self.assertTrue(pt_processed.input_values.dtype == torch.floataa    )






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






    """simple docstring"""


    from datasets import load_dataset

    A__       =     load_dataset('hf-internal-testing/librispeech_asr_dummy'    ,		'clean'    ,		split='validation'    )
    # automatic decoding with librispeech
    A__       =     ds.sort('id'    ).select(range(_SCREAMING_SNAKE_CASE    )    )[:num_samples]['audio']

    return [x["array"] for x in speech_samples]






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






    """simple docstring"""


    A__       =     torch.tensor(
        [-0.9894, -1.2776, -0.9066, -1.2776, -0.9349, -1.2609, -1.0386, -1.2776,
         -1.1561, -1.2776, -1.2052, -1.2723, -1.2190, -1.2132, -1.2776, -1.1133,
         -1.1953, -1.1343, -1.1584, -1.2203, -1.1770, -1.2474, -1.2381, -1.1936,
         -0.9270, -0.8317, -0.8049, -0.7706, -0.7565, -0.7869]    )
    # fmt: on

    A__       =     self._load_datasamples(1    )
    A__       =     ASTFeatureExtractor()
    A__       =     feature_extractor(_SCREAMING_SNAKE_CASE    ,		return_tensors='pt'    ).input_values
    self.assertEquals(input_values.shape    ,		(1, 10_24, 1_28)    )
    self.assertTrue(torch.allclose(input_values[0, 0, :30]    ,		_SCREAMING_SNAKE_CASE    ,		atol=1e-4    )    )






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