
'''simple docstring'''






import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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


if is_torch_available():
					import torch

					from transformers import CvtForImageClassification, CvtModel
					from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
					from PIL import Image

					from transformers import AutoImageProcessor




class     __snake_case		(   a__):
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Any									= self.config_class(**self.inputs_dict      )
									self.parent.assertTrue(hasattr(A,    'embed_dim'      )      )
									self.parent.assertTrue(hasattr(A,    'num_heads'      )      )





class     __snake_case		:
					def __init__(     self,    A,    A=13,    A=64,    A=3,    A=[16, 48, 96],    A=[1, 3, 6],    A=[1, 2, 10],    A=[7, 3, 3],    A=[4, 2, 2],    A=[2, 1, 1],    A=[2, 2, 2],    A=[False, False, True],    A=[0.0, 0.0, 0.0],    A=0.02,    A=1e-12,    A=True,    A=True,    A=2,    ):

									"""simple docstring"""


									lowerCamelCase					:			List[str]									= parent
									lowerCamelCase					:			Dict									= batch_size
									lowerCamelCase					:			Dict									= image_size
									lowerCamelCase					:			Optional[Any]									= patch_sizes
									lowerCamelCase					:			str									= patch_stride
									lowerCamelCase					:			Union[str, Any]									= patch_padding
									lowerCamelCase					:			Optional[Any]									= is_training
									lowerCamelCase					:			Tuple									= use_labels
									lowerCamelCase					:			Optional[int]									= num_labels
									lowerCamelCase					:			Optional[Any]									= num_channels
									lowerCamelCase					:			Optional[Any]									= embed_dim
									lowerCamelCase					:			int									= num_heads
									lowerCamelCase					:			List[Any]									= stride_kv
									lowerCamelCase					:			Any									= depth
									lowerCamelCase					:			Any									= cls_token
									lowerCamelCase					:			List[Any]									= attention_drop_rate
									lowerCamelCase					:			str									= initializer_range
									lowerCamelCase					:			Optional[Any]									= layer_norm_eps
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[int]									= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

									lowerCamelCase					:			Optional[Any]									= None
									if self.use_labels:
													lowerCamelCase					:			Optional[Any]									= ids_tensor([self.batch_size],    self.num_labels      )

									lowerCamelCase					:			Tuple									= self.get_config()
									return config, pixel_values, labels
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return CvtConfig(
									    image_size=self.image_size,    num_labels=self.num_labels,    num_channels=self.num_channels,    embed_dim=self.embed_dim,    num_heads=self.num_heads,    patch_sizes=self.patch_sizes,    patch_padding=self.patch_padding,    patch_stride=self.patch_stride,    stride_kv=self.stride_kv,    depth=self.depth,    cls_token=self.cls_token,    attention_drop_rate=self.attention_drop_rate,    initializer_range=self.initializer_range,    )
					def     UpperCAmelCase_							(     self,    A,    A,    A      ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[Any]									= CvtModel(config=A      )
									model.to(A      )
									model.eval()
									lowerCamelCase					:			Any									= model(A      )
									lowerCamelCase					:			Union[str, Any]									= (self.image_size, self.image_size)
									lowerCamelCase			, lowerCamelCase					:			Tuple									= image_size[0], image_size[1]
									for i in range(len(self.depth      )      ):
													lowerCamelCase					:			Optional[int]									= floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1      )
													lowerCamelCase					:			Union[str, Any]									= floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1      )
									self.parent.assertEqual(result.last_hidden_state.shape,    (self.batch_size, self.embed_dim[-1], height, width)      )
					def     UpperCAmelCase_							(     self,    A,    A,    A      ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[int]									= self.num_labels
									lowerCamelCase					:			Optional[Any]									= CvtForImageClassification(A      )
									model.to(A      )
									model.eval()
									lowerCamelCase					:			int									= model(A,    labels=A      )
									self.parent.assertEqual(result.logits.shape,    (self.batch_size, self.num_labels)      )




					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			int									= self.prepare_config_and_inputs()
									lowerCamelCase			, lowerCamelCase			, lowerCamelCase					:			str									= config_and_inputs
									lowerCamelCase					:			int									= {'pixel_values': pixel_values}
									return config, inputs_dict





@require_torch
class     __snake_case		(   a__    ,   a__    ,   unittest.TestCase):
					_lowerCAmelCase			     =	(CvtModel, CvtForImageClassification) if is_torch_available() else ()
					_lowerCAmelCase			     =	(
					    {'''feature-extraction''': CvtModel, '''image-classification''': CvtForImageClassification}
					    if is_torch_available()
					    else {}
					)

					_lowerCAmelCase			     =	False
					_lowerCAmelCase			     =	False
					_lowerCAmelCase			     =	False
					_lowerCAmelCase			     =	False
					_lowerCAmelCase			     =	False
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Any									= CvtModelTester(self      )
									lowerCamelCase					:			Optional[int]									= ConfigTester(self,    config_class=A,    has_text_modality=A,    hidden_size=37      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									self.create_and_test_config_common_properties()
									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     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return
					@unittest.skip(reason='Cvt does not output attentions'      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									pass
					@unittest.skip(reason='Cvt does not use inputs_embeds'      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									pass
					@unittest.skip(reason='Cvt does not support input and output embeddings'      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									pass
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


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

									for model_class in self.all_model_classes:
													lowerCamelCase					:			Tuple									= model_class(A      )
													lowerCamelCase					:			Dict									= inspect.signature(model.forward      )
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													lowerCamelCase					:			int									= [*signature.parameters.keys()]

													lowerCamelCase					:			Any									= ['pixel_values']
													self.assertListEqual(arg_names[:1],    A      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			List[str]									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*A      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									def check_hidden_states_output(A,    A,    A      ):
													lowerCamelCase					:			Optional[int]									= model_class(A      )
													model.to(A      )
													model.eval()

													with torch.no_grad():
																	lowerCamelCase					:			Union[str, Any]									= model(**self._prepare_for_class(A,    A      )      )

													lowerCamelCase					:			Union[str, Any]									= outputs.hidden_states

													lowerCamelCase					:			Optional[Any]									= len(self.model_tester.depth      )
													self.assertEqual(len(A      ),    A      )

													# verify the first hidden states (first block)
													self.assertListEqual(
													    list(hidden_states[0].shape[-3:]      ),    [
													        self.model_tester.embed_dim[0],
													        self.model_tester.image_size // 4,
													        self.model_tester.image_size // 4,
													    ],    )

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

									for model_class in self.all_model_classes:
													lowerCamelCase					:			Any									= True
													check_hidden_states_output(A,    A,    A      )

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													lowerCamelCase					:			List[str]									= True

													check_hidden_states_output(A,    A,    A      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			List[Any]									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*A      )
					@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									pass




					@slow
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													lowerCamelCase					:			Optional[int]									= CvtModel.from_pretrained(A      )
													self.assertIsNotNone(A      )





def    UpperCAmelCase							(  ):
				lowerCamelCase					:			List[str]									= Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
				return image




@require_torch
@require_vision
class     __snake_case		(   unittest.TestCase):
					@cached_property
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]      )




					@slow
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			List[Any]									= CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]      ).to(A      )

									lowerCamelCase					:			List[Any]									= self.default_image_processor
									lowerCamelCase					:			Optional[Any]									= prepare_img()
									lowerCamelCase					:			List[Any]									= image_processor(images=A,    return_tensors='pt'      ).to(A      )

									# forward pass
									with torch.no_grad():
													lowerCamelCase					:			Optional[Any]									= model(**A      )

									# verify the logits
									lowerCamelCase					:			Any									= torch.Size((1, 1000)      )
									self.assertEqual(outputs.logits.shape,    A      )

									lowerCamelCase					:			Any									= torch.tensor([0.9285, 0.9015, -0.3150]      ).to(A      )

									self.assertTrue(torch.allclose(outputs.logits[0, :3],    A,    atol=1e-4      )      )


'''simple docstring'''






import tempfile

import torch

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

from .test_schedulers import SchedulerCommonTest




class     __snake_case		(   a__):
					_lowerCAmelCase			     =	(DPMSolverSinglestepScheduler,)
					_lowerCAmelCase			     =	(('''num_inference_steps''', 25),)
					def     UpperCAmelCase_							(     self,    **A      ):

									"""simple docstring"""


									lowerCamelCase					:			List[Any]									= {
									    'num_train_timesteps': 1000,
									    'beta_start': 0.0001,
									    'beta_end': 0.02,
									    '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(**A      )
									return config
					def     UpperCAmelCase_							(     self,    A=0,    **A      ):

									"""simple docstring"""


									lowerCamelCase					:			List[str]									= dict(self.forward_default_kwargs      )
									lowerCamelCase					:			Optional[Any]									= kwargs.pop('num_inference_steps',    A      )
									lowerCamelCase					:			Union[str, Any]									= self.dummy_sample
									lowerCamelCase					:			Dict									= 0.1 * sample
									lowerCamelCase					:			Dict									= [residual + 0.2, residual + 0.15, residual + 0.10]

									for scheduler_class in self.scheduler_classes:
													lowerCamelCase					:			Optional[Any]									= self.get_scheduler_config(**A      )
													lowerCamelCase					:			Dict									= scheduler_class(**A      )
													scheduler.set_timesteps(A      )
													# copy over dummy past residuals
													lowerCamelCase					:			str									= dummy_past_residuals[: scheduler.config.solver_order]

													with tempfile.TemporaryDirectory() as tmpdirname:
																	scheduler.save_config(A      )
																	lowerCamelCase					:			List[Any]									= scheduler_class.from_pretrained(A      )
																	new_scheduler.set_timesteps(A      )
																	# copy over dummy past residuals
																	lowerCamelCase					:			Optional[int]									= dummy_past_residuals[: new_scheduler.config.solver_order]

													lowerCamelCase			, lowerCamelCase					:			Optional[int]									= sample, sample
													for t in range(A,    time_step + scheduler.config.solver_order + 1      ):
																	lowerCamelCase					:			Dict									= scheduler.step(A,    A,    A,    **A      ).prev_sample
																	lowerCamelCase					:			Optional[int]									= 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     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									pass
					def     UpperCAmelCase_							(     self,    A=0,    **A      ):

									"""simple docstring"""


									lowerCamelCase					:			List[str]									= dict(self.forward_default_kwargs      )
									lowerCamelCase					:			str									= kwargs.pop('num_inference_steps',    A      )
									lowerCamelCase					:			Union[str, Any]									= self.dummy_sample
									lowerCamelCase					:			List[str]									= 0.1 * sample
									lowerCamelCase					:			List[str]									= [residual + 0.2, residual + 0.15, residual + 0.10]

									for scheduler_class in self.scheduler_classes:
													lowerCamelCase					:			Tuple									= self.get_scheduler_config()
													lowerCamelCase					:			Optional[Any]									= scheduler_class(**A      )
													scheduler.set_timesteps(A      )

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

													with tempfile.TemporaryDirectory() as tmpdirname:
																	scheduler.save_config(A      )
																	lowerCamelCase					:			Tuple									= 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)
																	lowerCamelCase					:			List[Any]									= dummy_past_residuals[: new_scheduler.config.solver_order]

													lowerCamelCase					:			int									= scheduler.step(A,    A,    A,    **A      ).prev_sample
													lowerCamelCase					:			Dict									= 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     UpperCAmelCase_							(     self,    A=None,    **A      ):

									"""simple docstring"""


									if scheduler is None:
													lowerCamelCase					:			Any									= self.scheduler_classes[0]
													lowerCamelCase					:			Optional[Any]									= self.get_scheduler_config(**A      )
													lowerCamelCase					:			Optional[int]									= scheduler_class(**A      )

									lowerCamelCase					:			List[Any]									= self.scheduler_classes[0]
									lowerCamelCase					:			Optional[Any]									= self.get_scheduler_config(**A      )
									lowerCamelCase					:			Optional[int]									= scheduler_class(**A      )

									lowerCamelCase					:			Any									= 10
									lowerCamelCase					:			Dict									= self.dummy_model()
									lowerCamelCase					:			Any									= self.dummy_sample_deter
									scheduler.set_timesteps(A      )

									for i, t in enumerate(scheduler.timesteps      ):
													lowerCamelCase					:			Dict									= model(A,    A      )
													lowerCamelCase					:			List[str]									= scheduler.step(A,    A,    A      ).prev_sample

									return sample
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Dict									= DPMSolverSinglestepScheduler(**self.get_scheduler_config()      )
									lowerCamelCase					:			Dict									= 50
									lowerCamelCase					:			Tuple									= self.dummy_model()
									lowerCamelCase					:			Optional[int]									= self.dummy_sample_deter
									scheduler.set_timesteps(A      )

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

									lowerCamelCase					:			Any									= torch.mean(torch.abs(A      )      )

									assert abs(result_mean.item() - 0.2574      ) < 1e-3
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


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

									"""simple docstring"""


									lowerCamelCase					:			Dict									= DPMSolverSinglestepScheduler(**self.get_scheduler_config()      )
									lowerCamelCase					:			str									= self.full_loop(scheduler=A      )
									lowerCamelCase					:			Optional[int]									= torch.mean(torch.abs(A      )      )

									assert abs(result_mean.item() - 0.2791      ) < 1e-3

									lowerCamelCase					:			Dict									= DEISMultistepScheduler.from_config(scheduler.config      )
									lowerCamelCase					:			Optional[int]									= DPMSolverMultistepScheduler.from_config(scheduler.config      )
									lowerCamelCase					:			Any									= UniPCMultistepScheduler.from_config(scheduler.config      )
									lowerCamelCase					:			Optional[Any]									= DPMSolverSinglestepScheduler.from_config(scheduler.config      )

									lowerCamelCase					:			str									= self.full_loop(scheduler=A      )
									lowerCamelCase					:			Optional[int]									= torch.mean(torch.abs(A      )      )

									assert abs(result_mean.item() - 0.2791      ) < 1e-3
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


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

									"""simple docstring"""


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

									"""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=A,    solver_type=A,    prediction_type=A,    algorithm_type=A,    )
																									lowerCamelCase					:			Optional[Any]									= self.full_loop(
																									    solver_order=A,    solver_type=A,    prediction_type=A,    algorithm_type=A,    )
																									assert not torch.isnan(A      ).any(), "Samples have nan numbers"
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									self.check_over_configs(lower_order_final=A      )
									self.check_over_configs(lower_order_final=A      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


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

									"""simple docstring"""


									self.check_over_configs(variance_type=A      )
									self.check_over_configs(variance_type='learned_range'      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									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     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Union[str, Any]									= self.full_loop()
									lowerCamelCase					:			str									= torch.mean(torch.abs(A      )      )

									assert abs(result_mean.item() - 0.2791      ) < 1e-3
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Union[str, Any]									= self.full_loop(use_karras_sigmas=A      )
									lowerCamelCase					:			Tuple									= torch.mean(torch.abs(A      )      )

									assert abs(result_mean.item() - 0.2248      ) < 1e-3
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			List[Any]									= self.full_loop(prediction_type='v_prediction'      )
									lowerCamelCase					:			Dict									= torch.mean(torch.abs(A      )      )

									assert abs(result_mean.item() - 0.1453      ) < 1e-3
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			List[Any]									= self.full_loop(prediction_type='v_prediction',    use_karras_sigmas=A      )
									lowerCamelCase					:			Optional[Any]									= torch.mean(torch.abs(A      )      )

									assert abs(result_mean.item() - 0.0649      ) < 1e-3




					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[Any]									= self.scheduler_classes[0]
									lowerCamelCase					:			Dict									= self.get_scheduler_config(thresholding=A,    dynamic_thresholding_ratio=0      )
									lowerCamelCase					:			str									= scheduler_class(**A      )

									lowerCamelCase					:			List[Any]									= 10
									lowerCamelCase					:			List[str]									= self.dummy_model()
									lowerCamelCase					:			int									= self.dummy_sample_deter.half()
									scheduler.set_timesteps(A      )

									for i, t in enumerate(scheduler.timesteps      ):
													lowerCamelCase					:			str									= model(A,    A      )
													lowerCamelCase					:			Tuple									= scheduler.step(A,    A,    A      ).prev_sample

									assert sample.dtype == torch.floataa
