






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        lowerCAmelCase_				(      lowercase	):







				"""simple docstring"""




				def 				__a   ( self     :Optional[int]     ):
					UpperCamelCase__ :List[str]						=      self.config_class(**self.inputs_dict     )
					self.parent.assertTrue(hasattr(lowerCamelCase__  ,     """hidden_sizes"""     )     )
					self.parent.assertTrue(hasattr(lowerCamelCase__  ,     """num_attention_heads"""     )     )

class        lowerCAmelCase_				:







				"""simple docstring"""




				def __init__( self     :Dict  ,     lowerCamelCase__     :Optional[Any]  ,     lowerCamelCase__     :str=13  ,     lowerCamelCase__     :Dict=64  ,     lowerCamelCase__     :Dict=3  ,     lowerCamelCase__     :Union[str, Any]=3  ,     lowerCamelCase__     :Dict=2  ,     lowerCamelCase__     :Tuple=1  ,     lowerCamelCase__     :Tuple=16  ,     lowerCamelCase__     :int=[1_28, 2_56, 3_84]  ,     lowerCamelCase__     :int=[4, 6, 8]  ,     lowerCamelCase__     :Optional[Any]=[2, 3, 4]  ,     lowerCamelCase__     :int=[16, 16, 16]  ,     lowerCamelCase__     :List[Any]=0  ,     lowerCamelCase__     :List[Any]=[2, 2, 2]  ,     lowerCamelCase__     :List[Any]=[2, 2, 2]  ,     lowerCamelCase__     :Dict=0.02  ,     lowerCamelCase__     :Tuple=True  ,     lowerCamelCase__     :Any=True  ,     lowerCamelCase__     :int=2  ,     ):
					UpperCamelCase__ :Optional[Any]						=      parent
					UpperCamelCase__ :List[Any]						=      batch_size
					UpperCamelCase__ :Optional[int]						=      image_size
					UpperCamelCase__ :Any						=      num_channels
					UpperCamelCase__ :List[Any]						=      kernel_size
					UpperCamelCase__ :List[Any]						=      stride
					UpperCamelCase__ :Optional[Any]						=      padding
					UpperCamelCase__ :Optional[int]						=      hidden_sizes
					UpperCamelCase__ :Tuple						=      num_attention_heads
					UpperCamelCase__ :Optional[int]						=      depths
					UpperCamelCase__ :Dict						=      key_dim
					UpperCamelCase__ :Tuple						=      drop_path_rate
					UpperCamelCase__ :Optional[Any]						=      patch_size
					UpperCamelCase__ :Optional[Any]						=      attention_ratio
					UpperCamelCase__ :List[str]						=      mlp_ratio
					UpperCamelCase__ :int						=      initializer_range
					UpperCamelCase__ :List[Any]						=      [
					    ["""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],
					]
					UpperCamelCase__ :int						=      is_training
					UpperCamelCase__ :Union[str, Any]						=      use_labels
					UpperCamelCase__ :Optional[int]						=      num_labels
					UpperCamelCase__ :Any						=      initializer_range







				def 				__a   ( self     :str     ):
					UpperCamelCase__ :Tuple						=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

					UpperCamelCase__ :int						=      None
					if self.use_labels:
						UpperCamelCase__ :List[Any]						=      ids_tensor([self.batch_size]  ,     self.num_labels     )

					UpperCamelCase__ :Optional[Any]						=      self.get_config()
					return config, pixel_values, labels







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







				def 				__a   ( self     :Optional[Any]     ):
					UpperCamelCase__ :str						=      self.prepare_config_and_inputs()
					UpperCamelCase__				,		UpperCamelCase__				,		UpperCamelCase__ :Tuple						=      config_and_inputs
					UpperCamelCase__ :Optional[int]						=      {"""pixel_values""": pixel_values}
					return config, inputs_dict

@require_torch
class        lowerCAmelCase_				(      lowercase						,      lowercase						,      unittest.TestCase	):







				"""simple docstring"""




				_snake_case				:   int        =			(
				    (LevitModel, LevitForImageClassification, LevitForImageClassificationWithTeacher)
				    if is_torch_available()
				    else ()
				)
				_snake_case				:   Union[str, Any]        =			(
				    {
				        """feature-extraction""": LevitModel,
				        """image-classification""": (LevitForImageClassification, LevitForImageClassificationWithTeacher),
				    }
				    if is_torch_available()
				    else {}
				)

				_snake_case				:   List[str]        =			False
				_snake_case				:   Dict        =			False
				_snake_case				:   Dict        =			False
				_snake_case				:   List[Any]        =			False
				_snake_case				:   Dict        =			False







				def 				__a   ( self     :Optional[int]     ):
					UpperCamelCase__ :Optional[Any]						=      LevitModelTester(self     )
					UpperCamelCase__ :int						=      ConfigTester(self  ,     config_class=lowerCamelCase__  ,     has_text_modality=lowerCamelCase__  ,     hidden_size=37     )







				def 				__a   ( self     :Optional[int]     ):
					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     :Union[str, Any]     ):
					return







				@unittest.skip(reason="""Levit does not use inputs_embeds"""     )
				def 				__a   ( self     :Union[str, Any]     ):
					pass







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







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







				def 				__a   ( self     :Union[str, Any]     ):
					UpperCamelCase__				,		UpperCamelCase__ :str						=      self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						UpperCamelCase__ :List[str]						=      model_class(lowerCamelCase__     )
						UpperCamelCase__ :Any						=      inspect.signature(model.forward     )
						# signature.parameters is an OrderedDict => so arg_names order is deterministic
						UpperCamelCase__ :Tuple						=      [*signature.parameters.keys()]

						UpperCamelCase__ :Optional[int]						=      ["""pixel_values"""]
						self.assertListEqual(arg_names[:1]  ,     lowerCamelCase__     )







				def 				__a   ( self     :int     ):
					def check_hidden_states_output(lowerCamelCase__     :Dict  ,     lowerCamelCase__     :Dict  ,     lowerCamelCase__     :str     ):
						UpperCamelCase__ :Dict						=      model_class(lowerCamelCase__     )
						model.to(lowerCamelCase__     )
						model.eval()

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

						UpperCamelCase__ :Optional[int]						=      outputs.hidden_states

						UpperCamelCase__ :Dict						=      len(self.model_tester.depths     ) + 1
						self.assertEqual(len(lowerCamelCase__     )  ,     lowerCamelCase__     )

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

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

					for model_class in self.all_model_classes:
						UpperCamelCase__ :int						=      True
						check_hidden_states_output(lowerCamelCase__  ,     lowerCamelCase__  ,     lowerCamelCase__     )

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						UpperCamelCase__ :Dict						=      True

						check_hidden_states_output(lowerCamelCase__  ,     lowerCamelCase__  ,     lowerCamelCase__     )







				@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""     )
				def 				__a   ( self     :Dict     ):
					pass







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

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

					return inputs_dict







				def 				__a   ( self     :Dict     ):
					UpperCamelCase__ :Tuple						=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*lowerCamelCase__     )







				def 				__a   ( self     :Tuple     ):
					UpperCamelCase__ :List[Any]						=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__     )







				def 				__a   ( self     :List[Any]     ):
					if not self.model_tester.is_training:
						return

					UpperCamelCase__				,		UpperCamelCase__ :List[str]						=      self.model_tester.prepare_config_and_inputs_for_common()
					UpperCamelCase__ :str						=      True

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







				def 				__a   ( self     :Tuple     ):
					UpperCamelCase__				,		UpperCamelCase__ :Any						=      self.model_tester.prepare_config_and_inputs_for_common()
					if not self.model_tester.is_training:
						return

					UpperCamelCase__ :Union[str, Any]						=      False
					UpperCamelCase__ :Optional[int]						=      True

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







				def 				__a   ( self     :Optional[int]     ):
					UpperCamelCase__				,		UpperCamelCase__ :List[Any]						=      self.model_tester.prepare_config_and_inputs_for_common()

					UpperCamelCase__ :Dict						=      [
					    {"""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(lowerCamelCase__     ),
						    ]
						    or model_class.__name__ == "LevitForImageClassificationWithTeacher"
						):
							continue

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

								UpperCamelCase__ :Dict						=      model_class(lowerCamelCase__     )
								model.to(lowerCamelCase__     )
								model.train()

								UpperCamelCase__ :List[str]						=      self._prepare_for_class(lowerCamelCase__  ,     lowerCamelCase__  ,     return_labels=lowerCamelCase__     )

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

								UpperCamelCase__ :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=lowerCamelCase__     ) as warning_list:
									UpperCamelCase__ :Dict						=      model(**lowerCamelCase__     ).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     :Any     ):
					for model_name in LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						UpperCamelCase__ :Any						=      LevitModel.from_pretrained(lowerCamelCase__     )
						self.assertIsNotNone(lowerCamelCase__     )




def 				A		(					)		-> str:
	UpperCamelCase__ :int						=      Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  )
	return image




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







				"""simple docstring"""




				@cached_property
				def 				__a   ( self     :Any     ):
					return LevitImageProcessor.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]     )







				@slow
				def 				__a   ( self     :Tuple     ):
					UpperCamelCase__ :Any						=      LevitForImageClassificationWithTeacher.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]     ).to(
					    lowerCamelCase__     )

					UpperCamelCase__ :Optional[Any]						=      self.default_image_processor
					UpperCamelCase__ :Union[str, Any]						=      prepare_img()
					UpperCamelCase__ :str						=      image_processor(images=lowerCamelCase__  ,     return_tensors="""pt"""     ).to(lowerCamelCase__     )

					# forward pass
					with torch.no_grad():
						UpperCamelCase__ :List[str]						=      model(**lowerCamelCase__     )

					# verify the logits
					UpperCamelCase__ :str						=      torch.Size((1, 10_00)     )
					self.assertEqual(outputs.logits.shape  ,     lowerCamelCase__     )

					UpperCamelCase__ :Tuple						=      torch.tensor([1.0448, -0.3745, -1.8317]     ).to(lowerCamelCase__     )

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






import unittest

from transformers import LiltConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import (
       LiltForQuestionAnswering,
       LiltForSequenceClassification,
       LiltForTokenClassification,
       LiltModel,
   )
   from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST




class 			__UpperCamelCase    :





     def __init__(     self				,							_UpperCamelCase				,							_UpperCamelCase=13				,							_UpperCamelCase=7				,							_UpperCamelCase=True				,							_UpperCamelCase=True				,							_UpperCamelCase=True				,							_UpperCamelCase=True				,							_UpperCamelCase=99				,							_UpperCamelCase=24				,							_UpperCamelCase=2				,							_UpperCamelCase=6				,							_UpperCamelCase=37				,							_UpperCamelCase="gelu"				,							_UpperCamelCase=0.1				,							_UpperCamelCase=0.1				,							_UpperCamelCase=512				,							_UpperCamelCase=16				,							_UpperCamelCase=2				,							_UpperCamelCase=0.02				,							_UpperCamelCase=3				,							_UpperCamelCase=None				,							_UpperCamelCase=1000				,							):
        _UpperCAmelCase           =   parent
        _UpperCAmelCase           =   batch_size
        _UpperCAmelCase           =   seq_length
        _UpperCAmelCase           =   is_training
        _UpperCAmelCase           =   use_input_mask
        _UpperCAmelCase           =   use_token_type_ids
        _UpperCAmelCase           =   use_labels
        _UpperCAmelCase           =   vocab_size
        _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           =   max_position_embeddings
        _UpperCAmelCase           =   type_vocab_size
        _UpperCAmelCase           =   type_sequence_label_size
        _UpperCAmelCase           =   initializer_range
        _UpperCAmelCase           =   num_labels
        _UpperCAmelCase           =   scope
        _UpperCAmelCase           =   range_bbox





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   ids_tensor([self.batch_size, self.seq_length]				,							self.vocab_size					)

        _UpperCAmelCase           =   ids_tensor([self.batch_size, self.seq_length, 4]				,							self.range_bbox					)
        # Ensure that bbox is legal
        for i in range(bbox.shape[0]					):
           for j in range(bbox.shape[1]					):
              if bbox[i, j, 3] < bbox[i, j, 1]:
                 _UpperCAmelCase           =   bbox[i, j, 3]
                 _UpperCAmelCase           =   bbox[i, j, 1]
                 _UpperCAmelCase           =   t
              if bbox[i, j, 2] < bbox[i, j, 0]:
                 _UpperCAmelCase           =   bbox[i, j, 2]
                 _UpperCAmelCase           =   bbox[i, j, 0]
                 _UpperCAmelCase           =   t

        _UpperCAmelCase           =   None
        if self.use_input_mask:
           _UpperCAmelCase           =   ids_tensor([self.batch_size, self.seq_length]				,							vocab_size=2					)

        _UpperCAmelCase           =   None
        if self.use_token_type_ids:
           _UpperCAmelCase           =   ids_tensor([self.batch_size, self.seq_length]				,							self.type_vocab_size					)

        _UpperCAmelCase           =   None
        _UpperCAmelCase           =   None
        if self.use_labels:
           _UpperCAmelCase           =   ids_tensor([self.batch_size]				,							self.type_sequence_label_size					)
           _UpperCAmelCase           =   ids_tensor([self.batch_size, self.seq_length]				,							self.num_labels					)

        _UpperCAmelCase           =   self.get_config()

        return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels





     def 		UpperCamelCase(     self					):
        return LiltConfig(
            vocab_size=self.vocab_size				,							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				,							max_position_embeddings=self.max_position_embeddings				,							type_vocab_size=self.type_vocab_size				,							initializer_range=self.initializer_range				,							)





     def 		UpperCamelCase(     self				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							):
        _UpperCAmelCase           =   LiltModel(config=_UpperCamelCase					)
        model.to(_UpperCamelCase					)
        model.eval()
        _UpperCAmelCase           =   model(_UpperCamelCase				,							bbox=_UpperCamelCase				,							attention_mask=_UpperCamelCase				,							token_type_ids=_UpperCamelCase					)
        _UpperCAmelCase           =   model(_UpperCamelCase				,							bbox=_UpperCamelCase				,							token_type_ids=_UpperCamelCase					)
        _UpperCAmelCase           =   model(_UpperCamelCase				,							bbox=_UpperCamelCase					)

        self.parent.assertEqual(result.last_hidden_state.shape				,							(self.batch_size, self.seq_length, self.hidden_size)					)
        self.parent.assertEqual(result.pooler_output.shape				,							(self.batch_size, self.hidden_size)					)





     def 		UpperCamelCase(     self				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							):
        _UpperCAmelCase           =   self.num_labels
        _UpperCAmelCase           =   LiltForTokenClassification(config=_UpperCamelCase					)
        model.to(_UpperCamelCase					)
        model.eval()
        _UpperCAmelCase           =   model(
            _UpperCamelCase				,							bbox=_UpperCamelCase				,							attention_mask=_UpperCamelCase				,							token_type_ids=_UpperCamelCase				,							labels=_UpperCamelCase					)
        self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.seq_length, self.num_labels)					)





     def 		UpperCamelCase(     self				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							):
        _UpperCAmelCase           =   LiltForQuestionAnswering(config=_UpperCamelCase					)
        model.to(_UpperCamelCase					)
        model.eval()
        _UpperCAmelCase           =   model(
            _UpperCamelCase				,							bbox=_UpperCamelCase				,							attention_mask=_UpperCamelCase				,							token_type_ids=_UpperCamelCase				,							start_positions=_UpperCamelCase				,							end_positions=_UpperCamelCase				,							)
        self.parent.assertEqual(result.start_logits.shape				,							(self.batch_size, self.seq_length)					)
        self.parent.assertEqual(result.end_logits.shape				,							(self.batch_size, self.seq_length)					)





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.prepare_config_and_inputs()
        (
            (
            _UpperCAmelCase
        )       ,      (
            _UpperCAmelCase
        )       ,      (
            _UpperCAmelCase
        )       ,      (
            _UpperCAmelCase
        )       ,      (
            _UpperCAmelCase
        )       ,      (
            _UpperCAmelCase
        )       ,      (
            _UpperCAmelCase
        )       ,      
        )           =   config_and_inputs
        _UpperCAmelCase           =   {
            '''input_ids''': input_ids,
            '''bbox''': bbox,
            '''token_type_ids''': token_type_ids,
            '''attention_mask''': input_mask,
        }
        return config, inputs_dict




@require_torch
class 			__UpperCamelCase    (							A__	, A__	, A__	, unittest.TestCase    ):
     __A	:  Dict							  =   (
         (
             LiltModel,
             LiltForSequenceClassification,
             LiltForTokenClassification,
             LiltForQuestionAnswering,
         )
         if is_torch_available()
         else ()
     )
     __A	:  Optional[Any]							  =   (
         {
             """feature-extraction""": LiltModel,
             """question-answering""": LiltForQuestionAnswering,
             """text-classification""": LiltForSequenceClassification,
             """token-classification""": LiltForTokenClassification,
             """zero-shot""": LiltForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     __A	:  List[Any]							  =   False
     __A	:  Optional[int]							  =   False





     def 		UpperCamelCase(     self				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase					):
        return True





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   LiltModelTester(self					)
        _UpperCAmelCase           =   ConfigTester(self				,							config_class=_UpperCamelCase				,							hidden_size=37					)





     def 		UpperCamelCase(     self					):
        self.config_tester.run_common_tests()





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_UpperCamelCase					)





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
           _UpperCAmelCase           =   type
           self.model_tester.create_and_check_model(*_UpperCamelCase					)





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*_UpperCamelCase					)





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*_UpperCamelCase					)





     @slow
     def 		UpperCamelCase(     self					):
        for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _UpperCAmelCase           =   LiltModel.from_pretrained(_UpperCamelCase					)
           self.assertIsNotNone(_UpperCamelCase					)




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





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   LiltModel.from_pretrained('''SCUT-DLVCLab/lilt-roberta-en-base'''					).to(_UpperCamelCase					)

        _UpperCAmelCase           =   torch.tensor([[1, 2]]				,							device=_UpperCamelCase					)
        _UpperCAmelCase           =   torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]				,							device=_UpperCamelCase					)

        # forward pass
        with torch.no_grad():
           _UpperCAmelCase           =   model(input_ids=_UpperCamelCase				,							bbox=_UpperCamelCase					)

        _UpperCAmelCase           =   torch.Size([1, 2, 768]					)
        _UpperCAmelCase           =   torch.tensor(
            [[-0.0653, 0.0950, -0.0061], [-0.0545, 0.0926, -0.0324]]				,							device=_UpperCamelCase				,							)

        self.assertTrue(outputs.last_hidden_state.shape				,							_UpperCamelCase					)
        self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :, :3]				,							_UpperCamelCase				,							atol=1e-3					)					)