





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







       """simple docstring"""





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







       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					Optional[int]:
         _lowercase							= ids_tensor([self.batch_size, self.seq_length]						,self.vocab_size  )

         _lowercase							= 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]:
               _lowercase							= bbox[i, j, 3]
               _lowercase							= bbox[i, j, 1]
               _lowercase							= t
             if bbox[i, j, 2] < bbox[i, j, 0]:
               _lowercase							= bbox[i, j, 2]
               _lowercase							= bbox[i, j, 0]
               _lowercase							= t

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

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

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

         _lowercase							= self.get_config()

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







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

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







       def    __UpperCAmelCase (    self	:	str						,__A	:	List[Any]						,__A	:	int						,__A	:	Dict						,__A	:	Dict						,__A	:	Optional[Any]						,__A	:	str						,__A	:	Union[str, Any]						,)						->					str:
         _lowercase							= LiltForQuestionAnswering(config=__A  )
         model.to(__A  )
         model.eval()
         _lowercase							= model(
             __A						,bbox=__A						,attention_mask=__A						,token_type_ids=__A						,start_positions=__A						,end_positions=__A						,)
         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	:	Optional[int]  )						->					int:
         _lowercase							= self.prepare_config_and_inputs()
         (
             (
             _lowercase
         )						,		(
             _lowercase
         )						,		(
             _lowercase
         )						,		(
             _lowercase
         )						,		(
             _lowercase
         )						,		(
             _lowercase
         )						,		(
             _lowercase
         )						,		
         )							= config_and_inputs
         _lowercase							= {
             'input_ids': input_ids,
             'bbox': bbox,
             'token_type_ids': token_type_ids,
             'attention_mask': input_mask,
         }
         return config, inputs_dict





@require_torch
class 				A_				(      UpperCAmelCase					,	UpperCAmelCase					,	UpperCAmelCase					,	unittest.TestCase	):







       """simple docstring"""





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







       def    __UpperCAmelCase (    self	:	Dict						,__A	:	Dict						,__A	:	Dict						,__A	:	Union[str, Any]						,__A	:	Any						,__A	:	int  )						->					int:
         return True







       def    __UpperCAmelCase (    self	:	int  )						->					int:
         _lowercase							= LiltModelTester(self  )
         _lowercase							= ConfigTester(self						,config_class=__A						,hidden_size=37  )







       def    __UpperCAmelCase (    self	:	Tuple  )						->					Optional[int]:
         self.config_tester.run_common_tests()







       def    __UpperCAmelCase (    self	:	Optional[int]  )						->					Tuple:
         _lowercase							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__A  )







       def    __UpperCAmelCase (    self	:	Tuple  )						->					Any:
         _lowercase							= self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
           _lowercase							= type
           self.model_tester.create_and_check_model(*__A  )







       def    __UpperCAmelCase (    self	:	int  )						->					Optional[int]:
         _lowercase							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*__A  )







       def    __UpperCAmelCase (    self	:	Tuple  )						->					List[Any]:
         _lowercase							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*__A  )







       @slow
       def    __UpperCAmelCase (    self	:	Tuple  )						->					Optional[Any]:
         for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowercase							= LiltModel.from_pretrained(__A  )
           self.assertIsNotNone(__A  )





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







       """simple docstring"""





       def    __UpperCAmelCase (    self	:	Tuple  )						->					Union[str, Any]:
         _lowercase							= LiltModel.from_pretrained('SCUT-DLVCLab/lilt-roberta-en-base'  ).to(__A  )

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

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

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

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






from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


snake_case 				=       logging.get_logger(__name__)

snake_case 				=       {
    """EleutherAI/gpt-j-6B""": """https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json""",
    # See all GPT-J models at https://huggingface.co/models?filter=gpt_j
}





class 				A_				(      UpperCAmelCase	):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : str      =  '''gptj'''
       SCREAMING_SNAKE_CASE_      : int      =  {
           '''max_position_embeddings''': '''n_positions''',
           '''hidden_size''': '''n_embd''',
           '''num_attention_heads''': '''n_head''',
           '''num_hidden_layers''': '''n_layer''',
       }







       def __init__(    self	:	str						,__A	:	List[str]=5_0400						,__A	:	Dict=2048						,__A	:	List[str]=4096						,__A	:	Dict=28						,__A	:	Optional[int]=16						,__A	:	Tuple=64						,__A	:	Optional[int]=None						,__A	:	Optional[int]="gelu_new"						,__A	:	Dict=0.0						,__A	:	List[str]=0.0						,__A	:	Optional[int]=0.0						,__A	:	str=1e-5						,__A	:	List[Any]=0.02						,__A	:	str=True						,__A	:	int=5_0256						,__A	:	Union[str, Any]=5_0256						,__A	:	int=False						,**__A	:	List[Any]						,)						->					str:
         _lowercase							= vocab_size
         _lowercase							= n_positions
         _lowercase							= n_embd
         _lowercase							= n_layer
         _lowercase							= n_head
         _lowercase							= n_inner
         _lowercase							= rotary_dim
         _lowercase							= activation_function
         _lowercase							= resid_pdrop
         _lowercase							= embd_pdrop
         _lowercase							= attn_pdrop
         _lowercase							= layer_norm_epsilon
         _lowercase							= initializer_range
         _lowercase							= use_cache

         _lowercase							= bos_token_id
         _lowercase							= eos_token_id

         super().__init__(
             bos_token_id=__A						,eos_token_id=__A						,tie_word_embeddings=__A						,**__A  )





class 				A_				(      UpperCAmelCase	):







       """simple docstring"""





       def __init__(    self	:	Tuple						,__A	:	PretrainedConfig						,__A	:	str = "default"						,__A	:	List[PatchingSpec] = None						,__A	:	bool = False						,)						->					Any:
         super().__init__(__A						,task=__A						,patching_specs=__A						,use_past=__A  )
         if not getattr(self._config						,'pad_token_id'						,__A  ):
           # TODO: how to do that better?
           _lowercase							= 0







       @property
       def    __UpperCAmelCase (    self	:	Union[str, Any]  )						->					Mapping[str, Mapping[int, str]]:
         _lowercase							= OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}}  )
         if self.use_past:
           self.fill_with_past_key_values_(__A						,direction='inputs'  )
           _lowercase							= {0: 'batch', 1: 'past_sequence + sequence'}
         else:
           _lowercase							= {0: 'batch', 1: 'sequence'}

         return common_inputs







       @property
       def    __UpperCAmelCase (    self	:	Union[str, Any]  )						->					int:
         return self._config.n_layer







       @property
       def    __UpperCAmelCase (    self	:	Tuple  )						->					int:
         return self._config.n_head







       def    __UpperCAmelCase (    self	:	Any						,__A	:	PreTrainedTokenizer						,__A	:	int = -1						,__A	:	int = -1						,__A	:	bool = False						,__A	:	Optional[TensorType] = None						,)						->					Mapping[str, Any]:
         _lowercase							= super(__A						,self  ).generate_dummy_inputs(
             __A						,batch_size=__A						,seq_length=__A						,is_pair=__A						,framework=__A  )

         # We need to order the input in the way they appears in the forward()
         _lowercase							= OrderedDict({'input_ids': common_inputs['input_ids']}  )

         # Need to add the past_keys
         if self.use_past:
           if not is_torch_available():
             raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'  )
           else:
             import torch

             _lowercase						,		_lowercase							= common_inputs['input_ids'].shape
             # Not using the same length for past_key_values
             _lowercase							= seqlen + 2
             _lowercase							= (
                 batch,
                 self.num_attention_heads,
                 past_key_values_length,
                 self._config.hidden_size // self.num_attention_heads,
             )
             _lowercase							= [
                 (torch.zeros(__A  ), torch.zeros(__A  )) for _ in range(self.num_layers  )
             ]

         _lowercase							= common_inputs['attention_mask']
         if self.use_past:
           _lowercase							= ordered_inputs['attention_mask'].dtype
           _lowercase							= torch.cat(
               [ordered_inputs['attention_mask'], torch.ones(__A						,__A						,dtype=__A  )]						,dim=1  )

         return ordered_inputs







       @property
       def    __UpperCAmelCase (    self	:	str  )						->					int:
         return 13