



'''simple docstring'''


from typing import List, Optional, Union

import numpy as np

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


lowercase											=      logging.get_logger(__name__)







class 						UpperCAmelCase						(		__a):


 '''simple docstring'''




 __magic_name__       :			str	=   ["input_values", "attention_mask"]




 def __init__( self				,	lowerCAmelCase_=1				,	lowerCAmelCase_=1_6_0_0_0				,	lowerCAmelCase_=0.0				,	lowerCAmelCase_=False				,	lowerCAmelCase_=True				,	**lowerCAmelCase_				,	)			->   Dict:



      """simple docstring"""







      super().__init__(feature_size=lowerCAmelCase_				,	sampling_rate=lowerCAmelCase_				,	padding_value=lowerCAmelCase_				,	**lowerCAmelCase_)
      a_									=return_attention_mask
      a_									=do_normalize




 @staticmethod
 def    lowercase_	( lowerCAmelCase_				,	lowerCAmelCase_				,	lowerCAmelCase_ = 0.0)			->   List[np.ndarray]:



      """simple docstring"""







      if attention_mask is not None:
           a_									=np.array(lowerCAmelCase_				,	np.intaa)
           a_									=[]

           for vector, length in zip(lowerCAmelCase_				,	attention_mask.sum(-1)):
                a_									=(vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7)
                if length < normed_slice.shape[0]:
                     a_									=padding_value

                normed_input_values.append(lowerCAmelCase_)
      else:
           a_									=[(x - x.mean()) / np.sqrt(x.var() + 1e-7) for x in input_values]

      return normed_input_values




 def __call__( self				,	lowerCAmelCase_				,	lowerCAmelCase_ = False				,	lowerCAmelCase_ = None				,	lowerCAmelCase_ = False				,	lowerCAmelCase_ = None				,	lowerCAmelCase_ = None				,	lowerCAmelCase_ = None				,	lowerCAmelCase_ = None				,	**lowerCAmelCase_				,	)			->   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.")

      a_									=isinstance(lowerCAmelCase_				,	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_									=is_batched_numpy or (
          isinstance(lowerCAmelCase_				,	(list, tuple)) and (isinstance(raw_speech[0]				,	(np.ndarray, tuple, list)))
      )

      # always return batch
      if not is_batched:
           a_									=[raw_speech]

      # convert into correct format for padding
      a_									=BatchFeature({"input_values": raw_speech})

      a_									=self.pad(
          lowerCAmelCase_				,	padding=lowerCAmelCase_				,	max_length=lowerCAmelCase_				,	truncation=lowerCAmelCase_				,	pad_to_multiple_of=lowerCAmelCase_				,	return_attention_mask=lowerCAmelCase_				,	)

      # convert input values to correct format
      a_									=padded_inputs["input_values"]
      if not isinstance(input_values[0]				,	np.ndarray):
           a_									=[np.asarray(lowerCAmelCase_				,	dtype=np.floataa) for array in input_values]
      elif (
          not isinstance(lowerCAmelCase_				,	np.ndarray)
          and isinstance(input_values[0]				,	np.ndarray)
          and input_values[0].dtype is np.dtype(np.floataa)
      ):
           a_									=[array.astype(np.floataa) for array in input_values]
      elif isinstance(lowerCAmelCase_				,	np.ndarray) and input_values.dtype is np.dtype(np.floataa):
           a_									=input_values.astype(np.floataa)

      # convert attention_mask to correct format
      a_									=padded_inputs.get("attention_mask")
      if attention_mask is not None:
           a_									=[np.asarray(lowerCAmelCase_				,	dtype=np.intaa) for array in attention_mask]

      # zero-mean and unit-variance normalization
      if self.do_normalize:
           a_									=(
               attention_mask
               if self._get_padding_strategies(lowerCAmelCase_				,	max_length=lowerCAmelCase_) is not PaddingStrategy.DO_NOT_PAD
               else None
           )
           a_									=self.zero_mean_unit_var_norm(
               padded_inputs["input_values"]				,	attention_mask=lowerCAmelCase_				,	padding_value=self.padding_value)

      if return_tensors is not None:
           a_									=padded_inputs.convert_to_tensors(lowerCAmelCase_)

      return padded_inputs







'''simple docstring'''


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

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


lowercase											=      logging.get_logger(__name__)

lowercase											=      {
    '''gpt2''': '''https://huggingface.co/gpt2/resolve/main/config.json''',
    '''gpt2-medium''': '''https://huggingface.co/gpt2-medium/resolve/main/config.json''',
    '''gpt2-large''': '''https://huggingface.co/gpt2-large/resolve/main/config.json''',
    '''gpt2-xl''': '''https://huggingface.co/gpt2-xl/resolve/main/config.json''',
    '''distilgpt2''': '''https://huggingface.co/distilgpt2/resolve/main/config.json''',
}







class 						UpperCAmelCase						(		__a):


 '''simple docstring'''




 __magic_name__       :			str	=   "gpt2"
 __magic_name__       :			str	=   ["past_key_values"]
 __magic_name__       :			Dict	=   {
     "hidden_size": "n_embd",
     "max_position_embeddings": "n_positions",
     "num_attention_heads": "n_head",
     "num_hidden_layers": "n_layer",
 }




 def __init__( self				,	lowerCAmelCase_=5_0_2_5_7				,	lowerCAmelCase_=1_0_2_4				,	lowerCAmelCase_=7_6_8				,	lowerCAmelCase_=1_2				,	lowerCAmelCase_=1_2				,	lowerCAmelCase_=None				,	lowerCAmelCase_="gelu_new"				,	lowerCAmelCase_=0.1				,	lowerCAmelCase_=0.1				,	lowerCAmelCase_=0.1				,	lowerCAmelCase_=1e-5				,	lowerCAmelCase_=0.0_2				,	lowerCAmelCase_="cls_index"				,	lowerCAmelCase_=True				,	lowerCAmelCase_=None				,	lowerCAmelCase_=True				,	lowerCAmelCase_=0.1				,	lowerCAmelCase_=True				,	lowerCAmelCase_=True				,	lowerCAmelCase_=5_0_2_5_6				,	lowerCAmelCase_=5_0_2_5_6				,	lowerCAmelCase_=False				,	lowerCAmelCase_=False				,	**lowerCAmelCase_				,	)			->   Dict:



      """simple docstring"""







      a_									=vocab_size
      a_									=n_positions
      a_									=n_embd
      a_									=n_layer
      a_									=n_head
      a_									=n_inner
      a_									=activation_function
      a_									=resid_pdrop
      a_									=embd_pdrop
      a_									=attn_pdrop
      a_									=layer_norm_epsilon
      a_									=initializer_range
      a_									=summary_type
      a_									=summary_use_proj
      a_									=summary_activation
      a_									=summary_first_dropout
      a_									=summary_proj_to_labels
      a_									=scale_attn_weights
      a_									=use_cache
      a_									=scale_attn_by_inverse_layer_idx
      a_									=reorder_and_upcast_attn

      a_									=bos_token_id
      a_									=eos_token_id

      super().__init__(bos_token_id=lowerCAmelCase_				,	eos_token_id=lowerCAmelCase_				,	**lowerCAmelCase_)







class 						UpperCAmelCase						(		__a):


 '''simple docstring'''




 def __init__( self				,	lowerCAmelCase_				,	lowerCAmelCase_ = "default"				,	lowerCAmelCase_ = None				,	lowerCAmelCase_ = False				,	)			->   List[str]:



      """simple docstring"""







      super().__init__(lowerCAmelCase_				,	task=lowerCAmelCase_				,	patching_specs=lowerCAmelCase_				,	use_past=lowerCAmelCase_)
      if not getattr(self._config				,	"pad_token_id"				,	lowerCAmelCase_):
           # TODO: how to do that better?
           a_									=0




 @property
 def    lowercase_	( self)			->   Mapping[str, Mapping[int, str]]:



      """simple docstring"""







      a_									=OrderedDict({"input_ids": {0: "batch", 1: "sequence"}})
      if self.use_past:
           self.fill_with_past_key_values_(lowerCAmelCase_				,	direction="inputs")
           a_									={0: "batch", 1: "past_sequence + sequence"}
      else:
           a_									={0: "batch", 1: "sequence"}

      return common_inputs




 @property
 def    lowercase_	( self)			->   int:



      """simple docstring"""







      return self._config.n_layer




 @property
 def    lowercase_	( self)			->   int:



      """simple docstring"""







      return self._config.n_head




 def    lowercase_	( self				,	lowerCAmelCase_				,	lowerCAmelCase_ = -1				,	lowerCAmelCase_ = -1				,	lowerCAmelCase_ = False				,	lowerCAmelCase_ = None				,	)			->   Mapping[str, Any]:



      """simple docstring"""







      a_									=super(lowerCAmelCase_				,	self).generate_dummy_inputs(
          lowerCAmelCase_				,	batch_size=lowerCAmelCase_				,	seq_length=lowerCAmelCase_				,	is_pair=lowerCAmelCase_				,	framework=lowerCAmelCase_)

      # We need to order the input in the way they appears in the forward()
      a_									=OrderedDict({"input_ids": common_inputs["input_ids"]})

      # Need to add the past_keys
      if self.use_past:
           if not is_torch_available():
                raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed.")
           else:
                import torch

                a_		,    a_									=common_inputs["input_ids"].shape
                # Not using the same length for past_key_values
                a_									=seqlen + 2
                a_									=(
                    batch,
                    self.num_attention_heads,
                    past_key_values_length,
                    self._config.hidden_size // self.num_attention_heads,
                )
                a_									=[
                    (torch.zeros(lowerCAmelCase_), torch.zeros(lowerCAmelCase_)) for _ in range(self.num_layers)
                ]

      a_									=common_inputs["attention_mask"]
      if self.use_past:
           a_									=ordered_inputs["attention_mask"].dtype
           a_									=torch.cat(
               [ordered_inputs["attention_mask"], torch.ones(lowerCAmelCase_				,	lowerCAmelCase_				,	dtype=lowerCAmelCase_)]				,	dim=1)

      return ordered_inputs




 @property
 def    lowercase_	( self)			->   int:



      """simple docstring"""







      return 1_3


