'''simple docstring'''





import tempfile

import torch

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

from .test_schedulers import SchedulerCommonTest




class        __A      ( UpperCamelCase__	):
    a__       :						Any          =  (UniPCMultistepScheduler,)
    a__       :						Tuple          =  (("""num_inference_steps""", 25),)



    def 					_lowercase						(self    :    str   ,			**__a    :    Tuple ):
         UpperCAmelCase_           =   {
             "num_train_timesteps": 1000,
             "beta_start": 0.00_01,
             "beta_end": 0.02,
             "beta_schedule": "linear",
             "solver_order": 2,
             "solver_type": "bh2",
         }

         config.update(**__a )
         return config



    def 					_lowercase						(self    :    List[str]   ,			__a    :    Dict=0   ,			**__a    :    Dict ):
         UpperCAmelCase_           =   dict(self.forward_default_kwargs )
         UpperCAmelCase_           =   kwargs.pop("num_inference_steps"   ,			__a )
         UpperCAmelCase_           =   self.dummy_sample
         UpperCAmelCase_           =   0.1 * sample
         UpperCAmelCase_           =   [residual + 0.2, residual + 0.15, residual + 0.10]

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

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

              UpperCAmelCase_    , UpperCAmelCase_           =   sample, sample
              for t in range(__a   ,			time_step + scheduler.config.solver_order + 1 ):
                   UpperCAmelCase_           =   scheduler.step(__a   ,			__a   ,			__a   ,			**__a ).prev_sample
                   UpperCAmelCase_           =   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 					_lowercase						(self    :    Any   ,			__a    :    Optional[int]=0   ,			**__a    :    Dict ):
         UpperCAmelCase_           =   dict(self.forward_default_kwargs )
         UpperCAmelCase_           =   kwargs.pop("num_inference_steps"   ,			__a )
         UpperCAmelCase_           =   self.dummy_sample
         UpperCAmelCase_           =   0.1 * sample
         UpperCAmelCase_           =   [residual + 0.2, residual + 0.15, residual + 0.10]

         for scheduler_class in self.scheduler_classes:
              UpperCAmelCase_           =   self.get_scheduler_config()
              UpperCAmelCase_           =   scheduler_class(**__a )
              scheduler.set_timesteps(__a )

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

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

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

         UpperCAmelCase_           =   self.scheduler_classes[0]
         UpperCAmelCase_           =   self.get_scheduler_config(**__a )
         UpperCAmelCase_           =   scheduler_class(**__a )

         UpperCAmelCase_           =   10
         UpperCAmelCase_           =   self.dummy_model()
         UpperCAmelCase_           =   self.dummy_sample_deter
         scheduler.set_timesteps(__a )

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

         return sample



    def 					_lowercase						(self    :    List[Any] ):
         UpperCAmelCase_           =   dict(self.forward_default_kwargs )

         UpperCAmelCase_           =   kwargs.pop("num_inference_steps"   ,			__a )

         for scheduler_class in self.scheduler_classes:
              UpperCAmelCase_           =   self.get_scheduler_config()
              UpperCAmelCase_           =   scheduler_class(**__a )

              UpperCAmelCase_           =   self.dummy_sample
              UpperCAmelCase_           =   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" ):
                   UpperCAmelCase_           =   num_inference_steps

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

              UpperCAmelCase_           =   scheduler.timesteps[5]
              UpperCAmelCase_           =   scheduler.timesteps[6]

              UpperCAmelCase_           =   scheduler.step(__a   ,			__a   ,			__a   ,			**__a ).prev_sample
              UpperCAmelCase_           =   scheduler.step(__a   ,			__a   ,			__a   ,			**__a ).prev_sample

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



    def 					_lowercase						(self    :    List[Any] ):
         # make sure that iterating over schedulers with same config names gives same results
         # for defaults
         UpperCAmelCase_           =   UniPCMultistepScheduler(**self.get_scheduler_config() )
         UpperCAmelCase_           =   self.full_loop(scheduler=__a )
         UpperCAmelCase_           =   torch.mean(torch.abs(__a ) )

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

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

         UpperCAmelCase_           =   self.full_loop(scheduler=__a )
         UpperCAmelCase_           =   torch.mean(torch.abs(__a ) )

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



    def 					_lowercase						(self    :    Union[str, Any] ):
         for timesteps in [25, 50, 100, 999, 1000]:
              self.check_over_configs(num_train_timesteps=__a )



    def 					_lowercase						(self    :    Optional[int] ):
         self.check_over_configs(thresholding=__a )
         for order in [1, 2, 3]:
              for solver_type in ["bh1", "bh2"]:
                   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   ,			solver_order=__a   ,			solver_type=__a   ,			)



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



    def 					_lowercase						(self    :    List[Any] ):
         for solver_type in ["bh1", "bh2"]:
              for order in [1, 2, 3]:
                   for prediction_type in ["epsilon", "sample"]:
                        self.check_over_configs(
                            solver_order=__a   ,			solver_type=__a   ,			prediction_type=__a   ,			)
                        UpperCAmelCase_           =   self.full_loop(
                            solver_order=__a   ,			solver_type=__a   ,			prediction_type=__a   ,			)
                        assert not torch.isnan(__a ).any(), "Samples have nan numbers"



    def 					_lowercase						(self    :    int ):
         self.check_over_configs(lower_order_final=__a )
         self.check_over_configs(lower_order_final=__a )



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



    def 					_lowercase						(self    :    Union[str, Any] ):
         UpperCAmelCase_           =   self.full_loop()
         UpperCAmelCase_           =   torch.mean(torch.abs(__a ) )

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



    def 					_lowercase						(self    :    Tuple ):
         UpperCAmelCase_           =   self.full_loop(prediction_type="v_prediction" )
         UpperCAmelCase_           =   torch.mean(torch.abs(__a ) )

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



    def 					_lowercase						(self    :    Optional[Any] ):
         UpperCAmelCase_           =   self.scheduler_classes[0]
         UpperCAmelCase_           =   self.get_scheduler_config(thresholding=__a   ,			dynamic_thresholding_ratio=0 )
         UpperCAmelCase_           =   scheduler_class(**__a )

         UpperCAmelCase_           =   10
         UpperCAmelCase_           =   self.dummy_model()
         UpperCAmelCase_           =   self.dummy_sample_deter.half()
         scheduler.set_timesteps(__a )

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

         assert sample.dtype == torch.floataa






    def 					_lowercase						(self    :    Dict   ,			**__a    :    Optional[int] ):
         for scheduler_class in self.scheduler_classes:
              UpperCAmelCase_           =   self.get_scheduler_config(**__a )
              UpperCAmelCase_           =   scheduler_class(**__a )

              scheduler.set_timesteps(scheduler.config.num_train_timesteps )
              assert len(scheduler.timesteps.unique() ) == scheduler.num_inference_steps





'''simple docstring'''





import inspect
import unittest

import numpy as np

from transformers import ViTConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
    import jax

    from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel




class        __A      ( unittest.TestCase	):



    def __init__(self    :    str   ,			__a    :    Optional[Any]   ,			__a    :    Optional[Any]=13   ,			__a    :    int=30   ,			__a    :    Union[str, Any]=2   ,			__a    :    Dict=3   ,			__a    :    List[Any]=True   ,			__a    :    Optional[Any]=True   ,			__a    :    List[Any]=32   ,			__a    :    Any=5   ,			__a    :    str=4   ,			__a    :    Optional[int]=37   ,			__a    :    Optional[int]="gelu"   ,			__a    :    List[str]=0.1   ,			__a    :    Tuple=0.1   ,			__a    :    List[str]=10   ,			__a    :    Optional[int]=0.02   ,			):
         UpperCAmelCase_           =   parent
         UpperCAmelCase_           =   batch_size
         UpperCAmelCase_           =   image_size
         UpperCAmelCase_           =   patch_size
         UpperCAmelCase_           =   num_channels
         UpperCAmelCase_           =   is_training
         UpperCAmelCase_           =   use_labels
         UpperCAmelCase_           =   hidden_size
         UpperCAmelCase_           =   num_hidden_layers
         UpperCAmelCase_           =   num_attention_heads
         UpperCAmelCase_           =   intermediate_size
         UpperCAmelCase_           =   hidden_act
         UpperCAmelCase_           =   hidden_dropout_prob
         UpperCAmelCase_           =   attention_probs_dropout_prob
         UpperCAmelCase_           =   type_sequence_label_size
         UpperCAmelCase_           =   initializer_range

         # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
         UpperCAmelCase_           =   (image_size // patch_size) ** 2
         UpperCAmelCase_           =   num_patches + 1



    def 					_lowercase						(self    :    Any ):
         UpperCAmelCase_           =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

         UpperCAmelCase_           =   ViTConfig(
             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=__a   ,			initializer_range=self.initializer_range   ,			)

         return config, pixel_values



    def 					_lowercase						(self    :    Dict   ,			__a    :    Any   ,			__a    :    List[Any] ):
         UpperCAmelCase_           =   FlaxViTModel(config=__a )
         UpperCAmelCase_           =   model(__a )
         # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
         UpperCAmelCase_           =   (self.image_size, self.image_size)
         UpperCAmelCase_           =   (self.patch_size, self.patch_size)
         UpperCAmelCase_           =   (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
         self.parent.assertEqual(result.last_hidden_state.shape   ,			(self.batch_size, num_patches + 1, self.hidden_size) )



    def 					_lowercase						(self    :    Tuple   ,			__a    :    str   ,			__a    :    Any ):
         UpperCAmelCase_           =   self.type_sequence_label_size
         UpperCAmelCase_           =   FlaxViTForImageClassification(config=__a )
         UpperCAmelCase_           =   model(__a )
         self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.type_sequence_label_size) )

         # test greyscale images
         UpperCAmelCase_           =   1
         UpperCAmelCase_           =   FlaxViTForImageClassification(__a )

         UpperCAmelCase_           =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
         UpperCAmelCase_           =   model(__a )






    def 					_lowercase						(self    :    Optional[Any] ):
         UpperCAmelCase_           =   self.prepare_config_and_inputs()
         (
             (
             UpperCAmelCase_
         )    , (
             UpperCAmelCase_
         )    , 
         )           =   config_and_inputs
         UpperCAmelCase_           =   {"pixel_values": pixel_values}
         return config, inputs_dict




@require_flax
class        __A      ( UpperCamelCase__		,	unittest.TestCase	):
    a__       :						Tuple          =  (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()



    def 					_lowercase						(self    :    Any ):
         UpperCAmelCase_           =   FlaxViTModelTester(self )
         UpperCAmelCase_           =   ConfigTester(self   ,			config_class=__a   ,			has_text_modality=__a   ,			hidden_size=37 )



    def 					_lowercase						(self    :    Tuple ):
         self.config_tester.run_common_tests()



    def 					_lowercase						(self    :    str ):
         UpperCAmelCase_           =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__a )



    def 					_lowercase						(self    :    str ):
         UpperCAmelCase_           =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*__a )



    def 					_lowercase						(self    :    Tuple ):
         UpperCAmelCase_    , UpperCAmelCase_           =   self.model_tester.prepare_config_and_inputs_for_common()

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

              UpperCAmelCase_           =   ["pixel_values"]
              self.assertListEqual(arg_names[:1]   ,			__a )



    def 					_lowercase						(self    :    Optional[Any] ):
         UpperCAmelCase_    , UpperCAmelCase_           =   self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
              with self.subTest(model_class.__name__ ):
                   UpperCAmelCase_           =   self._prepare_for_class(__a   ,			__a )
                   UpperCAmelCase_           =   model_class(__a )

                   @jax.jit
                   def model_jitted(__a    :    Tuple   ,			**__a    :    List[Any] ):
                        return model(pixel_values=__a   ,			**__a )

                   with self.subTest("JIT Enabled" ):
                        UpperCAmelCase_           =   model_jitted(**__a ).to_tuple()

                   with self.subTest("JIT Disabled" ):
                        with jax.disable_jit():
                             UpperCAmelCase_           =   model_jitted(**__a ).to_tuple()

                   self.assertEqual(len(__a )   ,			len(__a ) )
                   for jitted_output, output in zip(__a   ,			__a ):
                        self.assertEqual(jitted_output.shape   ,			output.shape )






    @slow
    def 					_lowercase						(self    :    Tuple ):
         for model_class_name in self.all_model_classes:
              UpperCAmelCase_           =   model_class_name.from_pretrained("google/vit-base-patch16-224" )
              UpperCAmelCase_           =   model(np.ones((1, 3, 224, 224) ) )
              self.assertIsNotNone(__a )




