

import logging
import random

import ray

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


__A :  Dict			  =      logging.getLogger(__name__)






class  __A					:






    def __init__(						self      :     str     ):
        lowerCAmelCase					:      Any           =   False






    def 						lowercase__			(						self      :     int     ,					UpperCAmelCase_      :     List[str]     ,					UpperCAmelCase_      :     Tuple     ,					UpperCAmelCase_      :     Union[str, Any]     ,					UpperCAmelCase_      :     List[Any]     ):
        if not self.initialized:
            lowerCAmelCase					:      int           =   RagRetriever(
                __a     ,					question_encoder_tokenizer=__a     ,					generator_tokenizer=__a     ,					index=__a     ,					init_retrieval=__a     ,					)
            lowerCAmelCase					:      List[str]           =   True






    def 						lowercase__			(						self      :     Tuple     ):
        self.retriever.index.init_index()






    def 						lowercase__			(						self      :     List[Any]     ,					UpperCAmelCase_      :     Dict     ,					UpperCAmelCase_      :     Any     ):
        lowerCAmelCase					:      List[Any]           =   self.retriever._main_retrieve(__a     ,					__a     )
        return doc_ids, retrieved_doc_embeds






class  __A					(				_UpperCamelCase			):






    def __init__(						self      :     Optional[int]     ,					UpperCAmelCase_      :     int     ,					UpperCAmelCase_      :     Tuple     ,					UpperCAmelCase_      :     List[str]     ,					UpperCAmelCase_      :     Tuple     ,					UpperCAmelCase_      :     int=None     ):
        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					:      str           =   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 						lowercase__			(						self      :     str     ):
        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 						lowercase__			(						self      :     Dict     ,					UpperCAmelCase_      :     List[str]     ,					UpperCAmelCase_      :     str     ):
        if len(self.retrieval_workers     ) > 0:
            # Select a random retrieval actor.
            lowerCAmelCase					:      Tuple           =   self.retrieval_workers[random.randint(0     ,					len(self.retrieval_workers     ) - 1     )]
            lowerCAmelCase					:      str           =   ray.get(random_worker.retrieve.remote(__a     ,					__a     )     )
        else:
            lowerCAmelCase					:      Optional[int]           =   self._main_retrieve(__a     ,					__a     )
        return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(__a     )






    @classmethod
    def 						lowercase__			(						cls      :     Any     ,					UpperCAmelCase_      :     List[str]     ,					UpperCAmelCase_      :     Optional[Any]=None     ,					**UpperCAmelCase_      :     List[str]     ):
        return super(__a     ,					cls     ).get_tokenizers(__a     ,					__a     ,					**__a     )






    @classmethod
    def 						lowercase__			(						cls      :     List[Any]     ,					UpperCAmelCase_      :     Any     ,					UpperCAmelCase_      :     List[str]     ,					UpperCAmelCase_      :     str=None     ,					**UpperCAmelCase_      :     Any     ):
        lowerCAmelCase					:      Optional[Any]           =   kwargs.pop('config'     ,					__a     ) or RagConfig.from_pretrained(__a     ,					**__a     )
        lowerCAmelCase					:      Any           =   RagTokenizer.from_pretrained(__a     ,					config=__a     )
        lowerCAmelCase					:      List[Any]           =   rag_tokenizer.question_encoder
        lowerCAmelCase					:      Any           =   rag_tokenizer.generator
        if indexed_dataset is not None:
            lowerCAmelCase					:      int           =   "custom"
            lowerCAmelCase					:      Tuple           =   CustomHFIndex(config.retrieval_vector_size     ,					__a     )
        else:
            lowerCAmelCase					:      Optional[Any]           =   cls._build_index(__a     )
        return cls(
            __a     ,					question_encoder_tokenizer=__a     ,					generator_tokenizer=__a     ,					retrieval_workers=__a     ,					index=__a     ,					)












"""simple docstring"""


def        lowercase__     (       lowercase_      )       ->		list:




      """simple docstring"""

      if len(lowercase_      ) <= 1:
            return [tuple(lowercase_      )]

      _UpperCamelCase   :						Optional[Any]    =							[]

      def generate(lowercase_		,lowercase_      ):
            if k == 1:
                  res.append(tuple(arr[:]      )      )
                  return

            generate(k - 1		,lowercase_      )

            for i in range(k - 1      ):
                  if k % 2 == 0:  # k is even
                        _UpperCamelCase,	_UpperCamelCase   :						List[str]    =							arr[k - 1], arr[i]
                  else:  # k is odd
                        _UpperCamelCase,	_UpperCamelCase   :						int    =							arr[k - 1], arr[0]
                  generate(k - 1		,lowercase_      )

      generate(len(lowercase_      )		,lowercase_      )
      return res


if __name__ == "__main__":
  lowerCamelCase__						= input("Enter numbers separated by a comma:\n").strip()
  lowerCamelCase__						= [int(item) for item in user_input.split(",")]
  print(heaps(arr))
