

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 						__magic_name__					:






     def __init__(				self		,						_lowercase		,						_lowercase=13		,						_lowercase=7		,						_lowercase=True		,						_lowercase=True		,						_lowercase=True		,						_lowercase=True		,						_lowercase=99		,						_lowercase=24		,						_lowercase=2		,						_lowercase=6		,						_lowercase=37		,						_lowercase="gelu"		,						_lowercase=0.1		,						_lowercase=0.1		,						_lowercase=512		,						_lowercase=16		,						_lowercase=2		,						_lowercase=0.02		,						_lowercase=3		,						_lowercase=None		,						_lowercase=1_000		,						)->     Tuple:
         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	)->     List[Any]:
         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	)->     List[Any]:
         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		,						_lowercase		,						_lowercase		,						_lowercase		,						_lowercase		,						_lowercase		,						_lowercase		,						_lowercase		,						)->     Optional[Any]:
         UpperCamelCase_			  =						LiltModel(config=_lowercase	)
         model.to(_lowercase	)
         model.eval()
         UpperCamelCase_			  =						model(_lowercase		,						bbox=_lowercase		,						attention_mask=_lowercase		,						token_type_ids=_lowercase	)
         UpperCamelCase_			  =						model(_lowercase		,						bbox=_lowercase		,						token_type_ids=_lowercase	)
         UpperCamelCase_			  =						model(_lowercase		,						bbox=_lowercase	)

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






     def 	UpperCAmelCase_				(				self		,						_lowercase		,						_lowercase		,						_lowercase		,						_lowercase		,						_lowercase		,						_lowercase		,						_lowercase		,						)->     List[str]:
         UpperCamelCase_			  =						LiltForQuestionAnswering(config=_lowercase	)
         model.to(_lowercase	)
         model.eval()
         UpperCamelCase_			  =						model(
             _lowercase		,						bbox=_lowercase		,						attention_mask=_lowercase		,						token_type_ids=_lowercase		,						start_positions=_lowercase		,						end_positions=_lowercase		,						)
         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	)->     Union[str, Any]:
         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 						__magic_name__					(							snake_case			,      snake_case			,      snake_case			,      unittest.TestCase				):
     UpperCamelCase_	:Optional[Any]										=      (
         (
             LiltModel,
             LiltForSequenceClassification,
             LiltForTokenClassification,
             LiltForQuestionAnswering,
         )
         if is_torch_available()
         else ()
     )
     UpperCamelCase_	:Dict										=      (
         {
             """feature-extraction""": LiltModel,
             """question-answering""": LiltForQuestionAnswering,
             """text-classification""": LiltForSequenceClassification,
             """token-classification""": LiltForTokenClassification,
             """zero-shot""": LiltForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     UpperCamelCase_	:Tuple										=      False
     UpperCamelCase_	:Union[str, Any]										=      False






     def 	UpperCAmelCase_				(				self		,						_lowercase		,						_lowercase		,						_lowercase		,						_lowercase		,						_lowercase	)->     Tuple:
         return True






     def 	UpperCAmelCase_				(				self	)->     List[str]:
         UpperCamelCase_			  =						LiltModelTester(self	)
         UpperCamelCase_			  =						ConfigTester(self		,						config_class=_lowercase		,						hidden_size=37	)






     def 	UpperCAmelCase_				(				self	)->     Any:
         self.config_tester.run_common_tests()






     def 	UpperCAmelCase_				(				self	)->     Optional[int]:
         UpperCamelCase_			  =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowercase	)






     def 	UpperCAmelCase_				(				self	)->     Union[str, Any]:
         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(*_lowercase	)






     def 	UpperCAmelCase_				(				self	)->     Optional[Any]:
         UpperCamelCase_			  =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*_lowercase	)






     def 	UpperCAmelCase_				(				self	)->     str:
         UpperCamelCase_			  =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*_lowercase	)






     @slow
     def 	UpperCAmelCase_				(				self	)->     List[str]:
         for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCamelCase_			  =						LiltModel.from_pretrained(_lowercase	)
             self.assertIsNotNone(_lowercase	)






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






     def 	UpperCAmelCase_				(				self	)->     Tuple:
         UpperCamelCase_			  =						LiltModel.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base"	).to(_lowercase	)

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

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

         UpperCamelCase_			  =						torch.Size([1, 2, 768]	)
         UpperCamelCase_			  =						torch.tensor(
             [[-0.0_653, 0.0_950, -0.0_061], [-0.0_545, 0.0_926, -0.0_324]]		,						device=_lowercase		,						)

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



from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType






class 						__magic_name__					(							snake_case				):
     UpperCamelCase_	:Dict										=      ["""image_processor""", """tokenizer"""]
     UpperCamelCase_	:str										=      """BridgeTowerImageProcessor"""
     UpperCamelCase_	:Tuple										=      ("""RobertaTokenizer""", """RobertaTokenizerFast""")






     def __init__(				self		,						_lowercase		,						_lowercase	)->     Any:
         super().__init__(_lowercase		,						_lowercase	)






     def __call__(				self		,						_lowercase		,						_lowercase = None		,						_lowercase = True		,						_lowercase = False		,						_lowercase = None		,						_lowercase = None		,						_lowercase = 0		,						_lowercase = None		,						_lowercase = None		,						_lowercase = None		,						_lowercase = False		,						_lowercase = False		,						_lowercase = False		,						_lowercase = False		,						_lowercase = True		,						_lowercase = None		,						**_lowercase		,						)->     BatchEncoding:
         UpperCamelCase_			  =						self.tokenizer(
             text=_lowercase		,						add_special_tokens=_lowercase		,						padding=_lowercase		,						truncation=_lowercase		,						max_length=_lowercase		,						stride=_lowercase		,						pad_to_multiple_of=_lowercase		,						return_token_type_ids=_lowercase		,						return_attention_mask=_lowercase		,						return_overflowing_tokens=_lowercase		,						return_special_tokens_mask=_lowercase		,						return_offsets_mapping=_lowercase		,						return_length=_lowercase		,						verbose=_lowercase		,						return_tensors=_lowercase		,						**_lowercase		,						)
         # add pixel_values + pixel_mask
         UpperCamelCase_			  =						self.image_processor(
             _lowercase		,						return_tensors=_lowercase		,						do_normalize=_lowercase		,						do_center_crop=_lowercase		,						**_lowercase	)
         encoding.update(_lowercase	)

         return encoding






     def 	UpperCAmelCase_				(				self		,						*_lowercase		,						**_lowercase	)->     Union[str, Any]:
         return self.tokenizer.batch_decode(*_lowercase		,						**_lowercase	)






     def 	UpperCAmelCase_				(				self		,						*_lowercase		,						**_lowercase	)->     List[Any]:
         return self.tokenizer.decode(*_lowercase		,						**_lowercase	)






     @property
     def 	UpperCAmelCase_				(				self	)->     Any:
         UpperCamelCase_			  =						self.tokenizer.model_input_names
         UpperCamelCase_			  =						self.image_processor.model_input_names
         return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names	)	)
