
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			:






			'''simple docstring'''

			def __init__(    self	:    Optional[Any]  ,		snake_case_	:    Tuple  ,		snake_case_	:    int=13  ,		snake_case_	:    Dict=7  ,		snake_case_	:    Any=True  ,		snake_case_	:    Optional[Any]=True  ,		snake_case_	:    str=True  ,		snake_case_	:    str=True  ,		snake_case_	:    Tuple=99  ,		snake_case_	:    Dict=24  ,		snake_case_	:    Optional[Any]=2  ,		snake_case_	:    Dict=6  ,		snake_case_	:    str=37  ,		snake_case_	:    int="gelu"  ,		snake_case_	:    Optional[Any]=0.1  ,		snake_case_	:    Union[str, Any]=0.1  ,		snake_case_	:    List[Any]=512  ,		snake_case_	:    List[Any]=16  ,		snake_case_	:    List[Any]=2  ,		snake_case_	:    Dict=0.02  ,		snake_case_	:    Dict=3  ,		snake_case_	:    Optional[Any]=None  ,		snake_case_	:    Dict=1000  ,		):
							UpperCamelCase_:							Tuple		=  parent
							UpperCamelCase_:							int		=  batch_size
							UpperCamelCase_:							Any		=  seq_length
							UpperCamelCase_:							Optional[int]		=  is_training
							UpperCamelCase_:							List[Any]		=  use_input_mask
							UpperCamelCase_:							Optional[Any]		=  use_token_type_ids
							UpperCamelCase_:							str		=  use_labels
							UpperCamelCase_:							Any		=  vocab_size
							UpperCamelCase_:							List[str]		=  hidden_size
							UpperCamelCase_:							Optional[int]		=  num_hidden_layers
							UpperCamelCase_:							List[Any]		=  num_attention_heads
							UpperCamelCase_:							Any		=  intermediate_size
							UpperCamelCase_:							List[Any]		=  hidden_act
							UpperCamelCase_:							Optional[int]		=  hidden_dropout_prob
							UpperCamelCase_:							Tuple		=  attention_probs_dropout_prob
							UpperCamelCase_:							str		=  max_position_embeddings
							UpperCamelCase_:							Optional[int]		=  type_vocab_size
							UpperCamelCase_:							Tuple		=  type_sequence_label_size
							UpperCamelCase_:							str		=  initializer_range
							UpperCamelCase_:							Tuple		=  num_labels
							UpperCamelCase_:							str		=  scope
							UpperCamelCase_:							Tuple		=  range_bbox

			def  lowerCAmelCase__		(    self	:    Dict ):
							UpperCamelCase_:							Any		=  ids_tensor([self.batch_size, self.seq_length]  ,		self.vocab_size )

							UpperCamelCase_:							Union[str, Any]		=  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_:							List[Any]		=  bbox[i, j, 3]
																			UpperCamelCase_:							Optional[Any]		=  bbox[i, j, 1]
																			UpperCamelCase_:							Union[str, Any]		=  t
															if bbox[i, j, 2] < bbox[i, j, 0]:
																			UpperCamelCase_:							Dict		=  bbox[i, j, 2]
																			UpperCamelCase_:							int		=  bbox[i, j, 0]
																			UpperCamelCase_:							List[Any]		=  t

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

							UpperCamelCase_:							Optional[Any]		=  None
							if self.use_token_type_ids:
											UpperCamelCase_:							Union[str, Any]		=  ids_tensor([self.batch_size, self.seq_length]  ,		self.type_vocab_size )

							UpperCamelCase_:							Union[str, Any]		=  None
							UpperCamelCase_:							List[Any]		=  None
							if self.use_labels:
											UpperCamelCase_:							Optional[int]		=  ids_tensor([self.batch_size]  ,		self.type_sequence_label_size )
											UpperCamelCase_:							Union[str, Any]		=  ids_tensor([self.batch_size, self.seq_length]  ,		self.num_labels )

							UpperCamelCase_:							Dict		=  self.get_config()

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

			def  lowerCAmelCase__		(    self	:    Optional[int] ):
							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  lowerCAmelCase__		(    self	:    Dict  ,		snake_case_	:    Union[str, Any]  ,		snake_case_	:    List[str]  ,		snake_case_	:    int  ,		snake_case_	:    int  ,		snake_case_	:    Union[str, Any]  ,		snake_case_	:    Tuple  ,		snake_case_	:    Optional[int]  ,		):
							UpperCamelCase_:							Dict		=  LiltModel(config=snake_case_ )
							model.to(snake_case_ )
							model.eval()
							UpperCamelCase_:							Any		=  model(snake_case_  ,		bbox=snake_case_  ,		attention_mask=snake_case_  ,		token_type_ids=snake_case_ )
							UpperCamelCase_:							Tuple		=  model(snake_case_  ,		bbox=snake_case_  ,		token_type_ids=snake_case_ )
							UpperCamelCase_:							Dict		=  model(snake_case_  ,		bbox=snake_case_ )

							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  lowerCAmelCase__		(    self	:    Optional[Any]  ,		snake_case_	:    str  ,		snake_case_	:    Optional[Any]  ,		snake_case_	:    List[Any]  ,		snake_case_	:    Optional[Any]  ,		snake_case_	:    Optional[int]  ,		snake_case_	:    Optional[int]  ,		snake_case_	:    List[str]  ,		):
							UpperCamelCase_:							Union[str, Any]		=  self.num_labels
							UpperCamelCase_:							Dict		=  LiltForTokenClassification(config=snake_case_ )
							model.to(snake_case_ )
							model.eval()
							UpperCamelCase_:							Tuple		=  model(
							    snake_case_  ,		bbox=snake_case_  ,		attention_mask=snake_case_  ,		token_type_ids=snake_case_  ,		labels=snake_case_ )
							self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.seq_length, self.num_labels) )

			def  lowerCAmelCase__		(    self	:    Optional[int]  ,		snake_case_	:    str  ,		snake_case_	:    Any  ,		snake_case_	:    Optional[Any]  ,		snake_case_	:    List[Any]  ,		snake_case_	:    int  ,		snake_case_	:    Optional[int]  ,		snake_case_	:    int  ,		):
							UpperCamelCase_:							Union[str, Any]		=  LiltForQuestionAnswering(config=snake_case_ )
							model.to(snake_case_ )
							model.eval()
							UpperCamelCase_:							Any		=  model(
							    snake_case_  ,		bbox=snake_case_  ,		attention_mask=snake_case_  ,		token_type_ids=snake_case_  ,		start_positions=snake_case_  ,		end_positions=snake_case_  ,		)
							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  lowerCAmelCase__		(    self	:    str ):
							UpperCamelCase_:							str		=  self.prepare_config_and_inputs()
							(
							    (
							    UpperCamelCase_
							),				(
							    UpperCamelCase_
							),				(
							    UpperCamelCase_
							),				(
							    UpperCamelCase_
							),				(
							    UpperCamelCase_
							),				(
							    UpperCamelCase_
							),				(
							    UpperCamelCase_
							),				
							):							int		=  config_and_inputs
							UpperCamelCase_:							Optional[Any]		=  {
							    """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					):






			'''simple docstring'''

			__UpperCamelCase :  str    =	(
			    (
			        LiltModel,
			        LiltForSequenceClassification,
			        LiltForTokenClassification,
			        LiltForQuestionAnswering,
			    )
			    if is_torch_available()
			    else ()
			)
			__UpperCamelCase :  List[Any]    =	(
			    {
			        """feature-extraction""": LiltModel,
			        """question-answering""": LiltForQuestionAnswering,
			        """text-classification""": LiltForSequenceClassification,
			        """token-classification""": LiltForTokenClassification,
			        """zero-shot""": LiltForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			__UpperCamelCase :  Any    =	False
			__UpperCamelCase :  Union[str, Any]    =	False

			def  lowerCAmelCase__		(    self	:    Dict  ,		snake_case_	:    int  ,		snake_case_	:    Union[str, Any]  ,		snake_case_	:    Optional[Any]  ,		snake_case_	:    Optional[Any]  ,		snake_case_	:    Optional[int] ):
							return True

			def  lowerCAmelCase__		(    self	:    Union[str, Any] ):
							UpperCamelCase_:							Any		=  LiltModelTester(self )
							UpperCamelCase_:							str		=  ConfigTester(self  ,		config_class=snake_case_  ,		hidden_size=37 )

			def  lowerCAmelCase__		(    self	:    List[Any] ):
							self.config_tester.run_common_tests()

			def  lowerCAmelCase__		(    self	:    List[Any] ):
							UpperCamelCase_:							List[Any]		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*snake_case_ )

			def  lowerCAmelCase__		(    self	:    List[Any] ):
							UpperCamelCase_:							int		=  self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
											UpperCamelCase_:							int		=  type
											self.model_tester.create_and_check_model(*snake_case_ )

			def  lowerCAmelCase__		(    self	:    Any ):
							UpperCamelCase_:							List[str]		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*snake_case_ )

			def  lowerCAmelCase__		(    self	:    Union[str, Any] ):
							UpperCamelCase_:							int		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*snake_case_ )




			@slow
			def  lowerCAmelCase__		(    self	:    Optional[int] ):
							for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											UpperCamelCase_:							Optional[Any]		=  LiltModel.from_pretrained(snake_case_ )
											self.assertIsNotNone(snake_case_ )




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






			'''simple docstring'''

			def  lowerCAmelCase__		(    self	:    Optional[Any] ):
							UpperCamelCase_:							Tuple		=  LiltModel.from_pretrained("""SCUT-DLVCLab/lilt-roberta-en-base""" ).to(snake_case_ )

							UpperCamelCase_:							Dict		=  torch.tensor([[1, 2]]  ,		device=snake_case_ )
							UpperCamelCase_:							Any		=  torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]  ,		device=snake_case_ )

							# forward pass
							with torch.no_grad():
											UpperCamelCase_:							int		=  model(input_ids=snake_case_  ,		bbox=snake_case_ )

							UpperCamelCase_:							Dict		=  torch.Size([1, 2, 768] )
							UpperCamelCase_:							int		=  torch.tensor(
							    [[-0.0653, 0.0950, -0.0061], [-0.0545, 0.0926, -0.0324]]  ,		device=snake_case_  ,		)

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








from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCamelCase_  :	Tuple 				=					logging.get_logger(__name__)

lowerCamelCase_  :	str 				=					{
    """unc-nlp/lxmert-base-uncased""": """https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json""",
}

class     _UpperCamelCase			(   _A					):






			'''simple docstring'''

			__UpperCamelCase :  Any    =	"""lxmert"""
			__UpperCamelCase :  Optional[Any]    =	{}

			def __init__(    self	:    Dict  ,		snake_case_	:    List[str]=3_0522  ,		snake_case_	:    int=768  ,		snake_case_	:    str=12  ,		snake_case_	:    Tuple=9500  ,		snake_case_	:    Optional[Any]=1600  ,		snake_case_	:    str=400  ,		snake_case_	:    List[str]=3072  ,		snake_case_	:    Dict="gelu"  ,		snake_case_	:    Optional[Any]=0.1  ,		snake_case_	:    int=0.1  ,		snake_case_	:    Optional[int]=512  ,		snake_case_	:    Tuple=2  ,		snake_case_	:    Optional[int]=0.02  ,		snake_case_	:    Union[str, Any]=1e-12  ,		snake_case_	:    Tuple=9  ,		snake_case_	:    Tuple=5  ,		snake_case_	:    Union[str, Any]=5  ,		snake_case_	:    List[str]=2048  ,		snake_case_	:    Any=4  ,		snake_case_	:    int=6.67  ,		snake_case_	:    Dict=True  ,		snake_case_	:    Tuple=True  ,		snake_case_	:    int=True  ,		snake_case_	:    str=True  ,		snake_case_	:    List[Any]=True  ,		snake_case_	:    List[Any]=True  ,		snake_case_	:    List[str]=True  ,		**snake_case_	:    Optional[int]  ,		):
							UpperCamelCase_:							int		=  vocab_size
							UpperCamelCase_:							Tuple		=  hidden_size
							UpperCamelCase_:							Optional[Any]		=  num_attention_heads
							UpperCamelCase_:							List[str]		=  hidden_act
							UpperCamelCase_:							int		=  intermediate_size
							UpperCamelCase_:							Any		=  hidden_dropout_prob
							UpperCamelCase_:							List[Any]		=  attention_probs_dropout_prob
							UpperCamelCase_:							List[str]		=  max_position_embeddings
							UpperCamelCase_:							Optional[Any]		=  type_vocab_size
							UpperCamelCase_:							Optional[Any]		=  initializer_range
							UpperCamelCase_:							str		=  layer_norm_eps
							UpperCamelCase_:							List[str]		=  num_qa_labels
							UpperCamelCase_:							List[str]		=  num_object_labels
							UpperCamelCase_:							Tuple		=  num_attr_labels
							UpperCamelCase_:							List[Any]		=  l_layers
							UpperCamelCase_:							List[Any]		=  x_layers
							UpperCamelCase_:							Any		=  r_layers
							UpperCamelCase_:							str		=  visual_feat_dim
							UpperCamelCase_:							Union[str, Any]		=  visual_pos_dim
							UpperCamelCase_:							str		=  visual_loss_normalizer
							UpperCamelCase_:							Dict		=  task_matched
							UpperCamelCase_:							Tuple		=  task_mask_lm
							UpperCamelCase_:							List[str]		=  task_obj_predict
							UpperCamelCase_:							Any		=  task_qa
							UpperCamelCase_:							Dict		=  visual_obj_loss
							UpperCamelCase_:							Optional[Any]		=  visual_attr_loss
							UpperCamelCase_:							List[Any]		=  visual_feat_loss
							UpperCamelCase_:							Tuple		=  {"""vision""": r_layers, """cross_encoder""": x_layers, """language""": l_layers}
							super().__init__(**snake_case_ )






