



"""simple docstring"""
from typing import List, Optional, Union

import numpy as np

from ....audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function
from ....feature_extraction_sequence_utils import SequenceFeatureExtractor
from ....feature_extraction_utils import BatchFeature
from ....file_utils import PaddingStrategy, TensorType
from ....utils import logging


A:   Any								=		logging.get_logger(__name__)



class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__ ):
   __lowerCAmelCase  :   str						=						['input_features', 'attention_mask']

   def __init__(   self      ,     _SCREAMING_SNAKE_CASE=80      ,     _SCREAMING_SNAKE_CASE=16000      ,     _SCREAMING_SNAKE_CASE=0.0      ,     _SCREAMING_SNAKE_CASE=10      ,     _SCREAMING_SNAKE_CASE=25      ,     _SCREAMING_SNAKE_CASE="hamming_window"      ,     _SCREAMING_SNAKE_CASE=3_2768.0      ,     _SCREAMING_SNAKE_CASE=0.97      ,     _SCREAMING_SNAKE_CASE=1.0      ,     _SCREAMING_SNAKE_CASE=True      ,     _SCREAMING_SNAKE_CASE=True      ,     _SCREAMING_SNAKE_CASE=False      ,     **_SCREAMING_SNAKE_CASE      ,     )  ->      str:





    '''simple docstring'''




    super().__init__(feature_size=_SCREAMING_SNAKE_CASE      ,     sampling_rate=_SCREAMING_SNAKE_CASE      ,     padding_value=_SCREAMING_SNAKE_CASE      ,     **_SCREAMING_SNAKE_CASE						)

    UpperCAmelCase	:					str	=	feature_size
    UpperCAmelCase	:					str	=	sampling_rate
    UpperCAmelCase	:					List[str]	=	padding_value
    UpperCAmelCase	:					Dict	=	hop_length
    UpperCAmelCase	:					str	=	win_length
    UpperCAmelCase	:					int	=	frame_signal_scale
    UpperCAmelCase	:					Tuple	=	preemphasis_coeff
    UpperCAmelCase	:					Any	=	mel_floor
    UpperCAmelCase	:					Optional[int]	=	normalize_means
    UpperCAmelCase	:					Union[str, Any]	=	normalize_vars
    UpperCAmelCase	:					Optional[int]	=	win_function
    UpperCAmelCase	:					Optional[int]	=	return_attention_mask

    UpperCAmelCase	:					Optional[Any]	=	win_length * sampling_rate // 1000
    UpperCAmelCase	:					List[Any]	=	hop_length * sampling_rate // 1000

    UpperCAmelCase	:					Optional[int]	=	optimal_fft_length(self.sample_size						)
    UpperCAmelCase	:					List[Any]	=	(self.n_fft // 2) + 1

   def 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE						)  ->      np.ndarray:





    '''simple docstring'''




    if self.win_function == "hamming_window":
     UpperCAmelCase	:					str	=	window_function(window_length=self.sample_size      ,     name=self.win_function      ,     periodic=_SCREAMING_SNAKE_CASE						)
    else:
     UpperCAmelCase	:					Any	=	window_function(window_length=self.sample_size      ,     name=self.win_function						)

    UpperCAmelCase	:					Optional[int]	=	mel_filter_bank(
        num_frequency_bins=self.n_freqs      ,     num_mel_filters=self.feature_size      ,     min_frequency=0.0      ,     max_frequency=self.sampling_rate / 2.0      ,     sampling_rate=self.sampling_rate      ,     )

    UpperCAmelCase	:					Optional[int]	=	spectrogram(
        one_waveform * self.frame_signal_scale      ,     window=_SCREAMING_SNAKE_CASE      ,     frame_length=self.sample_size      ,     hop_length=self.sample_stride      ,     fft_length=self.n_fft      ,     center=_SCREAMING_SNAKE_CASE      ,     preemphasis=self.preemphasis_coeff      ,     mel_filters=_SCREAMING_SNAKE_CASE      ,     mel_floor=self.mel_floor      ,     log_mel="""log"""      ,     )
    return msfc_features.T

   def 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      Any:





    '''simple docstring'''




    if self.normalize_means:
     UpperCAmelCase	:					str	=	x[:input_length].mean(axis=0						)
     UpperCAmelCase	:					Optional[int]	=	np.subtract(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)
    if self.normalize_vars:
     UpperCAmelCase	:					Any	=	x[:input_length].std(axis=0						)
     UpperCAmelCase	:					List[str]	=	np.divide(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)

    if input_length < x.shape[0]:
     UpperCAmelCase	:					Dict	=	padding_value

    # make sure array is in float32
    UpperCAmelCase	:					Dict	=	x.astype(np.floataa						)

    return x

   def 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE = None						)  ->      List[np.ndarray]:





    '''simple docstring'''




    UpperCAmelCase	:					List[Any]	=	attention_mask.sum(-1						) if attention_mask is not None else [x.shape[0] for x in input_features]
    return [self._normalize_one(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     self.padding_value						) for x, n in zip(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)]




   def __call__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE = False      ,     _SCREAMING_SNAKE_CASE = None      ,     _SCREAMING_SNAKE_CASE = False      ,     _SCREAMING_SNAKE_CASE = None      ,     _SCREAMING_SNAKE_CASE = None      ,     _SCREAMING_SNAKE_CASE = None      ,     _SCREAMING_SNAKE_CASE = None      ,     **_SCREAMING_SNAKE_CASE      ,     )  ->      BatchFeature:





    '''simple docstring'''




    if sampling_rate is not None:
     if sampling_rate != self.sampling_rate:
      raise ValueError(
          F"The model corresponding to this feature extractor: {self} was trained using a sampling rate of"
          F" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with"
          F" {self.sampling_rate} and not {sampling_rate}."						)
    else:
     logger.warning(
         """It is strongly recommended to pass the ``sampling_rate`` argument to this function. """
         """Failing to do so can result in silent errors that might be hard to debug."""						)

    UpperCAmelCase	:					List[str]	=	isinstance(_SCREAMING_SNAKE_CASE      ,     np.ndarray						) and len(raw_speech.shape						) > 1
    if is_batched_numpy and len(raw_speech.shape						) > 2:
     raise ValueError(F"Only mono-channel audio is supported for input to {self}"						)
    UpperCAmelCase	:					Any	=	is_batched_numpy or (
        isinstance(_SCREAMING_SNAKE_CASE      ,     (list, tuple)						) and (isinstance(raw_speech[0]      ,     (np.ndarray, tuple, list)						))
    )

    if is_batched:
     UpperCAmelCase	:					str	=	[np.asarray(_SCREAMING_SNAKE_CASE      ,     dtype=np.floataa						) for speech in raw_speech]
    elif not is_batched and not isinstance(_SCREAMING_SNAKE_CASE      ,     np.ndarray						):
     UpperCAmelCase	:					List[Any]	=	np.asarray(_SCREAMING_SNAKE_CASE      ,     dtype=np.floataa						)
    elif isinstance(_SCREAMING_SNAKE_CASE      ,     np.ndarray						) and raw_speech.dtype is np.dtype(np.floataa						):
     UpperCAmelCase	:					List[Any]	=	raw_speech.astype(np.floataa						)

    # always return batch
    if not is_batched:
     UpperCAmelCase	:					Union[str, Any]	=	[raw_speech]

    # extract fbank features
    UpperCAmelCase	:					Optional[int]	=	[self._extract_mfsc_features(_SCREAMING_SNAKE_CASE						) for one_waveform in raw_speech]

    # convert into correct format for padding
    UpperCAmelCase	:					Optional[Any]	=	BatchFeature({"""input_features""": features}						)

    UpperCAmelCase	:					List[Any]	=	self.pad(
        _SCREAMING_SNAKE_CASE      ,     padding=_SCREAMING_SNAKE_CASE      ,     max_length=_SCREAMING_SNAKE_CASE      ,     truncation=_SCREAMING_SNAKE_CASE      ,     pad_to_multiple_of=_SCREAMING_SNAKE_CASE      ,     return_attention_mask=_SCREAMING_SNAKE_CASE      ,     **_SCREAMING_SNAKE_CASE      ,     )
    # make sure list is in array format
    UpperCAmelCase	:					Union[str, Any]	=	padded_inputs.get("""input_features"""						)
    if isinstance(input_features[0]      ,     _SCREAMING_SNAKE_CASE						):
     UpperCAmelCase	:					Optional[Any]	=	[np.asarray(_SCREAMING_SNAKE_CASE      ,     dtype=np.floataa						) for feature in input_features]

    UpperCAmelCase	:					Union[str, Any]	=	padded_inputs.get("""attention_mask"""						)
    if attention_mask is not None:
     UpperCAmelCase	:					Optional[Any]	=	[np.asarray(_SCREAMING_SNAKE_CASE      ,     dtype=np.intaa						) for array in attention_mask]

    if self.normalize_means or self.normalize_vars:
     UpperCAmelCase	:					Union[str, Any]	=	(
         np.array(_SCREAMING_SNAKE_CASE      ,     dtype=np.intaa						)
         if self._get_padding_strategies(_SCREAMING_SNAKE_CASE      ,     max_length=_SCREAMING_SNAKE_CASE						) is not PaddingStrategy.DO_NOT_PAD
         and padding
         else None
     )
     UpperCAmelCase	:					Any	=	self.normalize(
         padded_inputs["""input_features"""]      ,     attention_mask=_SCREAMING_SNAKE_CASE						)

    if return_tensors is not None:
     UpperCAmelCase	:					Optional[Any]	=	padded_inputs.convert_to_tensors(_SCREAMING_SNAKE_CASE						)

    return padded_inputs


'''simple docstring'''







import copy
import os
from typing import Union

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


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

_lowercase :  List[str]    							=       {
    "google/pix2struct-textcaps-base": (
        "https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json"
    ),
}






class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct_text_model'''
					lowerCAmelCase_        =     ['''past_key_values''']
					lowerCAmelCase_        =     {
					    '''hidden_size''': '''hidden_size''',
					    '''num_attention_heads''': '''num_heads''',
					    '''num_hidden_layers''': '''num_layers''',
					}






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=5_02_44				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=64				,							__SCREAMING_SNAKE_CASE=20_48				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=32				,							__SCREAMING_SNAKE_CASE=1_28				,							__SCREAMING_SNAKE_CASE=0.1				,							__SCREAMING_SNAKE_CASE=1E-6				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE="gelu_new"				,							__SCREAMING_SNAKE_CASE=0				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=0				,							__SCREAMING_SNAKE_CASE=1				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=True				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											lowercase_	:     Any													=       vocab_size
											lowercase_	:     Tuple													=       hidden_size
											lowercase_	:     Optional[Any]													=       d_kv
											lowercase_	:     List[str]													=       d_ff
											lowercase_	:     List[str]													=       num_layers
											lowercase_	:     Optional[Any]													=       num_heads
											lowercase_	:     Union[str, Any]													=       relative_attention_num_buckets
											lowercase_	:     Optional[int]													=       relative_attention_max_distance
											lowercase_	:     Union[str, Any]													=       dropout_rate
											lowercase_	:     Dict													=       layer_norm_epsilon
											lowercase_	:     Dict													=       initializer_factor
											lowercase_	:     List[Any]													=       use_cache

											lowercase_	:     Optional[int]													=       eos_token_id
											lowercase_	:     Optional[int]													=       decoder_start_token_id

											# for backwards compatibility
											lowercase_	:     Any													=       dense_act_fn

											super().__init__(
											    pad_token_id=__SCREAMING_SNAKE_CASE				,							eos_token_id=__SCREAMING_SNAKE_CASE				,							decoder_start_token_id=__SCREAMING_SNAKE_CASE				,							tie_word_embeddings=__SCREAMING_SNAKE_CASE				,							is_decoder=__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE				,							)






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											cls._set_token_in_kwargs(__SCREAMING_SNAKE_CASE		)

											lowercase_						, lowercase_	:     Optional[int]													=       cls.get_config_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)

											# get the text config dict if we are loading from Pix2StructConfig
											if config_dict.get('''model_type'''		) == "pix2struct":
																	lowercase_	:     List[Any]													=       config_dict['''text_config''']

											if "model_type" in config_dict and hasattr(cls				,							'''model_type'''		) and config_dict["model_type"] != cls.model_type:
																	logger.warning(
																	    F'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
																	    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''		)

											return cls.from_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)







class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct_vision_model'''






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=20_48				,							__SCREAMING_SNAKE_CASE=64				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE="gelu_new"				,							__SCREAMING_SNAKE_CASE=1E-6				,							__SCREAMING_SNAKE_CASE=0.0				,							__SCREAMING_SNAKE_CASE=0.0				,							__SCREAMING_SNAKE_CASE=1E-1_0				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE=40_96				,							__SCREAMING_SNAKE_CASE=32				,							__SCREAMING_SNAKE_CASE=1_28				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											super().__init__(**__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Union[str, Any]													=       hidden_size
											lowercase_	:     Any													=       patch_embed_hidden_size
											lowercase_	:     List[Any]													=       d_ff
											lowercase_	:     Dict													=       dropout_rate
											lowercase_	:     Any													=       num_hidden_layers
											lowercase_	:     Any													=       num_attention_heads
											lowercase_	:     int													=       initializer_range
											lowercase_	:     Dict													=       initializer_factor
											lowercase_	:     Dict													=       attention_dropout
											lowercase_	:     Optional[Any]													=       layer_norm_eps
											lowercase_	:     str													=       dense_act_fn
											lowercase_	:     Dict													=       seq_len
											lowercase_	:     List[Any]													=       relative_attention_num_buckets
											lowercase_	:     int													=       relative_attention_max_distance
											lowercase_	:     Optional[int]													=       d_kv






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											cls._set_token_in_kwargs(__SCREAMING_SNAKE_CASE		)

											lowercase_						, lowercase_	:     str													=       cls.get_config_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)

											# get the vision config dict if we are loading from Pix2StructConfig
											if config_dict.get('''model_type'''		) == "pix2struct":
																	lowercase_	:     Optional[int]													=       config_dict['''vision_config''']

											if "model_type" in config_dict and hasattr(cls				,							'''model_type'''		) and config_dict["model_type"] != cls.model_type:
																	logger.warning(
																	    F'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
																	    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''		)

											return cls.from_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)







class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct'''
					lowerCAmelCase_        =     True






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE=0.02				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=True				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											super().__init__(tie_word_embeddings=__SCREAMING_SNAKE_CASE				,							is_encoder_decoder=__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)

											if text_config is None:
																	lowercase_	:     Optional[Any]													=       {}
																	logger.info('''text_config is None. Initializing the Pix2StructTextConfig with default values.'''		)

											if vision_config is None:
																	lowercase_	:     Dict													=       {}
																	logger.info('''vision_config is None. Initializing the Pix2StructVisionConfig with default values.'''		)

											lowercase_	:     str													=       PixaStructTextConfig(**__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Tuple													=       PixaStructVisionConfig(**__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Optional[Any]													=       self.text_config.decoder_start_token_id
											lowercase_	:     Union[str, Any]													=       self.text_config.pad_token_id
											lowercase_	:     Union[str, Any]													=       self.text_config.eos_token_id

											lowercase_	:     int													=       initializer_factor
											lowercase_	:     Any													=       initializer_range

											lowercase_	:     str													=       self.initializer_range
											lowercase_	:     str													=       self.initializer_range

											lowercase_	:     int													=       is_vqa






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											return cls(text_config=text_config.to_dict()				,							vision_config=vision_config.to_dict()				,							**__SCREAMING_SNAKE_CASE		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Tuple													=       copy.deepcopy(self.__dict__		)
											lowercase_	:     Any													=       self.text_config.to_dict()
											lowercase_	:     Optional[Any]													=       self.vision_config.to_dict()
											lowercase_	:     Optional[int]													=       self.__class__.model_type
											return output



