


import math
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from diffusers.configuration_utils import ConfigMixin, register_to_config
from diffusers.schedulers.scheduling_utils import SchedulerMixin
from diffusers.utils import BaseOutput, deprecate





@dataclass
# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->DDIM
class  lowerCAmelCase__(    _lowerCamelCase						):






    '''simple docstring'''





    A_  :     Optional[int]			= 4_2
    A_  :     Optional[Any]			= None





def      snake_case_						( __lowercase			,       __lowercase=0.9_9_9			,       __lowercase="cosine"			,       ):
   if alpha_transform_type == "cosine":

      def alpha_bar_fn(__lowercase       ):
         return math.cos((t + 0.0_0_8) / 1.0_0_8 * math.pi / 2       ) ** 2

   elif alpha_transform_type == "exp":

      def alpha_bar_fn(__lowercase       ):
         return math.exp(t * -1_2.0       )

   else:
      raise ValueError(F'''Unsupported alpha_tranform_type: {alpha_transform_type}'''       )

   UpperCAmelCase_							:      int						=   []
   for i in range(SCREAMING_SNAKE_CASE_       ):
      UpperCAmelCase_							:      List[str]						=   i / num_diffusion_timesteps
      UpperCAmelCase_							:      str						=   (i + 1) / num_diffusion_timesteps
      betas.append(min(1 - alpha_bar_fn(SCREAMING_SNAKE_CASE_       ) / alpha_bar_fn(SCREAMING_SNAKE_CASE_       )			,       SCREAMING_SNAKE_CASE_       )       )
   return torch.tensor(SCREAMING_SNAKE_CASE_			,       dtype=torch.floataa       )





class  lowerCAmelCase__(    _lowerCamelCase    ,    _lowerCamelCase						):






    '''simple docstring'''





    A_  :     Tuple			= 1





    @register_to_config
    def __init__(	self				:   int       ,   __snake_case				:   Any = 1_000       ,   __snake_case				:   Tuple = 0.0_001       ,   __snake_case				:   Tuple = 0.02       ,   __snake_case				:   Tuple = "linear"       ,   __snake_case				:   Dict = None       ,   __snake_case				:   Optional[int] = True       ,   __snake_case				:   Optional[int] = True       ,   __snake_case				:   List[Any] = 0       ,   __snake_case				:   int = "epsilon"       ,   __snake_case				:   Tuple = 1.0       ,   **__snake_case				:   Dict       ,   ):





       '''simple docstring'''
       if kwargs.get('''set_alpha_to_one'''       ,   A__	) is not None:
          UpperCAmelCase_							:      List[str]						=   (
              '''The `set_alpha_to_one` argument is deprecated. Please use `set_alpha_to_zero` instead.'''
          )
          deprecate('''set_alpha_to_one'''       ,   '''1.0.0'''       ,   A__       ,   standard_warn=A__	)
          UpperCAmelCase_							:      Union[str, Any]						=   kwargs['''set_alpha_to_one''']
       if trained_betas is not None:
          UpperCAmelCase_							:      List[Any]						=   torch.tensor(A__       ,   dtype=torch.floataa	)
       elif beta_schedule == "linear":
          UpperCAmelCase_							:      int						=   torch.linspace(A__       ,   A__       ,   A__       ,   dtype=torch.floataa	)
       elif beta_schedule == "scaled_linear":
          # this schedule is very specific to the latent diffusion model.
          UpperCAmelCase_							:      Any						=   (
              torch.linspace(beta_start**0.5       ,   beta_end**0.5       ,   A__       ,   dtype=torch.floataa	) ** 2
          )
       elif beta_schedule == "squaredcos_cap_v2":
          # Glide cosine schedule
          UpperCAmelCase_							:      List[str]						=   betas_for_alpha_bar(A__	)
       else:
          raise NotImplementedError(f'''{beta_schedule} does is not implemented for {self.__class__}'''	)

       UpperCAmelCase_							:      Any						=   1.0 - self.betas
       UpperCAmelCase_							:      List[str]						=   torch.cumprod(self.alphas       ,   dim=0	)

       # At every step in inverted ddim, we are looking into the next alphas_cumprod
       # For the final step, there is no next alphas_cumprod, and the index is out of bounds
       # `set_alpha_to_zero` decides whether we set this parameter simply to zero
       # in this case, self.step() just output the predicted noise
       # or whether we use the final alpha of the "non-previous" one.
       UpperCAmelCase_							:      Any						=   torch.tensor(0.0	) if set_alpha_to_zero else self.alphas_cumprod[-1]

       # standard deviation of the initial noise distribution
       UpperCAmelCase_							:      Dict						=   1.0

       # setable values
       UpperCAmelCase_							:      int						=   None
       UpperCAmelCase_							:      List[str]						=   torch.from_numpy(np.arange(0       ,   A__	).copy().astype(np.intaa	)	)





    def 					_lowerCamelCase (	self				:   Dict       ,   __snake_case				:   Any       ,   __snake_case				:   Union[str, Any] = None	):





       '''simple docstring'''
       return sample





    def 					_lowerCamelCase (	self				:   Any       ,   __snake_case				:   Any       ,   __snake_case				:   Optional[Any] = None	):





       '''simple docstring'''

       if num_inference_steps > self.config.num_train_timesteps:
          raise ValueError(
              f'''`num_inference_steps`: {num_inference_steps} cannot be larger than `self.config.train_timesteps`:'''
              f''' {self.config.num_train_timesteps} as the unet model trained with this scheduler can only handle'''
              f''' maximal {self.config.num_train_timesteps} timesteps.'''	)

       UpperCAmelCase_							:      str						=   num_inference_steps
       UpperCAmelCase_							:      List[Any]						=   self.config.num_train_timesteps // self.num_inference_steps
       # creates integer timesteps by multiplying by ratio
       # casting to int to avoid issues when num_inference_step is power of 3
       UpperCAmelCase_							:      Any						=   (np.arange(0       ,   A__	) * step_ratio).round().copy().astype(np.intaa	)
       UpperCAmelCase_							:      Optional[int]						=   torch.from_numpy(A__	).to(A__	)
       self.timesteps += self.config.steps_offset





    def 					_lowerCamelCase (	self				:   Any       ,   __snake_case				:   List[Any]       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Any       ,   __snake_case				:   Union[str, Any] = 0.0       ,   __snake_case				:   Union[str, Any] = False       ,   __snake_case				:   Optional[Any] = None       ,   __snake_case				:   Union[str, Any] = True       ,   ):





       '''simple docstring'''
       # 1. get previous step value (=t+1)
       UpperCAmelCase_							:      Optional[int]						=   timestep + self.config.num_train_timesteps // self.num_inference_steps

       # 2. compute alphas, betas
       # change original implementation to exactly match noise levels for analogous forward process
       UpperCAmelCase_							:      Any						=   self.alphas_cumprod[timestep]
       UpperCAmelCase_							:      List[Any]						=   (
           self.alphas_cumprod[prev_timestep]
           if prev_timestep < self.config.num_train_timesteps
           else self.final_alpha_cumprod
       )

       UpperCAmelCase_							:      Union[str, Any]						=   1 - alpha_prod_t

       # 3. compute predicted original sample from predicted noise also called
       # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
       if self.config.prediction_type == "epsilon":
          UpperCAmelCase_							:      Optional[int]						=   (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5
          UpperCAmelCase_							:      Dict						=   model_output
       elif self.config.prediction_type == "sample":
          UpperCAmelCase_							:      Tuple						=   model_output
          UpperCAmelCase_							:      Tuple						=   (sample - alpha_prod_t ** 0.5 * pred_original_sample) / beta_prod_t ** 0.5
       elif self.config.prediction_type == "v_prediction":
          UpperCAmelCase_							:      Any						=   (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output
          UpperCAmelCase_							:      Tuple						=   (alpha_prod_t**0.5) * model_output + (beta_prod_t**0.5) * sample
       else:
          raise ValueError(
              f'''prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample`, or'''
              ''' `v_prediction`'''	)

       # 4. Clip or threshold "predicted x_0"
       if self.config.clip_sample:
          UpperCAmelCase_							:      List[Any]						=   pred_original_sample.clamp(
              -self.config.clip_sample_range       ,   self.config.clip_sample_range	)

       # 5. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
       UpperCAmelCase_							:      str						=   (1 - alpha_prod_t_prev) ** 0.5 * pred_epsilon

       # 6. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
       UpperCAmelCase_							:      Any						=   alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction

       if not return_dict:
          return (prev_sample, pred_original_sample)
       return DDIMSchedulerOutput(prev_sample=A__       ,   pred_original_sample=A__	)





    def __len__(	self				:   Dict	):





       '''simple docstring'''
       return self.config.num_train_timesteps





import copy

from ...configuration_utils import PretrainedConfig
from ...utils import add_start_docstrings


__UpperCamelCase				:  Dict      					=							R'\n    [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and\n    can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information.\n\n    Args:\n        title_sep (`str`, *optional*, defaults to  `" / "`):\n            Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`].\n        doc_sep (`str`, *optional*, defaults to  `" // "`):\n            Separator inserted between the text of the retrieved document and the original input when calling\n            [`RagRetriever`].\n        n_docs (`int`, *optional*, defaults to 5):\n            Number of documents to retrieve.\n        max_combined_length (`int`, *optional*, defaults to 300):\n            Max length of contextualized input returned by [`~RagRetriever.__call__`].\n        retrieval_vector_size (`int`, *optional*, defaults to 768):\n            Dimensionality of the document embeddings indexed by [`RagRetriever`].\n        retrieval_batch_size (`int`, *optional*, defaults to 8):\n            Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated\n            [`RagRetriever`].\n        dataset (`str`, *optional*, defaults to `"wiki_dpr"`):\n            A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids\n            using `datasets.list_datasets()`).\n        dataset_split (`str`, *optional*, defaults to `"train"`)\n            Which split of the `dataset` to load.\n        index_name (`str`, *optional*, defaults to `"compressed"`)\n            The index name of the index associated with the `dataset`. One can choose between `"legacy"`, `"exact"` and\n            `"compressed"`.\n        index_path (`str`, *optional*)\n            The path to the serialized faiss index on disk.\n        passages_path (`str`, *optional*):\n            A path to text passages compatible with the faiss index. Required if using\n            [`~models.rag.retrieval_rag.LegacyIndex`]\n        use_dummy_dataset (`bool`, *optional*, defaults to `False`)\n            Whether to load a "dummy" variant of the dataset specified by `dataset`.\n        label_smoothing (`float`, *optional*, defaults to 0.0):\n            Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing\n            in the loss calculation. If set to 0, no label smoothing is performed.\n        do_marginalize (`bool`, *optional*, defaults to `False`):\n            If `True`, the logits are marginalized over all documents by making use of\n            `torch.nn.functional.log_softmax`.\n        reduce_loss (`bool`, *optional*, defaults to `False`):\n            Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation.\n        do_deduplication (`bool`, *optional*, defaults to `True`):\n            Whether or not to deduplicate the generations from different context documents for a given input. Has to be\n            set to `False` if used while training with distributed backend.\n        exclude_bos_score (`bool`, *optional*, defaults to `False`):\n            Whether or not to disregard the BOS token when computing the loss.\n        output_retrieved(`bool`, *optional*, defaults to `False`):\n            If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and\n            `context_attention_mask` are returned. See returned tensors for more detail.\n        use_cache (`bool`, *optional*, defaults to `True`):\n            Whether or not the model should return the last key/values attentions (not used by all models).\n        forced_eos_token_id (`int`, *optional*):\n            The id of the token to force as the last generated token when `max_length` is reached. Usually set to\n            `eos_token_id`.\n'





@add_start_docstrings(snake_case__						)
class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    A_  :     Union[str, Any]			= 'rag'
    A_  :     Tuple			= True





    def __init__(	self				:   int       ,   __snake_case				:   List[str]=None       ,   __snake_case				:   List[Any]=True       ,   __snake_case				:   Optional[int]=None       ,   __snake_case				:   Union[str, Any]=None       ,   __snake_case				:   int=None       ,   __snake_case				:   Any=None       ,   __snake_case				:   Optional[int]=None       ,   __snake_case				:   Optional[int]=" / "       ,   __snake_case				:   Any=" // "       ,   __snake_case				:   Tuple=5       ,   __snake_case				:   Union[str, Any]=300       ,   __snake_case				:   Any=768       ,   __snake_case				:   Tuple=8       ,   __snake_case				:   int="wiki_dpr"       ,   __snake_case				:   Optional[int]="train"       ,   __snake_case				:   Tuple="compressed"       ,   __snake_case				:   Optional[int]=None       ,   __snake_case				:   List[Any]=None       ,   __snake_case				:   Optional[int]=False       ,   __snake_case				:   str=False       ,   __snake_case				:   Dict=0.0       ,   __snake_case				:   Union[str, Any]=True       ,   __snake_case				:   Union[str, Any]=False       ,   __snake_case				:   str=False       ,   __snake_case				:   str=False       ,   __snake_case				:   Optional[Any]=True       ,   __snake_case				:   int=None       ,   **__snake_case				:   str       ,   ):





       '''simple docstring'''
       super().__init__(
           bos_token_id=__snake_case       ,   pad_token_id=__snake_case       ,   eos_token_id=__snake_case       ,   decoder_start_token_id=__snake_case       ,   forced_eos_token_id=__snake_case       ,   is_encoder_decoder=__snake_case       ,   prefix=__snake_case       ,   vocab_size=__snake_case       ,   **__snake_case       ,   )
       assert (
           "question_encoder" in kwargs and "generator" in kwargs
       ), "Config has to be initialized with question_encoder and generator config"
       UpperCAmelCase_							:      Dict						=   kwargs.pop('''question_encoder'''	)
       UpperCAmelCase_							:      List[Any]						=   question_encoder_config.pop('''model_type'''	)
       UpperCAmelCase_							:      Any						=   kwargs.pop('''generator'''	)
       UpperCAmelCase_							:      Union[str, Any]						=   decoder_config.pop('''model_type'''	)

       from ..auto.configuration_auto import AutoConfig

       UpperCAmelCase_							:      Any						=   AutoConfig.for_model(__snake_case       ,   **__snake_case	)
       UpperCAmelCase_							:      int						=   AutoConfig.for_model(__snake_case       ,   **__snake_case	)

       UpperCAmelCase_							:      Any						=   reduce_loss
       UpperCAmelCase_							:      List[Any]						=   label_smoothing
       UpperCAmelCase_							:      Tuple						=   exclude_bos_score
       UpperCAmelCase_							:      int						=   do_marginalize

       UpperCAmelCase_							:      Tuple						=   title_sep
       UpperCAmelCase_							:      Union[str, Any]						=   doc_sep
       UpperCAmelCase_							:      Any						=   n_docs
       UpperCAmelCase_							:      Optional[int]						=   max_combined_length

       UpperCAmelCase_							:      Any						=   dataset
       UpperCAmelCase_							:      List[Any]						=   dataset_split
       UpperCAmelCase_							:      Union[str, Any]						=   index_name

       UpperCAmelCase_							:      List[str]						=   retrieval_vector_size
       UpperCAmelCase_							:      Optional[Any]						=   retrieval_batch_size
       UpperCAmelCase_							:      Optional[int]						=   passages_path
       UpperCAmelCase_							:      Optional[Any]						=   index_path
       UpperCAmelCase_							:      List[Any]						=   use_dummy_dataset

       UpperCAmelCase_							:      int						=   output_retrieved

       UpperCAmelCase_							:      int						=   do_deduplication

       UpperCAmelCase_							:      Optional[int]						=   use_cache

       if self.forced_eos_token_id is None:
          UpperCAmelCase_							:      int						=   getattr(self.generator       ,   '''forced_eos_token_id'''       ,   __snake_case	)





    @classmethod
    def 					_lowerCamelCase (	cls				:   Optional[Any]       ,   __snake_case				:   PretrainedConfig       ,   __snake_case				:   PretrainedConfig       ,   **__snake_case				:   str	):





       '''simple docstring'''
       return cls(question_encoder=question_encoder_config.to_dict()       ,   generator=generator_config.to_dict()       ,   **__snake_case	)





    def 					_lowerCamelCase (	self				:   int	):





       '''simple docstring'''
       UpperCAmelCase_							:      Union[str, Any]						=   copy.deepcopy(self.__dict__	)
       UpperCAmelCase_							:      Optional[int]						=   self.question_encoder.to_dict()
       UpperCAmelCase_							:      Dict						=   self.generator.to_dict()
       UpperCAmelCase_							:      Optional[Any]						=   self.__class__.model_type
       return output