
'''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    UpperCAmelCase_				:





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

     # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
     snake_case_ :    Tuple     			=						text_seq_length
     snake_case_ :    Tuple     			=						(image_size // patch_size) ** 2 + 1
     snake_case_ :    Any     			=						self.text_seq_length + self.image_seq_length





 def        snake_case__		(    self):
     snake_case_ :    str     			=						ids_tensor([self.batch_size, self.text_seq_length] ,       self.vocab_size)

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

     snake_case_ :    List[Any]     			=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

     snake_case_ :    List[Any]     			=						None
     if self.use_token_type_ids:
         snake_case_ :    List[str]     			=						ids_tensor([self.batch_size, self.text_seq_length] ,       self.type_vocab_size)

     snake_case_ :    Any     			=						None
     snake_case_ :    Optional[Any]     			=						None
     if self.use_labels:
         snake_case_ :    List[Any]     			=						ids_tensor([self.batch_size] ,       self.type_sequence_label_size)
         snake_case_ :    str     			=						ids_tensor([self.batch_size, self.text_seq_length] ,       self.num_labels)

     snake_case_ :    List[str]     			=						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        snake_case__		(    self ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_):
     snake_case_ :    Dict     			=						LayoutLMvaModel(config=lowercase_)
     model.to(lowercase_)
     model.eval()

     # text + image
     snake_case_ :    Tuple     			=						model(lowercase_ ,       pixel_values=lowercase_)
     snake_case_ :    Union[str, Any]     			=						model(
         lowercase_ ,       bbox=lowercase_ ,       pixel_values=lowercase_ ,       attention_mask=lowercase_ ,       token_type_ids=lowercase_)
     snake_case_ :    Tuple     			=						model(lowercase_ ,       bbox=lowercase_ ,       pixel_values=lowercase_ ,       token_type_ids=lowercase_)
     snake_case_ :    Union[str, Any]     			=						model(lowercase_ ,       bbox=lowercase_ ,       pixel_values=lowercase_)

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

     # text only
     snake_case_ :    str     			=						model(lowercase_)
     self.parent.assertEqual(
         result.last_hidden_state.shape ,       (self.batch_size, self.text_seq_length, self.hidden_size))

     # image only
     snake_case_ :    Tuple     			=						model(pixel_values=lowercase_)
     self.parent.assertEqual(
         result.last_hidden_state.shape ,       (self.batch_size, self.image_seq_length, self.hidden_size))





 def        snake_case__		(    self ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_):
     snake_case_ :    str     			=						self.num_labels
     snake_case_ :    Any     			=						LayoutLMvaForSequenceClassification(lowercase_)
     model.to(lowercase_)
     model.eval()
     snake_case_ :    int     			=						model(
         lowercase_ ,       bbox=lowercase_ ,       pixel_values=lowercase_ ,       attention_mask=lowercase_ ,       token_type_ids=lowercase_ ,       labels=lowercase_ ,       )
     self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.num_labels))





 def        snake_case__		(    self ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_):
     snake_case_ :    str     			=						self.num_labels
     snake_case_ :    Union[str, Any]     			=						LayoutLMvaForTokenClassification(config=lowercase_)
     model.to(lowercase_)
     model.eval()
     snake_case_ :    List[Any]     			=						model(
         lowercase_ ,       bbox=lowercase_ ,       pixel_values=lowercase_ ,       attention_mask=lowercase_ ,       token_type_ids=lowercase_ ,       labels=lowercase_ ,       )
     self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.text_seq_length, self.num_labels))





 def        snake_case__		(    self ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_):
     snake_case_ :    List[str]     			=						LayoutLMvaForQuestionAnswering(config=lowercase_)
     model.to(lowercase_)
     model.eval()
     snake_case_ :    int     			=						model(
         lowercase_ ,       bbox=lowercase_ ,       pixel_values=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        snake_case__		(    self):
     snake_case_ :    Dict     			=						self.prepare_config_and_inputs()
     (
         (
         snake_case_
     )			, (
         snake_case_
     )			, (
         snake_case_
     )			, (
         snake_case_
     )			, (
         snake_case_
     )			, (
         snake_case_
     )			, (
         snake_case_
     )			, (
         snake_case_
     )			, 
     ) :    Optional[int]     			=						config_and_inputs
     snake_case_ :    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    UpperCAmelCase_				(	snake_case__    ,						snake_case__    ,						unittest.TestCase					):
 UpperCAmelCase_		    =						False
 UpperCAmelCase_		    =						False
 UpperCAmelCase_		    =						False

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





 def        snake_case__		(    self ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_ ,       lowercase_):
     # `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        snake_case__		(    self):
     snake_case_ :    Union[str, Any]     			=						LayoutLMvaModelTester(self)
     snake_case_ :    List[Any]     			=						ConfigTester(self ,       config_class=lowercase_ ,       hidden_size=37)





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

     return inputs_dict





 def        snake_case__		(    self):
     self.config_tester.run_common_tests()





 def        snake_case__		(    self):
     snake_case_ :    Dict     			=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_)





 def        snake_case__		(    self):
     snake_case_ :    List[Any]     			=						self.model_tester.prepare_config_and_inputs()
     for type in ["absolute", "relative_key", "relative_key_query"]:
         snake_case_ :    Tuple     			=						type
         self.model_tester.create_and_check_model(*lowercase_)





 def        snake_case__		(    self):
     snake_case_ :    List[Any]     			=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*lowercase_)





 def        snake_case__		(    self):
     snake_case_ :    Any     			=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*lowercase_)





 def        snake_case__		(    self):
     snake_case_ :    List[str]     			=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*lowercase_)





 @slow
 def        snake_case__		(    self):
     for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case_ :    Optional[Any]     			=						LayoutLMvaModel.from_pretrained(lowercase_)
         self.assertIsNotNone(lowercase_)


def 						UpperCamelCase_     (	):
    """simple docstring"""






    snake_case_ :    int     			=						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"       )
    return image






@require_torch
class    UpperCAmelCase_				(	unittest.TestCase					):





 @cached_property
 def        snake_case__		(    self):
     return LayoutLMvaImageProcessor(apply_ocr=lowercase_) if is_vision_available() else None





 @slow
 def        snake_case__		(    self):
     snake_case_ :    Optional[Any]     			=						LayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base").to(lowercase_)

     snake_case_ :    Optional[Any]     			=						self.default_image_processor
     snake_case_ :    int     			=						prepare_img()
     snake_case_ :    Optional[int]     			=						image_processor(images=lowercase_ ,       return_tensors="pt").pixel_values.to(lowercase_)

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

     # forward pass
     snake_case_ :    Union[str, Any]     			=						model(
         input_ids=input_ids.to(lowercase_) ,       bbox=bbox.to(lowercase_) ,       pixel_values=pixel_values.to(lowercase_) ,       )

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

     snake_case_ :    Dict     			=						torch.tensor(
         [[-0.0_529, 0.3_618, 0.1_632], [-0.1_587, -0.1_667, -0.0_400], [-0.1_557, -0.1_671, -0.0_505]]).to(lowercase_)

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






'''simple docstring'''
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.testing_utils import require_tensorflow_text, require_tf, slow


if is_tf_available():
  import tensorflow as tf

if is_tensorflow_text_available():
  from transformers.models.bert import TFBertTokenizer


a_											=						["bert-base-uncased", "bert-base-cased"]
a_											=						"hf-internal-testing/tiny-bert-tf-only"

if is_tf_available():






  class    UpperCAmelCase_				(	tf.keras.Model					):





   def __init__(    self ,       lowercase_):
       super().__init__()
       snake_case_ :    Union[str, Any]     			=						tokenizer
       snake_case_ :    List[str]     			=						AutoConfig.from_pretrained(lowercase_)
       snake_case_ :    List[str]     			=						TFAutoModel.from_config(lowercase_)





   def        snake_case__		(    self ,       lowercase_):
       snake_case_ :    List[str]     			=						self.tokenizer(lowercase_)
       snake_case_ :    str     			=						self.bert(**lowercase_)
       return out["pooler_output"]






@require_tf
@require_tensorflow_text
class    UpperCAmelCase_				(	unittest.TestCase					):





 def        snake_case__		(    self):
     super().setUp()

     snake_case_ :    Dict     			=						[
         BertTokenizer.from_pretrained(lowercase_) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
     ]  # repeat for when fast_bert_tokenizer=false
     snake_case_ :    Tuple     			=						[TFBertTokenizer.from_pretrained(lowercase_) for checkpoint in TOKENIZER_CHECKPOINTS] + [
         TFBertTokenizer.from_pretrained(lowercase_ ,       use_fast_bert_tokenizer=lowercase_)
         for checkpoint in TOKENIZER_CHECKPOINTS
     ]
     assert len(self.tokenizers) == len(self.tf_tokenizers)

     snake_case_ :    Dict     			=						[
         "This is a straightforward English test sentence.",
         "This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.",
         "Now we're going to add some Chinese: 一 二 三 一二三",
         "And some much more rare Chinese: 齉 堃 齉堃",
         "Je vais aussi écrire en français pour tester les accents",
         "Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ",
     ]
     snake_case_ :    str     			=						list(zip(self.test_sentences ,       self.test_sentences[::-1]))





 def        snake_case__		(    self):
     for tokenizer, tf_tokenizer in zip(self.tokenizers ,       self.tf_tokenizers):
         for test_inputs in (self.test_sentences, self.paired_sentences):
             snake_case_ :    Any     			=						tokenizer(lowercase_ ,       return_tensors="tf" ,       padding="longest")
             snake_case_ :    Union[str, Any]     			=						tf_tokenizer(lowercase_)

             for key in python_outputs.keys():
                 self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape))
                 self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key] ,       tf.intaa) == tf_outputs[key]))





 @slow
 def        snake_case__		(    self):
     for tf_tokenizer in self.tf_tokenizers:
         snake_case_ :    Union[str, Any]     			=						tf_tokenizer(self.paired_sentences)
         snake_case_ :    str     			=						tf_tokenizer(
             text=[sentence[0] for sentence in self.paired_sentences] ,       text_pair=[sentence[1] for sentence in self.paired_sentences] ,       )
         for key in merged_outputs.keys():
             self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key] ,       tf.intaa) == separated_outputs[key]))





 @slow
 def        snake_case__		(    self):
     for tf_tokenizer in self.tf_tokenizers:
         snake_case_ :    int     			=						tf.function(lowercase_)
         for test_inputs in (self.test_sentences, self.paired_sentences):
             snake_case_ :    Optional[int]     			=						tf.constant(lowercase_)
             snake_case_ :    Tuple     			=						compiled_tokenizer(lowercase_)
             snake_case_ :    Any     			=						tf_tokenizer(lowercase_)

             for key in eager_outputs.keys():
                 self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key]))





 @slow
 def        snake_case__		(    self):
     for tf_tokenizer in self.tf_tokenizers:
         snake_case_ :    List[Any]     			=						ModelToSave(tokenizer=lowercase_)
         snake_case_ :    Any     			=						tf.convert_to_tensor(self.test_sentences)
         snake_case_ :    List[Any]     			=						model(lowercase_)  # Build model with some sample inputs
         with TemporaryDirectory() as tempdir:
             snake_case_ :    Optional[Any]     			=						Path(lowercase_) / "saved.model"
             model.save(lowercase_)
             snake_case_ :    Tuple     			=						tf.keras.models.load_model(lowercase_)
         snake_case_ :    str     			=						loaded_model(lowercase_)
         # We may see small differences because the loaded model is compiled, so we need an epsilon for the test
         self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output)) ,       1E-5)




