





from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import DeiTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

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


if is_tf_available():
       import tensorflow as tf

       from transformers import (
           TFDeiTForImageClassification,
           TFDeiTForImageClassificationWithTeacher,
           TFDeiTForMaskedImageModeling,
           TFDeiTModel,
       )
       from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from PIL import Image

       from transformers import DeiTImageProcessor




class     SCREAMING_SNAKE_CASE__ :



 def __init__(							self,__lowerCamelCase,__lowerCamelCase=13,__lowerCamelCase=30,__lowerCamelCase=2,__lowerCamelCase=3,__lowerCamelCase=True,__lowerCamelCase=True,__lowerCamelCase=32,__lowerCamelCase=2,__lowerCamelCase=4,__lowerCamelCase=37,__lowerCamelCase="gelu",__lowerCamelCase=0.1,__lowerCamelCase=0.1,__lowerCamelCase=10,__lowerCamelCase=0.02,__lowerCamelCase=3,__lowerCamelCase=None,__lowerCamelCase=2,):
        A__											=						parent
        A__											=						batch_size
        A__											=						image_size
        A__											=						patch_size
        A__											=						num_channels
        A__											=						is_training
        A__											=						use_labels
        A__											=						hidden_size
        A__											=						num_hidden_layers
        A__											=						num_attention_heads
        A__											=						intermediate_size
        A__											=						hidden_act
        A__											=						hidden_dropout_prob
        A__											=						attention_probs_dropout_prob
        A__											=						type_sequence_label_size
        A__											=						initializer_range
        A__											=						scope
        A__											=						encoder_stride

        # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
        A__											=						(image_size // patch_size) ** 2
        A__											=						num_patches + 2



 def 				UpperCamelCase			(							self ):
        A__											=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

        A__											=						None
        if self.use_labels:
               A__											=						ids_tensor([self.batch_size],self.type_sequence_label_size )

        A__											=						self.get_config()

        return config, pixel_values, labels



 def 				UpperCamelCase			(							self ):
        return DeiTConfig(
            image_size=self.image_size,patch_size=self.patch_size,num_channels=self.num_channels,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,is_decoder=__lowerCamelCase,initializer_range=self.initializer_range,encoder_stride=self.encoder_stride,)



 def 				UpperCamelCase			(							self,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase ):
        A__											=						TFDeiTModel(config=__lowerCamelCase )
        A__											=						model(__lowerCamelCase )
        self.parent.assertEqual(result.last_hidden_state.shape,(self.batch_size, self.seq_length, self.hidden_size) )



 def 				UpperCamelCase			(							self,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase ):
        A__											=						TFDeiTForMaskedImageModeling(config=__lowerCamelCase )
        A__											=						model(__lowerCamelCase )
        self.parent.assertEqual(
            result.reconstruction.shape,(self.batch_size, self.num_channels, self.image_size, self.image_size) )

        # test greyscale images
        A__											=						1
        A__											=						TFDeiTForMaskedImageModeling(__lowerCamelCase )

        A__											=						floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
        A__											=						model(__lowerCamelCase )
        self.parent.assertEqual(result.reconstruction.shape,(self.batch_size, 1, self.image_size, self.image_size) )



 def 				UpperCamelCase			(							self,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase ):
        A__											=						self.type_sequence_label_size
        A__											=						TFDeiTForImageClassification(__lowerCamelCase )
        A__											=						model(__lowerCamelCase,labels=__lowerCamelCase )
        self.parent.assertEqual(result.logits.shape,(self.batch_size, self.type_sequence_label_size) )

        # test greyscale images
        A__											=						1
        A__											=						TFDeiTForImageClassification(__lowerCamelCase )

        A__											=						floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
        A__											=						model(__lowerCamelCase,labels=__lowerCamelCase )
        self.parent.assertEqual(result.logits.shape,(self.batch_size, self.type_sequence_label_size) )




 def 				UpperCamelCase			(							self ):
        A__											=						self.prepare_config_and_inputs()
        A__					,	A__					,	A__											=						config_and_inputs
        A__											=						{'''pixel_values''': pixel_values}
        return config, inputs_dict




@require_tf
class     SCREAMING_SNAKE_CASE__ (						UpperCamelCase__				,					UpperCamelCase__				,					unittest.TestCase      ):
 __SCREAMING_SNAKE_CASE							       =	(
     (
         TFDeiTModel,
         TFDeiTForImageClassification,
         TFDeiTForImageClassificationWithTeacher,
         TFDeiTForMaskedImageModeling,
     )
     if is_tf_available()
     else ()
 )
 __SCREAMING_SNAKE_CASE							       =	(
     {
         '''feature-extraction''': TFDeiTModel,
         '''image-classification''': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
     }
     if is_tf_available()
     else {}
 )

 __SCREAMING_SNAKE_CASE							       =	False
 __SCREAMING_SNAKE_CASE							       =	False
 __SCREAMING_SNAKE_CASE							       =	False
 __SCREAMING_SNAKE_CASE							       =	False



 def 				UpperCamelCase			(							self ):
        A__											=						TFDeiTModelTester(self )
        A__											=						ConfigTester(self,config_class=__lowerCamelCase,has_text_modality=__lowerCamelCase,hidden_size=37 )



 def 				UpperCamelCase			(							self ):
        self.config_tester.run_common_tests()



 @unittest.skip(reason='''DeiT does not use inputs_embeds''' )
 def 				UpperCamelCase			(							self ):
        pass



 def 				UpperCamelCase			(							self ):
        A__					,	A__											=						self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
               A__											=						model_class(__lowerCamelCase )
               self.assertIsInstance(model.get_input_embeddings(),(tf.keras.layers.Layer) )
               A__											=						model.get_output_embeddings()
               self.assertTrue(x is None or isinstance(__lowerCamelCase,tf.keras.layers.Dense ) )



 def 				UpperCamelCase			(							self ):
        A__					,	A__											=						self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
               A__											=						model_class(__lowerCamelCase )
               A__											=						inspect.signature(model.call )
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               A__											=						[*signature.parameters.keys()]

               A__											=						['''pixel_values''']
               self.assertListEqual(arg_names[:1],__lowerCamelCase )



 def 				UpperCamelCase			(							self ):
        A__											=						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__lowerCamelCase )



 def 				UpperCamelCase			(							self ):
        A__											=						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_image_modeling(*__lowerCamelCase )



 def 				UpperCamelCase			(							self ):
        A__											=						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase )



 def 				UpperCamelCase			(							self,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase=False ):
        A__											=						super()._prepare_for_class(__lowerCamelCase,__lowerCamelCase,return_labels=__lowerCamelCase )

        if return_labels:
               if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call ).parameters:
                      del inputs_dict["labels"]

        return inputs_dict




 @slow
 def 				UpperCamelCase			(							self ):
        for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               A__											=						TFDeiTModel.from_pretrained(__lowerCamelCase )
               self.assertIsNotNone(__lowerCamelCase )


def 	UpperCamelCase__(       )->int:
       A__											=						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''  )
       return image




@require_tf
@require_vision
class     SCREAMING_SNAKE_CASE__ (						unittest.TestCase      ):



 @cached_property
 def 				UpperCamelCase			(							self ):
        return (
            DeiTImageProcessor.from_pretrained('''facebook/deit-base-distilled-patch16-224''' )
            if is_vision_available()
            else None
        )




 @slow
 def 				UpperCamelCase			(							self ):
        A__											=						TFDeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224''' )

        A__											=						self.default_image_processor
        A__											=						prepare_img()
        A__											=						image_processor(images=__lowerCamelCase,return_tensors='''tf''' )

        # forward pass
        A__											=						model(**__lowerCamelCase )

        # verify the logits
        A__											=						tf.TensorShape((1, 1000) )
        self.assertEqual(outputs.logits.shape,__lowerCamelCase )

        A__											=						tf.constant([-1.0266, 0.1912, -1.2861] )

        self.assertTrue(np.allclose(outputs.logits[0, :3],__lowerCamelCase,atol=1E-4 ) )












import tempfile

import torch

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

from .test_schedulers import SchedulerCommonTest




class     SCREAMING_SNAKE_CASE__ (						UpperCamelCase__      ):
 __SCREAMING_SNAKE_CASE							       =	(DPMSolverSinglestepScheduler,)
 __SCREAMING_SNAKE_CASE							       =	(('''num_inference_steps''', 25),)



 def 				UpperCamelCase			(							self,**__lowerCamelCase ):
        A__											=						{
            '''num_train_timesteps''': 1000,
            '''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''',
            '''lambda_min_clipped''': -float('''inf''' ),
            '''variance_type''': None,
        }

        config.update(**__lowerCamelCase )
        return config



 def 				UpperCamelCase			(							self,__lowerCamelCase=0,**__lowerCamelCase ):
        A__											=						dict(self.forward_default_kwargs )
        A__											=						kwargs.pop('''num_inference_steps''',__lowerCamelCase )
        A__											=						self.dummy_sample
        A__											=						0.1 * sample
        A__											=						[residual + 0.2, residual + 0.15, residual + 0.10]

        for scheduler_class in self.scheduler_classes:
               A__											=						self.get_scheduler_config(**__lowerCamelCase )
               A__											=						scheduler_class(**__lowerCamelCase )
               scheduler.set_timesteps(__lowerCamelCase )
               # copy over dummy past residuals
               A__											=						dummy_past_residuals[: scheduler.config.solver_order]

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

               A__					,	A__											=						sample, sample
               for t in range(__lowerCamelCase,time_step + scheduler.config.solver_order + 1 ):
                      A__											=						scheduler.step(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,**__lowerCamelCase ).prev_sample
                      A__											=						new_scheduler.step(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,**__lowerCamelCase ).prev_sample

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



 def 				UpperCamelCase			(							self ):
        pass



 def 				UpperCamelCase			(							self,__lowerCamelCase=0,**__lowerCamelCase ):
        A__											=						dict(self.forward_default_kwargs )
        A__											=						kwargs.pop('''num_inference_steps''',__lowerCamelCase )
        A__											=						self.dummy_sample
        A__											=						0.1 * sample
        A__											=						[residual + 0.2, residual + 0.15, residual + 0.10]

        for scheduler_class in self.scheduler_classes:
               A__											=						self.get_scheduler_config()
               A__											=						scheduler_class(**__lowerCamelCase )
               scheduler.set_timesteps(__lowerCamelCase )

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

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

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

               A__											=						scheduler.step(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,**__lowerCamelCase ).prev_sample
               A__											=						new_scheduler.step(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,**__lowerCamelCase ).prev_sample

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



 def 				UpperCamelCase			(							self,__lowerCamelCase=None,**__lowerCamelCase ):
        if scheduler is None:
               A__											=						self.scheduler_classes[0]
               A__											=						self.get_scheduler_config(**__lowerCamelCase )
               A__											=						scheduler_class(**__lowerCamelCase )

        A__											=						self.scheduler_classes[0]
        A__											=						self.get_scheduler_config(**__lowerCamelCase )
        A__											=						scheduler_class(**__lowerCamelCase )

        A__											=						10
        A__											=						self.dummy_model()
        A__											=						self.dummy_sample_deter
        scheduler.set_timesteps(__lowerCamelCase )

        for i, t in enumerate(scheduler.timesteps ):
               A__											=						model(__lowerCamelCase,__lowerCamelCase )
               A__											=						scheduler.step(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase ).prev_sample

        return sample



 def 				UpperCamelCase			(							self ):
        A__											=						DPMSolverSinglestepScheduler(**self.get_scheduler_config() )
        A__											=						50
        A__											=						self.dummy_model()
        A__											=						self.dummy_sample_deter
        scheduler.set_timesteps(__lowerCamelCase )

        # make sure that the first t is uneven
        for i, t in enumerate(scheduler.timesteps[3:] ):
               A__											=						model(__lowerCamelCase,__lowerCamelCase )
               A__											=						scheduler.step(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase ).prev_sample

        A__											=						torch.mean(torch.abs(__lowerCamelCase ) )

        assert abs(result_mean.item() - 0.2574 ) < 1E-3



 def 				UpperCamelCase			(							self ):
        for timesteps in [25, 50, 100, 999, 1000]:
               self.check_over_configs(num_train_timesteps=__lowerCamelCase )



 def 				UpperCamelCase			(							self ):
        # make sure that iterating over schedulers with same config names gives same results
        # for defaults
        A__											=						DPMSolverSinglestepScheduler(**self.get_scheduler_config() )
        A__											=						self.full_loop(scheduler=__lowerCamelCase )
        A__											=						torch.mean(torch.abs(__lowerCamelCase ) )

        assert abs(result_mean.item() - 0.2791 ) < 1E-3

        A__											=						DEISMultistepScheduler.from_config(scheduler.config )
        A__											=						DPMSolverMultistepScheduler.from_config(scheduler.config )
        A__											=						UniPCMultistepScheduler.from_config(scheduler.config )
        A__											=						DPMSolverSinglestepScheduler.from_config(scheduler.config )

        A__											=						self.full_loop(scheduler=__lowerCamelCase )
        A__											=						torch.mean(torch.abs(__lowerCamelCase ) )

        assert abs(result_mean.item() - 0.2791 ) < 1E-3



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



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



 def 				UpperCamelCase			(							self ):
        for algorithm_type in ["dpmsolver", "dpmsolver++"]:
               for solver_type in ["midpoint", "heun"]:
                      for order in [1, 2, 3]:
                             for prediction_type in ["epsilon", "sample"]:
                                    self.check_over_configs(
                                        solver_order=__lowerCamelCase,solver_type=__lowerCamelCase,prediction_type=__lowerCamelCase,algorithm_type=__lowerCamelCase,)
                                    A__											=						self.full_loop(
                                        solver_order=__lowerCamelCase,solver_type=__lowerCamelCase,prediction_type=__lowerCamelCase,algorithm_type=__lowerCamelCase,)
                                    assert not torch.isnan(__lowerCamelCase ).any(), "Samples have nan numbers"



 def 				UpperCamelCase			(							self ):
        self.check_over_configs(lower_order_final=__lowerCamelCase )
        self.check_over_configs(lower_order_final=__lowerCamelCase )



 def 				UpperCamelCase			(							self ):
        self.check_over_configs(lambda_min_clipped=-float('''inf''' ) )
        self.check_over_configs(lambda_min_clipped=-5.1 )



 def 				UpperCamelCase			(							self ):
        self.check_over_configs(variance_type=__lowerCamelCase )
        self.check_over_configs(variance_type='''learned_range''' )



 def 				UpperCamelCase			(							self ):
        for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]:
               self.check_over_forward(num_inference_steps=__lowerCamelCase,time_step=0 )



 def 				UpperCamelCase			(							self ):
        A__											=						self.full_loop()
        A__											=						torch.mean(torch.abs(__lowerCamelCase ) )

        assert abs(result_mean.item() - 0.2791 ) < 1E-3



 def 				UpperCamelCase			(							self ):
        A__											=						self.full_loop(use_karras_sigmas=__lowerCamelCase )
        A__											=						torch.mean(torch.abs(__lowerCamelCase ) )

        assert abs(result_mean.item() - 0.2248 ) < 1E-3



 def 				UpperCamelCase			(							self ):
        A__											=						self.full_loop(prediction_type='''v_prediction''' )
        A__											=						torch.mean(torch.abs(__lowerCamelCase ) )

        assert abs(result_mean.item() - 0.1453 ) < 1E-3



 def 				UpperCamelCase			(							self ):
        A__											=						self.full_loop(prediction_type='''v_prediction''',use_karras_sigmas=__lowerCamelCase )
        A__											=						torch.mean(torch.abs(__lowerCamelCase ) )

        assert abs(result_mean.item() - 0.0649 ) < 1E-3




 def 				UpperCamelCase			(							self ):
        A__											=						self.scheduler_classes[0]
        A__											=						self.get_scheduler_config(thresholding=__lowerCamelCase,dynamic_thresholding_ratio=0 )
        A__											=						scheduler_class(**__lowerCamelCase )

        A__											=						10
        A__											=						self.dummy_model()
        A__											=						self.dummy_sample_deter.half()
        scheduler.set_timesteps(__lowerCamelCase )

        for i, t in enumerate(scheduler.timesteps ):
               A__											=						model(__lowerCamelCase,__lowerCamelCase )
               A__											=						scheduler.step(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase ).prev_sample

        assert sample.dtype == torch.floataa





