






"""simple docstring"""

import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import TimesformerConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, 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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch
   from torch import nn

   from transformers import (
       MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING,
       TimesformerForVideoClassification,
       TimesformerModel,
   )
   from transformers.models.timesformer.modeling_timesformer import TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from transformers import VideoMAEImageProcessor

class 						A_							:







    def __init__(							self							:						Union[str, Any]						,    __lowerCamelCase							:						Dict						,    __lowerCamelCase							:						str=1_3						,    __lowerCamelCase							:						Dict=1_0						,    __lowerCamelCase							:						int=3						,    __lowerCamelCase							:						str=2						,    __lowerCamelCase							:						List[str]=2						,    __lowerCamelCase							:						Tuple=True						,    __lowerCamelCase							:						str=True						,    __lowerCamelCase							:						str=3_2						,    __lowerCamelCase							:						List[Any]=5						,    __lowerCamelCase							:						Union[str, Any]=4						,    __lowerCamelCase							:						List[str]=3_7						,    __lowerCamelCase							:						str="gelu"						,    __lowerCamelCase							:						Optional[Any]=0.1						,    __lowerCamelCase							:						Dict=0.1						,    __lowerCamelCase							:						Tuple=1_0						,    __lowerCamelCase							:						List[str]=0.02						,    __lowerCamelCase							:						Tuple="divided_space_time"						,    __lowerCamelCase							:						int=None						,    )				->					Tuple:
          __magic_name__            =     parent
          __magic_name__            =     batch_size
          __magic_name__            =     image_size
          __magic_name__            =     num_channels
          __magic_name__            =     patch_size
          __magic_name__            =     num_frames
          __magic_name__            =     is_training
          __magic_name__            =     use_labels
          __magic_name__            =     hidden_size
          __magic_name__            =     num_hidden_layers
          __magic_name__            =     num_attention_heads
          __magic_name__            =     intermediate_size
          __magic_name__            =     hidden_act
          __magic_name__            =     hidden_dropout_prob
          __magic_name__            =     attention_probs_dropout_prob
          __magic_name__            =     attention_type
          __magic_name__            =     initializer_range
          __magic_name__            =     scope
          __magic_name__            =     num_labels

          # in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token
          __magic_name__            =     (image_size // patch_size) ** 2
          __magic_name__            =     (num_frames) * self.num_patches_per_frame + 1







    def     _snake_case					(							self							:						List[str]	)				->					int:
          __magic_name__            =     floats_tensor(
              [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]	)

          __magic_name__            =     None
          if self.use_labels:
                __magic_name__            =     ids_tensor([self.batch_size]						,    self.num_labels	)

          __magic_name__            =     self.get_config()

          return config, pixel_values, labels







    def     _snake_case					(							self							:						int	)				->					Optional[Any]:
          __magic_name__            =     TimesformerConfig(
              image_size=self.image_size						,    patch_size=self.patch_size						,    num_channels=self.num_channels						,    num_frames=self.num_frames						,    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						,    initializer_range=self.initializer_range						,    attention_type=self.attention_type						,    )
          __magic_name__            =     self.num_labels
          return config







    def     _snake_case					(							self							:						Dict						,    __lowerCamelCase							:						Any						,    __lowerCamelCase							:						Dict						,    __lowerCamelCase							:						List[str]	)				->					Dict:
          __magic_name__            =     TimesformerModel(config=__UpperCamelCase	)
          model.to(__UpperCamelCase	)
          model.eval()
          __magic_name__            =     model(__UpperCamelCase	)
          self.parent.assertEqual(result.last_hidden_state.shape						,    (self.batch_size, self.seq_length, self.hidden_size)	)







    def     _snake_case					(							self							:						Dict						,    __lowerCamelCase							:						Tuple						,    __lowerCamelCase							:						List[str]						,    __lowerCamelCase							:						str	)				->					Optional[int]:
          __magic_name__            =     TimesformerForVideoClassification(__UpperCamelCase	)
          model.to(__UpperCamelCase	)
          model.eval()

          __magic_name__            =     model(__UpperCamelCase	)

          # verify the logits shape
          __magic_name__            =     torch.Size((self.batch_size, self.num_labels)	)
          self.parent.assertEqual(result.logits.shape						,    __UpperCamelCase	)







    def     _snake_case					(							self							:						Any	)				->					Any:
          __magic_name__            =     self.prepare_config_and_inputs()
          __magic_name__      ,    __magic_name__      ,    __magic_name__            =     config_and_inputs
          __magic_name__            =     {"pixel_values": pixel_values}
          return config, inputs_dict





@require_torch
class 						A_							(      _UpperCAmelCase					,							_UpperCAmelCase					,							unittest.TestCase  ):
    UpperCAmelCase__        =				(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
    UpperCAmelCase__        =				(
        {'feature-extraction': TimesformerModel, 'video-classification': TimesformerForVideoClassification}
        if is_torch_available()
        else {}
    )

    UpperCAmelCase__        =				False
    UpperCAmelCase__        =				False
    UpperCAmelCase__        =				False
    UpperCAmelCase__        =				False







    def     _snake_case					(							self							:						List[str]	)				->					List[str]:
          __magic_name__            =     TimesformerModelTester(self	)
          __magic_name__            =     ConfigTester(
              self						,    config_class=__UpperCamelCase						,    has_text_modality=__UpperCamelCase						,    hidden_size=3_7	)







    def     _snake_case					(							self							:						str						,    __lowerCamelCase							:						Dict						,    __lowerCamelCase							:						Optional[Any]						,    __lowerCamelCase							:						Dict=False	)				->					Any:
          __magic_name__            =     copy.deepcopy(__UpperCamelCase	)

          if return_labels:
                if model_class in get_values(__UpperCamelCase	):
                      __magic_name__            =     torch.zeros(
                          self.model_tester.batch_size						,    dtype=torch.long						,    device=__UpperCamelCase	)

          return inputs_dict







    def     _snake_case					(							self							:						Union[str, Any]	)				->					int:
          self.config_tester.run_common_tests()







    @unittest.skip(reason="TimeSformer does not use inputs_embeds"	)
    def     _snake_case					(							self							:						Tuple	)				->					Any:
          pass







    def     _snake_case					(							self							:						str	)				->					Tuple:
          __magic_name__      ,    __magic_name__            =     self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                __magic_name__            =     model_class(__UpperCamelCase	)
                self.assertIsInstance(model.get_input_embeddings()						,    (nn.Module)	)
                __magic_name__            =     model.get_output_embeddings()
                self.assertTrue(x is None or isinstance(__UpperCamelCase						,    nn.Linear	)	)







    def     _snake_case					(							self							:						Any	)				->					Tuple:
          __magic_name__      ,    __magic_name__            =     self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                __magic_name__            =     model_class(__UpperCamelCase	)
                __magic_name__            =     inspect.signature(model.forward	)
                # signature.parameters is an OrderedDict => so arg_names order is deterministic
                __magic_name__            =     [*signature.parameters.keys()]

                __magic_name__            =     ["pixel_values"]
                self.assertListEqual(arg_names[:1]						,    __UpperCamelCase	)







    def     _snake_case					(							self							:						Union[str, Any]	)				->					Optional[Any]:
          __magic_name__            =     self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__UpperCamelCase	)







    def     _snake_case					(							self							:						Optional[Any]	)				->					Tuple:
          __magic_name__            =     self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_video_classification(*__UpperCamelCase	)







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






          for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                __magic_name__            =     TimesformerModel.from_pretrained(__UpperCamelCase	)
                self.assertIsNotNone(__UpperCamelCase	)







    def     _snake_case					(							self							:						int	)				->					Dict:






          if not self.has_attentions:
                pass

          else:
                __magic_name__      ,    __magic_name__            =     self.model_tester.prepare_config_and_inputs_for_common()
                __magic_name__            =     True

                for model_class in self.all_model_classes:
                      __magic_name__            =     self.model_tester.seq_length
                      __magic_name__            =     self.model_tester.num_frames

                      __magic_name__            =     True
                      __magic_name__            =     False
                      __magic_name__            =     True
                      __magic_name__            =     model_class(__UpperCamelCase	)
                      model.to(__UpperCamelCase	)
                      model.eval()
                      with torch.no_grad():
                            __magic_name__            =     model(**self._prepare_for_class(__UpperCamelCase						,    __UpperCamelCase	)	)
                      __magic_name__            =     outputs.attentions
                      self.assertEqual(len(__UpperCamelCase	)						,    self.model_tester.num_hidden_layers	)

                      # check that output_attentions also work using config
                      del inputs_dict["output_attentions"]
                      __magic_name__            =     True
                      __magic_name__            =     model_class(__UpperCamelCase	)
                      model.to(__UpperCamelCase	)
                      model.eval()
                      with torch.no_grad():
                            __magic_name__            =     model(**self._prepare_for_class(__UpperCamelCase						,    __UpperCamelCase	)	)
                      __magic_name__            =     outputs.attentions
                      self.assertEqual(len(__UpperCamelCase	)						,    self.model_tester.num_hidden_layers	)

                      # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1)
                      self.assertListEqual(
                          list(attentions[0].shape[-3:]	)						,    [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1]						,    )
                      __magic_name__            =     len(__UpperCamelCase	)

                      # Check attention is always last and order is fine
                      __magic_name__            =     True
                      __magic_name__            =     True
                      __magic_name__            =     model_class(__UpperCamelCase	)
                      model.to(__UpperCamelCase	)
                      model.eval()
                      with torch.no_grad():
                            __magic_name__            =     model(**self._prepare_for_class(__UpperCamelCase						,    __UpperCamelCase	)	)

                      self.assertEqual(out_len + 1						,    len(__UpperCamelCase	)	)

                      __magic_name__            =     outputs.attentions

                      self.assertEqual(len(__UpperCamelCase	)						,    self.model_tester.num_hidden_layers	)

                      # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1)
                      self.assertListEqual(
                          list(self_attentions[0].shape[-3:]	)						,    [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1]						,    )







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






          def check_hidden_states_output(__lowerCamelCase							:						Optional[Any]						,    __lowerCamelCase							:						Optional[int]						,    __lowerCamelCase							:						Optional[int]	):
                __magic_name__            =     model_class(__UpperCamelCase	)
                model.to(__UpperCamelCase	)
                model.eval()

                with torch.no_grad():
                      __magic_name__            =     model(**self._prepare_for_class(__UpperCamelCase						,    __UpperCamelCase	)	)

                __magic_name__            =     outputs.hidden_states
                __magic_name__            =     self.model_tester.num_hidden_layers + 1
                self.assertEqual(len(__UpperCamelCase	)						,    __UpperCamelCase	)

                __magic_name__            =     self.model_tester.seq_length

                self.assertListEqual(
                    list(hidden_states[0].shape[-2:]	)						,    [seq_length, self.model_tester.hidden_size]						,    )

          __magic_name__      ,    __magic_name__            =     self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                __magic_name__            =     True
                check_hidden_states_output(__UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase	)

                # check that output_hidden_states also work using config
                del inputs_dict["output_hidden_states"]
                __magic_name__            =     True

                check_hidden_states_output(__UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase	)






def _lowerCAmelCase	(  ):

      '''simple docstring'''






      __magic_name__            =     hf_hub_download(
          repo_id="hf-internal-testing/spaghetti-video" ,						filename="eating_spaghetti.npy" ,						repo_type="dataset"   )
      __magic_name__            =     np.load(_A   )
      return list(_A   )


@require_torch
@require_vision
class 						A_							(      unittest.TestCase  ):







    @cached_property
    def     _snake_case					(							self							:						Tuple	)				->					List[Any]:
          return (
              VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5]						,    image_std=[0.5, 0.5, 0.5]	)
              if is_vision_available()
              else None
          )







    @slow
    def     _snake_case					(							self							:						str	)				->					str:
          __magic_name__            =     TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400"	).to(
              __UpperCamelCase	)

          __magic_name__            =     self.default_image_processor
          __magic_name__            =     prepare_video()
          __magic_name__            =     image_processor(video[:8]						,    return_tensors="pt"	).to(__UpperCamelCase	)

          # forward pass
          with torch.no_grad():
                __magic_name__            =     model(**__UpperCamelCase	)

          # verify the logits
          __magic_name__            =     torch.Size((1, 4_0_0)	)
          self.assertEqual(outputs.logits.shape						,    __UpperCamelCase	)

          __magic_name__            =     torch.tensor([-0.3016, -0.7713, -0.4205]	).to(__UpperCamelCase	)

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











"""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 						A_							:







				def __init__(							self							:						int						,    __lowerCamelCase							:						int						,    __lowerCamelCase							:						Optional[int]=2						,    __lowerCamelCase							:						Any=3						,    __lowerCamelCase							:						Any=4						,    __lowerCamelCase							:						str=2						,    __lowerCamelCase							:						int=7						,    __lowerCamelCase							:						Union[str, Any]=True						,    __lowerCamelCase							:						Optional[Any]=True						,    __lowerCamelCase							:						List[Any]=True						,    __lowerCamelCase							:						Dict=True						,    __lowerCamelCase							:						Any=9_9						,    __lowerCamelCase							:						int=3_6						,    __lowerCamelCase							:						Dict=2						,    __lowerCamelCase							:						int=4						,    __lowerCamelCase							:						Dict=3_7						,    __lowerCamelCase							:						str="gelu"						,    __lowerCamelCase							:						int=0.1						,    __lowerCamelCase							:						Optional[int]=0.1						,    __lowerCamelCase							:						int=5_1_2						,    __lowerCamelCase							:						Optional[Any]=1_6						,    __lowerCamelCase							:						Any=2						,    __lowerCamelCase							:						Union[str, Any]=0.02						,    __lowerCamelCase							:						List[Any]=6						,    __lowerCamelCase							:						Tuple=6						,    __lowerCamelCase							:						Optional[int]=3						,    __lowerCamelCase							:						Any=4						,    __lowerCamelCase							:						Optional[Any]=None						,    __lowerCamelCase							:						Optional[int]=1_0_0_0						,    )				->					int:
										__magic_name__            =     parent
										__magic_name__            =     batch_size
										__magic_name__            =     num_channels
										__magic_name__            =     image_size
										__magic_name__            =     patch_size
										__magic_name__            =     is_training
										__magic_name__            =     use_input_mask
										__magic_name__            =     use_token_type_ids
										__magic_name__            =     use_labels
										__magic_name__            =     vocab_size
										__magic_name__            =     hidden_size
										__magic_name__            =     num_hidden_layers
										__magic_name__            =     num_attention_heads
										__magic_name__            =     intermediate_size
										__magic_name__            =     hidden_act
										__magic_name__            =     hidden_dropout_prob
										__magic_name__            =     attention_probs_dropout_prob
										__magic_name__            =     max_position_embeddings
										__magic_name__            =     type_vocab_size
										__magic_name__            =     type_sequence_label_size
										__magic_name__            =     initializer_range
										__magic_name__            =     coordinate_size
										__magic_name__            =     shape_size
										__magic_name__            =     num_labels
										__magic_name__            =     num_choices
										__magic_name__            =     scope
										__magic_name__            =     range_bbox

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







				def     _snake_case					(							self							:						Optional[Any]	)				->					List[Any]:
										__magic_name__            =     ids_tensor([self.batch_size, self.text_seq_length]						,    self.vocab_size	)

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

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

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

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

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

										__magic_name__            =     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							:						Dict						,    __lowerCamelCase							:						Optional[Any]						,    __lowerCamelCase							:						Optional[int]						,    __lowerCamelCase							:						Tuple						,    __lowerCamelCase							:						Optional[Any]						,    __lowerCamelCase							:						Any						,    __lowerCamelCase							:						str	)				->					Union[str, Any]:
										__magic_name__            =     TFLayoutLMvaModel(config=__lowerCamelCase	)

										# text + image
										__magic_name__            =     model(__lowerCamelCase						,    pixel_values=__lowerCamelCase						,    training=__lowerCamelCase	)
										__magic_name__            =     model(
										    __lowerCamelCase						,    bbox=__lowerCamelCase						,    pixel_values=__lowerCamelCase						,    attention_mask=__lowerCamelCase						,    token_type_ids=__lowerCamelCase						,    training=__lowerCamelCase						,    )
										__magic_name__            =     model(__lowerCamelCase						,    bbox=__lowerCamelCase						,    pixel_values=__lowerCamelCase						,    training=__lowerCamelCase	)

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

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

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







				def     _snake_case					(							self							:						Any						,    __lowerCamelCase							:						List[Any]						,    __lowerCamelCase							:						List[str]						,    __lowerCamelCase							:						Optional[Any]						,    __lowerCamelCase							:						Optional[Any]						,    __lowerCamelCase							:						Any						,    __lowerCamelCase							:						List[Any]						,    __lowerCamelCase							:						Any	)				->					List[str]:
										__magic_name__            =     self.num_labels
										__magic_name__            =     TFLayoutLMvaForSequenceClassification(config=__lowerCamelCase	)
										__magic_name__            =     model(
										    __lowerCamelCase						,    bbox=__lowerCamelCase						,    pixel_values=__lowerCamelCase						,    attention_mask=__lowerCamelCase						,    token_type_ids=__lowerCamelCase						,    labels=__lowerCamelCase						,    training=__lowerCamelCase						,    )
										self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_labels)	)







				def     _snake_case					(							self							:						Any						,    __lowerCamelCase							:						Optional[int]						,    __lowerCamelCase							:						List[str]						,    __lowerCamelCase							:						str						,    __lowerCamelCase							:						List[Any]						,    __lowerCamelCase							:						List[Any]						,    __lowerCamelCase							:						Dict						,    __lowerCamelCase							:						List[Any]	)				->					Dict:
										__magic_name__            =     self.num_labels
										__magic_name__            =     TFLayoutLMvaForTokenClassification(config=__lowerCamelCase	)
										__magic_name__            =     model(
										    __lowerCamelCase						,    bbox=__lowerCamelCase						,    pixel_values=__lowerCamelCase						,    attention_mask=__lowerCamelCase						,    token_type_ids=__lowerCamelCase						,    labels=__lowerCamelCase						,    training=__lowerCamelCase						,    )
										self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.text_seq_length, self.num_labels)	)







				def     _snake_case					(							self							:						Union[str, Any]						,    __lowerCamelCase							:						Union[str, Any]						,    __lowerCamelCase							:						Union[str, Any]						,    __lowerCamelCase							:						Optional[Any]						,    __lowerCamelCase							:						Optional[Any]						,    __lowerCamelCase							:						Tuple						,    __lowerCamelCase							:						Any						,    __lowerCamelCase							:						Optional[int]	)				->					List[Any]:
										__magic_name__            =     2
										__magic_name__            =     TFLayoutLMvaForQuestionAnswering(config=__lowerCamelCase	)
										__magic_name__            =     model(
										    __lowerCamelCase						,    bbox=__lowerCamelCase						,    pixel_values=__lowerCamelCase						,    attention_mask=__lowerCamelCase						,    token_type_ids=__lowerCamelCase						,    start_positions=__lowerCamelCase						,    end_positions=__lowerCamelCase						,    training=__lowerCamelCase						,    )
										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							:						Optional[int]	)				->					str:
										__magic_name__            =     self.prepare_config_and_inputs()
										((__magic_name__)      ,    (__magic_name__)      ,    (__magic_name__)      ,    (__magic_name__)      ,    (__magic_name__)      ,    (__magic_name__)      ,    (__magic_name__)      ,    (__magic_name__))            =     config_and_inputs
										__magic_name__            =     {
										    "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 						A_							(      snake_case_					,							snake_case_					,							unittest.TestCase  ):
				UpperCAmelCase__        =				(
				    (
				        TFLayoutLMvaModel,
				        TFLayoutLMvaForQuestionAnswering,
				        TFLayoutLMvaForSequenceClassification,
				        TFLayoutLMvaForTokenClassification,
				    )
				    if is_tf_available()
				    else ()
				)
				UpperCAmelCase__        =				(
				    {'''document-question-answering''': TFLayoutLMvaForQuestionAnswering, '''feature-extraction''': TFLayoutLMvaModel}
				    if is_tf_available()
				    else {}
				)

				UpperCAmelCase__        =				False
				UpperCAmelCase__        =				False
				UpperCAmelCase__        =				False







				def     _snake_case					(							self							:						str						,    __lowerCamelCase							:						int						,    __lowerCamelCase							:						Optional[int]						,    __lowerCamelCase							:						List[Any]						,    __lowerCamelCase							:						Any						,    __lowerCamelCase							:						Any	)				->					Union[str, Any]:
										return True







				def     _snake_case					(							self							:						Union[str, Any]						,    __lowerCamelCase							:						str						,    __lowerCamelCase							:						Union[str, Any]						,    __lowerCamelCase							:						Optional[int]=False	)				->					dict:
										__magic_name__            =     copy.deepcopy(__lowerCamelCase	)

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

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

										return inputs_dict







				def     _snake_case					(							self							:						List[str]	)				->					str:
										__magic_name__            =     TFLayoutLMvaModelTester(self	)
										__magic_name__            =     ConfigTester(self						,    config_class=__lowerCamelCase						,    hidden_size=3_7	)







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







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

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

																						__magic_name__            =     model(__lowerCamelCase						,    **__lowerCamelCase	)[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
																						__magic_name__            =     self._prepare_for_class(inputs_dict.copy()						,    __lowerCamelCase						,    return_labels=__lowerCamelCase	)
																						__magic_name__            =     prepared_for_class.pop("input_ids"	)
																						if "labels" in prepared_for_class:
																												__magic_name__            =     prepared_for_class["labels"].numpy()
																												if len(labels.shape	) > 1 and labels.shape[1] != 1:
																																		__magic_name__            =     -1_0_0
																																		__magic_name__            =     tf.convert_to_tensor(__lowerCamelCase	)
																																		__magic_name__            =     model(__lowerCamelCase						,    **__lowerCamelCase	)[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
																						__magic_name__            =     self._prepare_for_class(inputs_dict.copy()						,    __lowerCamelCase						,    return_labels=__lowerCamelCase	)
																						__magic_name__            =     model(__lowerCamelCase	)[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
																						__magic_name__            =     self._prepare_for_class(inputs_dict.copy()						,    __lowerCamelCase						,    return_labels=__lowerCamelCase	)

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

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

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

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

																						__magic_name__            =     tuple(__lowerCamelCase	)

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

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







				def     _snake_case					(							self							:						Dict	)				->					Dict:
										(
										    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    
										)            =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(__lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase	)







				def     _snake_case					(							self							:						List[str]	)				->					int:
										(
										    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    
										)            =     self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
																__magic_name__            =     type
																self.model_tester.create_and_check_model(__lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase	)







				def     _snake_case					(							self							:						Dict	)				->					List[Any]:
										(
										    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    
										)            =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(
										    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase	)







				def     _snake_case					(							self							:						Any	)				->					List[str]:
										(
										    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    
										)            =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(
										    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase	)







				def     _snake_case					(							self							:						Tuple	)				->					Optional[int]:
										(
										    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    
										)            =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(
										    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase	)







				@slow
				def     _snake_case					(							self							:						int	)				->					Any:
										for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__magic_name__            =     TFLayoutLMvaModel.from_pretrained(__lowerCamelCase	)
																self.assertIsNotNone(__lowerCamelCase	)






def _lowerCAmelCase	(  ):

						'''simple docstring'''






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


@require_tf
class 						A_							(      unittest.TestCase  ):







				@cached_property
				def     _snake_case					(							self							:						List[Any]	)				->					Dict:
										return LayoutLMvaImageProcessor(apply_ocr=__lowerCamelCase	) if is_vision_available() else None







				@slow
				def     _snake_case					(							self							:						Dict	)				->					Dict:
										__magic_name__            =     TFLayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base"	)

										__magic_name__            =     self.default_image_processor
										__magic_name__            =     prepare_img()
										__magic_name__            =     image_processor(images=__lowerCamelCase						,    return_tensors="tf"	).pixel_values

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

										# forward pass
										__magic_name__            =     model(input_ids=__lowerCamelCase						,    bbox=__lowerCamelCase						,    pixel_values=__lowerCamelCase						,    training=__lowerCamelCase	)

										# verify the logits
										__magic_name__            =     (1, 1_9_9, 7_6_8)
										self.assertEqual(outputs.last_hidden_state.shape						,    __lowerCamelCase	)

										__magic_name__            =     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]						,    __lowerCamelCase						,    atol=1e-4	)	)



