


import tempfile

import torch

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

from .test_schedulers import SchedulerCommonTest






class    UpperCAmelCase__	( A_		):



    """simple docstring"""






    UpperCAmelCase__						:		Dict              =  (UniPCMultistepScheduler,)
    UpperCAmelCase__						:		int              =  (("num_inference_steps", 2_5),)






    def _a (      self ,       **A_   )  ->	Any:
         __UpperCamelCase   							={
             'num_train_timesteps': 1000,
             'beta_start': 0.0001,
             'beta_end': 0.02,
             'beta_schedule': 'linear',
             'solver_order': 2,
             'solver_type': 'bh2',
         }

         config.update(**A_   )
         return config






    def _a (      self ,       A_=0 ,       **A_   )  ->	int:
         __UpperCamelCase   							=dict(self.forward_default_kwargs   )
         __UpperCamelCase   							=kwargs.pop('num_inference_steps' ,       A_   )
         __UpperCamelCase   							=self.dummy_sample
         __UpperCamelCase   							=0.1 * sample
         __UpperCamelCase   							=[residual + 0.2, residual + 0.15, residual + 0.10]

         for scheduler_class in self.scheduler_classes:
              __UpperCamelCase   							=self.get_scheduler_config(**A_   )
              __UpperCamelCase   							=scheduler_class(**A_   )
              scheduler.set_timesteps(A_   )
              # copy over dummy past residuals
              __UpperCamelCase   							=dummy_past_residuals[: scheduler.config.solver_order]

              with tempfile.TemporaryDirectory() as tmpdirname:
                   scheduler.save_config(A_   )
                   __UpperCamelCase   							=scheduler_class.from_pretrained(A_   )
                   new_scheduler.set_timesteps(A_   )
                   # copy over dummy past residuals
                   __UpperCamelCase   							=dummy_past_residuals[: new_scheduler.config.solver_order]

              __UpperCamelCase							,				__UpperCamelCase   							=sample, sample
              for t in range(A_ ,       time_step + scheduler.config.solver_order + 1   ):
                   __UpperCamelCase   							=scheduler.step(A_ ,       A_ ,       A_ ,       **A_   ).prev_sample
                   __UpperCamelCase   							=new_scheduler.step(A_ ,       A_ ,       A_ ,       **A_   ).prev_sample

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






    def _a (      self ,       A_=0 ,       **A_   )  ->	Tuple:
         __UpperCamelCase   							=dict(self.forward_default_kwargs   )
         __UpperCamelCase   							=kwargs.pop('num_inference_steps' ,       A_   )
         __UpperCamelCase   							=self.dummy_sample
         __UpperCamelCase   							=0.1 * sample
         __UpperCamelCase   							=[residual + 0.2, residual + 0.15, residual + 0.10]

         for scheduler_class in self.scheduler_classes:
              __UpperCamelCase   							=self.get_scheduler_config()
              __UpperCamelCase   							=scheduler_class(**A_   )
              scheduler.set_timesteps(A_   )

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

              with tempfile.TemporaryDirectory() as tmpdirname:
                   scheduler.save_config(A_   )
                   __UpperCamelCase   							=scheduler_class.from_pretrained(A_   )
                   # copy over dummy past residuals
                   new_scheduler.set_timesteps(A_   )

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

              __UpperCamelCase   							=scheduler.step(A_ ,       A_ ,       A_ ,       **A_   ).prev_sample
              __UpperCamelCase   							=new_scheduler.step(A_ ,       A_ ,       A_ ,       **A_   ).prev_sample

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






    def _a (      self ,       A_=None ,       **A_   )  ->	Dict:
         if scheduler is None:
              __UpperCamelCase   							=self.scheduler_classes[0]
              __UpperCamelCase   							=self.get_scheduler_config(**A_   )
              __UpperCamelCase   							=scheduler_class(**A_   )

         __UpperCamelCase   							=self.scheduler_classes[0]
         __UpperCamelCase   							=self.get_scheduler_config(**A_   )
         __UpperCamelCase   							=scheduler_class(**A_   )

         __UpperCamelCase   							=10
         __UpperCamelCase   							=self.dummy_model()
         __UpperCamelCase   							=self.dummy_sample_deter
         scheduler.set_timesteps(A_   )

         for i, t in enumerate(scheduler.timesteps   ):
              __UpperCamelCase   							=model(A_ ,       A_   )
              __UpperCamelCase   							=scheduler.step(A_ ,       A_ ,       A_   ).prev_sample

         return sample






    def _a (      self   )  ->	Any:
         __UpperCamelCase   							=dict(self.forward_default_kwargs   )

         __UpperCamelCase   							=kwargs.pop('num_inference_steps' ,       A_   )

         for scheduler_class in self.scheduler_classes:
              __UpperCamelCase   							=self.get_scheduler_config()
              __UpperCamelCase   							=scheduler_class(**A_   )

              __UpperCamelCase   							=self.dummy_sample
              __UpperCamelCase   							=0.1 * sample

              if num_inference_steps is not None and hasattr(A_ ,       'set_timesteps'   ):
                   scheduler.set_timesteps(A_   )
              elif num_inference_steps is not None and not hasattr(A_ ,       'set_timesteps'   ):
                   __UpperCamelCase   							=num_inference_steps

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

              __UpperCamelCase   							=scheduler.timesteps[5]
              __UpperCamelCase   							=scheduler.timesteps[6]

              __UpperCamelCase   							=scheduler.step(A_ ,       A_ ,       A_ ,       **A_   ).prev_sample
              __UpperCamelCase   							=scheduler.step(A_ ,       A_ ,       A_ ,       **A_   ).prev_sample

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






    def _a (      self   )  ->	int:
         # make sure that iterating over schedulers with same config names gives same results
         # for defaults
         __UpperCamelCase   							=UniPCMultistepScheduler(**self.get_scheduler_config()   )
         __UpperCamelCase   							=self.full_loop(scheduler=A_   )
         __UpperCamelCase   							=torch.mean(torch.abs(A_   )   )

         assert abs(result_mean.item() - 0.2464   ) < 1E-3

         __UpperCamelCase   							=DPMSolverSinglestepScheduler.from_config(scheduler.config   )
         __UpperCamelCase   							=DEISMultistepScheduler.from_config(scheduler.config   )
         __UpperCamelCase   							=DPMSolverMultistepScheduler.from_config(scheduler.config   )
         __UpperCamelCase   							=UniPCMultistepScheduler.from_config(scheduler.config   )

         __UpperCamelCase   							=self.full_loop(scheduler=A_   )
         __UpperCamelCase   							=torch.mean(torch.abs(A_   )   )

         assert abs(result_mean.item() - 0.2464   ) < 1E-3






    def _a (      self   )  ->	Tuple:
         for timesteps in [25, 50, 100, 999, 1000]:
              self.check_over_configs(num_train_timesteps=A_   )






    def _a (      self   )  ->	Tuple:
         self.check_over_configs(thresholding=A_   )
         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=A_ ,       prediction_type=A_ ,       sample_max_value=A_ ,       solver_order=A_ ,       solver_type=A_ ,       )






    def _a (      self   )  ->	str:
         for prediction_type in ["epsilon", "v_prediction"]:
              self.check_over_configs(prediction_type=A_   )






    def _a (      self   )  ->	Union[str, Any]:
         for solver_type in ["bh1", "bh2"]:
              for order in [1, 2, 3]:
                   for prediction_type in ["epsilon", "sample"]:
                        self.check_over_configs(
                            solver_order=A_ ,       solver_type=A_ ,       prediction_type=A_ ,       )
                        __UpperCamelCase   							=self.full_loop(
                            solver_order=A_ ,       solver_type=A_ ,       prediction_type=A_ ,       )
                        assert not torch.isnan(A_   ).any(), "Samples have nan numbers"






    def _a (      self   )  ->	List[Any]:
         self.check_over_configs(lower_order_final=A_   )
         self.check_over_configs(lower_order_final=A_   )






    def _a (      self   )  ->	Union[str, Any]:
         for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]:
              self.check_over_forward(num_inference_steps=A_ ,       time_step=0   )






    def _a (      self   )  ->	str:
         __UpperCamelCase   							=self.full_loop()
         __UpperCamelCase   							=torch.mean(torch.abs(A_   )   )

         assert abs(result_mean.item() - 0.2464   ) < 1E-3






    def _a (      self   )  ->	Any:
         __UpperCamelCase   							=self.full_loop(prediction_type='v_prediction'   )
         __UpperCamelCase   							=torch.mean(torch.abs(A_   )   )

         assert abs(result_mean.item() - 0.1014   ) < 1E-3






    def _a (      self   )  ->	Optional[Any]:
         __UpperCamelCase   							=self.scheduler_classes[0]
         __UpperCamelCase   							=self.get_scheduler_config(thresholding=A_ ,       dynamic_thresholding_ratio=0   )
         __UpperCamelCase   							=scheduler_class(**A_   )

         __UpperCamelCase   							=10
         __UpperCamelCase   							=self.dummy_model()
         __UpperCamelCase   							=self.dummy_sample_deter.half()
         scheduler.set_timesteps(A_   )

         for i, t in enumerate(scheduler.timesteps   ):
              __UpperCamelCase   							=model(A_ ,       A_   )
              __UpperCamelCase   							=scheduler.step(A_ ,       A_ ,       A_   ).prev_sample

         assert sample.dtype == torch.floataa






    def _a (      self ,       **A_   )  ->	Tuple:
         for scheduler_class in self.scheduler_classes:
              __UpperCamelCase   							=self.get_scheduler_config(**A_   )
              __UpperCamelCase   							=scheduler_class(**A_   )

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









from ...configuration_utils import PretrainedConfig
from ...utils import logging


_A			=						logging.get_logger(__name__)

_A			=						{
    'google/realm-cc-news-pretrained-embedder': (
        'https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/config.json'
    ),
    'google/realm-cc-news-pretrained-encoder': (
        'https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/config.json'
    ),
    'google/realm-cc-news-pretrained-scorer': (
        'https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/config.json'
    ),
    'google/realm-cc-news-pretrained-openqa': (
        'https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/config.json'
    ),
    'google/realm-orqa-nq-openqa': 'https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/config.json',
    'google/realm-orqa-nq-reader': 'https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/config.json',
    'google/realm-orqa-wq-openqa': 'https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/config.json',
    'google/realm-orqa-wq-reader': 'https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/config.json',
    # See all REALM models at https://huggingface.co/models?filter=realm
}






class    UpperCAmelCase__	( A_		):



    """simple docstring"""






    UpperCAmelCase__						:		str              =  "realm"






    def __init__(      self ,       A_=30522 ,       A_=768 ,       A_=128 ,       A_=12 ,       A_=12 ,       A_=8 ,       A_=3072 ,       A_="gelu_new" ,       A_=0.1 ,       A_=0.1 ,       A_=512 ,       A_=2 ,       A_=0.02 ,       A_=1E-12 ,       A_=256 ,       A_=10 ,       A_=1E-3 ,       A_=5 ,       A_=320 ,       A_=13353718 ,       A_=5000 ,       A_=1 ,       A_=0 ,       A_=2 ,       **A_ ,       )  ->	Dict:
         super().__init__(pad_token_id=A_ ,       bos_token_id=A_ ,       eos_token_id=A_ ,       **A_   )

         # Common config
         __UpperCamelCase   							=vocab_size
         __UpperCamelCase   							=max_position_embeddings
         __UpperCamelCase   							=hidden_size
         __UpperCamelCase   							=retriever_proj_size
         __UpperCamelCase   							=num_hidden_layers
         __UpperCamelCase   							=num_attention_heads
         __UpperCamelCase   							=num_candidates
         __UpperCamelCase   							=intermediate_size
         __UpperCamelCase   							=hidden_act
         __UpperCamelCase   							=hidden_dropout_prob
         __UpperCamelCase   							=attention_probs_dropout_prob
         __UpperCamelCase   							=initializer_range
         __UpperCamelCase   							=type_vocab_size
         __UpperCamelCase   							=layer_norm_eps

         # Reader config
         __UpperCamelCase   							=span_hidden_size
         __UpperCamelCase   							=max_span_width
         __UpperCamelCase   							=reader_layer_norm_eps
         __UpperCamelCase   							=reader_beam_size
         __UpperCamelCase   							=reader_seq_len

         # Retrieval config
         __UpperCamelCase   							=num_block_records
         __UpperCamelCase   							=searcher_beam_size





