





import inspect
import unittest
import warnings
from math import ceil, floor

from transformers import LevitConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.models.auto import get_values
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 (
						    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
						    MODEL_MAPPING,
						    LevitForImageClassification,
						    LevitForImageClassificationWithTeacher,
						    LevitModel,
						)
						from transformers.models.levit.modeling_levit import LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import LevitImageProcessor

class    _UpperCAmelCase							(		_lowerCamelCase     ):
						def 		_lowerCamelCase      (		self       ):
									A_     :				Any			      =	self.config_class(**self.inputs_dict       )
									self.parent.assertTrue(hasattr(lowercase_      ,       """hidden_sizes"""       )       )
									self.parent.assertTrue(hasattr(lowercase_      ,       """num_attention_heads"""       )       )






class    _UpperCAmelCase							:
						def __init__(		self      ,       a__      ,       a__=13      ,       a__=64      ,       a__=3      ,       a__=3      ,       a__=2      ,       a__=1      ,       a__=16      ,       a__=[128, 256, 384]      ,       a__=[4, 6, 8]      ,       a__=[2, 3, 4]      ,       a__=[16, 16, 16]      ,       a__=0      ,       a__=[2, 2, 2]      ,       a__=[2, 2, 2]      ,       a__=0.02      ,       a__=True      ,       a__=True      ,       a__=2      ,       ):
									A_     :				Tuple			      =	parent
									A_     :				Union[str, Any]			      =	batch_size
									A_     :				Optional[Any]			      =	image_size
									A_     :				List[Any]			      =	num_channels
									A_     :				Optional[int]			      =	kernel_size
									A_     :				Dict			      =	stride
									A_     :				Optional[int]			      =	padding
									A_     :				Tuple			      =	hidden_sizes
									A_     :				Optional[int]			      =	num_attention_heads
									A_     :				Optional[Any]			      =	depths
									A_     :				Optional[int]			      =	key_dim
									A_     :				Union[str, Any]			      =	drop_path_rate
									A_     :				Dict			      =	patch_size
									A_     :				int			      =	attention_ratio
									A_     :				Optional[int]			      =	mlp_ratio
									A_     :				List[str]			      =	initializer_range
									A_     :				Optional[int]			      =	[
									    ["""Subsample""", key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2],
									    ["""Subsample""", key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2],
									]
									A_     :				List[str]			      =	is_training
									A_     :				List[Any]			      =	use_labels
									A_     :				Optional[int]			      =	num_labels
									A_     :				List[Any]			      =	initializer_range
						def 		_lowerCamelCase      (		self       ):
									A_     :				List[str]			      =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

									A_     :				Union[str, Any]			      =	None
									if self.use_labels:
												A_     :				str			      =	ids_tensor([self.batch_size]      ,       self.num_labels       )

									A_     :				List[Any]			      =	self.get_config()
									return config, pixel_values, labels
						def 		_lowerCamelCase      (		self       ):
									return LevitConfig(
									    image_size=self.image_size      ,       num_channels=self.num_channels      ,       kernel_size=self.kernel_size      ,       stride=self.stride      ,       padding=self.padding      ,       patch_size=self.patch_size      ,       hidden_sizes=self.hidden_sizes      ,       num_attention_heads=self.num_attention_heads      ,       depths=self.depths      ,       key_dim=self.key_dim      ,       drop_path_rate=self.drop_path_rate      ,       mlp_ratio=self.mlp_ratio      ,       attention_ratio=self.attention_ratio      ,       initializer_range=self.initializer_range      ,       down_ops=self.down_ops      ,       )
						def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__       ):
									A_     :				List[Any]			      =	LevitModel(config=lowercase_       )
									model.to(lowercase_       )
									model.eval()
									A_     :				Optional[Any]			      =	model(lowercase_       )
									A_     :				int			      =	(self.image_size, self.image_size)
									A_      ,				A_     :				Union[str, Any]			      =	image_size[0], image_size[1]
									for _ in range(4       ):
												A_     :				List[Any]			      =	floor(((height + 2 * self.padding - self.kernel_size) / self.stride) + 1       )
												A_     :				Any			      =	floor(((width + 2 * self.padding - self.kernel_size) / self.stride) + 1       )
									self.parent.assertEqual(
									    result.last_hidden_state.shape      ,       (self.batch_size, ceil(height / 4       ) * ceil(width / 4       ), self.hidden_sizes[-1])      ,       )
						def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__       ):
									A_     :				Optional[Any]			      =	self.num_labels
									A_     :				Any			      =	LevitForImageClassification(lowercase_       )
									model.to(lowercase_       )
									model.eval()
									A_     :				Any			      =	model(lowercase_      ,       labels=lowercase_       )
									self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.num_labels)       )






						def 		_lowerCamelCase      (		self       ):
									A_     :				int			      =	self.prepare_config_and_inputs()
									A_      ,				A_      ,				A_     :				Tuple			      =	config_and_inputs
									A_     :				List[str]			      =	{"""pixel_values""": pixel_values}
									return config, inputs_dict






@require_torch
class    _UpperCAmelCase							(		_lowerCamelCase			,       _lowerCamelCase			,       unittest.TestCase     ):
						a						       =							(
						    (LevitModel, LevitForImageClassification, LevitForImageClassificationWithTeacher)
						    if is_torch_available()
						    else ()
						)
						a						       =							(
						    {
						        '''feature-extraction''': LevitModel,
						        '''image-classification''': (LevitForImageClassification, LevitForImageClassificationWithTeacher),
						    }
						    if is_torch_available()
						    else {}
						)

						a						       =							False
						a						       =							False
						a						       =							False
						a						       =							False
						a						       =							False
						def 		_lowerCamelCase      (		self       ):
									A_     :				Tuple			      =	LevitModelTester(self       )
									A_     :				Optional[Any]			      =	ConfigTester(self      ,       config_class=lowercase_      ,       has_text_modality=lowercase_      ,       hidden_size=37       )
						def 		_lowerCamelCase      (		self       ):
									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 		_lowerCamelCase      (		self       ):
									return
						@unittest.skip(reason="""Levit does not use inputs_embeds"""       )
						def 		_lowerCamelCase      (		self       ):
									pass
						@unittest.skip(reason="""Levit does not support input and output embeddings"""       )
						def 		_lowerCamelCase      (		self       ):
									pass
						@unittest.skip(reason="""Levit does not output attentions"""       )
						def 		_lowerCamelCase      (		self       ):
									pass
						def 		_lowerCamelCase      (		self       ):
									A_      ,				A_     :				Tuple			      =	self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
												A_     :				Union[str, Any]			      =	model_class(lowercase_       )
												A_     :				List[Any]			      =	inspect.signature(model.forward       )
												# signature.parameters is an OrderedDict => so arg_names order is deterministic
												A_     :				int			      =	[*signature.parameters.keys()]

												A_     :				Dict			      =	["""pixel_values"""]
												self.assertListEqual(arg_names[:1]      ,       lowercase_       )
						def 		_lowerCamelCase      (		self       ):







									def check_hidden_states_output(a__      ,       a__      ,       a__       ):
												A_     :				str			      =	model_class(lowercase_       )
												model.to(lowercase_       )
												model.eval()

												with torch.no_grad():
															A_     :				Tuple			      =	model(**self._prepare_for_class(lowercase_      ,       lowercase_       )       )

												A_     :				Optional[Any]			      =	outputs.hidden_states

												A_     :				int			      =	len(self.model_tester.depths       ) + 1
												self.assertEqual(len(lowercase_       )      ,       lowercase_       )

												A_     :				List[str]			      =	(self.model_tester.image_size, self.model_tester.image_size)
												A_      ,				A_     :				List[Any]			      =	image_size[0], image_size[1]
												for _ in range(4       ):
															A_     :				List[Any]			      =	floor(
															    (
															        (height + 2 * self.model_tester.padding - self.model_tester.kernel_size)
															        / self.model_tester.stride
															    )
															    + 1       )
															A_     :				List[str]			      =	floor(
															    (
															        (width + 2 * self.model_tester.padding - self.model_tester.kernel_size)
															        / self.model_tester.stride
															    )
															    + 1       )
												# verify the first hidden states (first block)
												self.assertListEqual(
												    list(hidden_states[0].shape[-2:]       )      ,       [
												        height * width,
												        self.model_tester.hidden_sizes[0],
												    ]      ,       )

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

									for model_class in self.all_model_classes:
												A_     :				List[Any]			      =	True
												check_hidden_states_output(lowercase_      ,       lowercase_      ,       lowercase_       )

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

												check_hidden_states_output(lowercase_      ,       lowercase_      ,       lowercase_       )
						@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""       )
						def 		_lowerCamelCase      (		self       ):
									pass
						def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__=False       ):
									A_     :				Tuple			      =	super()._prepare_for_class(lowercase_      ,       lowercase_      ,       return_labels=lowercase_       )

									if return_labels:
												if model_class.__name__ == "LevitForImageClassificationWithTeacher":
															del inputs_dict["labels"]

									return inputs_dict
						def 		_lowerCamelCase      (		self       ):
									A_     :				Tuple			      =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowercase_       )
						def 		_lowerCamelCase      (		self       ):
									A_     :				int			      =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*lowercase_       )
						def 		_lowerCamelCase      (		self       ):







									if not self.model_tester.is_training:
												return

									A_      ,				A_     :				int			      =	self.model_tester.prepare_config_and_inputs_for_common()
									A_     :				Union[str, Any]			      =	True

									for model_class in self.all_model_classes:
												# LevitForImageClassificationWithTeacher supports inference-only
												if (
												    model_class in get_values(lowercase_       )
												    or model_class.__name__ == "LevitForImageClassificationWithTeacher"
												):
															continue
												A_     :				Optional[int]			      =	model_class(lowercase_       )
												model.to(lowercase_       )
												model.train()
												A_     :				Dict			      =	self._prepare_for_class(lowercase_      ,       lowercase_      ,       return_labels=lowercase_       )
												A_     :				List[str]			      =	model(**lowercase_       ).loss
												loss.backward()
						def 		_lowerCamelCase      (		self       ):
									A_      ,				A_     :				Optional[int]			      =	self.model_tester.prepare_config_and_inputs_for_common()
									if not self.model_tester.is_training:
												return

									A_     :				int			      =	False
									A_     :				Optional[Any]			      =	True

									for model_class in self.all_model_classes:
												if model_class in get_values(lowercase_       ) or not model_class.supports_gradient_checkpointing:
															continue
												# LevitForImageClassificationWithTeacher supports inference-only
												if model_class.__name__ == "LevitForImageClassificationWithTeacher":
															continue
												A_     :				List[str]			      =	model_class(lowercase_       )
												model.gradient_checkpointing_enable()
												model.to(lowercase_       )
												model.train()
												A_     :				Any			      =	self._prepare_for_class(lowercase_      ,       lowercase_      ,       return_labels=lowercase_       )
												A_     :				Union[str, Any]			      =	model(**lowercase_       ).loss
												loss.backward()
						def 		_lowerCamelCase      (		self       ):
									A_      ,				A_     :				Optional[int]			      =	self.model_tester.prepare_config_and_inputs_for_common()

									A_     :				str			      =	[
									    {"""title""": """multi_label_classification""", """num_labels""": 2, """dtype""": torch.float},
									    {"""title""": """single_label_classification""", """num_labels""": 1, """dtype""": torch.long},
									    {"""title""": """regression""", """num_labels""": 1, """dtype""": torch.float},
									]

									for model_class in self.all_model_classes:
												if (
												    model_class
												    not in [
												        *get_values(lowercase_       ),
												    ]
												    or model_class.__name__ == "LevitForImageClassificationWithTeacher"
												):
															continue

												for problem_type in problem_types:
															with self.subTest(msg=F"""Testing {model_class} with {problem_type['title']}"""       ):
																		A_     :				Tuple			      =	problem_type["""title"""]
																		A_     :				str			      =	problem_type["""num_labels"""]

																		A_     :				Tuple			      =	model_class(lowercase_       )
																		model.to(lowercase_       )
																		model.train()

																		A_     :				Optional[Any]			      =	self._prepare_for_class(lowercase_      ,       lowercase_      ,       return_labels=lowercase_       )

																		if problem_type["num_labels"] > 1:
																					A_     :				Tuple			      =	inputs["""labels"""].unsqueeze(1       ).repeat(1      ,       problem_type["""num_labels"""]       )

																		A_     :				str			      =	inputs["""labels"""].to(problem_type["""dtype"""]       )

																		# This tests that we do not trigger the warning form PyTorch "Using a target size that is different
																		# to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
																		# they have the same size." which is a symptom something in wrong for the regression problem.
																		# See https://github.com/huggingface/transformers/issues/11780
																		with warnings.catch_warnings(record=lowercase_       ) as warning_list:
																					A_     :				List[str]			      =	model(**lowercase_       ).loss
																		for w in warning_list:
																					if "Using a target size that is different to the input size" in str(w.message       ):
																								raise ValueError(
																								    F"""Something is going wrong in the regression problem: intercepted {w.message}"""       )

																		loss.backward()






						@slow
						def 		_lowerCamelCase      (		self       ):







									for model_name in LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												A_     :				List[str]			      =	LevitModel.from_pretrained(lowercase_       )
												self.assertIsNotNone(lowercase_       )






def     _lowerCAmelCase		(  ):
			'''simple docstring'''


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







@require_torch
@require_vision
class    _UpperCAmelCase							(		unittest.TestCase     ):
						@cached_property
						def 		_lowerCamelCase      (		self       ):
									return LevitImageProcessor.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]       )






						@slow
						def 		_lowerCamelCase      (		self       ):
									A_     :				Union[str, Any]			      =	LevitForImageClassificationWithTeacher.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]       ).to(
									    lowercase_       )

									A_     :				List[Any]			      =	self.default_image_processor
									A_     :				Union[str, Any]			      =	prepare_img()
									A_     :				Union[str, Any]			      =	image_processor(images=lowercase_      ,       return_tensors="""pt"""       ).to(lowercase_       )

									# forward pass
									with torch.no_grad():
												A_     :				int			      =	model(**lowercase_       )

									# verify the logits
									A_     :				Union[str, Any]			      =	torch.Size((1, 1000)       )
									self.assertEqual(outputs.logits.shape      ,       lowercase_       )

									A_     :				int			      =	torch.tensor([1.0448, -0.3745, -1.8317]       ).to(lowercase_       )

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













from . import __version__

# Backward compatibility imports, to make sure all those objects can be found in file_utils
from .utils import (
    CLOUDFRONT_DISTRIB_PREFIX,
    CONFIG_NAME,
    DISABLE_TELEMETRY,
    DUMMY_INPUTS,
    DUMMY_MASK,
    ENV_VARS_TRUE_AND_AUTO_VALUES,
    ENV_VARS_TRUE_VALUES,
    FEATURE_EXTRACTOR_NAME,
    FLAX_WEIGHTS_NAME,
    HF_MODULES_CACHE,
    HUGGINGFACE_CO_PREFIX,
    HUGGINGFACE_CO_RESOLVE_ENDPOINT,
    MODEL_CARD_NAME,
    MULTIPLE_CHOICE_DUMMY_INPUTS,
    PYTORCH_PRETRAINED_BERT_CACHE,
    PYTORCH_TRANSFORMERS_CACHE,
    S3_BUCKET_PREFIX,
    SENTENCEPIECE_UNDERLINE,
    SPIECE_UNDERLINE,
    TF2_WEIGHTS_NAME,
    TF_WEIGHTS_NAME,
    TORCH_FX_REQUIRED_VERSION,
    TRANSFORMERS_CACHE,
    TRANSFORMERS_DYNAMIC_MODULE_NAME,
    USE_JAX,
    USE_TF,
    USE_TORCH,
    WEIGHTS_INDEX_NAME,
    WEIGHTS_NAME,
    ContextManagers,
    DummyObject,
    EntryNotFoundError,
    ExplicitEnum,
    ModelOutput,
    PaddingStrategy,
    PushToHubMixin,
    RepositoryNotFoundError,
    RevisionNotFoundError,
    TensorType,
    _LazyModule,
    add_code_sample_docstrings,
    add_end_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    cached_property,
    copy_func,
    default_cache_path,
    define_sagemaker_information,
    get_cached_models,
    get_file_from_repo,
    get_full_repo_name,
    get_torch_version,
    has_file,
    http_user_agent,
    is_apex_available,
    is_bsa_available,
    is_coloredlogs_available,
    is_datasets_available,
    is_detectrona_available,
    is_faiss_available,
    is_flax_available,
    is_ftfy_available,
    is_in_notebook,
    is_ipex_available,
    is_librosa_available,
    is_offline_mode,
    is_onnx_available,
    is_pandas_available,
    is_phonemizer_available,
    is_protobuf_available,
    is_psutil_available,
    is_pyanvml_available,
    is_pyctcdecode_available,
    is_pytesseract_available,
    is_pytorch_quantization_available,
    is_rjieba_available,
    is_sagemaker_dp_enabled,
    is_sagemaker_mp_enabled,
    is_scipy_available,
    is_sentencepiece_available,
    is_seqio_available,
    is_sklearn_available,
    is_soundfile_availble,
    is_spacy_available,
    is_speech_available,
    is_tensor,
    is_tensorflow_probability_available,
    is_tfaonnx_available,
    is_tf_available,
    is_timm_available,
    is_tokenizers_available,
    is_torch_available,
    is_torch_bfaa_available,
    is_torch_cuda_available,
    is_torch_fx_available,
    is_torch_fx_proxy,
    is_torch_mps_available,
    is_torch_tfaa_available,
    is_torch_tpu_available,
    is_torchaudio_available,
    is_training_run_on_sagemaker,
    is_vision_available,
    replace_return_docstrings,
    requires_backends,
    to_numpy,
    to_py_obj,
    torch_only_method,
)






