
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    :       List[str]    ,  a    :       Tuple    ,  a    :       str=13    ,  a    :       List[Any]=7    ,  a    :       Dict=True    ,  a    :       Optional[int]=True    ,  a    :       Optional[int]=True    ,  a    :       Any=True    ,  a    :       Any=99    ,  a    :       Optional[int]=24    ,  a    :       Tuple=2    ,  a    :       int=6    ,  a    :       Dict=37    ,  a    :       List[str]="gelu"    ,  a    :       str=0.1    ,  a    :       List[Any]=0.1    ,  a    :       Any=512    ,  a    :       Union[str, Any]=16    ,  a    :       int=2    ,  a    :       Optional[Any]=0.02    ,  a    :       Any=3    ,  a    :       Tuple=None    ,  a    :       Optional[int]=1_000    ,  )->     Union[str, Any]:





     """simple docstring"""







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





     """simple docstring"""







     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  SCREAMING_SNAKE_CASE_							(					self    :       Optional[Any] )->     str:





     """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    :       Any    ,  a    :       int    ,  a    :       Optional[int]    ,  a    :       str    ,  a    :       Union[str, Any]    ,  a    :       str    ,  a    :       Dict    ,  a    :       Tuple    ,  )->     List[str]:





     """simple docstring"""







     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  SCREAMING_SNAKE_CASE_							(					self    :       str    ,  a    :       Dict    ,  a    :       Union[str, Any]    ,  a    :       Tuple    ,  a    :       Dict    ,  a    :       List[str]    ,  a    :       Dict    ,  a    :       int    ,  )->     Optional[Any]:





     """simple docstring"""







     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  SCREAMING_SNAKE_CASE_							(					self    :       int    ,  a    :       List[str]    ,  a    :       Dict    ,  a    :       Optional[int]    ,  a    :       Optional[int]    ,  a    :       Any    ,  a    :       Dict    ,  a    :       Dict    ,  )->     List[Any]:





     """simple docstring"""







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





     """simple docstring"""







     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        SCREAMING_SNAKE_CASE     (_lowercase    ,			_lowercase    ,			_lowercase    ,			unittest.TestCase   ):
   _UpperCamelCase					:	Union[str, Any]				      = (
       (
           LiltModel,
           LiltForSequenceClassification,
           LiltForTokenClassification,
           LiltForQuestionAnswering,
       )
       if is_torch_available()
       else ()
   )
   _UpperCamelCase					:	str				      = (
       {
           '''feature-extraction''': LiltModel,
           '''question-answering''': LiltForQuestionAnswering,
           '''text-classification''': LiltForSequenceClassification,
           '''token-classification''': LiltForTokenClassification,
           '''zero-shot''': LiltForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   _UpperCamelCase					:	Optional[Any]				      = False
   _UpperCamelCase					:	List[str]				      = False


   def  SCREAMING_SNAKE_CASE_							(					self    :       Any    ,  a    :       List[Any]    ,  a    :       Optional[Any]    ,  a    :       Any    ,  a    :       Dict    ,  a    :       Union[str, Any] )->     int:





     """simple docstring"""







     return True


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





     """simple docstring"""







     lowercase__			    =						LiltModelTester(self )
     lowercase__			    =						ConfigTester(self    ,  config_class=A_    ,  hidden_size=37 )


   def  SCREAMING_SNAKE_CASE_							(					self    :       Any )->     Optional[int]:





     """simple docstring"""







     self.config_tester.run_common_tests()


   def  SCREAMING_SNAKE_CASE_							(					self    :       Any )->     Any:





     """simple docstring"""







     lowercase__			    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*A_ )


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[str] )->     Tuple:





     """simple docstring"""







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





     """simple docstring"""







     lowercase__			    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*A_ )


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[str] )->     Any:





     """simple docstring"""







     lowercase__			    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*A_ )


   @slow
   def  SCREAMING_SNAKE_CASE_							(					self    :       List[Any] )->     List[Any]:





     """simple docstring"""







     for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       lowercase__			    =						LiltModel.from_pretrained(A_ )
       self.assertIsNotNone(A_ )






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


   def  SCREAMING_SNAKE_CASE_							(					self    :       int )->     int:





     """simple docstring"""







     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 ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class        SCREAMING_SNAKE_CASE     (UpperCAmelCase   ):
   _UpperCamelCase					:	Tuple				      = 'ClapFeatureExtractor'
   _UpperCamelCase					:	Union[str, Any]				      = ('RobertaTokenizer', 'RobertaTokenizerFast')


   def __init__(					self    :       List[Any]    ,  a    :       int    ,  a    :       str )->     Any:





     """simple docstring"""







     super().__init__(a    ,  a )


   def __call__(					self    :       Any    ,  a    :       Tuple=None    ,  a    :       Optional[int]=None    ,  a    :       int=None    ,  **a    :       Optional[int] )->     Union[str, Any]:





     """simple docstring"""







     lowercase__			    =						kwargs.pop('sampling_rate'    ,  a )

     if text is None and audios is None:
       raise ValueError('You have to specify either text or audios. Both cannot be none.' )

     if text is not None:
       lowercase__			    =						self.tokenizer(a    ,  return_tensors=a    ,  **a )

     if audios is not None:
       lowercase__			    =						self.feature_extractor(
           a    ,  sampling_rate=a    ,  return_tensors=a    ,  **a )

     if text is not None and audios is not None:
       lowercase__			    =						audio_features.input_features
       return encoding
     elif text is not None:
       return encoding
     else:
       return BatchEncoding(data=dict(**a )    ,  tensor_type=a )


   def  SCREAMING_SNAKE_CASE_							(					self    :       str    ,  *a    :       Dict    ,  **a    :       int )->     Optional[int]:





     """simple docstring"""







     return self.tokenizer.batch_decode(*a    ,  **a )


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[Any]    ,  *a    :       int    ,  **a    :       Dict )->     Dict:





     """simple docstring"""







     return self.tokenizer.decode(*a    ,  **a )


   @property
   def  SCREAMING_SNAKE_CASE_							(					self    :       Union[str, Any] )->     Optional[int]:





     """simple docstring"""







     lowercase__			    =						self.tokenizer.model_input_names
     lowercase__			    =						self.feature_extractor.model_input_names
     return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names ) )


