"""simple docstring"""






from dataclasses import dataclass
from typing import Optional

import numpy as np
import torch
import torch.nn as nn

from ..utils import BaseOutput, is_torch_version, randn_tensor
from .attention_processor import SpatialNorm
from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block




@dataclass
class 				__lowercase			(			__SCREAMING_SNAKE_CASE							):







					'''simple docstring'''




					__lowerCAmelCase  =  42






class 				__lowercase			(			nn.Module							):







					'''simple docstring'''




					def __init__(						self	,			_UpperCAmelCase=3	,			_UpperCAmelCase=3	,			_UpperCAmelCase=("DownEncoderBlock2D",)	,			_UpperCAmelCase=(64,)	,			_UpperCAmelCase=2	,			_UpperCAmelCase=32	,			_UpperCAmelCase="silu"	,			_UpperCAmelCase=True	,			):
									super().__init__()
									__a :						Optional[Any]			       =  layers_per_block

									__a :						List[str]			       =  torch.nn.Convad(
									    __snake_case	,			block_out_channels[0]	,			kernel_size=3	,			stride=1	,			padding=1	,			)

									__a :						Optional[int]			       =  None
									__a :						str			       =  nn.ModuleList([]     )

									# down
									__a :						Tuple			       =  block_out_channels[0]
									for i, down_block_type in enumerate(__snake_case     ):
													__a :						Union[str, Any]			       =  output_channel
													__a :						Tuple			       =  block_out_channels[i]
													__a :						Dict			       =  i == len(__snake_case     ) - 1

													__a :						int			       =  get_down_block(
													    __snake_case	,			num_layers=self.layers_per_block	,			in_channels=__snake_case	,			out_channels=__snake_case	,			add_downsample=not is_final_block	,			resnet_eps=1e-6	,			downsample_padding=0	,			resnet_act_fn=__snake_case	,			resnet_groups=__snake_case	,			attention_head_dim=__snake_case	,			temb_channels=__snake_case	,			)
													self.down_blocks.append(__snake_case     )

									# mid
									__a :						List[Any]			       =  UNetMidBlockaD(
									    in_channels=block_out_channels[-1]	,			resnet_eps=1e-6	,			resnet_act_fn=__snake_case	,			output_scale_factor=1	,			resnet_time_scale_shift='''default'''	,			attention_head_dim=block_out_channels[-1]	,			resnet_groups=__snake_case	,			temb_channels=__snake_case	,			)

									# out
									__a :						Optional[int]			       =  nn.GroupNorm(num_channels=block_out_channels[-1]	,			num_groups=__snake_case	,			eps=1e-6     )
									__a :						Union[str, Any]			       =  nn.SiLU()

									__a :						Any			       =  2 * out_channels if double_z else out_channels
									__a :						Optional[int]			       =  nn.Convad(block_out_channels[-1]	,			__snake_case	,			3	,			padding=1     )

									__a :						str			       =  False







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase     ):
									__a :						Optional[int]			       =  x
									__a :						Optional[Any]			       =  self.conv_in(__snake_case     )

									if self.training and self.gradient_checkpointing:

													def create_custom_forward(_UpperCAmelCase     ):
																	def custom_forward(*_UpperCAmelCase     ):
																					return module(*__snake_case     )

																	return custom_forward

													# down
													if is_torch_version('''>='''	,			'''1.11.0'''     ):
																	for down_block in self.down_blocks:
																					__a :						List[str]			       =  torch.utils.checkpoint.checkpoint(
																					    create_custom_forward(__snake_case     )	,			__snake_case	,			use_reentrant=__snake_case     )
																	# middle
																	__a :						int			       =  torch.utils.checkpoint.checkpoint(
																	    create_custom_forward(self.mid_block     )	,			__snake_case	,			use_reentrant=__snake_case     )
													else:
																	for down_block in self.down_blocks:
																					__a :						Optional[int]			       =  torch.utils.checkpoint.checkpoint(create_custom_forward(__snake_case     )	,			__snake_case     )
																	# middle
																	__a :						Tuple			       =  torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block     )	,			__snake_case     )

									else:
													# down
													for down_block in self.down_blocks:
																	__a :						Optional[Any]			       =  down_block(__snake_case     )

													# middle
													__a :						List[str]			       =  self.mid_block(__snake_case     )

									# post-process
									__a :						Tuple			       =  self.conv_norm_out(__snake_case     )
									__a :						List[str]			       =  self.conv_act(__snake_case     )
									__a :						Tuple			       =  self.conv_out(__snake_case     )

									return sample






class 				__lowercase			(			nn.Module							):







					'''simple docstring'''




					def __init__(						self	,			_UpperCAmelCase=3	,			_UpperCAmelCase=3	,			_UpperCAmelCase=("UpDecoderBlock2D",)	,			_UpperCAmelCase=(64,)	,			_UpperCAmelCase=2	,			_UpperCAmelCase=32	,			_UpperCAmelCase="silu"	,			_UpperCAmelCase="group"	,			):
									super().__init__()
									__a :						str			       =  layers_per_block

									__a :						Tuple			       =  nn.Convad(
									    __snake_case	,			block_out_channels[-1]	,			kernel_size=3	,			stride=1	,			padding=1	,			)

									__a :						Union[str, Any]			       =  None
									__a :						Tuple			       =  nn.ModuleList([]     )

									__a :						str			       =  in_channels if norm_type == '''spatial''' else None

									# mid
									__a :						str			       =  UNetMidBlockaD(
									    in_channels=block_out_channels[-1]	,			resnet_eps=1e-6	,			resnet_act_fn=__snake_case	,			output_scale_factor=1	,			resnet_time_scale_shift='''default''' if norm_type == '''group''' else norm_type	,			attention_head_dim=block_out_channels[-1]	,			resnet_groups=__snake_case	,			temb_channels=__snake_case	,			)

									# up
									__a :						Union[str, Any]			       =  list(reversed(__snake_case     )     )
									__a :						Optional[Any]			       =  reversed_block_out_channels[0]
									for i, up_block_type in enumerate(__snake_case     ):
													__a :						Optional[Any]			       =  output_channel
													__a :						str			       =  reversed_block_out_channels[i]

													__a :						int			       =  i == len(__snake_case     ) - 1

													__a :						List[Any]			       =  get_up_block(
													    __snake_case	,			num_layers=self.layers_per_block + 1	,			in_channels=__snake_case	,			out_channels=__snake_case	,			prev_output_channel=__snake_case	,			add_upsample=not is_final_block	,			resnet_eps=1e-6	,			resnet_act_fn=__snake_case	,			resnet_groups=__snake_case	,			attention_head_dim=__snake_case	,			temb_channels=__snake_case	,			resnet_time_scale_shift=__snake_case	,			)
													self.up_blocks.append(__snake_case     )
													__a :						Optional[Any]			       =  output_channel

									# out
									if norm_type == "spatial":
													__a :						Union[str, Any]			       =  SpatialNorm(block_out_channels[0]	,			__snake_case     )
									else:
													__a :						str			       =  nn.GroupNorm(num_channels=block_out_channels[0]	,			num_groups=__snake_case	,			eps=1e-6     )
									__a :						Any			       =  nn.SiLU()
									__a :						Optional[int]			       =  nn.Convad(block_out_channels[0]	,			__snake_case	,			3	,			padding=1     )

									__a :						str			       =  False







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase=None     ):
									__a :						str			       =  z
									__a :						List[Any]			       =  self.conv_in(__snake_case     )

									__a :						int			       =  next(iter(self.up_blocks.parameters()     )     ).dtype
									if self.training and self.gradient_checkpointing:

													def create_custom_forward(_UpperCAmelCase     ):
																	def custom_forward(*_UpperCAmelCase     ):
																					return module(*__snake_case     )

																	return custom_forward

													if is_torch_version('''>='''	,			'''1.11.0'''     ):
																	# middle
																	__a :						int			       =  torch.utils.checkpoint.checkpoint(
																	    create_custom_forward(self.mid_block     )	,			__snake_case	,			__snake_case	,			use_reentrant=__snake_case     )
																	__a :						Optional[int]			       =  sample.to(__snake_case     )

																	# up
																	for up_block in self.up_blocks:
																					__a :						List[Any]			       =  torch.utils.checkpoint.checkpoint(
																					    create_custom_forward(__snake_case     )	,			__snake_case	,			__snake_case	,			use_reentrant=__snake_case     )
													else:
																	# middle
																	__a :						str			       =  torch.utils.checkpoint.checkpoint(
																	    create_custom_forward(self.mid_block     )	,			__snake_case	,			__snake_case     )
																	__a :						Tuple			       =  sample.to(__snake_case     )

																	# up
																	for up_block in self.up_blocks:
																					__a :						str			       =  torch.utils.checkpoint.checkpoint(create_custom_forward(__snake_case     )	,			__snake_case	,			__snake_case     )
									else:
													# middle
													__a :						str			       =  self.mid_block(__snake_case	,			__snake_case     )
													__a :						Optional[int]			       =  sample.to(__snake_case     )

													# up
													for up_block in self.up_blocks:
																	__a :						Optional[Any]			       =  up_block(__snake_case	,			__snake_case     )

        # post-process
									if latent_embeds is None:
													__a :						Optional[int]			       =  self.conv_norm_out(__snake_case     )
									else:
													__a :						List[Any]			       =  self.conv_norm_out(__snake_case	,			__snake_case     )
									__a :						Tuple			       =  self.conv_act(__snake_case     )
									__a :						Union[str, Any]			       =  self.conv_out(__snake_case     )

									return sample






class 				__lowercase			(			nn.Module							):







					'''simple docstring'''




					def __init__(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase=None	,			_UpperCAmelCase="random"	,			_UpperCAmelCase=False	,			_UpperCAmelCase=True     ):
									super().__init__()
									__a :						List[str]			       =  n_e
									__a :						Dict			       =  vq_embed_dim
									__a :						int			       =  beta
									__a :						Optional[int]			       =  legacy

									__a :						Tuple			       =  nn.Embedding(self.n_e	,			self.vq_embed_dim     )
									self.embedding.weight.data.uniform_(-1.0 / self.n_e	,			1.0 / self.n_e     )

									__a :						Dict			       =  remap
									if self.remap is not None:
													self.register_buffer('''used'''	,			torch.tensor(np.load(self.remap     )     )     )
													__a :						Tuple			       =  self.used.shape[0]
													__a :						Tuple			       =  unknown_index  # "random" or "extra" or integer
													if self.unknown_index == "extra":
																	__a :						List[Any]			       =  self.re_embed
																	__a :						str			       =  self.re_embed + 1
													print(
													    f"""Remapping {self.n_e} indices to {self.re_embed} indices. """
													    f"""Using {self.unknown_index} for unknown indices."""     )
									else:
													__a :						Dict			       =  n_e

									__a :						Optional[Any]			       =  sane_index_shape







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase     ):
									__a :						Dict			       =  inds.shape
									assert len(__snake_case     ) > 1
									__a :						List[str]			       =  inds.reshape(ishape[0]	,			-1     )
									__a :						List[Any]			       =  self.used.to(__snake_case     )
									__a :						Any			       =  (inds[:, :, None] == used[None, None, ...]).long()
									__a :						int			       =  match.argmax(-1     )
									__a :						Any			       =  match.sum(2     ) < 1
									if self.unknown_index == "random":
													__a :						Optional[int]			       =  torch.randint(0	,			self.re_embed	,			size=new[unknown].shape     ).to(device=new.device     )
									else:
													__a :						List[Any]			       =  self.unknown_index
									return new.reshape(__snake_case     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase     ):
									__a :						Tuple			       =  inds.shape
									assert len(__snake_case     ) > 1
									__a :						Optional[Any]			       =  inds.reshape(ishape[0]	,			-1     )
									__a :						List[Any]			       =  self.used.to(__snake_case     )
									if self.re_embed > self.used.shape[0]:  # extra token
													__a :						Union[str, Any]			       =  0  # simply set to zero
									__a :						Union[str, Any]			       =  torch.gather(used[None, :][inds.shape[0] * [0], :]	,			1	,			__snake_case     )
									return back.reshape(__snake_case     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase     ):
									# reshape z -> (batch, height, width, channel) and flatten
									__a :						Any			       =  z.permute(0	,			2	,			3	,			1     ).contiguous()
									__a :						Optional[int]			       =  z.view(-1	,			self.vq_embed_dim     )

									# distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z
									__a :						int			       =  torch.argmin(torch.cdist(__snake_case	,			self.embedding.weight     )	,			dim=1     )

									__a :						List[str]			       =  self.embedding(__snake_case     ).view(z.shape     )
									__a :						Optional[int]			       =  None
									__a :						Tuple			       =  None

									# compute loss for embedding
									if not self.legacy:
													__a :						Optional[Any]			       =  self.beta * torch.mean((z_q.detach() - z) ** 2     ) + torch.mean((z_q - z.detach()) ** 2     )
									else:
													__a :						Tuple			       =  torch.mean((z_q.detach() - z) ** 2     ) + self.beta * torch.mean((z_q - z.detach()) ** 2     )

									# preserve gradients
									__a :						List[Any]			       =  z + (z_q - z).detach()

									# reshape back to match original input shape
									__a :						Optional[int]			       =  z_q.permute(0	,			3	,			1	,			2     ).contiguous()

									if self.remap is not None:
													__a :						Dict			       =  min_encoding_indices.reshape(z.shape[0]	,			-1     )  # add batch axis
													__a :						int			       =  self.remap_to_used(__snake_case     )
													__a :						Union[str, Any]			       =  min_encoding_indices.reshape(-1	,			1     )  # flatten

									if self.sane_index_shape:
													__a :						Tuple			       =  min_encoding_indices.reshape(z_q.shape[0]	,			z_q.shape[2]	,			z_q.shape[3]     )

									return z_q, loss, (perplexity, min_encodings, min_encoding_indices)







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									# shape specifying (batch, height, width, channel)
									if self.remap is not None:
													__a :						Optional[int]			       =  indices.reshape(shape[0]	,			-1     )  # add batch axis
													__a :						Tuple			       =  self.unmap_to_all(__snake_case     )
													__a :						List[Any]			       =  indices.reshape(-1     )  # flatten again

									# get quantized latent vectors
									__a :						Union[str, Any]			       =  self.embedding(__snake_case     )

									if shape is not None:
													__a :						Optional[Any]			       =  z_q.view(__snake_case     )
													# reshape back to match original input shape
													__a :						Optional[int]			       =  z_q.permute(0	,			3	,			1	,			2     ).contiguous()

									return z_q






class 				__lowercase			(			__SCREAMING_SNAKE_CASE							):







					'''simple docstring'''




					def __init__(						self	,			_UpperCAmelCase	,			_UpperCAmelCase=False     ):
									__a :						Any			       =  parameters
									__a :						List[str]			       =  torch.chunk(__snake_case	,			2	,			dim=1     )
									__a :						List[Any]			       =  torch.clamp(self.logvar	,			-30.0	,			20.0     )
									__a :						str			       =  deterministic
									__a :						Dict			       =  torch.exp(0.5 * self.logvar     )
									__a :						Optional[int]			       =  torch.exp(self.logvar     )
									if self.deterministic:
													__a :						Optional[int]			       =  torch.zeros_like(
													    self.mean	,			device=self.parameters.device	,			dtype=self.parameters.dtype     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase = None     ):
									# make sure sample is on the same device as the parameters and has same dtype
									__a :						Tuple			       =  randn_tensor(
									    self.mean.shape	,			generator=__snake_case	,			device=self.parameters.device	,			dtype=self.parameters.dtype     )
									__a :						str			       =  self.mean + self.std * sample
									return x







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase=None     ):
									if self.deterministic:
													return torch.Tensor([0.0]     )
									else:
													if other is None:
																	return 0.5 * torch.sum(torch.pow(self.mean	,			2     ) + self.var - 1.0 - self.logvar	,			dim=[1, 2, 3]     )
													else:
																	return 0.5 * torch.sum(
																	    torch.pow(self.mean - other.mean	,			2     ) / other.var
																	    + self.var / other.var
																	    - 1.0
																	    - self.logvar
																	    + other.logvar	,			dim=[1, 2, 3]	,			)







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase=[1, 2, 3]     ):
									if self.deterministic:
													return torch.Tensor([0.0]     )
									__a :						List[str]			       =  np.log(2.0 * np.pi     )
									return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean	,			2     ) / self.var	,			dim=__snake_case     )







					def  _lowerCamelCase		(						self     ):
									return self.mean






"""simple docstring"""






import copy
import unittest

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


if is_torch_available():
		import torch

		from transformers import (
		    MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
		    MODEL_FOR_QUESTION_ANSWERING_MAPPING,
		    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
		    MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
		    LayoutLMvaConfig,
		    LayoutLMvaForQuestionAnswering,
		    LayoutLMvaForSequenceClassification,
		    LayoutLMvaForTokenClassification,
		    LayoutLMvaModel,
		)
		from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
		from PIL import Image

		from transformers import LayoutLMvaImageProcessor




class 				__lowercase			:







					'''simple docstring'''




					def __init__(						self	,			_UpperCAmelCase	,			_UpperCAmelCase=2	,			_UpperCAmelCase=3	,			_UpperCAmelCase=4	,			_UpperCAmelCase=2	,			_UpperCAmelCase=7	,			_UpperCAmelCase=True	,			_UpperCAmelCase=True	,			_UpperCAmelCase=True	,			_UpperCAmelCase=True	,			_UpperCAmelCase=99	,			_UpperCAmelCase=36	,			_UpperCAmelCase=3	,			_UpperCAmelCase=4	,			_UpperCAmelCase=37	,			_UpperCAmelCase="gelu"	,			_UpperCAmelCase=0.1	,			_UpperCAmelCase=0.1	,			_UpperCAmelCase=512	,			_UpperCAmelCase=16	,			_UpperCAmelCase=2	,			_UpperCAmelCase=0.0_2	,			_UpperCAmelCase=6	,			_UpperCAmelCase=6	,			_UpperCAmelCase=3	,			_UpperCAmelCase=4	,			_UpperCAmelCase=None	,			_UpperCAmelCase=1000	,			):
									__a :						Dict			       =  parent
									__a :						Optional[int]			       =  batch_size
									__a :						Optional[int]			       =  num_channels
									__a :						List[Any]			       =  image_size
									__a :						int			       =  patch_size
									__a :						Tuple			       =  text_seq_length
									__a :						Dict			       =  is_training
									__a :						str			       =  use_input_mask
									__a :						Optional[int]			       =  use_token_type_ids
									__a :						List[Any]			       =  use_labels
									__a :						Tuple			       =  vocab_size
									__a :						str			       =  hidden_size
									__a :						Any			       =  num_hidden_layers
									__a :						List[str]			       =  num_attention_heads
									__a :						str			       =  intermediate_size
									__a :						int			       =  hidden_act
									__a :						List[Any]			       =  hidden_dropout_prob
									__a :						Tuple			       =  attention_probs_dropout_prob
									__a :						Tuple			       =  max_position_embeddings
									__a :						List[Any]			       =  type_vocab_size
									__a :						int			       =  type_sequence_label_size
									__a :						str			       =  initializer_range
									__a :						Dict			       =  coordinate_size
									__a :						int			       =  shape_size
									__a :						int			       =  num_labels
									__a :						Optional[int]			       =  num_choices
									__a :						Any			       =  scope
									__a :						Any			       =  range_bbox

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







					def  _lowerCamelCase		(						self     ):
									__a :						List[Any]			       =  ids_tensor([self.batch_size, self.text_seq_length]	,			self.vocab_size     )

									__a :						Union[str, Any]			       =  ids_tensor([self.batch_size, self.text_seq_length, 4]	,			self.range_bbox     )
									# 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 :						Optional[Any]			       =  bbox[i, j, 3]
																					__a :						Union[str, Any]			       =  bbox[i, j, 1]
																					__a :						Tuple			       =  t
																	if bbox[i, j, 2] < bbox[i, j, 0]:
																					__a :						Optional[int]			       =  bbox[i, j, 2]
																					__a :						Optional[int]			       =  bbox[i, j, 0]
																					__a :						str			       =  t

									__a :						str			       =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

									__a :						Union[str, Any]			       =  None
									if self.use_input_mask:
													__a :						int			       =  random_attention_mask([self.batch_size, self.text_seq_length]     )

									__a :						int			       =  None
									if self.use_token_type_ids:
													__a :						Union[str, Any]			       =  ids_tensor([self.batch_size, self.text_seq_length]	,			self.type_vocab_size     )

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

									__a :						int			       =  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  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						Union[str, Any]			       =  LayoutLMvaModel(config=_UpperCAmelCase     )
									model.to(_UpperCAmelCase     )
									model.eval()

									# text + image
									__a :						Union[str, Any]			       =  model(_UpperCAmelCase	,			pixel_values=_UpperCAmelCase     )
									__a :						List[Any]			       =  model(
									    _UpperCAmelCase	,			bbox=_UpperCAmelCase	,			pixel_values=_UpperCAmelCase	,			attention_mask=_UpperCAmelCase	,			token_type_ids=_UpperCAmelCase     )
									__a :						Optional[int]			       =  model(_UpperCAmelCase	,			bbox=_UpperCAmelCase	,			pixel_values=_UpperCAmelCase	,			token_type_ids=_UpperCAmelCase     )
									__a :						List[str]			       =  model(_UpperCAmelCase	,			bbox=_UpperCAmelCase	,			pixel_values=_UpperCAmelCase     )

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

									# text only
									__a :						int			       =  model(_UpperCAmelCase     )
									self.parent.assertEqual(
									    result.last_hidden_state.shape	,			(self.batch_size, self.text_seq_length, self.hidden_size)     )

									# image only
									__a :						Tuple			       =  model(pixel_values=_UpperCAmelCase     )
									self.parent.assertEqual(
									    result.last_hidden_state.shape	,			(self.batch_size, self.image_seq_length, self.hidden_size)     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						Optional[Any]			       =  self.num_labels
									__a :						Optional[Any]			       =  LayoutLMvaForSequenceClassification(_UpperCAmelCase     )
									model.to(_UpperCAmelCase     )
									model.eval()
									__a :						Union[str, Any]			       =  model(
									    _UpperCAmelCase	,			bbox=_UpperCAmelCase	,			pixel_values=_UpperCAmelCase	,			attention_mask=_UpperCAmelCase	,			token_type_ids=_UpperCAmelCase	,			labels=_UpperCAmelCase	,			)
									self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.num_labels)     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						List[str]			       =  self.num_labels
									__a :						int			       =  LayoutLMvaForTokenClassification(config=_UpperCAmelCase     )
									model.to(_UpperCAmelCase     )
									model.eval()
									__a :						List[str]			       =  model(
									    _UpperCAmelCase	,			bbox=_UpperCAmelCase	,			pixel_values=_UpperCAmelCase	,			attention_mask=_UpperCAmelCase	,			token_type_ids=_UpperCAmelCase	,			labels=_UpperCAmelCase	,			)
									self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.text_seq_length, self.num_labels)     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						List[Any]			       =  LayoutLMvaForQuestionAnswering(config=_UpperCAmelCase     )
									model.to(_UpperCAmelCase     )
									model.eval()
									__a :						List[str]			       =  model(
									    _UpperCAmelCase	,			bbox=_UpperCAmelCase	,			pixel_values=_UpperCAmelCase	,			attention_mask=_UpperCAmelCase	,			token_type_ids=_UpperCAmelCase	,			start_positions=_UpperCAmelCase	,			end_positions=_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  _lowerCamelCase		(						self     ):
									__a :						Optional[int]			       =  self.prepare_config_and_inputs()
									(
									    (
									    __a
									)       ,						(
									    __a
									)       ,						(
									    __a
									)       ,						(
									    __a
									)       ,						(
									    __a
									)       ,						(
									    __a
									)       ,						(
									    __a
									)       ,						(
									    __a
									)       ,						
									) :						List[Any]			       =  config_and_inputs
									__a :						Optional[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_torch
class 				__lowercase			(			_UpperCamelCase   ,      _UpperCamelCase   ,      unittest.TestCase							):







					'''simple docstring'''




					__lowerCAmelCase  =  False
					__lowerCAmelCase  =  False
					__lowerCAmelCase  =  False

					__lowerCAmelCase  =  (
					    (
					        LayoutLMvaModel,
					        LayoutLMvaForSequenceClassification,
					        LayoutLMvaForTokenClassification,
					        LayoutLMvaForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					__lowerCAmelCase  =  (
					    {'''document-question-answering''': LayoutLMvaForQuestionAnswering, '''feature-extraction''': LayoutLMvaModel}
					    if is_torch_available()
					    else {}
					)







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									# `DocumentQuestionAnsweringPipeline` is expected to work with this model, but it combines the text and visual
									# embedding along the sequence dimension (dim 1), which causes an error during post-processing as `p_mask` has
									# the sequence dimension of the text embedding only.
									# (see the line `embedding_output = torch.cat([embedding_output, visual_embeddings], dim=1)`)
									return True







					def  _lowerCamelCase		(						self     ):
									__a :						str			       =  LayoutLMvaModelTester(self     )
									__a :						int			       =  ConfigTester(self	,			config_class=_UpperCAmelCase	,			hidden_size=37     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase=False     ):
									__a :						int			       =  copy.deepcopy(_UpperCAmelCase     )
									if model_class in get_values(_UpperCAmelCase     ):
													__a :						List[str]			       =  {
													    k: v.unsqueeze(1     ).expand(-1	,			self.model_tester.num_choices	,			-1     ).contiguous()
													    if isinstance(_UpperCAmelCase	,			torch.Tensor     ) and v.ndim > 1
													    else v
													    for k, v in inputs_dict.items()
													}
									if return_labels:
													if model_class in get_values(_UpperCAmelCase     ):
																	__a :						Dict			       =  torch.ones(self.model_tester.batch_size	,			dtype=torch.long	,			device=_UpperCAmelCase     )
													elif model_class in get_values(_UpperCAmelCase     ):
																	__a :						int			       =  torch.zeros(
																	    self.model_tester.batch_size	,			dtype=torch.long	,			device=_UpperCAmelCase     )
																	__a :						Any			       =  torch.zeros(
																	    self.model_tester.batch_size	,			dtype=torch.long	,			device=_UpperCAmelCase     )
													elif model_class in [
													    *get_values(_UpperCAmelCase     ),
													]:
																	__a :						Any			       =  torch.zeros(
																	    self.model_tester.batch_size	,			dtype=torch.long	,			device=_UpperCAmelCase     )
													elif model_class in [
													    *get_values(_UpperCAmelCase     ),
													]:
																	__a :						int			       =  torch.zeros(
																	    (self.model_tester.batch_size, self.model_tester.text_seq_length)	,			dtype=torch.long	,			device=_UpperCAmelCase	,			)

									return inputs_dict







					def  _lowerCamelCase		(						self     ):
									self.config_tester.run_common_tests()







					def  _lowerCamelCase		(						self     ):
									__a :						List[str]			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_UpperCAmelCase     )







					def  _lowerCamelCase		(						self     ):
									__a :						Optional[int]			       =  self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
													__a :						Optional[Any]			       =  type
													self.model_tester.create_and_check_model(*_UpperCAmelCase     )







					def  _lowerCamelCase		(						self     ):
									__a :						Tuple			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase     )







					def  _lowerCamelCase		(						self     ):
									__a :						Tuple			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase     )







					def  _lowerCamelCase		(						self     ):
									__a :						int			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase     )







					@slow
					def  _lowerCamelCase		(						self     ):
									for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__a :						Tuple			       =  LayoutLMvaModel.from_pretrained(_UpperCAmelCase     )
													self.assertIsNotNone(_UpperCAmelCase     )






def     __A      (			)		->		Dict:
				__a :						Tuple			       =  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''')
				return image






@require_torch
class 				__lowercase			(			unittest.TestCase							):







					'''simple docstring'''




					@cached_property
					def  _lowerCamelCase		(						self     ):
									return LayoutLMvaImageProcessor(apply_ocr=_UpperCAmelCase     ) if is_vision_available() else None







					@slow
					def  _lowerCamelCase		(						self     ):
									__a :						List[Any]			       =  LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''     ).to(_UpperCAmelCase     )

									__a :						Dict			       =  self.default_image_processor
									__a :						Tuple			       =  prepare_img()
									__a :						Optional[Any]			       =  image_processor(images=_UpperCAmelCase	,			return_tensors='''pt'''     ).pixel_values.to(_UpperCAmelCase     )

									__a :						Tuple			       =  torch.tensor([[1, 2]]     )
									__a :						Dict			       =  torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]     ).unsqueeze(0     )

									# forward pass
									__a :						Tuple			       =  model(
									    input_ids=input_ids.to(_UpperCAmelCase     )	,			bbox=bbox.to(_UpperCAmelCase     )	,			pixel_values=pixel_values.to(_UpperCAmelCase     )	,			)

									# verify the logits
									__a :						Any			       =  torch.Size((1, 199, 768)     )
									self.assertEqual(outputs.last_hidden_state.shape	,			_UpperCAmelCase     )

									__a :						Union[str, Any]			       =  torch.tensor(
									    [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]]     ).to(_UpperCAmelCase     )

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