





import tempfile

import torch

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

from .test_schedulers import SchedulerCommonTest
class 							snake_case_		(      __SCREAMING_SNAKE_CASE):
		lowerCamelCase     :List[Any]							=  (DPMSolverMultistepScheduler,)
		lowerCamelCase     :List[str]							=  (("""num_inference_steps""", 25),)







		def       __lowercase (		self      ,  **__lowercase )					-> List[str]:
				lowerCamelCase :			Optional[int]    		={
				    '''num_train_timesteps''': 1_0_0_0,
				    '''beta_start''': 0.0_0_0_1,
				    '''beta_end''': 0.0_2,
				    '''beta_schedule''': '''linear''',
				    '''solver_order''': 2,
				    '''prediction_type''': '''epsilon''',
				    '''thresholding''': False,
				    '''sample_max_value''': 1.0,
				    '''algorithm_type''': '''dpmsolver++''',
				    '''solver_type''': '''midpoint''',
				    '''lower_order_final''': False,
				    '''lambda_min_clipped''': -float('''inf''' ),
				    '''variance_type''': None,
				}

				config.update(**__UpperCamelCase )
				return config







		def       __lowercase (		self      ,  __lowercase=0      ,  **__lowercase )					-> Dict:
				lowerCamelCase :			Dict    		=dict(self.forward_default_kwargs )
				lowerCamelCase :			int    		=kwargs.pop('''num_inference_steps'''      ,  __UpperCamelCase )
				lowerCamelCase :			Union[str, Any]    		=self.dummy_sample
				lowerCamelCase :			Dict    		=0.1 * sample
				lowerCamelCase :			Optional[Any]    		=[residual + 0.2, residual + 0.1_5, residual + 0.1_0]

				for scheduler_class in self.scheduler_classes:
						lowerCamelCase :			List[str]    		=self.get_scheduler_config(**__UpperCamelCase )
						lowerCamelCase :			Union[str, Any]    		=scheduler_class(**__UpperCamelCase )
						scheduler.set_timesteps(__UpperCamelCase )
						# copy over dummy past residuals
						lowerCamelCase :			Any    		=dummy_past_residuals[: scheduler.config.solver_order]

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

						lowerCamelCase		, lowerCamelCase :			List[str]    		=sample, sample
						for t in range(__UpperCamelCase      ,  time_step + scheduler.config.solver_order + 1 ):
								lowerCamelCase :			Union[str, Any]    		=scheduler.step(__UpperCamelCase      ,  __UpperCamelCase      ,  __UpperCamelCase      ,  **__UpperCamelCase ).prev_sample
								lowerCamelCase :			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       __lowercase (		self )					-> Optional[Any]:
				pass







		def       __lowercase (		self      ,  __lowercase=0      ,  **__lowercase )					-> List[Any]:
				lowerCamelCase :			str    		=dict(self.forward_default_kwargs )
				lowerCamelCase :			List[str]    		=kwargs.pop('''num_inference_steps'''      ,  __UpperCamelCase )
				lowerCamelCase :			Optional[Any]    		=self.dummy_sample
				lowerCamelCase :			Dict    		=0.1 * sample
				lowerCamelCase :			int    		=[residual + 0.2, residual + 0.1_5, residual + 0.1_0]

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

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

						with tempfile.TemporaryDirectory() as tmpdirname:
								scheduler.save_config(__UpperCamelCase )
								lowerCamelCase :			Optional[Any]    		=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)
								lowerCamelCase :			Tuple    		=dummy_past_residuals[: new_scheduler.config.solver_order]

						lowerCamelCase :			str    		=scheduler.step(__UpperCamelCase      ,  __UpperCamelCase      ,  __UpperCamelCase      ,  **__UpperCamelCase ).prev_sample
						lowerCamelCase :			int    		=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       __lowercase (		self      ,  __lowercase=None      ,  **__lowercase )					-> Tuple:


				if scheduler is None:
						lowerCamelCase :			Union[str, Any]    		=self.scheduler_classes[0]
						lowerCamelCase :			List[str]    		=self.get_scheduler_config(**__UpperCamelCase )
						lowerCamelCase :			Optional[int]    		=scheduler_class(**__UpperCamelCase )

				lowerCamelCase :			str    		=1_0
				lowerCamelCase :			Optional[Any]    		=self.dummy_model()
				lowerCamelCase :			Optional[int]    		=self.dummy_sample_deter
				scheduler.set_timesteps(__UpperCamelCase )

				for i, t in enumerate(scheduler.timesteps ):
						lowerCamelCase :			str    		=model(__UpperCamelCase      ,  __UpperCamelCase )
						lowerCamelCase :			List[Any]    		=scheduler.step(__UpperCamelCase      ,  __UpperCamelCase      ,  __UpperCamelCase ).prev_sample

				return sample







		def       __lowercase (		self )					-> Optional[int]:
				lowerCamelCase :			Tuple    		=dict(self.forward_default_kwargs )

				lowerCamelCase :			List[Any]    		=kwargs.pop('''num_inference_steps'''      ,  __UpperCamelCase )

				for scheduler_class in self.scheduler_classes:
						lowerCamelCase :			str    		=self.get_scheduler_config()
						lowerCamelCase :			Union[str, Any]    		=scheduler_class(**__UpperCamelCase )

						lowerCamelCase :			Dict    		=self.dummy_sample
						lowerCamelCase :			List[Any]    		=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''' ):
								lowerCamelCase :			List[Any]    		=num_inference_steps

						# copy over dummy past residuals (must be done after set_timesteps)
						lowerCamelCase :			Union[str, Any]    		=[residual + 0.2, residual + 0.1_5, residual + 0.1_0]
						lowerCamelCase :			Optional[Any]    		=dummy_past_residuals[: scheduler.config.solver_order]

						lowerCamelCase :			List[Any]    		=scheduler.timesteps[5]
						lowerCamelCase :			str    		=scheduler.timesteps[6]

						lowerCamelCase :			Dict    		=scheduler.step(__UpperCamelCase      ,  __UpperCamelCase      ,  __UpperCamelCase      ,  **__UpperCamelCase ).prev_sample
						lowerCamelCase :			Optional[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       __lowercase (		self )					-> List[Any]:


				for timesteps in [2_5, 5_0, 1_0_0, 9_9_9, 1_0_0_0]:
						self.check_over_configs(num_train_timesteps=__UpperCamelCase )







		def       __lowercase (		self )					-> List[str]:
				self.check_over_configs(thresholding=__UpperCamelCase )
				for order in [1, 2, 3]:
						for solver_type in ["midpoint", "heun"]:
								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      ,  algorithm_type='''dpmsolver++'''      ,  solver_order=__UpperCamelCase      ,  solver_type=__UpperCamelCase      ,  )







		def       __lowercase (		self )					-> Dict:


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







		def       __lowercase (		self )					-> List[Any]:


				for algorithm_type in ["dpmsolver", "dpmsolver++", "sde-dpmsolver", "sde-dpmsolver++"]:
						for solver_type in ["midpoint", "heun"]:
								for order in [1, 2, 3]:
										for prediction_type in ["epsilon", "sample"]:
												if algorithm_type in ["sde-dpmsolver", "sde-dpmsolver++"]:
														if order == 3:
																continue
												else:
														self.check_over_configs(
														    solver_order=__UpperCamelCase      ,  solver_type=__UpperCamelCase      ,  prediction_type=__UpperCamelCase      ,  algorithm_type=__UpperCamelCase      ,  )
												lowerCamelCase :			List[Any]    		=self.full_loop(
												    solver_order=__UpperCamelCase      ,  solver_type=__UpperCamelCase      ,  prediction_type=__UpperCamelCase      ,  algorithm_type=__UpperCamelCase      ,  )
												assert not torch.isnan(__UpperCamelCase ).any(), "Samples have nan numbers"







		def       __lowercase (		self )					-> List[Any]:
				self.check_over_configs(lower_order_final=__UpperCamelCase )
				self.check_over_configs(lower_order_final=__UpperCamelCase )







		def       __lowercase (		self )					-> Union[str, Any]:
				self.check_over_configs(lambda_min_clipped=-float('''inf''' ) )
				self.check_over_configs(lambda_min_clipped=-5.1 )







		def       __lowercase (		self )					-> Optional[Any]:
				self.check_over_configs(variance_type=__UpperCamelCase )
				self.check_over_configs(variance_type='''learned_range''' )







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


				for num_inference_steps in [1, 2, 3, 5, 1_0, 5_0, 1_0_0, 9_9_9, 1_0_0_0]:
						self.check_over_forward(num_inference_steps=__UpperCamelCase      ,  time_step=0 )







		def       __lowercase (		self )					-> List[Any]:
				lowerCamelCase :			Dict    		=self.full_loop()
				lowerCamelCase :			Any    		=torch.mean(torch.abs(__UpperCamelCase ) )

				assert abs(result_mean.item() - 0.3_3_0_1 ) < 1e-3







		def       __lowercase (		self )					-> str:
				lowerCamelCase :			List[Any]    		=self.full_loop(thresholding=__UpperCamelCase      ,  dynamic_thresholding_ratio=0.8_7      ,  sample_max_value=0.5 )
				lowerCamelCase :			str    		=torch.mean(torch.abs(__UpperCamelCase ) )

				assert abs(result_mean.item() - 1.1_3_6_4 ) < 1e-3







		def       __lowercase (		self )					-> Union[str, Any]:
				lowerCamelCase :			Dict    		=self.full_loop(prediction_type='''v_prediction''' )
				lowerCamelCase :			Dict    		=torch.mean(torch.abs(__UpperCamelCase ) )

				assert abs(result_mean.item() - 0.2_2_5_1 ) < 1e-3







		def       __lowercase (		self )					-> Union[str, Any]:
				lowerCamelCase :			Any    		=self.full_loop(prediction_type='''v_prediction'''      ,  use_karras_sigmas=__UpperCamelCase )
				lowerCamelCase :			Optional[Any]    		=torch.mean(torch.abs(__UpperCamelCase ) )

				assert abs(result_mean.item() - 0.2_0_9_6 ) < 1e-3







		def       __lowercase (		self )					-> List[Any]:
				lowerCamelCase :			int    		=DPMSolverMultistepScheduler(**self.get_scheduler_config() )
				lowerCamelCase :			int    		=self.full_loop(scheduler=__UpperCamelCase )
				lowerCamelCase :			Any    		=torch.mean(torch.abs(__UpperCamelCase ) )

				assert abs(result_mean.item() - 0.3_3_0_1 ) < 1e-3

				lowerCamelCase :			List[str]    		=DPMSolverSinglestepScheduler.from_config(scheduler.config )
				lowerCamelCase :			str    		=UniPCMultistepScheduler.from_config(scheduler.config )
				lowerCamelCase :			List[Any]    		=DEISMultistepScheduler.from_config(scheduler.config )
				lowerCamelCase :			Dict    		=DPMSolverMultistepScheduler.from_config(scheduler.config )

				lowerCamelCase :			str    		=self.full_loop(scheduler=__UpperCamelCase )
				lowerCamelCase :			str    		=torch.mean(torch.abs(__UpperCamelCase ) )

				assert abs(result_mean.item() - 0.3_3_0_1 ) < 1e-3







		def       __lowercase (		self )					-> Any:
				lowerCamelCase :			List[Any]    		=self.scheduler_classes[0]
				lowerCamelCase :			List[str]    		=self.get_scheduler_config(thresholding=__UpperCamelCase      ,  dynamic_thresholding_ratio=0 )
				lowerCamelCase :			List[Any]    		=scheduler_class(**__UpperCamelCase )

				lowerCamelCase :			Any    		=1_0
				lowerCamelCase :			Optional[Any]    		=self.dummy_model()
				lowerCamelCase :			Optional[Any]    		=self.dummy_sample_deter.half()
				scheduler.set_timesteps(__UpperCamelCase )

				for i, t in enumerate(scheduler.timesteps ):
						lowerCamelCase :			Union[str, Any]    		=model(__UpperCamelCase      ,  __UpperCamelCase )
						lowerCamelCase :			List[str]    		=scheduler.step(__UpperCamelCase      ,  __UpperCamelCase      ,  __UpperCamelCase ).prev_sample

				assert sample.dtype == torch.floataa







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


				for scheduler_class in self.scheduler_classes:
						lowerCamelCase :			Dict    		=self.get_scheduler_config(**__UpperCamelCase )
						lowerCamelCase :			Tuple    		=scheduler_class(**__UpperCamelCase )

						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							=       {
    '''facebook/opt-125m''': '''https://huggingface.co/facebook/opt-125m/blob/main/config.json''',
    '''facebook/opt-350m''': '''https://huggingface.co/facebook/opt-350m/blob/main/config.json''',
    '''facebook/opt-1.3b''': '''https://huggingface.co/facebook/opt-1.3b/blob/main/config.json''',
    '''facebook/opt-2.7b''': '''https://huggingface.co/facebook/opt-2.7b/blob/main/config.json''',
    '''facebook/opt-6.7b''': '''https://huggingface.co/facebook/opt-6.7b/blob/main/config.json''',
    '''facebook/opt-13b''': '''https://huggingface.co/facebook/opt-13b/blob/main/config.json''',
}

class 		lowercase_							(							__SCREAMING_SNAKE_CASE					):
 A__    :					Dict    =     """opt"""
 A__    :					int    =     ["""past_key_values"""]

 def __init__(   self  ,						__UpperCamelCase=5_0_2_7_2  ,						__UpperCamelCase=7_6_8  ,						__UpperCamelCase=1_2  ,						__UpperCamelCase=3_0_7_2  ,						__UpperCamelCase=2_0_4_8  ,						__UpperCamelCase=True  ,						__UpperCamelCase=False  ,						__UpperCamelCase=None  ,						__UpperCamelCase=0.1  ,						__UpperCamelCase=0.0  ,						__UpperCamelCase=1_2  ,						__UpperCamelCase="relu"  ,						__UpperCamelCase=0.0  ,						__UpperCamelCase=0.02  ,						__UpperCamelCase=True  ,						__UpperCamelCase=1  ,						__UpperCamelCase=2  ,						__UpperCamelCase=2  ,						__UpperCamelCase=True  ,						__UpperCamelCase=True  ,						**__UpperCamelCase  ,						):




        """simple docstring"""


        super().__init__(
            pad_token_id=__UpperCamelCase  ,						bos_token_id=__UpperCamelCase  ,						eos_token_id=__UpperCamelCase  ,						**__UpperCamelCase  ,						)
        UpperCamelCase_											=							vocab_size
        UpperCamelCase_											=							max_position_embeddings
        UpperCamelCase_											=							num_attention_heads
        UpperCamelCase_											=							word_embed_proj_dim if word_embed_proj_dim is not None else hidden_size
        UpperCamelCase_											=							ffn_dim
        UpperCamelCase_											=							hidden_size
        UpperCamelCase_											=							num_hidden_layers
        UpperCamelCase_											=							dropout
        UpperCamelCase_											=							attention_dropout
        UpperCamelCase_											=							activation_function
        UpperCamelCase_											=							init_std
        UpperCamelCase_											=							layerdrop
        UpperCamelCase_											=							use_cache
        UpperCamelCase_											=							do_layer_norm_before
        # We keep these variables at `True` for backward compatibility.
        UpperCamelCase_											=							enable_bias
        UpperCamelCase_											=							layer_norm_elementwise_affine

        # Note that the only purpose of `_remove_final_layer_norm` is to keep backward compatibility
        # with checkpoints that have been fine-tuned before transformers v4.20.1
        # see https://github.com/facebookresearch/metaseq/pull/164
        UpperCamelCase_											=							_remove_final_layer_norm



