





import argparse
import json
from dataclasses import dataclass, field
from functools import partial
from pathlib import Path
from typing import List

import timm
import torch
import torch.nn as nn
from huggingface_hub import hf_hub_download
from torch import Tensor

from transformers import AutoImageProcessor, ResNetConfig, ResNetForImageClassification
from transformers.utils import logging


logging.set_verbosity_info()
_lowerCamelCase :    int  							= logging.get_logger()


@dataclass
class 						lowerCamelCase       :







							"""simple docstring"""




							UpperCAmelCase_      						=   42
							UpperCAmelCase_      						=   field(default_factory=__lowerCamelCase      )
							UpperCAmelCase_      						=   field(default_factory=__lowerCamelCase      )






							def  A_     ( self     :					Tuple,			_UpperCAmelCase     :					Any,			_UpperCAmelCase     :					Tensor,			_UpperCAmelCase     :					Tensor						)				->			Optional[Any]:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      List[str]     =						len(list(m.modules()						)						) == 1 or isinstance(_UpperCAmelCase,			nn.Convad						) or isinstance(_UpperCAmelCase,			nn.BatchNormad						)
														if has_not_submodules:
																					self.traced.append(_UpperCAmelCase						)






							def __call__( self     :					Tuple,			_UpperCAmelCase     :					Tensor						)				->			List[str]:





														"""simple docstring"""
														for m in self.module.modules():
																					self.handles.append(m.register_forward_hook(self._forward_hook						)						)
														self.module(_UpperCAmelCase						)
														[x.remove() for x in self.handles]
														return self






							@property
							def  A_     ( self     :					str						)				->			Optional[int]:





														"""simple docstring"""
														return list(filter(lambda _UpperCAmelCase						: len(list(x.state_dict().keys()						)						) > 0,			self.traced						)						)


@dataclass
class 						lowerCamelCase       :







							"""simple docstring"""




							UpperCAmelCase_      						=   42
							UpperCAmelCase_      						=   42
							UpperCAmelCase_      						=   0
							UpperCAmelCase_      						=   field(default_factory=__lowerCamelCase      )
							UpperCAmelCase_      						=   field(default_factory=__lowerCamelCase      )






							def __call__( self     :					List[str],			_UpperCAmelCase     :					Tensor						)				->			Dict:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      Dict     =						Tracker(self.dest						)(_UpperCAmelCase						).parametrized
														SCREAMING_SNAKE_CASE__       :      Optional[int]     =						Tracker(self.src						)(_UpperCAmelCase						).parametrized

														SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						list(filter(lambda _UpperCAmelCase						: type(_UpperCAmelCase						) not in self.src_skip,			_UpperCAmelCase						)						)
														SCREAMING_SNAKE_CASE__       :      Any     =						list(filter(lambda _UpperCAmelCase						: type(_UpperCAmelCase						) not in self.dest_skip,			_UpperCAmelCase						)						)

														if len(_UpperCAmelCase						) != len(_UpperCAmelCase						):
																					raise Exception(
																					    F'''Numbers of operations are different. Source module has {len(_UpperCAmelCase						)} operations while'''
																					    F''' destination module has {len(_UpperCAmelCase						)}.'''						)

														for dest_m, src_m in zip(_UpperCAmelCase,			_UpperCAmelCase						):
																					dest_m.load_state_dict(src_m.state_dict()						)
																					if self.verbose == 1:
																												print(F'''Transfered from={src_m} to={dest_m}'''						)




def _a (  SCREAMING_SNAKE_CASE__      :  str			,				SCREAMING_SNAKE_CASE__      :  ResNetConfig			,				SCREAMING_SNAKE_CASE__      :  Path			,				SCREAMING_SNAKE_CASE__      :  bool = True       )	->    Optional[int]:

							'''simple docstring'''






							print(f'''Converting {name}...'''       )
							with torch.no_grad():
														SCREAMING_SNAKE_CASE__       :      Any     =						timm.create_model(SCREAMING_SNAKE_CASE__			,				pretrained=SCREAMING_SNAKE_CASE__       ).eval()
														SCREAMING_SNAKE_CASE__       :      List[str]     =						ResNetForImageClassification(SCREAMING_SNAKE_CASE__       ).eval()
														SCREAMING_SNAKE_CASE__       :      List[str]     =						ModuleTransfer(src=SCREAMING_SNAKE_CASE__			,				dest=SCREAMING_SNAKE_CASE__       )
														SCREAMING_SNAKE_CASE__       :      Optional[int]     =						torch.randn((1, 3, 2_24, 2_24)       )
														module_transfer(SCREAMING_SNAKE_CASE__       )

							assert torch.allclose(from_model(SCREAMING_SNAKE_CASE__       )			,				our_model(SCREAMING_SNAKE_CASE__       ).logits       ), "The model logits don't match the original one."

							SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						f'''resnet{"-".join(name.split("resnet"       )       )}'''
							print(SCREAMING_SNAKE_CASE__       )

							if push_to_hub:
														our_model.push_to_hub(
														    repo_path_or_name=save_directory / checkpoint_name			,				commit_message="Add model"			,				use_temp_dir=SCREAMING_SNAKE_CASE__			,				)

														# we can use the convnext one
														SCREAMING_SNAKE_CASE__       :      Tuple     =						AutoImageProcessor.from_pretrained("facebook/convnext-base-224-22k-1k"       )
														image_processor.push_to_hub(
														    repo_path_or_name=save_directory / checkpoint_name			,				commit_message="Add image processor"			,				use_temp_dir=SCREAMING_SNAKE_CASE__			,				)

														print(f'''Pushed {checkpoint_name}'''       )




def _a (  SCREAMING_SNAKE_CASE__      :  Path			,				SCREAMING_SNAKE_CASE__      :  str = None			,				SCREAMING_SNAKE_CASE__      :  bool = True       )	->    int:

							'''simple docstring'''






							SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						"imagenet-1k-id2label.json"
							SCREAMING_SNAKE_CASE__       :      int     =						10_00
							SCREAMING_SNAKE_CASE__       :      List[str]     =						(1, num_labels)

							SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						"huggingface/label-files"
							SCREAMING_SNAKE_CASE__       :      List[str]     =						num_labels
							SCREAMING_SNAKE_CASE__       :      Any     =						json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				repo_type="dataset"       )			,				"r"       )       )
							SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						{int(SCREAMING_SNAKE_CASE__       ): v for k, v in idalabel.items()}

							SCREAMING_SNAKE_CASE__       :      int     =						idalabel
							SCREAMING_SNAKE_CASE__       :      str     =						{v: k for k, v in idalabel.items()}

							SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						partial(SCREAMING_SNAKE_CASE__			,				num_labels=SCREAMING_SNAKE_CASE__			,				idalabel=SCREAMING_SNAKE_CASE__			,				labelaid=SCREAMING_SNAKE_CASE__       )

							SCREAMING_SNAKE_CASE__       :      Dict     =						{
							    "resnet18": ImageNetPreTrainedConfig(
							        depths=[2, 2, 2, 2]			,				hidden_sizes=[64, 1_28, 2_56, 5_12]			,				layer_type="basic"       ),
							    "resnet26": ImageNetPreTrainedConfig(
							        depths=[2, 2, 2, 2]			,				hidden_sizes=[2_56, 5_12, 10_24, 20_48]			,				layer_type="bottleneck"       ),
							    "resnet34": ImageNetPreTrainedConfig(
							        depths=[3, 4, 6, 3]			,				hidden_sizes=[64, 1_28, 2_56, 5_12]			,				layer_type="basic"       ),
							    "resnet50": ImageNetPreTrainedConfig(
							        depths=[3, 4, 6, 3]			,				hidden_sizes=[2_56, 5_12, 10_24, 20_48]			,				layer_type="bottleneck"       ),
							    "resnet101": ImageNetPreTrainedConfig(
							        depths=[3, 4, 23, 3]			,				hidden_sizes=[2_56, 5_12, 10_24, 20_48]			,				layer_type="bottleneck"       ),
							    "resnet152": ImageNetPreTrainedConfig(
							        depths=[3, 8, 36, 3]			,				hidden_sizes=[2_56, 5_12, 10_24, 20_48]			,				layer_type="bottleneck"       ),
							}

							if model_name:
														convert_weight_and_push(SCREAMING_SNAKE_CASE__			,				names_to_config[model_name]			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__       )
							else:
														for model_name, config in names_to_config.items():
																					convert_weight_and_push(SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__       )
							return config, expected_shape


if __name__ == "__main__":
		_lowerCamelCase :    List[str]  							= argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    '''--model_name''',
		    default=None,
		    type=str,
		    help=(
		        '''The name of the model you wish to convert, it must be one of the supported resnet* architecture,'''
		        ''' currently: resnet18,26,34,50,101,152. If `None`, all of them will the converted.'''
		    ),
		)
		parser.add_argument(
		    '''--pytorch_dump_folder_path''',
		    default=None,
		    type=Path,
		    required=True,
		    help='''Path to the output PyTorch model directory.''',
		)
		parser.add_argument(
		    '''--push_to_hub''',
		    default=True,
		    type=bool,
		    required=False,
		    help='''If True, push model and image processor to the hub.''',
		)

		_lowerCamelCase :    Optional[int]  							= parser.parse_args()
		_lowerCamelCase :    Path  							= args.pytorch_dump_folder_path
		pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True)
		convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)













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 						lowerCamelCase       :







							"""simple docstring"""




							def __init__( self     :					List[Any],			_UpperCAmelCase     :					List[str],			_UpperCAmelCase     :					Any=2,			_UpperCAmelCase     :					List[str]=3,			_UpperCAmelCase     :					Union[str, Any]=4,			_UpperCAmelCase     :					Union[str, Any]=2,			_UpperCAmelCase     :					int=7,			_UpperCAmelCase     :					Tuple=True,			_UpperCAmelCase     :					int=True,			_UpperCAmelCase     :					List[str]=True,			_UpperCAmelCase     :					List[str]=True,			_UpperCAmelCase     :					Tuple=9_9,			_UpperCAmelCase     :					Any=3_6,			_UpperCAmelCase     :					List[str]=2,			_UpperCAmelCase     :					int=4,			_UpperCAmelCase     :					str=3_7,			_UpperCAmelCase     :					List[str]="gelu",			_UpperCAmelCase     :					Optional[int]=0.1,			_UpperCAmelCase     :					str=0.1,			_UpperCAmelCase     :					Optional[int]=5_1_2,			_UpperCAmelCase     :					Optional[Any]=1_6,			_UpperCAmelCase     :					int=2,			_UpperCAmelCase     :					Tuple=0.02,			_UpperCAmelCase     :					Optional[int]=6,			_UpperCAmelCase     :					List[Any]=6,			_UpperCAmelCase     :					Any=3,			_UpperCAmelCase     :					List[str]=4,			_UpperCAmelCase     :					Tuple=None,			_UpperCAmelCase     :					str=1_0_0_0,			)				->			str:





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

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






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





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      List[str]     =						ids_tensor([self.batch_size, self.text_seq_length],			self.vocab_size						)

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

														SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

														SCREAMING_SNAKE_CASE__       :      List[Any]     =						None
														if self.use_input_mask:
																					SCREAMING_SNAKE_CASE__       :      Dict     =						random_attention_mask([self.batch_size, self.text_seq_length]						)

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

														SCREAMING_SNAKE_CASE__       :      int     =						None
														SCREAMING_SNAKE_CASE__       :      Tuple     =						None
														if self.use_labels:
																					SCREAMING_SNAKE_CASE__       :      Optional[int]     =						ids_tensor([self.batch_size],			self.type_sequence_label_size						)
																					SCREAMING_SNAKE_CASE__       :      Dict     =						ids_tensor([self.batch_size, self.text_seq_length],			self.num_labels						)

														SCREAMING_SNAKE_CASE__       :      List[Any]     =						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  A_     ( self     :					Tuple,			_UpperCAmelCase     :					str,			_UpperCAmelCase     :					Optional[Any],			_UpperCAmelCase     :					Any,			_UpperCAmelCase     :					List[Any],			_UpperCAmelCase     :					Union[str, Any],			_UpperCAmelCase     :					Optional[Any]						)				->			Union[str, Any]:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						TFLayoutLMvaModel(config=_UpperCAmelCase						)

														# text + image
														SCREAMING_SNAKE_CASE__       :      str     =						model(_UpperCAmelCase,			pixel_values=_UpperCAmelCase,			training=_UpperCAmelCase						)
														SCREAMING_SNAKE_CASE__       :      List[Any]     =						model(
														    _UpperCAmelCase,			bbox=_UpperCAmelCase,			pixel_values=_UpperCAmelCase,			attention_mask=_UpperCAmelCase,			token_type_ids=_UpperCAmelCase,			training=_UpperCAmelCase,			)
														SCREAMING_SNAKE_CASE__       :      List[Any]     =						model(_UpperCAmelCase,			bbox=_UpperCAmelCase,			pixel_values=_UpperCAmelCase,			training=_UpperCAmelCase						)

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

														# text only
														SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						model(_UpperCAmelCase,			training=_UpperCAmelCase						)
														self.parent.assertEqual(
														    result.last_hidden_state.shape,			(self.batch_size, self.text_seq_length, self.hidden_size)						)

														# image only
														SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						model({"pixel_values": pixel_values},			training=_UpperCAmelCase						)
														self.parent.assertEqual(
														    result.last_hidden_state.shape,			(self.batch_size, self.image_seq_length, self.hidden_size)						)






							def  A_     ( self     :					List[Any],			_UpperCAmelCase     :					List[Any],			_UpperCAmelCase     :					int,			_UpperCAmelCase     :					Union[str, Any],			_UpperCAmelCase     :					Dict,			_UpperCAmelCase     :					List[str],			_UpperCAmelCase     :					Dict,			_UpperCAmelCase     :					List[Any]						)				->			Union[str, Any]:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      Tuple     =						self.num_labels
														SCREAMING_SNAKE_CASE__       :      List[str]     =						TFLayoutLMvaForSequenceClassification(config=_UpperCAmelCase						)
														SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						model(
														    _UpperCAmelCase,			bbox=_UpperCAmelCase,			pixel_values=_UpperCAmelCase,			attention_mask=_UpperCAmelCase,			token_type_ids=_UpperCAmelCase,			labels=_UpperCAmelCase,			training=_UpperCAmelCase,			)
														self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.num_labels)						)






							def  A_     ( self     :					List[Any],			_UpperCAmelCase     :					str,			_UpperCAmelCase     :					int,			_UpperCAmelCase     :					str,			_UpperCAmelCase     :					Any,			_UpperCAmelCase     :					List[str],			_UpperCAmelCase     :					List[str],			_UpperCAmelCase     :					Dict						)				->			Dict:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      int     =						self.num_labels
														SCREAMING_SNAKE_CASE__       :      Dict     =						TFLayoutLMvaForTokenClassification(config=_UpperCAmelCase						)
														SCREAMING_SNAKE_CASE__       :      Tuple     =						model(
														    _UpperCAmelCase,			bbox=_UpperCAmelCase,			pixel_values=_UpperCAmelCase,			attention_mask=_UpperCAmelCase,			token_type_ids=_UpperCAmelCase,			labels=_UpperCAmelCase,			training=_UpperCAmelCase,			)
														self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.text_seq_length, self.num_labels)						)






							def  A_     ( self     :					Dict,			_UpperCAmelCase     :					Optional[int],			_UpperCAmelCase     :					Optional[Any],			_UpperCAmelCase     :					Dict,			_UpperCAmelCase     :					Union[str, Any],			_UpperCAmelCase     :					int,			_UpperCAmelCase     :					Any,			_UpperCAmelCase     :					Optional[Any]						)				->			str:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						2
														SCREAMING_SNAKE_CASE__       :      str     =						TFLayoutLMvaForQuestionAnswering(config=_UpperCAmelCase						)
														SCREAMING_SNAKE_CASE__       :      int     =						model(
														    _UpperCAmelCase,			bbox=_UpperCAmelCase,			pixel_values=_UpperCAmelCase,			attention_mask=_UpperCAmelCase,			token_type_ids=_UpperCAmelCase,			start_positions=_UpperCAmelCase,			end_positions=_UpperCAmelCase,			training=_UpperCAmelCase,			)
														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  A_     ( self     :					List[Any]						)				->			Any:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						self.prepare_config_and_inputs()
														((SCREAMING_SNAKE_CASE__)   ,(SCREAMING_SNAKE_CASE__)   ,(SCREAMING_SNAKE_CASE__)   ,(SCREAMING_SNAKE_CASE__)   ,(SCREAMING_SNAKE_CASE__)   ,(SCREAMING_SNAKE_CASE__)   ,(SCREAMING_SNAKE_CASE__)   ,(SCREAMING_SNAKE_CASE__))       :      Optional[Any]     =						config_and_inputs
														SCREAMING_SNAKE_CASE__       :      List[str]     =						{
														    "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 						lowerCamelCase       (__lowerCamelCase							,     __lowerCamelCase							,     unittest.TestCase      ):







							"""simple docstring"""




							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  A_     ( self     :					Optional[Any],			_UpperCAmelCase     :					Tuple,			_UpperCAmelCase     :					Any,			_UpperCAmelCase     :					Dict,			_UpperCAmelCase     :					str,			_UpperCAmelCase     :					Optional[int]						)				->			List[str]:





														"""simple docstring"""
														return True






							def  A_     ( self     :					Optional[int],			_UpperCAmelCase     :					Optional[int],			_UpperCAmelCase     :					str,			_UpperCAmelCase     :					Dict=False						)				->			dict:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						copy.deepcopy(_UpperCAmelCase						)

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

														if return_labels:
																					if model_class in get_values(_UpperCAmelCase						):
																												SCREAMING_SNAKE_CASE__       :      List[Any]     =						tf.ones(self.model_tester.batch_size,			dtype=tf.intaa						)
																					elif model_class in get_values(_UpperCAmelCase						):
																												SCREAMING_SNAKE_CASE__       :      Tuple     =						tf.zeros(self.model_tester.batch_size,			dtype=tf.intaa						)
																												SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						tf.zeros(self.model_tester.batch_size,			dtype=tf.intaa						)
																					elif model_class in get_values(_UpperCAmelCase						):
																												SCREAMING_SNAKE_CASE__       :      str     =						tf.zeros(self.model_tester.batch_size,			dtype=tf.intaa						)
																					elif model_class in get_values(_UpperCAmelCase						):
																												SCREAMING_SNAKE_CASE__       :      Tuple     =						tf.zeros(
																												    (self.model_tester.batch_size, self.model_tester.text_seq_length),			dtype=tf.intaa						)

														return inputs_dict






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





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      List[Any]     =						TFLayoutLMvaModelTester(self						)
														SCREAMING_SNAKE_CASE__       :      List[Any]     =						ConfigTester(self,			config_class=_UpperCAmelCase,			hidden_size=3_7						)






							def  A_     ( self     :					Optional[int]						)				->			List[str]:





														"""simple docstring"""
														self.config_tester.run_common_tests()






							def  A_     ( self     :					List[Any]						)				->			List[Any]:





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

																												# Test that model correctly compute the loss with kwargs
																												SCREAMING_SNAKE_CASE__       :      List[Any]     =						self._prepare_for_class(inputs_dict.copy(),			_UpperCAmelCase,			return_labels=_UpperCAmelCase						)
																												SCREAMING_SNAKE_CASE__       :      int     =						prepared_for_class.pop("input_ids"						)

																												SCREAMING_SNAKE_CASE__       :      Dict     =						model(_UpperCAmelCase,			**_UpperCAmelCase						)[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
																												SCREAMING_SNAKE_CASE__       :      List[Any]     =						self._prepare_for_class(inputs_dict.copy(),			_UpperCAmelCase,			return_labels=_UpperCAmelCase						)
																												SCREAMING_SNAKE_CASE__       :      Tuple     =						prepared_for_class.pop("input_ids"						)
																												if "labels" in prepared_for_class:
																																			SCREAMING_SNAKE_CASE__       :      str     =						prepared_for_class["labels"].numpy()
																																			if len(labels.shape						) > 1 and labels.shape[1] != 1:
																																										SCREAMING_SNAKE_CASE__       :      Any     =						-1_0_0
																																										SCREAMING_SNAKE_CASE__       :      Dict     =						tf.convert_to_tensor(_UpperCAmelCase						)
																																										SCREAMING_SNAKE_CASE__       :      Tuple     =						model(_UpperCAmelCase,			**_UpperCAmelCase						)[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
																												SCREAMING_SNAKE_CASE__       :      List[Any]     =						self._prepare_for_class(inputs_dict.copy(),			_UpperCAmelCase,			return_labels=_UpperCAmelCase						)
																												SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						model(_UpperCAmelCase						)[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
																												SCREAMING_SNAKE_CASE__       :      int     =						self._prepare_for_class(inputs_dict.copy(),			_UpperCAmelCase,			return_labels=_UpperCAmelCase						)

																												# Get keys that were added with the _prepare_for_class function
																												SCREAMING_SNAKE_CASE__       :      List[Any]     =						prepared_for_class.keys() - inputs_dict.keys()
																												SCREAMING_SNAKE_CASE__       :      List[Any]     =						inspect.signature(model.call						).parameters
																												SCREAMING_SNAKE_CASE__       :      Dict     =						list(signature.keys()						)

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

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

																												for index, value in sorted_tuple_index_mapping:
																																			SCREAMING_SNAKE_CASE__       :      int     =						prepared_for_class[value]

																												SCREAMING_SNAKE_CASE__       :      List[Any]     =						tuple(_UpperCAmelCase						)

																												# Send to model
																												SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						model(tuple_input[:-1]						)[0]

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






							def  A_     ( self     :					Dict						)				->			int:





														"""simple docstring"""
														(
														    (
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,
														)       :      Union[str, Any]     =						self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase						)






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





														"""simple docstring"""
														(
														    (
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,
														)       :      Tuple     =						self.model_tester.prepare_config_and_inputs()
														for type in ["absolute", "relative_key", "relative_key_query"]:
																					SCREAMING_SNAKE_CASE__       :      Optional[int]     =						type
																					self.model_tester.create_and_check_model(_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase						)






							def  A_     ( self     :					Dict						)				->			Optional[Any]:





														"""simple docstring"""
														(
														    (
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,
														)       :      List[Any]     =						self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_sequence_classification(
														    _UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase						)






							def  A_     ( self     :					Dict						)				->			str:





														"""simple docstring"""
														(
														    (
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,
														)       :      Any     =						self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_token_classification(
														    _UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase						)






							def  A_     ( self     :					Any						)				->			int:





														"""simple docstring"""
														(
														    (
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,(
														    SCREAMING_SNAKE_CASE__
														)   ,
														)       :      str     =						self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_question_answering(
														    _UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase						)






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





														"""simple docstring"""
														for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					SCREAMING_SNAKE_CASE__       :      List[Any]     =						TFLayoutLMvaModel.from_pretrained(_UpperCAmelCase						)
																					self.assertIsNotNone(_UpperCAmelCase						)




def _a (  )	->    List[str]:

							'''simple docstring'''






							SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"       )
							return image







@require_tf
class 						lowerCamelCase       (unittest.TestCase      ):







							"""simple docstring"""




							@cached_property
							def  A_     ( self     :					Tuple						)				->			List[Any]:





														"""simple docstring"""
														return LayoutLMvaImageProcessor(apply_ocr=_UpperCAmelCase						) if is_vision_available() else None






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





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

														SCREAMING_SNAKE_CASE__       :      Optional[int]     =						self.default_image_processor
														SCREAMING_SNAKE_CASE__       :      Dict     =						prepare_img()
														SCREAMING_SNAKE_CASE__       :      List[str]     =						image_processor(images=_UpperCAmelCase,			return_tensors="tf"						).pixel_values

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

														# forward pass
														SCREAMING_SNAKE_CASE__       :      Dict     =						model(input_ids=_UpperCAmelCase,			bbox=_UpperCAmelCase,			pixel_values=_UpperCAmelCase,			training=_UpperCAmelCase						)

														# verify the logits
														SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						(1, 1_9_9, 7_6_8)
														self.assertEqual(outputs.last_hidden_state.shape,			_UpperCAmelCase						)

														SCREAMING_SNAKE_CASE__       :      Optional[int]     =						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],			_UpperCAmelCase,			atol=1E-4						)						)






