





from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import logging


if TYPE_CHECKING:
						from ...processing_utils import ProcessorMixin
						from ...utils import TensorType


_SCREAMING_SNAKE_CASE							=						logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE							=						{
    """microsoft/layoutlmv3-base""": """https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json""",
}
class SCREAMING_SNAKE_CASE_  (    __lowercase    ):
						__magic_name__:	str       					=				"layoutlmv3"






						def __init__(					self					:					int						,      _A					:					str=50265						,      _A					:					List[str]=768						,      _A					:					Optional[Any]=12						,      _A					:					Dict=12						,      _A					:					List[str]=3072						,      _A					:					Dict="gelu"						,      _A					:					str=0.1						,      _A					:					Optional[Any]=0.1						,      _A					:					Any=512						,      _A					:					Optional[Any]=2						,      _A					:					Tuple=0.0_2						,      _A					:					Optional[Any]=1E-5						,      _A					:					List[str]=1						,      _A					:					List[Any]=0						,      _A					:					Optional[int]=2						,      _A					:					Optional[int]=1024						,      _A					:					List[str]=128						,      _A					:					Union[str, Any]=128						,      _A					:					int=True						,      _A					:					int=32						,      _A					:					int=128						,      _A					:					Optional[Any]=64						,      _A					:					Optional[Any]=256						,      _A					:					List[Any]=True						,      _A					:					List[Any]=True						,      _A					:					Optional[int]=True						,      _A					:					Optional[int]=224						,      _A					:					Dict=3						,      _A					:					Union[str, Any]=16						,      _A					:					Dict=None						,      **_A					:					List[Any]						,      )							->			List[str]:







							"""simple docstring"""


							super().__init__(
							    vocab_size=_a						,      hidden_size=_a						,      num_hidden_layers=_a						,      num_attention_heads=_a						,      intermediate_size=_a						,      hidden_act=_a						,      hidden_dropout_prob=_a						,      attention_probs_dropout_prob=_a						,      max_position_embeddings=_a						,      type_vocab_size=_a						,      initializer_range=_a						,      layer_norm_eps=_a						,      pad_token_id=_a						,      bos_token_id=_a						,      eos_token_id=_a						,      **_a						,      )
							snake_case_						:	List[Any]      =		max_ad_position_embeddings
							snake_case_						:	Tuple      =		coordinate_size
							snake_case_						:	Union[str, Any]      =		shape_size
							snake_case_						:	List[str]      =		has_relative_attention_bias
							snake_case_						:	Tuple      =		rel_pos_bins
							snake_case_						:	str      =		max_rel_pos
							snake_case_						:	List[Any]      =		has_spatial_attention_bias
							snake_case_						:	List[Any]      =		rel_ad_pos_bins
							snake_case_						:	Optional[Any]      =		max_rel_ad_pos
							snake_case_						:	str      =		text_embed
							snake_case_						:	str      =		visual_embed
							snake_case_						:	Optional[Any]      =		input_size
							snake_case_						:	Dict      =		num_channels
							snake_case_						:	List[str]      =		patch_size
							snake_case_						:	List[str]      =		classifier_dropout
class SCREAMING_SNAKE_CASE_  (    __lowercase    ):
						__magic_name__:	Dict       					=				version.parse("1.12"    )






						@property
						def UpperCAmelCase_ (					self					:					Optional[int]		)							->			Mapping[str, Mapping[int, str]]:







							"""simple docstring"""


							if self.task in ["question-answering", "sequence-classification"]:
								return OrderedDict(
								    [
								        ('input_ids', {0: 'batch', 1: 'sequence'}),
								        ('attention_mask', {0: 'batch', 1: 'sequence'}),
								        ('bbox', {0: 'batch', 1: 'sequence'}),
								        ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
								    ]		)
							else:
								return OrderedDict(
								    [
								        ('input_ids', {0: 'batch', 1: 'sequence'}),
								        ('bbox', {0: 'batch', 1: 'sequence'}),
								        ('attention_mask', {0: 'batch', 1: 'sequence'}),
								        ('pixel_values', {0: 'batch', 1: 'num_channels'}),
								    ]		)






						@property
						def UpperCAmelCase_ (					self					:					Optional[Any]		)							->			float:







							"""simple docstring"""


							return 1E-5






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







							"""simple docstring"""


							return 12






						def UpperCAmelCase_ (					self					:					int						,      _A					:					List[Any]						,      _A					:					Tuple = -1						,      _A					:					Union[str, Any] = -1						,      _A					:					Optional[int] = False						,      _A					:					Optional[Any] = None						,      _A					:					Optional[Any] = 3						,      _A					:					List[Any] = 40						,      _A					:					int = 40						,      )							->			Mapping[str, Any]:







							"""simple docstring"""


							setattr(processor.image_processor						,      'apply_ocr'						,      _a		)

							# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
							snake_case_						:	Union[str, Any]      =		compute_effective_axis_dimension(
							    _a						,      fixed_dimension=OnnxConfig.default_fixed_batch						,      num_token_to_add=0		)
							# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
							snake_case_						:	Tuple      =		processor.tokenizer.num_special_tokens_to_add(_a		)
							snake_case_						:	Optional[Any]      =		compute_effective_axis_dimension(
							    _a						,      fixed_dimension=OnnxConfig.default_fixed_sequence						,      num_token_to_add=_a		)
							# Generate dummy inputs according to compute batch and sequence
							snake_case_						:	Any      =		[[''' '''.join([processor.tokenizer.unk_token]		) * seq_length]] * batch_size

							# Generate dummy bounding boxes
							snake_case_						:	Optional[int]      =		[[[48, 84, 73, 128]]] * batch_size

							# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
							# batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch)
							snake_case_						:	Tuple      =		self._generate_dummy_images(_a						,      _a						,      _a						,      _a		)

							snake_case_						:	Union[str, Any]      =		dict(
							    processor(
							        _a						,      text=_a						,      boxes=_a						,      return_tensors=_a						,      )		)

							return inputs




import unittest

from transformers import EsmConfig, is_torch_available
from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers.models.esm.modeling_esmfold import EsmForProteinFolding

class 				UpperCAmelCase_		:
							"""simple docstring"""







							def __init__(			self       ,							_a       ,							_a=1_3       ,							_a=7       ,							_a=False       ,							_a=True       ,							_a=False       ,							_a=False       ,							_a=1_9       ,							_a=3_2       ,							_a=5       ,							_a=4       ,							_a=3_7       ,							_a="gelu"       ,							_a=0.1       ,							_a=0.1       ,							_a=5_1_2       ,							_a=1_6       ,							_a=2       ,							_a=0.02       ,							_a=3       ,							_a=4       ,							_a=None       ,							)    ->    Union[str, Any]:
												_a	:    Optional[Any]         =		parent
												_a	:    Union[str, Any]         =		batch_size
												_a	:    List[Any]         =		seq_length
												_a	:    Dict         =		is_training
												_a	:    int         =		use_input_mask
												_a	:    str         =		use_token_type_ids
												_a	:    Any         =		use_labels
												_a	:    List[Any]         =		vocab_size
												_a	:    Any         =		hidden_size
												_a	:    int         =		num_hidden_layers
												_a	:    str         =		num_attention_heads
												_a	:    Dict         =		intermediate_size
												_a	:    List[str]         =		hidden_act
												_a	:    Optional[Any]         =		hidden_dropout_prob
												_a	:    Optional[Any]         =		attention_probs_dropout_prob
												_a	:    int         =		max_position_embeddings
												_a	:    Tuple         =		type_vocab_size
												_a	:    str         =		type_sequence_label_size
												_a	:    Any         =		initializer_range
												_a	:    Union[str, Any]         =		num_labels
												_a	:    Dict         =		num_choices
												_a	:    Union[str, Any]         =		scope


							def 				__lowercase				(			self       )    ->    List[Any]:
												_a	:    str         =		ids_tensor([self.batch_size, self.seq_length]       ,							self.vocab_size       )

												_a	:    Dict         =		None
												if self.use_input_mask:
																	_a	:    int         =		random_attention_mask([self.batch_size, self.seq_length]       )

												_a	:    List[Any]         =		None
												_a	:    Tuple         =		None
												_a	:    Any         =		None
												if self.use_labels:
																	_a	:    Tuple         =		ids_tensor([self.batch_size]       ,							self.type_sequence_label_size       )
																	_a	:    Union[str, Any]         =		ids_tensor([self.batch_size, self.seq_length]       ,							self.num_labels       )
																	_a	:    List[Any]         =		ids_tensor([self.batch_size]       ,							self.num_choices       )

												_a	:    str         =		self.get_config()

												return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels


							def 				__lowercase				(			self       )    ->    str:
												_a	:    Optional[int]         =		EsmConfig(
												    vocab_size=3_3       ,							hidden_size=self.hidden_size       ,							pad_token_id=1       ,							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       ,							is_folding_model=_a       ,							esmfold_config={'''trunk''': {'''num_blocks''': 2}, '''fp16_esm''': False}       ,							)
												return config


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       )    ->    str:
												_a	:    Union[str, Any]         =		EsmForProteinFolding(config=_a       ).float()
												model.to(_a       )
												model.eval()
												_a	:    str         =		model(_a       ,							attention_mask=_a       )
												_a	:    Union[str, Any]         =		model(_a       )
												_a	:    Optional[int]         =		model(_a       )

												self.parent.assertEqual(result.positions.shape       ,							(8, self.batch_size, self.seq_length, 1_4, 3)       )
												self.parent.assertEqual(result.angles.shape       ,							(8, self.batch_size, self.seq_length, 7, 2)       )




							def 				__lowercase				(			self       )    ->    str:
												_a	:    List[str]         =		self.prepare_config_and_inputs()
												(
												    (
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					
												)	:    Optional[Any]         =		config_and_inputs
												_a	:    List[Any]         =		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
												return config, inputs_dict



@require_torch
class 				UpperCAmelCase_		(			__lowercase				,       __lowercase				,       unittest.TestCase   ):
							"""simple docstring"""







							UpperCAmelCase__       :						Any	    =    False

							UpperCAmelCase__       :						Any	    =    (EsmForProteinFolding,) if is_torch_available() else ()
							UpperCAmelCase__       :						Union[str, Any]	    =    ()
							UpperCAmelCase__       :						int	    =    {} if is_torch_available() else {}
							UpperCAmelCase__       :						Optional[int]	    =    False


							def 				__lowercase				(			self       )    ->    List[Any]:
												_a	:    Optional[int]         =		EsmFoldModelTester(self       )
												_a	:    Dict         =		ConfigTester(self       ,							config_class=_a       ,							hidden_size=3_7       )


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


							def 				__lowercase				(			self       )    ->    str:
												_a	:    str         =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*_a       )


							@unittest.skip('''Does not support attention outputs'''       )
							def 				__lowercase				(			self       )    ->    int:
												pass


							@unittest.skip
							def 				__lowercase				(			self       )    ->    List[str]:
												pass


							@unittest.skip('''Esm does not support embedding resizing'''       )
							def 				__lowercase				(			self       )    ->    int:
												pass


							@unittest.skip('''Esm does not support embedding resizing'''       )
							def 				__lowercase				(			self       )    ->    Optional[Any]:
												pass


							@unittest.skip('''ESMFold does not support passing input embeds!'''       )
							def 				__lowercase				(			self       )    ->    int:
												pass


							@unittest.skip('''ESMFold does not support head pruning.'''       )
							def 				__lowercase				(			self       )    ->    str:
												pass


							@unittest.skip('''ESMFold does not support head pruning.'''       )
							def 				__lowercase				(			self       )    ->    Optional[Any]:
												pass


							@unittest.skip('''ESMFold does not support head pruning.'''       )
							def 				__lowercase				(			self       )    ->    Optional[Any]:
												pass


							@unittest.skip('''ESMFold does not support head pruning.'''       )
							def 				__lowercase				(			self       )    ->    Any:
												pass


							@unittest.skip('''ESMFold does not support head pruning.'''       )
							def 				__lowercase				(			self       )    ->    Tuple:
												pass


							@unittest.skip('''ESMFold does not output hidden states in the normal way.'''       )
							def 				__lowercase				(			self       )    ->    Tuple:
												pass


							@unittest.skip('''ESMfold does not output hidden states in the normal way.'''       )
							def 				__lowercase				(			self       )    ->    Tuple:
												pass


							@unittest.skip('''ESMFold only has one output format.'''       )
							def 				__lowercase				(			self       )    ->    Tuple:
												pass


							@unittest.skip('''This test doesn\'t work for ESMFold and doesn\'t test core functionality'''       )
							def 				__lowercase				(			self       )    ->    Dict:
												pass


							@unittest.skip('''ESMFold does not support input chunking.'''       )
							def 				__lowercase				(			self       )    ->    Tuple:
												pass


							@unittest.skip('''ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.'''       )
							def 				__lowercase				(			self       )    ->    Optional[Any]:
												pass


							@unittest.skip('''ESMFold doesn\'t support torchscript compilation.'''       )
							def 				__lowercase				(			self       )    ->    List[str]:
												pass


							@unittest.skip('''ESMFold doesn\'t support torchscript compilation.'''       )
							def 				__lowercase				(			self       )    ->    List[str]:
												pass


							@unittest.skip('''ESMFold doesn\'t support torchscript compilation.'''       )
							def 				__lowercase				(			self       )    ->    List[Any]:
												pass


							@unittest.skip('''ESMFold doesn\'t support data parallel.'''       )
							def 				__lowercase				(			self       )    ->    Union[str, Any]:
												pass




							@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''       )
							def 				__lowercase				(			self       )    ->    Optional[Any]:
												pass



@require_torch
class 				UpperCAmelCase_		(			__lowercase   ):
							"""simple docstring"""







							@slow
							def 				__lowercase				(			self       )    ->    Optional[int]:
												_a	:    Dict         =		EsmForProteinFolding.from_pretrained('''facebook/esmfold_v1'''       ).float()
												model.eval()
												_a	:    Tuple         =		torch.tensor([[0, 6, 4, 1_3, 5, 4, 1_6, 1_2, 1_1, 7, 2]]       )
												_a	:    Optional[Any]         =		model(_a       )['''positions''']
												_a	:    Union[str, Any]         =		torch.tensor([2.5828, 0.7993, -10.9334]       ,							dtype=torch.floataa       )
												self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0]       ,							_a       ,							atol=1e-4       )       )



