
'''simple docstring'''





import logging
import random

import ray

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


__lowerCamelCase							= logging.getLogger(__name__)






class 						UpperCAmelCase	:



   def __init__(self	:      Any				)	->			Dict:


          '''simple docstring'''


          snake_case :     Optional[int]           =   False



   def        _SCREAMING_SNAKE_CASE     (self	:      Tuple					,		snake_case__	:      List[Any]					,		snake_case__	:      List[Any]					,		snake_case__	:      Union[str, Any]					,		snake_case__	:      Union[str, Any]				)	->			Tuple:


          '''simple docstring'''


          if not self.initialized:
                 snake_case :     List[str]           =   RagRetriever(
                     __A					,		question_encoder_tokenizer=__A					,		generator_tokenizer=__A					,		index=__A					,		init_retrieval=__A					,		)
                 snake_case :     Dict           =   True



   def        _SCREAMING_SNAKE_CASE     (self	:      int				)	->			Dict:


          '''simple docstring'''


          self.retriever.index.init_index()



   def        _SCREAMING_SNAKE_CASE     (self	:      str				)	->			List[Any]:


          '''simple docstring'''


          del self.retriever
          snake_case :     List[str]           =   False



   def        _SCREAMING_SNAKE_CASE     (self	:      Dict					,		snake_case__	:      Optional[Any]					,		snake_case__	:      Optional[int]				)	->			str:


          '''simple docstring'''


          snake_case       ,      snake_case :     str           =   self.retriever._main_retrieve(__A					,		__A				)
          snake_case :     Tuple           =   self.retriever.index.get_doc_dicts(__A				)
          return doc_ids, retrieved_doc_embeds, doc_dicts






class 						UpperCAmelCase	(		_SCREAMING_SNAKE_CASE				):



   def __init__(self	:      str					,		snake_case__	:      List[Any]					,		snake_case__	:      int					,		snake_case__	:      Optional[int]					,		snake_case__	:      List[str]					,		snake_case__	:      List[Any]=None				)	->			int:


          '''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
          snake_case :     Tuple           =   question_encoder_tokenizer
          snake_case :     Union[str, Any]           =   generator_tokenizer
          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	:      Dict				)	->			Union[str, Any]:


          '''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	:      Optional[Any]					,		snake_case__	:      List[Any]					,		snake_case__	:      Optional[Any]				)	->			Dict:


          '''simple docstring'''


          if len(self.retrieval_workers				) > 0:
                 # Select a random retrieval actor.
                 snake_case :     str           =   self.retrieval_workers[random.randint(0					,		len(self.retrieval_workers				) - 1				)]
                 snake_case       ,      snake_case       ,      snake_case :     Optional[int]           =   ray.get(
                     random_worker.retrieve.remote(__A					,		__A				)				)
          else:
                 snake_case       ,      snake_case :     Dict           =   self._main_retrieve(__A					,		__A				)
                 snake_case :     str           =   self.index.get_doc_dicts(__A				)
          return retrieved_doc_embeds, doc_ids, doc_dicts



   @classmethod
   def        _SCREAMING_SNAKE_CASE     (cls	:      Dict					,		snake_case__	:      List[Any]					,		snake_case__	:      Optional[Any]=None					,		**snake_case__	:      Dict				)	->			Tuple:


          '''simple docstring'''


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



   @classmethod
   def        _SCREAMING_SNAKE_CASE     (cls	:      Dict					,		snake_case__	:      Optional[int]					,		snake_case__	:      Dict					,		snake_case__	:      Tuple=None					,		**snake_case__	:      str				)	->			Optional[Any]:


          '''simple docstring'''


          snake_case :     Tuple           =   kwargs.pop("config"					,		__A				) or RagConfig.from_pretrained(__A					,		**__A				)
          snake_case :     List[str]           =   RagTokenizer.from_pretrained(__A					,		config=__A				)
          snake_case :     List[str]           =   rag_tokenizer.question_encoder
          snake_case :     List[Any]           =   rag_tokenizer.generator

          if indexed_dataset is not None:
                 snake_case :     Union[str, Any]           =   "custom"
                 snake_case :     Any           =   CustomHFIndex(config.retrieval_vector_size					,		__A				)
          else:
                 snake_case :     Union[str, Any]           =   cls._build_index(__A				)

          return cls(
              __A					,		question_encoder_tokenizer=__A					,		generator_tokenizer=__A					,		retrieval_workers=__A					,		index=__A					,		)



   def        _SCREAMING_SNAKE_CASE     (self	:      Optional[Any]				)	->			Union[str, Any]:


          '''simple docstring'''


          logger.info("re-loading the new dataset with embeddings"				)
          # access from the training loop

          ray.get([worker.clear_object.remote() for worker in self.retrieval_workers]				)

          # build the index object again
          snake_case :     List[str]           =   self._build_index(self.config				)

          ray.get(
              [
                  worker.create_rag_retriever.remote(
                      self.config					,		self.question_encoder_tokenizer					,		self.generator_tokenizer					,		__A				)
                  for worker in self.retrieval_workers
              ]				)







import math
from itertools import permutations


def     UpperCamelCase      (					__lowerCamelCase  :				int					):

       if 1 < number < 4:
              # 2 and 3 are primes
              return True
       elif number < 2 or number % 2 == 0 or number % 3 == 0:
              # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
              return False

       # All primes number are in format of 6k +/- 1
       for i in range(5       ,      int(math.sqrt(__lowerCamelCase					) + 1					)       ,      6					):
              if number % i == 0 or number % (i + 2) == 0:
                     return False
       return True


def     UpperCamelCase      (					__lowerCamelCase  :				int       ,      __lowerCamelCase  :				list					):
       snake_case       ,      snake_case :     List[str]           =   0, len(__lowerCamelCase					) - 1
       while left <= right:
              snake_case :     int           =   (left + right) // 2
              if prime_list[middle] == target:
                     return True
              elif prime_list[middle] < target:
                     snake_case :     Dict           =   middle + 1
              else:
                     snake_case :     Optional[int]           =   middle - 1

       return False


def     UpperCamelCase      (					):
       snake_case :     List[Any]           =   [n for n in range(1001       ,      10000       ,      2					) if is_prime(__lowerCamelCase					)]
       snake_case :     Dict           =   []

       for number in prime_list:
              snake_case :     Dict           =   []

              for prime_member in permutations(list(str(__lowerCamelCase					)					)					):
                     snake_case :     Dict           =   int("".join(__lowerCamelCase					)					)

                     if prime % 2 == 0:
                            continue

                     if search(__lowerCamelCase       ,      __lowerCamelCase					):
                            tmp_numbers.append(__lowerCamelCase					)

              tmp_numbers.sort()
              if len(__lowerCamelCase					) >= 3:
                     candidates.append(__lowerCamelCase					)

       snake_case :     Union[str, Any]           =   []
       for candidate in candidates:
              snake_case :     Dict           =   len(__lowerCamelCase					)
              snake_case :     Any           =   False

              for i in range(__lowerCamelCase					):
                     for j in range(i + 1       ,      __lowerCamelCase					):
                            for k in range(j + 1       ,      __lowerCamelCase					):
                                   if (
                                       abs(candidate[i] - candidate[j]					)
                                       == abs(candidate[j] - candidate[k]					)
                                       and len({candidate[i], candidate[j], candidate[k]}					) == 3
                                   ):
                                          passed.append(
                                              sorted([candidate[i], candidate[j], candidate[k]]					)					)
                                          snake_case :     Optional[Any]           =   True

                                   if found:
                                          break
                            if found:
                                   break
                     if found:
                            break

       snake_case :     Tuple           =   set()
       for seq in passed:
              answer.add("".join([str(__lowerCamelCase					) for i in seq]					)					)

       return max(int(__lowerCamelCase					) for x in answer					)


if __name__ == "__main__":
   print(solution())





