

from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

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


lowerCAmelCase     :   int						     =  logging.get_logger(__name__)
class  _A		(  lowerCAmelCase__):
  SCREAMING_SNAKE_CASE    :      Dict       =  ["input_features", "attention_mask"]

  def __init__(				self ,					_SCREAMING_SNAKE_CASE=80 ,					_SCREAMING_SNAKE_CASE=1_6000 ,					_SCREAMING_SNAKE_CASE=80 ,					_SCREAMING_SNAKE_CASE=0.0 ,					_SCREAMING_SNAKE_CASE=True ,					_SCREAMING_SNAKE_CASE=True ,					_SCREAMING_SNAKE_CASE=True ,					**_SCREAMING_SNAKE_CASE ,					):





        """simple docstring"""







        super().__init__(feature_size=_SCREAMING_SNAKE_CASE ,					sampling_rate=_SCREAMING_SNAKE_CASE ,					padding_value=_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Tuple           =     num_mel_bins
        SCREAMING_SNAKE_CASE_				:  Any           =     do_ceptral_normalize
        SCREAMING_SNAKE_CASE_				:  List[str]           =     normalize_means
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     normalize_vars
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     True

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  List[Any]           =     waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     torch.from_numpy(_SCREAMING_SNAKE_CASE					).unsqueeze(0					)
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     ta_kaldi.fbank(_SCREAMING_SNAKE_CASE ,					num_mel_bins=self.num_mel_bins ,					sample_frequency=self.sampling_rate					)
        return features.numpy()

  @staticmethod
  def  UpperCAmelCase    (				_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE = True ,					_SCREAMING_SNAKE_CASE = True ,					_SCREAMING_SNAKE_CASE = 0.0 ,					):





        """simple docstring"""







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

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

        # make sure array is in float32
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     x.astype(np.floataa					)

        return x

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE = None					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Tuple           =     attention_mask.sum(-1					) if attention_mask is not None else [x.shape[0] for x in input_features]
        return [
            self.utterance_cmvn(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					self.normalize_means ,					self.normalize_vars ,					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 ,					):





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

        SCREAMING_SNAKE_CASE_				:  Any           =     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}"					)
        SCREAMING_SNAKE_CASE_				:  Tuple           =     is_batched_numpy or (
            isinstance(_SCREAMING_SNAKE_CASE ,					(list, tuple)					) and (isinstance(raw_speech[0] ,					(np.ndarray, tuple, list)					))
        )

        if is_batched:
              SCREAMING_SNAKE_CASE_				:  Dict           =     [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					):
              SCREAMING_SNAKE_CASE_				:  Optional[int]           =     np.asarray(_SCREAMING_SNAKE_CASE ,					dtype=np.floataa					)
        elif isinstance(_SCREAMING_SNAKE_CASE ,					np.ndarray					) and raw_speech.dtype is np.dtype(np.floataa					):
              SCREAMING_SNAKE_CASE_				:  Any           =     raw_speech.astype(np.floataa					)

        # always return batch
        if not is_batched:
              SCREAMING_SNAKE_CASE_				:  Optional[int]           =     [raw_speech]

        # extract fbank features
        SCREAMING_SNAKE_CASE_				:  str           =     [self._extract_fbank_features(_SCREAMING_SNAKE_CASE					) for waveform in raw_speech]

        # convert into correct format for padding
        SCREAMING_SNAKE_CASE_				:  List[str]           =     BatchFeature({'input_features': features}					)

        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     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
        SCREAMING_SNAKE_CASE_				:  Tuple           =     padded_inputs.get('input_features'					)
        if isinstance(input_features[0] ,					_SCREAMING_SNAKE_CASE					):
              SCREAMING_SNAKE_CASE_				:  Dict           =     [np.asarray(_SCREAMING_SNAKE_CASE ,					dtype=np.floataa					) for feature in input_features]

        SCREAMING_SNAKE_CASE_				:  List[Any]           =     padded_inputs.get('attention_mask'					)
        if attention_mask is not None:
              SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     [np.asarray(_SCREAMING_SNAKE_CASE ,					dtype=np.intaa					) for array in attention_mask]

        # Utterance-level cepstral mean and variance normalization
        if self.do_ceptral_normalize:
              SCREAMING_SNAKE_CASE_				:  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
                  else None
              )
              SCREAMING_SNAKE_CASE_				:  str           =     self.normalize(
                  padded_inputs['input_features'] ,					attention_mask=_SCREAMING_SNAKE_CASE					)

        if return_tensors is not None:
              SCREAMING_SNAKE_CASE_				:  Dict           =     padded_inputs.convert_to_tensors(_SCREAMING_SNAKE_CASE					)

        return padded_inputs






# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
import importlib.metadata
import json
import os
from dataclasses import dataclass
from typing import Any, Dict, Union

from packaging import version

from ..utils import is_torch_available, logging


if is_torch_available():
    import torch


lowerCAmelCase     :   Optional[int]						     =  logging.get_logger(__name__)
@dataclass
class  _A		:

  def __init__(				self ,					_SCREAMING_SNAKE_CASE=False ,					_SCREAMING_SNAKE_CASE=False ,					_SCREAMING_SNAKE_CASE=6.0 ,					_SCREAMING_SNAKE_CASE=None ,					_SCREAMING_SNAKE_CASE=False ,					_SCREAMING_SNAKE_CASE=False ,					_SCREAMING_SNAKE_CASE=None ,					_SCREAMING_SNAKE_CASE="fp4" ,					_SCREAMING_SNAKE_CASE=False ,					**_SCREAMING_SNAKE_CASE ,					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  str           =     load_in_abit
        SCREAMING_SNAKE_CASE_				:  Tuple           =     load_in_abit
        SCREAMING_SNAKE_CASE_				:  Dict           =     llm_inta_threshold
        SCREAMING_SNAKE_CASE_				:  Tuple           =     llm_inta_skip_modules
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     llm_inta_enable_fpaa_cpu_offload
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     llm_inta_has_fpaa_weight
        SCREAMING_SNAKE_CASE_				:  List[str]           =     bnb_abit_quant_type
        SCREAMING_SNAKE_CASE_				:  int           =     bnb_abit_use_double_quant

        if bnb_abit_compute_dtype is None:
              SCREAMING_SNAKE_CASE_				:  List[Any]           =     torch.floataa
        elif isinstance(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):
              SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     getattr(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					)
        elif isinstance(_SCREAMING_SNAKE_CASE ,					torch.dtype					):
              SCREAMING_SNAKE_CASE_				:  Optional[int]           =     bnb_abit_compute_dtype
        else:
              raise ValueError('bnb_4bit_compute_dtype must be a string or a torch.dtype'					)

        self.post_init()

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        if not isinstance(self.llm_inta_threshold ,					_SCREAMING_SNAKE_CASE					):
              raise ValueError('llm_int8_threshold must be a float'					)

        if self.llm_inta_skip_modules is not None and not isinstance(self.llm_inta_skip_modules ,					_SCREAMING_SNAKE_CASE					):
              raise ValueError('llm_int8_skip_modules must be a list of strings'					)
        if not isinstance(self.llm_inta_enable_fpaa_cpu_offload ,					_SCREAMING_SNAKE_CASE					):
              raise ValueError('llm_int8_enable_fp32_cpu_offload must be a boolean'					)

        if not isinstance(self.llm_inta_has_fpaa_weight ,					_SCREAMING_SNAKE_CASE					):
              raise ValueError('llm_int8_has_fp16_weight must be a boolean'					)

        if self.bnb_abit_compute_dtype is not None and not isinstance(self.bnb_abit_compute_dtype ,					torch.dtype					):
              raise ValueError('bnb_4bit_compute_dtype must be torch.dtype'					)

        if not isinstance(self.bnb_abit_quant_type ,					_SCREAMING_SNAKE_CASE					):
              raise ValueError('bnb_4bit_quant_type must be a string'					)

        if not isinstance(self.bnb_abit_use_double_quant ,					_SCREAMING_SNAKE_CASE					):
              raise ValueError('bnb_4bit_use_double_quant must be a boolean'					)

        if self.load_in_abit and not version.parse(importlib.metadata.version('bitsandbytes'					)					) >= version.parse(
            '0.39.0'					):
              raise ValueError(
                  '4 bit quantization requires bitsandbytes>=0.39.0 - please upgrade your bitsandbytes version'					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        return self.load_in_abit or self.load_in_abit

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        if self.load_in_abit:
              return "llm_int8"
        elif self.load_in_abit and self.bnb_abit_quant_type == "fp4":
              return "fp4"
        elif self.load_in_abit and self.bnb_abit_quant_type == "nf4":
              return "nf4"
        else:
              return None

  @classmethod
  def  UpperCAmelCase    (				cls ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     cls(**_SCREAMING_SNAKE_CASE					)

        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     []
        for key, value in kwargs.items():
              if hasattr(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):
                    setattr(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					)
                    to_remove.append(_SCREAMING_SNAKE_CASE					)
        for key in to_remove:
              kwargs.pop(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					)

        if return_unused_kwargs:
              return config, kwargs
        else:
              return config

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        with open(_SCREAMING_SNAKE_CASE ,					'w' ,					encoding='utf-8'					) as writer:
              SCREAMING_SNAKE_CASE_				:  Optional[int]           =     self.to_dict()
              SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     json.dumps(_SCREAMING_SNAKE_CASE ,					indent=2 ,					sort_keys=_SCREAMING_SNAKE_CASE					) + '\n'

              writer.write(_SCREAMING_SNAKE_CASE					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  str           =     copy.deepcopy(self.__dict__					)
        SCREAMING_SNAKE_CASE_				:  str           =     str(output['bnb_4bit_compute_dtype']					).split('.'					)[1]

        return output

  def __repr__(				self					):





        """simple docstring"""







        return f"{self.__class__.__name__} {self.to_json_string()}"

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE = True					):





        """simple docstring"""







        if use_diff is True:
              SCREAMING_SNAKE_CASE_				:  int           =     self.to_diff_dict()
        else:
              SCREAMING_SNAKE_CASE_				:  List[str]           =     self.to_dict()
        return json.dumps(_SCREAMING_SNAKE_CASE ,					indent=2 ,					sort_keys=_SCREAMING_SNAKE_CASE					) + "\n"



  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  int           =     self.to_dict()

        # get the default config dict
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     BitsAndBytesConfig().to_dict()

        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     {}

        # only serialize values that differ from the default config
        for key, value in config_dict.items():
              if value != default_config_dict[key]:
                    SCREAMING_SNAKE_CASE_				:  int           =     value

        return serializable_config_dict






