




import logging
import os
from typing import List, Tuple

import numpy as np
import psutil
import torch
import torch.distributed as dist

from transformers import RagRetriever


_SCREAMING_SNAKE_CASE    :      str          =			logging.getLogger(__name__)







class  UpperCamelCase__		(    SCREAMING_SNAKE_CASE_   ):







	'''simple docstring'''





	def __init__(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__=None		):
					super().__init__(
					    UpperCamelCase__				,       question_encoder_tokenizer=UpperCamelCase__				,       generator_tokenizer=UpperCamelCase__				,       index=UpperCamelCase__				,       init_retrieval=UpperCamelCase__				,       )
					A__  :	str         =							None





	def   __snake_case				(			self				,       UpperCamelCase__		):
					logger.info('''initializing retrieval'''		)

					# initializing a separate process group for retrieval as the default
					# nccl backend doesn't support gather/scatter operations while gloo
					# is too slow to replace nccl for the core gpu communication
					if dist.is_initialized():
									logger.info('''dist initialized'''		)
									# needs to be set manually
									A__  :	Union[str, Any]         =							self._infer_socket_ifname()
									# avoid clash with the NCCL port
									A__  :	Union[str, Any]         =							str(distributed_port + 1		)
									A__  :	Union[str, Any]         =							dist.new_group(ranks=UpperCamelCase__				,       backend='''gloo'''		)

					# initialize retriever only on the main worker
					if not dist.is_initialized() or self._is_main():
									logger.info('''dist not initialized / main'''		)
									self.index.init_index()

					# all processes wait untill the retriever is initialized by the main process
					if dist.is_initialized():
									torch.distributed.barrier(group=self.process_group		)





	def   __snake_case				(			self		):
					return dist.get_rank(group=self.process_group		) == 0





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__=torch.floataa		):
					A__  :	Union[str, Any]         =							torch.empty(UpperCamelCase__				,       dtype=UpperCamelCase__		)
					dist.scatter(UpperCamelCase__				,       src=0				,       scatter_list=UpperCamelCase__				,       group=self.process_group		)
					return target_tensor





	def   __snake_case				(			self		):
					A__  :	int         =							psutil.net_if_addrs()
					# a hacky way to deal with varying network interface names
					A__  :	Union[str, Any]         =							next((addr for addr in addrs if addr.startswith('''e'''		))				,       UpperCamelCase__		)
					return ifname





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__		):

					if not dist.is_initialized():
									A__       ,							A__  :	Optional[Any]         =							self._main_retrieve(UpperCamelCase__				,       UpperCamelCase__		)
									return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(UpperCamelCase__		)

					# distributed training
					A__  :	Any         =							dist.get_world_size(group=self.process_group		)

					# gather logic
					A__  :	Dict         =							None
					if self._is_main():
									A__  :	Any         =							[torch.empty(question_hidden_states.shape				,       dtype=torch.floataa		) for _ in range(UpperCamelCase__		)]
					dist.gather(torch.tensor(UpperCamelCase__		)				,       dst=0				,       gather_list=UpperCamelCase__				,       group=self.process_group		)

					# scatter logic
					A__  :	Dict         =							question_hidden_states.shape[0]
					A__  :	str         =							[]
					A__  :	Tuple         =							[]
					if self._is_main():
									assert len(UpperCamelCase__		) == world_size
									A__       ,							A__  :	Any         =							self._main_retrieve(torch.cat(UpperCamelCase__		).numpy()				,       UpperCamelCase__		)
									A__       ,							A__  :	List[Any]         =							torch.tensor(UpperCamelCase__		), torch.tensor(UpperCamelCase__		)
									A__  :	Union[str, Any]         =							self._chunk_tensor(UpperCamelCase__				,       UpperCamelCase__		)
									A__  :	Tuple         =							self._chunk_tensor(UpperCamelCase__				,       UpperCamelCase__		)
					A__  :	int         =							self._scattered(UpperCamelCase__				,       [n_queries, n_docs]				,       target_type=torch.intaa		)
					A__  :	Tuple         =							self._scattered(UpperCamelCase__				,       [n_queries, n_docs, question_hidden_states.shape[1]]		)

					return retrieved_doc_embeds.numpy(), doc_ids.numpy(), self.index.get_doc_dicts(UpperCamelCase__		)

# 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 torch

from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor
from ..utils import is_datasets_available
from .base import PipelineTool


if is_datasets_available():
					from datasets import load_dataset







class  UpperCamelCase__		(    SCREAMING_SNAKE_CASE_   ):







	'''simple docstring'''
	_lowerCAmelCase							       =				"microsoft/speecht5_tts"
	_lowerCAmelCase							       =				(
	    "This is a tool that reads an English text out loud. It takes an input named `text` which should contain the "
	    "text to read (in English) and returns a waveform object containing the sound."
	)
	_lowerCAmelCase							       =				"text_reader"
	_lowerCAmelCase							       =				SpeechTaProcessor
	_lowerCAmelCase							       =				SpeechTaForTextToSpeech
	_lowerCAmelCase							       =				SpeechTaHifiGan

	_lowerCAmelCase							       =				["text"]
	_lowerCAmelCase							       =				["audio"]





	def   __snake_case				(			self		):
					if self.post_processor is None:
									A__  :	int         =							'''microsoft/speecht5_hifigan'''
					super().setup()





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__=None		):
					A__  :	List[Any]         =							self.pre_processor(text=UpperCamelCase__				,       return_tensors='''pt'''				,       truncation=UpperCamelCase__		)

					if speaker_embeddings is None:
									if not is_datasets_available():
													raise ImportError('''Datasets needs to be installed if not passing speaker embeddings.'''		)

									A__  :	List[Any]         =							load_dataset('''Matthijs/cmu-arctic-xvectors'''				,       split='''validation'''		)
									A__  :	Dict         =							torch.tensor(embeddings_dataset[7305]['''xvector''']		).unsqueeze(0		)

					return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings}





	def   __snake_case				(			self				,       UpperCamelCase__		):
					with torch.no_grad():
									return self.model.generate_speech(**UpperCamelCase__		)





	def   __snake_case				(			self				,       UpperCamelCase__		):
					with torch.no_grad():
									return self.post_processor(UpperCamelCase__		).cpu().detach()