





import collections
import inspect
import unittest
from typing import Dict, List, Tuple

from transformers import MaskFormerSwinConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device
from transformers.utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch
     from torch import nn

     from transformers import MaskFormerSwinBackbone
     from transformers.models.maskformer import MaskFormerSwinModel

class      A       :



      def __init__(	self:						List[str]		,  _lowerCAmelCase:						Optional[int]		,  _lowerCAmelCase:						List[Any]=13		,  _lowerCAmelCase:						Dict=32		,  _lowerCAmelCase:						str=2		,  _lowerCAmelCase:						int=3		,  _lowerCAmelCase:						List[str]=16		,  _lowerCAmelCase:						List[Any]=[1, 2, 1]		,  _lowerCAmelCase:						Optional[Any]=[2, 2, 4]		,  _lowerCAmelCase:						Any=2		,  _lowerCAmelCase:						Optional[int]=2.0		,  _lowerCAmelCase:						Tuple=True		,  _lowerCAmelCase:						Dict=0.0		,  _lowerCAmelCase:						Optional[Any]=0.0		,  _lowerCAmelCase:						Union[str, Any]=0.1		,  _lowerCAmelCase:						Optional[int]="gelu"		,  _lowerCAmelCase:						int=False		,  _lowerCAmelCase:						Dict=True		,  _lowerCAmelCase:						List[str]=0.02		,  _lowerCAmelCase:						List[str]=1e-5		,  _lowerCAmelCase:						Optional[int]=True		,  _lowerCAmelCase:						int=None		,  _lowerCAmelCase:						Tuple=True		,  _lowerCAmelCase:						str=10		,  _lowerCAmelCase:						List[Any]=8		,  _lowerCAmelCase:						Optional[int]=["stage1", "stage2", "stage3"]		,  _lowerCAmelCase:						int=[1, 2, 3]		,  )						->    List[str]:
         '''simple docstring'''

         UpperCAmelCase_											=parent
         UpperCAmelCase_											=batch_size
         UpperCAmelCase_											=image_size
         UpperCAmelCase_											=patch_size
         UpperCAmelCase_											=num_channels
         UpperCAmelCase_											=embed_dim
         UpperCAmelCase_											=depths
         UpperCAmelCase_											=num_heads
         UpperCAmelCase_											=window_size
         UpperCAmelCase_											=mlp_ratio
         UpperCAmelCase_											=qkv_bias
         UpperCAmelCase_											=hidden_dropout_prob
         UpperCAmelCase_											=attention_probs_dropout_prob
         UpperCAmelCase_											=drop_path_rate
         UpperCAmelCase_											=hidden_act
         UpperCAmelCase_											=use_absolute_embeddings
         UpperCAmelCase_											=patch_norm
         UpperCAmelCase_											=layer_norm_eps
         UpperCAmelCase_											=initializer_range
         UpperCAmelCase_											=is_training
         UpperCAmelCase_											=scope
         UpperCAmelCase_											=use_labels
         UpperCAmelCase_											=type_sequence_label_size
         UpperCAmelCase_											=encoder_stride
         UpperCAmelCase_											=out_features
         UpperCAmelCase_											=out_indices



      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    str:
         '''simple docstring'''

         UpperCAmelCase_											=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

         UpperCAmelCase_											=None
         if self.use_labels:
            UpperCAmelCase_											=ids_tensor([self.batch_size]		,  self.type_sequence_label_size       )

         UpperCAmelCase_											=self.get_config()

         return config, pixel_values, labels



      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    Optional[Any]:
         '''simple docstring'''

         return MaskFormerSwinConfig(
             image_size=self.image_size		,  patch_size=self.patch_size		,  num_channels=self.num_channels		,  embed_dim=self.embed_dim		,  depths=self.depths		,  num_heads=self.num_heads		,  window_size=self.window_size		,  mlp_ratio=self.mlp_ratio		,  qkv_bias=self.qkv_bias		,  hidden_dropout_prob=self.hidden_dropout_prob		,  attention_probs_dropout_prob=self.attention_probs_dropout_prob		,  drop_path_rate=self.drop_path_rate		,  hidden_act=self.hidden_act		,  use_absolute_embeddings=self.use_absolute_embeddings		,  path_norm=self.patch_norm		,  layer_norm_eps=self.layer_norm_eps		,  initializer_range=self.initializer_range		,  encoder_stride=self.encoder_stride		,  out_features=self.out_features		,  out_indices=self.out_indices		,  )



      def 	lowerCAmelCase__    (	self:						Optional[int]		,  _lowerCAmelCase:						Dict		,  _lowerCAmelCase:						Union[str, Any]		,  _lowerCAmelCase:						Dict       )						->    Any:
         '''simple docstring'''

         UpperCAmelCase_											=MaskFormerSwinModel(config=UpperCamelCase_       )
         model.to(UpperCamelCase_       )
         model.eval()
         UpperCAmelCase_											=model(UpperCamelCase_       )

         UpperCAmelCase_											=((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths       ) - 1))
         UpperCAmelCase_											=int(config.embed_dim * 2 ** (len(config.depths       ) - 1)       )

         self.parent.assertEqual(result.last_hidden_state.shape		,  (self.batch_size, expected_seq_len, expected_dim)       )



      def 	lowerCAmelCase__    (	self:						int		,  _lowerCAmelCase:						Any		,  _lowerCAmelCase:						List[str]		,  _lowerCAmelCase:						List[str]       )						->    Union[str, Any]:
         '''simple docstring'''

         UpperCAmelCase_											=MaskFormerSwinBackbone(config=UpperCamelCase_       )
         model.to(UpperCamelCase_       )
         model.eval()
         UpperCAmelCase_											=model(UpperCamelCase_       )

         # verify feature maps
         self.parent.assertEqual(len(result.feature_maps       )		,  len(config.out_features       )       )
         self.parent.assertListEqual(list(result.feature_maps[0].shape       )		,  [13, 16, 16, 16]       )

         # verify channels
         self.parent.assertEqual(len(model.channels       )		,  len(config.out_features       )       )
         self.parent.assertListEqual(model.channels		,  [16, 32, 64]       )

         # verify ValueError
         with self.parent.assertRaises(UpperCamelCase_       ):
            UpperCAmelCase_											=["stem"]
            UpperCAmelCase_											=MaskFormerSwinBackbone(config=UpperCamelCase_       )






      def 	lowerCAmelCase__    (	self:						Tuple       )						->    Optional[int]:
         '''simple docstring'''

         UpperCAmelCase_											=self.prepare_config_and_inputs()
         UpperCAmelCase_						,       UpperCAmelCase_						,       UpperCAmelCase_											=config_and_inputs
         UpperCAmelCase_											={"pixel_values": pixel_values}
         return config, inputs_dict

@require_torch
class      A       (	__lowerCamelCase		,      __lowerCamelCase		,      unittest.TestCase						):
      _snake_case							     =(
          (
              MaskFormerSwinModel,
              MaskFormerSwinBackbone,
          )
          if is_torch_available()
          else ()
      )
      _snake_case							     ={'feature-extraction': MaskFormerSwinModel} if is_torch_available() else {}
      _snake_case							     =False
      _snake_case							     =False
      _snake_case							     =False
      _snake_case							     =False
      _snake_case							     =False



      def 	lowerCAmelCase__    (	self:						List[str]       )						->    Tuple:
         '''simple docstring'''

         UpperCAmelCase_											=MaskFormerSwinModelTester(self       )
         UpperCAmelCase_											=ConfigTester(self		,  config_class=UpperCamelCase_		,  embed_dim=37       )



      @require_torch_multi_gpu
      @unittest.skip(
          reason=(
              "`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn't work well with"
              " `nn.DataParallel`"
          )       )
      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    int:
         '''simple docstring'''

         pass



      def 	lowerCAmelCase__    (	self:						Tuple       )						->    Dict:
         '''simple docstring'''

         self.create_and_test_config_common_properties()
         self.config_tester.create_and_test_config_to_json_string()
         self.config_tester.create_and_test_config_to_json_file()
         self.config_tester.create_and_test_config_from_and_save_pretrained()
         self.config_tester.create_and_test_config_with_num_labels()
         self.config_tester.check_config_can_be_init_without_params()
         self.config_tester.check_config_arguments_init()



      def 	lowerCAmelCase__    (	self:						Any       )						->    Dict:
         '''simple docstring'''

         return



      def 	lowerCAmelCase__    (	self:						str       )						->    int:
         '''simple docstring'''

         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*UpperCamelCase_       )



      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    str:
         '''simple docstring'''

         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_backbone(*UpperCamelCase_       )



      @unittest.skip("Swin does not use inputs_embeds"       )
      def 	lowerCAmelCase__    (	self:						Any       )						->    int:
         '''simple docstring'''

         pass



      @unittest.skip("Swin does not support feedforward chunking"       )
      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    int:
         '''simple docstring'''

         pass



      def 	lowerCAmelCase__    (	self:						Any       )						->    Union[str, Any]:
         '''simple docstring'''

         UpperCAmelCase_						,       UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
            UpperCAmelCase_											=model_class(UpperCamelCase_       )
            self.assertIsInstance(model.get_input_embeddings()		,  (nn.Module)       )
            UpperCAmelCase_											=model.get_output_embeddings()
            self.assertTrue(x is None or isinstance(UpperCamelCase_		,  nn.Linear       )       )



      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    Union[str, Any]:
         '''simple docstring'''

         UpperCAmelCase_						,       UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
            UpperCAmelCase_											=model_class(UpperCamelCase_       )
            UpperCAmelCase_											=inspect.signature(model.forward       )
            # signature.parameters is an OrderedDict => so arg_names order is deterministic
            UpperCAmelCase_											=[*signature.parameters.keys()]

            UpperCAmelCase_											=["pixel_values"]
            self.assertListEqual(arg_names[:1]		,  UpperCamelCase_       )



      @unittest.skip(reason="MaskFormerSwin is only used as backbone and doesn't support output_attentions"       )
      def 	lowerCAmelCase__    (	self:						Tuple       )						->    Any:
         '''simple docstring'''

         pass



      @unittest.skip(reason="MaskFormerSwin is only used as an internal backbone"       )
      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    Optional[Any]:
         '''simple docstring'''

         pass



      def 	lowerCAmelCase__    (	self:						List[str]		,  _lowerCAmelCase:						Tuple		,  _lowerCAmelCase:						Union[str, Any]		,  _lowerCAmelCase:						Tuple		,  _lowerCAmelCase:						Optional[int]       )						->    List[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=model_class(UpperCamelCase_       )
         model.to(UpperCamelCase_       )
         model.eval()

         with torch.no_grad():
            UpperCAmelCase_											=model(**self._prepare_for_class(UpperCamelCase_		,  UpperCamelCase_       )       )

         UpperCAmelCase_											=outputs.hidden_states

         UpperCAmelCase_											=getattr(
             self.model_tester		,  "expected_num_hidden_layers"		,  len(self.model_tester.depths       ) + 1       )
         self.assertEqual(len(UpperCamelCase_       )		,  UpperCamelCase_       )

         # Swin has a different seq_length
         UpperCAmelCase_											=(
             config.patch_size
             if isinstance(config.patch_size		,  collections.abc.Iterable       )
             else (config.patch_size, config.patch_size)
         )

         UpperCAmelCase_											=(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

         self.assertListEqual(
             list(hidden_states[0].shape[-2:]       )		,  [num_patches, self.model_tester.embed_dim]		,  )



      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    List[str]:
         '''simple docstring'''

         UpperCAmelCase_						,       UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs_for_common()

         UpperCAmelCase_											=(
             self.model_tester.image_size
             if isinstance(self.model_tester.image_size		,  collections.abc.Iterable       )
             else (self.model_tester.image_size, self.model_tester.image_size)
         )

         for model_class in self.all_model_classes:
            UpperCAmelCase_											=True
            self.check_hidden_states_output(UpperCamelCase_		,  UpperCamelCase_		,  UpperCamelCase_		,  UpperCamelCase_       )

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            UpperCAmelCase_											=True

            self.check_hidden_states_output(UpperCamelCase_		,  UpperCamelCase_		,  UpperCamelCase_		,  UpperCamelCase_       )



      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    Dict:
         '''simple docstring'''

         UpperCAmelCase_						,       UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs_for_common()
         UpperCAmelCase_											=3

         UpperCAmelCase_											=(
             self.model_tester.image_size
             if isinstance(self.model_tester.image_size		,  collections.abc.Iterable       )
             else (self.model_tester.image_size, self.model_tester.image_size)
         )
         UpperCAmelCase_											=(
             config.patch_size
             if isinstance(config.patch_size		,  collections.abc.Iterable       )
             else (config.patch_size, config.patch_size)
         )

         UpperCAmelCase_											=image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
         UpperCAmelCase_											=image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

         for model_class in self.all_model_classes:
            UpperCAmelCase_											=True
            self.check_hidden_states_output(UpperCamelCase_		,  UpperCamelCase_		,  UpperCamelCase_		,  (padded_height, padded_width)       )

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            UpperCAmelCase_											=True
            self.check_hidden_states_output(UpperCamelCase_		,  UpperCamelCase_		,  UpperCamelCase_		,  (padded_height, padded_width)       )



      @unittest.skip(reason="MaskFormerSwin doesn't have pretrained checkpoints"       )
      def 	lowerCAmelCase__    (	self:						Dict       )						->    str:
         '''simple docstring'''

         pass



      @unittest.skip(reason="This will be fixed once MaskFormerSwin is replaced by native Swin"       )
      def 	lowerCAmelCase__    (	self:						str       )						->    str:
         '''simple docstring'''

         pass



      @unittest.skip(reason="This will be fixed once MaskFormerSwin is replaced by native Swin"       )
      def 	lowerCAmelCase__    (	self:						str       )						->    str:
         '''simple docstring'''

         pass






      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    Union[str, Any]:
         '''simple docstring'''

         UpperCAmelCase_						,       UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs_for_common()

         def set_nan_tensor_to_zero(_lowerCAmelCase:						Optional[int]       ):
            UpperCAmelCase_											=0
            return t

         def check_equivalence(_lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						Tuple		,  _lowerCAmelCase:						Any={}       ):
            with torch.no_grad():
               UpperCAmelCase_											=model(**UpperCamelCase_		,  return_dict=UpperCamelCase_		,  **UpperCamelCase_       )
               UpperCAmelCase_											=model(**UpperCamelCase_		,  return_dict=UpperCamelCase_		,  **UpperCamelCase_       ).to_tuple()

               def recursive_check(_lowerCAmelCase:						str		,  _lowerCAmelCase:						List[str]       ):
                  if isinstance(UpperCamelCase_		,  (List, Tuple)       ):
                     for tuple_iterable_value, dict_iterable_value in zip(UpperCamelCase_		,  UpperCamelCase_       ):
                        recursive_check(UpperCamelCase_		,  UpperCamelCase_       )
                  elif isinstance(UpperCamelCase_		,  UpperCamelCase_       ):
                     for tuple_iterable_value, dict_iterable_value in zip(
                         tuple_object.values()		,  dict_object.values()       ):
                        recursive_check(UpperCamelCase_		,  UpperCamelCase_       )
                  elif tuple_object is None:
                     return
                  else:
                     self.assertTrue(
                         torch.allclose(
                             set_nan_tensor_to_zero(UpperCamelCase_       )		,  set_nan_tensor_to_zero(UpperCamelCase_       )		,  atol=1e-5       )		,  msg=(
                             "Tuple and dict output are not equal. Difference:"
                             F' {torch.max(torch.abs(tuple_object - dict_object       )       )}. Tuple has `nan`:'
                             F' {torch.isnan(UpperCamelCase_       ).any()} and `inf`: {torch.isinf(UpperCamelCase_       )}. Dict has'
                             F' `nan`: {torch.isnan(UpperCamelCase_       ).any()} and `inf`: {torch.isinf(UpperCamelCase_       )}.'
                         )		,  )

               recursive_check(UpperCamelCase_		,  UpperCamelCase_       )

         for model_class in self.all_model_classes:
            UpperCAmelCase_											=model_class(UpperCamelCase_       )
            model.to(UpperCamelCase_       )
            model.eval()

            UpperCAmelCase_											=self._prepare_for_class(UpperCamelCase_		,  UpperCamelCase_       )
            UpperCAmelCase_											=self._prepare_for_class(UpperCamelCase_		,  UpperCamelCase_       )
            check_equivalence(UpperCamelCase_		,  UpperCamelCase_		,  UpperCamelCase_       )

            UpperCAmelCase_											=self._prepare_for_class(UpperCamelCase_		,  UpperCamelCase_		,  return_labels=UpperCamelCase_       )
            UpperCAmelCase_											=self._prepare_for_class(UpperCamelCase_		,  UpperCamelCase_		,  return_labels=UpperCamelCase_       )
            check_equivalence(UpperCamelCase_		,  UpperCamelCase_		,  UpperCamelCase_       )

            UpperCAmelCase_											=self._prepare_for_class(UpperCamelCase_		,  UpperCamelCase_       )
            UpperCAmelCase_											=self._prepare_for_class(UpperCamelCase_		,  UpperCamelCase_       )
            check_equivalence(UpperCamelCase_		,  UpperCamelCase_		,  UpperCamelCase_		,  {"output_hidden_states": True}       )

            UpperCAmelCase_											=self._prepare_for_class(UpperCamelCase_		,  UpperCamelCase_		,  return_labels=UpperCamelCase_       )
            UpperCAmelCase_											=self._prepare_for_class(UpperCamelCase_		,  UpperCamelCase_		,  return_labels=UpperCamelCase_       )
            check_equivalence(UpperCamelCase_		,  UpperCamelCase_		,  UpperCamelCase_		,  {"output_hidden_states": True}       )

@require_torch
class      A       (	unittest.TestCase		,      __lowerCamelCase						):
      _snake_case							     =(MaskFormerSwinBackbone,) if is_torch_available() else ()
      _snake_case							     =MaskFormerSwinConfig



      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    List[str]:
         '''simple docstring'''

         UpperCAmelCase_											=MaskFormerSwinModelTester(self       )






      def 	lowerCAmelCase__    (	self:						Any       )						->    str:
         '''simple docstring'''

         UpperCAmelCase_						,       UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs_for_common()
         UpperCAmelCase_											=inputs_dict["pixel_values"].shape[0]

         for backbone_class in self.all_model_classes:
            UpperCAmelCase_											=backbone_class(UpperCamelCase_       )
            backbone.to(UpperCamelCase_       )
            backbone.eval()

            UpperCAmelCase_											=backbone(**UpperCamelCase_       )

            # Test default outputs and verify feature maps
            self.assertIsInstance(outputs.feature_maps		,  UpperCamelCase_       )
            self.assertTrue(len(outputs.feature_maps       ) == len(backbone.channels       )       )
            for feature_map, n_channels in zip(outputs.feature_maps		,  backbone.channels       ):
               self.assertTrue(feature_map.shape[:2]		,  (batch_size, n_channels)       )
            self.assertIsNone(outputs.hidden_states       )
            self.assertIsNone(outputs.attentions       )

            # Test output_hidden_states=True
            UpperCAmelCase_											=backbone(**UpperCamelCase_		,  output_hidden_states=UpperCamelCase_       )
            self.assertIsNotNone(outputs.hidden_states       )
            self.assertTrue(len(outputs.hidden_states       )		,  len(backbone.stage_names       )       )
            # We skip the stem layer
            for hidden_states, n_channels in zip(outputs.hidden_states[1:]		,  backbone.channels       ):
               for hidden_state in hidden_states:
                  # Hidden states are in the format (batch_size, (height * width), n_channels)
                  UpperCAmelCase_						,       UpperCAmelCase_						,       UpperCAmelCase_											=hidden_state.shape
                  self.assertTrue((h_batch_size, h_n_channels)		,  (batch_size, n_channels)       )

            # Test output_attentions=True
            if self.has_attentions:
               UpperCAmelCase_											=backbone(**UpperCamelCase_		,  output_attentions=UpperCamelCase_       )
               self.assertIsNotNone(outputs.attentions       )











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       :



      def __init__(	self:						Union[str, Any]		,  _lowerCAmelCase:						str		,  _lowerCAmelCase:						Tuple=13		,  _lowerCAmelCase:						Tuple=7		,  _lowerCAmelCase:						str=True		,  _lowerCAmelCase:						Optional[Any]=True		,  _lowerCAmelCase:						Union[str, Any]=True		,  _lowerCAmelCase:						Dict=True		,  _lowerCAmelCase:						List[Any]=99		,  _lowerCAmelCase:						str=24		,  _lowerCAmelCase:						List[str]=2		,  _lowerCAmelCase:						List[str]=6		,  _lowerCAmelCase:						Optional[int]=37		,  _lowerCAmelCase:						Dict="gelu"		,  _lowerCAmelCase:						Dict=0.1		,  _lowerCAmelCase:						List[str]=0.1		,  _lowerCAmelCase:						List[str]=512		,  _lowerCAmelCase:						List[str]=16		,  _lowerCAmelCase:						List[str]=2		,  _lowerCAmelCase:						Tuple=0.02		,  _lowerCAmelCase:						List[str]=3		,  _lowerCAmelCase:						List[str]=None		,  _lowerCAmelCase:						Optional[int]=1000		,  )						->    Tuple:
         '''simple docstring'''

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



      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    int:
         '''simple docstring'''

         UpperCAmelCase_											=ids_tensor([self.batch_size, self.seq_length]		,  self.vocab_size       )

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

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

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

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

         UpperCAmelCase_											=self.get_config()

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



      def 	lowerCAmelCase__    (	self:						List[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 	lowerCAmelCase__    (	self:						List[Any]		,  _lowerCAmelCase:						str		,  _lowerCAmelCase:						List[str]		,  _lowerCAmelCase:						Union[str, Any]		,  _lowerCAmelCase:						Union[str, Any]		,  _lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						Tuple		,  _lowerCAmelCase:						Optional[int]		,  )						->    Union[str, Any]:
         '''simple docstring'''

         UpperCAmelCase_											=LiltModel(config=_lowerCAmelCase       )
         model.to(_lowerCAmelCase       )
         model.eval()
         UpperCAmelCase_											=model(_lowerCAmelCase		,  bbox=_lowerCAmelCase		,  attention_mask=_lowerCAmelCase		,  token_type_ids=_lowerCAmelCase       )
         UpperCAmelCase_											=model(_lowerCAmelCase		,  bbox=_lowerCAmelCase		,  token_type_ids=_lowerCAmelCase       )
         UpperCAmelCase_											=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 	lowerCAmelCase__    (	self:						Optional[Any]		,  _lowerCAmelCase:						Union[str, Any]		,  _lowerCAmelCase:						int		,  _lowerCAmelCase:						str		,  _lowerCAmelCase:						Tuple		,  _lowerCAmelCase:						List[str]		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						Tuple		,  )						->    Tuple:
         '''simple docstring'''

         UpperCAmelCase_											=self.num_labels
         UpperCAmelCase_											=LiltForTokenClassification(config=_lowerCAmelCase       )
         model.to(_lowerCAmelCase       )
         model.eval()
         UpperCAmelCase_											=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 	lowerCAmelCase__    (	self:						List[Any]		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						List[str]		,  _lowerCAmelCase:						List[str]		,  _lowerCAmelCase:						List[str]		,  _lowerCAmelCase:						Tuple		,  _lowerCAmelCase:						int		,  _lowerCAmelCase:						Union[str, Any]		,  )						->    Optional[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=LiltForQuestionAnswering(config=_lowerCAmelCase       )
         model.to(_lowerCAmelCase       )
         model.eval()
         UpperCAmelCase_											=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 	lowerCAmelCase__    (	self:						List[Any]       )						->    Optional[int]:
         '''simple docstring'''

         UpperCAmelCase_											=self.prepare_config_and_inputs()
         (
             (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       
         )											=config_and_inputs
         UpperCAmelCase_											={
             "input_ids": input_ids,
             "bbox": bbox,
             "token_type_ids": token_type_ids,
             "attention_mask": input_mask,
         }
         return config, inputs_dict

@require_torch
class      A       (	__lowercase		,      __lowercase		,      __lowercase		,      unittest.TestCase						):
      _snake_case							     =(
          (
              LiltModel,
              LiltForSequenceClassification,
              LiltForTokenClassification,
              LiltForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      _snake_case							     =(
          {
              '''feature-extraction''': LiltModel,
              '''question-answering''': LiltForQuestionAnswering,
              '''text-classification''': LiltForSequenceClassification,
              '''token-classification''': LiltForTokenClassification,
              '''zero-shot''': LiltForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      _snake_case							     =False
      _snake_case							     =False



      def 	lowerCAmelCase__    (	self:						Any		,  _lowerCAmelCase:						Any		,  _lowerCAmelCase:						Dict		,  _lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						Tuple		,  _lowerCAmelCase:						Optional[Any]       )						->    Tuple:
         '''simple docstring'''

         return True



      def 	lowerCAmelCase__    (	self:						Any       )						->    List[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=LiltModelTester(self       )
         UpperCAmelCase_											=ConfigTester(self		,  config_class=_lowerCAmelCase		,  hidden_size=37       )



      def 	lowerCAmelCase__    (	self:						Union[str, Any]       )						->    List[Any]:
         '''simple docstring'''

         self.config_tester.run_common_tests()



      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    str:
         '''simple docstring'''

         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    int:
         '''simple docstring'''

         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
            UpperCAmelCase_											=type
            self.model_tester.create_and_check_model(*_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Any       )						->    Tuple:
         '''simple docstring'''

         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Dict       )						->    Optional[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*_lowerCAmelCase       )






      @slow
      def 	lowerCAmelCase__    (	self:						Tuple       )						->    int:
         '''simple docstring'''

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

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



      def 	lowerCAmelCase__    (	self:						str       )						->    Optional[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=LiltModel.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base"       ).to(_lowerCAmelCase       )

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

         # forward pass
         with torch.no_grad():
            UpperCAmelCase_											=model(input_ids=_lowerCAmelCase		,  bbox=_lowerCAmelCase       )

         UpperCAmelCase_											=torch.Size([1, 2, 768]       )
         UpperCAmelCase_											=torch.tensor(
             [[-0.06_53, 0.09_50, -0.00_61], [-0.05_45, 0.09_26, -0.03_24]]		,  device=_lowerCAmelCase		,  )

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




