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							:







 """simple docstring"""






 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_=5_12		,				lowerCAmelCase_=16		,				lowerCAmelCase_=2		,				lowerCAmelCase_=0.02		,				lowerCAmelCase_=3		,				lowerCAmelCase_=None		,				lowerCAmelCase_=10_00		,				)  ->					Optional[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							)  ->					int:
        _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							)  ->					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		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				)  ->					int:
        _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_		,				)  ->					int:
        _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_		,				)  ->					str:
        _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:
        _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 				a							(			__lowerCAmelCase       ,			__lowerCAmelCase       ,			__lowerCAmelCase       ,			unittest.TestCase       ):







 """simple docstring"""






 lowerCamelCase	:Any    			=		(
     (
         LiltModel,
         LiltForSequenceClassification,
         LiltForTokenClassification,
         LiltForQuestionAnswering,
     )
     if is_torch_available()
     else ()
 )
 lowerCamelCase	:Optional[Any]    			=		(
     {
         '''feature-extraction''': LiltModel,
         '''question-answering''': LiltForQuestionAnswering,
         '''text-classification''': LiltForSequenceClassification,
         '''token-classification''': LiltForTokenClassification,
         '''zero-shot''': LiltForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 lowerCamelCase	:Optional[int]    			=		False
 lowerCamelCase	:str    			=		False



 def      UpperCAmelCase							(					self		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_							)  ->					Dict:
        return True



 def      UpperCAmelCase							(					self							)  ->					Any:
        _A        =  LiltModelTester(self							)
        _A        =  ConfigTester(self		,				config_class=lowerCAmelCase_		,				hidden_size=37							)



 def      UpperCAmelCase							(					self							)  ->					List[Any]:
        self.config_tester.run_common_tests()



 def      UpperCAmelCase							(					self							)  ->					Tuple:
        _A        =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase_							)



 def      UpperCAmelCase							(					self							)  ->					Optional[int]:
        _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							)  ->					Optional[int]:
        _A        =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase_							)



 def      UpperCAmelCase							(					self							)  ->					str:
        _A        =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase_							)



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



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







 """simple docstring"""






 def      UpperCAmelCase							(					self							)  ->					int:
        _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_68]							)
        _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							)							)





from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_SCREAMING_SNAKE_CASE										=						logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE										=						{
    'microsoft/resnet-50': 'https://huggingface.co/microsoft/resnet-50/blob/main/config.json',
}



class 				a							(			__lowerCAmelCase       ,			__lowerCAmelCase       ):







 """simple docstring"""






 lowerCamelCase	:Any    			=		'''resnet'''
 lowerCamelCase	:Any    			=		['''basic''', '''bottleneck''']



 def __init__(					self		,				lowerCAmelCase_=3		,				lowerCAmelCase_=64		,				lowerCAmelCase_=[2_56, 5_12, 10_24, 20_48]		,				lowerCAmelCase_=[3, 4, 6, 3]		,				lowerCAmelCase_="bottleneck"		,				lowerCAmelCase_="relu"		,				lowerCAmelCase_=False		,				lowerCAmelCase_=None		,				lowerCAmelCase_=None		,				**lowerCAmelCase_		,				)  ->					Union[str, Any]:
        super().__init__(**lowerCAmelCase_							)
        if layer_type not in self.layer_types:
               raise ValueError(F'''layer_type={layer_type} is not one of {','.join(self.layer_types							)}'''							)
        _A        =  num_channels
        _A        =  embedding_size
        _A        =  hidden_sizes
        _A        =  depths
        _A        =  layer_type
        _A        =  hidden_act
        _A        =  downsample_in_first_stage
        _A        =  ["""stem"""] + [F'''stage{idx}''' for idx in range(1		,				len(lowerCAmelCase_							) + 1							)]
        _A   ,  _A        =  get_aligned_output_features_output_indices(
            out_features=lowerCAmelCase_		,				out_indices=lowerCAmelCase_		,				stage_names=self.stage_names							)



class 				a							(			__lowerCAmelCase       ):







 """simple docstring"""






 lowerCamelCase	:Optional[Any]    			=		version.parse('''1.11'''       )



 @property
 def      UpperCAmelCase							(					self							)  ->					Mapping[str, Mapping[int, str]]:
        return OrderedDict(
            [
                ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
            ]							)



 @property
 def      UpperCAmelCase							(					self							)  ->					float:
        return 1E-3




