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







   """simple docstring"""

   def __init__(      self      :			Optional[int], UpperCamelCase__      :			Optional[Any], UpperCamelCase__      :			Tuple=13, UpperCamelCase__      :			int=7, UpperCamelCase__      :			List[str]=True, UpperCamelCase__      :			Optional[Any]=True, UpperCamelCase__      :			Optional[Any]=True, UpperCamelCase__      :			Optional[Any]=True, UpperCamelCase__      :			str=99, UpperCamelCase__      :			List[str]=24, UpperCamelCase__      :			Tuple=2, UpperCamelCase__      :			Union[str, Any]=6, UpperCamelCase__      :			int=37, UpperCamelCase__      :			Dict="gelu", UpperCamelCase__      :			int=0.1, UpperCamelCase__      :			str=0.1, UpperCamelCase__      :			Union[str, Any]=5_12, UpperCamelCase__      :			Dict=16, UpperCamelCase__      :			int=2, UpperCamelCase__      :			Union[str, Any]=0.02, UpperCamelCase__      :			Optional[Any]=3, UpperCamelCase__      :			Any=None, UpperCamelCase__      :			Tuple=10_00, )  ->			Union[str, Any]:
        _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      :			List[Any]     )  ->			Dict:
        _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      :			List[Any]     )  ->			Optional[Any]:
        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      :			str, UpperCamelCase__      :			List[Any], UpperCamelCase__      :			Dict, UpperCamelCase__      :			Dict, UpperCamelCase__      :			int, UpperCamelCase__      :			Dict, UpperCamelCase__      :			Union[str, Any], UpperCamelCase__      :			int, )  ->			Dict:
        _A    		=  LiltModel(config=UpperCamelCase__     )
        model.to(UpperCamelCase__     )
        model.eval()
        _A    		=  model(UpperCamelCase__, bbox=UpperCamelCase__, attention_mask=UpperCamelCase__, token_type_ids=UpperCamelCase__     )
        _A    		=  model(UpperCamelCase__, bbox=UpperCamelCase__, token_type_ids=UpperCamelCase__     )
        _A    		=  model(UpperCamelCase__, bbox=UpperCamelCase__     )

        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      :			Any, UpperCamelCase__      :			Optional[int], UpperCamelCase__      :			List[Any], UpperCamelCase__      :			str, UpperCamelCase__      :			Optional[Any], UpperCamelCase__      :			Any, UpperCamelCase__      :			Any, UpperCamelCase__      :			List[str], )  ->			Union[str, Any]:
        _A    		=  self.num_labels
        _A    		=  LiltForTokenClassification(config=UpperCamelCase__     )
        model.to(UpperCamelCase__     )
        model.eval()
        _A    		=  model(
            UpperCamelCase__, bbox=UpperCamelCase__, attention_mask=UpperCamelCase__, token_type_ids=UpperCamelCase__, labels=UpperCamelCase__     )
        self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.num_labels)     )
   def 	__UpperCAmelCase   (      self      :			Optional[int], UpperCamelCase__      :			Any, UpperCamelCase__      :			Optional[int], UpperCamelCase__      :			str, UpperCamelCase__      :			Union[str, Any], UpperCamelCase__      :			str, UpperCamelCase__      :			Union[str, Any], UpperCamelCase__      :			Optional[Any], )  ->			List[str]:
        _A    		=  LiltForQuestionAnswering(config=UpperCamelCase__     )
        model.to(UpperCamelCase__     )
        model.eval()
        _A    		=  model(
            UpperCamelCase__, bbox=UpperCamelCase__, attention_mask=UpperCamelCase__, token_type_ids=UpperCamelCase__, start_positions=UpperCamelCase__, end_positions=UpperCamelCase__, )
        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      :			List[Any]     )  ->			Tuple:
        _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_       (		_UpperCamelCase    ,    _UpperCamelCase    ,    _UpperCamelCase    ,    unittest.TestCase			):







   """simple docstring"""

   __lowerCAmelCase         =   (
       (
           LiltModel,
           LiltForSequenceClassification,
           LiltForTokenClassification,
           LiltForQuestionAnswering,
       )
       if is_torch_available()
       else ()
   )
   __lowerCAmelCase         =   (
       {
           "feature-extraction": LiltModel,
           "question-answering": LiltForQuestionAnswering,
           "text-classification": LiltForSequenceClassification,
           "token-classification": LiltForTokenClassification,
           "zero-shot": LiltForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   __lowerCAmelCase         =   False
   __lowerCAmelCase         =   False
   def 	__UpperCAmelCase   (      self      :			int, UpperCamelCase__      :			str, UpperCamelCase__      :			str, UpperCamelCase__      :			List[str], UpperCamelCase__      :			Any, UpperCamelCase__      :			Union[str, Any]     )  ->			Optional[Any]:
        return True
   def 	__UpperCAmelCase   (      self      :			int     )  ->			int:
        _A    		=  LiltModelTester(self     )
        _A    		=  ConfigTester(self, config_class=UpperCamelCase__, hidden_size=37     )
   def 	__UpperCAmelCase   (      self      :			List[str]     )  ->			List[str]:
        self.config_tester.run_common_tests()
   def 	__UpperCAmelCase   (      self      :			List[str]     )  ->			List[str]:
        _A    		=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*UpperCamelCase__     )
   def 	__UpperCAmelCase   (      self      :			int     )  ->			str:
        _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(*UpperCamelCase__     )
   def 	__UpperCAmelCase   (      self      :			Any     )  ->			int:
        _A    		=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__     )
   def 	__UpperCAmelCase   (      self      :			Tuple     )  ->			Dict:
        _A    		=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__     )

   @slow
   def 	__UpperCAmelCase   (      self      :			List[Any]     )  ->			Optional[int]:
        for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _A    		=  LiltModel.from_pretrained(UpperCamelCase__     )
             self.assertIsNotNone(UpperCamelCase__     )







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







   """simple docstring"""

   def 	__UpperCAmelCase   (      self      :			List[str]     )  ->			Optional[int]:
        _A    		=  LiltModel.from_pretrained('SCUT-DLVCLab/lilt-roberta-en-base'     ).to(UpperCamelCase__     )

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

        # forward pass
        with torch.no_grad():
             _A    		=  model(input_ids=UpperCamelCase__, bbox=UpperCamelCase__     )

        _A    		=  torch.Size([1, 2, 7_68]     )
        _A    		=  torch.tensor(
            [[-0.0_653, 0.0_950, -0.0_061], [-0.0_545, 0.0_926, -0.0_324]], device=UpperCamelCase__, )

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




'''simple docstring'''






from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_UpperCAmelCase    :     Optional[Any]         =       logging.get_logger(__name__)

_UpperCAmelCase    :     List[Any]         =       {
    '''andreasmadsen/efficient_mlm_m0.40''': (
        '''https://huggingface.co/andreasmadsen/efficient_mlm_m0.40/resolve/main/config.json'''
    ),
}







class lowercase_       (		_UpperCamelCase			):







   """simple docstring"""

   __lowerCAmelCase         =   "roberta-prelayernorm"
   def __init__(      self      :			Optional[int], UpperCamelCase__      :			List[Any]=5_02_65, UpperCamelCase__      :			int=7_68, UpperCamelCase__      :			str=12, UpperCamelCase__      :			Union[str, Any]=12, UpperCamelCase__      :			Tuple=30_72, UpperCamelCase__      :			Tuple="gelu", UpperCamelCase__      :			List[Any]=0.1, UpperCamelCase__      :			Union[str, Any]=0.1, UpperCamelCase__      :			Any=5_12, UpperCamelCase__      :			Union[str, Any]=2, UpperCamelCase__      :			Optional[Any]=0.02, UpperCamelCase__      :			str=1e-12, UpperCamelCase__      :			str=1, UpperCamelCase__      :			List[str]=0, UpperCamelCase__      :			Optional[int]=2, UpperCamelCase__      :			Optional[Any]="absolute", UpperCamelCase__      :			Optional[Any]=True, UpperCamelCase__      :			List[str]=None, **UpperCamelCase__      :			Any, )  ->			Dict:
        super().__init__(pad_token_id=UpperCamelCase__, bos_token_id=UpperCamelCase__, eos_token_id=UpperCamelCase__, **UpperCamelCase__     )

        _A    		=  vocab_size
        _A    		=  hidden_size
        _A    		=  num_hidden_layers
        _A    		=  num_attention_heads
        _A    		=  hidden_act
        _A    		=  intermediate_size
        _A    		=  hidden_dropout_prob
        _A    		=  attention_probs_dropout_prob
        _A    		=  max_position_embeddings
        _A    		=  type_vocab_size
        _A    		=  initializer_range
        _A    		=  layer_norm_eps
        _A    		=  position_embedding_type
        _A    		=  use_cache
        _A    		=  classifier_dropout







class lowercase_       (		_UpperCamelCase			):







   """simple docstring"""

   @property
   def 	__UpperCAmelCase   (      self      :			Optional[int]     )  ->			Mapping[str, Mapping[int, str]]:
        if self.task == "multiple-choice":
             _A    		=  {0: 'batch', 1: 'choice', 2: 'sequence'}
        else:
             _A    		=  {0: 'batch', 1: 'sequence'}
        return OrderedDict(
            [
                ('input_ids', dynamic_axis),
                ('attention_mask', dynamic_axis),
            ]     )



