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 SCREAMING_SNAKE_CASE_		(			__lowerCAmelCase		):
   __lowerCAmelCase											=							42
   __lowerCAmelCase											=							None





def  lowercase(  UpperCamelCase_       ,				UpperCamelCase_=0.9_9_9       ,				UpperCamelCase_="cosine"       ,				)		->  Tuple:







 '''simple docstring'''
 if alpha_transform_type == "cosine":

  def alpha_bar_fn(UpperCamelCase_					):
   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(UpperCamelCase_					):
   return math.exp(t * -1_2.0					)

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

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


class SCREAMING_SNAKE_CASE_		(			__lowerCAmelCase     ,    __lowerCAmelCase		):
   __lowerCAmelCase											=							[e.name for e in KarrasDiffusionSchedulers]
   __lowerCAmelCase											=							1







   @register_to_config
   def __init__(    self	:	int      ,      lowerCamelCase_	:	int = 1000      ,      lowerCamelCase_	:	float = 0.0_0_0_1      ,      lowerCamelCase_	:	float = 0.0_2      ,      lowerCamelCase_	:	str = "linear"      ,      lowerCamelCase_	:	Optional[Union[np.ndarray, List[float]]] = None      ,      lowerCamelCase_	:	str = "epsilon"      ,      lowerCamelCase_	:	str = "linspace"      ,      lowerCamelCase_	:	int = 0      ,      ):





    """simple docstring"""


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

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

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

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







   @property
   def 	lowerCamelCase_  (    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 	lowerCamelCase_  (    self	:	List[Any]      ,      lowerCamelCase_	:	torch.FloatTensor      ,      lowerCamelCase_	:	Union[float, torch.FloatTensor]							):





    """simple docstring"""


    if isinstance(lowerCamelCase_      ,      torch.Tensor							):
     UpperCamelCase      =			timestep.to(self.timesteps.device							)
    UpperCamelCase      =			(self.timesteps == timestep).nonzero().item()
    UpperCamelCase      =			self.sigmas[step_index]
    UpperCamelCase      =			sample / ((sigma**2 + 1) ** 0.5)
    UpperCamelCase      =			True
    return sample







   def 	lowerCamelCase_  (    self	:	List[str]      ,      lowerCamelCase_	:	int      ,      lowerCamelCase_	:	Union[str, torch.device] = None							):





    """simple docstring"""


    UpperCamelCase      =			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      =			np.linspace(0      ,      self.config.num_train_timesteps - 1      ,      lowerCamelCase_      ,      dtype=lowerCamelCase_							)[
         ::-1
     ].copy()
    elif self.config.timestep_spacing == "leading":
     UpperCamelCase      =			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      =			(np.arange(0      ,      lowerCamelCase_							) * step_ratio).round()[::-1].copy().astype(lowerCamelCase_							)
     timesteps += self.config.steps_offset
    elif self.config.timestep_spacing == "trailing":
     UpperCamelCase      =			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      =			(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'."""							)

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







   def 	lowerCamelCase_  (    self	:	str      ,      lowerCamelCase_	:	torch.FloatTensor      ,      lowerCamelCase_	:	Union[float, torch.FloatTensor]      ,      lowerCamelCase_	:	torch.FloatTensor      ,      lowerCamelCase_	:	Optional[torch.Generator] = None      ,      lowerCamelCase_	:	bool = True      ,      ):





    """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							):
     UpperCamelCase      =			timestep.to(self.timesteps.device							)

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

    # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
    if self.config.prediction_type == "epsilon":
     UpperCamelCase      =			sample - sigma * model_output
    elif self.config.prediction_type == "v_prediction":
     # * c_out + input * c_skip
     UpperCamelCase      =			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      =			self.sigmas[step_index]
    UpperCamelCase      =			self.sigmas[step_index + 1]
    UpperCamelCase      =			(sigma_to**2 * (sigma_from**2 - sigma_to**2) / sigma_from**2) ** 0.5
    UpperCamelCase      =			(sigma_to**2 - sigma_up**2) ** 0.5

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

    UpperCamelCase      =			sigma_down - sigma

    UpperCamelCase      =			sample + derivative * dt

    UpperCamelCase      =			model_output.device
    UpperCamelCase      =			randn_tensor(model_output.shape      ,      dtype=model_output.dtype      ,      device=lowerCamelCase_      ,      generator=lowerCamelCase_							)

    UpperCamelCase      =			prev_sample + noise * sigma_up

    if not return_dict:
     return (prev_sample,)

    return EulerAncestralDiscreteSchedulerOutput(
        prev_sample=lowerCamelCase_      ,      pred_original_sample=lowerCamelCase_							)







   def 	lowerCamelCase_  (    self	:	Dict      ,      lowerCamelCase_	:	torch.FloatTensor      ,      lowerCamelCase_	:	torch.FloatTensor      ,      lowerCamelCase_	:	torch.FloatTensor      ,      ):





    """simple docstring"""


    UpperCamelCase      =			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
     UpperCamelCase      =			self.timesteps.to(original_samples.device      ,      dtype=torch.floataa							)
     UpperCamelCase      =			timesteps.to(original_samples.device      ,      dtype=torch.floataa							)
    else:
     UpperCamelCase      =			self.timesteps.to(original_samples.device							)
     UpperCamelCase      =			timesteps.to(original_samples.device							)

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

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

    UpperCamelCase      =			original_samples + noise * sigma
    return noisy_samples







   def __len__(    self	:	Optional[Any]							):





    """simple docstring"""


    return self.config.num_train_timesteps

import unittest

from transformers.testing_utils import is_torch_available, require_torch, tooslow

from ...generation.test_utils import torch_device
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import (
         CpmAntConfig,
         CpmAntForCausalLM,
         CpmAntModel,
         CpmAntTokenizer,
     )
@require_torch
class SCREAMING_SNAKE_CASE_		:







   def __init__(    self	:	Optional[int]      ,      lowerCamelCase_	:	List[str]      ,      lowerCamelCase_	:	Tuple=2      ,      lowerCamelCase_	:	List[Any]=8      ,      lowerCamelCase_	:	Any=True      ,      lowerCamelCase_	:	int=False      ,      lowerCamelCase_	:	Optional[Any]=False      ,      lowerCamelCase_	:	Optional[int]=False      ,      lowerCamelCase_	:	Any=False      ,      lowerCamelCase_	:	List[Any]=99      ,      lowerCamelCase_	:	Optional[int]=32      ,      lowerCamelCase_	:	List[str]=3      ,      lowerCamelCase_	:	List[str]=4      ,      lowerCamelCase_	:	Tuple=37      ,      lowerCamelCase_	:	str=32      ,      lowerCamelCase_	:	Any=128      ,      lowerCamelCase_	:	List[str]=8      ,      lowerCamelCase_	:	Union[str, Any]=8      ,      lowerCamelCase_	:	Any=8      ,      lowerCamelCase_	:	Optional[int]=1.0      ,      lowerCamelCase_	:	int=True      ,      ):





    """simple docstring"""


    UpperCamelCase      =			parent
    UpperCamelCase      =			batch_size
    UpperCamelCase      =			seq_length
    UpperCamelCase      =			is_training
    UpperCamelCase      =			use_token_type_ids
    UpperCamelCase      =			use_input_mask
    UpperCamelCase      =			use_labels
    UpperCamelCase      =			use_mc_token_ids
    UpperCamelCase      =			vocab_size
    UpperCamelCase      =			hidden_size
    UpperCamelCase      =			num_hidden_layers
    UpperCamelCase      =			num_attention_heads
    UpperCamelCase      =			intermediate_size
    UpperCamelCase      =			num_buckets
    UpperCamelCase      =			max_distance
    UpperCamelCase      =			prompt_length
    UpperCamelCase      =			prompt_types
    UpperCamelCase      =			segment_types
    UpperCamelCase      =			init_std
    UpperCamelCase      =			return_dict







   def 	lowerCamelCase_  (    self	:	List[str]							):





    """simple docstring"""


    UpperCamelCase      =			{}
    UpperCamelCase      =			ids_tensor([self.batch_size, self.seq_length]      ,      self.vocab_size							).type(torch.intaa							)
    UpperCamelCase      =			False

    UpperCamelCase      =			self.get_config()

    return (config, input_ids)







   def 	lowerCamelCase_  (    self	:	Optional[Any]							):





    """simple docstring"""


    return CpmAntConfig(
        vocab_size=self.vocab_size      ,      hidden_size=self.hidden_size      ,      num_hidden_layers=self.num_hidden_layers      ,      num_attention_heads=self.num_attention_heads      ,      dim_ff=self.intermediate_size      ,      position_bias_num_buckets=self.num_buckets      ,      position_bias_max_distance=self.max_distance      ,      prompt_types=self.prompt_types      ,      prompt_length=self.prompt_length      ,      segment_types=self.segment_types      ,      use_cache=lowerCamelCase_      ,      init_std=self.init_std      ,      return_dict=self.return_dict      ,      )







   def 	lowerCamelCase_  (    self	:	Optional[int]      ,      lowerCamelCase_	:	List[str]      ,      lowerCamelCase_	:	List[str]      ,      *lowerCamelCase_	:	str							):





    """simple docstring"""


    UpperCamelCase      =			CpmAntModel(config=lowerCamelCase_							)
    model.to(lowerCamelCase_							)
    model.eval()

    UpperCamelCase      =			model(**lowerCamelCase_							).last_hidden_state

    self.parent.assertEqual(hidden_states.shape      ,      (self.batch_size, self.seq_length, config.hidden_size)							)







   def 	lowerCamelCase_  (    self	:	Any      ,      lowerCamelCase_	:	Dict      ,      lowerCamelCase_	:	Optional[Any]      ,      *lowerCamelCase_	:	str							):





    """simple docstring"""


    UpperCamelCase      =			CpmAntForCausalLM(lowerCamelCase_							)
    model.to(lowerCamelCase_							)
    UpperCamelCase      =			input_ids["""input_ids"""].to(lowerCamelCase_							)
    model.eval()

    UpperCamelCase      =			model(**lowerCamelCase_							)
    self.parent.assertEqual(
        model_output.logits.shape      ,      (self.batch_size, self.seq_length, config.vocab_size + config.prompt_types * config.prompt_length)      ,      )







   def 	lowerCamelCase_  (    self	:	str							):





    """simple docstring"""


    UpperCamelCase     ,    UpperCamelCase      =			self.prepare_config_and_inputs()
    return config, inputs_dict





@require_torch
class SCREAMING_SNAKE_CASE_		(			__lowerCAmelCase     ,    __lowerCAmelCase     ,    unittest.TestCase		):
   __lowerCAmelCase											=							(CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
   __lowerCAmelCase											=							(
       {"""feature-extraction""": CpmAntModel, """text-generation""": CpmAntForCausalLM} if is_torch_available() else {}
   )

   __lowerCAmelCase											=							False
   __lowerCAmelCase											=							False
   __lowerCAmelCase											=							False
   __lowerCAmelCase											=							False
   __lowerCAmelCase											=							False







   def 	lowerCamelCase_  (    self	:	List[Any]							):





    """simple docstring"""


    UpperCamelCase      =			CpmAntModelTester(self							)
    UpperCamelCase      =			ConfigTester(self      ,      config_class=lowerCamelCase_							)







   def 	lowerCamelCase_  (    self	:	str							):





    """simple docstring"""


    self.config_tester.create_and_test_config_common_properties()
    self.config_tester.create_and_test_config_to_json_string()
    self.config_tester.create_and_test_config_to_json_file()
    self.config_tester.create_and_test_config_from_and_save_pretrained()
    self.config_tester.check_config_can_be_init_without_params()
    self.config_tester.check_config_arguments_init()







   def 	lowerCamelCase_  (    self	:	int							):





    """simple docstring"""


    unittest.skip("""CPMAnt doesn't support input_embeds."""							)(self.test_inputs_embeds							)







   def 	lowerCamelCase_  (    self	:	Tuple							):





    """simple docstring"""


    unittest.skip(
        """CPMAnt doesn't support retain grad in hidden_states or attentions, because prompt management will peel off the output.hidden_states from graph.\
                 So is attentions. We strongly recommand you use loss to tune model."""							)(self.test_retain_grad_hidden_states_attentions							)







   def 	lowerCamelCase_  (    self	:	Optional[int]							):





    """simple docstring"""


    UpperCamelCase     ,    UpperCamelCase      =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_cpmant_model(lowerCamelCase_      ,      lowerCamelCase_							)







   def 	lowerCamelCase_  (    self	:	str							):





    """simple docstring"""


    UpperCamelCase     ,    UpperCamelCase      =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_lm_head_model(lowerCamelCase_      ,      lowerCamelCase_							)





@require_torch
class SCREAMING_SNAKE_CASE_		(			unittest.TestCase		):







   @tooslow
   def 	lowerCamelCase_  (    self	:	List[str]							):





    """simple docstring"""


    UpperCamelCase      =			"""今天天气真好！"""
    UpperCamelCase      =			"""openbmb/cpm-ant-10b"""
    UpperCamelCase      =			CpmAntModel.from_pretrained(lowerCamelCase_							)
    UpperCamelCase      =			CpmAntTokenizer.from_pretrained(lowerCamelCase_							)
    UpperCamelCase      =			tokenizer(lowerCamelCase_      ,      return_tensors="""pt"""							)
    UpperCamelCase      =			model(**lowerCamelCase_							).last_hidden_state

    UpperCamelCase      =			torch.tensor(
        [[[6.1_7_0_8, 5.9_2_4_4, 1.0_8_3_5], [6.5_2_0_7, 6.2_8_9_3, -1_1.3_3_2_4], [-1.0_1_0_7, -0.0_5_7_6, -5.9_5_7_7]]]      ,      )
    self.assertTrue(torch.allclose(hidden_states[:, :3, :3]      ,      lowerCamelCase_      ,      atol=1E-2							)							)





@require_torch
class SCREAMING_SNAKE_CASE_		(			unittest.TestCase		):







   @tooslow
   def 	lowerCamelCase_  (    self	:	str							):





    """simple docstring"""


    UpperCamelCase      =			"""今天天气真好！"""
    UpperCamelCase      =			"""openbmb/cpm-ant-10b"""
    UpperCamelCase      =			CpmAntForCausalLM.from_pretrained(lowerCamelCase_							)
    UpperCamelCase      =			CpmAntTokenizer.from_pretrained(lowerCamelCase_							)
    UpperCamelCase      =			tokenizer(lowerCamelCase_      ,      return_tensors="""pt"""							)
    UpperCamelCase      =			model(**lowerCamelCase_							).logits

    UpperCamelCase      =			torch.tensor(
        [[[-6.4_2_6_7, -6.4_0_8_3, -6.3_9_5_8], [-5.8_8_0_2, -5.9_4_4_7, -5.7_8_1_1], [-5.3_8_9_6, -5.4_8_2_0, -5.4_2_9_5]]]      ,      )
    self.assertTrue(torch.allclose(hidden_states[:, :3, :3]      ,      lowerCamelCase_      ,      atol=1E-2							)							)







   @tooslow
   def 	lowerCamelCase_  (    self	:	Dict							):





    """simple docstring"""


    UpperCamelCase      =			"""openbmb/cpm-ant-10b"""
    UpperCamelCase      =			CpmAntForCausalLM.from_pretrained(lowerCamelCase_							)
    UpperCamelCase      =			CpmAntTokenizer.from_pretrained(lowerCamelCase_							)
    UpperCamelCase      =			"""今天天气不错，"""
    UpperCamelCase      =			"""今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的"""
    UpperCamelCase      =			tokenizer(lowerCamelCase_      ,      return_tensors="""pt"""							)
    UpperCamelCase      =			model.generate(**lowerCamelCase_							)
    UpperCamelCase      =			tokenizer.batch_decode(lowerCamelCase_							)
    self.assertEqual(lowerCamelCase_      ,      lowerCamelCase_							)







   @tooslow
   def 	lowerCamelCase_  (    self	:	int							):





    """simple docstring"""


    UpperCamelCase      =			"""openbmb/cpm-ant-10b"""
    UpperCamelCase      =			CpmAntForCausalLM.from_pretrained(lowerCamelCase_							)
    UpperCamelCase      =			CpmAntTokenizer.from_pretrained(lowerCamelCase_							)
    UpperCamelCase      =			["""今天天气不错，""", """新年快乐，万事如意！"""]
    UpperCamelCase      =			[
        """今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的""",
        """新年快乐，万事如意！在这辞旧迎新的美好时刻，我谨代表《农村新技术》杂志社全体同仁，向一直以来关心、支持《农村新技术》杂志发展的各级领导、各界朋友和广大读者致以最诚挚的""",
    ]
    UpperCamelCase      =			tokenizer(lowerCamelCase_      ,      return_tensors="""pt"""      ,      padding=lowerCamelCase_							)
    UpperCamelCase      =			model.generate(**lowerCamelCase_							)
    UpperCamelCase      =			tokenizer.batch_decode(lowerCamelCase_							)
    self.assertEqual(lowerCamelCase_      ,      lowerCamelCase_							)
