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





     """simple docstring"""







     def __init__(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE=1_3						,						__SCREAMING_SNAKE_CASE=7						,						__SCREAMING_SNAKE_CASE=True						,						__SCREAMING_SNAKE_CASE=True						,						__SCREAMING_SNAKE_CASE=True						,						__SCREAMING_SNAKE_CASE=True						,						__SCREAMING_SNAKE_CASE=9_9						,						__SCREAMING_SNAKE_CASE=2_4						,						__SCREAMING_SNAKE_CASE=2						,						__SCREAMING_SNAKE_CASE=6						,						__SCREAMING_SNAKE_CASE=3_7						,						__SCREAMING_SNAKE_CASE="gelu"						,						__SCREAMING_SNAKE_CASE=0.1						,						__SCREAMING_SNAKE_CASE=0.1						,						__SCREAMING_SNAKE_CASE=5_1_2						,						__SCREAMING_SNAKE_CASE=1_6						,						__SCREAMING_SNAKE_CASE=2						,						__SCREAMING_SNAKE_CASE=0.02						,						__SCREAMING_SNAKE_CASE=3						,						__SCREAMING_SNAKE_CASE=None						,						__SCREAMING_SNAKE_CASE=1_0_0_0						,						)  ->   List[str]:




           """simple docstring"""







           UpperCamelCase__    :   Dict					=				parent
           UpperCamelCase__    :   Union[str, Any]					=				batch_size
           UpperCamelCase__    :   Optional[int]					=				seq_length
           UpperCamelCase__    :   Optional[Any]					=				is_training
           UpperCamelCase__    :   int					=				use_input_mask
           UpperCamelCase__    :   Dict					=				use_token_type_ids
           UpperCamelCase__    :   Tuple					=				use_labels
           UpperCamelCase__    :   str					=				vocab_size
           UpperCamelCase__    :   Union[str, Any]					=				hidden_size
           UpperCamelCase__    :   Optional[Any]					=				num_hidden_layers
           UpperCamelCase__    :   List[Any]					=				num_attention_heads
           UpperCamelCase__    :   Any					=				intermediate_size
           UpperCamelCase__    :   Any					=				hidden_act
           UpperCamelCase__    :   Dict					=				hidden_dropout_prob
           UpperCamelCase__    :   str					=				attention_probs_dropout_prob
           UpperCamelCase__    :   Dict					=				max_position_embeddings
           UpperCamelCase__    :   str					=				type_vocab_size
           UpperCamelCase__    :   Union[str, Any]					=				type_sequence_label_size
           UpperCamelCase__    :   Optional[Any]					=				initializer_range
           UpperCamelCase__    :   List[str]					=				num_labels
           UpperCamelCase__    :   Optional[int]					=				scope
           UpperCamelCase__    :   str					=				range_bbox






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   Union[str, Any]:




           """simple docstring"""







           UpperCamelCase__    :   Union[str, Any]					=				ids_tensor([self.batch_size, self.seq_length]						,						self.vocab_size  )

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

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

           UpperCamelCase__    :   Optional[int]					=				None
           if self.use_token_type_ids:
                 UpperCamelCase__    :   List[str]					=				ids_tensor([self.batch_size, self.seq_length]						,						self.type_vocab_size  )

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

           UpperCamelCase__    :   List[str]					=				self.get_config()

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






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   List[Any]:




           """simple docstring"""







           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    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						)  ->   List[Any]:




           """simple docstring"""







           UpperCamelCase__    :   Dict					=				LiltModel(config=__SCREAMING_SNAKE_CASE  )
           model.to(__SCREAMING_SNAKE_CASE  )
           model.eval()
           UpperCamelCase__    :   Dict					=				model(__SCREAMING_SNAKE_CASE						,						bbox=__SCREAMING_SNAKE_CASE						,						attention_mask=__SCREAMING_SNAKE_CASE						,						token_type_ids=__SCREAMING_SNAKE_CASE  )
           UpperCamelCase__    :   Any					=				model(__SCREAMING_SNAKE_CASE						,						bbox=__SCREAMING_SNAKE_CASE						,						token_type_ids=__SCREAMING_SNAKE_CASE  )
           UpperCamelCase__    :   Any					=				model(__SCREAMING_SNAKE_CASE						,						bbox=__SCREAMING_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    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						)  ->   int:




           """simple docstring"""







           UpperCamelCase__    :   Optional[Any]					=				self.num_labels
           UpperCamelCase__    :   int					=				LiltForTokenClassification(config=__SCREAMING_SNAKE_CASE  )
           model.to(__SCREAMING_SNAKE_CASE  )
           model.eval()
           UpperCamelCase__    :   Any					=				model(
               __SCREAMING_SNAKE_CASE						,						bbox=__SCREAMING_SNAKE_CASE						,						attention_mask=__SCREAMING_SNAKE_CASE						,						token_type_ids=__SCREAMING_SNAKE_CASE						,						labels=__SCREAMING_SNAKE_CASE  )
           self.parent.assertEqual(result.logits.shape						,						(self.batch_size, self.seq_length, self.num_labels)  )






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						)  ->   Dict:




           """simple docstring"""







           UpperCamelCase__    :   Tuple					=				LiltForQuestionAnswering(config=__SCREAMING_SNAKE_CASE  )
           model.to(__SCREAMING_SNAKE_CASE  )
           model.eval()
           UpperCamelCase__    :   Tuple					=				model(
               __SCREAMING_SNAKE_CASE						,						bbox=__SCREAMING_SNAKE_CASE						,						attention_mask=__SCREAMING_SNAKE_CASE						,						token_type_ids=__SCREAMING_SNAKE_CASE						,						start_positions=__SCREAMING_SNAKE_CASE						,						end_positions=__SCREAMING_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    __SCREAMING_SNAKE_CASE			(							self  )  ->   str:




           """simple docstring"""







           UpperCamelCase__    :   int					=				self.prepare_config_and_inputs()
           (
               (
               UpperCamelCase__
           )				,(
               UpperCamelCase__
           )				,(
               UpperCamelCase__
           )				,(
               UpperCamelCase__
           )				,(
               UpperCamelCase__
           )				,(
               UpperCamelCase__
           )				,(
               UpperCamelCase__
           )				,
           )    :   Union[str, Any]					=				config_and_inputs
           UpperCamelCase__    :   Union[str, Any]					=				{
               '''input_ids''': input_ids,
               '''bbox''': bbox,
               '''token_type_ids''': token_type_ids,
               '''attention_mask''': input_mask,
           }
           return config, inputs_dict




@require_torch
class 				_lowerCamelCase (      UpperCamelCase_	,							UpperCamelCase_	,							UpperCamelCase_	,							unittest.TestCase ):





     """simple docstring"""







     SCREAMING_SNAKE_CASE_		   =  (
         (
             LiltModel,
             LiltForSequenceClassification,
             LiltForTokenClassification,
             LiltForQuestionAnswering,
         )
         if is_torch_available()
         else ()
     )
     SCREAMING_SNAKE_CASE_		   =  (
         {
             '''feature-extraction''': LiltModel,
             '''question-answering''': LiltForQuestionAnswering,
             '''text-classification''': LiltForSequenceClassification,
             '''token-classification''': LiltForTokenClassification,
             '''zero-shot''': LiltForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     SCREAMING_SNAKE_CASE_		   =  False
     SCREAMING_SNAKE_CASE_		   =  False






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE  )  ->   List[Any]:




           """simple docstring"""







           return True






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   Union[str, Any]:




           """simple docstring"""







           UpperCamelCase__    :   int					=				LiltModelTester(self  )
           UpperCamelCase__    :   List[str]					=				ConfigTester(self						,						config_class=__SCREAMING_SNAKE_CASE						,						hidden_size=3_7  )






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   Union[str, Any]:




           """simple docstring"""







           self.config_tester.run_common_tests()






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   List[Any]:




           """simple docstring"""







           UpperCamelCase__    :   int					=				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE  )






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   Any:




           """simple docstring"""







           UpperCamelCase__    :   Any					=				self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
                 UpperCamelCase__    :   Any					=				type
                 self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE  )






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   Optional[Any]:




           """simple docstring"""







           UpperCamelCase__    :   Union[str, Any]					=				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE  )






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   Dict:




           """simple docstring"""







           UpperCamelCase__    :   Any					=				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE  )






     @slow
     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   Dict:




           """simple docstring"""







           for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 UpperCamelCase__    :   Dict					=				LiltModel.from_pretrained(__SCREAMING_SNAKE_CASE  )
                 self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )




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





     """simple docstring"""







     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   List[str]:




           """simple docstring"""







           UpperCamelCase__    :   List[str]					=				LiltModel.from_pretrained('''SCUT-DLVCLab/lilt-roberta-en-base'''  ).to(__SCREAMING_SNAKE_CASE  )

           UpperCamelCase__    :   Optional[int]					=				torch.tensor([[1, 2]]						,						device=__SCREAMING_SNAKE_CASE  )
           UpperCamelCase__    :   int					=				torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]						,						device=__SCREAMING_SNAKE_CASE  )

           # forward pass
           with torch.no_grad():
                 UpperCamelCase__    :   str					=				model(input_ids=__SCREAMING_SNAKE_CASE						,						bbox=__SCREAMING_SNAKE_CASE  )

           UpperCamelCase__    :   Optional[Any]					=				torch.Size([1, 2, 7_6_8]  )
           UpperCamelCase__    :   Dict					=				torch.tensor(
               [[-0.0653, 0.0950, -0.0061], [-0.0545, 0.0926, -0.0324]]						,						device=__SCREAMING_SNAKE_CASE						,						)

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




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


lowerCamelCase      =logging.get_logger(__name__)

lowerCamelCase      ={
    "google/realm-cc-news-pretrained-embedder": (
        "https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/config.json"
    ),
    "google/realm-cc-news-pretrained-encoder": (
        "https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/config.json"
    ),
    "google/realm-cc-news-pretrained-scorer": (
        "https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/config.json"
    ),
    "google/realm-cc-news-pretrained-openqa": (
        "https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/config.json"
    ),
    "google/realm-orqa-nq-openqa": "https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/config.json",
    "google/realm-orqa-nq-reader": "https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/config.json",
    "google/realm-orqa-wq-openqa": "https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/config.json",
    "google/realm-orqa-wq-reader": "https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/config.json",
    # See all REALM models at https://huggingface.co/models?filter=realm
}




class 				_lowerCamelCase (      UpperCamelCase_ ):





     """simple docstring"""







     SCREAMING_SNAKE_CASE_		   =  '''realm'''






     def __init__(							self						,						__SCREAMING_SNAKE_CASE=3_0_5_2_2						,						__SCREAMING_SNAKE_CASE=7_6_8						,						__SCREAMING_SNAKE_CASE=1_2_8						,						__SCREAMING_SNAKE_CASE=1_2						,						__SCREAMING_SNAKE_CASE=1_2						,						__SCREAMING_SNAKE_CASE=8						,						__SCREAMING_SNAKE_CASE=3_0_7_2						,						__SCREAMING_SNAKE_CASE="gelu_new"						,						__SCREAMING_SNAKE_CASE=0.1						,						__SCREAMING_SNAKE_CASE=0.1						,						__SCREAMING_SNAKE_CASE=5_1_2						,						__SCREAMING_SNAKE_CASE=2						,						__SCREAMING_SNAKE_CASE=0.02						,						__SCREAMING_SNAKE_CASE=1e-12						,						__SCREAMING_SNAKE_CASE=2_5_6						,						__SCREAMING_SNAKE_CASE=1_0						,						__SCREAMING_SNAKE_CASE=1e-3						,						__SCREAMING_SNAKE_CASE=5						,						__SCREAMING_SNAKE_CASE=3_2_0						,						__SCREAMING_SNAKE_CASE=1_3_3_5_3_7_1_8						,						__SCREAMING_SNAKE_CASE=5_0_0_0						,						__SCREAMING_SNAKE_CASE=1						,						__SCREAMING_SNAKE_CASE=0						,						__SCREAMING_SNAKE_CASE=2						,						**__SCREAMING_SNAKE_CASE						,						)  ->   List[str]:




           """simple docstring"""







           super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE						,						bos_token_id=__SCREAMING_SNAKE_CASE						,						eos_token_id=__SCREAMING_SNAKE_CASE						,						**__SCREAMING_SNAKE_CASE  )

           # Common config
           UpperCamelCase__    :   int					=				vocab_size
           UpperCamelCase__    :   Any					=				max_position_embeddings
           UpperCamelCase__    :   List[str]					=				hidden_size
           UpperCamelCase__    :   Union[str, Any]					=				retriever_proj_size
           UpperCamelCase__    :   int					=				num_hidden_layers
           UpperCamelCase__    :   Union[str, Any]					=				num_attention_heads
           UpperCamelCase__    :   str					=				num_candidates
           UpperCamelCase__    :   int					=				intermediate_size
           UpperCamelCase__    :   Dict					=				hidden_act
           UpperCamelCase__    :   Any					=				hidden_dropout_prob
           UpperCamelCase__    :   Any					=				attention_probs_dropout_prob
           UpperCamelCase__    :   Any					=				initializer_range
           UpperCamelCase__    :   Optional[Any]					=				type_vocab_size
           UpperCamelCase__    :   List[Any]					=				layer_norm_eps

           # Reader config
           UpperCamelCase__    :   List[Any]					=				span_hidden_size
           UpperCamelCase__    :   List[Any]					=				max_span_width
           UpperCamelCase__    :   Optional[Any]					=				reader_layer_norm_eps
           UpperCamelCase__    :   Optional[Any]					=				reader_beam_size
           UpperCamelCase__    :   int					=				reader_seq_len

           # Retrieval config
           UpperCamelCase__    :   List[str]					=				num_block_records
           UpperCamelCase__    :   Union[str, Any]					=				searcher_beam_size


