


"""simple docstring"""



import copy
import inspect
import unittest

from transformers import AutoBackbone
from transformers.configuration_utils import PretrainedConfig
from transformers.testing_utils import require_timm, require_torch, torch_device
from transformers.utils.import_utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor


if is_torch_available():
  import torch

  from transformers import TimmBackbone, TimmBackboneConfig

from ...test_pipeline_mixin import PipelineTesterMixin







class _lowerCAmelCase							:







   def __init__(      self		,      UpperCamelCase__		,      UpperCamelCase__=None		,      UpperCamelCase__=None		,      UpperCamelCase__=None		,      UpperCamelCase__="resnet50"		,      UpperCamelCase__=3		,      UpperCamelCase__=32		,      UpperCamelCase__=3		,      UpperCamelCase__=True		,      UpperCamelCase__=True		,      )      ->						Optional[Any]:

       '''simple docstring'''




       snake_case       : Optional[int]    =       parent
       snake_case       : Union[str, Any]    =       out_indices if out_indices is not None else [4]
       snake_case       : str    =       stage_names
       snake_case       : Optional[Any]    =       out_features
       snake_case       : Tuple    =       backbone
       snake_case       : List[str]    =       batch_size
       snake_case       : Any    =       image_size
       snake_case       : Tuple    =       num_channels
       snake_case       : Tuple    =       use_pretrained_backbone
       snake_case       : Dict    =       is_training







   def 					lowerCamelCase						(      self		)      ->						Dict:

       '''simple docstring'''




       snake_case       : Tuple    =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)
       snake_case       : int    =       self.get_config()

       return config, pixel_values







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

       '''simple docstring'''




       return TimmBackboneConfig(
           image_size=self.image_size		,      num_channels=self.num_channels		,      out_features=self.out_features		,      out_indices=self.out_indices		,      stage_names=self.stage_names		,      use_pretrained_backbone=self.use_pretrained_backbone		,      backbone=self.backbone		,      )







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

       '''simple docstring'''




       snake_case       : Dict    =       TimmBackbone(config=UpperCamelCase__		)
       model.to(UpperCamelCase__		)
       model.eval()
       with torch.no_grad():
           snake_case       : int    =       model(UpperCamelCase__		)
       self.parent.assertEqual(
           result.feature_map[-1].shape		,      (self.batch_size, model.channels[-1], 14, 14)		,      )







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

       '''simple docstring'''




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







@require_torch
@require_timm
class _lowerCAmelCase							( snake_case_       ,				snake_case_       ,				snake_case_       ,				unittest.TestCase      ):
   __UpperCAmelCase					:			List[str]				    =    (TimmBackbone,) if is_torch_available() else ()
   __UpperCAmelCase					:			str				    =    {'''feature-extraction''': TimmBackbone} if is_torch_available() else {}
   __UpperCAmelCase					:			Tuple				    =    False
   __UpperCAmelCase					:			List[str]				    =    False
   __UpperCAmelCase					:			Optional[int]				    =    False
   __UpperCAmelCase					:			int				    =    False







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

       '''simple docstring'''




       snake_case       : List[str]    =       TimmBackboneModelTester(self		)
       snake_case       : Dict    =       ConfigTester(self		,      config_class=UpperCamelCase__		,      has_text_modality=UpperCamelCase__		)







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

       '''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 					lowerCamelCase						(      self		)      ->						Optional[int]:

       '''simple docstring'''




       snake_case       : Any    =       "resnet18"
       snake_case       : int    =       "microsoft/resnet-18"

       snake_case       : Optional[int]    =       AutoBackbone.from_pretrained(UpperCamelCase__		,      use_timm_backbone=UpperCamelCase__		)
       snake_case       : List[Any]    =       AutoBackbone.from_pretrained(UpperCamelCase__		)

       self.assertEqual(len(timm_model.out_features		)		,      len(transformers_model.out_features		)		)
       self.assertEqual(len(timm_model.stage_names		)		,      len(transformers_model.stage_names		)		)
       self.assertEqual(timm_model.channels		,      transformers_model.channels		)
       # Out indices are set to the last layer by default. For timm models, we don't know
       # the number of layers in advance, so we set it to (-1,), whereas for transformers
       # models, we set it to [len(stage_names) - 1] (kept for backward compatibility).
       self.assertEqual(timm_model.out_indices		,      (-1,)		)
       self.assertEqual(transformers_model.out_indices		,      [len(timm_model.stage_names		) - 1]		)

       snake_case       : int    =       AutoBackbone.from_pretrained(UpperCamelCase__		,      use_timm_backbone=UpperCamelCase__		,      out_indices=[1, 2, 3]		)
       snake_case       : Tuple    =       AutoBackbone.from_pretrained(UpperCamelCase__		,      out_indices=[1, 2, 3]		)

       self.assertEqual(timm_model.out_indices		,      transformers_model.out_indices		)
       self.assertEqual(len(timm_model.out_features		)		,      len(transformers_model.out_features		)		)
       self.assertEqual(timm_model.channels		,      transformers_model.channels		)







   @unittest.skip("TimmBackbone doesn't support feed forward chunking"		)
   def 					lowerCamelCase						(      self		)      ->						str:

       '''simple docstring'''




       pass







   @unittest.skip("TimmBackbone doesn't have num_hidden_layers attribute"		)
   def 					lowerCamelCase						(      self		)      ->						int:

       '''simple docstring'''




       pass







   @unittest.skip("TimmBackbone initialization is managed on the timm side"		)
   def 					lowerCamelCase						(      self		)      ->						int:

       '''simple docstring'''




       pass







   @unittest.skip("TimmBackbone models doesn't have inputs_embeds"		)
   def 					lowerCamelCase						(      self		)      ->						Tuple:

       '''simple docstring'''




       pass







   @unittest.skip("TimmBackbone models doesn't have inputs_embeds"		)
   def 					lowerCamelCase						(      self		)      ->						List[Any]:

       '''simple docstring'''




       pass







   @unittest.skip("TimmBackbone model cannot be created without specifying a backbone checkpoint"		)
   def 					lowerCamelCase						(      self		)      ->						List[str]:

       '''simple docstring'''




       pass







   @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone"		)
   def 					lowerCamelCase						(      self		)      ->						Union[str, Any]:

       '''simple docstring'''




       pass







   @unittest.skip("model weights aren't tied in TimmBackbone."		)
   def 					lowerCamelCase						(      self		)      ->						Optional[int]:

       '''simple docstring'''




       pass







   @unittest.skip("model weights aren't tied in TimmBackbone."		)
   def 					lowerCamelCase						(      self		)      ->						Dict:

       '''simple docstring'''




       pass







   @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone"		)
   def 					lowerCamelCase						(      self		)      ->						Union[str, Any]:

       '''simple docstring'''




       pass







   @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone"		)
   def 					lowerCamelCase						(      self		)      ->						Any:

       '''simple docstring'''




       pass







   @unittest.skip("TimmBackbone doesn't have hidden size info in its configuration."		)
   def 					lowerCamelCase						(      self		)      ->						Any:

       '''simple docstring'''




       pass







   @unittest.skip("TimmBackbone doesn't support output_attentions."		)
   def 					lowerCamelCase						(      self		)      ->						List[Any]:

       '''simple docstring'''




       pass







   @unittest.skip("Safetensors is not supported by timm."		)
   def 					lowerCamelCase						(      self		)      ->						Any:

       '''simple docstring'''




       pass







   @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."		)
   def 					lowerCamelCase						(      self		)      ->						Dict:

       '''simple docstring'''




       pass







   def 					lowerCamelCase						(      self		)      ->						Any:

       '''simple docstring'''




       snake_case   ,snake_case       : List[Any]    =       self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           snake_case       : Dict    =       model_class(UpperCamelCase__		)
           snake_case       : List[Any]    =       inspect.signature(model.forward		)
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           snake_case       : Optional[Any]    =       [*signature.parameters.keys()]

           snake_case       : int    =       ["pixel_values"]
           self.assertListEqual(arg_names[:1]		,      UpperCamelCase__		)







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

       '''simple docstring'''




       snake_case   ,snake_case       : Dict    =       self.model_tester.prepare_config_and_inputs_for_common()
       snake_case       : Optional[int]    =       True
       snake_case       : Dict    =       self.has_attentions

       # no need to test all models as different heads yield the same functionality
       snake_case       : List[Any]    =       self.all_model_classes[0]
       snake_case       : Optional[Any]    =       model_class(UpperCamelCase__		)
       model.to(UpperCamelCase__		)

       snake_case       : Tuple    =       self._prepare_for_class(UpperCamelCase__		,      UpperCamelCase__		)
       snake_case       : Dict    =       model(**UpperCamelCase__		)
       snake_case       : Optional[int]    =       outputs[0][-1]

       # Encoder-/Decoder-only models
       snake_case       : Any    =       outputs.hidden_states[0]
       hidden_states.retain_grad()

       if self.has_attentions:
           snake_case       : Tuple    =       outputs.attentions[0]
           attentions.retain_grad()

       output.flatten()[0].backward(retain_graph=UpperCamelCase__		)

       self.assertIsNotNone(hidden_states.grad		)

       if self.has_attentions:
           self.assertIsNotNone(attentions.grad		)







   def 					lowerCamelCase						(      self		)      ->						int:

       '''simple docstring'''




       snake_case   ,snake_case       : Tuple    =       self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           snake_case       : List[Any]    =       model_class(UpperCamelCase__		)
           model.to(UpperCamelCase__		)
           model.eval()
           snake_case       : str    =       model(**UpperCamelCase__		)

           self.assertEqual(len(result.feature_maps		)		,      len(config.out_indices		)		)
           self.assertEqual(len(model.channels		)		,      len(config.out_indices		)		)

           # Check output of last stage is taken if out_features=None, out_indices=None
           snake_case       : Any    =       copy.deepcopy(UpperCamelCase__		)
           snake_case       : Optional[int]    =       None
           snake_case       : Dict    =       model_class(UpperCamelCase__		)
           model.to(UpperCamelCase__		)
           model.eval()
           snake_case       : Optional[int]    =       model(**UpperCamelCase__		)

           self.assertEqual(len(result.feature_maps		)		,      1		)
           self.assertEqual(len(model.channels		)		,      1		)

           # Check backbone can be initialized with fresh weights
           snake_case       : Optional[Any]    =       copy.deepcopy(UpperCamelCase__		)
           snake_case       : Optional[int]    =       False
           snake_case       : int    =       model_class(UpperCamelCase__		)
           model.to(UpperCamelCase__		)
           model.eval()
           snake_case       : Any    =       model(**UpperCamelCase__		)









"""simple docstring"""



import torch

from diffusers import KDPMaDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest







class _lowerCAmelCase							( snake_case_      ):
   __UpperCAmelCase					:			List[str]				    =    (KDPMaDiscreteScheduler,)
   __UpperCAmelCase					:			List[Any]				    =    10







   def 					lowerCamelCase						(      self		,      **UpperCamelCase__		)      ->						Optional[int]:

       '''simple docstring'''




       snake_case       : List[str]    =       {
           "num_train_timesteps": 1100,
           "beta_start": 0.0001,
           "beta_end": 0.02,
           "beta_schedule": "linear",
       }

       config.update(**UpperCamelCase__		)
       return config







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

       '''simple docstring'''




       for timesteps in [10, 50, 100, 1000]:
           self.check_over_configs(num_train_timesteps=UpperCamelCase__		)







   def 					lowerCamelCase						(      self		)      ->						str:

       '''simple docstring'''




       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=UpperCamelCase__		,      beta_end=UpperCamelCase__		)







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

       '''simple docstring'''




       for schedule in ["linear", "scaled_linear"]:
           self.check_over_configs(beta_schedule=UpperCamelCase__		)







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

       '''simple docstring'''




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







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

       '''simple docstring'''




       snake_case       : str    =       self.scheduler_classes[0]
       snake_case       : str    =       self.get_scheduler_config(prediction_type="v_prediction"		)
       snake_case       : Dict    =       scheduler_class(**UpperCamelCase__		)

       scheduler.set_timesteps(self.num_inference_steps		)

       snake_case       : List[Any]    =       self.dummy_model()
       snake_case       : List[str]    =       self.dummy_sample_deter * scheduler.init_noise_sigma
       snake_case       : Optional[Any]    =       sample.to(UpperCamelCase__		)

       for i, t in enumerate(scheduler.timesteps		):
           snake_case       : int    =       scheduler.scale_model_input(UpperCamelCase__		,      UpperCamelCase__		)

           snake_case       : int    =       model(UpperCamelCase__		,      UpperCamelCase__		)

           snake_case       : Optional[Any]    =       scheduler.step(UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		)
           snake_case       : Optional[Any]    =       output.prev_sample

       snake_case       : Optional[Any]    =       torch.sum(torch.abs(UpperCamelCase__		)		)
       snake_case       : Any    =       torch.mean(torch.abs(UpperCamelCase__		)		)

       if torch_device in ["cpu", "mps"]:
           assert abs(result_sum.item() - 4.6934e-07		) < 1e-2
           assert abs(result_mean.item() - 6.1112e-10		) < 1e-3
       else:
           # CUDA
           assert abs(result_sum.item() - 4.693_4286_5017_0972e-07		) < 1e-2
           assert abs(result_mean.item() - 0.0002		) < 1e-3







   def 					lowerCamelCase						(      self		)      ->						Dict:

       '''simple docstring'''




       if torch_device == "mps":
           return
       snake_case       : Optional[Any]    =       self.scheduler_classes[0]
       snake_case       : Optional[int]    =       self.get_scheduler_config()
       snake_case       : Dict    =       scheduler_class(**UpperCamelCase__		)

       scheduler.set_timesteps(self.num_inference_steps		)

       snake_case       : List[Any]    =       self.dummy_model()
       snake_case       : Union[str, Any]    =       self.dummy_sample_deter * scheduler.init_noise_sigma
       snake_case       : List[str]    =       sample.to(UpperCamelCase__		)

       for i, t in enumerate(scheduler.timesteps		):
           snake_case       : List[Any]    =       scheduler.scale_model_input(UpperCamelCase__		,      UpperCamelCase__		)

           snake_case       : Optional[int]    =       model(UpperCamelCase__		,      UpperCamelCase__		)

           snake_case       : Optional[int]    =       scheduler.step(UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		)
           snake_case       : int    =       output.prev_sample

       snake_case       : int    =       torch.sum(torch.abs(UpperCamelCase__		)		)
       snake_case       : Union[str, Any]    =       torch.mean(torch.abs(UpperCamelCase__		)		)

       if torch_device in ["cpu", "mps"]:
           assert abs(result_sum.item() - 20.4125		) < 1e-2
           assert abs(result_mean.item() - 0.0266		) < 1e-3
       else:
           # CUDA
           assert abs(result_sum.item() - 20.4125		) < 1e-2
           assert abs(result_mean.item() - 0.0266		) < 1e-3







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

       '''simple docstring'''




       if torch_device == "mps":
           return
       snake_case       : Any    =       self.scheduler_classes[0]
       snake_case       : Any    =       self.get_scheduler_config()
       snake_case       : Union[str, Any]    =       scheduler_class(**UpperCamelCase__		)

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

       snake_case       : Optional[Any]    =       self.dummy_model()
       snake_case       : Optional[int]    =       self.dummy_sample_deter.to(UpperCamelCase__		) * scheduler.init_noise_sigma

       for t in scheduler.timesteps:
           snake_case       : Optional[Any]    =       scheduler.scale_model_input(UpperCamelCase__		,      UpperCamelCase__		)

           snake_case       : Any    =       model(UpperCamelCase__		,      UpperCamelCase__		)

           snake_case       : List[Any]    =       scheduler.step(UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		)
           snake_case       : Dict    =       output.prev_sample

       snake_case       : Dict    =       torch.sum(torch.abs(UpperCamelCase__		)		)
       snake_case       : str    =       torch.mean(torch.abs(UpperCamelCase__		)		)

       if str(UpperCamelCase__		).startswith("cpu"		):
           # The following sum varies between 148 and 156 on mps. Why?
           assert abs(result_sum.item() - 20.4125		) < 1e-2
           assert abs(result_mean.item() - 0.0266		) < 1e-3
       else:
           # CUDA
           assert abs(result_sum.item() - 20.4125		) < 1e-2
           assert abs(result_mean.item() - 0.0266		) < 1e-3





