






'''simple docstring'''







import copy
import inspect

from transformers.testing_utils import require_torch, torch_device
from transformers.utils.backbone_utils import BackboneType




@require_torch
class 					__UpperCAmelCase :

      '''simple docstring'''





      SCREAMING_SNAKE_CASE				:    List[Any]				       =					()
      SCREAMING_SNAKE_CASE				:    Union[str, Any]				       =					True




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   Optional[int]												=	self.config_class

             # test default config
             UpperCAmelCase__	:   Optional[int]												=	config_class()
             self.assertIsNotNone(_UpperCAmelCase						)
             UpperCAmelCase__	:   Optional[int]												=	['''stem'''] + [F"""stage{idx}""" for idx in range(1  ,		len(config.depths						) + 1						)]
             self.assertEqual(config.stage_names  ,		_UpperCAmelCase						)
             self.assertTrue(set(config.out_features						).issubset(set(config.stage_names						)						)						)

             # Test out_features and out_indices are correctly set
             # out_features and out_indices both None
             UpperCAmelCase__	:   Union[str, Any]												=	config_class(out_features=_UpperCAmelCase  ,		out_indices=_UpperCAmelCase						)
             self.assertEqual(config.out_features  ,		[config.stage_names[-1]]						)
             self.assertEqual(config.out_indices  ,		[len(config.stage_names						) - 1]						)

             # out_features and out_indices both set
             UpperCAmelCase__	:   List[Any]												=	config_class(out_features=['''stem''', '''stage1''']  ,		out_indices=[0, 1]						)
             self.assertEqual(config.out_features  ,		['''stem''', '''stage1''']						)
             self.assertEqual(config.out_indices  ,		[0, 1]						)

             # Only out_features set
             UpperCAmelCase__	:   Optional[Any]												=	config_class(out_features=['''stage1''', '''stage3''']						)
             self.assertEqual(config.out_features  ,		['''stage1''', '''stage3''']						)
             self.assertEqual(config.out_indices  ,		[1, 3]						)

             # Only out_indices set
             UpperCAmelCase__	:   List[Any]												=	config_class(out_indices=[0, 2]						)
             self.assertEqual(config.out_features  ,		[config.stage_names[0], config.stage_names[2]]						)
             self.assertEqual(config.out_indices  ,		[0, 2]						)

             # Error raised when out_indices do not correspond to out_features
             with self.assertRaises(_UpperCAmelCase						):
                    UpperCAmelCase__	:   Optional[Any]												=	config_class(out_features=['''stage1''', '''stage2''']  ,		out_indices=[0, 2]						)




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__							,							UpperCAmelCase__	:   List[Any]												=	self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    UpperCAmelCase__	:   Optional[int]												=	model_class(_UpperCAmelCase						)
                    UpperCAmelCase__	:   int												=	inspect.signature(model.forward						)
                    # signature.parameters is an OrderedDict => so arg_names order is deterministic
                    UpperCAmelCase__	:   Any												=	[*signature.parameters.keys()]
                    UpperCAmelCase__	:   Optional[Any]												=	['''pixel_values''']
                    self.assertListEqual(arg_names[:1]  ,		_UpperCAmelCase						)




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__							,							UpperCAmelCase__	:   str												=	self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    UpperCAmelCase__	:   List[Any]												=	model_class(_UpperCAmelCase						)
                    self.assertEqual(len(model.channels						)  ,		len(config.out_features						)						)
                    UpperCAmelCase__	:   List[Any]												=	model.num_features
                    UpperCAmelCase__	:   Dict												=	[config.stage_names.index(_UpperCAmelCase						) for feat in config.out_features]
                    UpperCAmelCase__	:   Tuple												=	[num_features[idx] for idx in out_indices]
                    self.assertListEqual(model.channels  ,		_UpperCAmelCase						)

                    UpperCAmelCase__	:   str												=	copy.deepcopy(_UpperCAmelCase						)
                    UpperCAmelCase__	:   Optional[int]												=	None
                    UpperCAmelCase__	:   str												=	model_class(_UpperCAmelCase						)
                    self.assertEqual(len(model.channels						)  ,		1						)
                    self.assertListEqual(model.channels  ,		[num_features[-1]]						)

                    UpperCAmelCase__	:   str												=	copy.deepcopy(_UpperCAmelCase						)
                    UpperCAmelCase__	:   int												=	None
                    UpperCAmelCase__	:   Union[str, Any]												=	model_class(_UpperCAmelCase						)
                    self.assertEqual(len(model.channels						)  ,		1						)
                    self.assertListEqual(model.channels  ,		[num_features[-1]]						)




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__							,							UpperCAmelCase__	:   Any												=	self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    UpperCAmelCase__	:   Tuple												=	model_class(_UpperCAmelCase						)
                    model.to(_UpperCAmelCase						)
                    model.eval()
                    UpperCAmelCase__	:   Optional[Any]												=	model(**_UpperCAmelCase						)

                    self.assertEqual(len(result.feature_maps						)  ,		len(config.out_features						)						)
                    self.assertEqual(len(model.channels						)  ,		len(config.out_features						)						)
                    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
                    UpperCAmelCase__	:   Any												=	copy.deepcopy(_UpperCAmelCase						)
                    UpperCAmelCase__	:   Dict												=	None
                    UpperCAmelCase__	:   Optional[int]												=	model_class(_UpperCAmelCase						)
                    model.to(_UpperCAmelCase						)
                    model.eval()
                    UpperCAmelCase__	:   Dict												=	model(**_UpperCAmelCase						)

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

                    UpperCAmelCase__	:   Optional[Any]												=	copy.deepcopy(_UpperCAmelCase						)
                    UpperCAmelCase__	:   str												=	None
                    UpperCAmelCase__	:   Dict												=	model_class(_UpperCAmelCase						)
                    model.to(_UpperCAmelCase						)
                    model.eval()
                    UpperCAmelCase__	:   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
                    UpperCAmelCase__	:   Optional[int]												=	copy.deepcopy(_UpperCAmelCase						)
                    UpperCAmelCase__	:   Optional[Any]												=	False
                    UpperCAmelCase__	:   List[Any]												=	model_class(_UpperCAmelCase						)
                    model.to(_UpperCAmelCase						)
                    model.eval()
                    UpperCAmelCase__	:   List[Any]												=	model(**_UpperCAmelCase						)




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__							,							UpperCAmelCase__	:   Union[str, Any]												=	self.model_tester.prepare_config_and_inputs_for_common()

             for backbone_class in self.all_model_classes:
                    UpperCAmelCase__	:   Tuple												=	backbone_class(_UpperCAmelCase						)

                    self.assertTrue(hasattr(_UpperCAmelCase  ,		'''backbone_type'''						)						)
                    self.assertTrue(hasattr(_UpperCAmelCase  ,		'''stage_names'''						)						)
                    self.assertTrue(hasattr(_UpperCAmelCase  ,		'''num_features'''						)						)
                    self.assertTrue(hasattr(_UpperCAmelCase  ,		'''out_indices'''						)						)
                    self.assertTrue(hasattr(_UpperCAmelCase  ,		'''out_features'''						)						)
                    self.assertTrue(hasattr(_UpperCAmelCase  ,		'''out_feature_channels'''						)						)
                    self.assertTrue(hasattr(_UpperCAmelCase  ,		'''channels'''						)						)

                    self.assertIsInstance(backbone.backbone_type  ,		_UpperCAmelCase						)
                    # Verify num_features has been initialized in the backbone init
                    self.assertIsNotNone(backbone.num_features						)
                    self.assertTrue(len(backbone.channels						) == len(backbone.out_indices						)						)
                    self.assertTrue(len(backbone.stage_names						) == len(backbone.num_features						)						)
                    self.assertTrue(len(backbone.channels						) <= len(backbone.num_features						)						)
                    self.assertTrue(len(backbone.out_feature_channels						) == len(backbone.stage_names						)						)




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__							,							UpperCAmelCase__	:   Optional[Any]												=	self.model_tester.prepare_config_and_inputs_for_common()
             UpperCAmelCase__	:   Dict												=	inputs_dict['''pixel_values'''].shape[0]

             for backbone_class in self.all_model_classes:
                    UpperCAmelCase__	:   Optional[Any]												=	backbone_class(_UpperCAmelCase						)
                    backbone.to(_UpperCAmelCase						)
                    backbone.eval()

                    UpperCAmelCase__	:   List[str]												=	backbone(**_UpperCAmelCase						)

                    # Test default outputs and verify feature maps
                    self.assertIsInstance(outputs.feature_maps  ,		_UpperCAmelCase						)
                    self.assertTrue(len(outputs.feature_maps						) == len(backbone.channels						)						)
                    for feature_map, n_channels in zip(outputs.feature_maps  ,		backbone.channels						):
                           self.assertTrue(feature_map.shape[:2]  ,		(batch_size, n_channels)						)
                    self.assertIsNone(outputs.hidden_states						)
                    self.assertIsNone(outputs.attentions						)

                    # Test output_hidden_states=True
                    UpperCAmelCase__	:   List[Any]												=	backbone(**_UpperCAmelCase  ,		output_hidden_states=_UpperCAmelCase						)
                    self.assertIsNotNone(outputs.hidden_states						)
                    self.assertTrue(len(outputs.hidden_states						)  ,		len(backbone.stage_names						)						)
                    for hidden_state, n_channels in zip(outputs.hidden_states  ,		backbone.channels						):
                           self.assertTrue(hidden_state.shape[:2]  ,		(batch_size, n_channels)						)

                    # Test output_attentions=True
                    if self.has_attentions:
                           UpperCAmelCase__	:   Optional[Any]												=	backbone(**_UpperCAmelCase  ,		output_attentions=_UpperCAmelCase						)
                           self.assertIsNotNone(outputs.attentions						)







'''simple docstring'''







import torch

from diffusers import DDIMScheduler

from .test_schedulers import SchedulerCommonTest




class 					__UpperCAmelCase (		UpperCamelCase__	):

      '''simple docstring'''





      SCREAMING_SNAKE_CASE				:    List[Any]				       =					(DDIMScheduler,)
      SCREAMING_SNAKE_CASE				:    str				       =					(("eta", 0.0), ("num_inference_steps", 5_0))




      def 							lowerCamelCase     (					self  ,		**_UpperCAmelCase						):
             UpperCAmelCase__	:   List[str]												=	{
                 '''num_train_timesteps''': 1000,
                 '''beta_start''': 0.00_01,
                 '''beta_end''': 0.02,
                 '''beta_schedule''': '''linear''',
                 '''clip_sample''': True,
             }

             config.update(**_UpperCAmelCase						)
             return config




      def 							lowerCamelCase     (					self  ,		**_UpperCAmelCase						):
             UpperCAmelCase__	:   Dict												=	self.scheduler_classes[0]
             UpperCAmelCase__	:   Union[str, Any]												=	self.get_scheduler_config(**_UpperCAmelCase						)
             UpperCAmelCase__	:   str												=	scheduler_class(**_UpperCAmelCase						)

             UpperCAmelCase__							,							UpperCAmelCase__	:   int												=	10, 0.0

             UpperCAmelCase__	:   Optional[Any]												=	self.dummy_model()
             UpperCAmelCase__	:   List[Any]												=	self.dummy_sample_deter

             scheduler.set_timesteps(_UpperCAmelCase						)

             for t in scheduler.timesteps:
                    UpperCAmelCase__	:   Dict												=	model(_UpperCAmelCase  ,		_UpperCAmelCase						)
                    UpperCAmelCase__	:   int												=	scheduler.step(_UpperCAmelCase  ,		_UpperCAmelCase  ,		_UpperCAmelCase  ,		_UpperCAmelCase						).prev_sample

             return sample




      def 							lowerCamelCase     (					self						):
             for timesteps in [100, 500, 1000]:
                    self.check_over_configs(num_train_timesteps=_UpperCAmelCase						)




      def 							lowerCamelCase     (					self						):
             for steps_offset in [0, 1]:
                    self.check_over_configs(steps_offset=_UpperCAmelCase						)

             UpperCAmelCase__	:   Optional[int]												=	self.scheduler_classes[0]
             UpperCAmelCase__	:   Any												=	self.get_scheduler_config(steps_offset=1						)
             UpperCAmelCase__	:   Optional[Any]												=	scheduler_class(**_UpperCAmelCase						)
             scheduler.set_timesteps(5						)
             assert torch.equal(scheduler.timesteps  ,		torch.LongTensor([801, 601, 401, 201, 1]						)						)




      def 							lowerCamelCase     (					self						):
             for beta_start, beta_end in zip([0.00_01, 0.0_01, 0.01, 0.1]  ,		[0.0_02, 0.02, 0.2, 2]						):
                    self.check_over_configs(beta_start=_UpperCAmelCase  ,		beta_end=_UpperCAmelCase						)




      def 							lowerCamelCase     (					self						):
             for schedule in ["linear", "squaredcos_cap_v2"]:
                    self.check_over_configs(beta_schedule=_UpperCAmelCase						)




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




      def 							lowerCamelCase     (					self						):
             for clip_sample in [True, False]:
                    self.check_over_configs(clip_sample=_UpperCAmelCase						)




      def 							lowerCamelCase     (					self						):
             for timestep_spacing in ["trailing", "leading"]:
                    self.check_over_configs(timestep_spacing=_UpperCAmelCase						)




      def 							lowerCamelCase     (					self						):
             for rescale_betas_zero_snr in [True, False]:
                    self.check_over_configs(rescale_betas_zero_snr=_UpperCAmelCase						)




      def 							lowerCamelCase     (					self						):
             self.check_over_configs(thresholding=_UpperCAmelCase						)
             for threshold in [0.5, 1.0, 2.0]:
                    for prediction_type in ["epsilon", "v_prediction"]:
                           self.check_over_configs(
                               thresholding=_UpperCAmelCase  ,		prediction_type=_UpperCAmelCase  ,		sample_max_value=_UpperCAmelCase  ,		)




      def 							lowerCamelCase     (					self						):
             for t in [1, 10, 49]:
                    self.check_over_forward(time_step=_UpperCAmelCase						)




      def 							lowerCamelCase     (					self						):
             for t, num_inference_steps in zip([1, 10, 50]  ,		[10, 50, 500]						):
                    self.check_over_forward(time_step=_UpperCAmelCase  ,		num_inference_steps=_UpperCAmelCase						)




      def 							lowerCamelCase     (					self						):
             for t, eta in zip([1, 10, 49]  ,		[0.0, 0.5, 1.0]						):
                    self.check_over_forward(time_step=_UpperCAmelCase  ,		eta=_UpperCAmelCase						)




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   str												=	self.scheduler_classes[0]
             UpperCAmelCase__	:   int												=	self.get_scheduler_config()
             UpperCAmelCase__	:   Optional[Any]												=	scheduler_class(**_UpperCAmelCase						)

             assert torch.sum(torch.abs(scheduler._get_variance(0  ,		0						) - 0.0						)						) < 1E-5
             assert torch.sum(torch.abs(scheduler._get_variance(420  ,		400						) - 0.1_47_71						)						) < 1E-5
             assert torch.sum(torch.abs(scheduler._get_variance(980  ,		960						) - 0.3_24_60						)						) < 1E-5
             assert torch.sum(torch.abs(scheduler._get_variance(0  ,		0						) - 0.0						)						) < 1E-5
             assert torch.sum(torch.abs(scheduler._get_variance(487  ,		486						) - 0.0_09_79						)						) < 1E-5
             assert torch.sum(torch.abs(scheduler._get_variance(999  ,		998						) - 0.02						)						) < 1E-5




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   str												=	self.full_loop()

             UpperCAmelCase__	:   Dict												=	torch.sum(torch.abs(_UpperCAmelCase						)						)
             UpperCAmelCase__	:   str												=	torch.mean(torch.abs(_UpperCAmelCase						)						)

             assert abs(result_sum.item() - 1_72.00_67						) < 1E-2
             assert abs(result_mean.item() - 0.22_39_67						) < 1E-3




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   Any												=	self.full_loop(prediction_type='''v_prediction'''						)

             UpperCAmelCase__	:   Union[str, Any]												=	torch.sum(torch.abs(_UpperCAmelCase						)						)
             UpperCAmelCase__	:   Tuple												=	torch.mean(torch.abs(_UpperCAmelCase						)						)

             assert abs(result_sum.item() - 52.53_02						) < 1E-2
             assert abs(result_mean.item() - 0.06_84						) < 1E-3




      def 							lowerCamelCase     (					self						):
             # We specify different beta, so that the first alpha is 0.99
             UpperCAmelCase__	:   str												=	self.full_loop(set_alpha_to_one=_UpperCAmelCase  ,		beta_start=0.01						)
             UpperCAmelCase__	:   List[str]												=	torch.sum(torch.abs(_UpperCAmelCase						)						)
             UpperCAmelCase__	:   Union[str, Any]												=	torch.mean(torch.abs(_UpperCAmelCase						)						)

             assert abs(result_sum.item() - 1_49.82_95						) < 1E-2
             assert abs(result_mean.item() - 0.19_51						) < 1E-3




      def 							lowerCamelCase     (					self						):
             # We specify different beta, so that the first alpha is 0.99
             UpperCAmelCase__	:   str												=	self.full_loop(set_alpha_to_one=_UpperCAmelCase  ,		beta_start=0.01						)
             UpperCAmelCase__	:   Optional[Any]												=	torch.sum(torch.abs(_UpperCAmelCase						)						)
             UpperCAmelCase__	:   int												=	torch.mean(torch.abs(_UpperCAmelCase						)						)

             assert abs(result_sum.item() - 1_49.07_84						) < 1E-2
             assert abs(result_mean.item() - 0.19_41						) < 1E-3