






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 :       List[str]							,       SCREAMING_SNAKE_CASE :       Dict							,       SCREAMING_SNAKE_CASE :       Dict=13							,       SCREAMING_SNAKE_CASE :       List[Any]=7							,       SCREAMING_SNAKE_CASE :       Any=True							,       SCREAMING_SNAKE_CASE :       Union[str, Any]=True							,       SCREAMING_SNAKE_CASE :       Dict=True							,       SCREAMING_SNAKE_CASE :       Optional[int]=True							,       SCREAMING_SNAKE_CASE :       Union[str, Any]=99							,       SCREAMING_SNAKE_CASE :       List[str]=24							,       SCREAMING_SNAKE_CASE :       str=2							,       SCREAMING_SNAKE_CASE :       List[str]=6							,       SCREAMING_SNAKE_CASE :       Optional[int]=37							,       SCREAMING_SNAKE_CASE :       int="gelu"							,       SCREAMING_SNAKE_CASE :       Optional[int]=0.1							,       SCREAMING_SNAKE_CASE :       Optional[int]=0.1							,       SCREAMING_SNAKE_CASE :       List[Any]=5_12							,       SCREAMING_SNAKE_CASE :       Any=16							,       SCREAMING_SNAKE_CASE :       Any=2							,       SCREAMING_SNAKE_CASE :       str=0.0_2							,       SCREAMING_SNAKE_CASE :       List[Any]=3							,       SCREAMING_SNAKE_CASE :       List[Any]=None							,       SCREAMING_SNAKE_CASE :       Tuple=10_00							,       ):


      '''simple docstring'''






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







 def  __lowercase (		self :       Tuple						):


      '''simple docstring'''






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

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

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

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

      UpperCamelCase__      :       Dict	    =  None
      UpperCamelCase__      :       Union[str, Any]	    =  None
      if self.use_labels:
           UpperCamelCase__      :       Tuple	    =  ids_tensor([self.batch_size]							,       self.type_sequence_label_size						)
           UpperCamelCase__      :       List[Any]	    =  ids_tensor([self.batch_size, self.seq_length]							,       self.num_labels						)

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

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







 def  __lowercase (		self :       Tuple						):


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


      '''simple docstring'''






      UpperCamelCase__      :       Tuple	    =  LiltModel(config=snake_case_						)
      model.to(snake_case_						)
      model.eval()
      UpperCamelCase__      :       Dict	    =  model(snake_case_							,       bbox=snake_case_							,       attention_mask=snake_case_							,       token_type_ids=snake_case_						)
      UpperCamelCase__      :       List[str]	    =  model(snake_case_							,       bbox=snake_case_							,       token_type_ids=snake_case_						)
      UpperCamelCase__      :       List[str]	    =  model(snake_case_							,       bbox=snake_case_						)

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


      '''simple docstring'''






      UpperCamelCase__      :       Optional[int]	    =  self.num_labels
      UpperCamelCase__      :       Tuple	    =  LiltForTokenClassification(config=snake_case_						)
      model.to(snake_case_						)
      model.eval()
      UpperCamelCase__      :       Optional[int]	    =  model(
          snake_case_							,       bbox=snake_case_							,       attention_mask=snake_case_							,       token_type_ids=snake_case_							,       labels=snake_case_						)
      self.parent.assertEqual(result.logits.shape							,       (self.batch_size, self.seq_length, self.num_labels)						)







 def  __lowercase (		self :       Optional[Any]							,       SCREAMING_SNAKE_CASE :       Dict							,       SCREAMING_SNAKE_CASE :       int							,       SCREAMING_SNAKE_CASE :       Optional[Any]							,       SCREAMING_SNAKE_CASE :       Any							,       SCREAMING_SNAKE_CASE :       List[str]							,       SCREAMING_SNAKE_CASE :       Optional[Any]							,       SCREAMING_SNAKE_CASE :       Any							,       ):


      '''simple docstring'''






      UpperCamelCase__      :       Tuple	    =  LiltForQuestionAnswering(config=snake_case_						)
      model.to(snake_case_						)
      model.eval()
      UpperCamelCase__      :       Union[str, Any]	    =  model(
          snake_case_							,       bbox=snake_case_							,       attention_mask=snake_case_							,       token_type_ids=snake_case_							,       start_positions=snake_case_							,       end_positions=snake_case_							,       )
      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  __lowercase (		self :       Dict						):


      '''simple docstring'''






      UpperCamelCase__      :       Any	    =  self.prepare_config_and_inputs()
      (
          UpperCamelCase__	
      )      :       int	    =  config_and_inputs
      UpperCamelCase__      :       Optional[Any]	    =  {
          '''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	):
 _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	:	Dict				=			False
 _lowerCAmelCase	:	Optional[int]				=			False







 def  __lowercase (		self :       str							,       SCREAMING_SNAKE_CASE :       Optional[Any]							,       SCREAMING_SNAKE_CASE :       Dict							,       SCREAMING_SNAKE_CASE :       Optional[Any]							,       SCREAMING_SNAKE_CASE :       Optional[int]							,       SCREAMING_SNAKE_CASE :       Union[str, Any]						):


      '''simple docstring'''






      return True







 def  __lowercase (		self :       List[Any]						):


      '''simple docstring'''






      UpperCamelCase__      :       Dict	    =  LiltModelTester(self						)
      UpperCamelCase__      :       Union[str, Any]	    =  ConfigTester(self							,       config_class=snake_case_							,       hidden_size=37						)







 def  __lowercase (		self :       str						):


      '''simple docstring'''






      self.config_tester.run_common_tests()







 def  __lowercase (		self :       int						):


      '''simple docstring'''






      UpperCamelCase__      :       Optional[Any]	    =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*snake_case_						)







 def  __lowercase (		self :       Tuple						):


      '''simple docstring'''






      UpperCamelCase__      :       Dict	    =  self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
           UpperCamelCase__      :       List[str]	    =  type
           self.model_tester.create_and_check_model(*snake_case_						)







 def  __lowercase (		self :       int						):


      '''simple docstring'''






      UpperCamelCase__      :       Optional[Any]	    =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*snake_case_						)







 def  __lowercase (		self :       Any						):


      '''simple docstring'''






      UpperCamelCase__      :       int	    =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*snake_case_						)







 @slow
 def  __lowercase (		self :       Dict						):


      '''simple docstring'''






      for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__      :       Union[str, Any]	    =  LiltModel.from_pretrained(snake_case_						)
           self.assertIsNotNone(snake_case_						)







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







 def  __lowercase (		self :       Dict						):


      '''simple docstring'''






      UpperCamelCase__      :       int	    =  LiltModel.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base"						).to(snake_case_						)

      UpperCamelCase__      :       str	    =  torch.tensor([[1, 2]]							,       device=snake_case_						)
      UpperCamelCase__      :       Dict	    =  torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]							,       device=snake_case_						)

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

      UpperCamelCase__      :       Tuple	    =  torch.Size([1, 2, 7_68]						)
      UpperCamelCase__      :       Optional[Any]	    =  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=snake_case_							,       )

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








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

lowerCamelCase     :							str    =logging.get_logger(__name__)





@add_end_docstrings(A__	)
class       __a  (   A__	):







 def __init__(		self :       List[str]							,       **SCREAMING_SNAKE_CASE :       Tuple						):


      '''simple docstring'''






      super().__init__(**SCREAMING_SNAKE_CASE						)

      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 :       Dict							,       SCREAMING_SNAKE_CASE :       Union[str, List[str], "Image", List["Image"]]							,       **SCREAMING_SNAKE_CASE :       str						):


      '''simple docstring'''






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







 def  __lowercase (		self :       List[str]							,       **SCREAMING_SNAKE_CASE :       List[str]						):


      '''simple docstring'''






      UpperCamelCase__      :       List[Any]	    =  {}
      if "candidate_labels" in kwargs:
           UpperCamelCase__      :       Optional[Any]	    =  kwargs["candidate_labels"]
      if "hypothesis_template" in kwargs:
           UpperCamelCase__      :       int	    =  kwargs["hypothesis_template"]

      return preprocess_params, {}, {}







 def  __lowercase (		self :       List[Any]							,       SCREAMING_SNAKE_CASE :       List[Any]							,       SCREAMING_SNAKE_CASE :       Tuple=None							,       SCREAMING_SNAKE_CASE :       Optional[int]="This is a photo of {}."						):


      '''simple docstring'''






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







 def  __lowercase (		self :       int							,       SCREAMING_SNAKE_CASE :       Union[str, Any]						):


      '''simple docstring'''






      UpperCamelCase__      :       Tuple	    =  model_inputs.pop("candidate_labels"						)
      UpperCamelCase__      :       List[str]	    =  model_inputs.pop("text_inputs"						)
      if isinstance(text_inputs[0]							,       SCREAMING_SNAKE_CASE						):
           UpperCamelCase__      :       Dict	    =  text_inputs[0]
      else:
           # Batching case.
           UpperCamelCase__      :       Union[str, Any]	    =  text_inputs[0][0]

      UpperCamelCase__      :       Any	    =  self.model(**SCREAMING_SNAKE_CASE							,       **SCREAMING_SNAKE_CASE						)

      UpperCamelCase__      :       Dict	    =  {
          "candidate_labels": candidate_labels,
          "logits": outputs.logits_per_image,
      }
      return model_outputs







 def  __lowercase (		self :       Any							,       SCREAMING_SNAKE_CASE :       List[str]						):


      '''simple docstring'''






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

      UpperCamelCase__      :       Optional[int]	    =  [
          {"score": score, "label": candidate_label}
          for score, candidate_label in sorted(zip(SCREAMING_SNAKE_CASE							,       SCREAMING_SNAKE_CASE						)							,       key=lambda SCREAMING_SNAKE_CASE						: -x[0]						)
      ]
      return result