





'''simple docstring'''


from __future__ import annotations

import unittest

from transformers import DebertaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import tensorflow as tf

				from transformers import (
				    TFDebertaForMaskedLM,
				    TFDebertaForQuestionAnswering,
				    TFDebertaForSequenceClassification,
				    TFDebertaForTokenClassification,
				    TFDebertaModel,
				)



class 							__UpperCAmelCase  :







		'''simple docstring'''







		def __init__(self						:      Optional[Any]      ,      _lowerCAmelCase						:      str      ,      _lowerCAmelCase						:      List[Any]=13      ,      _lowerCAmelCase						:      int=7      ,      _lowerCAmelCase						:      int=True      ,      _lowerCAmelCase						:      List[Any]=True      ,      _lowerCAmelCase						:      int=True      ,      _lowerCAmelCase						:      Dict=True      ,      _lowerCAmelCase						:      List[Any]=99      ,      _lowerCAmelCase						:      Tuple=32      ,      _lowerCAmelCase						:      int=2      ,      _lowerCAmelCase						:      str=4      ,      _lowerCAmelCase						:      Any=37      ,      _lowerCAmelCase						:      Tuple="gelu"      ,      _lowerCAmelCase						:      Tuple=0.1      ,      _lowerCAmelCase						:      str=0.1      ,      _lowerCAmelCase						:      List[Any]=512      ,      _lowerCAmelCase						:      Dict=16      ,      _lowerCAmelCase						:      Tuple=2      ,      _lowerCAmelCase						:      List[str]=0.02      ,      _lowerCAmelCase						:      List[Any]=3      ,      _lowerCAmelCase						:      List[Any]=4      ,      _lowerCAmelCase						:      str=None      ,      ):
			A							  =      parent
			A							  =      13
			A							  =      7
			A							  =      True
			A							  =      True
			A							  =      True
			A							  =      True
			A							  =      99
			A							  =      32
			A							  =      2
			A							  =      4
			A							  =      37
			A							  =      """gelu"""
			A							  =      0.1
			A							  =      0.1
			A							  =      512
			A							  =      16
			A							  =      False
			A							  =      -1
			A							  =      True
			A							  =      2
			A							  =      0.02
			A							  =      3
			A							  =      4
			A							  =      None







		def        A	(self						:      List[str]      ):
			A							  =      ids_tensor([self.batch_size, self.seq_length]      ,      self.vocab_size      )

			A							  =      None
			if self.use_input_mask:
				A							  =      random_attention_mask([self.batch_size, self.seq_length]      )

			A							  =      None
			if self.use_token_type_ids:
				A							  =      ids_tensor([self.batch_size, self.seq_length]      ,      self.type_vocab_size      )

			A							  =      None
			A							  =      None
			A							  =      None
			if self.use_labels:
				A							  =      ids_tensor([self.batch_size]      ,      self.type_sequence_label_size      )
				A							  =      ids_tensor([self.batch_size, self.seq_length]      ,      self.num_labels      )

			A							  =      DebertaConfig(
			    vocab_size=self.vocab_size      ,      hidden_size=self.hidden_size      ,      num_hidden_layers=self.num_hidden_layers      ,      num_attention_heads=self.num_attention_heads      ,      intermediate_size=self.intermediate_size      ,      hidden_act=self.hidden_act      ,      hidden_dropout_prob=self.hidden_dropout_prob      ,      attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,      max_position_embeddings=self.max_position_embeddings      ,      type_vocab_size=self.type_vocab_size      ,      relative_attention=self.relative_attention      ,      max_relative_positions=self.max_relative_positions      ,      position_biased_input=self.position_biased_input      ,      initializer_range=self.initializer_range      ,      return_dict=_lowerCAmelCase      ,      )

			return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







		def        A	(self						:      Union[str, Any]      ,      _lowerCAmelCase						:      str      ,      _lowerCAmelCase						:      int      ,      _lowerCAmelCase						:      List[Any]      ,      _lowerCAmelCase						:      Tuple      ,      _lowerCAmelCase						:      List[str]      ,      _lowerCAmelCase						:      Union[str, Any]      ,      _lowerCAmelCase						:      Tuple      ):
			A							  =      TFDebertaModel(config=_lowerCAmelCase      )
			A							  =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

			A							  =      [input_ids, input_mask]
			A							  =      model(_lowerCAmelCase      )

			A							  =      model(_lowerCAmelCase      )

			self.parent.assertEqual(result.last_hidden_state.shape      ,      (self.batch_size, self.seq_length, self.hidden_size)      )







		def        A	(self						:      str      ,      _lowerCAmelCase						:      str      ,      _lowerCAmelCase						:      Optional[Any]      ,      _lowerCAmelCase						:      List[Any]      ,      _lowerCAmelCase						:      str      ,      _lowerCAmelCase						:      Optional[Any]      ,      _lowerCAmelCase						:      Optional[int]      ,      _lowerCAmelCase						:      str      ):
			A							  =      TFDebertaForMaskedLM(config=_lowerCAmelCase      )
			A							  =      {
			    """input_ids""": input_ids,
			    """attention_mask""": input_mask,
			    """token_type_ids""": token_type_ids,
			}
			A							  =      model(_lowerCAmelCase      )
			self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.vocab_size)      )







		def        A	(self						:      Optional[int]      ,      _lowerCAmelCase						:      Any      ,      _lowerCAmelCase						:      Optional[int]      ,      _lowerCAmelCase						:      Union[str, Any]      ,      _lowerCAmelCase						:      Optional[int]      ,      _lowerCAmelCase						:      List[str]      ,      _lowerCAmelCase						:      List[str]      ,      _lowerCAmelCase						:      int      ):
			A							  =      self.num_labels
			A							  =      TFDebertaForSequenceClassification(config=_lowerCAmelCase      )
			A							  =      {
			    """input_ids""": input_ids,
			    """attention_mask""": input_mask,
			    """token_type_ids""": token_type_ids,
			}

			A							  =      model(_lowerCAmelCase      )
			self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.num_labels)      )







		def        A	(self						:      List[Any]      ,      _lowerCAmelCase						:      str      ,      _lowerCAmelCase						:      Optional[Any]      ,      _lowerCAmelCase						:      Union[str, Any]      ,      _lowerCAmelCase						:      List[Any]      ,      _lowerCAmelCase						:      Any      ,      _lowerCAmelCase						:      str      ,      _lowerCAmelCase						:      Tuple      ):
			A							  =      self.num_labels
			A							  =      TFDebertaForTokenClassification(config=_lowerCAmelCase      )
			A							  =      {
			    """input_ids""": input_ids,
			    """attention_mask""": input_mask,
			    """token_type_ids""": token_type_ids,
			}
			A							  =      model(_lowerCAmelCase      )
			self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.num_labels)      )







		def        A	(self						:      List[Any]      ,      _lowerCAmelCase						:      Union[str, Any]      ,      _lowerCAmelCase						:      Optional[Any]      ,      _lowerCAmelCase						:      Dict      ,      _lowerCAmelCase						:      Optional[int]      ,      _lowerCAmelCase						:      List[str]      ,      _lowerCAmelCase						:      List[str]      ,      _lowerCAmelCase						:      Union[str, Any]      ):
			A							  =      TFDebertaForQuestionAnswering(config=_lowerCAmelCase      )
			A							  =      {
			    """input_ids""": input_ids,
			    """attention_mask""": input_mask,
			    """token_type_ids""": token_type_ids,
			}

			A							  =      model(_lowerCAmelCase      )
			self.parent.assertEqual(result.start_logits.shape      ,      (self.batch_size, self.seq_length)      )
			self.parent.assertEqual(result.end_logits.shape      ,      (self.batch_size, self.seq_length)      )







		def        A	(self						:      List[str]      ):
			A							  =      self.prepare_config_and_inputs()
			(
			    (
			    A		
			)  ,    (
			    A		
			)  ,    (
			    A		
			)  ,    (
			    A		
			)  ,    (
			    A		
			)  ,    (
			    A		
			)  ,    (
			    A		
			)  ,    		
			)							  =      config_and_inputs
			A							  =      {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
			return config, inputs_dict






@require_tf
class 							__UpperCAmelCase  (       UpperCAmelCase_		,					UpperCAmelCase_		,					unittest.TestCase						):







		'''simple docstring'''







		__lowerCAmelCase 				=    (
		    (
		        TFDebertaModel,
		        TFDebertaForMaskedLM,
		        TFDebertaForQuestionAnswering,
		        TFDebertaForSequenceClassification,
		        TFDebertaForTokenClassification,
		    )
		    if is_tf_available()
		    else ()
		)
		__lowerCAmelCase 				=    (
		    {
		        '''feature-extraction''': TFDebertaModel,
		        '''fill-mask''': TFDebertaForMaskedLM,
		        '''question-answering''': TFDebertaForQuestionAnswering,
		        '''text-classification''': TFDebertaForSequenceClassification,
		        '''token-classification''': TFDebertaForTokenClassification,
		        '''zero-shot''': TFDebertaForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)

		__lowerCAmelCase 				=    False
		__lowerCAmelCase 				=    False







		def        A	(self						:      int      ):
			A							  =      TFDebertaModelTester(self      )
			A							  =      ConfigTester(self      ,      config_class=_lowerCAmelCase      ,      hidden_size=37      )







		def        A	(self						:      Union[str, Any]      ):
			self.config_tester.run_common_tests()







		def        A	(self						:      Tuple      ):
			A							  =      self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*_lowerCAmelCase      )







		def        A	(self						:      Any      ):
			A							  =      self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_masked_lm(*_lowerCAmelCase      )







		def        A	(self						:      Any      ):
			A							  =      self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_question_answering(*_lowerCAmelCase      )







		def        A	(self						:      int      ):
			A							  =      self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_sequence_classification(*_lowerCAmelCase      )







		def        A	(self						:      Dict      ):
			A							  =      self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_token_classification(*_lowerCAmelCase      )







		@slow
		def        A	(self						:      str      ):
			A							  =      TFDebertaModel.from_pretrained("""kamalkraj/deberta-base"""      )
			self.assertIsNotNone(_lowerCAmelCase      )






@require_tf
class 							__UpperCAmelCase  (       unittest.TestCase						):







		'''simple docstring'''







		@unittest.skip(reason="""Model not available yet"""      )
		def        A	(self						:      int      ):
			pass







		@slow
		def        A	(self						:      List[Any]      ):
			A							  =      TFDebertaModel.from_pretrained("""kamalkraj/deberta-base"""      )
			A							  =      tf.constant([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]      )
			A							  =      tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
			A							  =      model(_lowerCAmelCase      ,      attention_mask=_lowerCAmelCase      )[0]

			A							  =      tf.constant(
			    [
			        [
			            [-0.59_855_896, -0.80_552_566, -0.8_462_135],
			            [1.4_484_025, -0.93_483_794, -0.80_593_085],
			            [0.3_122_741, 0.00_316_059, -1.4_131_377],
			        ]
			    ]      )
			tf.debugging.assert_near(output[:, 1:4, 1:4]      ,      _lowerCAmelCase      ,      atol=1e-4      )








'''simple docstring'''


import torch

from diffusers import KDPMaAncestralDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest



class 							__UpperCAmelCase  (       A__						):







		'''simple docstring'''







		__lowerCAmelCase 				=    (KDPMaAncestralDiscreteScheduler,)
		__lowerCAmelCase 				=    10







		def        A     (self						:      Any      ,      **_lowerCAmelCase						:      List[Any]      ):
			A							  =      {
			    """num_train_timesteps""": 1100,
			    """beta_start""": 0.0_001,
			    """beta_end""": 0.02,
			    """beta_schedule""": """linear""",
			}

			config.update(**_lowerCAmelCase      )
			return config







		def        A     (self						:      Dict      ):
			for timesteps in [10, 50, 100, 1000]:
				self.check_over_configs(num_train_timesteps=_lowerCAmelCase      )







		def        A     (self						:      Optional[int]      ):
			for beta_start, beta_end in zip([0.00_001, 0.0_001, 0.001]      ,      [0.0_002, 0.002, 0.02]      ):
				self.check_over_configs(beta_start=_lowerCAmelCase      ,      beta_end=_lowerCAmelCase      )







		def        A     (self						:      Tuple      ):
			for schedule in ["linear", "scaled_linear"]:
				self.check_over_configs(beta_schedule=_lowerCAmelCase      )







		def        A     (self						:      Dict      ):
			if torch_device == "mps":
				return
			A							  =      self.scheduler_classes[0]
			A							  =      self.get_scheduler_config()
			A							  =      scheduler_class(**_lowerCAmelCase      )

			scheduler.set_timesteps(self.num_inference_steps      )

			A							  =      torch.manual_seed(0      )

			A							  =      self.dummy_model()
			A							  =      self.dummy_sample_deter * scheduler.init_noise_sigma
			A							  =      sample.to(_lowerCAmelCase      )

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

				A							  =      model(_lowerCAmelCase      ,      _lowerCAmelCase      )

				A							  =      scheduler.step(_lowerCAmelCase      ,      _lowerCAmelCase      ,      _lowerCAmelCase      ,      generator=_lowerCAmelCase      )
				A							  =      output.prev_sample

			A							  =      torch.sum(torch.abs(_lowerCAmelCase      )      )
			A							  =      torch.mean(torch.abs(_lowerCAmelCase      )      )

			assert abs(result_sum.item() - 13_849.3_877      ) < 1e-2
			assert abs(result_mean.item() - 18.0_331      ) < 5e-3







		def        A     (self						:      Union[str, Any]      ):
			for prediction_type in ["epsilon", "v_prediction"]:
				self.check_over_configs(prediction_type=_lowerCAmelCase      )







		def        A     (self						:      Dict      ):
			if torch_device == "mps":
				return
			A							  =      self.scheduler_classes[0]
			A							  =      self.get_scheduler_config(prediction_type="""v_prediction"""      )
			A							  =      scheduler_class(**_lowerCAmelCase      )

			scheduler.set_timesteps(self.num_inference_steps      )

			A							  =      self.dummy_model()
			A							  =      self.dummy_sample_deter * scheduler.init_noise_sigma
			A							  =      sample.to(_lowerCAmelCase      )

			A							  =      torch.manual_seed(0      )

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

				A							  =      model(_lowerCAmelCase      ,      _lowerCAmelCase      )

				A							  =      scheduler.step(_lowerCAmelCase      ,      _lowerCAmelCase      ,      _lowerCAmelCase      ,      generator=_lowerCAmelCase      )
				A							  =      output.prev_sample

			A							  =      torch.sum(torch.abs(_lowerCAmelCase      )      )
			A							  =      torch.mean(torch.abs(_lowerCAmelCase      )      )

			assert abs(result_sum.item() - 328.9_970      ) < 1e-2
			assert abs(result_mean.item() - 0.4_284      ) < 1e-3







		def        A     (self						:      Optional[int]      ):
			if torch_device == "mps":
				return
			A							  =      self.scheduler_classes[0]
			A							  =      self.get_scheduler_config()
			A							  =      scheduler_class(**_lowerCAmelCase      )

			scheduler.set_timesteps(self.num_inference_steps      ,      device=_lowerCAmelCase      )
			A							  =      torch.manual_seed(0      )

			A							  =      self.dummy_model()
			A							  =      self.dummy_sample_deter.to(_lowerCAmelCase      ) * scheduler.init_noise_sigma

			for t in scheduler.timesteps:
				A							  =      scheduler.scale_model_input(_lowerCAmelCase      ,      _lowerCAmelCase      )

				A							  =      model(_lowerCAmelCase      ,      _lowerCAmelCase      )

				A							  =      scheduler.step(_lowerCAmelCase      ,      _lowerCAmelCase      ,      _lowerCAmelCase      ,      generator=_lowerCAmelCase      )
				A							  =      output.prev_sample

			A							  =      torch.sum(torch.abs(_lowerCAmelCase      )      )
			A							  =      torch.mean(torch.abs(_lowerCAmelCase      )      )

			assert abs(result_sum.item() - 13_849.3_818      ) < 1e-1
			assert abs(result_mean.item() - 18.0_331      ) < 1e-3

