






'''simple docstring'''


from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers import TFViTForImageClassification, TFViTModel


if is_vision_available():
					from PIL import Image

					from transformers import ViTImageProcessor
class    _SCREAMING_SNAKE_CASE							:


			'''simple docstring'''





			def __init__(   self							:						Optional[int] ,      lowercase							:						List[Any] ,      lowercase							:						Tuple=1_3 ,      lowercase							:						Optional[Any]=3_0 ,      lowercase							:						Optional[Any]=2 ,      lowercase							:						List[Any]=3 ,      lowercase							:						Optional[int]=True ,      lowercase							:						str=True ,      lowercase							:						Optional[Any]=3_2 ,      lowercase							:						Optional[int]=2 ,      lowercase							:						int=4 ,      lowercase							:						Optional[int]=3_7 ,      lowercase							:						Optional[int]="gelu" ,      lowercase							:						List[Any]=0.1 ,      lowercase							:						str=0.1 ,      lowercase							:						List[str]=1_0 ,      lowercase							:						Dict=0.0_2 ,      lowercase							:						int=3 ,      lowercase							:						Tuple=None ,      )							->    Tuple:

									'''simple docstring'''







									UpperCamelCase__         = parent
									UpperCamelCase__         = batch_size
									UpperCamelCase__         = image_size
									UpperCamelCase__         = patch_size
									UpperCamelCase__         = num_channels
									UpperCamelCase__         = is_training
									UpperCamelCase__         = use_labels
									UpperCamelCase__         = hidden_size
									UpperCamelCase__         = num_hidden_layers
									UpperCamelCase__         = num_attention_heads
									UpperCamelCase__         = intermediate_size
									UpperCamelCase__         = hidden_act
									UpperCamelCase__         = hidden_dropout_prob
									UpperCamelCase__         = attention_probs_dropout_prob
									UpperCamelCase__         = type_sequence_label_size
									UpperCamelCase__         = initializer_range
									UpperCamelCase__         = scope

									# in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
									UpperCamelCase__         = (image_size // patch_size) ** 2
									UpperCamelCase__         = num_patches + 1






			def 				A      (   self							:						Tuple			)							->    List[str]:

									'''simple docstring'''







									UpperCamelCase__         = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

									UpperCamelCase__         = None
									if self.use_labels:
															UpperCamelCase__         = ids_tensor([self.batch_size] ,      self.type_sequence_label_size			)

									UpperCamelCase__         = self.get_config()

									return config, pixel_values, labels






			def 				A      (   self							:						str			)							->    int:

									'''simple docstring'''







									return ViTConfig(
									    image_size=self.image_size ,      patch_size=self.patch_size ,      num_channels=self.num_channels ,      hidden_size=self.hidden_size ,      num_hidden_layers=self.num_hidden_layers ,      num_attention_heads=self.num_attention_heads ,      intermediate_size=self.intermediate_size ,      hidden_act=self.hidden_act ,      hidden_dropout_prob=self.hidden_dropout_prob ,      attention_probs_dropout_prob=self.attention_probs_dropout_prob ,      is_decoder=lowercase ,      initializer_range=self.initializer_range ,      )






			def 				A      (   self							:						List[Any] ,      lowercase							:						int ,      lowercase							:						Union[str, Any] ,      lowercase							:						Optional[Any]			)							->    Union[str, Any]:

									'''simple docstring'''







									UpperCamelCase__         = TFViTModel(config=lowercase			)
									UpperCamelCase__         = model(lowercase ,      training=lowercase			)
									self.parent.assertEqual(result.last_hidden_state.shape ,      (self.batch_size, self.seq_length, self.hidden_size)			)

									# Test with an image with different size than the one specified in config.
									UpperCamelCase__         = self.image_size // 2
									UpperCamelCase__         = pixel_values[:, :, :image_size, :image_size]
									UpperCamelCase__         = model(lowercase ,      interpolate_pos_encoding=lowercase ,      training=lowercase			)
									UpperCamelCase__         = (image_size // self.patch_size) ** 2 + 1
									self.parent.assertEqual(result.last_hidden_state.shape ,      (self.batch_size, seq_length, self.hidden_size)			)






			def 				A      (   self							:						int ,      lowercase							:						Optional[int] ,      lowercase							:						Union[str, Any] ,      lowercase							:						Optional[int]			)							->    str:

									'''simple docstring'''







									UpperCamelCase__         = self.type_sequence_label_size
									UpperCamelCase__         = TFViTForImageClassification(lowercase			)
									UpperCamelCase__         = model(lowercase ,      labels=lowercase ,      training=lowercase			)
									self.parent.assertEqual(result.logits.shape ,      (self.batch_size, self.type_sequence_label_size)			)

									# Test with an image with different size than the one specified in config.
									UpperCamelCase__         = self.image_size // 2
									UpperCamelCase__         = pixel_values[:, :, :image_size, :image_size]
									UpperCamelCase__         = model(lowercase ,      interpolate_pos_encoding=lowercase ,      training=lowercase			)
									self.parent.assertEqual(result.logits.shape ,      (self.batch_size, self.type_sequence_label_size)			)

									# test greyscale images
									UpperCamelCase__         = 1
									UpperCamelCase__         = TFViTForImageClassification(lowercase			)
									UpperCamelCase__         = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
									UpperCamelCase__         = model(lowercase			)
									self.parent.assertEqual(result.logits.shape ,      (self.batch_size, self.type_sequence_label_size)			)







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

									'''simple docstring'''







									UpperCamelCase__         = self.prepare_config_and_inputs()
									UpperCamelCase__         = config_and_inputs
									UpperCamelCase__         = {'pixel_values': pixel_values}
									return config, inputs_dict
@require_tf
class    _SCREAMING_SNAKE_CASE							(		__A       ,__A       ,unittest.TestCase	):


			'''simple docstring'''





			__a							:							str					     =					(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
			__a							:							List[Any]					     =					(
			    {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
			    if is_tf_available()
			    else {}
			)

			__a							:							Any					     =					False
			__a							:							int					     =					False
			__a							:							Dict					     =					False






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

									'''simple docstring'''







									UpperCamelCase__         = TFViTModelTester(self			)
									UpperCamelCase__         = ConfigTester(self ,      config_class=lowercase ,      has_text_modality=lowercase ,      hidden_size=3_7			)






			def 				A      (   self							:						Dict			)							->    List[str]:

									'''simple docstring'''







									self.config_tester.run_common_tests()






			@unittest.skip(reason="""ViT does not use inputs_embeds"""			)
			def 				A      (   self							:						Dict			)							->    str:

									'''simple docstring'''







									pass






			@unittest.skip(reason="""ViT does not use inputs_embeds"""			)
			def 				A      (   self							:						Tuple			)							->    Dict:

									'''simple docstring'''







									pass






			def 				A      (   self							:						Tuple			)							->    Optional[Any]:

									'''simple docstring'''







									UpperCamelCase__         = self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															UpperCamelCase__         = model_class(lowercase			)
															self.assertIsInstance(model.get_input_embeddings() ,      (tf.keras.layers.Layer)			)
															UpperCamelCase__         = model.get_output_embeddings()
															self.assertTrue(x is None or isinstance(lowercase ,      tf.keras.layers.Layer			)			)






			def 				A      (   self							:						List[str]			)							->    Optional[Any]:

									'''simple docstring'''







									UpperCamelCase__         = self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															UpperCamelCase__         = model_class(lowercase			)
															UpperCamelCase__         = inspect.signature(model.call			)
															# signature.parameters is an OrderedDict => so arg_names order is deterministic
															UpperCamelCase__         = [*signature.parameters.keys()]

															UpperCamelCase__         = ['pixel_values']
															self.assertListEqual(arg_names[:1] ,      lowercase			)






			def 				A      (   self							:						Optional[int]			)							->    str:

									'''simple docstring'''







									UpperCamelCase__         = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowercase			)






			def 				A      (   self							:						List[Any]			)							->    str:

									'''simple docstring'''







									UpperCamelCase__         = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*lowercase			)







			@slow
			def 				A      (   self							:						Union[str, Any]			)							->    str:

									'''simple docstring'''







									UpperCamelCase__         = TFViTModel.from_pretrained("""google/vit-base-patch16-224"""			)
									self.assertIsNotNone(lowercase			)







def 	__magic_name__( ):
						'''simple docstring'''





						UpperCamelCase__         = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
						return image







@require_tf
@require_vision
class    _SCREAMING_SNAKE_CASE							(		unittest.TestCase	):


			'''simple docstring'''





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

									'''simple docstring'''







									return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224"""			) if is_vision_available() else None







			@slow
			def 				A      (   self							:						List[str]			)							->    int:

									'''simple docstring'''







									UpperCamelCase__         = TFViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""			)

									UpperCamelCase__         = self.default_image_processor
									UpperCamelCase__         = prepare_img()
									UpperCamelCase__         = image_processor(images=lowercase ,      return_tensors="""tf"""			)

									# forward pass
									UpperCamelCase__         = model(**lowercase			)

									# verify the logits
									UpperCamelCase__         = tf.TensorShape((1, 1_0_0_0)			)
									self.assertEqual(outputs.logits.shape ,      lowercase			)

									UpperCamelCase__         = tf.constant([-0.2_7_4_4, 0.8_2_1_5, -0.0_8_3_6]			)

									tf.debugging.assert_near(outputs.logits[0, :3] ,      lowercase ,      atol=1e-4			)












'''simple docstring'''


import tempfile

import torch

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

from .test_schedulers import SchedulerCommonTest
class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





			__a							:							Dict					     =					(DPMSolverSinglestepScheduler,)
			__a							:							List[Any]					     =					(("num_inference_steps", 25),)






			def 				A      (   self							:						List[Any] ,      **lowercase							:						Any			)							->    Optional[int]:

									'''simple docstring'''







									UpperCamelCase__         = {
									    """num_train_timesteps""": 1_0_0_0,
									    """beta_start""": 0.0_0_0_1,
									    """beta_end""": 0.0_2,
									    """beta_schedule""": """linear""",
									    """solver_order""": 2,
									    """prediction_type""": """epsilon""",
									    """thresholding""": False,
									    """sample_max_value""": 1.0,
									    """algorithm_type""": """dpmsolver++""",
									    """solver_type""": """midpoint""",
									    """lambda_min_clipped""": -float("""inf"""			),
									    """variance_type""": None,
									}

									config.update(**lowercase			)
									return config






			def 				A      (   self							:						List[Any] ,      lowercase							:						Tuple=0 ,      **lowercase							:						Optional[Any]			)							->    Any:

									'''simple docstring'''







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

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

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

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

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






			def 				A      (   self							:						List[str]			)							->    Any:

									'''simple docstring'''







									pass






			def 				A      (   self							:						Any ,      lowercase							:						List[Any]=0 ,      **lowercase							:						Optional[int]			)							->    Optional[int]:

									'''simple docstring'''







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

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

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

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

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

															UpperCamelCase__         = scheduler.step(lowercase ,      lowercase ,      lowercase ,      **lowercase			).prev_sample
															UpperCamelCase__         = new_scheduler.step(lowercase ,      lowercase ,      lowercase ,      **lowercase			).prev_sample

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






			def 				A      (   self							:						Union[str, Any] ,      lowercase							:						Any=None ,      **lowercase							:						List[Any]			)							->    Dict:

									'''simple docstring'''







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

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

									UpperCamelCase__         = 1_0
									UpperCamelCase__         = self.dummy_model()
									UpperCamelCase__         = self.dummy_sample_deter
									scheduler.set_timesteps(lowercase			)

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

									return sample






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

									'''simple docstring'''







									UpperCamelCase__         = DPMSolverSinglestepScheduler(**self.get_scheduler_config()			)
									UpperCamelCase__         = 5_0
									UpperCamelCase__         = self.dummy_model()
									UpperCamelCase__         = self.dummy_sample_deter
									scheduler.set_timesteps(lowercase			)

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

									UpperCamelCase__         = torch.mean(torch.abs(lowercase			)			)

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






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

									'''simple docstring'''







									for timesteps in [2_5, 5_0, 1_0_0, 9_9_9, 1_0_0_0]:
															self.check_over_configs(num_train_timesteps=lowercase			)






			def 				A      (   self							:						Optional[int]			)							->    List[Any]:

									'''simple docstring'''







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

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

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

									UpperCamelCase__         = self.full_loop(scheduler=lowercase			)
									UpperCamelCase__         = torch.mean(torch.abs(lowercase			)			)

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






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

									'''simple docstring'''







									self.check_over_configs(thresholding=lowercase			)
									for order in [1, 2, 3]:
															for solver_type in ["midpoint", "heun"]:
																					for threshold in [0.5, 1.0, 2.0]:
																											for prediction_type in ["epsilon", "sample"]:
																																	self.check_over_configs(
																																	    thresholding=lowercase ,      prediction_type=lowercase ,      sample_max_value=lowercase ,      algorithm_type="""dpmsolver++""" ,      solver_order=lowercase ,      solver_type=lowercase ,      )






			def 				A      (   self							:						Optional[int]			)							->    Tuple:

									'''simple docstring'''







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






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

									'''simple docstring'''







									for algorithm_type in ["dpmsolver", "dpmsolver++"]:
															for solver_type in ["midpoint", "heun"]:
																					for order in [1, 2, 3]:
																											for prediction_type in ["epsilon", "sample"]:
																																	self.check_over_configs(
																																	    solver_order=lowercase ,      solver_type=lowercase ,      prediction_type=lowercase ,      algorithm_type=lowercase ,      )
																																	UpperCamelCase__         = self.full_loop(
																																	    solver_order=lowercase ,      solver_type=lowercase ,      prediction_type=lowercase ,      algorithm_type=lowercase ,      )
																																	assert not torch.isnan(lowercase			).any(), "Samples have nan numbers"






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

									'''simple docstring'''







									self.check_over_configs(lower_order_final=lowercase			)
									self.check_over_configs(lower_order_final=lowercase			)






			def 				A      (   self							:						Tuple			)							->    Optional[int]:

									'''simple docstring'''







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






			def 				A      (   self							:						List[Any]			)							->    int:

									'''simple docstring'''







									self.check_over_configs(variance_type=lowercase			)
									self.check_over_configs(variance_type="""learned_range"""			)






			def 				A      (   self							:						Tuple			)							->    int:

									'''simple docstring'''







									for num_inference_steps in [1, 2, 3, 5, 1_0, 5_0, 1_0_0, 9_9_9, 1_0_0_0]:
															self.check_over_forward(num_inference_steps=lowercase ,      time_step=0			)






			def 				A      (   self							:						List[str]			)							->    Optional[int]:

									'''simple docstring'''







									UpperCamelCase__         = self.full_loop()
									UpperCamelCase__         = torch.mean(torch.abs(lowercase			)			)

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






			def 				A      (   self							:						str			)							->    int:

									'''simple docstring'''







									UpperCamelCase__         = self.full_loop(use_karras_sigmas=lowercase			)
									UpperCamelCase__         = torch.mean(torch.abs(lowercase			)			)

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






			def 				A      (   self							:						List[Any]			)							->    List[str]:

									'''simple docstring'''







									UpperCamelCase__         = self.full_loop(prediction_type="""v_prediction"""			)
									UpperCamelCase__         = torch.mean(torch.abs(lowercase			)			)

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






			def 				A      (   self							:						int			)							->    int:

									'''simple docstring'''







									UpperCamelCase__         = self.full_loop(prediction_type="""v_prediction""" ,      use_karras_sigmas=lowercase			)
									UpperCamelCase__         = torch.mean(torch.abs(lowercase			)			)

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







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

									'''simple docstring'''







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

									UpperCamelCase__         = 1_0
									UpperCamelCase__         = self.dummy_model()
									UpperCamelCase__         = self.dummy_sample_deter.half()
									scheduler.set_timesteps(lowercase			)

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

									assert sample.dtype == torch.floataa
