



'''simple docstring'''






import copy
from typing import Any, Dict, List, Optional, Union

import numpy as np
import torch

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


lowerCAmelCase:      List[str]		 =		logging.get_logger(__name__)

class 							a__(      lowerCamelCase__			):
    lowercase__				=					["""input_features""", """is_longer"""]







    def __init__(   self				:						Optional[int]		,    __snake_case				:						List[str]=64		,    __snake_case				:						Tuple=4_80_00		,    __snake_case				:						Optional[int]=4_80		,    __snake_case				:						Optional[int]=10		,    __snake_case				:						Optional[int]=10_24		,    __snake_case				:						str=0.0		,    __snake_case				:						str=False		,    __snake_case				:						float = 0		,    __snake_case				:						float = 1_40_00		,    __snake_case				:						int = None		,    __snake_case				:						str = "fusion"		,    __snake_case				:						str = "repeatpad"		,    **__snake_case				:						int		,    ):
      super().__init__(
          feature_size=__snake_case		,    sampling_rate=__snake_case		,    padding_value=__snake_case		,    return_attention_mask=__snake_case		,    **__snake_case		,    )
      a		:      Tuple       =  top_db
      a		:      str       =  truncation
      a		:      Union[str, Any]       =  padding
      a		:      Optional[int]       =  fft_window_size
      a		:      Any       =  (fft_window_size >> 1) + 1
      a		:      Optional[Any]       =  hop_length
      a		:      List[str]       =  max_length_s
      a		:      Tuple       =  max_length_s * sampling_rate
      a		:      str       =  sampling_rate
      a		:      int       =  frequency_min
      a		:      List[str]       =  frequency_max
      a		:      Union[str, Any]       =  mel_filter_bank(
          num_frequency_bins=self.nb_frequency_bins		,    num_mel_filters=__snake_case		,    min_frequency=__snake_case		,    max_frequency=__snake_case		,    sampling_rate=__snake_case		,    norm=__snake_case		,    mel_scale='htk'		,    )
      a		:      Union[str, Any]       =  mel_filter_bank(
          num_frequency_bins=self.nb_frequency_bins		,    num_mel_filters=__snake_case		,    min_frequency=__snake_case		,    max_frequency=__snake_case		,    sampling_rate=__snake_case		,    norm='slaney'		,    mel_scale='slaney'		,    )







    def     lowercase_ (   self				:						Optional[int]			):
      a		:      int       =  copy.deepcopy(self.__dict__			)
      a		:      Union[str, Any]       =  self.__class__.__name__
      if "mel_filters" in output:
        del output["mel_filters"]
      if "mel_filters_slaney" in output:
        del output["mel_filters_slaney"]
      return output







    def     lowercase_ (   self				:						Dict		,    __snake_case				:						np.array		,    __snake_case				:						Optional[np.array] = None			):
      a		:      Optional[Any]       =  spectrogram(
          __snake_case		,    window_function(self.fft_window_size		,    'hann'			)		,    frame_length=self.fft_window_size		,    hop_length=self.hop_length		,    power=2.0		,    mel_filters=__snake_case		,    log_mel='dB'		,    )
      return log_mel_spectrogram.T







    def     lowercase_ (   self				:						Union[str, Any]		,    __snake_case				:						Any		,    __snake_case				:						Tuple		,    __snake_case				:						Union[str, Any]			):
      a		:      Union[str, Any]       =  np.array_split(list(range(0		,    total_frames - chunk_frames + 1			)			)		,    3			)
      if len(ranges[1]			) == 0:
        # if the audio is too short, we just use the first chunk
        a		:      Any       =  [0]
      if len(ranges[2]			) == 0:
        # if the audio is too short, we just use the first chunk
        a		:      str       =  [0]
      # randomly choose index for each part
      a		:      str       =  np.random.choice(ranges[0]			)
      a		:      List[Any]       =  np.random.choice(ranges[1]			)
      a		:      str       =  np.random.choice(ranges[2]			)

      a		:      Any       =  mel[idx_front : idx_front + chunk_frames, :]
      a		:      Tuple       =  mel[idx_middle : idx_middle + chunk_frames, :]
      a		:      Optional[int]       =  mel[idx_back : idx_back + chunk_frames, :]

      a		:      Tuple       =  torch.tensor(mel[None, None, :]			)
      a		:      Union[str, Any]       =  torch.nn.functional.interpolate(
          __snake_case		,    size=[chunk_frames, 64]		,    mode='bilinear'		,    align_corners=__snake_case			)
      a		:      Tuple       =  mel_shrink[0][0].numpy()
      a		:      Optional[int]       =  np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back]		,    axis=0			)
      return mel_fusion







    def     lowercase_ (   self				:						Any		,    __snake_case				:						np.array		,    __snake_case				:						Optional[Any]		,    __snake_case				:						Dict		,    __snake_case				:						Tuple			):
      if waveform.shape[0] > max_length:
        if truncation == "rand_trunc":
          a		:      Optional[int]       =  True
          # random crop to max_length (for compatibility) -> this should be handled by self.pad
          a		:      Any       =  len(__snake_case			) - max_length
          a		:      Union[str, Any]       =  np.random.randint(0		,    overflow + 1			)
          a		:      Dict       =  waveform[idx : idx + max_length]
          a		:      List[str]       =  self._np_extract_fbank_features(__snake_case		,    self.mel_filters_slaney			)[None, :]
        elif truncation == "fusion":
          a		:      Union[str, Any]       =  self._np_extract_fbank_features(__snake_case		,    self.mel_filters			)
          a		:      List[str]       =  max_length // self.hop_length + 1  # the +1 related to how the spectrogram is computed
          a		:      Dict       =  mel.shape[0]
          if chunk_frames == total_frames:
            # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length.
            # In this case, we just use the whole audio.
            a		:      Any       =  np.stack([mel, mel, mel, mel]		,    axis=0			)
            a		:      str       =  False
          else:
            a		:      str       =  self._random_mel_fusion(__snake_case		,    __snake_case		,    __snake_case			)
            a		:      Optional[int]       =  True
        else:
          raise NotImplementedError(F"""data_truncating {truncation} not implemented"""			)

      else:
        a		:      Any       =  False
        # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding
        if waveform.shape[0] < max_length:
          if padding == "repeat":
            a		:      List[str]       =  int(max_length / len(__snake_case			)			)
            a		:      Any       =  np.stack(np.tile(__snake_case		,    n_repeat + 1			)			)[:max_length]
          if padding == "repeatpad":
            a		:      Any       =  int(max_length / len(__snake_case			)			)
            a		:      List[Any]       =  np.stack(np.tile(__snake_case		,    __snake_case			)			)
          a		:      Dict       =  np.pad(__snake_case		,    (0, max_length - waveform.shape[0])		,    mode='constant'		,    constant_values=0			)

        if truncation == "fusion":
          a		:      Union[str, Any]       =  self._np_extract_fbank_features(__snake_case		,    self.mel_filters			)
          a		:      int       =  np.stack([input_mel, input_mel, input_mel, input_mel]		,    axis=0			)
        else:
          a		:      List[str]       =  self._np_extract_fbank_features(__snake_case		,    self.mel_filters_slaney			)[None, :]

      return input_mel, longer







    def __call__(   self				:						Dict		,    __snake_case				:						Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]		,    __snake_case				:						str = None		,    __snake_case				:						Optional[str] = None		,    __snake_case				:						Optional[int] = None		,    __snake_case				:						Optional[int] = None		,    __snake_case				:						Optional[Union[str, TensorType]] = None		,    **__snake_case				:						List[Any]		,    ):
      a		:      Optional[int]       =  truncation if truncation is not None else self.truncation
      a		:      Union[str, Any]       =  padding if padding else self.padding

      if sampling_rate is not None:
        if sampling_rate != self.sampling_rate:
          raise ValueError(
              F"""The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a"""
              F""" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input"""
              F""" was sampled with {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.'			)

      a		:      Union[str, Any]       =  isinstance(__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}"""			)
      a		:      Optional[Any]       =  is_batched_numpy or (
          isinstance(__snake_case		,    (list, tuple)			) and (isinstance(raw_speech[0]		,    (np.ndarray, tuple, list)			))
      )

      if is_batched:
        a		:      Optional[Any]       =  [np.asarray(__snake_case		,    dtype=np.floataa			) for speech in raw_speech]
      elif not is_batched and not isinstance(__snake_case		,    np.ndarray			):
        a		:      Dict       =  np.asarray(__snake_case		,    dtype=np.floataa			)
      elif isinstance(__snake_case		,    np.ndarray			) and raw_speech.dtype is np.dtype(np.floataa			):
        a		:      List[Any]       =  raw_speech.astype(np.floataa			)

      # always return batch
      if not is_batched:
        a		:      List[Any]       =  [np.asarray(__snake_case			)]

      # convert to mel spectrogram, truncate and pad if needed.
      a		:      Optional[int]       =  [
          self._get_input_mel(__snake_case		,    max_length if max_length else self.nb_max_samples		,    __snake_case		,    __snake_case			)
          for waveform in raw_speech
      ]

      a		:      str       =  []
      a		:      Optional[int]       =  []
      for mel, longer in padded_inputs:
        input_mel.append(__snake_case			)
        is_longer.append(__snake_case			)

      if truncation == "fusion" and sum(__snake_case			) == 0:
        # if no audio is longer than 10s, then randomly select one audio to be longer
        a		:      str       =  np.random.randint(0		,    len(__snake_case			)			)
        a		:      List[str]       =  True

      if isinstance(input_mel[0]		,    __snake_case			):
        a		:      int       =  [np.asarray(__snake_case		,    dtype=np.floataa			) for feature in input_mel]

      # is_longer is a list of bool
      a		:      Optional[int]       =  [[longer] for longer in is_longer]

      a		:      Optional[Any]       =  {'input_features': input_mel, 'is_longer': is_longer}
      a		:      List[str]       =  BatchFeature(__snake_case			)

      if return_tensors is not None:
        a		:      List[Any]       =  input_features.convert_to_tensors(__snake_case			)

      return input_features




'''simple docstring'''






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

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


if TYPE_CHECKING:
     from ... import FeatureExtractionMixin, TensorType

lowerCAmelCase:      List[Any]		 =		logging.get_logger(__name__)

lowerCAmelCase:      List[str]		 =		{
    'openai/imagegpt-small': '',
    'openai/imagegpt-medium': '',
    'openai/imagegpt-large': '',
}

class 							a__(      lowerCamelCase__			):
    lowercase__				=					"""imagegpt"""
    lowercase__				=					["""past_key_values"""]
    lowercase__				=					{
        """hidden_size""": """n_embd""",
        """max_position_embeddings""": """n_positions""",
        """num_attention_heads""": """n_head""",
        """num_hidden_layers""": """n_layer""",
    }







    def __init__(   self				:						List[str]		,    __snake_case				:						int=5_12 + 1		,    __snake_case				:						Optional[Any]=32 * 32		,    __snake_case				:						Optional[Any]=5_12		,    __snake_case				:						List[str]=24		,    __snake_case				:						Tuple=8		,    __snake_case				:						Optional[int]=None		,    __snake_case				:						Dict="quick_gelu"		,    __snake_case				:						Dict=0.1		,    __snake_case				:						List[str]=0.1		,    __snake_case				:						Optional[Any]=0.1		,    __snake_case				:						Tuple=1e-5		,    __snake_case				:						Union[str, Any]=0.02		,    __snake_case				:						Optional[int]=True		,    __snake_case				:						Dict=True		,    __snake_case				:						str=False		,    __snake_case				:						Any=False		,    __snake_case				:						List[Any]=False		,    **__snake_case				:						List[str]		,    ):
      a		:      Optional[Any]       =  vocab_size
      a		:      Union[str, Any]       =  n_positions
      a		:      int       =  n_embd
      a		:      int       =  n_layer
      a		:      str       =  n_head
      a		:      int       =  n_inner
      a		:      int       =  activation_function
      a		:      Any       =  resid_pdrop
      a		:      Tuple       =  embd_pdrop
      a		:      List[Any]       =  attn_pdrop
      a		:      Tuple       =  layer_norm_epsilon
      a		:      Any       =  initializer_range
      a		:      str       =  scale_attn_weights
      a		:      List[Any]       =  use_cache
      a		:      Any       =  scale_attn_by_inverse_layer_idx
      a		:      Optional[Any]       =  reorder_and_upcast_attn
      a		:      str       =  tie_word_embeddings

      super().__init__(tie_word_embeddings=__snake_case		,    **__snake_case			)

class 							a__(      lowerCamelCase__			):







    @property
    def     lowercase_ (   self				:						Union[str, Any]			):
      return OrderedDict(
          [
              ('input_ids', {0: 'batch', 1: 'sequence'}),
          ]			)







    def     lowercase_ (   self				:						Union[str, Any]		,    __snake_case				:						"FeatureExtractionMixin"		,    __snake_case				:						int = 1		,    __snake_case				:						int = -1		,    __snake_case				:						bool = False		,    __snake_case				:						Optional["TensorType"] = None		,    __snake_case				:						int = 3		,    __snake_case				:						int = 32		,    __snake_case				:						int = 32		,    ):
      a		:      Dict       =  self._generate_dummy_images(__snake_case		,    __snake_case		,    __snake_case		,    __snake_case			)
      a		:      Union[str, Any]       =  dict(preprocessor(images=__snake_case		,    return_tensors=__snake_case			)			)

      return inputs