





"""simple docstring"""



from __future__ import annotations

import copy
import inspect
import unittest

import numpy as np

from transformers import is_tf_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
      import tensorflow as tf

      from transformers import (
          TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
          TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
          TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
          TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
          TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
          LayoutLMvaConfig,
          TFLayoutLMvaForQuestionAnswering,
          TFLayoutLMvaForSequenceClassification,
          TFLayoutLMvaForTokenClassification,
          TFLayoutLMvaModel,
      )

if is_vision_available():
      from PIL import Image

      from transformers import LayoutLMvaImageProcessor




class      UpperCAmelCase_  :




    def __init__(   self						:	Optional[Any]     ,			snake_case_						:	List[Any]     ,			snake_case_						:	Tuple=2     ,			snake_case_						:	List[Any]=3     ,			snake_case_						:	Optional[int]=4     ,			snake_case_						:	Any=2     ,			snake_case_						:	Union[str, Any]=7     ,			snake_case_						:	Dict=True     ,			snake_case_						:	Optional[Any]=True     ,			snake_case_						:	Union[str, Any]=True     ,			snake_case_						:	int=True     ,			snake_case_						:	List[Any]=99     ,			snake_case_						:	List[Any]=36     ,			snake_case_						:	List[Any]=2     ,			snake_case_						:	str=4     ,			snake_case_						:	int=37     ,			snake_case_						:	int="gelu"     ,			snake_case_						:	Any=0.1     ,			snake_case_						:	Union[str, Any]=0.1     ,			snake_case_						:	int=512     ,			snake_case_						:	Union[str, Any]=16     ,			snake_case_						:	Optional[Any]=2     ,			snake_case_						:	Tuple=0.02     ,			snake_case_						:	List[str]=6     ,			snake_case_						:	Dict=6     ,			snake_case_						:	Optional[Any]=3     ,			snake_case_						:	str=4     ,			snake_case_						:	Union[str, Any]=None     ,			snake_case_						:	Union[str, Any]=1_000     ,			)				->							int:




     '''simple docstring'''

     A__	=    parent
     A__	=    batch_size
     A__	=    num_channels
     A__	=    image_size
     A__	=    patch_size
     A__	=    is_training
     A__	=    use_input_mask
     A__	=    use_token_type_ids
     A__	=    use_labels
     A__	=    vocab_size
     A__	=    hidden_size
     A__	=    num_hidden_layers
     A__	=    num_attention_heads
     A__	=    intermediate_size
     A__	=    hidden_act
     A__	=    hidden_dropout_prob
     A__	=    attention_probs_dropout_prob
     A__	=    max_position_embeddings
     A__	=    type_vocab_size
     A__	=    type_sequence_label_size
     A__	=    initializer_range
     A__	=    coordinate_size
     A__	=    shape_size
     A__	=    num_labels
     A__	=    num_choices
     A__	=    scope
     A__	=    range_bbox

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




    def 							__magic_name__							(   self						:	List[Any]				)				->							Optional[int]:




     '''simple docstring'''

     A__	=    ids_tensor([self.batch_size, self.text_seq_length]     ,			self.vocab_size				)

     A__	=    ids_tensor([self.batch_size, self.text_seq_length, 4]     ,			self.range_bbox				)
     A__	=    bbox.numpy()
     # 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__	=    bbox[i, j, 3]
        A__	=    bbox[i, j, 1]
        A__	=    tmp_coordinate
       if bbox[i, j, 2] < bbox[i, j, 0]:
        A__	=    bbox[i, j, 2]
        A__	=    bbox[i, j, 0]
        A__	=    tmp_coordinate
     A__	=    tf.constant(snake_case_				)

     A__	=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

     A__	=    None
     if self.use_input_mask:
      A__	=    random_attention_mask([self.batch_size, self.text_seq_length]				)

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

     A__	=    None
     A__	=    None
     if self.use_labels:
      A__	=    ids_tensor([self.batch_size]     ,			self.type_sequence_label_size				)
      A__	=    ids_tensor([self.batch_size, self.text_seq_length]     ,			self.num_labels				)

     A__	=    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 							__magic_name__							(   self						:	List[str]     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	str     ,			snake_case_						:	int     ,			snake_case_						:	Any     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	Dict				)				->							List[str]:




     '''simple docstring'''

     A__	=    TFLayoutLMvaModel(config=snake_case_				)

     # text + image
     A__	=    model(snake_case_     ,			pixel_values=snake_case_     ,			training=snake_case_				)
     A__	=    model(
         snake_case_     ,			bbox=snake_case_     ,			pixel_values=snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_     ,			training=snake_case_     ,			)
     A__	=    model(snake_case_     ,			bbox=snake_case_     ,			pixel_values=snake_case_     ,			training=snake_case_				)

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

     # text only
     A__	=    model(snake_case_     ,			training=snake_case_				)
     self.parent.assertEqual(
         result.last_hidden_state.shape     ,			(self.batch_size, self.text_seq_length, self.hidden_size)				)

     # image only
     A__	=    model({"pixel_values": pixel_values}     ,			training=snake_case_				)
     self.parent.assertEqual(
         result.last_hidden_state.shape     ,			(self.batch_size, self.image_seq_length, self.hidden_size)				)




    def 							__magic_name__							(   self						:	List[Any]     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	List[Any]     ,			snake_case_						:	List[Any]     ,			snake_case_						:	Tuple     ,			snake_case_						:	Tuple     ,			snake_case_						:	str				)				->							Any:




     '''simple docstring'''

     A__	=    self.num_labels
     A__	=    TFLayoutLMvaForSequenceClassification(config=snake_case_				)
     A__	=    model(
         snake_case_     ,			bbox=snake_case_     ,			pixel_values=snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_     ,			labels=snake_case_     ,			training=snake_case_     ,			)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.num_labels)				)




    def 							__magic_name__							(   self						:	Any     ,			snake_case_						:	Any     ,			snake_case_						:	List[Any]     ,			snake_case_						:	int     ,			snake_case_						:	Tuple     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	int     ,			snake_case_						:	Tuple				)				->							List[str]:




     '''simple docstring'''

     A__	=    self.num_labels
     A__	=    TFLayoutLMvaForTokenClassification(config=snake_case_				)
     A__	=    model(
         snake_case_     ,			bbox=snake_case_     ,			pixel_values=snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_     ,			labels=snake_case_     ,			training=snake_case_     ,			)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.text_seq_length, self.num_labels)				)




    def 							__magic_name__							(   self						:	Union[str, Any]     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	List[Any]     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	List[str]     ,			snake_case_						:	int     ,			snake_case_						:	List[str]				)				->							List[str]:




     '''simple docstring'''

     A__	=    2
     A__	=    TFLayoutLMvaForQuestionAnswering(config=snake_case_				)
     A__	=    model(
         snake_case_     ,			bbox=snake_case_     ,			pixel_values=snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_     ,			start_positions=snake_case_     ,			end_positions=snake_case_     ,			training=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 							__magic_name__							(   self						:	Optional[Any]				)				->							List[str]:




     '''simple docstring'''

     A__	=    self.prepare_config_and_inputs()
     (A__)	=    config_and_inputs
     A__	=    {
         """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_tf
class      UpperCAmelCase_  (						__lowercase,       __lowercase,       unittest.TestCase							):
    lowercase__						 =				(
        (
            TFLayoutLMvaModel,
            TFLayoutLMvaForQuestionAnswering,
            TFLayoutLMvaForSequenceClassification,
            TFLayoutLMvaForTokenClassification,
        )
        if is_tf_available()
        else ()
    )
    lowercase__						 =				(
        {'''document-question-answering''': TFLayoutLMvaForQuestionAnswering, '''feature-extraction''': TFLayoutLMvaModel}
        if is_tf_available()
        else {}
    )

    lowercase__						 =				False
    lowercase__						 =				False
    lowercase__						 =				False




    def 							__magic_name__							(   self						:	int     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	List[Any]     ,			snake_case_						:	Dict     ,			snake_case_						:	Dict     ,			snake_case_						:	List[str]				)				->							Optional[Any]:




     '''simple docstring'''

     return True




    def 							__magic_name__							(   self						:	int     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	List[Any]     ,			snake_case_						:	int=False				)				->							dict:




     '''simple docstring'''

     A__	=    copy.deepcopy(snake_case_				)

     if model_class in get_values(snake_case_				):
      A__	=    {
          k: tf.tile(tf.expand_dims(snake_case_     ,			1				)     ,			(1, self.model_tester.num_choices) + (1,) * (v.ndim - 1)				)
          if isinstance(snake_case_     ,			tf.Tensor				) and v.ndim > 0
          else v
          for k, v in inputs_dict.items()
      }

     if return_labels:
      if model_class in get_values(snake_case_				):
       A__	=    tf.ones(self.model_tester.batch_size     ,			dtype=tf.intaa				)
      elif model_class in get_values(snake_case_				):
       A__	=    tf.zeros(self.model_tester.batch_size     ,			dtype=tf.intaa				)
       A__	=    tf.zeros(self.model_tester.batch_size     ,			dtype=tf.intaa				)
      elif model_class in get_values(snake_case_				):
       A__	=    tf.zeros(self.model_tester.batch_size     ,			dtype=tf.intaa				)
      elif model_class in get_values(snake_case_				):
       A__	=    tf.zeros(
           (self.model_tester.batch_size, self.model_tester.text_seq_length)     ,			dtype=tf.intaa				)

     return inputs_dict




    def 							__magic_name__							(   self						:	Any				)				->							int:




     '''simple docstring'''

     A__	=    TFLayoutLMvaModelTester(self				)
     A__	=    ConfigTester(self     ,			config_class=snake_case_     ,			hidden_size=37				)




    def 							__magic_name__							(   self						:	List[str]				)				->							List[str]:




     '''simple docstring'''

     self.config_tester.run_common_tests()




    def 							__magic_name__							(   self						:	List[Any]				)				->							Dict:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs_for_common()
     for model_class in self.all_model_classes:
      A__	=    model_class(snake_case_				)
      if getattr(snake_case_     ,			"hf_compute_loss"     ,			snake_case_				):
       # The number of elements in the loss should be the same as the number of elements in the label
       A__	=    self._prepare_for_class(inputs_dict.copy()     ,			snake_case_     ,			return_labels=snake_case_				)
       A__	=    prepared_for_class[
           sorted(prepared_for_class.keys() - inputs_dict.keys()     ,			reverse=snake_case_				)[0]
       ]
       A__	=    added_label.shape.as_list()[:1]

       # Test that model correctly compute the loss with kwargs
       A__	=    self._prepare_for_class(inputs_dict.copy()     ,			snake_case_     ,			return_labels=snake_case_				)
       A__	=    prepared_for_class.pop("input_ids"				)

       A__	=    model(snake_case_     ,			**snake_case_				)[0]
       self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]				)

       # Test that model correctly compute the loss when we mask some positions
       A__	=    self._prepare_for_class(inputs_dict.copy()     ,			snake_case_     ,			return_labels=snake_case_				)
       A__	=    prepared_for_class.pop("input_ids"				)
       if "labels" in prepared_for_class:
        A__	=    prepared_for_class["""labels"""].numpy()
        if len(labels.shape				) > 1 and labels.shape[1] != 1:
         A__	=    -100
         A__	=    tf.convert_to_tensor(snake_case_				)
         A__	=    model(snake_case_     ,			**snake_case_				)[0]
         self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]				)
         self.assertTrue(not np.any(np.isnan(loss.numpy()				)				)				)

                # Test that model correctly compute the loss with a dict
       A__	=    self._prepare_for_class(inputs_dict.copy()     ,			snake_case_     ,			return_labels=snake_case_				)
       A__	=    model(snake_case_				)[0]
       self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]				)

       # Test that model correctly compute the loss with a tuple
       A__	=    self._prepare_for_class(inputs_dict.copy()     ,			snake_case_     ,			return_labels=snake_case_				)

       # Get keys that were added with the _prepare_for_class function
       A__	=    prepared_for_class.keys() - inputs_dict.keys()
       A__	=    inspect.signature(model.call				).parameters
       A__	=    list(signature.keys()				)

       # Create a dictionary holding the location of the tensors in the tuple
       A__	=    {0: """input_ids"""}
       for label_key in label_keys:
        A__	=    signature_names.index(snake_case_				)
        A__	=    label_key
       A__	=    sorted(tuple_index_mapping.items()				)
       # Initialize a list with their default values, update the values and convert to a tuple
       A__	=    []

       for name in signature_names:
        if name != "kwargs":
         list_input.append(signature[name].default				)

       for index, value in sorted_tuple_index_mapping:
        A__	=    prepared_for_class[value]

       A__	=    tuple(snake_case_				)

       # Send to model
       A__	=    model(tuple_input[:-1]				)[0]

       self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]				)




    def 							__magic_name__							(   self						:	List[str]				)				->							List[Any]:




     '''simple docstring'''

     (
         A__      
     )	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_				)




    def 							__magic_name__							(   self						:	List[Any]				)				->							Optional[int]:




     '''simple docstring'''

     (
         A__      
     )	=    self.model_tester.prepare_config_and_inputs()
     for type in ["absolute", "relative_key", "relative_key_query"]:
      A__	=    type
      self.model_tester.create_and_check_model(snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_				)




    def 							__magic_name__							(   self						:	Union[str, Any]				)				->							Optional[Any]:




     '''simple docstring'''

     (
         A__      
     )	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(
         snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_				)




    def 							__magic_name__							(   self						:	Tuple				)				->							Optional[int]:




     '''simple docstring'''

     (
         A__      
     )	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(
         snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_				)




    def 							__magic_name__							(   self						:	List[str]				)				->							Union[str, Any]:




     '''simple docstring'''

     (
         A__      
     )	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(
         snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_     ,			snake_case_				)




    @slow
    def 							__magic_name__							(   self						:	str				)				->							Optional[int]:




     '''simple docstring'''

     for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      A__	=    TFLayoutLMvaModel.from_pretrained(snake_case_				)
      self.assertIsNotNone(snake_case_				)







def   _SCREAMING_SNAKE_CASE       (							)						->		Optional[Any]:
 A__	=    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
 return image






@require_tf
class      UpperCAmelCase_  (						unittest.TestCase							):




    @cached_property
    def 							__magic_name__							(   self						:	Optional[int]				)				->							Dict:




     '''simple docstring'''

     return LayoutLMvaImageProcessor(apply_ocr=snake_case_				) if is_vision_available() else None




    @slow
    def 							__magic_name__							(   self						:	str				)				->							str:




     '''simple docstring'''

     A__	=    TFLayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base"				)

     A__	=    self.default_image_processor
     A__	=    prepare_img()
     A__	=    image_processor(images=snake_case_     ,			return_tensors="tf"				).pixel_values

     A__	=    tf.constant([[1, 2]]				)
     A__	=    tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]]				)     ,			axis=0				)

     # forward pass
     A__	=    model(input_ids=snake_case_     ,			bbox=snake_case_     ,			pixel_values=snake_case_     ,			training=snake_case_				)

     # verify the logits
     A__	=    (1, 199, 768)
     self.assertEqual(outputs.last_hidden_state.shape     ,			snake_case_				)

     A__	=    tf.constant(
         [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]				)

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











"""simple docstring"""



from typing import Optional

from torch import nn

from .transformer_ad import TransformeraDModel, TransformeraDModelOutput




class      UpperCAmelCase_  (						nn.Module							):




    def __init__(   self						:	Optional[int]     ,			snake_case_						:	int = 16     ,			snake_case_						:	int = 88     ,			snake_case_						:	Optional[int] = None     ,			snake_case_						:	int = 1     ,			snake_case_						:	float = 0.0     ,			snake_case_						:	int = 32     ,			snake_case_						:	Optional[int] = None     ,			snake_case_						:	bool = False     ,			snake_case_						:	Optional[int] = None     ,			snake_case_						:	Optional[int] = None     ,			snake_case_						:	str = "geglu"     ,			snake_case_						:	Optional[int] = None     ,			)				->							str:




     '''simple docstring'''

     super().__init__()
     A__	=    nn.ModuleList(
         [
             TransformeraDModel(
                 num_attention_heads=snake_case_     ,			attention_head_dim=snake_case_     ,			in_channels=snake_case_     ,			num_layers=snake_case_     ,			dropout=snake_case_     ,			norm_num_groups=snake_case_     ,			cross_attention_dim=snake_case_     ,			attention_bias=snake_case_     ,			sample_size=snake_case_     ,			num_vector_embeds=snake_case_     ,			activation_fn=snake_case_     ,			num_embeds_ada_norm=snake_case_     ,			)
             for _ in range(2				)
         ]				)

     # Variables that can be set by a pipeline:

     # The ratio of transformer1 to transformer2's output states to be combined during inference
     A__	=    0.5

     # The shape of `encoder_hidden_states` is expected to be
     # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)`
     A__	=    [77, 257]

     # Which transformer to use to encode which condition.
     # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])`
     A__	=    [1, 0]




    def 							__magic_name__							(   self						:	Dict     ,			snake_case_						:	List[Any]     ,			snake_case_						:	Tuple     ,			snake_case_						:	Any=None     ,			snake_case_						:	int=None     ,			snake_case_						:	Union[str, Any]=None     ,			snake_case_						:	bool = True     ,			)				->							Union[str, Any]:




     '''simple docstring'''

     A__	=    hidden_states

     A__	=    []
     A__	=    0
     # attention_mask is not used yet
     for i in range(2				):
      # for each of the two transformers, pass the corresponding condition tokens
      A__	=    encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]]
      A__	=    self.transformer_index_for_condition[i]
      A__	=    self.transformers[transformer_index](
          snake_case_     ,			encoder_hidden_states=snake_case_     ,			timestep=snake_case_     ,			cross_attention_kwargs=snake_case_     ,			return_dict=snake_case_     ,			)[0]
      encoded_states.append(encoded_state - input_states				)
      tokens_start += self.condition_lengths[i]

     A__	=    encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio)
     A__	=    output_states + input_states

     if not return_dict:
      return (output_states,)

     return TransformeraDModelOutput(sample=snake_case_				)





