






"""simple docstring"""


import inspect
import re

from transformers.utils import direct_transformers_import


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_config_docstrings.py
__SCREAMING_SNAKE_CASE   =   """src/transformers"""


# This is to make sure the transformers module imported is the one in the repo.
__SCREAMING_SNAKE_CASE   =   direct_transformers_import(PATH_TO_TRANSFORMERS)

__SCREAMING_SNAKE_CASE   =   transformers.models.auto.configuration_auto.CONFIG_MAPPING

# Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`.
# For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)`
__SCREAMING_SNAKE_CASE   =   re.compile(R'\[(.+?)\]\((https://huggingface\.co/.+?)\)')


__SCREAMING_SNAKE_CASE   =   {
    """DecisionTransformerConfig""",
    """EncoderDecoderConfig""",
    """MusicgenConfig""",
    """RagConfig""",
    """SpeechEncoderDecoderConfig""",
    """TimmBackboneConfig""",
    """VisionEncoderDecoderConfig""",
    """VisionTextDualEncoderConfig""",
    """LlamaConfig""",
}


def        UpperCAmelCase      (					a__ ):





						'''simple docstring'''






						lowerCAmelCase   :Dict						=			None

						# source code of `config_class`
						lowerCAmelCase   :str						=			inspect.getsource(_UpperCAmelCase )
						lowerCAmelCase   :str						=			_re_checkpoint.findall(_UpperCAmelCase )

						# Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.
						# For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')`
						for ckpt_name, ckpt_link in checkpoints:
												# allow the link to end with `/`
												if ckpt_link.endswith('/' ):
																		lowerCAmelCase   :int						=			ckpt_link[:-1]

												# verify the checkpoint name corresponds to the checkpoint link
												lowerCAmelCase   :List[Any]						=			F"""https://huggingface.co/{ckpt_name}"""
												if ckpt_link == ckpt_link_from_name:
																		lowerCAmelCase   :List[Any]						=			ckpt_name
																		break

						return checkpoint


def        UpperCAmelCase      (					):





						'''simple docstring'''






						lowerCAmelCase   :List[Any]						=			[]

						for config_class in list(CONFIG_MAPPING.values() ):
												# Skip deprecated models
												if "models.deprecated" in config_class.__module__:
																		continue
												lowerCAmelCase   :Tuple						=			get_checkpoint_from_config_class(_UpperCAmelCase )

												lowerCAmelCase   :int						=			config_class.__name__
												if checkpoint is None and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
																		configs_without_checkpoint.append(_UpperCAmelCase )

						if len(_UpperCAmelCase ) > 0:
												lowerCAmelCase   :List[str]						=			"\n".join(sorted(_UpperCAmelCase ) )
												raise ValueError(F"""The following configurations don't contain any valid checkpoint:\n{message}""" )


if __name__ == "__main__":
		check_config_docstrings_have_checkpoints()

import tempfile

import torch

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

from .test_schedulers import SchedulerCommonTest




class   __lowercase						(						__snake_case       ):
	_A       =   (DEISMultistepScheduler,)
	_A       =   (("num_inference_steps", 25),)






	def       _a(self				: Optional[int]      ,	**snake_case				: str    ) ->			Optional[Any]:
							_lowercase       :	str           =   {
							    "num_train_timesteps": 1000,
							    "beta_start": 0.00_01,
							    "beta_end": 0.02,
							    "beta_schedule": "linear",
							    "solver_order": 2,
							}

							config.update(**snake_case    )
							return config






	def       _a(self				: Optional[int]      ,	snake_case				: Optional[int]=0      ,	**snake_case				: List[Any]    ) ->			Dict:
							_lowercase       :	Dict           =   dict(self.forward_default_kwargs    )
							_lowercase       :	Optional[Any]           =   kwargs.pop("num_inference_steps"      ,	snake_case    )
							_lowercase       :	Optional[int]           =   self.dummy_sample
							_lowercase       :	Tuple           =   0.1 * sample
							_lowercase       :	Optional[Any]           =   [residual + 0.2, residual + 0.15, residual + 0.10]

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

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

													_lowercase      ,    _lowercase       :	Optional[Any]           =   sample, sample
													for t in range(snake_case      ,	time_step + scheduler.config.solver_order + 1    ):
																			_lowercase       :	List[Any]           =   scheduler.step(snake_case      ,	snake_case      ,	snake_case      ,	**snake_case    ).prev_sample
																			_lowercase       :	Union[str, Any]           =   new_scheduler.step(snake_case      ,	snake_case      ,	snake_case      ,	**snake_case    ).prev_sample

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






	def       _a(self				: str    ) ->			List[str]:
							pass






	def       _a(self				: Optional[Any]      ,	snake_case				: List[Any]=0      ,	**snake_case				: str    ) ->			Union[str, Any]:
							_lowercase       :	List[str]           =   dict(self.forward_default_kwargs    )
							_lowercase       :	Optional[int]           =   kwargs.pop("num_inference_steps"      ,	snake_case    )
							_lowercase       :	Dict           =   self.dummy_sample
							_lowercase       :	Union[str, Any]           =   0.1 * sample
							_lowercase       :	List[str]           =   [residual + 0.2, residual + 0.15, residual + 0.10]

							for scheduler_class in self.scheduler_classes:
													_lowercase       :	List[Any]           =   self.get_scheduler_config()
													_lowercase       :	int           =   scheduler_class(**snake_case    )
													scheduler.set_timesteps(snake_case    )

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

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

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

													_lowercase       :	Any           =   scheduler.step(snake_case      ,	snake_case      ,	snake_case      ,	**snake_case    ).prev_sample
													_lowercase       :	Dict           =   new_scheduler.step(snake_case      ,	snake_case      ,	snake_case      ,	**snake_case    ).prev_sample

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






	def       _a(self				: Dict      ,	snake_case				: str=None      ,	**snake_case				: Dict    ) ->			Optional[Any]:
							if scheduler is None:
													_lowercase       :	Optional[Any]           =   self.scheduler_classes[0]
													_lowercase       :	Dict           =   self.get_scheduler_config(**snake_case    )
													_lowercase       :	Optional[int]           =   scheduler_class(**snake_case    )

							_lowercase       :	Tuple           =   self.scheduler_classes[0]
							_lowercase       :	str           =   self.get_scheduler_config(**snake_case    )
							_lowercase       :	Tuple           =   scheduler_class(**snake_case    )

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

							for i, t in enumerate(scheduler.timesteps    ):
													_lowercase       :	str           =   model(snake_case      ,	snake_case    )
													_lowercase       :	int           =   scheduler.step(snake_case      ,	snake_case      ,	snake_case    ).prev_sample

							return sample






	def       _a(self				: Any    ) ->			Tuple:
							_lowercase       :	Union[str, Any]           =   dict(self.forward_default_kwargs    )

							_lowercase       :	Optional[int]           =   kwargs.pop("num_inference_steps"      ,	snake_case    )

							for scheduler_class in self.scheduler_classes:
													_lowercase       :	List[Any]           =   self.get_scheduler_config()
													_lowercase       :	Optional[Any]           =   scheduler_class(**snake_case    )

													_lowercase       :	Tuple           =   self.dummy_sample
													_lowercase       :	List[Any]           =   0.1 * sample

													if num_inference_steps is not None and hasattr(snake_case      ,	"set_timesteps"    ):
																			scheduler.set_timesteps(snake_case    )
													elif num_inference_steps is not None and not hasattr(snake_case      ,	"set_timesteps"    ):
																			_lowercase       :	str           =   num_inference_steps

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

													_lowercase       :	Tuple           =   scheduler.timesteps[5]
													_lowercase       :	Dict           =   scheduler.timesteps[6]

													_lowercase       :	Tuple           =   scheduler.step(snake_case      ,	snake_case      ,	snake_case      ,	**snake_case    ).prev_sample
													_lowercase       :	Union[str, Any]           =   scheduler.step(snake_case      ,	snake_case      ,	snake_case      ,	**snake_case    ).prev_sample

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






	def       _a(self				: Optional[int]    ) ->			List[Any]:
							# make sure that iterating over schedulers with same config names gives same results
							# for defaults
							_lowercase       :	Any           =   DEISMultistepScheduler(**self.get_scheduler_config()    )
							_lowercase       :	List[Any]           =   self.full_loop(scheduler=snake_case    )
							_lowercase       :	Tuple           =   torch.mean(torch.abs(snake_case    )    )

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

							_lowercase       :	str           =   DPMSolverSinglestepScheduler.from_config(scheduler.config    )
							_lowercase       :	Optional[int]           =   DPMSolverMultistepScheduler.from_config(scheduler.config    )
							_lowercase       :	List[Any]           =   UniPCMultistepScheduler.from_config(scheduler.config    )
							_lowercase       :	int           =   DEISMultistepScheduler.from_config(scheduler.config    )

							_lowercase       :	Union[str, Any]           =   self.full_loop(scheduler=snake_case    )
							_lowercase       :	Dict           =   torch.mean(torch.abs(snake_case    )    )

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






	def       _a(self				: Union[str, Any]    ) ->			Optional[int]:
							for timesteps in [25, 50, 100, 999, 1000]:
													self.check_over_configs(num_train_timesteps=snake_case    )






	def       _a(self				: Optional[Any]    ) ->			List[str]:
							self.check_over_configs(thresholding=snake_case    )
							for order in [1, 2, 3]:
													for solver_type in ["logrho"]:
																			for threshold in [0.5, 1.0, 2.0]:
																									for prediction_type in ["epsilon", "sample"]:
																															self.check_over_configs(
																															    thresholding=snake_case      ,	prediction_type=snake_case      ,	sample_max_value=snake_case      ,	algorithm_type="deis"      ,	solver_order=snake_case      ,	solver_type=snake_case      ,	)






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






	def       _a(self				: Tuple    ) ->			List[Any]:
							for algorithm_type in ["deis"]:
													for solver_type in ["logrho"]:
																			for order in [1, 2, 3]:
																									for prediction_type in ["epsilon", "sample"]:
																															self.check_over_configs(
																															    solver_order=snake_case      ,	solver_type=snake_case      ,	prediction_type=snake_case      ,	algorithm_type=snake_case      ,	)
																															_lowercase       :	Any           =   self.full_loop(
																															    solver_order=snake_case      ,	solver_type=snake_case      ,	prediction_type=snake_case      ,	algorithm_type=snake_case      ,	)
																															assert not torch.isnan(snake_case    ).any(), "Samples have nan numbers"






	def       _a(self				: int    ) ->			List[str]:
							self.check_over_configs(lower_order_final=snake_case    )
							self.check_over_configs(lower_order_final=snake_case    )






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






	def       _a(self				: Tuple    ) ->			Optional[Any]:
							_lowercase       :	Optional[Any]           =   self.full_loop()
							_lowercase       :	Tuple           =   torch.mean(torch.abs(snake_case    )    )

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






	def       _a(self				: Optional[int]    ) ->			Dict:
							_lowercase       :	int           =   self.full_loop(prediction_type="v_prediction"    )
							_lowercase       :	str           =   torch.mean(torch.abs(snake_case    )    )

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






	def       _a(self				: int    ) ->			Optional[int]:
							_lowercase       :	int           =   self.scheduler_classes[0]
							_lowercase       :	Tuple           =   self.get_scheduler_config(thresholding=snake_case      ,	dynamic_thresholding_ratio=0    )
							_lowercase       :	int           =   scheduler_class(**snake_case    )

							_lowercase       :	Optional[int]           =   10
							_lowercase       :	Union[str, Any]           =   self.dummy_model()
							_lowercase       :	Dict           =   self.dummy_sample_deter.half()
							scheduler.set_timesteps(snake_case    )

							for i, t in enumerate(scheduler.timesteps    ):
													_lowercase       :	Tuple           =   model(snake_case      ,	snake_case    )
													_lowercase       :	List[str]           =   scheduler.step(snake_case      ,	snake_case      ,	snake_case    ).prev_sample

							assert sample.dtype == torch.floataa




