
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			: List[str] ,    snake_case_			: Dict ,    snake_case_			: Dict=1_3 ,    snake_case_			: List[Any]=7 ,    snake_case_			: Any=True ,    snake_case_			: Union[str, Any]=True ,    snake_case_			: Dict=True ,    snake_case_			: Optional[int]=True ,    snake_case_			: Union[str, Any]=9_9 ,    snake_case_			: List[str]=2_4 ,    snake_case_			: str=2 ,    snake_case_			: List[str]=6 ,    snake_case_			: Optional[int]=3_7 ,    snake_case_			: int="gelu" ,    snake_case_			: Optional[int]=0.1 ,    snake_case_			: Optional[int]=0.1 ,    snake_case_			: List[Any]=5_1_2 ,    snake_case_			: Any=1_6 ,    snake_case_			: Any=2 ,    snake_case_			: str=0.02 ,    snake_case_			: List[Any]=3 ,    snake_case_			: List[Any]=None ,    snake_case_			: Tuple=1_0_0_0 ,    ):
    __a			:  Dict		    =  parent
    __a			:  Tuple		    =  batch_size
    __a			:  Dict		    =  seq_length
    __a			:  str		    =  is_training
    __a			:  int		    =  use_input_mask
    __a			:  Optional[Any]		    =  use_token_type_ids
    __a			:  Any		    =  use_labels
    __a			:  int		    =  vocab_size
    __a			:  str		    =  hidden_size
    __a			:  str		    =  num_hidden_layers
    __a			:  Any		    =  num_attention_heads
    __a			:  List[str]		    =  intermediate_size
    __a			:  List[Any]		    =  hidden_act
    __a			:  int		    =  hidden_dropout_prob
    __a			:  Optional[Any]		    =  attention_probs_dropout_prob
    __a			:  Optional[Any]		    =  max_position_embeddings
    __a			:  List[Any]		    =  type_vocab_size
    __a			:  Dict		    =  type_sequence_label_size
    __a			:  List[str]		    =  initializer_range
    __a			:  List[Any]		    =  num_labels
    __a			:  List[Any]		    =  scope
    __a			:  Dict		    =  range_bbox
   def 				lowerCAmelCase      (self			: Tuple       ):
    __a			:  str		    =  ids_tensor([self.batch_size, self.seq_length] ,    self.vocab_size       )

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

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

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

    __a			:  Dict		    =  None
    __a			:  Union[str, Any]		    =  None
    if self.use_labels:
     __a			:  Tuple		    =  ids_tensor([self.batch_size] ,    self.type_sequence_label_size       )
     __a			:  List[Any]		    =  ids_tensor([self.batch_size, self.seq_length] ,    self.num_labels       )

    __a			:  List[Any]		    =  self.get_config()

    return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels
   def 				lowerCAmelCase      (self			: Tuple       ):
    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			: Union[str, Any] ,    snake_case_			: Dict ,    snake_case_			: List[Any] ,    snake_case_			: List[str] ,    snake_case_			: Union[str, Any] ,    snake_case_			: Optional[Any] ,    snake_case_			: str ,    snake_case_			: Any ,    ):
    __a			:  Tuple		    =  LiltModel(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  Dict		    =  model(snake_case_ ,    bbox=snake_case_ ,    attention_mask=snake_case_ ,    token_type_ids=snake_case_       )
    __a			:  List[str]		    =  model(snake_case_ ,    bbox=snake_case_ ,    token_type_ids=snake_case_       )
    __a			:  List[str]		    =  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			: Tuple ,    snake_case_			: Any ,    snake_case_			: Dict ,    snake_case_			: Union[str, Any] ,    snake_case_			: List[str] ,    snake_case_			: str ,    snake_case_			: Optional[int] ,    snake_case_			: str ,    ):
    __a			:  Optional[int]		    =  self.num_labels
    __a			:  Tuple		    =  LiltForTokenClassification(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  Optional[int]		    =  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[Any] ,    snake_case_			: Dict ,    snake_case_			: int ,    snake_case_			: Optional[Any] ,    snake_case_			: Any ,    snake_case_			: List[str] ,    snake_case_			: Optional[Any] ,    snake_case_			: Any ,    ):
    __a			:  Tuple		    =  LiltForQuestionAnswering(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  Union[str, 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			: Dict       ):
    __a			:  Any		    =  self.prepare_config_and_inputs()
    (
        (
        __a
    )    ,	(
        __a
    )    ,	(
        __a
    )    ,	(
        __a
    )    ,	(
        __a
    )    ,	(
        __a
    )    ,	(
        __a
    )    ,	
    )			:  int		    =  config_and_inputs
    __a			:  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__  (	__lowercase   ,__lowercase   ,__lowercase   ,unittest.TestCase   ):
   _SCREAMING_SNAKE_CASE				:							Any     	=				(
       (
           LiltModel,
           LiltForSequenceClassification,
           LiltForTokenClassification,
           LiltForQuestionAnswering,
       )
       if is_torch_available()
       else ()
   )
   _SCREAMING_SNAKE_CASE				:							Optional[Any]     	=				(
       {
           "feature-extraction": LiltModel,
           "question-answering": LiltForQuestionAnswering,
           "text-classification": LiltForSequenceClassification,
           "token-classification": LiltForTokenClassification,
           "zero-shot": LiltForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   _SCREAMING_SNAKE_CASE				:							Dict     	=				False
   _SCREAMING_SNAKE_CASE				:							Optional[int]     	=				False
   def 				lowerCAmelCase      (self			: str ,    snake_case_			: Optional[Any] ,    snake_case_			: Dict ,    snake_case_			: Optional[Any] ,    snake_case_			: Optional[int] ,    snake_case_			: Union[str, Any]       ):
    return True
   def 				lowerCAmelCase      (self			: List[Any]       ):
    __a			:  Dict		    =  LiltModelTester(self       )
    __a			:  Union[str, Any]		    =  ConfigTester(self ,    config_class=snake_case_ ,    hidden_size=3_7       )
   def 				lowerCAmelCase      (self			: str       ):
    self.config_tester.run_common_tests()
   def 				lowerCAmelCase      (self			: int       ):
    __a			:  Optional[Any]		    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*snake_case_       )
   def 				lowerCAmelCase      (self			: Tuple       ):
    __a			:  Dict		    =  self.model_tester.prepare_config_and_inputs()
    for type in ["absolute", "relative_key", "relative_key_query"]:
     __a			:  List[str]		    =  type
     self.model_tester.create_and_check_model(*snake_case_       )
   def 				lowerCAmelCase      (self			: int       ):
    __a			:  Optional[Any]		    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_token_classification(*snake_case_       )
   def 				lowerCAmelCase      (self			: Any       ):
    __a			:  int		    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_question_answering(*snake_case_       )







   @slow
   def 				lowerCAmelCase      (self			: Dict       ):
    for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     __a			:  Union[str, Any]		    =  LiltModel.from_pretrained(snake_case_       )
     self.assertIsNotNone(snake_case_       )





@require_torch
@slow
class 		UpperCamelCase__  (	unittest.TestCase   ):
   def 				lowerCAmelCase      (self			: Dict       ):
    __a			:  int		    =  LiltModel.from_pretrained('''SCUT-DLVCLab/lilt-roberta-en-base'''       ).to(snake_case_       )

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

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

    __a			:  Tuple		    =  torch.Size([1, 2, 7_6_8]       )
    __a			:  Optional[Any]		    =  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 typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


lowercase__          ={
    'configuration_data2vec_audio': ['DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Data2VecAudioConfig'],
    'configuration_data2vec_text': [
        'DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'Data2VecTextConfig',
        'Data2VecTextOnnxConfig',
    ],
    'configuration_data2vec_vision': [
        'DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'Data2VecVisionConfig',
        'Data2VecVisionOnnxConfig',
    ],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase__          =[
          'DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST',
          'Data2VecAudioForAudioFrameClassification',
          'Data2VecAudioForCTC',
          'Data2VecAudioForSequenceClassification',
          'Data2VecAudioForXVector',
          'Data2VecAudioModel',
          'Data2VecAudioPreTrainedModel',
      ]
      lowercase__          =[
          'DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST',
          'Data2VecTextForCausalLM',
          'Data2VecTextForMaskedLM',
          'Data2VecTextForMultipleChoice',
          'Data2VecTextForQuestionAnswering',
          'Data2VecTextForSequenceClassification',
          'Data2VecTextForTokenClassification',
          'Data2VecTextModel',
          'Data2VecTextPreTrainedModel',
      ]
      lowercase__          =[
          'DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST',
          'Data2VecVisionForImageClassification',
          'Data2VecVisionForMaskedImageModeling',
          'Data2VecVisionForSemanticSegmentation',
          'Data2VecVisionModel',
          'Data2VecVisionPreTrainedModel',
      ]

if is_tf_available():
      lowercase__          =[
          'TFData2VecVisionForImageClassification',
          'TFData2VecVisionForSemanticSegmentation',
          'TFData2VecVisionModel',
          'TFData2VecVisionPreTrainedModel',
      ]

if TYPE_CHECKING:
      from .configuration_dataavec_audio import DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecAudioConfig
      from .configuration_dataavec_text import (
          DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP,
          DataaVecTextConfig,
          DataaVecTextOnnxConfig,
      )
      from .configuration_dataavec_vision import (
          DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP,
          DataaVecVisionConfig,
          DataaVecVisionOnnxConfig,
      )

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_dataavec_audio import (
                DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST,
                DataaVecAudioForAudioFrameClassification,
                DataaVecAudioForCTC,
                DataaVecAudioForSequenceClassification,
                DataaVecAudioForXVector,
                DataaVecAudioModel,
                DataaVecAudioPreTrainedModel,
            )
            from .modeling_dataavec_text import (
                DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
                DataaVecTextForCausalLM,
                DataaVecTextForMaskedLM,
                DataaVecTextForMultipleChoice,
                DataaVecTextForQuestionAnswering,
                DataaVecTextForSequenceClassification,
                DataaVecTextForTokenClassification,
                DataaVecTextModel,
                DataaVecTextPreTrainedModel,
            )
            from .modeling_dataavec_vision import (
                DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST,
                DataaVecVisionForImageClassification,
                DataaVecVisionForMaskedImageModeling,
                DataaVecVisionForSemanticSegmentation,
                DataaVecVisionModel,
                DataaVecVisionPreTrainedModel,
            )
      if is_tf_available():
            from .modeling_tf_dataavec_vision import (
                TFDataaVecVisionForImageClassification,
                TFDataaVecVisionForSemanticSegmentation,
                TFDataaVecVisionModel,
                TFDataaVecVisionPreTrainedModel,
            )

else:
      import sys

      lowercase__          =_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
