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


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






@dataclass
# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->EulerAncestralDiscrete
class        UpperCAmelCase_       (						A							):

 '''simple docstring'''
 lowercase_				:						torch.FloatTensor
 lowercase_				:						Optional[torch.FloatTensor]				=       None







def snake_case_    (				lowercase__      ,					lowercase__=0.999      ,					lowercase__="cosine"      ,					):
  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}"""		)

  UpperCAmelCase__			:				Optional[int]												=       []
  for i in range(lowercase__		):
    UpperCAmelCase__			:				Optional[int]												=       i / num_diffusion_timesteps
    UpperCAmelCase__			:				Optional[Any]												=       (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        UpperCAmelCase_       (						A				,							A							):

 '''simple docstring'''
 lowercase_				:						Any				=       [e.name for e in KarrasDiffusionSchedulers]
 lowercase_				:						Optional[Any]				=       1







 @register_to_config
 def __init__(				self		:     Dict ,							snake_case__		:     int = 10_00 ,							snake_case__		:     float = 0.0001 ,							snake_case__		:     float = 0.02 ,							snake_case__		:     str = "linear" ,							snake_case__		:     Optional[Union[np.ndarray, List[float]]] = None ,							snake_case__		:     str = "epsilon" ,							snake_case__		:     str = "linspace" ,							snake_case__		:     int = 0 ,							):



   '''simple docstring'''




   if trained_betas is not None:
     UpperCAmelCase__			:				Any												=       torch.tensor(snake_case__ ,							dtype=torch.floataa   )
   elif beta_schedule == "linear":
     UpperCAmelCase__			:				Optional[Any]												=       torch.linspace(snake_case__ ,							snake_case__ ,							snake_case__ ,							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 ,							snake_case__ ,							dtype=torch.floataa   ) ** 2
     )
   elif beta_schedule == "squaredcos_cap_v2":
     # Glide cosine schedule
     UpperCAmelCase__			:				Optional[Any]												=       betas_for_alpha_bar(snake_case__   )
   else:
     raise NotImplementedError(F"""{beta_schedule} does is not implemented for {self.__class__}"""   )

   UpperCAmelCase__			:				Any												=       1.0 - self.betas
   UpperCAmelCase__			:				Dict												=       torch.cumprod(self.alphas ,							dim=0   )

   UpperCAmelCase__			:				Any												=       np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5   )
   UpperCAmelCase__			:				Union[str, Any]												=       np.concatenate([sigmas[::-1], [0.0]]   ).astype(np.floataa   )
   UpperCAmelCase__			:				Any												=       torch.from_numpy(snake_case__   )

   # setable values
   UpperCAmelCase__			:				Any												=       None
   UpperCAmelCase__			:				List[str]												=       np.linspace(0 ,							num_train_timesteps - 1 ,							snake_case__ ,							dtype=snake_case__   )[::-1].copy()
   UpperCAmelCase__			:				str												=       torch.from_numpy(snake_case__   )
   UpperCAmelCase__			:				Dict												=       False







 @property
 def        UpperCamelCase     (				self		:     Optional[int]   ):



   '''simple docstring'''




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

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







 def        UpperCamelCase     (				self		:     Optional[int] ,							snake_case__		:     torch.FloatTensor ,							snake_case__		:     Union[float, torch.FloatTensor]   ):



   '''simple docstring'''




   if isinstance(snake_case__ ,							torch.Tensor   ):
     UpperCAmelCase__			:				Union[str, Any]												=       timestep.to(self.timesteps.device   )
   UpperCAmelCase__			:				Dict												=       (self.timesteps == timestep).nonzero().item()
   UpperCAmelCase__			:				Tuple												=       self.sigmas[step_index]
   UpperCAmelCase__			:				Any												=       sample / ((sigma**2 + 1) ** 0.5)
   UpperCAmelCase__			:				Optional[Any]												=       True
   return sample







 def        UpperCamelCase     (				self		:     Tuple ,							snake_case__		:     int ,							snake_case__		:     Union[str, torch.device] = None   ):



   '''simple docstring'''




   UpperCAmelCase__			:				str												=       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":
     UpperCAmelCase__			:				Any												=       np.linspace(0 ,							self.config.num_train_timesteps - 1 ,							snake_case__ ,							dtype=snake_case__   )[
         ::-1
     ].copy()
   elif self.config.timestep_spacing == "leading":
     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__			:				List[str]												=       (np.arange(0 ,							snake_case__   ) * step_ratio).round()[::-1].copy().astype(snake_case__   )
     timesteps += self.config.steps_offset
   elif self.config.timestep_spacing == "trailing":
     UpperCAmelCase__			:				Optional[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__			:				List[Any]												=       (np.arange(self.config.num_train_timesteps ,							0 ,							-step_ratio   )).round().copy().astype(snake_case__   )
     timesteps -= 1
   else:
     raise ValueError(
         F"""{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'."""   )

   UpperCAmelCase__			:				str												=       np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5   )
   UpperCAmelCase__			:				int												=       np.interp(snake_case__ ,							np.arange(0 ,							len(snake_case__   )   ) ,							snake_case__   )
   UpperCAmelCase__			:				Optional[int]												=       np.concatenate([sigmas, [0.0]]   ).astype(np.floataa   )
   UpperCAmelCase__			:				Optional[int]												=       torch.from_numpy(snake_case__   ).to(device=snake_case__   )
   if str(snake_case__   ).startswith("mps"   ):
     # mps does not support float64
     UpperCAmelCase__			:				int												=       torch.from_numpy(snake_case__   ).to(snake_case__ ,							dtype=torch.floataa   )
   else:
     UpperCAmelCase__			:				List[Any]												=       torch.from_numpy(snake_case__   ).to(device=snake_case__   )







 def        UpperCamelCase     (				self		:     Any ,							snake_case__		:     torch.FloatTensor ,							snake_case__		:     Union[float, torch.FloatTensor] ,							snake_case__		:     torch.FloatTensor ,							snake_case__		:     Optional[torch.Generator] = None ,							snake_case__		:     bool = True ,							):



   '''simple docstring'''




   if (
       isinstance(snake_case__ ,							snake_case__   )
       or isinstance(snake_case__ ,							torch.IntTensor   )
       or isinstance(snake_case__ ,							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(snake_case__ ,							torch.Tensor   ):
     UpperCAmelCase__			:				int												=       timestep.to(self.timesteps.device   )

   UpperCAmelCase__			:				str												=       (self.timesteps == timestep).nonzero().item()
   UpperCAmelCase__			:				List[str]												=       self.sigmas[step_index]

   # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
   if self.config.prediction_type == "epsilon":
     UpperCAmelCase__			:				Union[str, Any]												=       sample - sigma * model_output
   elif self.config.prediction_type == "v_prediction":
     # * c_out + input * c_skip
     UpperCAmelCase__			:				Optional[int]												=       model_output * (-sigma / (sigma**2 + 1) ** 0.5) + (sample / (sigma**2 + 1))
   elif self.config.prediction_type == "sample":
     raise NotImplementedError("prediction_type not implemented yet: sample"   )
   else:
     raise ValueError(
         F"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`"""   )

   UpperCAmelCase__			:				Optional[Any]												=       self.sigmas[step_index]
   UpperCAmelCase__			:				List[str]												=       self.sigmas[step_index + 1]
   UpperCAmelCase__			:				List[str]												=       (sigma_to**2 * (sigma_from**2 - sigma_to**2) / sigma_from**2) ** 0.5
   UpperCAmelCase__			:				Optional[int]												=       (sigma_to**2 - sigma_up**2) ** 0.5

   # 2. Convert to an ODE derivative
   UpperCAmelCase__			:				Tuple												=       (sample - pred_original_sample) / sigma

   UpperCAmelCase__			:				int												=       sigma_down - sigma

   UpperCAmelCase__			:				List[Any]												=       sample + derivative * dt

   UpperCAmelCase__			:				List[Any]												=       model_output.device
   UpperCAmelCase__			:				Optional[Any]												=       randn_tensor(model_output.shape ,							dtype=model_output.dtype ,							device=snake_case__ ,							generator=snake_case__   )

   UpperCAmelCase__			:				Dict												=       prev_sample + noise * sigma_up

   if not return_dict:
     return (prev_sample,)

   return EulerAncestralDiscreteSchedulerOutput(
       prev_sample=snake_case__ ,							pred_original_sample=snake_case__   )







 def        UpperCamelCase     (				self		:     str ,							snake_case__		:     torch.FloatTensor ,							snake_case__		:     torch.FloatTensor ,							snake_case__		:     torch.FloatTensor ,							):



   '''simple docstring'''




   UpperCAmelCase__			:				Optional[Any]												=       self.sigmas.to(device=original_samples.device ,							dtype=original_samples.dtype   )
   if original_samples.device.type == "mps" and torch.is_floating_point(snake_case__   ):
     # mps does not support float64
     UpperCAmelCase__			:				Union[str, Any]												=       self.timesteps.to(original_samples.device ,							dtype=torch.floataa   )
     UpperCAmelCase__			:				Dict												=       timesteps.to(original_samples.device ,							dtype=torch.floataa   )
   else:
     UpperCAmelCase__			:				List[str]												=       self.timesteps.to(original_samples.device   )
     UpperCAmelCase__			:				str												=       timesteps.to(original_samples.device   )

   UpperCAmelCase__			:				Any												=       [(schedule_timesteps == t).nonzero().item() for t in timesteps]

   UpperCAmelCase__			:				List[str]												=       sigmas[step_indices].flatten()
   while len(sigma.shape   ) < len(original_samples.shape   ):
     UpperCAmelCase__			:				Optional[Any]												=       sigma.unsqueeze(-1   )

   UpperCAmelCase__			:				List[Any]												=       original_samples + noise * sigma
   return noisy_samples







 def __len__(				self		:     Tuple   ):



   '''simple docstring'''




   return self.config.num_train_timesteps





'''simple docstring'''





from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


SCREAMING_SNAKE_CASE    =					logging.get_logger(__name__)

SCREAMING_SNAKE_CASE    =					{
    """gpt2""": """https://huggingface.co/gpt2/resolve/main/config.json""",
    """gpt2-medium""": """https://huggingface.co/gpt2-medium/resolve/main/config.json""",
    """gpt2-large""": """https://huggingface.co/gpt2-large/resolve/main/config.json""",
    """gpt2-xl""": """https://huggingface.co/gpt2-xl/resolve/main/config.json""",
    """distilgpt2""": """https://huggingface.co/distilgpt2/resolve/main/config.json""",
}






class        UpperCAmelCase_       (						A							):

 '''simple docstring'''
 lowercase_				:						Optional[int]				=       "gpt2"
 lowercase_				:						int				=       ["past_key_values"]
 lowercase_				:						Any				=       {
     "hidden_size": "n_embd",
     "max_position_embeddings": "n_positions",
     "num_attention_heads": "n_head",
     "num_hidden_layers": "n_layer",
 }







 def __init__(				self		:     Union[str, Any] ,							snake_case__		:     Optional[int]=5_02_57 ,							snake_case__		:     List[Any]=10_24 ,							snake_case__		:     Tuple=7_68 ,							snake_case__		:     List[Any]=12 ,							snake_case__		:     List[str]=12 ,							snake_case__		:     Union[str, Any]=None ,							snake_case__		:     str="gelu_new" ,							snake_case__		:     Any=0.1 ,							snake_case__		:     Union[str, Any]=0.1 ,							snake_case__		:     Tuple=0.1 ,							snake_case__		:     List[str]=1e-5 ,							snake_case__		:     Optional[int]=0.02 ,							snake_case__		:     int="cls_index" ,							snake_case__		:     Tuple=True ,							snake_case__		:     List[Any]=None ,							snake_case__		:     Optional[int]=True ,							snake_case__		:     Optional[Any]=0.1 ,							snake_case__		:     Tuple=True ,							snake_case__		:     int=True ,							snake_case__		:     Optional[int]=5_02_56 ,							snake_case__		:     List[Any]=5_02_56 ,							snake_case__		:     Union[str, Any]=False ,							snake_case__		:     Optional[int]=False ,							**snake_case__		:     List[Any] ,							):



   '''simple docstring'''




   UpperCAmelCase__			:				List[str]												=       vocab_size
   UpperCAmelCase__			:				Any												=       n_positions
   UpperCAmelCase__			:				str												=       n_embd
   UpperCAmelCase__			:				List[Any]												=       n_layer
   UpperCAmelCase__			:				Any												=       n_head
   UpperCAmelCase__			:				Any												=       n_inner
   UpperCAmelCase__			:				Tuple												=       activation_function
   UpperCAmelCase__			:				List[str]												=       resid_pdrop
   UpperCAmelCase__			:				Union[str, Any]												=       embd_pdrop
   UpperCAmelCase__			:				int												=       attn_pdrop
   UpperCAmelCase__			:				Optional[Any]												=       layer_norm_epsilon
   UpperCAmelCase__			:				Optional[Any]												=       initializer_range
   UpperCAmelCase__			:				Optional[Any]												=       summary_type
   UpperCAmelCase__			:				Tuple												=       summary_use_proj
   UpperCAmelCase__			:				int												=       summary_activation
   UpperCAmelCase__			:				List[str]												=       summary_first_dropout
   UpperCAmelCase__			:				Optional[Any]												=       summary_proj_to_labels
   UpperCAmelCase__			:				Optional[Any]												=       scale_attn_weights
   UpperCAmelCase__			:				List[Any]												=       use_cache
   UpperCAmelCase__			:				Union[str, Any]												=       scale_attn_by_inverse_layer_idx
   UpperCAmelCase__			:				Tuple												=       reorder_and_upcast_attn

   UpperCAmelCase__			:				Dict												=       bos_token_id
   UpperCAmelCase__			:				Union[str, Any]												=       eos_token_id

   super().__init__(bos_token_id=snake_case__ ,							eos_token_id=snake_case__ ,							**snake_case__   )






class        UpperCAmelCase_       (						A							):

 '''simple docstring'''







 def __init__(				self		:     Union[str, Any] ,							snake_case__		:     PretrainedConfig ,							snake_case__		:     str = "default" ,							snake_case__		:     List[PatchingSpec] = None ,							snake_case__		:     bool = False ,							):



   '''simple docstring'''




   super().__init__(snake_case__ ,							task=snake_case__ ,							patching_specs=snake_case__ ,							use_past=snake_case__   )
   if not getattr(self._config ,							"pad_token_id" ,							snake_case__   ):
     # TODO: how to do that better?
     UpperCAmelCase__			:				List[str]												=       0







 @property
 def        UpperCamelCase     (				self		:     Any   ):



   '''simple docstring'''




   UpperCAmelCase__			:				int												=       OrderedDict({"input_ids": {0: "batch", 1: "sequence"}}   )
   if self.use_past:
     self.fill_with_past_key_values_(snake_case__ ,							direction="inputs"   )
     UpperCAmelCase__			:				Dict												=       {0: "batch", 1: "past_sequence + sequence"}
   else:
     UpperCAmelCase__			:				Dict												=       {0: "batch", 1: "sequence"}

   return common_inputs







 @property
 def        UpperCamelCase     (				self		:     Any   ):



   '''simple docstring'''




   return self._config.n_layer







 @property
 def        UpperCamelCase     (				self		:     List[str]   ):



   '''simple docstring'''




   return self._config.n_head







 def        UpperCamelCase     (				self		:     int ,							snake_case__		:     PreTrainedTokenizer ,							snake_case__		:     int = -1 ,							snake_case__		:     int = -1 ,							snake_case__		:     bool = False ,							snake_case__		:     Optional[TensorType] = None ,							):



   '''simple docstring'''




   UpperCAmelCase__			:				Union[str, Any]												=       super(snake_case__ ,							self   ).generate_dummy_inputs(
       snake_case__ ,							batch_size=snake_case__ ,							seq_length=snake_case__ ,							is_pair=snake_case__ ,							framework=snake_case__   )

   # We need to order the input in the way they appears in the forward()
   UpperCAmelCase__			:				Dict												=       OrderedDict({"input_ids": common_inputs["input_ids"]}   )

   # Need to add the past_keys
   if self.use_past:
     if not is_torch_available():
       raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed."   )
     else:
       import torch

       UpperCAmelCase__					, UpperCAmelCase__			:				int												=       common_inputs["input_ids"].shape
       # Not using the same length for past_key_values
       UpperCAmelCase__			:				int												=       seqlen + 2
       UpperCAmelCase__			:				Union[str, Any]												=       (
           batch,
           self.num_attention_heads,
           past_key_values_length,
           self._config.hidden_size // self.num_attention_heads,
       )
       UpperCAmelCase__			:				Optional[Any]												=       [
           (torch.zeros(snake_case__   ), torch.zeros(snake_case__   )) for _ in range(self.num_layers   )
       ]

   UpperCAmelCase__			:				List[str]												=       common_inputs["attention_mask"]
   if self.use_past:
     UpperCAmelCase__			:				List[Any]												=       ordered_inputs["attention_mask"].dtype
     UpperCAmelCase__			:				Optional[int]												=       torch.cat(
         [ordered_inputs["attention_mask"], torch.ones(snake_case__ ,							snake_case__ ,							dtype=snake_case__   )] ,							dim=1   )

   return ordered_inputs







 @property
 def        UpperCamelCase     (				self		:     Union[str, Any]   ):



   '''simple docstring'''




   return 13



