





'''simple docstring'''


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





     def __init__( self		:					List[str]	,      lowercase		:					Union[str, Any]	,      lowercase		:					Optional[Any]=13	,      lowercase		:					Optional[int]=7	,      lowercase		:					Any=True	,      lowercase		:					List[Any]=True	,      lowercase		:					Tuple=True	,      lowercase		:					Any=True	,      lowercase		:					int=99	,      lowercase		:					str=24	,      lowercase		:					Dict=2	,      lowercase		:					int=6	,      lowercase		:					Dict=37	,      lowercase		:					List[str]="gelu"	,      lowercase		:					str=0.1	,      lowercase		:					Union[str, Any]=0.1	,      lowercase		:					Union[str, Any]=512	,      lowercase		:					Dict=16	,      lowercase		:					int=2	,      lowercase		:					int=0.02	,      lowercase		:					Tuple=3	,      lowercase		:					Any=None	,      lowercase		:					List[Any]=1_000	,      ):





        """simple docstring"""




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





     def 							lowercase__      ( self		:					Union[str, Any]						):





        """simple docstring"""




        lowercase_			:int    			= ids_tensor([self.batch_size, self.seq_length]	,      self.vocab_size						)

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

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

        lowercase_			:Optional[Any]    			= None
        if self.use_token_type_ids:
           lowercase_			:Tuple    			= ids_tensor([self.batch_size, self.seq_length]	,      self.type_vocab_size						)

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

        lowercase_			:Dict    			= self.get_config()

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





     def 							lowercase__      ( self		:					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 							lowercase__      ( self		:					Any	,      lowercase		:					Dict	,      lowercase		:					Any	,      lowercase		:					Optional[Any]	,      lowercase		:					List[Any]	,      lowercase		:					Dict	,      lowercase		:					Tuple	,      lowercase		:					List[str]	,      ):





        """simple docstring"""




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





        """simple docstring"""




        lowercase_			:int    			= self.num_labels
        lowercase_			:Optional[Any]    			= LiltForTokenClassification(config=lowercase						)
        model.to(lowercase						)
        model.eval()
        lowercase_			:Union[str, Any]    			= 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 							lowercase__      ( self		:					str	,      lowercase		:					str	,      lowercase		:					List[str]	,      lowercase		:					Tuple	,      lowercase		:					Optional[int]	,      lowercase		:					int	,      lowercase		:					int	,      lowercase		:					List[Any]	,      ):





        """simple docstring"""




        lowercase_			:str    			= LiltForQuestionAnswering(config=lowercase						)
        model.to(lowercase						)
        model.eval()
        lowercase_			:str    			= 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 							lowercase__      ( self		:					Optional[Any]						):





        """simple docstring"""




        lowercase_			:Any    			= self.prepare_config_and_inputs()
        (
            (
            lowercase_
        )			,							(
            lowercase_
        )			,							(
            lowercase_
        )			,							(
            lowercase_
        )			,							(
            lowercase_
        )			,							(
            lowercase_
        )			,							(
            lowercase_
        )			,							
        )			:List[Any]    			= config_and_inputs
        lowercase_			:Tuple    			= {
            "input_ids": input_ids,
            "bbox": bbox,
            "token_type_ids": token_type_ids,
            "attention_mask": input_mask,
        }
        return config, inputs_dict







@require_torch
class       a_   (				_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						unittest.TestCase							):
     __A      =			(
         (
             LiltModel,
             LiltForSequenceClassification,
             LiltForTokenClassification,
             LiltForQuestionAnswering,
         )
         if is_torch_available()
         else ()
     )
     __A      =			(
         {
             "feature-extraction": LiltModel,
             "question-answering": LiltForQuestionAnswering,
             "text-classification": LiltForSequenceClassification,
             "token-classification": LiltForTokenClassification,
             "zero-shot": LiltForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     __A      =			False
     __A      =			False





     def 							lowercase__      ( self		:					int	,      lowercase		:					Any	,      lowercase		:					List[str]	,      lowercase		:					List[str]	,      lowercase		:					Dict	,      lowercase		:					Optional[Any]						):





        """simple docstring"""




        return True





     def 							lowercase__      ( self		:					int						):





        """simple docstring"""




        lowercase_			:List[Any]    			= LiltModelTester(self						)
        lowercase_			:Optional[int]    			= ConfigTester(self	,      config_class=lowercase	,      hidden_size=37						)





     def 							lowercase__      ( self		:					Tuple						):





        """simple docstring"""




        self.config_tester.run_common_tests()





     def 							lowercase__      ( self		:					List[str]						):





        """simple docstring"""




        lowercase_			:Tuple    			= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowercase						)





     def 							lowercase__      ( self		:					Dict						):





        """simple docstring"""




        lowercase_			:str    			= self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
           lowercase_			:Optional[int]    			= type
           self.model_tester.create_and_check_model(*lowercase						)





     def 							lowercase__      ( self		:					Optional[int]						):





        """simple docstring"""




        lowercase_			:Any    			= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*lowercase						)





     def 							lowercase__      ( self		:					str						):





        """simple docstring"""




        lowercase_			:str    			= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*lowercase						)







     @slow
     def 							lowercase__      ( self		:					str						):





        """simple docstring"""




        for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase_			:List[str]    			= LiltModel.from_pretrained(lowercase						)
           self.assertIsNotNone(lowercase						)







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





     def 							lowercase__      ( self		:					Optional[int]						):





        """simple docstring"""




        lowercase_			:Any    			= LiltModel.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base"						).to(lowercase						)

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

        # forward pass
        with torch.no_grad():
           lowercase_			:Dict    			= model(input_ids=lowercase	,      bbox=lowercase						)

        lowercase_			:int    			= torch.Size([1, 2, 768]						)
        lowercase_			:List[Any]    			= torch.tensor(
            [[-0.06_53, 0.09_50, -0.00_61], [-0.05_45, 0.09_26, -0.03_24]]	,      device=lowercase	,      )

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









'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowerCAmelCase					:     Optional[Any]												={
    '''configuration_x_clip''': [
        '''XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''XCLIPConfig''',
        '''XCLIPTextConfig''',
        '''XCLIPVisionConfig''',
    ],
    '''processing_x_clip''': ['''XCLIPProcessor'''],
}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCAmelCase					:     Tuple												=[
     '''XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
     '''XCLIPModel''',
     '''XCLIPPreTrainedModel''',
     '''XCLIPTextModel''',
     '''XCLIPVisionModel''',
 ]

if TYPE_CHECKING:
 from .configuration_x_clip import (
     XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
     XCLIPConfig,
     XCLIPTextConfig,
     XCLIPVisionConfig,
 )
 from .processing_x_clip import XCLIPProcessor

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_x_clip import (
      XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
      XCLIPModel,
      XCLIPPreTrainedModel,
      XCLIPTextModel,
      XCLIPVisionModel,
  )

else:
 import sys

 lowerCAmelCase					:     str												=_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


