


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

    '''simple docstring'''






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





           '''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  SCREAMING_SNAKE_CASE							(    self   :			Optional[int])		->str:





           '''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  SCREAMING_SNAKE_CASE							(    self   :			Optional[int])		->Optional[int]:





           '''simple docstring'''

           return LiltConfig(
               vocab_size=self.vocab_size      ,				hidden_size=self.hidden_size      ,				num_hidden_layers=self.num_hidden_layers      ,				num_attention_heads=self.num_attention_heads      ,				intermediate_size=self.intermediate_size      ,				hidden_act=self.hidden_act      ,				hidden_dropout_prob=self.hidden_dropout_prob      ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,				max_position_embeddings=self.max_position_embeddings      ,				type_vocab_size=self.type_vocab_size      ,				initializer_range=self.initializer_range      ,				)


    def  SCREAMING_SNAKE_CASE							(    self   :			Optional[int]      ,				UpperCAmelCase__   :			int      ,				UpperCAmelCase__   :			Dict      ,				UpperCAmelCase__   :			Optional[Any]      ,				UpperCAmelCase__   :			Dict      ,				UpperCAmelCase__   :			int      ,				UpperCAmelCase__   :			Optional[Any]      ,				UpperCAmelCase__   :			Any      ,				)		->List[str]:





           '''simple docstring'''

           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  SCREAMING_SNAKE_CASE							(    self   :			List[str]      ,				UpperCAmelCase__   :			Any      ,				UpperCAmelCase__   :			Optional[int]      ,				UpperCAmelCase__   :			Optional[Any]      ,				UpperCAmelCase__   :			Any      ,				UpperCAmelCase__   :			int      ,				UpperCAmelCase__   :			int      ,				UpperCAmelCase__   :			Any      ,				)		->Union[str, Any]:





           '''simple docstring'''

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





           '''simple docstring'''

           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  SCREAMING_SNAKE_CASE							(    self   :			int)		->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        UpperCamelCase_					(				UpperCAmelCase__	,      UpperCAmelCase__	,      UpperCAmelCase__	,      unittest.TestCase  ):

    '''simple docstring'''






    UpperCAmelCase__													=   (
        (
            LiltModel,
            LiltForSequenceClassification,
            LiltForTokenClassification,
            LiltForQuestionAnswering,
        )
        if is_torch_available()
        else ()
    )
    UpperCAmelCase__													=   (
        {
            '''feature-extraction''': LiltModel,
            '''question-answering''': LiltForQuestionAnswering,
            '''text-classification''': LiltForSequenceClassification,
            '''token-classification''': LiltForTokenClassification,
            '''zero-shot''': LiltForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    UpperCAmelCase__													=   False
    UpperCAmelCase__													=   False


    def  SCREAMING_SNAKE_CASE							(    self   :			Optional[int]      ,				UpperCAmelCase__   :			Union[str, Any]      ,				UpperCAmelCase__   :			Optional[int]      ,				UpperCAmelCase__   :			Tuple      ,				UpperCAmelCase__   :			List[Any]      ,				UpperCAmelCase__   :			List[str])		->Tuple:





           '''simple docstring'''

           return True


    def  SCREAMING_SNAKE_CASE							(    self   :			Tuple)		->Union[str, Any]:





           '''simple docstring'''

           A__   				=					LiltModelTester(self)
           A__   				=					ConfigTester(self      ,				config_class=UpperCAmelCase__      ,				hidden_size=37)


    def  SCREAMING_SNAKE_CASE							(    self   :			Tuple)		->int:





           '''simple docstring'''

           self.config_tester.run_common_tests()


    def  SCREAMING_SNAKE_CASE							(    self   :			Dict)		->Dict:





           '''simple docstring'''

           A__   				=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCAmelCase__)


    def  SCREAMING_SNAKE_CASE							(    self   :			Optional[Any])		->List[Any]:





           '''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(*UpperCAmelCase__)


    def  SCREAMING_SNAKE_CASE							(    self   :			int)		->List[Any]:





           '''simple docstring'''

           A__   				=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase__)


    def  SCREAMING_SNAKE_CASE							(    self   :			Dict)		->List[Any]:





           '''simple docstring'''

           A__   				=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase__)





    @slow
    def  SCREAMING_SNAKE_CASE							(    self   :			Dict)		->int:





           '''simple docstring'''

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


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

    '''simple docstring'''






    def  SCREAMING_SNAKE_CASE							(    self   :			int)		->Any:





           '''simple docstring'''

           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, 768])
           A__   				=					torch.tensor(
               [[-0.0653, 0.0950, -0.0061], [-0.0545, 0.0926, -0.0324]]      ,				device=UpperCAmelCase__      ,				)

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








from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

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


if is_tf_available():
  import tensorflow as tf

  from transformers import TFViTForImageClassification, TFViTModel


if is_vision_available():
  from PIL import Image

  from transformers import ViTImageProcessor


class        UpperCamelCase_					:

    '''simple docstring'''






    def __init__(    self   :			Dict      ,				UpperCAmelCase__   :			Optional[Any]      ,				UpperCAmelCase__   :			str=13      ,				UpperCAmelCase__   :			Union[str, Any]=30      ,				UpperCAmelCase__   :			List[str]=2      ,				UpperCAmelCase__   :			List[Any]=3      ,				UpperCAmelCase__   :			int=True      ,				UpperCAmelCase__   :			Tuple=True      ,				UpperCAmelCase__   :			Optional[int]=32      ,				UpperCAmelCase__   :			Tuple=2      ,				UpperCAmelCase__   :			Optional[Any]=4      ,				UpperCAmelCase__   :			Any=37      ,				UpperCAmelCase__   :			List[Any]="gelu"      ,				UpperCAmelCase__   :			Dict=0.1      ,				UpperCAmelCase__   :			List[Any]=0.1      ,				UpperCAmelCase__   :			Union[str, Any]=10      ,				UpperCAmelCase__   :			int=0.02      ,				UpperCAmelCase__   :			Optional[Any]=3      ,				UpperCAmelCase__   :			List[Any]=None      ,				)		->Dict:





           '''simple docstring'''

           A__   				=					parent
           A__   				=					batch_size
           A__   				=					image_size
           A__   				=					patch_size
           A__   				=					num_channels
           A__   				=					is_training
           A__   				=					use_labels
           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__   				=					type_sequence_label_size
           A__   				=					initializer_range
           A__   				=					scope

           # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
           A__   				=					(image_size // patch_size) ** 2
           A__   				=					num_patches + 1


    def  SCREAMING_SNAKE_CASE							(    self   :			List[Any])		->List[Any]:





           '''simple docstring'''

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

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

           A__   				=					self.get_config()

           return config, pixel_values, labels


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





           '''simple docstring'''

           return ViTConfig(
               image_size=self.image_size      ,				patch_size=self.patch_size      ,				num_channels=self.num_channels      ,				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      ,				is_decoder=UpperCAmelCase__      ,				initializer_range=self.initializer_range      ,				)


    def  SCREAMING_SNAKE_CASE							(    self   :			List[Any]      ,				UpperCAmelCase__   :			List[str]      ,				UpperCAmelCase__   :			Any      ,				UpperCAmelCase__   :			str)		->Tuple:





           '''simple docstring'''

           A__   				=					TFViTModel(config=UpperCAmelCase__)
           A__   				=					model(UpperCAmelCase__      ,				training=UpperCAmelCase__)
           self.parent.assertEqual(result.last_hidden_state.shape      ,				(self.batch_size, self.seq_length, self.hidden_size))

           # Test with an image with different size than the one specified in config.
           A__   				=					self.image_size // 2
           A__   				=					pixel_values[:, :, :image_size, :image_size]
           A__   				=					model(UpperCAmelCase__      ,				interpolate_pos_encoding=UpperCAmelCase__      ,				training=UpperCAmelCase__)
           A__   				=					(image_size // self.patch_size) ** 2 + 1
           self.parent.assertEqual(result.last_hidden_state.shape      ,				(self.batch_size, seq_length, self.hidden_size))


    def  SCREAMING_SNAKE_CASE							(    self   :			Any      ,				UpperCAmelCase__   :			Tuple      ,				UpperCAmelCase__   :			List[Any]      ,				UpperCAmelCase__   :			List[str])		->List[str]:





           '''simple docstring'''

           A__   				=					self.type_sequence_label_size
           A__   				=					TFViTForImageClassification(UpperCAmelCase__)
           A__   				=					model(UpperCAmelCase__      ,				labels=UpperCAmelCase__      ,				training=UpperCAmelCase__)
           self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.type_sequence_label_size))

           # Test with an image with different size than the one specified in config.
           A__   				=					self.image_size // 2
           A__   				=					pixel_values[:, :, :image_size, :image_size]
           A__   				=					model(UpperCAmelCase__      ,				interpolate_pos_encoding=UpperCAmelCase__      ,				training=UpperCAmelCase__)
           self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.type_sequence_label_size))

           # test greyscale images
           A__   				=					1
           A__   				=					TFViTForImageClassification(UpperCAmelCase__)
           A__   				=					floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
           A__   				=					model(UpperCAmelCase__)
           self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.type_sequence_label_size))





    def  SCREAMING_SNAKE_CASE							(    self   :			List[Any])		->int:





           '''simple docstring'''

           A__   				=					self.prepare_config_and_inputs()
           A__				,							A__				,							A__   				=					config_and_inputs
           A__   				=					{'''pixel_values''': pixel_values}
           return config, inputs_dict


@require_tf
class        UpperCamelCase_					(				UpperCAmelCase__	,      UpperCAmelCase__	,      unittest.TestCase  ):

    '''simple docstring'''






    UpperCAmelCase__													=   (TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
    UpperCAmelCase__													=   (
        {'''feature-extraction''': TFViTModel, '''image-classification''': TFViTForImageClassification}
        if is_tf_available()
        else {}
    )

    UpperCAmelCase__													=   False
    UpperCAmelCase__													=   False
    UpperCAmelCase__													=   False


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





           '''simple docstring'''

           A__   				=					TFViTModelTester(self)
           A__   				=					ConfigTester(self      ,				config_class=UpperCAmelCase__      ,				has_text_modality=UpperCAmelCase__      ,				hidden_size=37)


    def  SCREAMING_SNAKE_CASE							(    self   :			str)		->List[str]:





           '''simple docstring'''

           self.config_tester.run_common_tests()


    @unittest.skip(reason='''ViT does not use inputs_embeds''')
    def  SCREAMING_SNAKE_CASE							(    self   :			List[Any])		->Tuple:





           '''simple docstring'''

           pass


    @unittest.skip(reason='''ViT does not use inputs_embeds''')
    def  SCREAMING_SNAKE_CASE							(    self   :			int)		->Optional[Any]:





           '''simple docstring'''

           pass


    def  SCREAMING_SNAKE_CASE							(    self   :			int)		->List[Any]:





           '''simple docstring'''

           A__				,							A__   				=					self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                  A__   				=					model_class(UpperCAmelCase__)
                  self.assertIsInstance(model.get_input_embeddings()      ,				(tf.keras.layers.Layer))
                  A__   				=					model.get_output_embeddings()
                  self.assertTrue(x is None or isinstance(UpperCAmelCase__      ,				tf.keras.layers.Layer))


    def  SCREAMING_SNAKE_CASE							(    self   :			int)		->Any:





           '''simple docstring'''

           A__				,							A__   				=					self.model_tester.prepare_config_and_inputs_for_common()

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

                  A__   				=					['''pixel_values''']
                  self.assertListEqual(arg_names[:1]      ,				UpperCAmelCase__)


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





           '''simple docstring'''

           A__   				=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCAmelCase__)


    def  SCREAMING_SNAKE_CASE							(    self   :			Dict)		->Optional[Any]:





           '''simple docstring'''

           A__   				=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase__)





    @slow
    def  SCREAMING_SNAKE_CASE							(    self   :			Optional[int])		->List[Any]:





           '''simple docstring'''

           A__   				=					TFViTModel.from_pretrained('''google/vit-base-patch16-224''')
           self.assertIsNotNone(UpperCAmelCase__)







def SCREAMING_SNAKE_CASE       ( )     -> int:



       """simple docstring"""

       A__   				=					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
       return image


@require_tf
@require_vision
class        UpperCamelCase_					(				unittest.TestCase  ):

    '''simple docstring'''






    @cached_property
    def  SCREAMING_SNAKE_CASE							(    self   :			Dict)		->Optional[Any]:





           '''simple docstring'''

           return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224''') if is_vision_available() else None





    @slow
    def  SCREAMING_SNAKE_CASE							(    self   :			Any)		->int:





           '''simple docstring'''

           A__   				=					TFViTForImageClassification.from_pretrained('''google/vit-base-patch16-224''')

           A__   				=					self.default_image_processor
           A__   				=					prepare_img()
           A__   				=					image_processor(images=UpperCAmelCase__      ,				return_tensors='''tf''')

           # forward pass
           A__   				=					model(**UpperCAmelCase__)

           # verify the logits
           A__   				=					tf.TensorShape((1, 1_000))
           self.assertEqual(outputs.logits.shape      ,				UpperCAmelCase__)

           A__   				=					tf.constant([-0.2744, 0.8215, -0.0836])

           tf.debugging.assert_near(outputs.logits[0, :3]      ,				UpperCAmelCase__      ,				atol=1e-4)


