import unittest

from transformers import ElectraConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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_PRETRAINING_MAPPING,
          ElectraForCausalLM,
          ElectraForMaskedLM,
          ElectraForMultipleChoice,
          ElectraForPreTraining,
          ElectraForQuestionAnswering,
          ElectraForSequenceClassification,
          ElectraForTokenClassification,
          ElectraModel,
      )
      from transformers.models.electra.modeling_electra import ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST



class 						snake_case_ :






   def __init__(   self       :  Tuple			,		lowercase_       :  Union[str, Any]			,		lowercase_       :  str=13			,		lowercase_       :  Dict=7			,		lowercase_       :  List[str]=True			,		lowercase_       :  int=True			,		lowercase_       :  str=True			,		lowercase_       :  Optional[Any]=True			,		lowercase_       :  Tuple=99			,		lowercase_       :  Optional[Any]=32			,		lowercase_       :  Any=5			,		lowercase_       :  str=4			,		lowercase_       :  str=37			,		lowercase_       :  Dict="gelu"			,		lowercase_       :  Any=0.1			,		lowercase_       :  List[Any]=0.1			,		lowercase_       :  Dict=5_12			,		lowercase_       :  Tuple=16			,		lowercase_       :  int=2			,		lowercase_       :  Optional[Any]=0.02			,		lowercase_       :  Dict=3			,		lowercase_       :  Union[str, Any]=4			,		lowercase_       :  Dict=None			,		)      ->		Dict:
       lowercase__				:     Union[str, Any]					      =       parent
       lowercase__				:     Tuple					      =       batch_size
       lowercase__				:     List[str]					      =       seq_length
       lowercase__				:     Any					      =       is_training
       lowercase__				:     int					      =       use_input_mask
       lowercase__				:     Optional[int]					      =       use_token_type_ids
       lowercase__				:     List[str]					      =       use_labels
       lowercase__				:     int					      =       vocab_size
       lowercase__				:     int					      =       hidden_size
       lowercase__				:     Optional[Any]					      =       num_hidden_layers
       lowercase__				:     str					      =       num_attention_heads
       lowercase__				:     Any					      =       intermediate_size
       lowercase__				:     Dict					      =       hidden_act
       lowercase__				:     List[str]					      =       hidden_dropout_prob
       lowercase__				:     List[str]					      =       attention_probs_dropout_prob
       lowercase__				:     Optional[Any]					      =       max_position_embeddings
       lowercase__				:     Tuple					      =       type_vocab_size
       lowercase__				:     List[Any]					      =       type_sequence_label_size
       lowercase__				:     Tuple					      =       initializer_range
       lowercase__				:     Dict					      =       num_labels
       lowercase__				:     Optional[Any]					      =       num_choices
       lowercase__				:     int					      =       scope






   def    __UpperCamelCase  (   self       :  Dict						)      ->		Optional[int]:
       lowercase__				:     Dict					      =       ids_tensor([self.batch_size, self.seq_length]			,		self.vocab_size						)

       lowercase__				:     str					      =       None
       if self.use_input_mask:
           lowercase__				:     Dict					      =       random_attention_mask([self.batch_size, self.seq_length]						)

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

       lowercase__				:     Optional[int]					      =       None
       lowercase__				:     List[str]					      =       None
       lowercase__				:     Dict					      =       None
       if self.use_labels:
           lowercase__				:     str					      =       ids_tensor([self.batch_size]			,		self.type_sequence_label_size						)
           lowercase__				:     Tuple					      =       ids_tensor([self.batch_size, self.seq_length]			,		self.num_labels						)
           lowercase__				:     Optional[int]					      =       ids_tensor([self.batch_size]			,		self.num_choices						)
           lowercase__				:     Optional[int]					      =       ids_tensor([self.batch_size, self.seq_length]			,		1						)

       lowercase__				:     Union[str, Any]					      =       self.get_config()

       return (
           config,
           input_ids,
           token_type_ids,
           input_mask,
           sequence_labels,
           token_labels,
           choice_labels,
           fake_token_labels,
       )






   def    __UpperCamelCase  (   self       :  Any						)      ->		int:
       return ElectraConfig(
           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			,		is_decoder=lowercase_			,		initializer_range=self.initializer_range			,		)






   def    __UpperCamelCase  (   self       :  List[str]						)      ->		int:
       (
           (
           lowercase__
       )      ,     (
           lowercase__
       )      ,     (
           lowercase__
       )      ,     (
           lowercase__
       )      ,     (
           lowercase__
       )      ,     (
           lowercase__
       )      ,     (
           lowercase__
       )      ,     (
           lowercase__
       )      ,     
       )				:     List[str]					      =       self.prepare_config_and_inputs()

       lowercase__				:     int					      =       True
       lowercase__				:     Any					      =       floats_tensor([self.batch_size, self.seq_length, self.hidden_size]						)
       lowercase__				:     Any					      =       ids_tensor([self.batch_size, self.seq_length]			,		vocab_size=2						)

       return (
           config,
           input_ids,
           token_type_ids,
           input_mask,
           sequence_labels,
           token_labels,
           choice_labels,
           encoder_hidden_states,
           encoder_attention_mask,
       )






   def    __UpperCamelCase  (   self       :  List[str]			,		lowercase_       :  List[str]			,		lowercase_       :  List[str]			,		lowercase_       :  Optional[int]			,		lowercase_       :  Tuple			,		lowercase_       :  str			,		lowercase_       :  str			,		lowercase_       :  Tuple			,		lowercase_       :  Optional[Any]			,		)      ->		Dict:
       lowercase__				:     Optional[Any]					      =       ElectraModel(config=lowercase_						)
       model.to(lowercase_						)
       model.eval()
       lowercase__				:     Optional[Any]					      =       model(lowercase_			,		attention_mask=lowercase_			,		token_type_ids=lowercase_						)
       lowercase__				:     Optional[int]					      =       model(lowercase_			,		token_type_ids=lowercase_						)
       lowercase__				:     Optional[int]					      =       model(lowercase_						)
       self.parent.assertEqual(result.last_hidden_state.shape			,		(self.batch_size, self.seq_length, self.hidden_size)						)






   def    __UpperCamelCase  (   self       :  Tuple			,		lowercase_       :  Optional[Any]			,		lowercase_       :  Optional[Any]			,		lowercase_       :  Any			,		lowercase_       :  Tuple			,		lowercase_       :  str			,		lowercase_       :  List[str]			,		lowercase_       :  Any			,		lowercase_       :  List[Any]			,		lowercase_       :  str			,		)      ->		Union[str, Any]:
       lowercase__				:     Tuple					      =       True
       lowercase__				:     List[Any]					      =       ElectraModel(lowercase_						)
       model.to(lowercase_						)
       model.eval()
       lowercase__				:     List[str]					      =       model(
           lowercase_			,		attention_mask=lowercase_			,		token_type_ids=lowercase_			,		encoder_hidden_states=lowercase_			,		encoder_attention_mask=lowercase_			,		)
       lowercase__				:     Tuple					      =       model(
           lowercase_			,		attention_mask=lowercase_			,		token_type_ids=lowercase_			,		encoder_hidden_states=lowercase_			,		)
       lowercase__				:     Tuple					      =       model(lowercase_			,		attention_mask=lowercase_			,		token_type_ids=lowercase_						)
       self.parent.assertEqual(result.last_hidden_state.shape			,		(self.batch_size, self.seq_length, self.hidden_size)						)






   def    __UpperCamelCase  (   self       :  List[Any]			,		lowercase_       :  List[str]			,		lowercase_       :  Tuple			,		lowercase_       :  Any			,		lowercase_       :  Union[str, Any]			,		lowercase_       :  Optional[Any]			,		lowercase_       :  Dict			,		lowercase_       :  Union[str, Any]			,		lowercase_       :  Optional[int]			,		)      ->		Optional[Any]:
       lowercase__				:     List[str]					      =       ElectraForMaskedLM(config=lowercase_						)
       model.to(lowercase_						)
       model.eval()
       lowercase__				:     str					      =       model(lowercase_			,		attention_mask=lowercase_			,		token_type_ids=lowercase_			,		labels=lowercase_						)
       self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.seq_length, self.vocab_size)						)






   def    __UpperCamelCase  (   self       :  str			,		lowercase_       :  Optional[Any]			,		lowercase_       :  int			,		lowercase_       :  List[str]			,		lowercase_       :  int			,		lowercase_       :  str			,		lowercase_       :  int			,		lowercase_       :  Optional[Any]			,		lowercase_       :  int			,		lowercase_       :  int			,		)      ->		Tuple:
       lowercase__				:     Any					      =       ElectraForCausalLM(config=lowercase_						)
       model.to(lowercase_						)
       model.eval()
       lowercase__				:     Dict					      =       model(lowercase_			,		attention_mask=lowercase_			,		token_type_ids=lowercase_			,		labels=lowercase_						)
       self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.seq_length, self.vocab_size)						)






   def    __UpperCamelCase  (   self       :  List[Any]			,		lowercase_       :  int			,		lowercase_       :  Tuple			,		lowercase_       :  Optional[Any]			,		lowercase_       :  int			,		lowercase_       :  str			,		lowercase_       :  int			,		lowercase_       :  List[str]			,		lowercase_       :  List[Any]			,		)      ->		Dict:
       lowercase__				:     Union[str, Any]					      =       self.num_labels
       lowercase__				:     Union[str, Any]					      =       ElectraForTokenClassification(config=lowercase_						)
       model.to(lowercase_						)
       model.eval()
       lowercase__				:     Optional[Any]					      =       model(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       :  Optional[Any]			,		lowercase_       :  List[Any]			,		lowercase_       :  Tuple			,		lowercase_       :  Any			,		lowercase_       :  Dict			,		lowercase_       :  Union[str, Any]			,		lowercase_       :  Any			,		lowercase_       :  List[str]			,		lowercase_       :  Optional[Any]			,		)      ->		Any:
       lowercase__				:     int					      =       self.num_labels
       lowercase__				:     Tuple					      =       ElectraForPreTraining(config=lowercase_						)
       model.to(lowercase_						)
       model.eval()
       lowercase__				:     Tuple					      =       model(lowercase_			,		attention_mask=lowercase_			,		token_type_ids=lowercase_			,		labels=lowercase_						)
       self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.seq_length)						)






   def    __UpperCamelCase  (   self       :  Optional[Any]			,		lowercase_       :  Any			,		lowercase_       :  List[Any]			,		lowercase_       :  Optional[Any]			,		lowercase_       :  Optional[int]			,		lowercase_       :  Any			,		lowercase_       :  Any			,		lowercase_       :  str			,		lowercase_       :  Optional[int]			,		)      ->		Optional[int]:
       lowercase__				:     str					      =       self.num_labels
       lowercase__				:     Optional[int]					      =       ElectraForSequenceClassification(lowercase_						)
       model.to(lowercase_						)
       model.eval()
       lowercase__				:     List[str]					      =       model(lowercase_			,		attention_mask=lowercase_			,		token_type_ids=lowercase_			,		labels=lowercase_						)
       self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.num_labels)						)






   def    __UpperCamelCase  (   self       :  Any			,		lowercase_       :  List[Any]			,		lowercase_       :  List[str]			,		lowercase_       :  Optional[int]			,		lowercase_       :  str			,		lowercase_       :  str			,		lowercase_       :  List[str]			,		lowercase_       :  Optional[Any]			,		lowercase_       :  Union[str, Any]			,		)      ->		Any:
       lowercase__				:     Optional[Any]					      =       ElectraForQuestionAnswering(config=lowercase_						)
       model.to(lowercase_						)
       model.eval()
       lowercase__				:     Optional[int]					      =       model(
           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       :  Dict			,		lowercase_       :  List[str]			,		lowercase_       :  Optional[Any]			,		lowercase_       :  List[str]			,		lowercase_       :  Dict			,		lowercase_       :  str			,		lowercase_       :  Optional[Any]			,		lowercase_       :  Dict			,		lowercase_       :  int			,		)      ->		Tuple:
       lowercase__				:     Union[str, Any]					      =       self.num_choices
       lowercase__				:     Tuple					      =       ElectraForMultipleChoice(config=lowercase_						)
       model.to(lowercase_						)
       model.eval()
       lowercase__				:     List[str]					      =       input_ids.unsqueeze(1						).expand(-1			,		self.num_choices			,		-1						).contiguous()
       lowercase__				:     int					      =       token_type_ids.unsqueeze(1						).expand(-1			,		self.num_choices			,		-1						).contiguous()
       lowercase__				:     Any					      =       input_mask.unsqueeze(1						).expand(-1			,		self.num_choices			,		-1						).contiguous()
       lowercase__				:     Optional[int]					      =       model(
           lowercase_			,		attention_mask=lowercase_			,		token_type_ids=lowercase_			,		labels=lowercase_			,		)
       self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.num_choices)						)







   def    __UpperCamelCase  (   self       :  List[Any]						)      ->		List[str]:
       lowercase__				:     List[str]					      =       self.prepare_config_and_inputs()
       (
           (
           lowercase__
       )      ,     (
           lowercase__
       )      ,     (
           lowercase__
       )      ,     (
           lowercase__
       )      ,     (
           lowercase__
       )      ,     (
           lowercase__
       )      ,     (
           lowercase__
       )      ,     (
           lowercase__
       )      ,     
       )				:     List[str]					      =       config_and_inputs
       lowercase__				:     Dict					      =       {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
       return config, inputs_dict



@require_torch
class 						snake_case_ (	__A						,__A						,unittest.TestCase ):
   __A			:	Optional[int]        =       (
       (
           ElectraModel,
           ElectraForPreTraining,
           ElectraForMaskedLM,
           ElectraForCausalLM,
           ElectraForMultipleChoice,
           ElectraForTokenClassification,
           ElectraForSequenceClassification,
           ElectraForQuestionAnswering,
       )
       if is_torch_available()
       else ()
   )
   __A			:	List[Any]        =       (
       {
           "feature-extraction": ElectraModel,
           "fill-mask": ElectraForMaskedLM,
           "question-answering": ElectraForQuestionAnswering,
           "text-classification": ElectraForSequenceClassification,
           "text-generation": ElectraForCausalLM,
           "token-classification": ElectraForTokenClassification,
           "zero-shot": ElectraForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   __A			:	Optional[int]        =       True






   def    __UpperCamelCase  (   self       :  List[str]			,		lowercase_       :  Optional[Any]			,		lowercase_       :  Optional[int]			,		lowercase_       :  Tuple=False						)      ->		List[Any]:
       lowercase__				:     int					      =       super()._prepare_for_class(lowercase_			,		lowercase_			,		return_labels=lowercase_						)

       if return_labels:
           if model_class in get_values(lowercase_						):
               lowercase__				:     Optional[int]					      =       torch.zeros(
                   (self.model_tester.batch_size, self.model_tester.seq_length)			,		dtype=torch.long			,		device=lowercase_						)
       return inputs_dict






   def    __UpperCamelCase  (   self       :  List[Any]						)      ->		Any:
       lowercase__				:     List[str]					      =       ElectraModelTester(self						)
       lowercase__				:     Union[str, Any]					      =       ConfigTester(self			,		config_class=lowercase_			,		hidden_size=37						)






   def    __UpperCamelCase  (   self       :  List[Any]						)      ->		str:
       self.config_tester.run_common_tests()






   def    __UpperCamelCase  (   self       :  str						)      ->		int:
       lowercase__				:     List[Any]					      =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_model(*lowercase_						)






   def    __UpperCamelCase  (   self       :  Any						)      ->		Union[str, Any]:
       lowercase__				:     Tuple					      =       self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_electra_model_as_decoder(*lowercase_						)






   def    __UpperCamelCase  (   self       :  Optional[Any]						)      ->		Dict:
       lowercase__				:     int					      =       self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
           lowercase__				:     int					      =       type
           self.model_tester.create_and_check_electra_model(*lowercase_						)






   def    __UpperCamelCase  (   self       :  Tuple						)      ->		Dict:
       lowercase__				:     str					      =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_masked_lm(*lowercase_						)






   def    __UpperCamelCase  (   self       :  Tuple						)      ->		Optional[Any]:
       lowercase__				:     Optional[int]					      =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_token_classification(*lowercase_						)






   def    __UpperCamelCase  (   self       :  List[str]						)      ->		Tuple:
       lowercase__				:     Union[str, Any]					      =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_pretraining(*lowercase_						)






   def    __UpperCamelCase  (   self       :  int						)      ->		List[Any]:
       lowercase__				:     int					      =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_sequence_classification(*lowercase_						)






   def    __UpperCamelCase  (   self       :  Any						)      ->		Tuple:
       lowercase__				:     Optional[Any]					      =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_question_answering(*lowercase_						)






   def    __UpperCamelCase  (   self       :  Optional[int]						)      ->		Optional[Any]:
       lowercase__				:     Any					      =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_multiple_choice(*lowercase_						)






   @slow
   def    __UpperCamelCase  (   self       :  Any						)      ->		Optional[int]:
       for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase__				:     List[Any]					      =       ElectraModel.from_pretrained(lowercase_						)
           self.assertIsNotNone(lowercase_						)







   def    __UpperCamelCase  (   self       :  Tuple						)      ->		Optional[int]:
       lowercase__				:     Union[str, Any]					      =       self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_electra_for_causal_lm(*lowercase_						)



@require_torch
class 						snake_case_ (	unittest.TestCase ):






   @slow
   def    __UpperCamelCase  (   self       :  Optional[Any]						)      ->		Union[str, Any]:
       lowercase__				:     Any					      =       ElectraModel.from_pretrained("google/electra-small-discriminator"						)
       lowercase__				:     str					      =       torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]						)
       lowercase__				:     List[str]					      =       torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
       lowercase__				:     List[Any]					      =       model(lowercase_			,		attention_mask=lowercase_						)[0]
       lowercase__				:     Dict					      =       torch.Size((1, 11, 2_56)						)
       self.assertEqual(output.shape			,		lowercase_						)
       lowercase__				:     Dict					      =       torch.tensor(
           [[[0.44_71, 0.68_21, -0.32_65], [0.46_27, 0.52_55, -0.36_68], [0.45_32, 0.33_13, -0.43_44]]]						)

       self.assertTrue(torch.allclose(output[:, 1:4, 1:4]			,		lowercase_			,		atol=1E-4						)						)




import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType



class 						snake_case_ (	__A ):
   __A			:	Optional[Any]        =       ["image_processor", "tokenizer"]
   __A			:	Tuple        =       "LayoutLMv2ImageProcessor"
   __A			:	Optional[int]        =       ("LayoutLMv2Tokenizer", "LayoutLMv2TokenizerFast")






   def __init__(   self       :  Tuple			,		lowercase_       :  List[str]=None			,		lowercase_       :  Dict=None			,		**lowercase_       :  List[str]						)      ->		int:
       lowercase__				:     Optional[Any]					      =       None
       if "feature_extractor" in kwargs:
           warnings.warn(
               "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
               " instead."			,		lowercase_			,		)
           lowercase__				:     Any					      =       kwargs.pop("feature_extractor"						)

       lowercase__				:     List[str]					      =       image_processor if image_processor is not None else feature_extractor
       if image_processor is None:
           raise ValueError("You need to specify an `image_processor`."						)
       if tokenizer is None:
           raise ValueError("You need to specify a `tokenizer`."						)

       super().__init__(lowercase_			,		lowercase_						)






   def __call__(   self       :  Union[str, Any]			,		lowercase_       :  Optional[Any]			,		lowercase_       :  Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None			,		lowercase_       :  Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None			,		lowercase_       :  Union[List[List[int]], List[List[List[int]]]] = None			,		lowercase_       :  Optional[Union[List[int], List[List[int]]]] = None			,		lowercase_       :  bool = True			,		lowercase_       :  Union[bool, str, PaddingStrategy] = False			,		lowercase_       :  Union[bool, str, TruncationStrategy] = False			,		lowercase_       :  Optional[int] = None			,		lowercase_       :  int = 0			,		lowercase_       :  Optional[int] = None			,		lowercase_       :  Optional[bool] = None			,		lowercase_       :  Optional[bool] = None			,		lowercase_       :  bool = False			,		lowercase_       :  bool = False			,		lowercase_       :  bool = False			,		lowercase_       :  bool = False			,		lowercase_       :  bool = True			,		lowercase_       :  Optional[Union[str, TensorType]] = None			,		**lowercase_       :  List[str]			,		)      ->		BatchEncoding:
       # verify input
       if self.image_processor.apply_ocr and (boxes is not None):
           raise ValueError(
               "You cannot provide bounding boxes if you initialized the image processor with apply_ocr set to True."						)

       if self.image_processor.apply_ocr and (word_labels is not None):
           raise ValueError(
               "You cannot provide word labels if you initialized the image processor with apply_ocr set to True."						)

       if return_overflowing_tokens is True and return_offsets_mapping is False:
           raise ValueError("You cannot return overflowing tokens without returning the offsets mapping."						)

       # first, apply the image processor
       lowercase__				:     List[Any]					      =       self.image_processor(images=lowercase_			,		return_tensors=lowercase_						)

       # second, apply the tokenizer
       if text is not None and self.image_processor.apply_ocr and text_pair is None:
           if isinstance(lowercase_			,		lowercase_						):
               lowercase__				:     List[Any]					      =       [text]  # add batch dimension (as the image processor always adds a batch dimension)
           lowercase__				:     Union[str, Any]					      =       features["words"]

       lowercase__				:     List[str]					      =       self.tokenizer(
           text=text if text is not None else features["words"]			,		text_pair=text_pair if text_pair is not None else None			,		boxes=boxes if boxes is not None else features["boxes"]			,		word_labels=lowercase_			,		add_special_tokens=lowercase_			,		padding=lowercase_			,		truncation=lowercase_			,		max_length=lowercase_			,		stride=lowercase_			,		pad_to_multiple_of=lowercase_			,		return_token_type_ids=lowercase_			,		return_attention_mask=lowercase_			,		return_overflowing_tokens=lowercase_			,		return_special_tokens_mask=lowercase_			,		return_offsets_mapping=lowercase_			,		return_length=lowercase_			,		verbose=lowercase_			,		return_tensors=lowercase_			,		**lowercase_			,		)

       # add pixel values
       lowercase__				:     Optional[int]					      =       features.pop("pixel_values"						)
       if return_overflowing_tokens is True:
           lowercase__				:     Tuple					      =       self.get_overflowing_images(lowercase_			,		encoded_inputs["overflow_to_sample_mapping"]						)
       lowercase__				:     str					      =       images

       return encoded_inputs






   def    __UpperCamelCase  (   self       :  Tuple			,		lowercase_       :  Optional[int]			,		lowercase_       :  Union[str, Any]						)      ->		Any:
       # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
       lowercase__				:     str					      =       []
       for sample_idx in overflow_to_sample_mapping:
           images_with_overflow.append(images[sample_idx]						)

       if len(lowercase_						) != len(lowercase_						):
           raise ValueError(
               "Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got"
               F''' {len(lowercase_						)} and {len(lowercase_						)}'''						)

       return images_with_overflow






   def    __UpperCamelCase  (   self       :  Any			,		*lowercase_       :  Tuple			,		**lowercase_       :  List[Any]						)      ->		Union[str, Any]:
       return self.tokenizer.batch_decode(*lowercase_			,		**lowercase_						)






   def    __UpperCamelCase  (   self       :  List[Any]			,		*lowercase_       :  Tuple			,		**lowercase_       :  Union[str, Any]						)      ->		List[Any]:
       return self.tokenizer.decode(*lowercase_			,		**lowercase_						)






   @property
   def    __UpperCamelCase  (   self       :  int						)      ->		Optional[Any]:
       return ["input_ids", "bbox", "token_type_ids", "attention_mask", "image"]






   @property
   def    __UpperCamelCase  (   self       :  Optional[int]						)      ->		Tuple:
       warnings.warn(
           "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."			,		lowercase_			,		)
       return self.image_processor_class







   @property
   def    __UpperCamelCase  (   self       :  Dict						)      ->		List[Any]:
       warnings.warn(
           "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."			,		lowercase_			,		)
       return self.image_processor



