


"""simple docstring"""






from __future__ import annotations

import unittest

from transformers import RoFormerConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers import (
      TFRoFormerForCausalLM,
      TFRoFormerForMaskedLM,
      TFRoFormerForMultipleChoice,
      TFRoFormerForQuestionAnswering,
      TFRoFormerForSequenceClassification,
      TFRoFormerForTokenClassification,
      TFRoFormerModel,
  )
  from transformers.models.roformer.modeling_tf_roformer import (
      TFRoFormerSelfAttention,
      TFRoFormerSinusoidalPositionalEmbedding,
  )




class 							A_      :


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







     '''simple docstring'''







     _lowerCamelCase   :				int							=     parent
     _lowerCamelCase   :				Tuple							=     13
     _lowerCamelCase   :				List[Any]							=     7
     _lowerCamelCase   :				Any							=     True
     _lowerCamelCase   :				Any							=     True
     _lowerCamelCase   :				str							=     True
     _lowerCamelCase   :				str							=     True
     _lowerCamelCase   :				Any							=     99
     _lowerCamelCase   :				Tuple							=     32
     _lowerCamelCase   :				Tuple							=     2
     _lowerCamelCase   :				str							=     4
     _lowerCamelCase   :				Optional[Any]							=     37
     _lowerCamelCase   :				List[str]							=     "gelu"
     _lowerCamelCase   :				int							=     0.1
     _lowerCamelCase   :				str							=     0.1
     _lowerCamelCase   :				Dict							=     512
     _lowerCamelCase   :				List[Any]							=     16
     _lowerCamelCase   :				str							=     2
     _lowerCamelCase   :				int							=     0.02
     _lowerCamelCase   :				Union[str, Any]							=     3
     _lowerCamelCase   :				int							=     4
     _lowerCamelCase   :				Dict							=     None


  def 							_lowercase					(			self:						Optional[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Dict							=     ids_tensor([self.batch_size, self.seq_length]							,self.vocab_size    )

     _lowerCamelCase   :				Optional[int]							=     None
     if self.use_input_mask:
        _lowerCamelCase   :				List[Any]							=     random_attention_mask([self.batch_size, self.seq_length]    )

     _lowerCamelCase   :				List[Any]							=     None
     if self.use_token_type_ids:
        _lowerCamelCase   :				str							=     ids_tensor([self.batch_size, self.seq_length]							,self.type_vocab_size    )

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

     _lowerCamelCase   :				List[str]							=     RoFormerConfig(
         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							,return_dict=lowerCamelCase__							,)

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


  def 							_lowercase					(			self:						Optional[int]							,__lowerCAmelCase:						List[Any]							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						List[Any]							,__lowerCAmelCase:						List[str]							,__lowerCAmelCase:						Any							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						int    ):







     '''simple docstring'''







     _lowerCamelCase   :				Any							=     TFRoFormerModel(config=lowerCamelCase__    )
     _lowerCamelCase   :				Optional[Any]							=     {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

     _lowerCamelCase   :				Tuple							=     [input_ids, input_mask]
     _lowerCamelCase   :				Optional[Any]							=     model(lowerCamelCase__    )

     _lowerCamelCase   :				int							=     model(lowerCamelCase__    )

     self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, self.seq_length, self.hidden_size)    )


  def 							_lowercase					(			self:						Any							,__lowerCAmelCase:						List[Any]							,__lowerCAmelCase:						str							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						List[Any]							,__lowerCAmelCase:						Optional[Any]							,__lowerCAmelCase:						Optional[Any]							,__lowerCAmelCase:						Union[str, Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Dict							=     True
     _lowerCamelCase   :				int							=     TFRoFormerForCausalLM(config=lowerCamelCase__    )
     _lowerCamelCase   :				Union[str, Any]							=     {
         "input_ids": input_ids,
         "attention_mask": input_mask,
         "token_type_ids": token_type_ids,
     }
     _lowerCamelCase   :				int							=     model(lowerCamelCase__    )["logits"]
     self.parent.assertListEqual(
         list(prediction_scores.numpy().shape    )							,[self.batch_size, self.seq_length, self.vocab_size]    )


  def 							_lowercase					(			self:						Any							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						str							,__lowerCAmelCase:						int							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						Any							,__lowerCAmelCase:						int							,__lowerCAmelCase:						List[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				str							=     TFRoFormerForMaskedLM(config=lowerCamelCase__    )
     _lowerCamelCase   :				Any							=     {
         "input_ids": input_ids,
         "attention_mask": input_mask,
         "token_type_ids": token_type_ids,
     }
     _lowerCamelCase   :				List[Any]							=     model(lowerCamelCase__    )
     self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.vocab_size)    )


  def 							_lowercase					(			self:						Optional[int]							,__lowerCAmelCase:						int							,__lowerCAmelCase:						List[str]							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						Optional[Any]							,__lowerCAmelCase:						List[str]							,__lowerCAmelCase:						List[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Any							=     self.num_labels
     _lowerCamelCase   :				Union[str, Any]							=     TFRoFormerForSequenceClassification(config=lowerCamelCase__    )
     _lowerCamelCase   :				Any							=     {
         "input_ids": input_ids,
         "attention_mask": input_mask,
         "token_type_ids": token_type_ids,
     }

     _lowerCamelCase   :				Any							=     model(lowerCamelCase__    )
     self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.num_labels)    )


  def 							_lowercase					(			self:						List[Any]							,__lowerCAmelCase:						int							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						Any							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						List[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[Any]							=     self.num_choices
     _lowerCamelCase   :				Union[str, Any]							=     TFRoFormerForMultipleChoice(config=lowerCamelCase__    )
     _lowerCamelCase   :				str							=     tf.tile(tf.expand_dims(lowerCamelCase__							,1    )							,(1, self.num_choices, 1)    )
     _lowerCamelCase   :				Any							=     tf.tile(tf.expand_dims(lowerCamelCase__							,1    )							,(1, self.num_choices, 1)    )
     _lowerCamelCase   :				str							=     tf.tile(tf.expand_dims(lowerCamelCase__							,1    )							,(1, self.num_choices, 1)    )
     _lowerCamelCase   :				Any							=     {
         "input_ids": multiple_choice_inputs_ids,
         "attention_mask": multiple_choice_input_mask,
         "token_type_ids": multiple_choice_token_type_ids,
     }
     _lowerCamelCase   :				Dict							=     model(lowerCamelCase__    )
     self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.num_choices)    )


  def 							_lowercase					(			self:						List[str]							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						Dict							,__lowerCAmelCase:						int							,__lowerCAmelCase:						List[Any]							,__lowerCAmelCase:						str							,__lowerCAmelCase:						Dict    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[int]							=     self.num_labels
     _lowerCamelCase   :				int							=     TFRoFormerForTokenClassification(config=lowerCamelCase__    )
     _lowerCamelCase   :				Any							=     {
         "input_ids": input_ids,
         "attention_mask": input_mask,
         "token_type_ids": token_type_ids,
     }
     _lowerCamelCase   :				Any							=     model(lowerCamelCase__    )
     self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.num_labels)    )


  def 							_lowercase					(			self:						str							,__lowerCAmelCase:						Dict							,__lowerCAmelCase:						Dict							,__lowerCAmelCase:						int							,__lowerCAmelCase:						str							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						str    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[Any]							=     TFRoFormerForQuestionAnswering(config=lowerCamelCase__    )
     _lowerCamelCase   :				str							=     {
         "input_ids": input_ids,
         "attention_mask": input_mask,
         "token_type_ids": token_type_ids,
     }

     _lowerCamelCase   :				Tuple							=     model(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 							_lowercase					(			self:						List[str]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Any							=     self.prepare_config_and_inputs()
     (
         (
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					
     )   :				Dict							=     config_and_inputs
     _lowerCamelCase   :				Dict							=     {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
     return config, inputs_dict




@require_tf
class 							A_      (       __lowerCamelCase     ,	__lowerCamelCase     ,	unittest.TestCase ):
  lowerCAmelCase__      			=	(
      (
          TFRoFormerModel,
          TFRoFormerForCausalLM,
          TFRoFormerForMaskedLM,
          TFRoFormerForQuestionAnswering,
          TFRoFormerForSequenceClassification,
          TFRoFormerForTokenClassification,
          TFRoFormerForMultipleChoice,
      )
      if is_tf_available()
      else ()
  )
  lowerCAmelCase__      			=	(
      {
          'feature-extraction': TFRoFormerModel,
          'fill-mask': TFRoFormerForMaskedLM,
          'question-answering': TFRoFormerForQuestionAnswering,
          'text-classification': TFRoFormerForSequenceClassification,
          'text-generation': TFRoFormerForCausalLM,
          'token-classification': TFRoFormerForTokenClassification,
          'zero-shot': TFRoFormerForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )

  lowerCAmelCase__      			=	False
  lowerCAmelCase__      			=	False


  def 							_lowercase					(			self:						Optional[int]							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						int							,__lowerCAmelCase:						List[str]							,__lowerCAmelCase:						Optional[int]    ):







     '''simple docstring'''







     if pipeline_test_casse_name == "TextGenerationPipelineTests":
        return True

     return False


  def 							_lowercase					(			self:						List[str]    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[str]							=     TFRoFormerModelTester(self    )
     _lowerCamelCase   :				int							=     ConfigTester(self							,config_class=lowerCamelCase__							,hidden_size=37    )


  def 							_lowercase					(			self:						Optional[int]    ):







     '''simple docstring'''







     self.config_tester.run_common_tests()


  def 							_lowercase					(			self:						Optional[int]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Union[str, Any]							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowerCamelCase__    )


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







     '''simple docstring'''







     _lowerCamelCase   :				Optional[Any]							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase__    )


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







     '''simple docstring'''







     _lowerCamelCase   :				int							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_lm_head(*lowerCamelCase__    )


  def 							_lowercase					(			self:						List[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[int]							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase__    )


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







     '''simple docstring'''







     _lowerCamelCase   :				Optional[int]							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*lowerCamelCase__    )


  def 							_lowercase					(			self:						str    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[Any]							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase__    )


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







     '''simple docstring'''







     _lowerCamelCase   :				int							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*lowerCamelCase__    )






  @slow
  def 							_lowercase					(			self:						str    ):







     '''simple docstring'''







     _lowerCamelCase   :				str							=     TFRoFormerModel.from_pretrained("junnyu/roformer_chinese_base"    )
     self.assertIsNotNone(lowerCamelCase__    )




@require_tf
class 							A_      (       unittest.TestCase ):


  @slow
  def 							_lowercase					(			self:						Optional[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Dict							=     TFRoFormerForMaskedLM.from_pretrained("junnyu/roformer_chinese_base"    )
     _lowerCamelCase   :				Optional[int]							=     tf.constant([[0, 1, 2, 3, 4, 5]]    )
     _lowerCamelCase   :				Any							=     model(lowerCamelCase__    )[0]

     # TODO Replace vocab size
     _lowerCamelCase   :				Dict							=     50_000

     _lowerCamelCase   :				Optional[int]							=     [1, 6, vocab_size]
     self.assertEqual(output.shape							,lowerCamelCase__    )

     print(output[:, :3, :3]    )

     # TODO Replace values below with what was printed above.
     _lowerCamelCase   :				Optional[Any]							=     tf.constant(
         [
             [
                 [-0.12_05_33_41, -1.0_26_49_01, 0.29_22_19_46],
                 [-1.5_13_37_83, 0.19_74_33, 0.15_19_06_07],
                 [-5.0_13_54_03, -3.90_02_56, -0.84_03_87_64],
             ]
         ]    )
     tf.debugging.assert_near(output[:, :3, :3]							,lowerCamelCase__							,atol=1e-4    )




@require_tf
class 							A_      (       unittest.TestCase ):
  lowerCAmelCase__      			=	1E-4


  def 							_lowercase					(			self:						Tuple    ):







     '''simple docstring'''







     _lowerCamelCase   :				Any							=     tf.constant([[4, 10]]    )
     _lowerCamelCase   :				Optional[int]							=     TFRoFormerSinusoidalPositionalEmbedding(num_positions=6							,embedding_dim=6    )

     _lowerCamelCase   :				List[str]							=     emba(input_ids.shape    )
     _lowerCamelCase   :				List[Any]							=     tf.constant(
         [[0.00_00, 0.00_00, 0.00_00, 1.00_00, 1.00_00, 1.00_00], [0.84_15, 0.04_64, 0.00_22, 0.54_03, 0.99_89, 1.00_00]]    )

     tf.debugging.assert_near(lowerCamelCase__							,lowerCamelCase__							,atol=self.tolerance    )






  def 							_lowercase					(			self:						Tuple    ):







     '''simple docstring'''







     _lowerCamelCase   :				Any							=     tf.constant(
         [
             [0.00_00, 0.00_00, 0.00_00, 0.00_00, 0.00_00],
             [0.84_15, 0.82_19, 0.80_20, 0.78_19, 0.76_17],
             [0.90_93, 0.93_64, 0.95_81, 0.97_49, 0.98_70],
         ]    )
     _lowerCamelCase   :				Dict							=     TFRoFormerSinusoidalPositionalEmbedding(num_positions=512							,embedding_dim=512    )
     emba([2, 16, 512]    )
     _lowerCamelCase   :				Tuple							=     emba.weight[:3, :5]

     tf.debugging.assert_near(lowerCamelCase__							,lowerCamelCase__							,atol=self.tolerance    )




@require_tf
class 							A_      (       unittest.TestCase ):
  lowerCAmelCase__      			=	1E-4


  def 							_lowercase					(			self:						int    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[Any]							=     tf.reshape(tf.range(2 * 12 * 16 * 64							,dtype=tf.floataa    )							,shape=(2, 12, 16, 64)    ) / 100

     _lowerCamelCase   :				List[str]							=     -tf.reshape(tf.range(2 * 12 * 16 * 64							,dtype=tf.floataa    )							,shape=(2, 12, 16, 64)    ) / 100

     _lowerCamelCase   :				Tuple							=     TFRoFormerSinusoidalPositionalEmbedding(num_positions=32							,embedding_dim=64    )
     _lowerCamelCase   :				str							=     embed_positions([2, 16, 768]    )[None, None, :, :]

     _lowerCamelCase,					_lowerCamelCase   :				Tuple							=     TFRoFormerSelfAttention.apply_rotary_position_embeddings(
         lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__    )

     _lowerCamelCase   :				Optional[Any]							=     tf.constant(
         [
             [0.00_00, 0.01_00, 0.02_00, 0.03_00, 0.04_00, 0.05_00, 0.06_00, 0.07_00],
             [-0.20_12, 0.88_97, 0.02_63, 0.94_01, 0.20_74, 0.94_63, 0.34_81, 0.93_43],
             [-1.70_57, 0.62_71, -1.21_45, 1.38_97, -0.63_03, 1.76_47, -0.11_73, 1.89_85],
             [-2.17_31, -1.63_97, -2.73_58, 0.28_54, -2.18_40, 1.71_83, -1.30_18, 2.48_71],
             [0.27_17, -3.61_73, -2.92_06, -2.19_88, -3.66_38, 0.38_58, -2.91_55, 2.29_80],
             [3.98_59, -2.15_80, -0.79_84, -4.49_04, -4.11_81, -2.02_52, -4.47_82, 1.12_53],
         ]    )
     _lowerCamelCase   :				Tuple							=     tf.constant(
         [
             [0.00_00, -0.01_00, -0.02_00, -0.03_00, -0.04_00, -0.05_00, -0.06_00, -0.07_00],
             [0.20_12, -0.88_97, -0.02_63, -0.94_01, -0.20_74, -0.94_63, -0.34_81, -0.93_43],
             [1.70_57, -0.62_71, 1.21_45, -1.38_97, 0.63_03, -1.76_47, 0.11_73, -1.89_85],
             [2.17_31, 1.63_97, 2.73_58, -0.28_54, 2.18_40, -1.71_83, 1.30_18, -2.48_71],
             [-0.27_17, 3.61_73, 2.92_06, 2.19_88, 3.66_38, -0.38_58, 2.91_55, -2.29_80],
             [-3.98_59, 2.15_80, 0.79_84, 4.49_04, 4.11_81, 2.02_52, 4.47_82, -1.12_53],
         ]    )

     tf.debugging.assert_near(query_layer[0, 0, :6, :8]							,lowerCamelCase__							,atol=self.tolerance    )
     tf.debugging.assert_near(key_layer[0, 0, :6, :8]							,lowerCamelCase__							,atol=self.tolerance    )


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

   def __init__(				self	:    Tuple			,						lowerCamelCase__	:    Optional[int]			,						lowerCamelCase__	:    Dict=1_3			,						lowerCamelCase__	:    Any=7			,						lowerCamelCase__	:    Optional[Any]=True			,						lowerCamelCase__	:    Optional[Any]=True			,						lowerCamelCase__	:    List[Any]=True			,						lowerCamelCase__	:    Union[str, Any]=True			,						lowerCamelCase__	:    Optional[int]=9_9			,						lowerCamelCase__	:    Optional[int]=2_4			,						lowerCamelCase__	:    Optional[int]=2			,						lowerCamelCase__	:    Dict=6			,						lowerCamelCase__	:    List[str]=3_7			,						lowerCamelCase__	:    Optional[Any]="gelu"			,						lowerCamelCase__	:    Dict=0.1			,						lowerCamelCase__	:    Optional[int]=0.1			,						lowerCamelCase__	:    Any=5_1_2			,						lowerCamelCase__	:    int=1_6			,						lowerCamelCase__	:    Union[str, Any]=2			,						lowerCamelCase__	:    Optional[int]=0.02			,						lowerCamelCase__	:    Optional[Any]=3			,						lowerCamelCase__	:    Optional[int]=None			,						lowerCamelCase__	:    Any=1_0_0_0			,						) ->			Any:
         """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	:    int       ) ->			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	:    Optional[int]       ) ->			List[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     UpperCamelCase					(				self	:    Dict			,						lowerCamelCase__	:    Any			,						lowerCamelCase__	:    Union[str, Any]			,						lowerCamelCase__	:    List[Any]			,						lowerCamelCase__	:    Any			,						lowerCamelCase__	:    List[Any]			,						lowerCamelCase__	:    List[str]			,						lowerCamelCase__	:    Any			,						) ->			List[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	:    Optional[int]			,						lowerCamelCase__	:    str			,						lowerCamelCase__	:    Union[str, Any]			,						lowerCamelCase__	:    Union[str, Any]			,						lowerCamelCase__	:    Dict			,						lowerCamelCase__	:    List[Any]			,						lowerCamelCase__	:    Dict			,						lowerCamelCase__	:    Optional[Any]			,						) ->			Union[str, Any]:
         """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	:    Dict			,						lowerCamelCase__	:    List[Any]			,						lowerCamelCase__	:    str			,						lowerCamelCase__	:    str			,						lowerCamelCase__	:    Dict			,						lowerCamelCase__	:    Any			,						lowerCamelCase__	:    Any			,						lowerCamelCase__	:    List[str]			,						) ->			Any:
         """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	:    Optional[int]       ) ->			List[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    _lowercase				(					__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,unittest.TestCase			):
   _lowercase     :       Dict  					=    (
       (
           LiltModel,
           LiltForSequenceClassification,
           LiltForTokenClassification,
           LiltForQuestionAnswering,
       )
       if is_torch_available()
       else ()
   )
   _lowercase     :       List[Any]  					=    (
       {
           'feature-extraction': LiltModel,
           'question-answering': LiltForQuestionAnswering,
           'text-classification': LiltForSequenceClassification,
           'token-classification': LiltForTokenClassification,
           'zero-shot': LiltForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   _lowercase     :       Dict  					=    False
   _lowercase     :       Optional[int]  					=    False

   def     UpperCamelCase					(				self	:    Optional[Any]			,						lowerCamelCase__	:    Dict			,						lowerCamelCase__	:    int			,						lowerCamelCase__	:    List[str]			,						lowerCamelCase__	:    List[str]			,						lowerCamelCase__	:    List[str]       ) ->			Any:
         """simple docstring"""





         return True

   def     UpperCamelCase					(				self	:    int       ) ->			Any:
         """simple docstring"""





         A_							       =		LiltModelTester(self       )
         A_							       =		ConfigTester(self			,						config_class=lowerCamelCase__			,						hidden_size=3_7       )

   def     UpperCamelCase					(				self	:    int       ) ->			Optional[Any]:
         """simple docstring"""





         self.config_tester.run_common_tests()

   def     UpperCamelCase					(				self	:    Union[str, Any]       ) ->			Dict:
         """simple docstring"""





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

   def     UpperCamelCase					(				self	:    Optional[Any]       ) ->			int:
         """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	:    List[Any]       ) ->			int:
         """simple docstring"""





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

   def     UpperCamelCase					(				self	:    List[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	:    Any       ) ->			Dict:
         """simple docstring"""





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



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

   def     UpperCamelCase					(				self	:    List[str]       ) ->			Optional[Any]:
         """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, 7_6_8]       )
         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       )       )





