





"""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 		__snake_case :





 """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=512 ,						__lowerCamelCase=16 ,						__lowerCamelCase=2 ,						__lowerCamelCase=0.0_2 ,						__lowerCamelCase=3 ,						__lowerCamelCase=None ,						__lowerCamelCase=1000 ,						):



     '''simple docstring'''




     __A   :						int									=					parent
     __A   :						Optional[Any]									=					batch_size
     __A   :						Union[str, Any]									=					seq_length
     __A   :						Tuple									=					is_training
     __A   :						Any									=					use_input_mask
     __A   :						str									=					use_token_type_ids
     __A   :						int									=					use_labels
     __A   :						Dict									=					vocab_size
     __A   :						List[Any]									=					hidden_size
     __A   :						Tuple									=					num_hidden_layers
     __A   :						int									=					num_attention_heads
     __A   :						Dict									=					intermediate_size
     __A   :						Tuple									=					hidden_act
     __A   :						List[str]									=					hidden_dropout_prob
     __A   :						str									=					attention_probs_dropout_prob
     __A   :						Tuple									=					max_position_embeddings
     __A   :						List[str]									=					type_vocab_size
     __A   :						Union[str, Any]									=					type_sequence_label_size
     __A   :						Optional[Any]									=					initializer_range
     __A   :						Tuple									=					num_labels
     __A   :						Any									=					scope
     __A   :						List[Any]									=					range_bbox

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						str									=					ids_tensor([self.batch_size, self.seq_length] ,						self.vocab_size  )

     __A   :						List[Any]									=					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   :						List[Any]									=					bbox[i, j, 3]
                 __A   :						Optional[Any]									=					bbox[i, j, 1]
                 __A   :						Any									=					t
             if bbox[i, j, 2] < bbox[i, j, 0]:
                 __A   :						Optional[Any]									=					bbox[i, j, 2]
                 __A   :						List[Any]									=					bbox[i, j, 0]
                 __A   :						int									=					t

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

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

     __A   :						Tuple									=					None
     __A   :						Optional[Any]									=					None
     if self.use_labels:
         __A   :						Tuple									=					ids_tensor([self.batch_size] ,						self.type_sequence_label_size  )
         __A   :						str									=					ids_tensor([self.batch_size, self.seq_length] ,						self.num_labels  )

     __A   :						List[Any]									=					self.get_config()

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

 def  UpperCamelCase__(   self  ):



     '''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 ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						):



     '''simple docstring'''




     __A   :						List[Any]									=					LiltModel(config=lowercase__  )
     model.to(lowercase__  )
     model.eval()
     __A   :						Tuple									=					model(lowercase__ ,						bbox=lowercase__ ,						attention_mask=lowercase__ ,						token_type_ids=lowercase__  )
     __A   :						Dict									=					model(lowercase__ ,						bbox=lowercase__ ,						token_type_ids=lowercase__  )
     __A   :						Optional[int]									=					model(lowercase__ ,						bbox=lowercase__  )

     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 ,						):



     '''simple docstring'''




     __A   :						Tuple									=					self.num_labels
     __A   :						List[str]									=					LiltForTokenClassification(config=lowercase__  )
     model.to(lowercase__  )
     model.eval()
     __A   :						int									=					model(
         lowercase__ ,						bbox=lowercase__ ,						attention_mask=lowercase__ ,						token_type_ids=lowercase__ ,						labels=lowercase__  )
     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 ,						):



     '''simple docstring'''




     __A   :						Optional[int]									=					LiltForQuestionAnswering(config=lowercase__  )
     model.to(lowercase__  )
     model.eval()
     __A   :						List[Any]									=					model(
         lowercase__ ,						bbox=lowercase__ ,						attention_mask=lowercase__ ,						token_type_ids=lowercase__ ,						start_positions=lowercase__ ,						end_positions=lowercase__ ,						)
     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  ):



     '''simple docstring'''




     __A   :						List[str]									=					self.prepare_config_and_inputs()
     (
         (
         __A     
     )			,	(
         __A     
     )			,	(
         __A     
     )			,	(
         __A     
     )			,	(
         __A     
     )			,	(
         __A     
     )			,	(
         __A     
     )			,	     
     )   :						Dict									=					config_and_inputs
     __A   :						int									=					{
         '''input_ids''': input_ids,
         '''bbox''': bbox,
         '''token_type_ids''': token_type_ids,
         '''attention_mask''': input_mask,
     }
     return config, inputs_dict






@require_torch
class 		__snake_case (		lowerCamelCase__							,							lowerCamelCase__							,							lowerCamelCase__							,							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 ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase  ):



     '''simple docstring'''




     return True

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Any									=					LiltModelTester(self  )
     __A   :						str									=					ConfigTester(self ,						config_class=lowercase__ ,						hidden_size=37  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     self.config_tester.run_common_tests()

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Optional[Any]									=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase__  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					self.model_tester.prepare_config_and_inputs()
     for type in ["absolute", "relative_key", "relative_key_query"]:
         __A   :						Optional[int]									=					type
         self.model_tester.create_and_check_model(*lowercase__  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						str									=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*lowercase__  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Union[str, Any]									=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*lowercase__  )







 @slow
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         __A   :						str									=					LiltModel.from_pretrained(lowercase__  )
         self.assertIsNotNone(lowercase__  )






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





 """simple docstring"""


 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						int									=					LiltModel.from_pretrained('''SCUT-DLVCLab/lilt-roberta-en-base'''  ).to(lowercase__  )

     __A   :						Tuple									=					torch.tensor([[1, 2]] ,						device=lowercase__  )
     __A   :						List[str]									=					torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]] ,						device=lowercase__  )

     # forward pass
     with torch.no_grad():
         __A   :						List[Any]									=					model(input_ids=lowercase__ ,						bbox=lowercase__  )

     __A   :						str									=					torch.Size([1, 2, 768]  )
     __A   :						str									=					torch.tensor(
         [[-0.0_6_5_3, 0.0_9_5_0, -0.0_0_6_1], [-0.0_5_4_5, 0.0_9_2_6, -0.0_3_2_4]] ,						device=lowercase__ ,						)

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











"""simple docstring"""



from collections import UserDict
from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
       from PIL import Image

       from ..image_utils import load_image

if is_torch_available():
       from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
       from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
       from ..tf_utils import stable_softmax

a_			=      logging.get_logger(__name__)






@add_end_docstrings(SCREAMING_SNAKE_CASE__					)
class 		__snake_case (		SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 def __init__(   self ,						**__lowerCamelCase  ):



     '''simple docstring'''




     super().__init__(**__lowerCamelCase  )

     requires_backends(self ,						'''vision'''  )
     self.check_model_type(
         TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
         if self.framework == '''tf'''
         else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING  )

 def __call__(   self ,						__lowerCamelCase ,						**__lowerCamelCase  ):



     '''simple docstring'''




     return super().__call__(__lowerCamelCase ,						**__lowerCamelCase  )

 def  UpperCamelCase__(   self ,						**__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Union[str, Any]									=					{}
     if "candidate_labels" in kwargs:
         __A   :						Tuple									=					kwargs['''candidate_labels''']
     if "hypothesis_template" in kwargs:
         __A   :						List[str]									=					kwargs['''hypothesis_template''']

     return preprocess_params, {}, {}

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase=None ,						__lowerCamelCase="This is a photo of {}."  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					load_image(__lowerCamelCase  )
     __A   :						Optional[int]									=					self.image_processor(images=[image] ,						return_tensors=self.framework  )
     __A   :						int									=					candidate_labels
     __A   :						int									=					[hypothesis_template.format(__lowerCamelCase  ) for x in candidate_labels]
     __A   :						Dict									=					self.tokenizer(__lowerCamelCase ,						return_tensors=self.framework ,						padding=__lowerCamelCase  )
     __A   :						int									=					[text_inputs]
     return inputs

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					model_inputs.pop('''candidate_labels'''  )
     __A   :						str									=					model_inputs.pop('''text_inputs'''  )
     if isinstance(text_inputs[0] ,						__lowerCamelCase  ):
         __A   :						Union[str, Any]									=					text_inputs[0]
     else:
         # Batching case.
         __A   :						str									=					text_inputs[0][0]

     __A   :						List[str]									=					self.model(**__lowerCamelCase ,						**__lowerCamelCase  )

     __A   :						Dict									=					{
         '''candidate_labels''': candidate_labels,
         '''logits''': outputs.logits_per_image,
     }
     return model_outputs







 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					model_outputs.pop('''candidate_labels'''  )
     __A   :						int									=					model_outputs['''logits'''][0]
     if self.framework == "pt":
         __A   :						Union[str, Any]									=					logits.softmax(dim=-1  ).squeeze(-1  )
         __A   :						Dict									=					probs.tolist()
         if not isinstance(__lowerCamelCase ,						__lowerCamelCase  ):
             __A   :						List[Any]									=					[scores]
     elif self.framework == "tf":
         __A   :						List[Any]									=					stable_softmax(__lowerCamelCase ,						axis=-1  )
         __A   :						str									=					probs.numpy().tolist()
     else:
         raise ValueError(F"""Unsupported framework: {self.framework}"""  )

     __A   :						str									=					[
         {'''score''': score, '''label''': candidate_label}
         for score, candidate_label in sorted(zip(__lowerCamelCase ,						__lowerCamelCase  ) ,						key=lambda __lowerCamelCase  : -x[0]  )
     ]
     return result




