




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


							def __init__(			self      :						int			,    snake_case      :						int			,    snake_case      :						Any=2			,    snake_case      :						Union[str, Any]=3			,    snake_case      :						List[Any]=4			,    snake_case      :						Tuple=2			,    snake_case      :						Union[str, Any]=7			,    snake_case      :						List[Any]=True			,    snake_case      :						int=True			,    snake_case      :						Any=True			,    snake_case      :						Dict=True			,    snake_case      :						Optional[Any]=99			,    snake_case      :						int=36			,    snake_case      :						List[Any]=2			,    snake_case      :						List[str]=4			,    snake_case      :						int=37			,    snake_case      :						Union[str, Any]="gelu"			,    snake_case      :						Dict=0.1			,    snake_case      :						int=0.1			,    snake_case      :						Optional[int]=512			,    snake_case      :						int=16			,    snake_case      :						List[Any]=2			,    snake_case      :						List[str]=0.02			,    snake_case      :						Optional[int]=6			,    snake_case      :						str=6			,    snake_case      :						str=3			,    snake_case      :						Any=4			,    snake_case      :						int=None			,    snake_case      :						Optional[int]=1000			,    ):
													'''simple docstring'''





													A__						:  int		  =  parent
													A__						:  Dict		  =  batch_size
													A__						:  Optional[Any]		  =  num_channels
													A__						:  int		  =  image_size
													A__						:  int		  =  patch_size
													A__						:  Union[str, Any]		  =  is_training
													A__						:  List[str]		  =  use_input_mask
													A__						:  Tuple		  =  use_token_type_ids
													A__						:  List[Any]		  =  use_labels
													A__						:  List[Any]		  =  vocab_size
													A__						:  Any		  =  hidden_size
													A__						:  int		  =  num_hidden_layers
													A__						:  Union[str, Any]		  =  num_attention_heads
													A__						:  Dict		  =  intermediate_size
													A__						:  Tuple		  =  hidden_act
													A__						:  Any		  =  hidden_dropout_prob
													A__						:  Optional[int]		  =  attention_probs_dropout_prob
													A__						:  List[str]		  =  max_position_embeddings
													A__						:  int		  =  type_vocab_size
													A__						:  Union[str, Any]		  =  type_sequence_label_size
													A__						:  Optional[Any]		  =  initializer_range
													A__						:  Tuple		  =  coordinate_size
													A__						:  int		  =  shape_size
													A__						:  int		  =  num_labels
													A__						:  Optional[int]		  =  num_choices
													A__						:  Union[str, Any]		  =  scope
													A__						:  List[str]		  =  range_bbox

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


							def 						_UpperCamelCase       (			self      :						int							):
													'''simple docstring'''





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

													A__						:  Any		  =  ids_tensor([self.batch_size, self.text_seq_length, 4]			,    self.range_bbox							)
													A__						:  Optional[int]		  =  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__						:  Dict		  =  bbox[i, j, 3]
																															A__						:  Dict		  =  bbox[i, j, 1]
																															A__						:  str		  =  tmp_coordinate
																									if bbox[i, j, 2] < bbox[i, j, 0]:
																															A__						:  List[Any]		  =  bbox[i, j, 2]
																															A__						:  Optional[int]		  =  bbox[i, j, 0]
																															A__						:  List[str]		  =  tmp_coordinate
													A__						:  str		  =  tf.constant(snake_case							)

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

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

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

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

													A__						:  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 						_UpperCamelCase       (			self      :						Any			,    snake_case      :						Union[str, Any]			,    snake_case      :						Optional[int]			,    snake_case      :						Any			,    snake_case      :						Optional[int]			,    snake_case      :						Any			,    snake_case      :						Union[str, Any]							):
													'''simple docstring'''





													A__						:  List[Any]		  =  TFLayoutLMvaModel(config=snake_case							)

													# text + image
													A__						:  int		  =  model(snake_case			,    pixel_values=snake_case			,    training=snake_case							)
													A__						:  Dict		  =  model(
													    snake_case			,    bbox=snake_case			,    pixel_values=snake_case			,    attention_mask=snake_case			,    token_type_ids=snake_case			,    training=snake_case			,    )
													A__						:  Optional[int]		  =  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__						:  Tuple		  =  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__						:  Union[str, Any]		  =  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 						_UpperCamelCase       (			self      :						Optional[int]			,    snake_case      :						str			,    snake_case      :						Optional[int]			,    snake_case      :						int			,    snake_case      :						Union[str, Any]			,    snake_case      :						str			,    snake_case      :						Dict			,    snake_case      :						Tuple							):
													'''simple docstring'''





													A__						:  Optional[int]		  =  self.num_labels
													A__						:  Tuple		  =  TFLayoutLMvaForSequenceClassification(config=snake_case							)
													A__						:  Optional[Any]		  =  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 						_UpperCamelCase       (			self      :						List[Any]			,    snake_case      :						Tuple			,    snake_case      :						List[Any]			,    snake_case      :						List[str]			,    snake_case      :						Union[str, Any]			,    snake_case      :						Any			,    snake_case      :						Union[str, Any]			,    snake_case      :						str							):
													'''simple docstring'''





													A__						:  Dict		  =  self.num_labels
													A__						:  Optional[int]		  =  TFLayoutLMvaForTokenClassification(config=snake_case							)
													A__						:  Optional[int]		  =  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 						_UpperCamelCase       (			self      :						Any			,    snake_case      :						Tuple			,    snake_case      :						Any			,    snake_case      :						int			,    snake_case      :						Any			,    snake_case      :						List[Any]			,    snake_case      :						Dict			,    snake_case      :						Optional[Any]							):
													'''simple docstring'''





													A__						:  Dict		  =  2
													A__						:  int		  =  TFLayoutLMvaForQuestionAnswering(config=snake_case							)
													A__						:  int		  =  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 						_UpperCamelCase       (			self      :						str							):
													'''simple docstring'''





													A__						:  str		  =  self.prepare_config_and_inputs()
													(A__)						:  Optional[int]		  =  config_and_inputs
													A__						:  int		  =  {
													    """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 							__SCREAMING_SNAKE_CASE						(				UpperCAmelCase__       ,		UpperCAmelCase__       ,		unittest.TestCase		):
							snake_case_     =							(
							    (
							        TFLayoutLMvaModel,
							        TFLayoutLMvaForQuestionAnswering,
							        TFLayoutLMvaForSequenceClassification,
							        TFLayoutLMvaForTokenClassification,
							    )
							    if is_tf_available()
							    else ()
							)
							snake_case_     =							(
							    {'document-question-answering': TFLayoutLMvaForQuestionAnswering, 'feature-extraction': TFLayoutLMvaModel}
							    if is_tf_available()
							    else {}
							)

							snake_case_     =							False
							snake_case_     =							False
							snake_case_     =							False


							def 						_UpperCamelCase       (			self      :						List[Any]			,    snake_case      :						str			,    snake_case      :						Union[str, Any]			,    snake_case      :						Union[str, Any]			,    snake_case      :						Union[str, Any]			,    snake_case      :						Any							):
													'''simple docstring'''





													return True


							def 						_UpperCamelCase       (			self      :						Tuple			,    snake_case      :						int			,    snake_case      :						Union[str, Any]			,    snake_case      :						Optional[int]=False							):
													'''simple docstring'''





													A__						:  Union[str, Any]		  =  copy.deepcopy(snake_case							)

													if model_class in get_values(snake_case							):
																			A__						:  Tuple		  =  {
																			    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__						:  Union[str, Any]		  =  tf.ones(self.model_tester.batch_size			,    dtype=tf.intaa							)
																			elif model_class in get_values(snake_case							):
																									A__						:  List[Any]		  =  tf.zeros(self.model_tester.batch_size			,    dtype=tf.intaa							)
																									A__						:  Dict		  =  tf.zeros(self.model_tester.batch_size			,    dtype=tf.intaa							)
																			elif model_class in get_values(snake_case							):
																									A__						:  Union[str, Any]		  =  tf.zeros(self.model_tester.batch_size			,    dtype=tf.intaa							)
																			elif model_class in get_values(snake_case							):
																									A__						:  Tuple		  =  tf.zeros(
																									    (self.model_tester.batch_size, self.model_tester.text_seq_length)			,    dtype=tf.intaa							)

													return inputs_dict


							def 						_UpperCamelCase       (			self      :						Any							):
													'''simple docstring'''





													A__						:  List[str]		  =  TFLayoutLMvaModelTester(self							)
													A__						:  int		  =  ConfigTester(self			,    config_class=snake_case			,    hidden_size=37							)


							def 						_UpperCamelCase       (			self      :						Dict							):
													'''simple docstring'''





													self.config_tester.run_common_tests()


							def 						_UpperCamelCase       (			self      :						Optional[Any]							):
													'''simple docstring'''





													A__						:  List[Any]		  =  self.model_tester.prepare_config_and_inputs_for_common()
													for model_class in self.all_model_classes:
																			A__						:  str		  =  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__						:  Optional[int]		  =  self._prepare_for_class(inputs_dict.copy()			,    snake_case			,    return_labels=snake_case							)
																									A__						:  Tuple		  =  prepared_for_class[
																									    sorted(prepared_for_class.keys() - inputs_dict.keys()			,    reverse=snake_case							)[0]
																									]
																									A__						:  Any		  =  added_label.shape.as_list()[:1]

																									# Test that model correctly compute the loss with kwargs
																									A__						:  Union[str, Any]		  =  self._prepare_for_class(inputs_dict.copy()			,    snake_case			,    return_labels=snake_case							)
																									A__						:  Any		  =  prepared_for_class.pop("""input_ids"""							)

																									A__						:  Tuple		  =  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__						:  List[Any]		  =  self._prepare_for_class(inputs_dict.copy()			,    snake_case			,    return_labels=snake_case							)
																									A__						:  Optional[int]		  =  prepared_for_class.pop("""input_ids"""							)
																									if "labels" in prepared_for_class:
																															A__						:  str		  =  prepared_for_class["""labels"""].numpy()
																															if len(labels.shape							) > 1 and labels.shape[1] != 1:
																																					A__						:  Dict		  =  -100
																																					A__						:  Any		  =  tf.convert_to_tensor(snake_case							)
																																					A__						:  Optional[Any]		  =  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__						:  str		  =  self._prepare_for_class(inputs_dict.copy()			,    snake_case			,    return_labels=snake_case							)
																									A__						:  List[str]		  =  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__						:  List[str]		  =  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__						:  List[Any]		  =  prepared_for_class.keys() - inputs_dict.keys()
																									A__						:  List[str]		  =  inspect.signature(model.call							).parameters
																									A__						:  Optional[Any]		  =  list(signature.keys()							)

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

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

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

																									A__						:  Dict		  =  tuple(snake_case							)

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

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


							def 						_UpperCamelCase       (			self      :						Union[str, Any]							):
													'''simple docstring'''





													(
													    A__		
													)						:  Union[str, Any]		  =  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 						_UpperCamelCase       (			self      :						Optional[int]							):
													'''simple docstring'''





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


							def 						_UpperCamelCase       (			self      :						List[str]							):
													'''simple docstring'''





													(
													    A__		
													)						:  str		  =  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 						_UpperCamelCase       (			self      :						Any							):
													'''simple docstring'''





													(
													    A__		
													)						:  Optional[int]		  =  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 						_UpperCamelCase       (			self      :						List[Any]							):
													'''simple docstring'''





													(
													    A__		
													)						:  Tuple		  =  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 						_UpperCamelCase       (			self      :						str							):
													'''simple docstring'''





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




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






@require_tf
class 							__SCREAMING_SNAKE_CASE						(				unittest.TestCase		):


							@cached_property
							def 						_UpperCamelCase       (			self      :						str							):
													'''simple docstring'''





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


							@slow
							def 						_UpperCamelCase       (			self      :						Dict							):
													'''simple docstring'''





													A__						:  List[Any]		  =  TFLayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base"""							)

													A__						:  str		  =  self.default_image_processor
													A__						:  Dict		  =  prepare_img()
													A__						:  List[str]		  =  image_processor(images=snake_case			,    return_tensors="""tf"""							).pixel_values

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

													# forward pass
													A__						:  Tuple		  =  model(input_ids=snake_case			,    bbox=snake_case			,    pixel_values=snake_case			,    training=snake_case							)

													# verify the logits
													A__						:  List[Any]		  =  (1, 199, 768)
													self.assertEqual(outputs.last_hidden_state.shape			,    snake_case							)

													A__						:  Optional[Any]		  =  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 TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


A_    =							{'''configuration_sew''': ['''SEW_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SEWConfig''']}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						A_    =							[
						    '''SEW_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''SEWForCTC''',
						    '''SEWForSequenceClassification''',
						    '''SEWModel''',
						    '''SEWPreTrainedModel''',
						]

if TYPE_CHECKING:
						from .configuration_sew import SEW_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_sew import (
												    SEW_PRETRAINED_MODEL_ARCHIVE_LIST,
												    SEWForCTC,
												    SEWForSequenceClassification,
												    SEWModel,
												    SEWPreTrainedModel,
												)


else:
						import sys

						A_    =							_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)






