


'''simple docstring'''






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						(    _UpperCamelCase				):





							def 		a__ (	self		:Optional[Any]      ):
									snake_case_		:				List[str]      							=    self.config_class(**self.inputs_dict      )
									self.parent.assertTrue(hasattr(__a	,"""hidden_sizes"""      )      )
									self.parent.assertTrue(hasattr(__a	,"""num_attention_heads"""      )      )





class     __UpperCamelCase						:





							def __init__(	self		:Dict	,_UpperCamelCase		:Any	,_UpperCamelCase		:Union[str, Any]=1_3	,_UpperCamelCase		:Dict=6_4	,_UpperCamelCase		:List[str]=3	,_UpperCamelCase		:Any=3	,_UpperCamelCase		:Optional[Any]=2	,_UpperCamelCase		:Optional[Any]=1	,_UpperCamelCase		:Any=1_6	,_UpperCamelCase		:Dict=[1_2_8, 2_5_6, 3_8_4]	,_UpperCamelCase		:str=[4, 6, 8]	,_UpperCamelCase		:List[str]=[2, 3, 4]	,_UpperCamelCase		:Tuple=[1_6, 1_6, 1_6]	,_UpperCamelCase		:str=0	,_UpperCamelCase		:Any=[2, 2, 2]	,_UpperCamelCase		:str=[2, 2, 2]	,_UpperCamelCase		:int=0.02	,_UpperCamelCase		:Any=True	,_UpperCamelCase		:List[str]=True	,_UpperCamelCase		:Union[str, Any]=2	,):
									snake_case_		:				Dict      							=    parent
									snake_case_		:				str      							=    batch_size
									snake_case_		:				Any      							=    image_size
									snake_case_		:				Dict      							=    num_channels
									snake_case_		:				List[Any]      							=    kernel_size
									snake_case_		:				Tuple      							=    stride
									snake_case_		:				List[str]      							=    padding
									snake_case_		:				List[str]      							=    hidden_sizes
									snake_case_		:				List[str]      							=    num_attention_heads
									snake_case_		:				List[Any]      							=    depths
									snake_case_		:				Optional[int]      							=    key_dim
									snake_case_		:				Optional[int]      							=    drop_path_rate
									snake_case_		:				List[Any]      							=    patch_size
									snake_case_		:				Any      							=    attention_ratio
									snake_case_		:				Union[str, Any]      							=    mlp_ratio
									snake_case_		:				str      							=    initializer_range
									snake_case_		:				List[str]      							=    [
									    ["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],
									]
									snake_case_		:				Optional[Any]      							=    is_training
									snake_case_		:				Dict      							=    use_labels
									snake_case_		:				List[Any]      							=    num_labels
									snake_case_		:				Tuple      							=    initializer_range





							def 		a__ (	self		:Tuple      ):
									snake_case_		:				Dict      							=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

									snake_case_		:				Optional[int]      							=    None
									if self.use_labels:
											snake_case_		:				Tuple      							=    ids_tensor([self.batch_size]	,self.num_labels      )

									snake_case_		:				Union[str, Any]      							=    self.get_config()
									return config, pixel_values, labels





							def 		a__ (	self		:str      ):
									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 		a__ (	self		:Any	,_UpperCamelCase		:Any	,_UpperCamelCase		:Optional[Any]	,_UpperCamelCase		:str      ):
									snake_case_		:				Optional[int]      							=    LevitModel(config=__a      )
									model.to(__a      )
									model.eval()
									snake_case_		:				Optional[Any]      							=    model(__a      )
									snake_case_		:				List[str]      							=    (self.image_size, self.image_size)
									snake_case_		:				Optional[Any]      							=    image_size[0], image_size[1]
									for _ in range(4      ):
											snake_case_		:				Dict      							=    floor(((height + 2 * self.padding - self.kernel_size) / self.stride) + 1      )
											snake_case_		:				Optional[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 		a__ (	self		:Tuple	,_UpperCamelCase		:str	,_UpperCamelCase		:Optional[Any]	,_UpperCamelCase		:Dict      ):
									snake_case_		:				Union[str, Any]      							=    self.num_labels
									snake_case_		:				str      							=    LevitForImageClassification(__a      )
									model.to(__a      )
									model.eval()
									snake_case_		:				Any      							=    model(__a	,labels=__a      )
									self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.num_labels)      )





							def 		a__ (	self		:Union[str, Any]      ):
									snake_case_		:				Union[str, Any]      							=    self.prepare_config_and_inputs()
									snake_case_		:				List[str]      							=    config_and_inputs
									snake_case_		:				List[Any]      							=    {"pixel_values": pixel_values}
									return config, inputs_dict





@require_torch
class     __UpperCamelCase						(    _UpperCamelCase    ,	_UpperCamelCase    ,	unittest.TestCase				):
							lowercase      :		Any 		=	(
							    (LevitModel, LevitForImageClassification, LevitForImageClassificationWithTeacher)
							    if is_torch_available()
							    else ()
							)
							lowercase      :		List[str] 		=	(
							    {
							        "feature-extraction": LevitModel,
							        "image-classification": (LevitForImageClassification, LevitForImageClassificationWithTeacher),
							    }
							    if is_torch_available()
							    else {}
							)

							lowercase      :		Union[str, Any] 		=	False
							lowercase      :		str 		=	False
							lowercase      :		List[Any] 		=	False
							lowercase      :		str 		=	False
							lowercase      :		int 		=	False





							def 		a__ (	self		:List[Any]      ):
									snake_case_		:				Optional[Any]      							=    LevitModelTester(self      )
									snake_case_		:				Any      							=    ConfigTester(self	,config_class=__a	,has_text_modality=__a	,hidden_size=3_7      )





							def 		a__ (	self		:List[str]      ):
									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 		a__ (	self		:List[Any]      ):
									return





							@unittest.skip(reason="""Levit does not use inputs_embeds"""      )
							def 		a__ (	self		:int      ):
									pass





							@unittest.skip(reason="""Levit does not support input and output embeddings"""      )
							def 		a__ (	self		:int      ):
									pass





							@unittest.skip(reason="""Levit does not output attentions"""      )
							def 		a__ (	self		:Any      ):
									pass





							def 		a__ (	self		:Optional[int]      ):
									snake_case_		:				Any      							=    self.model_tester.prepare_config_and_inputs_for_common()

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

											snake_case_		:				str      							=    ["pixel_values"]
											self.assertListEqual(arg_names[:1]	,__a      )





							def 		a__ (	self		:Dict      ):
									def check_hidden_states_output(_UpperCamelCase		:int	,_UpperCamelCase		:Any	,_UpperCamelCase		:List[str]      ):
											snake_case_		:				Union[str, Any]      							=    model_class(__a      )
											model.to(__a      )
											model.eval()

											with torch.no_grad():
													snake_case_		:				str      							=    model(**self._prepare_for_class(__a	,__a      )      )

											snake_case_		:				Any      							=    outputs.hidden_states

											snake_case_		:				Optional[int]      							=    len(self.model_tester.depths      ) + 1
											self.assertEqual(len(__a      )	,__a      )

											snake_case_		:				int      							=    (self.model_tester.image_size, self.model_tester.image_size)
											snake_case_		:				int      							=    image_size[0], image_size[1]
											for _ in range(4      ):
													snake_case_		:				Optional[int]      							=    floor(
													    (
													        (height + 2 * self.model_tester.padding - self.model_tester.kernel_size)
													        / self.model_tester.stride
													    )
													    + 1      )
													snake_case_		:				Optional[Any]      							=    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],
											    ]	,)

									snake_case_		:				Union[str, Any]      							=    self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											snake_case_		:				str      							=    True
											check_hidden_states_output(__a	,__a	,__a      )

											# check that output_hidden_states also work using config
											del inputs_dict["output_hidden_states"]
											snake_case_		:				Union[str, Any]      							=    True

											check_hidden_states_output(__a	,__a	,__a      )





							@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""      )
							def 		a__ (	self		:List[str]      ):
									pass





							def 		a__ (	self		:Dict	,_UpperCamelCase		:str	,_UpperCamelCase		:List[str]	,_UpperCamelCase		:Optional[int]=False      ):
									snake_case_		:				Dict      							=    super()._prepare_for_class(__a	,__a	,return_labels=__a      )

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

									return inputs_dict





							def 		a__ (	self		:Tuple      ):
									snake_case_		:				Dict      							=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__a      )





							def 		a__ (	self		:str      ):
									snake_case_		:				List[str]      							=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*__a      )





							def 		a__ (	self		:Any      ):
									if not self.model_tester.is_training:
											return

									snake_case_		:				Dict      							=    self.model_tester.prepare_config_and_inputs_for_common()
									snake_case_		:				Dict      							=    True

									for model_class in self.all_model_classes:
											# LevitForImageClassificationWithTeacher supports inference-only
											if (
											    model_class in get_values(__a      )
											    or model_class.__name__ == "LevitForImageClassificationWithTeacher"
											):
													continue
											snake_case_		:				Optional[Any]      							=    model_class(__a      )
											model.to(__a      )
											model.train()
											snake_case_		:				str      							=    self._prepare_for_class(__a	,__a	,return_labels=__a      )
											snake_case_		:				str      							=    model(**__a      ).loss
											loss.backward()





							def 		a__ (	self		:str      ):
									snake_case_		:				Tuple      							=    self.model_tester.prepare_config_and_inputs_for_common()
									if not self.model_tester.is_training:
											return

									snake_case_		:				int      							=    False
									snake_case_		:				str      							=    True

									for model_class in self.all_model_classes:
											if model_class in get_values(__a      ) or not model_class.supports_gradient_checkpointing:
													continue
											# LevitForImageClassificationWithTeacher supports inference-only
											if model_class.__name__ == "LevitForImageClassificationWithTeacher":
													continue
											snake_case_		:				List[str]      							=    model_class(__a      )
											model.gradient_checkpointing_enable()
											model.to(__a      )
											model.train()
											snake_case_		:				int      							=    self._prepare_for_class(__a	,__a	,return_labels=__a      )
											snake_case_		:				Tuple      							=    model(**__a      ).loss
											loss.backward()





							def 		a__ (	self		:List[str]      ):
									snake_case_		:				Optional[int]      							=    self.model_tester.prepare_config_and_inputs_for_common()

									snake_case_		:				List[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(__a      ),
											    ]
											    or model_class.__name__ == "LevitForImageClassificationWithTeacher"
											):
													continue

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

															snake_case_		:				Tuple      							=    model_class(__a      )
															model.to(__a      )
															model.train()

															snake_case_		:				int      							=    self._prepare_for_class(__a	,__a	,return_labels=__a      )

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

															snake_case_		:				List[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=__a      ) as warning_list:
																	snake_case_		:				Tuple      							=    model(**__a      ).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 		a__ (	self		:int      ):
									for model_name in LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											snake_case_		:				List[Any]      							=    LevitModel.from_pretrained(__a      )
											self.assertIsNotNone(__a      )





def   UpperCAmelCase   (		):







		'''simple docstring'''
		snake_case_		:				List[Any]      							=    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
		return image






@require_torch
@require_vision
class     __UpperCamelCase						(    unittest.TestCase				):





							@cached_property
							def 		a__ (	self		:str      ):
									return LevitImageProcessor.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]      )





							@slow
							def 		a__ (	self		:Optional[int]      ):
									snake_case_		:				Union[str, Any]      							=    LevitForImageClassificationWithTeacher.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]      ).to(
									    __a      )

									snake_case_		:				Tuple      							=    self.default_image_processor
									snake_case_		:				List[str]      							=    prepare_img()
									snake_case_		:				Dict      							=    image_processor(images=__a	,return_tensors="""pt"""      ).to(__a      )

									# forward pass
									with torch.no_grad():
											snake_case_		:				Union[str, Any]      							=    model(**__a      )

									# verify the logits
									snake_case_		:				Optional[Any]      							=    torch.Size((1, 1_0_0_0)      )
									self.assertEqual(outputs.logits.shape	,__a      )

									snake_case_		:				List[str]      							=    torch.tensor([1.04_48, -0.37_45, -1.83_17]      ).to(__a      )

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






"""simple docstring"""


import builtins
import sys

from ...utils.imports import _is_package_available
from . import cursor, input
from .helpers import Direction, clear_line, forceWrite, linebreak, move_cursor, reset_cursor, writeColor
from .keymap import KEYMAP


lowerCamelCase__						= False
try:
  lowerCamelCase__						= _is_package_available("google.colab")
except ModuleNotFoundError:
  pass



@input.register
class   __SCREAMING_SNAKE_CASE     :







  '''simple docstring'''


  def __init__(		self :							Union[str, Any]       , __a :							str = None       , __a :							list = []						)		->					Union[str, Any]:
        _UpperCamelCase   :						Any    =							0
        _UpperCamelCase   :						Any    =							choices
        _UpperCamelCase   :						Tuple    =							prompt
        if sys.platform == "win32":
              _UpperCamelCase   :						str    =							"*"
        else:
              _UpperCamelCase   :						Dict    =							"➔ "

  def  __SCREAMING_SNAKE_CASE					(		self :							int       , __a :							Tuple       , __a :							str = ""						)		->					Dict:
        if sys.platform != "win32":
              writeColor(self.choices[index]       , 32       , __a						)
        else:
              forceWrite(self.choices[index]       , __a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Tuple       , __a :							int						)		->					Dict:
        if index == self.position:
              forceWrite(F''' {self.arrow_char} '''						)
              self.write_choice(__a						)
        else:
              forceWrite(F'''    {self.choices[index]}'''						)
        reset_cursor()

  def  __SCREAMING_SNAKE_CASE					(		self :							List[str]       , __a :							Direction       , __a :							int = 1						)		->					List[str]:
        _UpperCamelCase   :						Tuple    =							self.position
        if direction == Direction.DOWN:
              if self.position + 1 >= len(self.choices						):
                    return
              self.position += num_spaces
        else:
              if self.position - 1 < 0:
                    return
              self.position -= num_spaces
        clear_line()
        self.print_choice(__a						)
        move_cursor(__a       , direction.name						)
        self.print_choice(self.position						)

  @input.mark(KEYMAP["up"]						)
  def  __SCREAMING_SNAKE_CASE					(		self :							Union[str, Any]						)		->					Any:
        self.move_direction(Direction.UP						)

  @input.mark(KEYMAP["down"]						)
  def  __SCREAMING_SNAKE_CASE					(		self :							Dict						)		->					List[Any]:
        self.move_direction(Direction.DOWN						)

  @input.mark(KEYMAP["newline"]						)
  def  __SCREAMING_SNAKE_CASE					(		self :							int						)		->					str:
        move_cursor(len(self.choices						) - self.position       , "DOWN"						)
        return self.position

  @input.mark(KEYMAP["interrupt"]						)
  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[Any]						)		->					Tuple:
        move_cursor(len(self.choices						) - self.position       , "DOWN"						)
        raise KeyboardInterrupt

  @input.mark_multiple(*[KEYMAP[str(__a						)] for number in range(10						)]						)
  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[int]						)		->					List[str]:
        _UpperCamelCase   :						Union[str, Any]    =							int(chr(self.current_selection						)						)
        _UpperCamelCase   :						List[str]    =							index - self.position
        if index == self.position:
              return
        if index < len(self.choices						):
              if self.position > index:
                    self.move_direction(Direction.UP       , -movement						)
              elif self.position < index:
                    self.move_direction(Direction.DOWN       , __a						)
              else:
                    return
        else:
              return


  def  __SCREAMING_SNAKE_CASE					(		self :							List[Any]       , __a :							int = 0						)		->					List[Any]:
        if self.prompt:
              linebreak()
              forceWrite(self.prompt       , "\n"						)
              if in_colab:
                    forceWrite("Please input a choice index (starting from 0), and press enter"       , "\n"						)
              else:
                    forceWrite("Please select a choice using the arrow or number keys, and selecting with enter"       , "\n"						)
        _UpperCamelCase   :						int    =							default_choice
        for i in range(len(self.choices						)						):
              self.print_choice(__a						)
              forceWrite("\n"						)
        move_cursor(len(self.choices						) - self.position       , "UP"						)
        with cursor.hide():
              while True:
                    if in_colab:
                          try:
                                _UpperCamelCase   :						Union[str, Any]    =							int(builtins.input()						)
                          except ValueError:
                                _UpperCamelCase   :						int    =							default_choice
                    else:
                          _UpperCamelCase   :						Any    =							self.handle_input()
                    if choice is not None:
                          reset_cursor()
                          for _ in range(len(self.choices						) + 1						):
                                move_cursor(1       , "UP"						)
                                clear_line()
                          self.write_choice(__a       , "\n"						)
                          return choice
