



import tempfile

import torch

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

from .test_schedulers import SchedulerCommonTest



class     __snake_case						(					UpperCamelCase_						):
							_a      				=			(DPMSolverMultistepScheduler,)
							_a      				=			(('''num_inference_steps''', 25),)




							def        UpperCAmelCase__      (	self					:	Tuple    ,    **A_					:	Dict):
									lowerCAmelCase_      :					int     =			{
									    '''num_train_timesteps''': 1_0_0_0,
									    '''beta_start''': 0.0001,
									    '''beta_end''': 0.02,
									    '''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(**A_)
									return config




							def        UpperCAmelCase__      (	self					:	Tuple    ,    A_					:	Optional[Any]=0    ,    **A_					:	Tuple):
									lowerCAmelCase_      :					Optional[int]     =			dict(self.forward_default_kwargs)
									lowerCAmelCase_      :					Any     =			kwargs.pop('''num_inference_steps'''    ,    A_)
									lowerCAmelCase_      :					Tuple     =			self.dummy_sample
									lowerCAmelCase_      :					Dict     =			0.1 * sample
									lowerCAmelCase_      :					Optional[Any]     =			[residual + 0.2, residual + 0.15, residual + 0.10]

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

											with tempfile.TemporaryDirectory() as tmpdirname:
													scheduler.save_config(A_)
													lowerCAmelCase_      :					Optional[int]     =			scheduler_class.from_pretrained(A_)
													new_scheduler.set_timesteps(A_)
													# copy over dummy past residuals
													lowerCAmelCase_      :					Union[str, Any]     =			dummy_past_residuals[: new_scheduler.config.solver_order]

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




							def        UpperCAmelCase__      (	self					:	int    ,    A_					:	List[str]=0    ,    **A_					:	Union[str, Any]):
									lowerCAmelCase_      :					Tuple     =			dict(self.forward_default_kwargs)
									lowerCAmelCase_      :					List[str]     =			kwargs.pop('''num_inference_steps'''    ,    A_)
									lowerCAmelCase_      :					Optional[int]     =			self.dummy_sample
									lowerCAmelCase_      :					Optional[Any]     =			0.1 * sample
									lowerCAmelCase_      :					List[str]     =			[residual + 0.2, residual + 0.15, residual + 0.10]

									for scheduler_class in self.scheduler_classes:
											lowerCAmelCase_      :					Tuple     =			self.get_scheduler_config()
											lowerCAmelCase_      :					List[Any]     =			scheduler_class(**A_)
											scheduler.set_timesteps(A_)

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

											with tempfile.TemporaryDirectory() as tmpdirname:
													scheduler.save_config(A_)
													lowerCAmelCase_      :					str     =			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)
													lowerCAmelCase_      :					List[str]     =			dummy_past_residuals[: new_scheduler.config.solver_order]

											lowerCAmelCase_      :					List[Any]     =			scheduler.step(A_    ,    A_    ,    A_    ,    **A_).prev_sample
											lowerCAmelCase_      :					Any     =			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        UpperCAmelCase__      (	self					:	int    ,    A_					:	Union[str, Any]=None    ,    **A_					:	Any):
									if scheduler is None:
											lowerCAmelCase_      :					Dict     =			self.scheduler_classes[0]
											lowerCAmelCase_      :					Optional[Any]     =			self.get_scheduler_config(**A_)
											lowerCAmelCase_      :					Optional[int]     =			scheduler_class(**A_)

									lowerCAmelCase_      :					Tuple     =			1_0
									lowerCAmelCase_      :					List[str]     =			self.dummy_model()
									lowerCAmelCase_      :					List[Any]     =			self.dummy_sample_deter
									scheduler.set_timesteps(A_)

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

									return sample




							def        UpperCAmelCase__      (	self					:	int):
									lowerCAmelCase_      :					Dict     =			dict(self.forward_default_kwargs)

									lowerCAmelCase_      :					Optional[Any]     =			kwargs.pop('''num_inference_steps'''    ,    A_)

									for scheduler_class in self.scheduler_classes:
											lowerCAmelCase_      :					Optional[int]     =			self.get_scheduler_config()
											lowerCAmelCase_      :					List[str]     =			scheduler_class(**A_)

											lowerCAmelCase_      :					List[str]     =			self.dummy_sample
											lowerCAmelCase_      :					List[str]     =			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'''):
													lowerCAmelCase_      :					str     =			num_inference_steps

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

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

											lowerCAmelCase_      :					List[Any]     =			scheduler.step(A_    ,    A_    ,    A_    ,    **A_).prev_sample
											lowerCAmelCase_      :					Any     =			scheduler.step(A_    ,    A_    ,    A_    ,    **A_).prev_sample

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




							def        UpperCAmelCase__      (	self					:	Optional[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=A_)




							def        UpperCAmelCase__      (	self					:	str):
									self.check_over_configs(thresholding=A_)
									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=A_    ,    prediction_type=A_    ,    sample_max_value=A_    ,    algorithm_type='''dpmsolver++'''    ,    solver_order=A_    ,    solver_type=A_    ,    )




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




							def        UpperCAmelCase__      (	self					:	Optional[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=A_    ,    solver_type=A_    ,    prediction_type=A_    ,    algorithm_type=A_    ,    )
																	lowerCAmelCase_      :					Tuple     =			self.full_loop(
																	    solver_order=A_    ,    solver_type=A_    ,    prediction_type=A_    ,    algorithm_type=A_    ,    )
																	assert not torch.isnan(A_).any(), "Samples have nan numbers"




							def        UpperCAmelCase__      (	self					:	Dict):
									self.check_over_configs(lower_order_final=A_)
									self.check_over_configs(lower_order_final=A_)




							def        UpperCAmelCase__      (	self					:	List[str]):
									self.check_over_configs(lambda_min_clipped=-float('''inf'''))
									self.check_over_configs(lambda_min_clipped=-5.1)




							def        UpperCAmelCase__      (	self					:	Tuple):
									self.check_over_configs(variance_type=A_)
									self.check_over_configs(variance_type='''learned_range''')




							def        UpperCAmelCase__      (	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=A_    ,    time_step=0)




							def        UpperCAmelCase__      (	self					:	Optional[Any]):
									lowerCAmelCase_      :					List[str]     =			self.full_loop()
									lowerCAmelCase_      :					Optional[int]     =			torch.mean(torch.abs(A_))

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




							def        UpperCAmelCase__      (	self					:	List[str]):
									lowerCAmelCase_      :					Tuple     =			self.full_loop(thresholding=A_    ,    dynamic_thresholding_ratio=0.87    ,    sample_max_value=0.5)
									lowerCAmelCase_      :					Tuple     =			torch.mean(torch.abs(A_))

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




							def        UpperCAmelCase__      (	self					:	str):
									lowerCAmelCase_      :					Tuple     =			self.full_loop(prediction_type='''v_prediction''')
									lowerCAmelCase_      :					int     =			torch.mean(torch.abs(A_))

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




							def        UpperCAmelCase__      (	self					:	str):
									lowerCAmelCase_      :					List[str]     =			self.full_loop(prediction_type='''v_prediction'''    ,    use_karras_sigmas=A_)
									lowerCAmelCase_      :					Optional[int]     =			torch.mean(torch.abs(A_))

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




							def        UpperCAmelCase__      (	self					:	Union[str, Any]):
									# make sure that iterating over schedulers with same config names gives same results
									# for defaults
									lowerCAmelCase_      :					str     =			DPMSolverMultistepScheduler(**self.get_scheduler_config())
									lowerCAmelCase_      :					Any     =			self.full_loop(scheduler=A_)
									lowerCAmelCase_      :					Tuple     =			torch.mean(torch.abs(A_))

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

									lowerCAmelCase_      :					Tuple     =			DPMSolverSinglestepScheduler.from_config(scheduler.config)
									lowerCAmelCase_      :					Union[str, Any]     =			UniPCMultistepScheduler.from_config(scheduler.config)
									lowerCAmelCase_      :					Dict     =			DEISMultistepScheduler.from_config(scheduler.config)
									lowerCAmelCase_      :					Union[str, Any]     =			DPMSolverMultistepScheduler.from_config(scheduler.config)

									lowerCAmelCase_      :					Optional[Any]     =			self.full_loop(scheduler=A_)
									lowerCAmelCase_      :					str     =			torch.mean(torch.abs(A_))

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




							def        UpperCAmelCase__      (	self					:	Tuple):
									lowerCAmelCase_      :					Optional[int]     =			self.scheduler_classes[0]
									lowerCAmelCase_      :					Any     =			self.get_scheduler_config(thresholding=A_    ,    dynamic_thresholding_ratio=0)
									lowerCAmelCase_      :					Dict     =			scheduler_class(**A_)

									lowerCAmelCase_      :					Any     =			1_0
									lowerCAmelCase_      :					Union[str, Any]     =			self.dummy_model()
									lowerCAmelCase_      :					List[str]     =			self.dummy_sample_deter.half()
									scheduler.set_timesteps(A_)

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

									assert sample.dtype == torch.floataa







							def        UpperCAmelCase__      (	self					:	Union[str, Any]    ,    **A_					:	Optional[int]):
									for scheduler_class in self.scheduler_classes:
											lowerCAmelCase_      :					Optional[int]     =			self.get_scheduler_config(**A_)
											lowerCAmelCase_      :					Optional[int]     =			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__  :  Tuple       = logging.get_logger(__name__)

A__  :  Tuple       = {
    '''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     __snake_case						(					UpperCamelCase_						):
							_a      				=			'''opt'''
							_a      				=			['''past_key_values''']




							def __init__(	self					:	Optional[int]    ,    A_					:	Any=5_0_2_7_2    ,    A_					:	Tuple=7_6_8    ,    A_					:	Optional[Any]=1_2    ,    A_					:	str=3_0_7_2    ,    A_					:	Dict=2_0_4_8    ,    A_					:	List[Any]=True    ,    A_					:	Optional[int]=False    ,    A_					:	Optional[int]=None    ,    A_					:	Dict=0.1    ,    A_					:	str=0.0    ,    A_					:	Union[str, Any]=1_2    ,    A_					:	List[str]="relu"    ,    A_					:	Dict=0.0    ,    A_					:	Union[str, Any]=0.02    ,    A_					:	Optional[Any]=True    ,    A_					:	Optional[Any]=1    ,    A_					:	List[Any]=2    ,    A_					:	Dict=2    ,    A_					:	Optional[int]=True    ,    A_					:	Optional[int]=True    ,    **A_					:	List[Any]    ,    ):
									super().__init__(
									    pad_token_id=A_    ,    bos_token_id=A_    ,    eos_token_id=A_    ,    **A_    ,    )
									lowerCAmelCase_      :					List[Any]     =			vocab_size
									lowerCAmelCase_      :					Union[str, Any]     =			max_position_embeddings
									lowerCAmelCase_      :					Optional[int]     =			num_attention_heads
									lowerCAmelCase_      :					int     =			word_embed_proj_dim if word_embed_proj_dim is not None else hidden_size
									lowerCAmelCase_      :					List[Any]     =			ffn_dim
									lowerCAmelCase_      :					Any     =			hidden_size
									lowerCAmelCase_      :					Union[str, Any]     =			num_hidden_layers
									lowerCAmelCase_      :					str     =			dropout
									lowerCAmelCase_      :					List[str]     =			attention_dropout
									lowerCAmelCase_      :					Any     =			activation_function
									lowerCAmelCase_      :					Union[str, Any]     =			init_std
									lowerCAmelCase_      :					int     =			layerdrop
									lowerCAmelCase_      :					Optional[int]     =			use_cache
									lowerCAmelCase_      :					int     =			do_layer_norm_before
									# We keep these variables at `True` for backward compatibility.
									lowerCAmelCase_      :					Optional[Any]     =			enable_bias
									lowerCAmelCase_      :					Optional[int]     =			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
									lowerCAmelCase_      :					Optional[Any]     =			_remove_final_layer_norm





