






'''simple docstring'''





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

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, logging, randn_tensor
from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin


a_      = logging.get_logger(__name__)  # pylint: disable=invalid-name





@dataclass
# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->EulerDiscrete
class 		SCREAMING_SNAKE_CASE__   (     A__    ):
		_UpperCAmelCase         =42
		_UpperCAmelCase         =None





def 			__UpperCAmelCase				(lowercase__	,lowercase__=0.999	,lowercase__="cosine"	,)		->				Any:

						'''simple docstring'''


						if alpha_transform_type == "cosine":

												def alpha_bar_fn(lowercase__				):
																		return math.cos((t + 0.008) / 1.008 * math.pi / 2				) ** 2

						elif alpha_transform_type == "exp":

												def alpha_bar_fn(lowercase__				):
																		return math.exp(t * -12.0				)

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

						a_				     =   []
						for i in range(lowercase__				):
												a_				     =   i / num_diffusion_timesteps
												a_				     =   (i + 1) / num_diffusion_timesteps
												betas.append(min(1 - alpha_bar_fn(lowercase__				) / alpha_bar_fn(lowercase__				)	,lowercase__				)				)
						return torch.tensor(lowercase__	,dtype=torch.floataa				)





class 		SCREAMING_SNAKE_CASE__   (     A__		,		A__    ):
		_UpperCAmelCase         =[e.name for e in KarrasDiffusionSchedulers]
		_UpperCAmelCase         =1



		@register_to_config
		def __init__(    self:    int				,   a:    int = 10_00				,   a:    float = 0.0001				,   a:    float = 0.02				,   a:    str = "linear"				,   a:    Optional[Union[np.ndarray, List[float]]] = None				,   a:    str = "epsilon"				,   a:    str = "linear"				,   a:    Optional[bool] = False				,   a:    str = "linspace"				,   a:    int = 0				,   )  ->Union[str, Any]:






								'''simple docstring'''


								if trained_betas is not None:
														a_				     =   torch.tensor(_lowerCAmelCase				,   dtype=torch.floataa)
								elif beta_schedule == "linear":
														a_				     =   torch.linspace(_lowerCAmelCase				,   _lowerCAmelCase				,   _lowerCAmelCase				,   dtype=torch.floataa)
								elif beta_schedule == "scaled_linear":
														# this schedule is very specific to the latent diffusion model.
														a_				     =   (
														    torch.linspace(beta_start**0.5				,   beta_end**0.5				,   _lowerCAmelCase				,   dtype=torch.floataa) ** 2
														)
								elif beta_schedule == "squaredcos_cap_v2":
														# Glide cosine schedule
														a_				     =   betas_for_alpha_bar(_lowerCAmelCase)
								else:
														raise NotImplementedError(f"""{beta_schedule} does is not implemented for {self.__class__}""")

								a_				     =   1.0 - self.betas
								a_				     =   torch.cumprod(self.alphas				,   dim=0)

								a_				     =   np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5)
								a_				     =   np.concatenate([sigmas[::-1], [0.0]]).astype(np.floataa)
								a_				     =   torch.from_numpy(_lowerCAmelCase)

								# setable values
								a_				     =   None
								a_				     =   np.linspace(0				,   num_train_timesteps - 1				,   _lowerCAmelCase				,   dtype=_lowerCAmelCase)[::-1].copy()
								a_				     =   torch.from_numpy(_lowerCAmelCase)
								a_				     =   False
								a_				     =   use_karras_sigmas



		@property
		def _lowerCAmelCase     (    self:    Tuple)  ->int:






								'''simple docstring'''


								if self.config.timestep_spacing in ["linspace", "trailing"]:
														return self.sigmas.max()

								return (self.sigmas.max() ** 2 + 1) ** 0.5



		def _lowerCAmelCase     (    self:    Optional[Any]				,   a:    torch.FloatTensor				,   a:    Union[float, torch.FloatTensor])  ->Tuple:






								'''simple docstring'''


								if isinstance(_lowerCAmelCase				,   torch.Tensor):
														a_				     =   timestep.to(self.timesteps.device)
								a_				     =   (self.timesteps == timestep).nonzero().item()
								a_				     =   self.sigmas[step_index]

								a_				     =   sample / ((sigma**2 + 1) ** 0.5)

								a_				     =   True
								return sample



		def _lowerCAmelCase     (    self:    Dict				,   a:    int				,   a:    Union[str, torch.device] = None)  ->List[str]:






								'''simple docstring'''


								a_				     =   num_inference_steps

								# "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891
								if self.config.timestep_spacing == "linspace":
														a_				     =   np.linspace(0				,   self.config.num_train_timesteps - 1				,   _lowerCAmelCase				,   dtype=_lowerCAmelCase)[
														    ::-1
														].copy()
								elif self.config.timestep_spacing == "leading":
														a_				     =   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
														a_				     =   (np.arange(0				,   _lowerCAmelCase) * step_ratio).round()[::-1].copy().astype(_lowerCAmelCase)
														timesteps += self.config.steps_offset
								elif self.config.timestep_spacing == "trailing":
														a_				     =   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
														a_				     =   (np.arange(self.config.num_train_timesteps				,   0				,   -step_ratio)).round().copy().astype(_lowerCAmelCase)
														timesteps -= 1
								else:
														raise ValueError(
														    f"""{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'.""")

								a_				     =   np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5)
								a_				     =   np.log(_lowerCAmelCase)

								if self.config.interpolation_type == "linear":
														a_				     =   np.interp(_lowerCAmelCase				,   np.arange(0				,   len(_lowerCAmelCase))				,   _lowerCAmelCase)
								elif self.config.interpolation_type == "log_linear":
														a_				     =   torch.linspace(np.log(sigmas[-1])				,   np.log(sigmas[0])				,   num_inference_steps + 1).exp()
								else:
														raise ValueError(
														    f"""{self.config.interpolation_type} is not implemented. Please specify interpolation_type to either"""
														    " 'linear' or 'log_linear'")

								if self.use_karras_sigmas:
														a_				     =   self._convert_to_karras(in_sigmas=_lowerCAmelCase				,   num_inference_steps=self.num_inference_steps)
														a_				     =   np.array([self._sigma_to_t(_lowerCAmelCase				,   _lowerCAmelCase) for sigma in sigmas])

								a_				     =   np.concatenate([sigmas, [0.0]]).astype(np.floataa)
								a_				     =   torch.from_numpy(_lowerCAmelCase).to(device=_lowerCAmelCase)
								if str(_lowerCAmelCase).startswith("mps"):
														# mps does not support float64
														a_				     =   torch.from_numpy(_lowerCAmelCase).to(_lowerCAmelCase				,   dtype=torch.floataa)
								else:
														a_				     =   torch.from_numpy(_lowerCAmelCase).to(device=_lowerCAmelCase)



		def _lowerCAmelCase     (    self:    List[str]				,   a:    Tuple				,   a:    Union[str, Any])  ->Any:






								'''simple docstring'''


								a_				     =   np.log(_lowerCAmelCase)

								# get distribution
								a_				     =   log_sigma - log_sigmas[:, np.newaxis]

								# get sigmas range
								a_				     =   np.cumsum((dists >= 0)				,   axis=0).argmax(axis=0).clip(max=log_sigmas.shape[0] - 2)
								a_				     =   low_idx + 1

								a_				     =   log_sigmas[low_idx]
								a_				     =   log_sigmas[high_idx]

								# interpolate sigmas
								a_				     =   (low - log_sigma) / (low - high)
								a_				     =   np.clip(_lowerCAmelCase				,   0				,   1)

								# transform interpolation to time range
								a_				     =   (1 - w) * low_idx + w * high_idx
								a_				     =   t.reshape(sigma.shape)
								return t



		def _lowerCAmelCase     (    self:    List[Any]				,   a:    torch.FloatTensor				,   a:    List[Any])  ->List[Any]:






								'''simple docstring'''


								a_				     =   in_sigmas[-1].item()
								a_				     =   in_sigmas[0].item()

								a_				     =   7.0  # 7.0 is the value used in the paper
								a_				     =   np.linspace(0				,   1				,   _lowerCAmelCase)
								a_				     =   sigma_min ** (1 / rho)
								a_				     =   sigma_max ** (1 / rho)
								a_				     =   (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho
								return sigmas



		def _lowerCAmelCase     (    self:    Optional[int]				,   a:    torch.FloatTensor				,   a:    Union[float, torch.FloatTensor]				,   a:    torch.FloatTensor				,   a:    float = 0.0				,   a:    float = 0.0				,   a:    float = float("inf")				,   a:    float = 1.0				,   a:    Optional[torch.Generator] = None				,   a:    bool = True				,   )  ->Optional[Any]:






								'''simple docstring'''


								if (
								    isinstance(_lowerCAmelCase				,   _lowerCAmelCase)
								    or isinstance(_lowerCAmelCase				,   torch.IntTensor)
								    or isinstance(_lowerCAmelCase				,   torch.LongTensor)
								):
														raise ValueError(
														    (
														        "Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to"
														        " `EulerDiscreteScheduler.step()` is not supported. Make sure to pass"
														        " one of the `scheduler.timesteps` as a timestep."
														    )				,   )

								if not self.is_scale_input_called:
														logger.warning(
														    "The `scale_model_input` function should be called before `step` to ensure correct denoising. "
														    "See `StableDiffusionPipeline` for a usage example.")

								if isinstance(_lowerCAmelCase				,   torch.Tensor):
														a_				     =   timestep.to(self.timesteps.device)

								a_				     =   (self.timesteps == timestep).nonzero().item()
								a_				     =   self.sigmas[step_index]

								a_				     =   min(s_churn / (len(self.sigmas) - 1)				,   2**0.5 - 1) if s_tmin <= sigma <= s_tmax else 0.0

								a_				     =   randn_tensor(
								    model_output.shape				,   dtype=model_output.dtype				,   device=model_output.device				,   generator=_lowerCAmelCase)

								a_				     =   noise * s_noise
								a_				     =   sigma * (gamma + 1)

								if gamma > 0:
														a_				     =   sample + eps * (sigma_hat**2 - sigma**2) ** 0.5

								# 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
								# NOTE: "original_sample" should not be an expected prediction_type but is left in for
								# backwards compatibility
								if self.config.prediction_type == "original_sample" or self.config.prediction_type == "sample":
														a_				     =   model_output
								elif self.config.prediction_type == "epsilon":
														a_				     =   sample - sigma_hat * model_output
								elif self.config.prediction_type == "v_prediction":
														# * c_out + input * c_skip
														a_				     =   model_output * (-sigma / (sigma**2 + 1) ** 0.5) + (sample / (sigma**2 + 1))
								else:
														raise ValueError(
														    f"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`""")

								# 2. Convert to an ODE derivative
								a_				     =   (sample - pred_original_sample) / sigma_hat

								a_				     =   self.sigmas[step_index + 1] - sigma_hat

								a_				     =   sample + derivative * dt

								if not return_dict:
														return (prev_sample,)

								return EulerDiscreteSchedulerOutput(prev_sample=_lowerCAmelCase				,   pred_original_sample=_lowerCAmelCase)



		def _lowerCAmelCase     (    self:    Dict				,   a:    torch.FloatTensor				,   a:    torch.FloatTensor				,   a:    torch.FloatTensor				,   )  ->List[Any]:






								'''simple docstring'''


								a_				     =   self.sigmas.to(device=original_samples.device				,   dtype=original_samples.dtype)
								if original_samples.device.type == "mps" and torch.is_floating_point(_lowerCAmelCase):
														# mps does not support float64
														a_				     =   self.timesteps.to(original_samples.device				,   dtype=torch.floataa)
														a_				     =   timesteps.to(original_samples.device				,   dtype=torch.floataa)
								else:
														a_				     =   self.timesteps.to(original_samples.device)
														a_				     =   timesteps.to(original_samples.device)

								a_				     =   [(schedule_timesteps == t).nonzero().item() for t in timesteps]

								a_				     =   sigmas[step_indices].flatten()
								while len(sigma.shape) < len(original_samples.shape):
														a_				     =   sigma.unsqueeze(-1)

								a_				     =   original_samples + noise * sigma
								return noisy_samples




		def __len__(    self:    Any)  ->int:






								'''simple docstring'''


								return self.config.num_train_timesteps














'''simple docstring'''





import argparse
import collections
from pathlib import Path

import torch
from torch.serialization import default_restore_location

from transformers import BertConfig, DPRConfig, DPRContextEncoder, DPRQuestionEncoder, DPRReader


a_      = collections.namedtuple(
    'CheckpointState', ['model_dict', 'optimizer_dict', 'scheduler_dict', 'offset', 'epoch', 'encoder_params']
)



def 			__UpperCAmelCase				(lowercase__				)		->				CheckpointState:

						'''simple docstring'''


						print(F"""Reading saved model from {model_file}"""				)
						a_				     =   torch.load(lowercase__	,map_location=lambda lowercase__	,lowercase__				: default_restore_location(lowercase__	,"cpu"				)				)
						return CheckpointState(**lowercase__				)





class 		SCREAMING_SNAKE_CASE__   :



		def __init__(    self:    int				,   a:    Path)  ->str:






								'''simple docstring'''


								a_				     =   src_file



		def _lowerCAmelCase     (    self:    Dict)  ->Optional[Any]:






								'''simple docstring'''


								raise NotImplementedError




		@staticmethod
		def _lowerCAmelCase     (    a:    str				,   *a:    int				,   **a:    int)  ->"DPRState":






								'''simple docstring'''


								if comp_type.startswith("c"):
														return DPRContextEncoderState(*a				,   **a)
								if comp_type.startswith("q"):
														return DPRQuestionEncoderState(*a				,   **a)
								if comp_type.startswith("r"):
														return DPRReaderState(*a				,   **a)
								else:
														raise ValueError("Component type must be either 'ctx_encoder', 'question_encoder' or 'reader'.")





class 		SCREAMING_SNAKE_CASE__   (     lowercase_    ):



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






								'''simple docstring'''


								a_				     =   DPRContextEncoder(DPRConfig(**BertConfig.get_config_dict("bert-base-uncased")[0]))
								print(f"""Loading DPR biencoder from {self.src_file}""")
								a_				     =   load_states_from_checkpoint(self.src_file)
								a_     ,       a_				     =   model.ctx_encoder, "ctx_model."
								# Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
								a_				     =   {"bert_model.embeddings.position_ids": model.ctx_encoder.bert_model.embeddings.position_ids}
								for key, value in saved_state.model_dict.items():
														if key.startswith(a):
																				a_				     =   key[len(a) :]
																				if not key.startswith("encode_proj."):
																										a_				     =   "bert_model." + key
																				a_				     =   value
								encoder.load_state_dict(a)
								return model





class 		SCREAMING_SNAKE_CASE__   (     lowercase_    ):



		def _lowerCAmelCase     (    self:    Optional[int])  ->Optional[int]:






								'''simple docstring'''


								a_				     =   DPRQuestionEncoder(DPRConfig(**BertConfig.get_config_dict("bert-base-uncased")[0]))
								print(f"""Loading DPR biencoder from {self.src_file}""")
								a_				     =   load_states_from_checkpoint(self.src_file)
								a_     ,       a_				     =   model.question_encoder, "question_model."
								# Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
								a_				     =   {"bert_model.embeddings.position_ids": model.question_encoder.bert_model.embeddings.position_ids}
								for key, value in saved_state.model_dict.items():
														if key.startswith(a):
																				a_				     =   key[len(a) :]
																				if not key.startswith("encode_proj."):
																										a_				     =   "bert_model." + key
																				a_				     =   value
								encoder.load_state_dict(a)
								return model





class 		SCREAMING_SNAKE_CASE__   (     lowercase_    ):



		def _lowerCAmelCase     (    self:    Tuple)  ->Optional[Any]:






								'''simple docstring'''


								a_				     =   DPRReader(DPRConfig(**BertConfig.get_config_dict("bert-base-uncased")[0]))
								print(f"""Loading DPR reader from {self.src_file}""")
								a_				     =   load_states_from_checkpoint(self.src_file)
								# Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
								a_				     =   {
								    "encoder.bert_model.embeddings.position_ids": model.span_predictor.encoder.bert_model.embeddings.position_ids
								}
								for key, value in saved_state.model_dict.items():
														if key.startswith("encoder.") and not key.startswith("encoder.encode_proj"):
																				a_				     =   "encoder.bert_model." + key[len("encoder.") :]
														a_				     =   value
								model.span_predictor.load_state_dict(a)
								return model





def 			__UpperCAmelCase				(lowercase__	,lowercase__	,lowercase__				)		->				Any:

						'''simple docstring'''


						a_				     =   Path(lowercase__				)
						dest_dir.mkdir(exist_ok=lowercase__				)

						a_				     =   DPRState.from_type(lowercase__	,src_file=lowercase__				)
						a_				     =   dpr_state.load_dpr_model()
						model.save_pretrained(lowercase__				)
						model.from_pretrained(lowercase__				)  # sanity check


if __name__ == "__main__":
							a_      = argparse.ArgumentParser()
							# Required parameters
							parser.add_argument(
							    '--type', type=str, help='Type of the component to convert: \'ctx_encoder\', \'question_encoder\' or \'reader\'.'
							)
							parser.add_argument(
							    '--src',
							    type=str,
							    help=(
							        'Path to the dpr checkpoint file. They can be downloaded from the official DPR repo'
							        ' https://github.com/facebookresearch/DPR. Note that in the official repo, both encoders are stored in the'
							        ' \'retriever\' checkpoints.'
							    ),
							)
							parser.add_argument('--dest', type=str, default=None, help='Path to the output PyTorch model directory.')
							a_      = parser.parse_args()

							a_      = Path(args.src)
							a_      = F'converted-{src_file.name}' if args.dest is None else args.dest
							a_      = Path(dest_dir)
							assert src_file.exists()
							assert (
							    args.type is not None
							), "Please specify the component type of the DPR model to convert: 'ctx_encoder', 'question_encoder' or 'reader'."
							convert(args.type, src_file, dest_dir)



