import unittest

from transformers import MobileBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

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


if is_torch_available():
			import torch

			from transformers import (
			    MODEL_FOR_PRETRAINING_MAPPING,
			    MobileBertForMaskedLM,
			    MobileBertForMultipleChoice,
			    MobileBertForNextSentencePrediction,
			    MobileBertForPreTraining,
			    MobileBertForQuestionAnswering,
			    MobileBertForSequenceClassification,
			    MobileBertForTokenClassification,
			    MobileBertModel,
			)
class 	_lowerCAmelCase		:







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







							def 					snake_case	(					self  :     Optional[int]		):
										lowerCamelCase    :Dict    	=  ids_tensor([self.batch_size, self.seq_length]						,       self.vocab_size		)

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

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

										lowerCamelCase    :Union[str, Any]    	=  None
										lowerCamelCase    :Optional[int]    	=  None
										lowerCamelCase    :Optional[Any]    	=  None
										if self.use_labels:
													lowerCamelCase    :List[str]    	=  ids_tensor([self.batch_size]						,       self.type_sequence_label_size		)
													lowerCamelCase    :Optional[int]    	=  ids_tensor([self.batch_size, self.seq_length]						,       self.num_labels		)
													lowerCamelCase    :Any    	=  ids_tensor([self.batch_size]						,       self.num_choices		)

										lowerCamelCase    :List[str]    	=  self.get_config()

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







							def 					snake_case	(					self  :     Optional[int]		):
										return MobileBertConfig(
										    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						,       embedding_size=self.embedding_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=__snake_case						,       initializer_range=self.initializer_range						,       )







							def 					snake_case	(					self  :     Any						,       __snake_case  :     Tuple						,       __snake_case  :     Union[str, Any]						,       __snake_case  :     Any						,       __snake_case  :     Tuple						,       __snake_case  :     List[str]						,       __snake_case  :     Dict						,       __snake_case  :     str		):
										lowerCamelCase    :Tuple    	=  MobileBertModel(config=__snake_case		)
										model.to(__snake_case		)
										model.eval()
										lowerCamelCase    :List[str]    	=  model(__snake_case						,       attention_mask=__snake_case						,       token_type_ids=__snake_case		)
										lowerCamelCase    :Union[str, Any]    	=  model(__snake_case						,       token_type_ids=__snake_case		)
										lowerCamelCase    :int    	=  model(__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 					snake_case	(					self  :     Any						,       __snake_case  :     Union[str, Any]						,       __snake_case  :     Union[str, Any]						,       __snake_case  :     Any						,       __snake_case  :     Union[str, Any]						,       __snake_case  :     Any						,       __snake_case  :     List[str]						,       __snake_case  :     Dict		):
										lowerCamelCase    :Union[str, Any]    	=  MobileBertForMaskedLM(config=__snake_case		)
										model.to(__snake_case		)
										model.eval()
										lowerCamelCase    :Optional[int]    	=  model(__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.vocab_size)		)







							def 					snake_case	(					self  :     List[str]						,       __snake_case  :     Optional[Any]						,       __snake_case  :     Any						,       __snake_case  :     int						,       __snake_case  :     Union[str, Any]						,       __snake_case  :     Optional[Any]						,       __snake_case  :     Union[str, Any]						,       __snake_case  :     int		):
										lowerCamelCase    :List[str]    	=  MobileBertForNextSentencePrediction(config=__snake_case		)
										model.to(__snake_case		)
										model.eval()
										lowerCamelCase    :Any    	=  model(
										    __snake_case						,       attention_mask=__snake_case						,       token_type_ids=__snake_case						,       labels=__snake_case						,       )
										self.parent.assertEqual(result.logits.shape						,       (self.batch_size, 2)		)







							def 					snake_case	(					self  :     Optional[Any]						,       __snake_case  :     List[Any]						,       __snake_case  :     str						,       __snake_case  :     int						,       __snake_case  :     Any						,       __snake_case  :     Dict						,       __snake_case  :     List[Any]						,       __snake_case  :     str		):
										lowerCamelCase    :Tuple    	=  MobileBertForPreTraining(config=__snake_case		)
										model.to(__snake_case		)
										model.eval()
										lowerCamelCase    :Dict    	=  model(
										    __snake_case						,       attention_mask=__snake_case						,       token_type_ids=__snake_case						,       labels=__snake_case						,       next_sentence_label=__snake_case						,       )
										self.parent.assertEqual(result.prediction_logits.shape						,       (self.batch_size, self.seq_length, self.vocab_size)		)
										self.parent.assertEqual(result.seq_relationship_logits.shape						,       (self.batch_size, 2)		)







							def 					snake_case	(					self  :     Optional[Any]						,       __snake_case  :     Tuple						,       __snake_case  :     Optional[Any]						,       __snake_case  :     List[Any]						,       __snake_case  :     str						,       __snake_case  :     List[str]						,       __snake_case  :     List[str]						,       __snake_case  :     Union[str, Any]		):
										lowerCamelCase    :Tuple    	=  MobileBertForQuestionAnswering(config=__snake_case		)
										model.to(__snake_case		)
										model.eval()
										lowerCamelCase    :Any    	=  model(
										    __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 					snake_case	(					self  :     str						,       __snake_case  :     Union[str, Any]						,       __snake_case  :     Any						,       __snake_case  :     Optional[int]						,       __snake_case  :     Any						,       __snake_case  :     int						,       __snake_case  :     Optional[Any]						,       __snake_case  :     Optional[int]		):
										lowerCamelCase    :str    	=  self.num_labels
										lowerCamelCase    :List[Any]    	=  MobileBertForSequenceClassification(__snake_case		)
										model.to(__snake_case		)
										model.eval()
										lowerCamelCase    :Dict    	=  model(__snake_case						,       attention_mask=__snake_case						,       token_type_ids=__snake_case						,       labels=__snake_case		)
										self.parent.assertEqual(result.logits.shape						,       (self.batch_size, self.num_labels)		)







							def 					snake_case	(					self  :     Union[str, Any]						,       __snake_case  :     int						,       __snake_case  :     Union[str, Any]						,       __snake_case  :     List[Any]						,       __snake_case  :     str						,       __snake_case  :     List[Any]						,       __snake_case  :     Tuple						,       __snake_case  :     Dict		):
										lowerCamelCase    :List[str]    	=  self.num_labels
										lowerCamelCase    :str    	=  MobileBertForTokenClassification(config=__snake_case		)
										model.to(__snake_case		)
										model.eval()
										lowerCamelCase    :Tuple    	=  model(__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 					snake_case	(					self  :     Optional[Any]						,       __snake_case  :     Optional[int]						,       __snake_case  :     int						,       __snake_case  :     Any						,       __snake_case  :     List[Any]						,       __snake_case  :     Optional[int]						,       __snake_case  :     Union[str, Any]						,       __snake_case  :     int		):
										lowerCamelCase    :Any    	=  self.num_choices
										lowerCamelCase    :Any    	=  MobileBertForMultipleChoice(config=__snake_case		)
										model.to(__snake_case		)
										model.eval()
										lowerCamelCase    :List[Any]    	=  input_ids.unsqueeze(1		).expand(-1						,       self.num_choices						,       -1		).contiguous()
										lowerCamelCase    :Dict    	=  token_type_ids.unsqueeze(1		).expand(-1						,       self.num_choices						,       -1		).contiguous()
										lowerCamelCase    :str    	=  input_mask.unsqueeze(1		).expand(-1						,       self.num_choices						,       -1		).contiguous()
										lowerCamelCase    :int    	=  model(
										    __snake_case						,       attention_mask=__snake_case						,       token_type_ids=__snake_case						,       labels=__snake_case						,       )
										self.parent.assertEqual(result.logits.shape						,       (self.batch_size, self.num_choices)		)







							def 					snake_case	(					self  :     Optional[int]		):
										lowerCamelCase    :int    	=  self.prepare_config_and_inputs()
										(
										    lowerCamelCase				
										)    :str    	=  config_and_inputs
										lowerCamelCase    :int    	=  {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
										return config, inputs_dict


@require_torch
class 	_lowerCAmelCase		(  _SCREAMING_SNAKE_CASE   , _SCREAMING_SNAKE_CASE   , unittest.TestCase					):
							_UpperCAmelCase         =  (
							    (
							        MobileBertModel,
							        MobileBertForMaskedLM,
							        MobileBertForMultipleChoice,
							        MobileBertForNextSentencePrediction,
							        MobileBertForPreTraining,
							        MobileBertForQuestionAnswering,
							        MobileBertForSequenceClassification,
							        MobileBertForTokenClassification,
							    )
							    if is_torch_available()
							    else ()
							)
							_UpperCAmelCase         =  (
							    {
							        'feature-extraction': MobileBertModel,
							        'fill-mask': MobileBertForMaskedLM,
							        'question-answering': MobileBertForQuestionAnswering,
							        'text-classification': MobileBertForSequenceClassification,
							        'token-classification': MobileBertForTokenClassification,
							        'zero-shot': MobileBertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							_UpperCAmelCase         =  True







							def 					snake_case	(					self  :     Dict						,       __snake_case  :     Optional[Any]						,       __snake_case  :     Union[str, Any]						,       __snake_case  :     Dict=False		):
										lowerCamelCase    :Optional[int]    	=  super()._prepare_for_class(__snake_case						,       __snake_case						,       return_labels=__snake_case		)

										if return_labels:
													if model_class in get_values(__snake_case		):
																lowerCamelCase    :str    	=  torch.zeros(
																    (self.model_tester.batch_size, self.model_tester.seq_length)						,       dtype=torch.long						,       device=__snake_case		)
																lowerCamelCase    :List[Any]    	=  torch.zeros(
																    self.model_tester.batch_size						,       dtype=torch.long						,       device=__snake_case		)
										return inputs_dict







							def 					snake_case	(					self  :     str		):
										lowerCamelCase    :Optional[int]    	=  MobileBertModelTester(self		)
										lowerCamelCase    :int    	=  ConfigTester(self						,       config_class=__snake_case						,       hidden_size=37		)







							def 					snake_case	(					self  :     Optional[Any]		):
										self.config_tester.run_common_tests()







							def 					snake_case	(					self  :     int		):
										lowerCamelCase    :Optional[int]    	=  self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_model(*__snake_case		)







							def 					snake_case	(					self  :     Union[str, Any]		):
										lowerCamelCase    :Optional[Any]    	=  self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_masked_lm(*__snake_case		)







							def 					snake_case	(					self  :     Optional[Any]		):
										lowerCamelCase    :Tuple    	=  self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__snake_case		)







							def 					snake_case	(					self  :     List[str]		):
										lowerCamelCase    :List[Any]    	=  self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__snake_case		)







							def 					snake_case	(					self  :     List[str]		):
										lowerCamelCase    :Optional[int]    	=  self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_pretraining(*__snake_case		)







							def 					snake_case	(					self  :     Optional[int]		):
										lowerCamelCase    :Any    	=  self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_question_answering(*__snake_case		)







							def 					snake_case	(					self  :     Union[str, Any]		):
										lowerCamelCase    :Optional[int]    	=  self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__snake_case		)







							def 					snake_case	(					self  :     List[Any]		):
										lowerCamelCase    :Union[str, Any]    	=  self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_token_classification(*__snake_case		)





def 			_lowerCamelCase       ( a_					:			List[Any]):
			return torch.tensor(
			    A__    ,      dtype=torch.long    ,      device=A__    ,      )


A__     =				1E-3


@require_torch
@require_sentencepiece
@require_tokenizers
class 	_lowerCAmelCase		(  unittest.TestCase					):







							@slow
							def 					snake_case	(					self  :     str		):
										lowerCamelCase    :Optional[Any]    	=  MobileBertModel.from_pretrained('''google/mobilebert-uncased'''		).to(__snake_case		)
										lowerCamelCase    :Optional[int]    	=  _long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]		)
										with torch.no_grad():
													lowerCamelCase    :Tuple    	=  model(__snake_case		)[0]
										lowerCamelCase    :List[Any]    	=  torch.Size((1, 9, 512)		)
										self.assertEqual(output.shape						,       __snake_case		)
										lowerCamelCase    :int    	=  torch.tensor(
										    [
										        [
										            [-2.4_7_3_6_5_2_6e0_7, 8.2_6_9_1_6_5_6e0_4, 1.6_5_2_1_8_3_8e0_5],
										            [-5.7_5_4_1_7_0_4e-0_1, 3.9_0_5_6_0_2_2e0_0, 4.4_0_1_1_5_0_7e0_0],
										            [2.6_0_4_7_3_5_9e0_0, 1.5_6_7_7_6_5_2e0_0, -1.7_3_2_4_1_8_8e-0_1],
										        ]
										    ]						,       device=__snake_case						,       )

										# MobileBERT results range from 10e0 to 10e8. Even a 0.0000001% difference with a value of 10e8 results in a
										# ~1 difference, it's therefore not a good idea to measure using addition.
										# Here, we instead divide the expected result with the result in order to obtain ~1. We then check that the
										# result is held between bounds: 1 - TOLERANCE < expected_result / result < 1 + TOLERANCE
										lowerCamelCase    :Dict    	=  torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE		)
										lowerCamelCase    :str    	=  torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE		)

										self.assertTrue(lower_bound and upper_bound		)







'''simple docstring'''






import unittest

import numpy as np

from transformers.testing_utils import require_pytesseract, require_torch
from transformers.utils import is_pytesseract_available, is_torch_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
  import torch

if is_pytesseract_available():
  from PIL import Image

  from transformers import LayoutLMvaImageProcessor






class   __snake_case			(  unittest.TestCase):







    """simple docstring"""



    def __init__(  self       :    Union[str, Any]       ,		lowerCamelCase       :    str       ,		lowerCamelCase       :    Union[str, Any]=7       ,		lowerCamelCase       :    Any=3       ,		lowerCamelCase       :    Tuple=18       ,		lowerCamelCase       :    List[Any]=30       ,		lowerCamelCase       :    str=4_00       ,		lowerCamelCase       :    List[str]=True       ,		lowerCamelCase       :    List[Any]=None       ,		lowerCamelCase       :    Any=True       ,		)  ->    Any:
      lowerCAmelCase_     : Optional[int]     =  size if size is not None else {"""height""": 18, """width""": 18}
      lowerCAmelCase_     : Dict     =  parent
      lowerCAmelCase_     : Tuple     =  batch_size
      lowerCAmelCase_     : int     =  num_channels
      lowerCAmelCase_     : List[str]     =  image_size
      lowerCAmelCase_     : Optional[int]     =  min_resolution
      lowerCAmelCase_     : List[str]     =  max_resolution
      lowerCAmelCase_     : List[str]     =  do_resize
      lowerCAmelCase_     : str     =  size
      lowerCAmelCase_     : int     =  apply_ocr





    def __lowercase  (  self       :    Optional[Any]						)  ->    List[Any]:
      return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr}







@require_torch
@require_pytesseract
class   __snake_case			(  _SCREAMING_SNAKE_CASE      ,unittest.TestCase):







    """simple docstring"""



    lowercase  					=   LayoutLMvaImageProcessor if is_pytesseract_available() else None

    def __lowercase  (  self       :    List[Any]						)  ->    Dict:
      lowerCAmelCase_     : Union[str, Any]     =  LayoutLMvaImageProcessingTester(self						)

    @property
    def __lowercase  (  self       :    Optional[Any]						)  ->    Optional[Any]:
      return self.image_processor_tester.prepare_image_processor_dict()

    def __lowercase  (  self       :    Optional[Any]						)  ->    Dict:
      lowerCAmelCase_     : Dict     =  self.image_processing_class(**self.image_processor_dict						)
      self.assertTrue(hasattr(lowerCamelCase       ,		"""do_resize"""						)						)
      self.assertTrue(hasattr(lowerCamelCase       ,		"""size"""						)						)
      self.assertTrue(hasattr(lowerCamelCase       ,		"""apply_ocr"""						)						)

    def __lowercase  (  self       :    List[str]						)  ->    int:
      lowerCAmelCase_     : Dict     =  self.image_processing_class.from_dict(self.image_processor_dict						)
      self.assertEqual(image_processor.size       ,		{"""height""": 18, """width""": 18}						)

      lowerCAmelCase_     : Tuple     =  self.image_processing_class.from_dict(self.image_processor_dict       ,		size=42						)
      self.assertEqual(image_processor.size       ,		{"""height""": 42, """width""": 42}						)

    def __lowercase  (  self       :    str						)  ->    Dict:
      pass

    def __lowercase  (  self       :    str						)  ->    Union[str, Any]:
      # Initialize image_processing
      lowerCAmelCase_     : Union[str, Any]     =  self.image_processing_class(**self.image_processor_dict						)
      # create random PIL images
      lowerCAmelCase_     : Tuple     =  prepare_image_inputs(self.image_processor_tester       ,		equal_resolution=lowerCamelCase						)
      for image in image_inputs:
        self.assertIsInstance(lowerCamelCase       ,		Image.Image						)

      # Test not batched input
      lowerCAmelCase_     : List[str]     =  image_processing(image_inputs[0]       ,		return_tensors="""pt"""						)
      self.assertEqual(
          encoding.pixel_values.shape       ,		(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size["""height"""],
              self.image_processor_tester.size["""width"""],
          )       ,		)

      self.assertIsInstance(encoding.words       ,		lowerCamelCase						)
      self.assertIsInstance(encoding.boxes       ,		lowerCamelCase						)

      # Test batched
      lowerCAmelCase_     : Union[str, Any]     =  image_processing(lowerCamelCase       ,		return_tensors="""pt"""						).pixel_values
      self.assertEqual(
          encoded_images.shape       ,		(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size["""height"""],
              self.image_processor_tester.size["""width"""],
          )       ,		)

    def __lowercase  (  self       :    Any						)  ->    Any:
      # Initialize image_processing
      lowerCAmelCase_     : Any     =  self.image_processing_class(**self.image_processor_dict						)
      # create random numpy tensors
      lowerCAmelCase_     : List[Any]     =  prepare_image_inputs(self.image_processor_tester       ,		equal_resolution=lowerCamelCase       ,		numpify=lowerCamelCase						)
      for image in image_inputs:
        self.assertIsInstance(lowerCamelCase       ,		np.ndarray						)

      # Test not batched input
      lowerCAmelCase_     : List[Any]     =  image_processing(image_inputs[0]       ,		return_tensors="""pt"""						).pixel_values
      self.assertEqual(
          encoded_images.shape       ,		(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size["""height"""],
              self.image_processor_tester.size["""width"""],
          )       ,		)

      # Test batched
      lowerCAmelCase_     : Optional[int]     =  image_processing(lowerCamelCase       ,		return_tensors="""pt"""						).pixel_values
      self.assertEqual(
          encoded_images.shape       ,		(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size["""height"""],
              self.image_processor_tester.size["""width"""],
          )       ,		)

    def __lowercase  (  self       :    List[str]						)  ->    int:
      # Initialize image_processing
      lowerCAmelCase_     : Dict     =  self.image_processing_class(**self.image_processor_dict						)
      # create random PyTorch tensors
      lowerCAmelCase_     : Union[str, Any]     =  prepare_image_inputs(self.image_processor_tester       ,		equal_resolution=lowerCamelCase       ,		torchify=lowerCamelCase						)
      for image in image_inputs:
        self.assertIsInstance(lowerCamelCase       ,		torch.Tensor						)

      # Test not batched input
      lowerCAmelCase_     : Optional[int]     =  image_processing(image_inputs[0]       ,		return_tensors="""pt"""						).pixel_values
      self.assertEqual(
          encoded_images.shape       ,		(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size["""height"""],
              self.image_processor_tester.size["""width"""],
          )       ,		)

      # Test batched
      lowerCAmelCase_     : Tuple     =  image_processing(lowerCamelCase       ,		return_tensors="""pt"""						).pixel_values
      self.assertEqual(
          encoded_images.shape       ,		(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size["""height"""],
              self.image_processor_tester.size["""width"""],
          )       ,		)





    def __lowercase  (  self       :    int						)  ->    Optional[Any]:
      # with apply_OCR = True
      lowerCAmelCase_     : Optional[int]     =  LayoutLMvaImageProcessor()

      from datasets import load_dataset

      lowerCAmelCase_     : int     =  load_dataset("""hf-internal-testing/fixtures_docvqa"""       ,		split="""test"""						)

      lowerCAmelCase_     : Any     =  Image.open(ds[0]["""file"""]						).convert("""RGB"""						)

      lowerCAmelCase_     : Dict     =  image_processing(lowerCamelCase       ,		return_tensors="""pt"""						)

      self.assertEqual(encoding.pixel_values.shape       ,		(1, 3, 2_24, 2_24)						)
      self.assertEqual(len(encoding.words						)       ,		len(encoding.boxes						)						)

      # fmt: off
      # the words and boxes were obtained with Tesseract 4.1.1
      lowerCAmelCase_     : Union[str, Any]     =  [["""11:14""", """to""", """11:39""", """a.m""", """11:39""", """to""", """11:44""", """a.m.""", """11:44""", """a.m.""", """to""", """12:25""", """p.m.""", """12:25""", """to""", """12:58""", """p.m.""", """12:58""", """to""", """4:00""", """p.m.""", """2:00""", """to""", """5:00""", """p.m.""", """Coffee""", """Break""", """Coffee""", """will""", """be""", """served""", """for""", """men""", """and""", """women""", """in""", """the""", """lobby""", """adjacent""", """to""", """exhibit""", """area.""", """Please""", """move""", """into""", """exhibit""", """area.""", """(Exhibits""", """Open)""", """TRRF""", """GENERAL""", """SESSION""", """(PART""", """|)""", """Presiding:""", """Lee""", """A.""", """Waller""", """TRRF""", """Vice""", """President""", """“Introductory""", """Remarks”""", """Lee""", """A.""", """Waller,""", """TRRF""", """Vice""", """Presi-""", """dent""", """Individual""", """Interviews""", """with""", """TRRF""", """Public""", """Board""", """Members""", """and""", """Sci-""", """entific""", """Advisory""", """Council""", """Mem-""", """bers""", """Conducted""", """by""", """TRRF""", """Treasurer""", """Philip""", """G.""", """Kuehn""", """to""", """get""", """answers""", """which""", """the""", """public""", """refrigerated""", """warehousing""", """industry""", """is""", """looking""", """for.""", """Plus""", """questions""", """from""", """the""", """floor.""", """Dr.""", """Emil""", """M.""", """Mrak,""", """University""", """of""", """Cal-""", """ifornia,""", """Chairman,""", """TRRF""", """Board;""", """Sam""", """R.""", """Cecil,""", """University""", """of""", """Georgia""", """College""", """of""", """Agriculture;""", """Dr.""", """Stanley""", """Charm,""", """Tufts""", """University""", """School""", """of""", """Medicine;""", """Dr.""", """Robert""", """H.""", """Cotton,""", """ITT""", """Continental""", """Baking""", """Company;""", """Dr.""", """Owen""", """Fennema,""", """University""", """of""", """Wis-""", """consin;""", """Dr.""", """Robert""", """E.""", """Hardenburg,""", """USDA.""", """Questions""", """and""", """Answers""", """Exhibits""", """Open""", """Capt.""", """Jack""", """Stoney""", """Room""", """TRRF""", """Scientific""", """Advisory""", """Council""", """Meeting""", """Ballroom""", """Foyer"""]]  # noqa: E231
      lowerCAmelCase_     : Any     =  [[[1_41, 57, 2_14, 69], [2_28, 58, 2_52, 69], [1_41, 75, 2_16, 88], [2_30, 79, 2_80, 88], [1_42, 2_60, 2_18, 2_73], [2_30, 2_61, 2_55, 2_73], [1_43, 2_79, 2_18, 2_90], [2_31, 2_82, 2_90, 2_91], [1_43, 3_42, 2_18, 3_54], [2_31, 3_45, 2_89, 3_55], [2_02, 3_62, 2_27, 3_73], [1_43, 3_79, 2_20, 3_92], [2_31, 3_82, 2_91, 3_94], [1_44, 7_14, 2_20, 7_26], [2_31, 7_15, 2_56, 7_26], [1_44, 7_32, 2_20, 7_45], [2_32, 7_36, 2_91, 7_47], [1_44, 7_69, 2_18, 7_82], [2_31, 7_70, 2_56, 7_82], [1_41, 7_88, 2_02, 8_01], [2_15, 7_91, 2_74, 8_04], [1_43, 8_26, 2_04, 8_38], [2_15, 8_26, 2_40, 8_38], [1_42, 8_44, 2_02, 8_57], [2_15, 8_47, 2_74, 8_59], [3_34, 57, 4_27, 69], [4_40, 57, 5_22, 69], [3_69, 75, 4_61, 88], [4_69, 75, 5_16, 88], [5_28, 76, 5_62, 88], [5_70, 76, 6_67, 88], [6_75, 75, 7_11, 87], [7_21, 79, 7_78, 88], [7_89, 75, 8_40, 88], [3_69, 97, 4_70, 1_07], [4_84, 94, 5_07, 1_06], [5_18, 94, 5_62, 1_07], [5_76, 94, 6_55, 1_10], [6_68, 94, 7_92, 1_09], [8_04, 95, 8_29, 1_07], [3_69, 1_13, 4_65, 1_25], [4_77, 1_16, 5_47, 1_25], [5_62, 1_13, 6_58, 1_25], [6_71, 1_16, 7_48, 1_25], [7_61, 1_13, 8_11, 1_25], [3_69, 1_31, 4_65, 1_43], [4_77, 1_33, 5_48, 1_43], [5_63, 1_30, 6_98, 1_45], [7_10, 1_30, 8_02, 1_46], [3_36, 1_71, 4_12, 1_83], [4_23, 1_71, 5_72, 1_83], [5_82, 1_70, 7_16, 1_84], [7_28, 1_71, 8_17, 1_87], [8_29, 1_71, 8_44, 1_86], [3_38, 1_97, 4_82, 2_12], [5_07, 1_96, 5_57, 2_09], [5_69, 1_96, 5_95, 2_08], [6_10, 1_96, 7_02, 2_09], [5_05, 2_14, 5_83, 2_26], [5_95, 2_14, 6_56, 2_27], [6_70, 2_15, 8_07, 2_27], [3_35, 2_59, 5_43, 2_74], [5_56, 2_59, 7_08, 2_72], [3_72, 2_79, 4_22, 2_91], [4_35, 2_79, 4_60, 2_91], [4_74, 2_79, 5_74, 2_92], [5_87, 2_78, 6_64, 2_91], [6_76, 2_78, 7_38, 2_91], [7_51, 2_79, 8_34, 2_91], [3_72, 2_98, 4_34, 3_10], [3_35, 3_41, 4_83, 3_54], [4_97, 3_41, 6_55, 3_54], [6_67, 3_41, 7_28, 3_54], [7_40, 3_41, 8_25, 3_54], [3_35, 3_60, 4_30, 3_72], [4_42, 3_60, 5_34, 3_72], [5_45, 3_59, 6_87, 3_72], [6_97, 3_60, 7_54, 3_72], [7_65, 3_60, 8_23, 3_73], [3_34, 3_78, 4_28, 3_91], [4_40, 3_78, 5_77, 3_94], [5_90, 3_78, 7_05, 3_91], [7_20, 3_78, 8_01, 3_91], [3_34, 3_97, 4_00, 4_09], [3_70, 4_16, 5_29, 4_29], [5_44, 4_16, 5_76, 4_32], [5_87, 4_16, 6_65, 4_28], [6_77, 4_16, 8_14, 4_29], [3_72, 4_35, 4_52, 4_50], [4_65, 4_34, 4_95, 4_47], [5_11, 4_34, 6_00, 4_47], [6_11, 4_36, 6_37, 4_47], [6_49, 4_36, 6_94, 4_51], [7_05, 4_38, 8_24, 4_47], [3_69, 4_53, 4_52, 4_66], [4_64, 4_54, 5_09, 4_66], [5_22, 4_53, 6_11, 4_69], [6_25, 4_53, 7_92, 4_69], [3_70, 4_72, 5_56, 4_88], [5_70, 4_72, 6_84, 4_87], [6_97, 4_72, 7_18, 4_85], [7_32, 4_72, 8_35, 4_88], [3_69, 4_90, 4_11, 5_03], [4_25, 4_90, 4_84, 5_03], [4_96, 4_90, 6_35, 5_06], [6_45, 4_90, 7_07, 5_03], [7_18, 4_91, 7_61, 5_03], [7_71, 4_90, 8_40, 5_03], [3_36, 5_10, 3_74, 5_21], [3_88, 5_10, 4_47, 5_22], [4_60, 5_10, 4_89, 5_21], [5_03, 5_10, 5_80, 5_22], [5_92, 5_09, 7_36, 5_25], [7_45, 5_09, 7_70, 5_22], [7_81, 5_09, 8_40, 5_22], [3_38, 5_28, 4_34, 5_41], [4_48, 5_28, 5_96, 5_41], [6_09, 5_27, 6_87, 5_40], [7_00, 5_28, 7_92, 5_41], [3_36, 5_46, 3_97, 5_59], [4_07, 5_46, 4_31, 5_59], [4_43, 5_46, 5_25, 5_60], [5_37, 5_46, 6_80, 5_62], [6_88, 5_46, 7_14, 5_59], [7_22, 5_46, 8_37, 5_62], [3_36, 5_65, 4_49, 5_81], [4_61, 5_65, 4_85, 5_77], [4_97, 5_65, 6_65, 5_81], [6_81, 5_65, 7_18, 5_77], [7_32, 5_65, 8_37, 5_80], [3_37, 5_84, 4_38, 5_97], [4_52, 5_83, 5_21, 5_96], [5_35, 5_84, 6_77, 5_99], [6_90, 5_83, 7_87, 5_96], [8_01, 5_83, 8_25, 5_96], [3_38, 6_02, 4_78, 6_15], [4_92, 6_02, 5_30, 6_14], [5_43, 6_02, 6_38, 6_15], [6_50, 6_02, 6_76, 6_14], [6_88, 6_02, 7_88, 6_15], [8_02, 6_02, 8_43, 6_14], [3_37, 6_21, 5_02, 6_33], [5_16, 6_21, 6_15, 6_37], [6_29, 6_21, 7_74, 6_36], [7_89, 6_21, 8_27, 6_33], [3_37, 6_39, 4_18, 6_52], [4_32, 6_40, 5_71, 6_53], [5_87, 6_39, 7_31, 6_55], [7_43, 6_39, 7_69, 6_52], [7_80, 6_39, 8_41, 6_52], [3_38, 6_58, 4_40, 6_73], [4_55, 6_58, 4_91, 6_70], [5_08, 6_58, 6_02, 6_71], [6_16, 6_58, 6_38, 6_70], [6_54, 6_58, 8_35, 6_74], [3_37, 6_77, 4_29, 6_89], [3_37, 7_14, 4_82, 7_26], [4_95, 7_14, 5_48, 7_26], [5_61, 7_14, 6_83, 7_26], [3_38, 7_70, 4_61, 7_82], [4_74, 7_69, 5_54, 7_85], [4_89, 7_88, 5_62, 8_03], [5_76, 7_88, 6_43, 8_01], [6_56, 7_87, 7_51, 8_04], [7_64, 7_88, 8_44, 8_01], [3_34, 8_25, 4_21, 8_38], [4_30, 8_24, 5_74, 8_38], [5_84, 8_24, 7_23, 8_41], [3_35, 8_44, 4_50, 8_57], [4_64, 8_43, 5_83, 8_60], [6_28, 8_62, 7_55, 8_75], [7_69, 8_61, 8_48, 8_78]]]  # noqa: E231
      # fmt: on

      self.assertListEqual(encoding.words       ,		lowerCamelCase						)
      self.assertListEqual(encoding.boxes       ,		lowerCamelCase						)

      # with apply_OCR = False
      lowerCAmelCase_     : Dict     =  LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase						)

      lowerCAmelCase_     : Dict     =  image_processing(lowerCamelCase       ,		return_tensors="""pt"""						)

      self.assertEqual(encoding.pixel_values.shape       ,		(1, 3, 2_24, 2_24)						)



