

'''simple docstring'''

import tempfile

import torch

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

from .test_schedulers import SchedulerCommonTest


class 	lowerCAmelCase  (   A ):
       lowerCAmelCase_												=			(DPMSolverSinglestepScheduler,)
       lowerCAmelCase_												=			(("num_inference_steps", 2_5),)







       def       snake_case (				self      :			Dict    ,   **__lowercase      :			Optional[Any]   ):







            """simple docstring"""







            __lowercase      		={
                'num_train_timesteps': 1000,
                'beta_start': 0.0_0_0_1,
                'beta_end': 0.0_2,
                '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(**__lowercase   )
            return config







       def       snake_case (				self      :			Dict    ,   __lowercase      :			Union[str, Any]=0    ,   **__lowercase      :			str   ):







            """simple docstring"""







            __lowercase      		=dict(self.forward_default_kwargs   )
            __lowercase      		=kwargs.pop('num_inference_steps'    ,   __lowercase   )
            __lowercase      		=self.dummy_sample
            __lowercase      		=0.1 * sample
            __lowercase      		=[residual + 0.2, residual + 0.1_5, residual + 0.1_0]

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

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

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

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







       def       snake_case (				self      :			Optional[Any]   ):







            """simple docstring"""







            pass







       def       snake_case (				self      :			Tuple    ,   __lowercase      :			int=0    ,   **__lowercase      :			str   ):







            """simple docstring"""







            __lowercase      		=dict(self.forward_default_kwargs   )
            __lowercase      		=kwargs.pop('num_inference_steps'    ,   __lowercase   )
            __lowercase      		=self.dummy_sample
            __lowercase      		=0.1 * sample
            __lowercase      		=[residual + 0.2, residual + 0.1_5, residual + 0.1_0]

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

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

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

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

                 __lowercase      		=scheduler.step(__lowercase    ,   __lowercase    ,   __lowercase    ,   **__lowercase   ).prev_sample
                 __lowercase      		=new_scheduler.step(__lowercase    ,   __lowercase    ,   __lowercase    ,   **__lowercase   ).prev_sample

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







       def       snake_case (				self      :			Tuple    ,   __lowercase      :			str=None    ,   **__lowercase      :			List[Any]   ):







            """simple docstring"""







            if scheduler is None:
                 __lowercase      		=self.scheduler_classes[0]
                 __lowercase      		=self.get_scheduler_config(**__lowercase   )
                 __lowercase      		=scheduler_class(**__lowercase   )

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

            __lowercase      		=10
            __lowercase      		=self.dummy_model()
            __lowercase      		=self.dummy_sample_deter
            scheduler.set_timesteps(__lowercase   )

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

            return sample







       def       snake_case (				self      :			Optional[Any]   ):







            """simple docstring"""







            __lowercase      		=DPMSolverSinglestepScheduler(**self.get_scheduler_config()   )
            __lowercase      		=50
            __lowercase      		=self.dummy_model()
            __lowercase      		=self.dummy_sample_deter
            scheduler.set_timesteps(__lowercase   )

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

            __lowercase      		=torch.mean(torch.abs(__lowercase   )   )

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







       def       snake_case (				self      :			Optional[Any]   ):







            """simple docstring"""







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







       def       snake_case (				self      :			List[Any]   ):







            """simple docstring"""







            __lowercase      		=DPMSolverSinglestepScheduler(**self.get_scheduler_config()   )
            __lowercase      		=self.full_loop(scheduler=__lowercase   )
            __lowercase      		=torch.mean(torch.abs(__lowercase   )   )

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

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

            __lowercase      		=self.full_loop(scheduler=__lowercase   )
            __lowercase      		=torch.mean(torch.abs(__lowercase   )   )

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







       def       snake_case (				self      :			List[str]   ):







            """simple docstring"""







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







       def       snake_case (				self      :			Optional[int]   ):







            """simple docstring"""







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







       def       snake_case (				self      :			Optional[int]   ):







            """simple docstring"""







            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=__lowercase    ,   solver_type=__lowercase    ,   prediction_type=__lowercase    ,   algorithm_type=__lowercase    ,   )
                                __lowercase      		=self.full_loop(
                                    solver_order=__lowercase    ,   solver_type=__lowercase    ,   prediction_type=__lowercase    ,   algorithm_type=__lowercase    ,   )
                                assert not torch.isnan(__lowercase   ).any(), "Samples have nan numbers"







       def       snake_case (				self      :			Union[str, Any]   ):







            """simple docstring"""







            self.check_over_configs(lower_order_final=__lowercase   )
            self.check_over_configs(lower_order_final=__lowercase   )







       def       snake_case (				self      :			Union[str, Any]   ):







            """simple docstring"""







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







       def       snake_case (				self      :			int   ):







            """simple docstring"""







            self.check_over_configs(variance_type=__lowercase   )
            self.check_over_configs(variance_type='learned_range'   )







       def       snake_case (				self      :			Optional[Any]   ):







            """simple docstring"""







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







       def       snake_case (				self      :			str   ):







            """simple docstring"""







            __lowercase      		=self.full_loop()
            __lowercase      		=torch.mean(torch.abs(__lowercase   )   )

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







       def       snake_case (				self      :			Dict   ):







            """simple docstring"""







            __lowercase      		=self.full_loop(use_karras_sigmas=__lowercase   )
            __lowercase      		=torch.mean(torch.abs(__lowercase   )   )

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







       def       snake_case (				self      :			Any   ):







            """simple docstring"""







            __lowercase      		=self.full_loop(prediction_type='v_prediction'   )
            __lowercase      		=torch.mean(torch.abs(__lowercase   )   )

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







       def       snake_case (				self      :			List[str]   ):







            """simple docstring"""







            __lowercase      		=self.full_loop(prediction_type='v_prediction'    ,   use_karras_sigmas=__lowercase   )
            __lowercase      		=torch.mean(torch.abs(__lowercase   )   )

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







       def       snake_case (				self      :			Any   ):







            """simple docstring"""







            __lowercase      		=self.scheduler_classes[0]
            __lowercase      		=self.get_scheduler_config(thresholding=__lowercase    ,   dynamic_thresholding_ratio=0   )
            __lowercase      		=scheduler_class(**__lowercase   )

            __lowercase      		=10
            __lowercase      		=self.dummy_model()
            __lowercase      		=self.dummy_sample_deter.half()
            scheduler.set_timesteps(__lowercase   )

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

            assert sample.dtype == torch.floataa





'''simple docstring'''

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


UpperCAmelCase					 =	logging.get_logger(__name__)

UpperCAmelCase					 =	{
    '''shi-labs/dinat-mini-in1k-224''': '''https://huggingface.co/shi-labs/dinat-mini-in1k-224/resolve/main/config.json''',
    # See all Dinat models at https://huggingface.co/models?filter=dinat
}


class 	lowerCAmelCase  (   A ,      A ):
       lowerCAmelCase_												=			"dinat"

       lowerCAmelCase_												=			{
           "num_attention_heads": "num_heads",
           "num_hidden_layers": "num_layers",
       }







       def __init__(				self      :			int    ,   __lowercase      :			str=4    ,   __lowercase      :			str=3    ,   __lowercase      :			int=64    ,   __lowercase      :			int=[3, 4, 6, 5]    ,   __lowercase      :			Union[str, Any]=[2, 4, 8, 16]    ,   __lowercase      :			Optional[int]=7    ,   __lowercase      :			str=[[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]]    ,   __lowercase      :			Any=3.0    ,   __lowercase      :			Optional[Any]=True    ,   __lowercase      :			List[Any]=0.0    ,   __lowercase      :			Optional[Any]=0.0    ,   __lowercase      :			int=0.1    ,   __lowercase      :			Union[str, Any]="gelu"    ,   __lowercase      :			Any=0.0_2    ,   __lowercase      :			int=1E-5    ,   __lowercase      :			Optional[int]=0.0    ,   __lowercase      :			Optional[int]=None    ,   __lowercase      :			str=None    ,   **__lowercase      :			Any    ,   ):







            """simple docstring"""







            super().__init__(**__lowercase   )

            __lowercase      		=patch_size
            __lowercase      		=num_channels
            __lowercase      		=embed_dim
            __lowercase      		=depths
            __lowercase      		=len(__lowercase   )
            __lowercase      		=num_heads
            __lowercase      		=kernel_size
            __lowercase      		=dilations
            __lowercase      		=mlp_ratio
            __lowercase      		=qkv_bias
            __lowercase      		=hidden_dropout_prob
            __lowercase      		=attention_probs_dropout_prob
            __lowercase      		=drop_path_rate
            __lowercase      		=hidden_act
            __lowercase      		=layer_norm_eps
            __lowercase      		=initializer_range
            # we set the hidden_size attribute in order to make Dinat work with VisionEncoderDecoderModel
            # this indicates the channel dimension after the last stage of the model
            __lowercase      		=int(embed_dim * 2 ** (len(__lowercase   ) - 1)   )
            __lowercase      		=layer_scale_init_value
            __lowercase      		=['stem'] + [f'''stage{idx}''' for idx in range(1    ,   len(__lowercase   ) + 1   )]
            __lowercase		,  __lowercase      		=get_aligned_output_features_output_indices(
                out_features=__lowercase    ,   out_indices=__lowercase    ,   stage_names=self.stage_names   )


