



'''simple docstring'''






import copy
import unittest

from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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


if is_torch_available():
     import torch

     from transformers import (
         MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
         MODEL_FOR_QUESTION_ANSWERING_MAPPING,
         MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
         MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
         LayoutLMvaConfig,
         LayoutLMvaForQuestionAnswering,
         LayoutLMvaForSequenceClassification,
         LayoutLMvaForTokenClassification,
         LayoutLMvaModel,
     )
     from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
     from PIL import Image

     from transformers import LayoutLMvaImageProcessor

class 							a__:







    def __init__(      self :  int						,			__snake_case :  str						,			__snake_case :  Tuple=2						,			__snake_case :  Optional[Any]=3						,			__snake_case :  List[str]=4						,			__snake_case :  Union[str, Any]=2						,			__snake_case :  str=7						,			__snake_case :  Tuple=True						,			__snake_case :  List[Any]=True						,			__snake_case :  Tuple=True						,			__snake_case :  int=True						,			__snake_case :  List[Any]=99						,			__snake_case :  Any=36						,			__snake_case :  Any=3						,			__snake_case :  str=4						,			__snake_case :  Tuple=37						,			__snake_case :  Union[str, Any]="gelu"						,			__snake_case :  Tuple=0.1						,			__snake_case :  int=0.1						,			__snake_case :  Tuple=5_12						,			__snake_case :  List[Any]=16						,			__snake_case :  Any=2						,			__snake_case :  Optional[int]=0.02						,			__snake_case :  Any=6						,			__snake_case :  Optional[Any]=6						,			__snake_case :  Tuple=3						,			__snake_case :  Optional[int]=4						,			__snake_case :  str=None						,			__snake_case :  Tuple=10_00						,			):
      a						:					Union[str, Any]        =     parent
      a						:					List[Any]        =     batch_size
      a						:					int        =     num_channels
      a						:					str        =     image_size
      a						:					int        =     patch_size
      a						:					Dict        =     text_seq_length
      a						:					Optional[int]        =     is_training
      a						:					int        =     use_input_mask
      a						:					List[str]        =     use_token_type_ids
      a						:					Tuple        =     use_labels
      a						:					Optional[int]        =     vocab_size
      a						:					str        =     hidden_size
      a						:					Tuple        =     num_hidden_layers
      a						:					Dict        =     num_attention_heads
      a						:					str        =     intermediate_size
      a						:					List[Any]        =     hidden_act
      a						:					Union[str, Any]        =     hidden_dropout_prob
      a						:					List[str]        =     attention_probs_dropout_prob
      a						:					int        =     max_position_embeddings
      a						:					str        =     type_vocab_size
      a						:					Any        =     type_sequence_label_size
      a						:					str        =     initializer_range
      a						:					Dict        =     coordinate_size
      a						:					int        =     shape_size
      a						:					Union[str, Any]        =     num_labels
      a						:					Optional[Any]        =     num_choices
      a						:					Optional[int]        =     scope
      a						:					List[str]        =     range_bbox

      # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
      a						:					Optional[int]        =     text_seq_length
      a						:					Optional[int]        =     (image_size // patch_size) ** 2 + 1
      a						:					Dict        =     self.text_seq_length + self.image_seq_length







    def 			lowercase_       (      self :  Optional[int]	):
      a						:					Optional[Any]        =     ids_tensor([self.batch_size, self.text_seq_length]						,			self.vocab_size	)

      a						:					Union[str, Any]        =     ids_tensor([self.batch_size, self.text_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						:					Optional[int]        =     bbox[i, j, 3]
            a						:					Dict        =     bbox[i, j, 1]
            a						:					List[str]        =     t
          if bbox[i, j, 2] < bbox[i, j, 0]:
            a						:					List[Any]        =     bbox[i, j, 2]
            a						:					Any        =     bbox[i, j, 0]
            a						:					List[str]        =     t

      a						:					Optional[int]        =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

      a						:					Union[str, Any]        =     None
      if self.use_input_mask:
        a						:					Any        =     random_attention_mask([self.batch_size, self.text_seq_length]	)

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

      a						:					str        =     None
      a						:					Optional[int]        =     None
      if self.use_labels:
        a						:					str        =     ids_tensor([self.batch_size]						,			self.type_sequence_label_size	)
        a						:					int        =     ids_tensor([self.batch_size, self.text_seq_length]						,			self.num_labels	)

      a						:					int        =     LayoutLMvaConfig(
          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						,			coordinate_size=self.coordinate_size						,			shape_size=self.shape_size						,			input_size=self.image_size						,			patch_size=self.patch_size						,			)

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







    def 			lowercase_       (      self :  Any						,			__snake_case :  int						,			__snake_case :  str						,			__snake_case :  Optional[int]						,			__snake_case :  int						,			__snake_case :  Tuple						,			__snake_case :  List[Any]						,			__snake_case :  List[str]						,			__snake_case :  List[str]	):
      a						:					Optional[int]        =     LayoutLMvaModel(config=__A	)
      model.to(__A	)
      model.eval()

      # text + image
      a						:					Tuple        =     model(__A						,			pixel_values=__A	)
      a						:					Dict        =     model(
          __A						,			bbox=__A						,			pixel_values=__A						,			attention_mask=__A						,			token_type_ids=__A	)
      a						:					Dict        =     model(__A						,			bbox=__A						,			pixel_values=__A						,			token_type_ids=__A	)
      a						:					Any        =     model(__A						,			bbox=__A						,			pixel_values=__A	)

      self.parent.assertEqual(result.last_hidden_state.shape						,			(self.batch_size, self.seq_length, self.hidden_size)	)

      # text only
      a						:					Optional[int]        =     model(__A	)
      self.parent.assertEqual(
          result.last_hidden_state.shape						,			(self.batch_size, self.text_seq_length, self.hidden_size)	)

      # image only
      a						:					str        =     model(pixel_values=__A	)
      self.parent.assertEqual(
          result.last_hidden_state.shape						,			(self.batch_size, self.image_seq_length, self.hidden_size)	)







    def 			lowercase_       (      self :  List[str]						,			__snake_case :  Union[str, Any]						,			__snake_case :  List[Any]						,			__snake_case :  Any						,			__snake_case :  List[Any]						,			__snake_case :  Optional[int]						,			__snake_case :  List[str]						,			__snake_case :  int						,			__snake_case :  List[Any]	):
      a						:					Optional[Any]        =     self.num_labels
      a						:					Any        =     LayoutLMvaForSequenceClassification(__A	)
      model.to(__A	)
      model.eval()
      a						:					Tuple        =     model(
          __A						,			bbox=__A						,			pixel_values=__A						,			attention_mask=__A						,			token_type_ids=__A						,			labels=__A						,			)
      self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.num_labels)	)







    def 			lowercase_       (      self :  List[str]						,			__snake_case :  Dict						,			__snake_case :  Tuple						,			__snake_case :  int						,			__snake_case :  Optional[int]						,			__snake_case :  List[str]						,			__snake_case :  str						,			__snake_case :  Dict						,			__snake_case :  List[str]	):
      a						:					str        =     self.num_labels
      a						:					List[Any]        =     LayoutLMvaForTokenClassification(config=__A	)
      model.to(__A	)
      model.eval()
      a						:					Dict        =     model(
          __A						,			bbox=__A						,			pixel_values=__A						,			attention_mask=__A						,			token_type_ids=__A						,			labels=__A						,			)
      self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.text_seq_length, self.num_labels)	)







    def 			lowercase_       (      self :  Tuple						,			__snake_case :  Dict						,			__snake_case :  Dict						,			__snake_case :  List[str]						,			__snake_case :  Tuple						,			__snake_case :  List[str]						,			__snake_case :  Dict						,			__snake_case :  int						,			__snake_case :  Optional[int]	):
      a						:					Union[str, Any]        =     LayoutLMvaForQuestionAnswering(config=__A	)
      model.to(__A	)
      model.eval()
      a						:					str        =     model(
          __A						,			bbox=__A						,			pixel_values=__A						,			attention_mask=__A						,			token_type_ids=__A						,			start_positions=__A						,			end_positions=__A						,			)
      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 :  List[str]	):
      a						:					Tuple        =     self.prepare_config_and_inputs()
      (
          (
          a						
      )   ,	(
          a						
      )   ,	(
          a						
      )   ,	(
          a						
      )   ,	(
          a						
      )   ,	(
          a						
      )   ,	(
          a						
      )   ,	(
          a						
      )   ,							
      )						:					Tuple        =     config_and_inputs
      a						:					Dict        =     {
          'input_ids': input_ids,
          'bbox': bbox,
          'pixel_values': pixel_values,
          'token_type_ids': token_type_ids,
          'attention_mask': input_mask,
      }
      return config, inputs_dict

@require_torch
class 							a__(					__lowerCamelCase		, __lowerCamelCase		, unittest.TestCase	):
    lowercase__ =    False
    lowercase__ =    False
    lowercase__ =    False

    lowercase__ =    (
        (
            LayoutLMvaModel,
            LayoutLMvaForSequenceClassification,
            LayoutLMvaForTokenClassification,
            LayoutLMvaForQuestionAnswering,
        )
        if is_torch_available()
        else ()
    )
    lowercase__ =    (
        {"document-question-answering": LayoutLMvaForQuestionAnswering, "feature-extraction": LayoutLMvaModel}
        if is_torch_available()
        else {}
    )







    def 			lowercase_       (      self :  Optional[Any]						,			__snake_case :  str						,			__snake_case :  Dict						,			__snake_case :  Optional[int]						,			__snake_case :  Union[str, Any]						,			__snake_case :  Union[str, Any]	):
      # `DocumentQuestionAnsweringPipeline` is expected to work with this model, but it combines the text and visual
      # embedding along the sequence dimension (dim 1), which causes an error during post-processing as `p_mask` has
      # the sequence dimension of the text embedding only.
      # (see the line `embedding_output = torch.cat([embedding_output, visual_embeddings], dim=1)`)
      return True







    def 			lowercase_       (      self :  int	):
      a						:					Optional[Any]        =     LayoutLMvaModelTester(self	)
      a						:					int        =     ConfigTester(self						,			config_class=__A						,			hidden_size=37	)







    def 			lowercase_       (      self :  str						,			__snake_case :  Dict						,			__snake_case :  Optional[int]						,			__snake_case :  Tuple=False	):
      a						:					int        =     copy.deepcopy(__A	)
      if model_class in get_values(__A	):
        a						:					Dict        =     {
            k: v.unsqueeze(1	).expand(-1						,			self.model_tester.num_choices						,			-1	).contiguous()
            if isinstance(__A						,			torch.Tensor	) and v.ndim > 1
            else v
            for k, v in inputs_dict.items()
        }
      if return_labels:
        if model_class in get_values(__A	):
          a						:					List[Any]        =     torch.ones(self.model_tester.batch_size						,			dtype=torch.long						,			device=__A	)
        elif model_class in get_values(__A	):
          a						:					Optional[Any]        =     torch.zeros(
              self.model_tester.batch_size						,			dtype=torch.long						,			device=__A	)
          a						:					str        =     torch.zeros(
              self.model_tester.batch_size						,			dtype=torch.long						,			device=__A	)
        elif model_class in [
            *get_values(__A	),
        ]:
          a						:					Union[str, Any]        =     torch.zeros(
              self.model_tester.batch_size						,			dtype=torch.long						,			device=__A	)
        elif model_class in [
            *get_values(__A	),
        ]:
          a						:					Dict        =     torch.zeros(
              (self.model_tester.batch_size, self.model_tester.text_seq_length)						,			dtype=torch.long						,			device=__A						,			)

      return inputs_dict







    def 			lowercase_       (      self :  Optional[int]	):
      self.config_tester.run_common_tests()







    def 			lowercase_       (      self :  str	):
      a						:					Union[str, Any]        =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__A	)







    def 			lowercase_       (      self :  Any	):
      a						:					Dict        =     self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
        a						:					int        =     type
        self.model_tester.create_and_check_model(*__A	)







    def 			lowercase_       (      self :  Tuple	):
      a						:					Any        =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*__A	)







    def 			lowercase_       (      self :  str	):
      a						:					int        =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*__A	)







    def 			lowercase_       (      self :  Optional[Any]	):
      a						:					Tuple        =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*__A	)







    @slow
    def 			lowercase_       (      self :  List[str]	):
      for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a						:					Optional[Any]        =     LayoutLMvaModel.from_pretrained(__A	)
        self.assertIsNotNone(__A	)
def       lowerCamelCase__       (						):
  a						:					Tuple        =     Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
  return image

@require_torch
class 							a__(					unittest.TestCase	):







    @cached_property
    def 			lowercase_       (      self :  List[str]	):
      return LayoutLMvaImageProcessor(apply_ocr=__A	) if is_vision_available() else None







    @slow
    def 			lowercase_       (      self :  str	):
      a						:					str        =     LayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base'	).to(__A	)

      a						:					Union[str, Any]        =     self.default_image_processor
      a						:					int        =     prepare_img()
      a						:					int        =     image_processor(images=__A						,			return_tensors='pt'	).pixel_values.to(__A	)

      a						:					Optional[Any]        =     torch.tensor([[1, 2]]	)
      a						:					Any        =     torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]	).unsqueeze(0	)

      # forward pass
      a						:					List[str]        =     model(
          input_ids=input_ids.to(__A	)						,			bbox=bbox.to(__A	)						,			pixel_values=pixel_values.to(__A	)						,			)

      # verify the logits
      a						:					Optional[Any]        =     torch.Size((1, 1_99, 7_68)	)
      self.assertEqual(outputs.last_hidden_state.shape						,			__A	)

      a						:					Union[str, Any]        =     torch.tensor(
          [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]	).to(__A	)

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






'''simple docstring'''
# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re

from ..models.auto import AutoProcessor
from ..models.vision_encoder_decoder import VisionEncoderDecoderModel
from ..utils import is_vision_available
from .base import PipelineTool


if is_vision_available():
    from PIL import Image

class      snake_case     (    __lowerCamelCase       ):




 """simple docstring"""



 SCREAMING_SNAKE_CASE_	:					Tuple					="naver-clova-ix/donut-base-finetuned-docvqa"
 SCREAMING_SNAKE_CASE_	:					Dict					=(
     "This is a tool that answers a question about an document (pdf). It takes an input named `document` which "
     "should be the document containing the information, as well as a `question` that is the question about the "
     "document. It returns a text that contains the answer to the question."
 )
 SCREAMING_SNAKE_CASE_	:					List[str]					="document_qa"
 SCREAMING_SNAKE_CASE_	:					Union[str, Any]					=AutoProcessor
 SCREAMING_SNAKE_CASE_	:					Union[str, Any]					=VisionEncoderDecoderModel

 SCREAMING_SNAKE_CASE_	:					List[Any]					=["image", "text"]
 SCREAMING_SNAKE_CASE_	:					Any					=["text"]






 def __init__(       self       :							Optional[int]							,	*__A       :							List[str]							,	**__A       :							List[Any]	):
        if not is_vision_available():
               raise ValueError('Pillow must be installed to use the DocumentQuestionAnsweringTool.'	)

        super().__init__(*__A							,	**__A	)






 def        _lowerCamelCase					(       self       :							Any							,	__A       :							"Image"							,	__A       :							str	):
        __UpperCamelCase       =  '<s_docvqa><s_question>{user_input}</s_question><s_answer>'
        __UpperCamelCase       =  task_prompt.replace('{user_input}'							,	__A	)
        __UpperCamelCase       =  self.pre_processor.tokenizer(
            __A							,	add_special_tokens=__A							,	return_tensors='pt'	).input_ids
        __UpperCamelCase       =  self.pre_processor(__A							,	return_tensors='pt'	).pixel_values

        return {"decoder_input_ids": decoder_input_ids, "pixel_values": pixel_values}






 def        _lowerCamelCase					(       self       :							Union[str, Any]							,	__A       :							Optional[Any]	):
        return self.model.generate(
            inputs['pixel_values'].to(self.device	)							,	decoder_input_ids=inputs['decoder_input_ids'].to(self.device	)							,	max_length=self.model.decoder.config.max_position_embeddings							,	early_stopping=__A							,	pad_token_id=self.pre_processor.tokenizer.pad_token_id							,	eos_token_id=self.pre_processor.tokenizer.eos_token_id							,	use_cache=__A							,	num_beams=1							,	bad_words_ids=[[self.pre_processor.tokenizer.unk_token_id]]							,	return_dict_in_generate=__A							,	).sequences






 def        _lowerCamelCase					(       self       :							Tuple							,	__A       :							List[Any]	):
        __UpperCamelCase       =  self.pre_processor.batch_decode(__A	)[0]
        __UpperCamelCase       =  sequence.replace(self.pre_processor.tokenizer.eos_token							,	''	)
        __UpperCamelCase       =  sequence.replace(self.pre_processor.tokenizer.pad_token							,	''	)
        __UpperCamelCase       =  re.sub(R'<.*?>'							,	''							,	__A							,	count=1	).strip()  # remove first task start token
        __UpperCamelCase       =  self.pre_processor.tokenajson(__A	)

        return sequence["answer"]
