




"""simple docstring"""


import logging
import random

import ray

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


A_      =    logging.getLogger(__name__)






class  lowercase:







 '''simple docstring'''





 def __init__(    self:		str		):




   '''simple docstring'''



   _snake_case							:		List[Any]								=  False


 def 			UpperCamelCase_       (    self:		Optional[int],				a_:		List[str],				a_:		Tuple,				a_:		str,				a_:		Optional[Any]		):




   '''simple docstring'''



   if not self.initialized:
     _snake_case							:		List[Any]								=  RagRetriever(
         a_,				question_encoder_tokenizer=a_,				generator_tokenizer=a_,				index=a_,				init_retrieval=a_,				)
     _snake_case							:		Any								=  True


 def 			UpperCamelCase_       (    self:		str		):




   '''simple docstring'''



   self.retriever.index.init_index()


 def 			UpperCamelCase_       (    self:		int,				a_:		List[str],				a_:		str		):




   '''simple docstring'''



   _snake_case		, _snake_case							:		Tuple								=  self.retriever._main_retrieve(a_,				a_		)
   return doc_ids, retrieved_doc_embeds







class  lowercase(					__a			):







 '''simple docstring'''





 def __init__(    self:		str,				a_:		List[Any],				a_:		Union[str, Any],				a_:		List[str],				a_:		Dict,				a_:		Optional[Any]=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_,				)
   _snake_case							:		Optional[Any]								=  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 			UpperCamelCase_       (    self:		List[str]		):




   '''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 			UpperCamelCase_       (    self:		Dict,				a_:		Any,				a_:		int		):




   '''simple docstring'''



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


 @classmethod
 def 			UpperCamelCase_       (    cls:		int,				a_:		Union[str, Any],				a_:		List[str]=None,				**a_:		int		):




   '''simple docstring'''



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


 @classmethod
 def 			UpperCamelCase_       (    cls:		Any,				a_:		Optional[Any],				a_:		int,				a_:		Optional[Any]=None,				**a_:		List[Any]		):




   '''simple docstring'''



   _snake_case							:		int								=  kwargs.pop("""config""",				a_		) or RagConfig.from_pretrained(a_,				**a_		)
   _snake_case							:		Tuple								=  RagTokenizer.from_pretrained(a_,				config=a_		)
   _snake_case							:		str								=  rag_tokenizer.question_encoder
   _snake_case							:		Union[str, Any]								=  rag_tokenizer.generator
   if indexed_dataset is not None:
     _snake_case							:		Tuple								=  """custom"""
     _snake_case							:		List[str]								=  CustomHFIndex(config.retrieval_vector_size,				a_		)
   else:
     _snake_case							:		int								=  cls._build_index(a_		)
   return cls(
       a_,				question_encoder_tokenizer=a_,				generator_tokenizer=a_,				retrieval_workers=a_,				index=a_,				)








"""simple docstring"""


def  UpperCAmelCase__	(snake_case__							: int   ):
  """simple docstring"""

  _snake_case							:		Optional[Any]								=  (1 + 24 * n) ** 0.5
  return ((1 + root) / 6) % 1 == 0


def  UpperCAmelCase__	(snake_case__							: int = 50_00   ):
  """simple docstring"""

  _snake_case							:		List[str]								=  [(i * (3 * i - 1)) // 2 for i in range(1		,       snake_case__   )]
  for i, pentagonal_i in enumerate(snake_case__   ):
    for j in range(snake_case__		,       len(snake_case__   )   ):
      _snake_case							:		Dict								=  pentagonal_nums[j]
      _snake_case							:		Optional[Any]								=  pentagonal_i + pentagonal_j
      _snake_case							:		List[str]								=  pentagonal_j - pentagonal_i
      if is_pentagonal(snake_case__   ) and is_pentagonal(snake_case__   ):
        return b

  return -1


if __name__ == "__main__":
     print(F'''{solution() = }''')


