


'''simple docstring'''






import os
import unittest

from huggingface_hub.utils import are_progress_bars_disabled

import transformers.models.bart.tokenization_bart
from transformers import logging
from transformers.testing_utils import CaptureLogger, mockenv, mockenv_context
from transformers.utils.logging import disable_progress_bar, enable_progress_bar




class 	a__       (							unittest.TestCase						):





							'''simple docstring'''






							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							int:
														lowerCAmelCase__      =			logging.get_logger()

														# the current default level is logging.WARNING
														lowerCAmelCase__      =			logging.get_verbosity()

														logging.set_verbosity_error()
														self.assertEqual(logger.getEffectiveLevel()      ,					logging.get_verbosity()     )

														logging.set_verbosity_warning()
														self.assertEqual(logger.getEffectiveLevel()      ,					logging.get_verbosity()     )

														logging.set_verbosity_info()
														self.assertEqual(logger.getEffectiveLevel()      ,					logging.get_verbosity()     )

														logging.set_verbosity_debug()
														self.assertEqual(logger.getEffectiveLevel()      ,					logging.get_verbosity()     )

														# restore to the original level
														logging.set_verbosity(lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Union[str, Any]:
														lowerCAmelCase__      =			logging.get_verbosity()

														lowerCAmelCase__      =			logging.get_logger('''transformers.models.bart.tokenization_bart'''     )
														lowerCAmelCase__      =			'''Testing 1, 2, 3'''

														# should be able to log warnings (if default settings weren't overridden by `pytest --log-level-all`)
														if level_origin <= logging.WARNING:
																					with CaptureLogger(lowerCamelCase_     ) as cl:
																												logger.warning(lowerCamelCase_     )
																					self.assertEqual(cl.out      ,					msg + '''\n'''     )

														# this is setting the level for all of `transformers.*` loggers
														logging.set_verbosity_error()

														# should not be able to log warnings
														with CaptureLogger(lowerCamelCase_     ) as cl:
																					logger.warning(lowerCamelCase_     )
														self.assertEqual(cl.out      ,					''''''     )

														# should be able to log warnings again
														logging.set_verbosity_warning()
														with CaptureLogger(lowerCamelCase_     ) as cl:
																					logger.warning(lowerCamelCase_     )
														self.assertEqual(cl.out      ,					msg + '''\n'''     )

														# restore to the original level
														logging.set_verbosity(lowerCamelCase_     )







							@mockenv(TRANSFORMERS_VERBOSITY='''error'''     )
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Union[str, Any]:
														# reset for the env var to take effect, next time some logger call is made
														transformers.utils.logging._reset_library_root_logger()
														# this action activates the env var
														lowerCAmelCase__      =			logging.get_logger('''transformers.models.bart.tokenization_bart'''     )

														lowerCAmelCase__      =			os.getenv('''TRANSFORMERS_VERBOSITY'''      ,					lowerCamelCase_     )
														lowerCAmelCase__      =			logging.log_levels[env_level_str]

														lowerCAmelCase__      =			logging.get_verbosity()
														self.assertEqual(
														    lowerCamelCase_      ,					lowerCamelCase_      ,					F"""TRANSFORMERS_VERBOSITY={env_level_str}/{env_level}, but internal verbosity is {current_level}"""      ,					)

														# restore to the original level
														lowerCAmelCase__      =			''''''
														transformers.utils.logging._reset_library_root_logger()







							@mockenv(TRANSFORMERS_VERBOSITY='''super-error'''     )
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[Any]:
														# reset for the env var to take effect, next time some logger call is made
														transformers.utils.logging._reset_library_root_logger()
														lowerCAmelCase__      =			logging.logging.getLogger()
														with CaptureLogger(lowerCamelCase_     ) as cl:
																					# this action activates the env var
																					logging.get_logger('''transformers.models.bart.tokenization_bart'''     )
														self.assertIn('''Unknown option TRANSFORMERS_VERBOSITY=super-error'''      ,					cl.out     )

														# no need to restore as nothing was changed







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[int]:
														# testing `logger.warning_advice()`
														transformers.utils.logging._reset_library_root_logger()

														lowerCAmelCase__      =			logging.get_logger('''transformers.models.bart.tokenization_bart'''     )
														lowerCAmelCase__      =			'''Testing 1, 2, 3'''

														with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS='''1'''     ):
																					# nothing should be logged as env var disables this method
																					with CaptureLogger(lowerCamelCase_     ) as cl:
																												logger.warning_advice(lowerCamelCase_     )
																					self.assertEqual(cl.out      ,					''''''     )

														with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS=''''''     ):
																					# should log normally as TRANSFORMERS_NO_ADVISORY_WARNINGS is unset
																					with CaptureLogger(lowerCamelCase_     ) as cl:
																												logger.warning_advice(lowerCamelCase_     )
																					self.assertEqual(cl.out      ,					msg + '''\n'''     )


def       _snake_case     (  )		->					List[Any]:
							disable_progress_bar()
							assert are_progress_bars_disabled()

							enable_progress_bar()
							assert not are_progress_bars_disabled()



'''simple docstring'''






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


__UpperCAmelCase					       =							logging.getLogger(__name__)




class 	a__       (							a__						):





							'''simple docstring'''






							def __init__(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_=None     ) ->							Dict:
														super().__init__(
														    lowerCamelCase_      ,					question_encoder_tokenizer=lowerCamelCase_      ,					generator_tokenizer=lowerCamelCase_      ,					index=lowerCamelCase_      ,					init_retrieval=lowerCamelCase_      ,					)
														lowerCAmelCase__      =			None







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_     ) ->							str:
														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
																					lowerCAmelCase__      =			self._infer_socket_ifname()
																					# avoid clash with the NCCL port
																					lowerCAmelCase__      =			str(distributed_port + 1     )
																					lowerCAmelCase__      =			dist.new_group(ranks=lowerCamelCase_      ,					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 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[Any]:
														return dist.get_rank(group=self.process_group     ) == 0







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_=torch.floataa     ) ->							Union[str, Any]:
														lowerCAmelCase__      =			torch.empty(lowerCamelCase_      ,					dtype=lowerCamelCase_     )
														dist.scatter(lowerCamelCase_      ,					src=0      ,					scatter_list=lowerCamelCase_      ,					group=self.process_group     )
														return target_tensor







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							str:
														lowerCAmelCase__      =			psutil.net_if_addrs()
														# a hacky way to deal with varying network interface names
														lowerCAmelCase__      =			next((addr for addr in addrs if addr.startswith('''e'''     ))      ,					lowerCamelCase_     )
														return ifname







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							Tuple[np.ndarray, List[dict]]:

														# single GPU training
														if not dist.is_initialized():
																					lowerCAmelCase__     ,      lowerCAmelCase__      =			self._main_retrieve(lowerCamelCase_      ,					lowerCamelCase_     )
																					return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(lowerCamelCase_     )

														# distributed training
														lowerCAmelCase__      =			dist.get_world_size(group=self.process_group     )

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

														# scatter logic
														lowerCAmelCase__      =			question_hidden_states.shape[0]
														lowerCAmelCase__      =			[]
														lowerCAmelCase__      =			[]
														if self._is_main():
																					assert len(lowerCamelCase_     ) == world_size
																					lowerCAmelCase__     ,      lowerCAmelCase__      =			self._main_retrieve(torch.cat(lowerCamelCase_     ).numpy()      ,					lowerCamelCase_     )
																					lowerCAmelCase__     ,      lowerCAmelCase__      =			torch.tensor(lowerCamelCase_     ), torch.tensor(lowerCamelCase_     )
																					lowerCAmelCase__      =			self._chunk_tensor(lowerCamelCase_      ,					lowerCamelCase_     )
																					lowerCAmelCase__      =			self._chunk_tensor(lowerCamelCase_      ,					lowerCamelCase_     )
														lowerCAmelCase__      =			self._scattered(lowerCamelCase_      ,					[n_queries, n_docs]      ,					target_type=torch.intaa     )
														lowerCAmelCase__      =			self._scattered(lowerCamelCase_      ,					[n_queries, n_docs, question_hidden_states.shape[1]]     )

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