



"""simple docstring"""
import collections
import inspect
import unittest

from transformers import SwinvaConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch
      from torch import nn

      from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
      from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
      from PIL import Image

      from transformers import AutoImageProcessor



class       SCREAMING_SNAKE_CASE__  :

   def __init__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE=13      ,     _SCREAMING_SNAKE_CASE=32      ,     _SCREAMING_SNAKE_CASE=2      ,     _SCREAMING_SNAKE_CASE=3      ,     _SCREAMING_SNAKE_CASE=16      ,     _SCREAMING_SNAKE_CASE=[1, 2, 1]      ,     _SCREAMING_SNAKE_CASE=[2, 2, 4]      ,     _SCREAMING_SNAKE_CASE=2      ,     _SCREAMING_SNAKE_CASE=2.0      ,     _SCREAMING_SNAKE_CASE=True      ,     _SCREAMING_SNAKE_CASE=0.0      ,     _SCREAMING_SNAKE_CASE=0.0      ,     _SCREAMING_SNAKE_CASE=0.1      ,     _SCREAMING_SNAKE_CASE="gelu"      ,     _SCREAMING_SNAKE_CASE=False      ,     _SCREAMING_SNAKE_CASE=True      ,     _SCREAMING_SNAKE_CASE=0.02      ,     _SCREAMING_SNAKE_CASE=1E-5      ,     _SCREAMING_SNAKE_CASE=True      ,     _SCREAMING_SNAKE_CASE=None      ,     _SCREAMING_SNAKE_CASE=True      ,     _SCREAMING_SNAKE_CASE=10      ,     _SCREAMING_SNAKE_CASE=8      ,     )  ->      List[str]:





    '''simple docstring'''




    UpperCAmelCase	:					str	=	parent
    UpperCAmelCase	:					str	=	batch_size
    UpperCAmelCase	:					int	=	image_size
    UpperCAmelCase	:					str	=	patch_size
    UpperCAmelCase	:					Any	=	num_channels
    UpperCAmelCase	:					Dict	=	embed_dim
    UpperCAmelCase	:					Any	=	depths
    UpperCAmelCase	:					str	=	num_heads
    UpperCAmelCase	:					Optional[Any]	=	window_size
    UpperCAmelCase	:					int	=	mlp_ratio
    UpperCAmelCase	:					Any	=	qkv_bias
    UpperCAmelCase	:					Union[str, Any]	=	hidden_dropout_prob
    UpperCAmelCase	:					List[Any]	=	attention_probs_dropout_prob
    UpperCAmelCase	:					Any	=	drop_path_rate
    UpperCAmelCase	:					List[Any]	=	hidden_act
    UpperCAmelCase	:					Tuple	=	use_absolute_embeddings
    UpperCAmelCase	:					Optional[Any]	=	patch_norm
    UpperCAmelCase	:					Tuple	=	layer_norm_eps
    UpperCAmelCase	:					List[Any]	=	initializer_range
    UpperCAmelCase	:					Optional[int]	=	is_training
    UpperCAmelCase	:					Any	=	scope
    UpperCAmelCase	:					List[str]	=	use_labels
    UpperCAmelCase	:					Union[str, Any]	=	type_sequence_label_size
    UpperCAmelCase	:					Any	=	encoder_stride

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Any:





    '''simple docstring'''




    UpperCAmelCase	:					str	=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

    UpperCAmelCase	:					Optional[Any]	=	None
    if self.use_labels:
     UpperCAmelCase	:					Tuple	=	ids_tensor([self.batch_size]      ,     self.type_sequence_label_size						)

    UpperCAmelCase	:					Any	=	self.get_config()

    return config, pixel_values, labels

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





    '''simple docstring'''




    return SwinvaConfig(
        image_size=self.image_size      ,     patch_size=self.patch_size      ,     num_channels=self.num_channels      ,     embed_dim=self.embed_dim      ,     depths=self.depths      ,     num_heads=self.num_heads      ,     window_size=self.window_size      ,     mlp_ratio=self.mlp_ratio      ,     qkv_bias=self.qkv_bias      ,     hidden_dropout_prob=self.hidden_dropout_prob      ,     attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,     drop_path_rate=self.drop_path_rate      ,     hidden_act=self.hidden_act      ,     use_absolute_embeddings=self.use_absolute_embeddings      ,     path_norm=self.patch_norm      ,     layer_norm_eps=self.layer_norm_eps      ,     initializer_range=self.initializer_range      ,     encoder_stride=self.encoder_stride      ,     )

   def 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      List[str]:





    '''simple docstring'''




    UpperCAmelCase	:					Tuple	=	SwinvaModel(config=_SCREAMING_SNAKE_CASE						)
    model.to(_SCREAMING_SNAKE_CASE						)
    model.eval()
    UpperCAmelCase	:					str	=	model(_SCREAMING_SNAKE_CASE						)

    UpperCAmelCase	:					List[Any]	=	((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths						) - 1))
    UpperCAmelCase	:					str	=	int(config.embed_dim * 2 ** (len(config.depths						) - 1)						)

    self.parent.assertEqual(result.last_hidden_state.shape      ,     (self.batch_size, expected_seq_len, expected_dim)						)

   def 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      str:





    '''simple docstring'''




    UpperCAmelCase	:					int	=	SwinvaForMaskedImageModeling(config=_SCREAMING_SNAKE_CASE						)
    model.to(_SCREAMING_SNAKE_CASE						)
    model.eval()
    UpperCAmelCase	:					List[Any]	=	model(_SCREAMING_SNAKE_CASE						)
    self.parent.assertEqual(
        result.logits.shape      ,     (self.batch_size, self.num_channels, self.image_size, self.image_size)						)

    # test greyscale images
    UpperCAmelCase	:					List[Any]	=	1
    UpperCAmelCase	:					Tuple	=	SwinvaForMaskedImageModeling(_SCREAMING_SNAKE_CASE						)
    model.to(_SCREAMING_SNAKE_CASE						)
    model.eval()

    UpperCAmelCase	:					Optional[Any]	=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
    UpperCAmelCase	:					Union[str, Any]	=	model(_SCREAMING_SNAKE_CASE						)
    self.parent.assertEqual(result.logits.shape      ,     (self.batch_size, 1, self.image_size, self.image_size)						)

   def 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      int:





    '''simple docstring'''




    UpperCAmelCase	:					Union[str, Any]	=	self.type_sequence_label_size
    UpperCAmelCase	:					str	=	SwinvaForImageClassification(_SCREAMING_SNAKE_CASE						)
    model.to(_SCREAMING_SNAKE_CASE						)
    model.eval()
    UpperCAmelCase	:					Optional[Any]	=	model(_SCREAMING_SNAKE_CASE      ,     labels=_SCREAMING_SNAKE_CASE						)
    self.parent.assertEqual(result.logits.shape      ,     (self.batch_size, self.type_sequence_label_size)						)




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





    '''simple docstring'''




    UpperCAmelCase	:					Tuple	=	self.prepare_config_and_inputs()
    UpperCAmelCase	,		UpperCAmelCase	,		UpperCAmelCase	:					Optional[int]	=	config_and_inputs
    UpperCAmelCase	:					Dict	=	{"""pixel_values""": pixel_values}
    return config, inputs_dict



@require_torch
class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__      ,			UpperCAmelCase__      ,			unittest.TestCase ):
   __lowerCAmelCase  :   Tuple						=						(
       (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
   )
   __lowerCAmelCase  :   Union[str, Any]						=						(
       {'feature-extraction': SwinvaModel, 'image-classification': SwinvaForImageClassification}
       if is_torch_available()
       else {}
   )

   __lowerCAmelCase  :   Dict						=						False
   __lowerCAmelCase  :   Optional[Any]						=						False
   __lowerCAmelCase  :   Optional[int]						=						False
   __lowerCAmelCase  :   str						=						False

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Optional[Any]:





    '''simple docstring'''




    UpperCAmelCase	:					str	=	SwinvaModelTester(self						)
    UpperCAmelCase	:					Tuple	=	ConfigTester(self      ,     config_class=_SCREAMING_SNAKE_CASE      ,     embed_dim=37						)

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      int:





    '''simple docstring'''




    self.config_tester.create_and_test_config_to_json_string()
    self.config_tester.create_and_test_config_to_json_file()
    self.config_tester.create_and_test_config_from_and_save_pretrained()
    self.config_tester.create_and_test_config_with_num_labels()
    self.config_tester.check_config_can_be_init_without_params()
    self.config_tester.check_config_arguments_init()

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      int:





    '''simple docstring'''




    UpperCAmelCase	:					List[Any]	=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE						)

   @unittest.skip(reason="""Got `CUDA error: misaligned address` with PyTorch 2.0.0."""						)
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Union[str, Any]:





    '''simple docstring'''




    pass

   @unittest.skip(reason="""Swinv2 does not use inputs_embeds"""						)
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Optional[int]:





    '''simple docstring'''




    pass

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      List[Any]:





    '''simple docstring'''




    UpperCAmelCase	,		UpperCAmelCase	:					str	=	self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
     UpperCAmelCase	:					int	=	model_class(_SCREAMING_SNAKE_CASE						)
     self.assertIsInstance(model.get_input_embeddings()      ,     (nn.Module)						)
     UpperCAmelCase	:					List[str]	=	model.get_output_embeddings()
     self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE      ,     nn.Linear						)						)

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      List[str]:





    '''simple docstring'''




    UpperCAmelCase	,		UpperCAmelCase	:					Any	=	self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
     UpperCAmelCase	:					List[str]	=	model_class(_SCREAMING_SNAKE_CASE						)
     UpperCAmelCase	:					List[str]	=	inspect.signature(model.forward						)
     # signature.parameters is an OrderedDict => so arg_names order is deterministic
     UpperCAmelCase	:					List[str]	=	[*signature.parameters.keys()]

     UpperCAmelCase	:					int	=	["""pixel_values"""]
     self.assertListEqual(arg_names[:1]      ,     _SCREAMING_SNAKE_CASE						)

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Tuple:





    '''simple docstring'''




    UpperCAmelCase	,		UpperCAmelCase	:					List[str]	=	self.model_tester.prepare_config_and_inputs_for_common()
    UpperCAmelCase	:					List[str]	=	True

    for model_class in self.all_model_classes:
     UpperCAmelCase	:					str	=	True
     UpperCAmelCase	:					str	=	False
     UpperCAmelCase	:					Dict	=	True
     UpperCAmelCase	:					Dict	=	model_class(_SCREAMING_SNAKE_CASE						)
     model.to(_SCREAMING_SNAKE_CASE						)
     model.eval()
     with torch.no_grad():
      UpperCAmelCase	:					Union[str, Any]	=	model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)						)
     UpperCAmelCase	:					Any	=	outputs.attentions
     UpperCAmelCase	:					Tuple	=	len(self.model_tester.depths						)
     self.assertEqual(len(_SCREAMING_SNAKE_CASE						)      ,     _SCREAMING_SNAKE_CASE						)

     # check that output_attentions also work using config
     del inputs_dict["output_attentions"]
     UpperCAmelCase	:					Any	=	True
     UpperCAmelCase	:					Optional[int]	=	config.window_size**2
     UpperCAmelCase	:					Dict	=	model_class(_SCREAMING_SNAKE_CASE						)
     model.to(_SCREAMING_SNAKE_CASE						)
     model.eval()
     with torch.no_grad():
      UpperCAmelCase	:					Optional[int]	=	model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)						)
     UpperCAmelCase	:					Dict	=	outputs.attentions
     self.assertEqual(len(_SCREAMING_SNAKE_CASE						)      ,     _SCREAMING_SNAKE_CASE						)

     self.assertListEqual(
         list(attentions[0].shape[-3:]						)      ,     [self.model_tester.num_heads[0], window_size_squared, window_size_squared]      ,     )
     UpperCAmelCase	:					Any	=	len(_SCREAMING_SNAKE_CASE						)

     # Check attention is always last and order is fine
     UpperCAmelCase	:					int	=	True
     UpperCAmelCase	:					int	=	True
     UpperCAmelCase	:					Union[str, Any]	=	model_class(_SCREAMING_SNAKE_CASE						)
     model.to(_SCREAMING_SNAKE_CASE						)
     model.eval()
     with torch.no_grad():
      UpperCAmelCase	:					int	=	model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)						)

     if hasattr(self.model_tester      ,     """num_hidden_states_types"""						):
      UpperCAmelCase	:					List[Any]	=	self.model_tester.num_hidden_states_types
     else:
      # also another +1 for reshaped_hidden_states
      UpperCAmelCase	:					List[Any]	=	2
     self.assertEqual(out_len + added_hidden_states      ,     len(_SCREAMING_SNAKE_CASE						)						)

     UpperCAmelCase	:					Optional[int]	=	outputs.attentions

     self.assertEqual(len(_SCREAMING_SNAKE_CASE						)      ,     _SCREAMING_SNAKE_CASE						)

     self.assertListEqual(
         list(self_attentions[0].shape[-3:]						)      ,     [self.model_tester.num_heads[0], window_size_squared, window_size_squared]      ,     )

   def 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      List[str]:





    '''simple docstring'''




    UpperCAmelCase	:					Dict	=	model_class(_SCREAMING_SNAKE_CASE						)
    model.to(_SCREAMING_SNAKE_CASE						)
    model.eval()

    with torch.no_grad():
     UpperCAmelCase	:					int	=	model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)						)

    UpperCAmelCase	:					List[Any]	=	outputs.hidden_states

    UpperCAmelCase	:					Optional[Any]	=	getattr(
        self.model_tester      ,     """expected_num_hidden_layers"""      ,     len(self.model_tester.depths						) + 1						)
    self.assertEqual(len(_SCREAMING_SNAKE_CASE						)      ,     _SCREAMING_SNAKE_CASE						)

    # Swinv2 has a different seq_length
    UpperCAmelCase	:					Union[str, Any]	=	(
        config.patch_size
        if isinstance(config.patch_size      ,     collections.abc.Iterable						)
        else (config.patch_size, config.patch_size)
    )

    UpperCAmelCase	:					Tuple	=	(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

    self.assertListEqual(
        list(hidden_states[0].shape[-2:]						)      ,     [num_patches, self.model_tester.embed_dim]      ,     )

    UpperCAmelCase	:					List[str]	=	outputs.reshaped_hidden_states
    self.assertEqual(len(_SCREAMING_SNAKE_CASE						)      ,     _SCREAMING_SNAKE_CASE						)

    UpperCAmelCase	,		UpperCAmelCase	,		UpperCAmelCase	,		UpperCAmelCase	:					Any	=	reshaped_hidden_states[0].shape
    UpperCAmelCase	:					List[str]	=	(
        reshaped_hidden_states[0].view(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     height * width						).permute(0      ,     2      ,     1						)
    )
    self.assertListEqual(
        list(reshaped_hidden_states.shape[-2:]						)      ,     [num_patches, self.model_tester.embed_dim]      ,     )

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Dict:





    '''simple docstring'''




    UpperCAmelCase	,		UpperCAmelCase	:					int	=	self.model_tester.prepare_config_and_inputs_for_common()

    UpperCAmelCase	:					Optional[int]	=	(
        self.model_tester.image_size
        if isinstance(self.model_tester.image_size      ,     collections.abc.Iterable						)
        else (self.model_tester.image_size, self.model_tester.image_size)
    )

    for model_class in self.all_model_classes:
     UpperCAmelCase	:					List[str]	=	True
     self.check_hidden_states_output(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)

     # check that output_hidden_states also work using config
     del inputs_dict["output_hidden_states"]
     UpperCAmelCase	:					Optional[Any]	=	True

     self.check_hidden_states_output(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      List[str]:





    '''simple docstring'''




    UpperCAmelCase	,		UpperCAmelCase	:					Any	=	self.model_tester.prepare_config_and_inputs_for_common()
    UpperCAmelCase	:					List[str]	=	3

    UpperCAmelCase	:					Union[str, Any]	=	(
        self.model_tester.image_size
        if isinstance(self.model_tester.image_size      ,     collections.abc.Iterable						)
        else (self.model_tester.image_size, self.model_tester.image_size)
    )
    UpperCAmelCase	:					Dict	=	(
        config.patch_size
        if isinstance(config.patch_size      ,     collections.abc.Iterable						)
        else (config.patch_size, config.patch_size)
    )

    UpperCAmelCase	:					Optional[int]	=	image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
    UpperCAmelCase	:					Dict	=	image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

    for model_class in self.all_model_classes:
     UpperCAmelCase	:					List[Any]	=	True
     self.check_hidden_states_output(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     (padded_height, padded_width)						)

     # check that output_hidden_states also work using config
     del inputs_dict["output_hidden_states"]
     UpperCAmelCase	:					Union[str, Any]	=	True
     self.check_hidden_states_output(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     (padded_height, padded_width)						)

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





    '''simple docstring'''




    UpperCAmelCase	:					int	=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_masked_image_modeling(*_SCREAMING_SNAKE_CASE						)

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





    '''simple docstring'''




    UpperCAmelCase	:					Union[str, Any]	=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE						)

   @slow
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Optional[Any]:





    '''simple docstring'''




    for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     UpperCAmelCase	:					Tuple	=	SwinvaModel.from_pretrained(_SCREAMING_SNAKE_CASE						)
     self.assertIsNotNone(_SCREAMING_SNAKE_CASE						)




   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      List[Any]:





    '''simple docstring'''




    UpperCAmelCase	,		UpperCAmelCase	:					Optional[Any]	=	self.model_tester.prepare_config_and_inputs_for_common()

    UpperCAmelCase	:					Any	=	_config_zero_init(_SCREAMING_SNAKE_CASE						)
    for model_class in self.all_model_classes:
     UpperCAmelCase	:					List[str]	=	model_class(config=_SCREAMING_SNAKE_CASE						)
     for name, param in model.named_parameters():
      if "embeddings" not in name and "logit_scale" not in name and param.requires_grad:
       self.assertIn(
           ((param.data.mean() * 1E9).round() / 1E9).item()      ,     [0.0, 1.0]      ,     msg=F"Parameter {name} of model {model_class} seems not properly initialized"      ,     )



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

   @cached_property
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Optional[Any]:





    '''simple docstring'''




    return (
        AutoImageProcessor.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256"""						)
        if is_vision_available()
        else None
    )




   @slow
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      str:





    '''simple docstring'''




    UpperCAmelCase	:					Union[str, Any]	=	SwinvaForImageClassification.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256"""						).to(
        _SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					List[str]	=	self.default_image_processor

    UpperCAmelCase	:					Any	=	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						)
    UpperCAmelCase	:					Tuple	=	image_processor(images=_SCREAMING_SNAKE_CASE      ,     return_tensors="""pt"""						).to(_SCREAMING_SNAKE_CASE						)

    # forward pass
    with torch.no_grad():
     UpperCAmelCase	:					Dict	=	model(**_SCREAMING_SNAKE_CASE						)

    # verify the logits
    UpperCAmelCase	:					int	=	torch.Size((1, 1000)						)
    self.assertEqual(outputs.logits.shape      ,     _SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					str	=	torch.tensor([-0.3947, -0.4306, 0.0026]						).to(_SCREAMING_SNAKE_CASE						)
    self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,     _SCREAMING_SNAKE_CASE      ,     atol=1E-4						)						)






import torch

from diffusers import DPMSolverSDEScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import require_torchsde

from .test_schedulers import SchedulerCommonTest







@require_torchsde
class 						lowercase							(   UpperCamelCase__		):
     _a							 =						(DPMSolverSDEScheduler,)
     _a							 =						1_0

     def 					a__	(  self		,     **_a				)						->							Optional[Any]:
           _A							:     str			=						{
               """num_train_timesteps""": 1100,
               """beta_start""": 0.0001,
               """beta_end""": 0.02,
               """beta_schedule""": """linear""",
               """noise_sampler_seed""": 0,
           }

           config.update(**_a				)
           return config

     def 					a__	(  self				)						->							Tuple:
           for timesteps in [10, 50, 100, 1000]:
                 self.check_over_configs(num_train_timesteps=_a				)

     def 					a__	(  self				)						->							Optional[int]:
           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 					a__	(  self				)						->							Any:
           for schedule in ["linear", "scaled_linear"]:
                 self.check_over_configs(beta_schedule=_a				)

     def 					a__	(  self				)						->							Optional[int]:
           for prediction_type in ["epsilon", "v_prediction"]:
                 self.check_over_configs(prediction_type=_a				)

     def 					a__	(  self				)						->							Optional[int]:
           _A							:     Any			=						self.scheduler_classes[0]
           _A							:     List[str]			=						self.get_scheduler_config()
           _A							:     Optional[Any]			=						scheduler_class(**_a				)

           scheduler.set_timesteps(self.num_inference_steps				)

           _A							:     Dict			=						self.dummy_model()
           _A							:     Any			=						self.dummy_sample_deter * scheduler.init_noise_sigma
           _A							:     Dict			=						sample.to(_a				)

           for i, t in enumerate(scheduler.timesteps				):
                 _A							:     Optional[int]			=						scheduler.scale_model_input(_a		,     _a				)

                 _A							:     str			=						model(_a		,     _a				)

                 _A							:     List[Any]			=						scheduler.step(_a		,     _a		,     _a				)
                 _A							:     Optional[int]			=						output.prev_sample

           _A							:     Dict			=						torch.sum(torch.abs(_a				)				)
           _A							:     Dict			=						torch.mean(torch.abs(_a				)				)

           if torch_device in ["mps"]:
                 assert abs(result_sum.item() - 167.47821044921875				) < 1e-2
                 assert abs(result_mean.item() - 0.2178705964565277				) < 1e-3
           elif torch_device in ["cuda"]:
                 assert abs(result_sum.item() - 171.59352111816406				) < 1e-2
                 assert abs(result_mean.item() - 0.22342906892299652				) < 1e-3
           else:
                 assert abs(result_sum.item() - 162.52383422851562				) < 1e-2
                 assert abs(result_mean.item() - 0.211619570851326				) < 1e-3

     def 					a__	(  self				)						->							Optional[Any]:
           _A							:     Dict			=						self.scheduler_classes[0]
           _A							:     Optional[int]			=						self.get_scheduler_config(prediction_type="""v_prediction"""				)
           _A							:     Optional[Any]			=						scheduler_class(**_a				)

           scheduler.set_timesteps(self.num_inference_steps				)

           _A							:     Tuple			=						self.dummy_model()
           _A							:     int			=						self.dummy_sample_deter * scheduler.init_noise_sigma
           _A							:     Tuple			=						sample.to(_a				)

           for i, t in enumerate(scheduler.timesteps				):
                 _A							:     int			=						scheduler.scale_model_input(_a		,     _a				)

                 _A							:     Tuple			=						model(_a		,     _a				)

                 _A							:     Dict			=						scheduler.step(_a		,     _a		,     _a				)
                 _A							:     Optional[int]			=						output.prev_sample

           _A							:     Optional[Any]			=						torch.sum(torch.abs(_a				)				)
           _A							:     List[Any]			=						torch.mean(torch.abs(_a				)				)

           if torch_device in ["mps"]:
                 assert abs(result_sum.item() - 124.77149200439453				) < 1e-2
                 assert abs(result_mean.item() - 0.16226289014816284				) < 1e-3
           elif torch_device in ["cuda"]:
                 assert abs(result_sum.item() - 128.1663360595703				) < 1e-2
                 assert abs(result_mean.item() - 0.16688326001167297				) < 1e-3
           else:
                 assert abs(result_sum.item() - 119.8487548828125				) < 1e-2
                 assert abs(result_mean.item() - 0.1560530662536621				) < 1e-3

     def 					a__	(  self				)						->							List[str]:
           _A							:     Union[str, Any]			=						self.scheduler_classes[0]
           _A							:     List[Any]			=						self.get_scheduler_config()
           _A							:     List[str]			=						scheduler_class(**_a				)

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

           _A							:     Union[str, Any]			=						self.dummy_model()
           _A							:     Optional[Any]			=						self.dummy_sample_deter.to(_a				) * scheduler.init_noise_sigma

           for t in scheduler.timesteps:
                 _A							:     int			=						scheduler.scale_model_input(_a		,     _a				)

                 _A							:     List[Any]			=						model(_a		,     _a				)

                 _A							:     Dict			=						scheduler.step(_a		,     _a		,     _a				)
                 _A							:     Dict			=						output.prev_sample

           _A							:     str			=						torch.sum(torch.abs(_a				)				)
           _A							:     str			=						torch.mean(torch.abs(_a				)				)

           if torch_device in ["mps"]:
                 assert abs(result_sum.item() - 167.46957397460938				) < 1e-2
                 assert abs(result_mean.item() - 0.21805934607982635				) < 1e-3
           elif torch_device in ["cuda"]:
                 assert abs(result_sum.item() - 171.59353637695312				) < 1e-2
                 assert abs(result_mean.item() - 0.22342908382415771				) < 1e-3
           else:
                 assert abs(result_sum.item() - 162.52383422851562				) < 1e-2
                 assert abs(result_mean.item() - 0.211619570851326				) < 1e-3



     def 					a__	(  self				)						->							Union[str, Any]:
           _A							:     List[Any]			=						self.scheduler_classes[0]
           _A							:     Optional[Any]			=						self.get_scheduler_config()
           _A							:     int			=						scheduler_class(**_a		,     use_karras_sigmas=_a				)

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

           _A							:     Optional[Any]			=						self.dummy_model()
           _A							:     Dict			=						self.dummy_sample_deter.to(_a				) * scheduler.init_noise_sigma
           _A							:     str			=						sample.to(_a				)

           for t in scheduler.timesteps:
                 _A							:     Optional[int]			=						scheduler.scale_model_input(_a		,     _a				)

                 _A							:     List[Any]			=						model(_a		,     _a				)

                 _A							:     Dict			=						scheduler.step(_a		,     _a		,     _a				)
                 _A							:     List[str]			=						output.prev_sample

           _A							:     str			=						torch.sum(torch.abs(_a				)				)
           _A							:     List[str]			=						torch.mean(torch.abs(_a				)				)

           if torch_device in ["mps"]:
                 assert abs(result_sum.item() - 176.66974135742188				) < 1e-2
                 assert abs(result_mean.item() - 0.23003872730981811				) < 1e-2
           elif torch_device in ["cuda"]:
                 assert abs(result_sum.item() - 177.63653564453125				) < 1e-2
                 assert abs(result_mean.item() - 0.23003872730981811				) < 1e-2
           else:
                 assert abs(result_sum.item() - 170.3135223388672				) < 1e-2
                 assert abs(result_mean.item() - 0.23003872730981811				) < 1e-2

