






'''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 					lowercase						:


							def __init__(			self)							->				List[str]:
								UpperCAmelCase_		:      str       	=					False


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				Dict:
								if not self.initialized:
									UpperCAmelCase_		:      Any       	=					RagRetriever(
									    _snake_case    ,			question_encoder_tokenizer=_snake_case    ,			generator_tokenizer=_snake_case    ,			index=_snake_case    ,			init_retrieval=_snake_case    ,			)
									UpperCAmelCase_		:      Dict       	=					True


							def 						_snake_case							(			self)							->				str:
								self.retriever.index.init_index()





							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case)							->				str:
								UpperCAmelCase_		:      Optional[Any]       	=					self.retriever._main_retrieve(_snake_case    ,			_snake_case)
								return doc_ids, retrieved_doc_embeds



class 					lowercase						(  a_ ):


							def __init__(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case=None)							->				Dict:
								if index is not None and index.is_initialized() and len(_snake_case) > 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__(
								    _snake_case    ,			question_encoder_tokenizer=_snake_case    ,			generator_tokenizer=_snake_case    ,			index=_snake_case    ,			init_retrieval=_snake_case    ,			)
								UpperCAmelCase_		:      List[str]       	=					retrieval_workers
								if len(self.retrieval_workers) > 0:
									ray.get(
									    [
									        worker.create_rag_retriever.remote(_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case)
									        for worker in self.retrieval_workers
									    ])


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


							@classmethod
							def 						_snake_case							(			cls    ,			_snake_case    ,			_snake_case=None    ,			**_snake_case)							->				str:
								return super(_snake_case    ,			cls).get_tokenizers(_snake_case    ,			_snake_case    ,			**_snake_case)





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









'''simple docstring'''


from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase__										= logging.get_logger(__name__)

lowerCAmelCase__										= {
    "tanreinama/GPTSAN-2.8B-spout_is_uniform": (
        "https://huggingface.co/tanreinama/GPTSAN-2.8B-spout_is_uniform/resolve/main/config.json"
    ),
}



class 					lowercase						(  a_ ):
							_lowerCamelCase		:      Any=   "gptsan-japanese"
							_lowerCamelCase		:      List[Any]=   [
							    "past_key_values",
							]
							_lowerCamelCase		:      Tuple=   {
							    "hidden_size": "d_model",
							    "num_attention_heads": "num_heads",
							    "num_hidden_layers": "num_layers",
							}


							def __init__(			self    ,			_snake_case=3_6000    ,			_snake_case=1280    ,			_snake_case=1024    ,			_snake_case=8192    ,			_snake_case=4096    ,			_snake_case=128    ,			_snake_case=10    ,			_snake_case=0    ,			_snake_case=16    ,			_snake_case=16    ,			_snake_case=128    ,			_snake_case=0.0    ,			_snake_case=1e-5    ,			_snake_case=False    ,			_snake_case=0.0    ,			_snake_case="float32"    ,			_snake_case=False    ,			_snake_case=False    ,			_snake_case=False    ,			_snake_case=0.002    ,			_snake_case=False    ,			_snake_case=True    ,			_snake_case=3_5998    ,			_snake_case=3_5995    ,			_snake_case=3_5999    ,			**_snake_case    ,			)							->				Optional[int]:
								UpperCAmelCase_		:      Dict       	=					vocab_size
								UpperCAmelCase_		:      Optional[int]       	=					max_position_embeddings
								UpperCAmelCase_		:      Any       	=					d_model
								UpperCAmelCase_		:      int       	=					d_ff
								UpperCAmelCase_		:      Tuple       	=					d_ext
								UpperCAmelCase_		:      Tuple       	=					d_spout
								UpperCAmelCase_		:      List[Any]       	=					num_switch_layers
								UpperCAmelCase_		:      Tuple       	=					num_ext_layers
								UpperCAmelCase_		:      Optional[int]       	=					num_switch_layers + num_ext_layers
								UpperCAmelCase_		:      Tuple       	=					num_heads
								UpperCAmelCase_		:      List[str]       	=					num_experts
								UpperCAmelCase_		:      List[str]       	=					expert_capacity
								UpperCAmelCase_		:      Optional[int]       	=					dropout_rate
								UpperCAmelCase_		:      List[Any]       	=					layer_norm_epsilon
								UpperCAmelCase_		:      Any       	=					router_bias
								UpperCAmelCase_		:      Optional[Any]       	=					router_jitter_noise
								UpperCAmelCase_		:      str       	=					router_dtype
								UpperCAmelCase_		:      List[str]       	=					router_ignore_padding_tokens
								UpperCAmelCase_		:      Optional[int]       	=					output_hidden_states
								UpperCAmelCase_		:      Union[str, Any]       	=					output_attentions
								UpperCAmelCase_		:      Optional[int]       	=					initializer_factor
								UpperCAmelCase_		:      Optional[int]       	=					output_router_logits
								UpperCAmelCase_		:      List[str]       	=					use_cache

								super().__init__(
								    separator_token_id=_snake_case    ,			pad_token_id=_snake_case    ,			eos_token_id=_snake_case    ,			**_snake_case    ,			)






