





import tempfile

import torch

from diffusers import (
    DEISMultistepScheduler,
    DPMSolverMultistepScheduler,
    DPMSolverSinglestepScheduler,
    UniPCMultistepScheduler,
)

from .test_schedulers import SchedulerCommonTest



class        _lowerCAmelCase    (			UpperCAmelCase_							):


  '''simple docstring'''
  a_							:       Optional[Any]      					=(UniPCMultistepScheduler,)
  a_							:       Tuple      					=(("""num_inference_steps""", 25),)







  def UpperCamelCase_ (      self      :					int      ,				**UpperCamelCase      :					Optional[Any]							)     ->   Any:


    '''simple docstring'''





    _snake_case   :				Tuple														=	{
        'num_train_timesteps': 10_00,
        'beta_start': 0.00_01,
        'beta_end': 0.02,
        'beta_schedule': 'linear',
        'solver_order': 2,
        'solver_type': 'bh2',
    }

    config.update(**UpperCamelCase							)
    return config







  def UpperCamelCase_ (      self      :					Tuple      ,				UpperCamelCase      :					str=0      ,				**UpperCamelCase      :					Dict							)     ->   Optional[int]:


    '''simple docstring'''





    _snake_case   :				Tuple														=	dict(self.forward_default_kwargs							)
    _snake_case   :				Optional[int]														=	kwargs.pop('num_inference_steps'      ,				UpperCamelCase							)
    _snake_case   :				str														=	self.dummy_sample
    _snake_case   :				Tuple														=	0.1 * sample
    _snake_case   :				Dict														=	[residual + 0.2, residual + 0.15, residual + 0.10]

    for scheduler_class in self.scheduler_classes:
      _snake_case   :				int														=	self.get_scheduler_config(**UpperCamelCase							)
      _snake_case   :				List[Any]														=	scheduler_class(**UpperCamelCase							)
      scheduler.set_timesteps(UpperCamelCase							)
      # copy over dummy past residuals
      _snake_case   :				Dict														=	dummy_past_residuals[: scheduler.config.solver_order]

      with tempfile.TemporaryDirectory() as tmpdirname:
        scheduler.save_config(UpperCamelCase							)
        _snake_case   :				Optional[Any]														=	scheduler_class.from_pretrained(UpperCamelCase							)
        new_scheduler.set_timesteps(UpperCamelCase							)
        # copy over dummy past residuals
        _snake_case   :				List[Any]														=	dummy_past_residuals[: new_scheduler.config.solver_order]

      _snake_case   :				Optional[int]														=	sample, sample
      for t in range(UpperCamelCase      ,				time_step + scheduler.config.solver_order + 1							):
        _snake_case   :				int														=	scheduler.step(UpperCamelCase      ,				UpperCamelCase      ,				UpperCamelCase      ,				**UpperCamelCase							).prev_sample
        _snake_case   :				List[Any]														=	new_scheduler.step(UpperCamelCase      ,				UpperCamelCase      ,				UpperCamelCase      ,				**UpperCamelCase							).prev_sample

        assert torch.sum(torch.abs(output - new_output							)							) < 1e-5, "Scheduler outputs are not identical"







  def UpperCamelCase_ (      self      :					Any      ,				UpperCamelCase      :					int=0      ,				**UpperCamelCase      :					Optional[int]							)     ->   Dict:


    '''simple docstring'''





    _snake_case   :				str														=	dict(self.forward_default_kwargs							)
    _snake_case   :				Optional[Any]														=	kwargs.pop('num_inference_steps'      ,				UpperCamelCase							)
    _snake_case   :				int														=	self.dummy_sample
    _snake_case   :				Dict														=	0.1 * sample
    _snake_case   :				int														=	[residual + 0.2, residual + 0.15, residual + 0.10]

    for scheduler_class in self.scheduler_classes:
      _snake_case   :				List[Any]														=	self.get_scheduler_config()
      _snake_case   :				List[str]														=	scheduler_class(**UpperCamelCase							)
      scheduler.set_timesteps(UpperCamelCase							)

      # copy over dummy past residuals (must be after setting timesteps)
      _snake_case   :				str														=	dummy_past_residuals[: scheduler.config.solver_order]

      with tempfile.TemporaryDirectory() as tmpdirname:
        scheduler.save_config(UpperCamelCase							)
        _snake_case   :				List[str]														=	scheduler_class.from_pretrained(UpperCamelCase							)
        # copy over dummy past residuals
        new_scheduler.set_timesteps(UpperCamelCase							)

        # copy over dummy past residual (must be after setting timesteps)
        _snake_case   :				Optional[int]														=	dummy_past_residuals[: new_scheduler.config.solver_order]

      _snake_case   :				Dict														=	scheduler.step(UpperCamelCase      ,				UpperCamelCase      ,				UpperCamelCase      ,				**UpperCamelCase							).prev_sample
      _snake_case   :				Tuple														=	new_scheduler.step(UpperCamelCase      ,				UpperCamelCase      ,				UpperCamelCase      ,				**UpperCamelCase							).prev_sample

      assert torch.sum(torch.abs(output - new_output							)							) < 1e-5, "Scheduler outputs are not identical"







  def UpperCamelCase_ (      self      :					str      ,				UpperCamelCase      :					Optional[Any]=None      ,				**UpperCamelCase      :					Optional[Any]							)     ->   Optional[Any]:


    '''simple docstring'''





    if scheduler is None:
      _snake_case   :				List[Any]														=	self.scheduler_classes[0]
      _snake_case   :				Optional[Any]														=	self.get_scheduler_config(**UpperCamelCase							)
      _snake_case   :				Optional[int]														=	scheduler_class(**UpperCamelCase							)

    _snake_case   :				int														=	self.scheduler_classes[0]
    _snake_case   :				Any														=	self.get_scheduler_config(**UpperCamelCase							)
    _snake_case   :				Any														=	scheduler_class(**UpperCamelCase							)

    _snake_case   :				List[Any]														=	10
    _snake_case   :				Optional[int]														=	self.dummy_model()
    _snake_case   :				List[str]														=	self.dummy_sample_deter
    scheduler.set_timesteps(UpperCamelCase							)

    for i, t in enumerate(scheduler.timesteps							):
      _snake_case   :				Union[str, Any]														=	model(UpperCamelCase      ,				UpperCamelCase							)
      _snake_case   :				Optional[int]														=	scheduler.step(UpperCamelCase      ,				UpperCamelCase      ,				UpperCamelCase							).prev_sample

    return sample







  def UpperCamelCase_ (      self      :					List[str]							)     ->   Tuple:


    '''simple docstring'''





    _snake_case   :				List[str]														=	dict(self.forward_default_kwargs							)

    _snake_case   :				Optional[int]														=	kwargs.pop('num_inference_steps'      ,				UpperCamelCase							)

    for scheduler_class in self.scheduler_classes:
      _snake_case   :				str														=	self.get_scheduler_config()
      _snake_case   :				Tuple														=	scheduler_class(**UpperCamelCase							)

      _snake_case   :				Union[str, Any]														=	self.dummy_sample
      _snake_case   :				Dict														=	0.1 * sample

      if num_inference_steps is not None and hasattr(UpperCamelCase      ,				'set_timesteps'							):
        scheduler.set_timesteps(UpperCamelCase							)
      elif num_inference_steps is not None and not hasattr(UpperCamelCase      ,				'set_timesteps'							):
        _snake_case   :				Dict														=	num_inference_steps

      # copy over dummy past residuals (must be done after set_timesteps)
      _snake_case   :				Any														=	[residual + 0.2, residual + 0.15, residual + 0.10]
      _snake_case   :				int														=	dummy_past_residuals[: scheduler.config.solver_order]

      _snake_case   :				Union[str, Any]														=	scheduler.timesteps[5]
      _snake_case   :				str														=	scheduler.timesteps[6]

      _snake_case   :				Any														=	scheduler.step(UpperCamelCase      ,				UpperCamelCase      ,				UpperCamelCase      ,				**UpperCamelCase							).prev_sample
      _snake_case   :				Any														=	scheduler.step(UpperCamelCase      ,				UpperCamelCase      ,				UpperCamelCase      ,				**UpperCamelCase							).prev_sample

      self.assertEqual(output_a.shape      ,				sample.shape							)
      self.assertEqual(output_a.shape      ,				output_a.shape							)







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


    '''simple docstring'''





    _snake_case   :				str														=	UniPCMultistepScheduler(**self.get_scheduler_config()							)
    _snake_case   :				Optional[int]														=	self.full_loop(scheduler=UpperCamelCase							)
    _snake_case   :				Tuple														=	torch.mean(torch.abs(UpperCamelCase							)							)

    assert abs(result_mean.item() - 0.24_64							) < 1e-3

    _snake_case   :				List[Any]														=	DPMSolverSinglestepScheduler.from_config(scheduler.config							)
    _snake_case   :				Union[str, Any]														=	DEISMultistepScheduler.from_config(scheduler.config							)
    _snake_case   :				List[str]														=	DPMSolverMultistepScheduler.from_config(scheduler.config							)
    _snake_case   :				Any														=	UniPCMultistepScheduler.from_config(scheduler.config							)

    _snake_case   :				int														=	self.full_loop(scheduler=UpperCamelCase							)
    _snake_case   :				List[Any]														=	torch.mean(torch.abs(UpperCamelCase							)							)

    assert abs(result_mean.item() - 0.24_64							) < 1e-3







  def UpperCamelCase_ (      self      :					List[str]							)     ->   str:


    '''simple docstring'''





    for timesteps in [25, 50, 1_00, 9_99, 10_00]:
      self.check_over_configs(num_train_timesteps=UpperCamelCase							)







  def UpperCamelCase_ (      self      :					Optional[int]							)     ->   Dict:


    '''simple docstring'''





    self.check_over_configs(thresholding=UpperCamelCase							)
    for order in [1, 2, 3]:
      for solver_type in ["bh1", "bh2"]:
        for threshold in [0.5, 1.0, 2.0]:
          for prediction_type in ["epsilon", "sample"]:
            self.check_over_configs(
                thresholding=UpperCamelCase      ,				prediction_type=UpperCamelCase      ,				sample_max_value=UpperCamelCase      ,				solver_order=UpperCamelCase      ,				solver_type=UpperCamelCase      ,				)







  def UpperCamelCase_ (      self      :					List[Any]							)     ->   Optional[Any]:


    '''simple docstring'''





    for prediction_type in ["epsilon", "v_prediction"]:
      self.check_over_configs(prediction_type=UpperCamelCase							)







  def UpperCamelCase_ (      self      :					Dict							)     ->   Any:


    '''simple docstring'''





    for solver_type in ["bh1", "bh2"]:
      for order in [1, 2, 3]:
        for prediction_type in ["epsilon", "sample"]:
          self.check_over_configs(
              solver_order=UpperCamelCase      ,				solver_type=UpperCamelCase      ,				prediction_type=UpperCamelCase      ,				)
          _snake_case   :				Optional[int]														=	self.full_loop(
              solver_order=UpperCamelCase      ,				solver_type=UpperCamelCase      ,				prediction_type=UpperCamelCase      ,				)
          assert not torch.isnan(UpperCamelCase							).any(), "Samples have nan numbers"







  def UpperCamelCase_ (      self      :					Optional[int]							)     ->   Union[str, Any]:


    '''simple docstring'''





    self.check_over_configs(lower_order_final=UpperCamelCase							)
    self.check_over_configs(lower_order_final=UpperCamelCase							)







  def UpperCamelCase_ (      self      :					int							)     ->   Tuple:


    '''simple docstring'''





    for num_inference_steps in [1, 2, 3, 5, 10, 50, 1_00, 9_99, 10_00]:
      self.check_over_forward(num_inference_steps=UpperCamelCase      ,				time_step=0							)







  def UpperCamelCase_ (      self      :					Dict							)     ->   int:


    '''simple docstring'''





    _snake_case   :				Dict														=	self.full_loop()
    _snake_case   :				Union[str, Any]														=	torch.mean(torch.abs(UpperCamelCase							)							)

    assert abs(result_mean.item() - 0.24_64							) < 1e-3







  def UpperCamelCase_ (      self      :					str							)     ->   Optional[int]:


    '''simple docstring'''





    _snake_case   :				str														=	self.full_loop(prediction_type='v_prediction'							)
    _snake_case   :				Dict														=	torch.mean(torch.abs(UpperCamelCase							)							)

    assert abs(result_mean.item() - 0.10_14							) < 1e-3







  def UpperCamelCase_ (      self      :					str							)     ->   List[Any]:


    '''simple docstring'''





    _snake_case   :				Tuple														=	self.scheduler_classes[0]
    _snake_case   :				int														=	self.get_scheduler_config(thresholding=UpperCamelCase      ,				dynamic_thresholding_ratio=0							)
    _snake_case   :				int														=	scheduler_class(**UpperCamelCase							)

    _snake_case   :				Dict														=	10
    _snake_case   :				int														=	self.dummy_model()
    _snake_case   :				Optional[Any]														=	self.dummy_sample_deter.half()
    scheduler.set_timesteps(UpperCamelCase							)

    for i, t in enumerate(scheduler.timesteps							):
      _snake_case   :				Dict														=	model(UpperCamelCase      ,				UpperCamelCase							)
      _snake_case   :				int														=	scheduler.step(UpperCamelCase      ,				UpperCamelCase      ,				UpperCamelCase							).prev_sample

    assert sample.dtype == torch.floataa







  def UpperCamelCase_ (      self      :					int      ,				**UpperCamelCase      :					List[str]							)     ->   Any:


    '''simple docstring'''





    for scheduler_class in self.scheduler_classes:
      _snake_case   :				str														=	self.get_scheduler_config(**UpperCamelCase							)
      _snake_case   :				Dict														=	scheduler_class(**UpperCamelCase							)

      scheduler.set_timesteps(scheduler.config.num_train_timesteps							)
      assert len(scheduler.timesteps.unique()							) == scheduler.num_inference_steps











import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from .tokenization_lxmert import LxmertTokenizer


lowerCAmelCase_						   =    {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

lowerCAmelCase_						   =    {
    """vocab_file""": {
        """unc-nlp/lxmert-base-uncased""": """https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/vocab.txt""",
    },
    """tokenizer_file""": {
        """unc-nlp/lxmert-base-uncased""": (
            """https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/tokenizer.json"""
        ),
    },
}

lowerCAmelCase_						   =    {
    """unc-nlp/lxmert-base-uncased""": 512,
}

lowerCAmelCase_						   =    {
    """unc-nlp/lxmert-base-uncased""": {"""do_lower_case""": True},
}



class        _lowerCAmelCase    (			UpperCAmelCase_							):


  '''simple docstring'''
  a_							:       List[Any]      					=VOCAB_FILES_NAMES
  a_							:       Tuple      					=PRETRAINED_VOCAB_FILES_MAP
  a_							:       Optional[Any]      					=PRETRAINED_INIT_CONFIGURATION
  a_							:       Any      					=PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  a_							:       Any      					=LxmertTokenizer







  def __init__(      self      :					Any      ,				UpperCamelCase      :					Union[str, Any]=None      ,				UpperCamelCase      :					Dict=None      ,				UpperCamelCase      :					List[str]=True      ,				UpperCamelCase      :					List[str]="[UNK]"      ,				UpperCamelCase      :					List[Any]="[SEP]"      ,				UpperCamelCase      :					List[Any]="[PAD]"      ,				UpperCamelCase      :					Optional[Any]="[CLS]"      ,				UpperCamelCase      :					Optional[int]="[MASK]"      ,				UpperCamelCase      :					Optional[int]=True      ,				UpperCamelCase      :					str=None      ,				**UpperCamelCase      :					List[str]      ,				):


    '''simple docstring'''





    super().__init__(
        UpperCamelCase      ,				tokenizer_file=UpperCamelCase      ,				do_lower_case=UpperCamelCase      ,				unk_token=UpperCamelCase      ,				sep_token=UpperCamelCase      ,				pad_token=UpperCamelCase      ,				cls_token=UpperCamelCase      ,				mask_token=UpperCamelCase      ,				tokenize_chinese_chars=UpperCamelCase      ,				strip_accents=UpperCamelCase      ,				**UpperCamelCase      ,				)

    _snake_case   :				Tuple														=	json.loads(self.backend_tokenizer.normalizer.__getstate__()							)
    if (
        normalizer_state.get('lowercase'      ,				UpperCamelCase							) != do_lower_case
        or normalizer_state.get('strip_accents'      ,				UpperCamelCase							) != strip_accents
        or normalizer_state.get('handle_chinese_chars'      ,				UpperCamelCase							) != tokenize_chinese_chars
    ):
      _snake_case   :				List[Any]														=	getattr(UpperCamelCase      ,				normalizer_state.pop('type'							)							)
      _snake_case   :				Optional[int]														=	do_lower_case
      _snake_case   :				Dict														=	strip_accents
      _snake_case   :				Optional[int]														=	tokenize_chinese_chars
      _snake_case   :				Optional[Any]														=	normalizer_class(**UpperCamelCase							)

    _snake_case   :				int														=	do_lower_case







  def UpperCamelCase_ (      self      :					int      ,				UpperCamelCase      :					List[str]      ,				UpperCamelCase      :					str=None							):


    '''simple docstring'''





    _snake_case   :				List[Any]														=	[self.cls_token_id] + token_ids_a + [self.sep_token_id]

    if token_ids_a:
      output += token_ids_a + [self.sep_token_id]

    return output







  def UpperCamelCase_ (      self      :					List[str]      ,				UpperCamelCase      :					List[int]      ,				UpperCamelCase      :					Optional[List[int]] = None							):


    '''simple docstring'''





    _snake_case   :				Tuple														=	[self.sep_token_id]
    _snake_case   :				List[str]														=	[self.cls_token_id]
    if token_ids_a is None:
      return len(cls + token_ids_a + sep							) * [0]
    return len(cls + token_ids_a + sep							) * [0] + len(token_ids_a + sep							) * [1]







  def UpperCamelCase_ (      self      :					int      ,				UpperCamelCase      :					str      ,				UpperCamelCase      :					Optional[str] = None							):


    '''simple docstring'''





    _snake_case   :				int														=	self._tokenizer.model.save(UpperCamelCase      ,				name=UpperCamelCase							)
    return tuple(UpperCamelCase							)




