




"""simple docstring"""






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCamelCase__						=			{
    '''configuration_distilbert''': [
        '''DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''DistilBertConfig''',
        '''DistilBertOnnxConfig''',
    ],
    '''tokenization_distilbert''': ['''DistilBertTokenizer'''],
}

try:
				if not is_tokenizers_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCamelCase__						=			['''DistilBertTokenizerFast''']

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCamelCase__						=			[
				    '''DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST''',
				    '''DistilBertForMaskedLM''',
				    '''DistilBertForMultipleChoice''',
				    '''DistilBertForQuestionAnswering''',
				    '''DistilBertForSequenceClassification''',
				    '''DistilBertForTokenClassification''',
				    '''DistilBertModel''',
				    '''DistilBertPreTrainedModel''',
				]

try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCamelCase__						=			[
				    '''TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST''',
				    '''TFDistilBertForMaskedLM''',
				    '''TFDistilBertForMultipleChoice''',
				    '''TFDistilBertForQuestionAnswering''',
				    '''TFDistilBertForSequenceClassification''',
				    '''TFDistilBertForTokenClassification''',
				    '''TFDistilBertMainLayer''',
				    '''TFDistilBertModel''',
				    '''TFDistilBertPreTrainedModel''',
				]

try:
				if not is_flax_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCamelCase__						=			[
				    '''FlaxDistilBertForMaskedLM''',
				    '''FlaxDistilBertForMultipleChoice''',
				    '''FlaxDistilBertForQuestionAnswering''',
				    '''FlaxDistilBertForSequenceClassification''',
				    '''FlaxDistilBertForTokenClassification''',
				    '''FlaxDistilBertModel''',
				    '''FlaxDistilBertPreTrainedModel''',
				]


if TYPE_CHECKING:
				from .configuration_distilbert import (
				    DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
				    DistilBertConfig,
				    DistilBertOnnxConfig,
				)
				from .tokenization_distilbert import DistilBertTokenizer

				try:
								if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_distilbert_fast import DistilBertTokenizerFast

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_distilbert import (
								    DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    DistilBertForMaskedLM,
								    DistilBertForMultipleChoice,
								    DistilBertForQuestionAnswering,
								    DistilBertForSequenceClassification,
								    DistilBertForTokenClassification,
								    DistilBertModel,
								    DistilBertPreTrainedModel,
								)

				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_distilbert import (
								    TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TFDistilBertForMaskedLM,
								    TFDistilBertForMultipleChoice,
								    TFDistilBertForQuestionAnswering,
								    TFDistilBertForSequenceClassification,
								    TFDistilBertForTokenClassification,
								    TFDistilBertMainLayer,
								    TFDistilBertModel,
								    TFDistilBertPreTrainedModel,
								)

				try:
								if not is_flax_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_flax_distilbert import (
								    FlaxDistilBertForMaskedLM,
								    FlaxDistilBertForMultipleChoice,
								    FlaxDistilBertForQuestionAnswering,
								    FlaxDistilBertForSequenceClassification,
								    FlaxDistilBertForTokenClassification,
								    FlaxDistilBertModel,
								    FlaxDistilBertPreTrainedModel,
								)

else:
				import sys

				UpperCamelCase__						=			_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)












"""simple docstring"""






import torch

from diffusers import KDPMaDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest




class   a__			(       UpperCamelCase_  ):
						snake_case__             = (KDPMaDiscreteScheduler,)
						snake_case__             = 1_0





						def       __UpperCamelCase     (							self      :	Dict     ,**a__      :	str)						->       int:




											"""simple docstring"""







											_lowerCAmelCase:Optional[int]									=  {
											    '''num_train_timesteps''': 1100,
											    '''beta_start''': 0.0001,
											    '''beta_end''': 0.02,
											    '''beta_schedule''': '''linear''',
											}

											config.update(**a__)
											return config





						def       __UpperCamelCase     (							self      :	Optional[int])						->       Optional[int]:




											"""simple docstring"""







											for timesteps in [10, 50, 100, 1000]:
																self.check_over_configs(num_train_timesteps=a__)





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




											"""simple docstring"""







											for beta_start, beta_end in zip([0.00001, 0.0001, 0.001]     ,[0.0002, 0.002, 0.02]):
																self.check_over_configs(beta_start=a__     ,beta_end=a__)





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




											"""simple docstring"""







											for schedule in ["linear", "scaled_linear"]:
																self.check_over_configs(beta_schedule=a__)





						def       __UpperCamelCase     (							self      :	str)						->       str:




											"""simple docstring"""







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





						def       __UpperCamelCase     (							self      :	Tuple)						->       Tuple:




											"""simple docstring"""







											_lowerCAmelCase:Union[str, Any]									=  self.scheduler_classes[0]
											_lowerCAmelCase:Union[str, Any]									=  self.get_scheduler_config(prediction_type='''v_prediction''')
											_lowerCAmelCase:Tuple									=  scheduler_class(**a__)

											scheduler.set_timesteps(self.num_inference_steps)

											_lowerCAmelCase:Optional[Any]									=  self.dummy_model()
											_lowerCAmelCase:List[Any]									=  self.dummy_sample_deter * scheduler.init_noise_sigma
											_lowerCAmelCase:Optional[Any]									=  sample.to(a__)

											for i, t in enumerate(scheduler.timesteps):
																_lowerCAmelCase:Tuple									=  scheduler.scale_model_input(a__     ,a__)

																_lowerCAmelCase:Any									=  model(a__     ,a__)

																_lowerCAmelCase:Union[str, Any]									=  scheduler.step(a__     ,a__     ,a__)
																_lowerCAmelCase:Optional[int]									=  output.prev_sample

											_lowerCAmelCase:str									=  torch.sum(torch.abs(a__))
											_lowerCAmelCase:Union[str, Any]									=  torch.mean(torch.abs(a__))

											if torch_device in ["cpu", "mps"]:
																assert abs(result_sum.item() - 4.6_934E-07) < 1E-2
																assert abs(result_mean.item() - 6.1_112E-10) < 1E-3
											else:
																# CUDA
																assert abs(result_sum.item() - 4.693_428_650_170_972E-07) < 1E-2
																assert abs(result_mean.item() - 0.0002) < 1E-3





						def       __UpperCamelCase     (							self      :	Tuple)						->       Dict:




											"""simple docstring"""







											if torch_device == "mps":
																return
											_lowerCAmelCase:Tuple									=  self.scheduler_classes[0]
											_lowerCAmelCase:List[str]									=  self.get_scheduler_config()
											_lowerCAmelCase:Union[str, Any]									=  scheduler_class(**a__)

											scheduler.set_timesteps(self.num_inference_steps)

											_lowerCAmelCase:Any									=  self.dummy_model()
											_lowerCAmelCase:str									=  self.dummy_sample_deter * scheduler.init_noise_sigma
											_lowerCAmelCase:List[str]									=  sample.to(a__)

											for i, t in enumerate(scheduler.timesteps):
																_lowerCAmelCase:Tuple									=  scheduler.scale_model_input(a__     ,a__)

																_lowerCAmelCase:List[str]									=  model(a__     ,a__)

																_lowerCAmelCase:int									=  scheduler.step(a__     ,a__     ,a__)
																_lowerCAmelCase:List[Any]									=  output.prev_sample

											_lowerCAmelCase:Tuple									=  torch.sum(torch.abs(a__))
											_lowerCAmelCase:Dict									=  torch.mean(torch.abs(a__))

											if torch_device in ["cpu", "mps"]:
																assert abs(result_sum.item() - 20.4125) < 1E-2
																assert abs(result_mean.item() - 0.0266) < 1E-3
											else:
																# CUDA
																assert abs(result_sum.item() - 20.4125) < 1E-2
																assert abs(result_mean.item() - 0.0266) < 1E-3







						def       __UpperCamelCase     (							self      :	Union[str, Any])						->       List[Any]:




											"""simple docstring"""







											if torch_device == "mps":
																return
											_lowerCAmelCase:str									=  self.scheduler_classes[0]
											_lowerCAmelCase:Optional[Any]									=  self.get_scheduler_config()
											_lowerCAmelCase:Any									=  scheduler_class(**a__)

											scheduler.set_timesteps(self.num_inference_steps     ,device=a__)

											_lowerCAmelCase:Optional[int]									=  self.dummy_model()
											_lowerCAmelCase:Optional[Any]									=  self.dummy_sample_deter.to(a__) * scheduler.init_noise_sigma

											for t in scheduler.timesteps:
																_lowerCAmelCase:Union[str, Any]									=  scheduler.scale_model_input(a__     ,a__)

																_lowerCAmelCase:Tuple									=  model(a__     ,a__)

																_lowerCAmelCase:str									=  scheduler.step(a__     ,a__     ,a__)
																_lowerCAmelCase:Dict									=  output.prev_sample

											_lowerCAmelCase:Any									=  torch.sum(torch.abs(a__))
											_lowerCAmelCase:str									=  torch.mean(torch.abs(a__))

											if str(a__).startswith('''cpu'''):
																# The following sum varies between 148 and 156 on mps. Why?
																assert abs(result_sum.item() - 20.4125) < 1E-2
																assert abs(result_mean.item() - 0.0266) < 1E-3
											else:
																# CUDA
																assert abs(result_sum.item() - 20.4125) < 1E-2
																assert abs(result_mean.item() - 0.0266) < 1E-3






