




import warnings
from typing import Any, Dict, 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 ...utils import PaddingStrategy, TensorType, logging


UpperCamelCase__         =      logging.get_logger(__name__)
class       a__   ( snake_case__				):
						_a							:   Dict =   ["""input_values""", """attention_mask"""]






						def __init__(       self		,      _A = 1		,      _A = 1_6_0_0_0		,      _A = 0.0		,      _A = False		,      _A = 8_0		,      _A = 1_6		,      _A = 6_4		,      _A = "hann_window"		,      _A = 1.0		,      _A = 8_0		,      _A = 7_6_0_0		,      _A = 1E-1_0		,      _A = 2		,      _A = True		,      **_A		,      ):




											"""simple docstring"""



											super().__init__(feature_size=_A		,      sampling_rate=_A		,      padding_value=_A		,      **_A      )
											__lowerCAmelCase           =	do_normalize
											__lowerCAmelCase           =	return_attention_mask

											__lowerCAmelCase           =	num_mel_bins
											__lowerCAmelCase           =	hop_length
											__lowerCAmelCase           =	win_length
											__lowerCAmelCase           =	win_function
											__lowerCAmelCase           =	frame_signal_scale
											__lowerCAmelCase           =	fmin
											__lowerCAmelCase           =	fmax
											__lowerCAmelCase           =	mel_floor
											__lowerCAmelCase           =	reduction_factor

											__lowerCAmelCase           =	win_length * sampling_rate // 1_0_0_0
											__lowerCAmelCase           =	hop_length * sampling_rate // 1_0_0_0
											__lowerCAmelCase           =	optimal_fft_length(self.sample_size      )
											__lowerCAmelCase           =	(self.n_fft // 2) + 1

											__lowerCAmelCase           =	window_function(window_length=self.sample_size		,      name=self.win_function		,      periodic=_A      )

											__lowerCAmelCase           =	mel_filter_bank(
											    num_frequency_bins=self.n_freqs		,      num_mel_filters=self.num_mel_bins		,      min_frequency=self.fmin		,      max_frequency=self.fmax		,      sampling_rate=self.sampling_rate		,      norm="slaney"		,      mel_scale="slaney"		,      )

											if frame_signal_scale != 1.0:
																warnings.warn(
																    "The argument `frame_signal_scale` is deprecated and will be removed in version 4.30.0 of Transformers"		,      _A		,      )
											if reduction_factor != 2.0:
																warnings.warn(
																    "The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers"		,      _A		,      )






						@staticmethod
						# Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
						def     __SCREAMING_SNAKE_CASE(       _A		,      _A		,      _A = 0.0      ):




											"""simple docstring"""



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

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

																					normed_input_values.append(_A      )
											else:
																__lowerCAmelCase           =	[(x - x.mean()) / np.sqrt(x.var() + 1E-7      ) for x in input_values]

											return normed_input_values






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      ):




											"""simple docstring"""



											__lowerCAmelCase           =	spectrogram(
											    _A		,      window=self.window		,      frame_length=self.sample_size		,      hop_length=self.sample_stride		,      fft_length=self.n_fft		,      mel_filters=self.mel_filters		,      mel_floor=self.mel_floor		,      log_mel="log10"		,      )
											return log_mel_spec.T






						def __call__(       self		,      _A = None		,      _A = None		,      _A = False		,      _A = None		,      _A = False		,      _A = None		,      _A = None		,      _A = None		,      _A = None		,      **_A		,      ):




											"""simple docstring"""



											if audio is None and audio_target is None:
																raise ValueError("You must provide either `audio` or `audio_target` values."      )

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

											if audio is not None:
																__lowerCAmelCase           =	self._process_audio(
																    _A		,      _A		,      _A		,      _A		,      _A		,      _A		,      _A		,      _A		,      **_A		,      )
											else:
																__lowerCAmelCase           =	None

											if audio_target is not None:
																__lowerCAmelCase           =	self._process_audio(
																    _A		,      _A		,      _A		,      _A		,      _A		,      _A		,      _A		,      _A		,      **_A		,      )

																if inputs is None:
																					return inputs_target
																else:
																					__lowerCAmelCase           =	inputs_target["input_values"]
																					__lowerCAmelCase           =	inputs_target.get("attention_mask"      )
																					if decoder_attention_mask is not None:
																										__lowerCAmelCase           =	decoder_attention_mask

											return inputs






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A = False		,      _A = False		,      _A = None		,      _A = False		,      _A = None		,      _A = None		,      _A = None		,      **_A		,      ):




											"""simple docstring"""



											__lowerCAmelCase           =	isinstance(_A		,      np.ndarray      ) and len(speech.shape      ) > 1
											if is_batched_numpy and len(speech.shape      ) > 2:
																raise ValueError(f"""Only mono-channel audio is supported for input to {self}"""      )
											__lowerCAmelCase           =	is_batched_numpy or (
											    isinstance(_A		,      (list, tuple)      ) and (isinstance(speech[0]		,      (np.ndarray, tuple, list)      ))
											)

											if is_batched:
																__lowerCAmelCase           =	[np.asarray(_A		,      dtype=np.floataa      ) for speech in speech]
											elif not is_batched and not isinstance(_A		,      np.ndarray      ):
																__lowerCAmelCase           =	np.asarray(_A		,      dtype=np.floataa      )
											elif isinstance(_A		,      np.ndarray      ) and speech.dtype is np.dtype(np.floataa      ):
																__lowerCAmelCase           =	speech.astype(np.floataa      )

											# always return batch
											if not is_batched:
																__lowerCAmelCase           =	[speech]

											# needed to make pad() work on spectrogram inputs
											__lowerCAmelCase           =	self.feature_size

											# convert into correct format for padding
											if is_target:
																__lowerCAmelCase           =	[self._extract_mel_features(_A      ) for waveform in speech]
																__lowerCAmelCase           =	BatchFeature({"input_values": features}      )
																__lowerCAmelCase           =	self.num_mel_bins
											else:
																__lowerCAmelCase           =	BatchFeature({"input_values": speech}      )

											__lowerCAmelCase           =	self.pad(
											    _A		,      padding=_A		,      max_length=_A		,      truncation=_A		,      pad_to_multiple_of=_A		,      return_attention_mask=_A		,      **_A		,      )

											__lowerCAmelCase           =	feature_size_hack

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

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

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

											if return_tensors is not None:
																__lowerCAmelCase           =	padded_inputs.convert_to_tensors(_A      )

											return padded_inputs






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	super().to_dict()

											# Don't serialize these as they are derived from the other properties.
											__lowerCAmelCase           =	["window", "mel_filters", "sample_size", "sample_stride", "n_fft", "n_freqs"]
											for name in names:
																if name in output:
																					del output[name]

											return output










import logging
import random

import ray

from transformers import RagConfig, RagRetriever, RagTokenizer
from transformers.models.rag.retrieval_rag import CustomHFIndex


UpperCamelCase__         =      logging.getLogger(__name__)
class       a__   :






						def __init__(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	False






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A		,      _A		,      _A      ):




											"""simple docstring"""



											if not self.initialized:
																__lowerCAmelCase           =	RagRetriever(
																    _A		,      question_encoder_tokenizer=_A		,      generator_tokenizer=_A		,      index=_A		,      init_retrieval=_A		,      )
																__lowerCAmelCase           =	True






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											self.retriever.index.init_index()






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A      ):




											"""simple docstring"""



											__lowerCAmelCase       ,							__lowerCAmelCase           =	self.retriever._main_retrieve(_A		,      _A      )
											return doc_ids, retrieved_doc_embeds






class       a__   ( snake_case__				):






						def __init__(       self		,      _A		,      _A		,      _A		,      _A		,      _A=None      ):




											"""simple docstring"""



											if index is not None and index.is_initialized() and len(_A      ) > 0:
																raise ValueError(
																    "When using Ray for distributed fine-tuning, "
																    "you'll need to provide the paths instead, "
																    "as the dataset and the index are loaded "
																    "separately. More info in examples/rag/use_own_knowledge_dataset.py "      )
											super().__init__(
											    _A		,      question_encoder_tokenizer=_A		,      generator_tokenizer=_A		,      index=_A		,      init_retrieval=_A		,      )
											__lowerCAmelCase           =	retrieval_workers
											if len(self.retrieval_workers      ) > 0:
																ray.get(
																    [
																        worker.create_rag_retriever.remote(_A		,      _A		,      _A		,      _A      )
																        for worker in self.retrieval_workers
																    ]      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											logger.info("initializing retrieval"      )

											if len(self.retrieval_workers      ) > 0:
																ray.get([worker.init_retrieval.remote() for worker in self.retrieval_workers]      )
											else:
																# Non-distributed training. Load index into this same process.
																self.index.init_index()






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A      ):




											"""simple docstring"""



											if len(self.retrieval_workers      ) > 0:
																# Select a random retrieval actor.
																__lowerCAmelCase           =	self.retrieval_workers[random.randint(0		,      len(self.retrieval_workers      ) - 1      )]
																__lowerCAmelCase       ,							__lowerCAmelCase           =	ray.get(random_worker.retrieve.remote(_A		,      _A      )      )
											else:
																__lowerCAmelCase       ,							__lowerCAmelCase           =	self._main_retrieve(_A		,      _A      )
											return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(_A      )






						@classmethod
						def     __SCREAMING_SNAKE_CASE(       cls		,      _A		,      _A=None		,      **_A      ):




											"""simple docstring"""



											return super(_A		,      cls      ).get_tokenizers(_A		,      _A		,      **_A      )






						@classmethod
						def     __SCREAMING_SNAKE_CASE(       cls		,      _A		,      _A		,      _A=None		,      **_A      ):




											"""simple docstring"""



											__lowerCAmelCase           =	kwargs.pop("config"		,      _A      ) or RagConfig.from_pretrained(_A		,      **_A      )
											__lowerCAmelCase           =	RagTokenizer.from_pretrained(_A		,      config=_A      )
											__lowerCAmelCase           =	rag_tokenizer.question_encoder
											__lowerCAmelCase           =	rag_tokenizer.generator
											if indexed_dataset is not None:
																__lowerCAmelCase           =	"custom"
																__lowerCAmelCase           =	CustomHFIndex(config.retrieval_vector_size		,      _A      )
											else:
																__lowerCAmelCase           =	cls._build_index(_A      )
											return cls(
											    _A		,      question_encoder_tokenizer=_A		,      generator_tokenizer=_A		,      retrieval_workers=_A		,      index=_A		,      )




