






'''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 							__SCREAMING_SNAKE_CASE       :




      def __init__(  self ,   lowerCamelCase ,   lowerCamelCase=13 ,   lowerCamelCase=7 ,   lowerCamelCase=True ,   lowerCamelCase=True ,   lowerCamelCase=True ,   lowerCamelCase=True ,   lowerCamelCase=99 ,   lowerCamelCase=24 ,   lowerCamelCase=2 ,   lowerCamelCase=6 ,   lowerCamelCase=37 ,   lowerCamelCase="gelu" ,   lowerCamelCase=0.1 ,   lowerCamelCase=0.1 ,   lowerCamelCase=512 ,   lowerCamelCase=16 ,   lowerCamelCase=2 ,   lowerCamelCase=0.02 ,   lowerCamelCase=3 ,   lowerCamelCase=None ,   lowerCamelCase=1000 ,   )							->Optional[int]:



         '''simple docstring'''
         __a				     =    parent
         __a				     =    batch_size
         __a				     =    seq_length
         __a				     =    is_training
         __a				     =    use_input_mask
         __a				     =    use_token_type_ids
         __a				     =    use_labels
         __a				     =    vocab_size
         __a				     =    hidden_size
         __a				     =    num_hidden_layers
         __a				     =    num_attention_heads
         __a				     =    intermediate_size
         __a				     =    hidden_act
         __a				     =    hidden_dropout_prob
         __a				     =    attention_probs_dropout_prob
         __a				     =    max_position_embeddings
         __a				     =    type_vocab_size
         __a				     =    type_sequence_label_size
         __a				     =    initializer_range
         __a				     =    num_labels
         __a				     =    scope
         __a				     =    range_bbox




      def  __UpperCamelCase							(  self      )							->Optional[int]:



         '''simple docstring'''
         __a				     =    ids_tensor([self.batch_size, self.seq_length] ,   self.vocab_size      )

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

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

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

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

         __a				     =    self.get_config()

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




      def  __UpperCamelCase							(  self      )							->Union[str, 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  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   )							->Optional[Any]:



         '''simple docstring'''
         __a				     =    LiltModel(config=lowerCamelCase      )
         model.to(lowerCamelCase      )
         model.eval()
         __a				     =    model(lowerCamelCase ,   bbox=lowerCamelCase ,   attention_mask=lowerCamelCase ,   token_type_ids=lowerCamelCase      )
         __a				     =    model(lowerCamelCase ,   bbox=lowerCamelCase ,   token_type_ids=lowerCamelCase      )
         __a				     =    model(lowerCamelCase ,   bbox=lowerCamelCase      )

         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 ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   )							->str:



         '''simple docstring'''
         __a				     =    self.num_labels
         __a				     =    LiltForTokenClassification(config=lowerCamelCase      )
         model.to(lowerCamelCase      )
         model.eval()
         __a				     =    model(
             lowerCamelCase ,   bbox=lowerCamelCase ,   attention_mask=lowerCamelCase ,   token_type_ids=lowerCamelCase ,   labels=lowerCamelCase      )
         self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.seq_length, self.num_labels)      )




      def  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   )							->Tuple:



         '''simple docstring'''
         __a				     =    LiltForQuestionAnswering(config=lowerCamelCase      )
         model.to(lowerCamelCase      )
         model.eval()
         __a				     =    model(
             lowerCamelCase ,   bbox=lowerCamelCase ,   attention_mask=lowerCamelCase ,   token_type_ids=lowerCamelCase ,   start_positions=lowerCamelCase ,   end_positions=lowerCamelCase ,   )
         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      )							->Any:



         '''simple docstring'''
         __a				     =    self.prepare_config_and_inputs()
         (
             (
             __a
         )     ,			(
             __a
         )     ,			(
             __a
         )     ,			(
             __a
         )     ,			(
             __a
         )     ,			(
             __a
         )     ,			(
             __a
         )     ,			
         )				     =    config_and_inputs
         __a				     =    {
             'input_ids': input_ids,
             'bbox': bbox,
             'token_type_ids': token_type_ids,
             'attention_mask': input_mask,
         }
         return config, inputs_dict







@require_torch
class 							__SCREAMING_SNAKE_CASE       (							_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  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase      )							->Tuple:



         '''simple docstring'''
         return True




      def  __UpperCamelCase							(  self      )							->Tuple:



         '''simple docstring'''
         __a				     =    LiltModelTester(self      )
         __a				     =    ConfigTester(self ,   config_class=lowerCamelCase ,   hidden_size=37      )




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



         '''simple docstring'''
         self.config_tester.run_common_tests()




      def  __UpperCamelCase							(  self      )							->Optional[int]:



         '''simple docstring'''
         __a				     =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*lowerCamelCase      )




      def  __UpperCamelCase							(  self      )							->Tuple:



         '''simple docstring'''
         __a				     =    self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
            __a				     =    type
            self.model_tester.create_and_check_model(*lowerCamelCase      )




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



         '''simple docstring'''
         __a				     =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*lowerCamelCase      )




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



         '''simple docstring'''
         __a				     =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*lowerCamelCase      )






      @slow
      def  __UpperCamelCase							(  self      )							->Optional[int]:



         '''simple docstring'''
         for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            __a				     =    LiltModel.from_pretrained(lowerCamelCase      )
            self.assertIsNotNone(lowerCamelCase      )







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




      def  __UpperCamelCase							(  self      )							->str:



         '''simple docstring'''
         __a				     =    LiltModel.from_pretrained('SCUT-DLVCLab/lilt-roberta-en-base'      ).to(lowerCamelCase      )

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

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

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

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







'''simple docstring'''



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


__UpperCamelCase      :       Dict      			=      logging.get_logger(__name__)

__UpperCamelCase      :       Any      			=      {
    """google/vivit-b-16x2-kinetics400""": (
        """https://huggingface.co/google/vivit-b-16x2-kinetics400/resolve/main/config.json"""
    ),
    # See all Vivit models at https://huggingface.co/models?filter=vivit
}
class 							__SCREAMING_SNAKE_CASE       (							_lowerCAmelCase     ):
      __a  					="vivit"




      def __init__(  self ,   lowerCamelCase=224 ,   lowerCamelCase=32 ,   lowerCamelCase=[2, 16, 16] ,   lowerCamelCase=3 ,   lowerCamelCase=768 ,   lowerCamelCase=12 ,   lowerCamelCase=12 ,   lowerCamelCase=3072 ,   lowerCamelCase="gelu_fast" ,   lowerCamelCase=0.0 ,   lowerCamelCase=0.0 ,   lowerCamelCase=0.02 ,   lowerCamelCase=1e-06 ,   lowerCamelCase=True ,   **lowerCamelCase ,   )							->Tuple:



         '''simple docstring'''
         __a				     =    hidden_size
         __a				     =    num_hidden_layers
         __a				     =    num_attention_heads
         __a				     =    intermediate_size
         __a				     =    hidden_act
         __a				     =    hidden_dropout_prob
         __a				     =    attention_probs_dropout_prob
         __a				     =    initializer_range
         __a				     =    layer_norm_eps

         __a				     =    image_size
         __a				     =    num_frames
         __a				     =    tubelet_size
         __a				     =    num_channels
         __a				     =    qkv_bias

         super().__init__(**lowerCamelCase      )