





from typing import Optional

import numpy as np
import torch
from torch import nn
from transformers import GPTaConfig, GPTaLMHeadModel
from transformers.modeling_utils import ModuleUtilsMixin

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin




class   UpperCamelCase_       (     UpperCamelCase__				,  UpperCamelCase__				,  UpperCamelCase__    ):
							lowerCamelCase_           =					[R"h\.\d+\.attn\.bias", R"h\.\d+\.attn\.masked_bias"]


							@register_to_config
							def __init__(							self    :Any							,				__A    :int							,				__A    :int							,				__A    :Optional[int] = None							,				__A    :int = 5_0257							,				__A    :int = 1024							,				__A    :int = 768							,				__A    :int = 12							,				__A    :int = 12							,				__A    :Optional[int] = None							,				__A    :str = "gelu_new"							,				__A    :float = 0.1							,				__A    :float = 0.1							,				__A    :float = 0.1							,				__A    :float = 1E-5							,				__A    :float = 0.0_2							,				__A    :bool = True							,				__A    :bool = True							,				__A    :bool = False							,				__A    :bool = False							,				)   ->   List[Any]:



									"""simple docstring"""



									super().__init__()

									SCREAMING_SNAKE_CASE__			=      prefix_length

									if prefix_inner_dim != n_embd and prefix_hidden_dim is None:
											raise ValueError(
											    f'''`prefix_hidden_dim` cannot be `None` when `prefix_inner_dim`: {prefix_hidden_dim} and'''
											    f''' `n_embd`: {n_embd} are not equal.'''		)

									SCREAMING_SNAKE_CASE__			=      prefix_inner_dim
									SCREAMING_SNAKE_CASE__			=      prefix_hidden_dim

									SCREAMING_SNAKE_CASE__			=      (
									    nn.Linear(self.prefix_inner_dim							,				self.prefix_hidden_dim		)
									    if self.prefix_hidden_dim is not None
									    else nn.Identity()
									)
									SCREAMING_SNAKE_CASE__			=      (
									    nn.Linear(self.prefix_hidden_dim							,				__A		) if self.prefix_hidden_dim is not None else nn.Identity()
									)

									SCREAMING_SNAKE_CASE__			=      GPTaConfig(
									    vocab_size=__A							,				n_positions=__A							,				n_embd=__A							,				n_layer=__A							,				n_head=__A							,				n_inner=__A							,				activation_function=__A							,				resid_pdrop=__A							,				embd_pdrop=__A							,				attn_pdrop=__A							,				layer_norm_epsilon=__A							,				initializer_range=__A							,				scale_attn_weights=__A							,				use_cache=__A							,				scale_attn_by_inverse_layer_idx=__A							,				reorder_and_upcast_attn=__A							,				)
									SCREAMING_SNAKE_CASE__			=      GPTaLMHeadModel(__A		)


							def      _snake_case  (							self    :Optional[Any]							,				__A    :torch.Tensor							,				__A    :torch.Tensor							,				__A    :Optional[torch.Tensor] = None							,				__A    :Optional[torch.Tensor] = None							,				)   ->   Tuple:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.transformer.transformer.wte(__A		)
									SCREAMING_SNAKE_CASE__			=      self.encode_prefix(__A		)
									SCREAMING_SNAKE_CASE__			=      self.decode_prefix(__A		)
									SCREAMING_SNAKE_CASE__			=      torch.cat((prefix_embeds, embedding_text)							,				dim=1		)

									if labels is not None:
											SCREAMING_SNAKE_CASE__			=      self.get_dummy_token(input_ids.shape[0]							,				input_ids.device		)
											SCREAMING_SNAKE_CASE__			=      torch.cat((dummy_token, input_ids)							,				dim=1		)
									SCREAMING_SNAKE_CASE__			=      self.transformer(inputs_embeds=__A							,				labels=__A							,				attention_mask=__A		)
									if self.prefix_hidden_dim is not None:
											return out, hidden
									else:
											return out


							def      _snake_case  (							self    :Optional[int]							,				__A    :int							,				__A    :torch.device		)   ->   torch.Tensor:



									"""simple docstring"""



									return torch.zeros(__A							,				self.prefix_length							,				dtype=torch.intaa							,				device=__A		)


							def      _snake_case  (							self    :Optional[Any]							,				__A    :Dict		)   ->   Optional[int]:



									"""simple docstring"""



									return self.encode_prefix(__A		)


							@torch.no_grad()
							def      _snake_case  (							self    :str							,				__A    :List[str]							,				__A    :List[Any]							,				__A    :List[Any]		)   ->   List[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      torch.split(__A							,				1							,				dim=0		)
									SCREAMING_SNAKE_CASE__			=      []
									SCREAMING_SNAKE_CASE__			=      []
									for feature in features:
											SCREAMING_SNAKE_CASE__			=      self.decode_prefix(feature.to(__A		)		)  # back to the clip feature
											# Only support beam search for now
											SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      self.generate_beam(
											    input_embeds=__A							,				device=__A							,				eos_token_id=__A		)
											generated_tokens.append(output_tokens[0]		)
											generated_seq_lengths.append(seq_lengths[0]		)
									SCREAMING_SNAKE_CASE__			=      torch.stack(__A		)
									SCREAMING_SNAKE_CASE__			=      torch.stack(__A		)
									return generated_tokens, generated_seq_lengths


							@torch.no_grad()
							def      _snake_case  (							self    :List[str]							,				__A    :Union[str, Any]=None							,				__A    :Optional[int]=None							,				__A    :List[Any]=None							,				__A    :int = 5							,				__A    :int = 67							,				__A    :float = 1.0							,				__A    :Optional[int] = None							,				)   ->   Tuple:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      eos_token_id
									SCREAMING_SNAKE_CASE__			=      None
									SCREAMING_SNAKE_CASE__			=      None
									SCREAMING_SNAKE_CASE__			=      torch.ones(__A							,				device=__A							,				dtype=torch.int		)
									SCREAMING_SNAKE_CASE__			=      torch.zeros(__A							,				device=__A							,				dtype=torch.bool		)

									if input_embeds is not None:
											SCREAMING_SNAKE_CASE__			=      input_embeds
									else:
											SCREAMING_SNAKE_CASE__			=      self.transformer.transformer.wte(__A		)

									for i in range(__A		):
											SCREAMING_SNAKE_CASE__			=      self.transformer(inputs_embeds=__A		)
											SCREAMING_SNAKE_CASE__			=      outputs.logits
											SCREAMING_SNAKE_CASE__			=      logits[:, -1, :] / (temperature if temperature > 0 else 1.0)
											SCREAMING_SNAKE_CASE__			=      logits.softmax(-1		).log()

											if scores is None:
													SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      logits.topk(__A							,				-1		)
													SCREAMING_SNAKE_CASE__			=      generated.expand(__A							,				*generated.shape[1:]		)
													SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      next_tokens.permute(1							,				0		), scores.squeeze(0		)
													if tokens is None:
															SCREAMING_SNAKE_CASE__			=      next_tokens
													else:
															SCREAMING_SNAKE_CASE__			=      tokens.expand(__A							,				*tokens.shape[1:]		)
															SCREAMING_SNAKE_CASE__			=      torch.cat((tokens, next_tokens)							,				dim=1		)
											else:
													SCREAMING_SNAKE_CASE__			=      -float(np.inf		)
													SCREAMING_SNAKE_CASE__			=      0
													SCREAMING_SNAKE_CASE__			=      scores[:, None] + logits
													seq_lengths[~is_stopped] += 1
													SCREAMING_SNAKE_CASE__			=      scores_sum / seq_lengths[:, None]
													SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      scores_sum_average.view(-1		).topk(__A							,				-1		)
													SCREAMING_SNAKE_CASE__			=      next_tokens // scores_sum.shape[1]
													SCREAMING_SNAKE_CASE__			=      seq_lengths[next_tokens_source]
													SCREAMING_SNAKE_CASE__			=      next_tokens % scores_sum.shape[1]
													SCREAMING_SNAKE_CASE__			=      next_tokens.unsqueeze(1		)
													SCREAMING_SNAKE_CASE__			=      tokens[next_tokens_source]
													SCREAMING_SNAKE_CASE__			=      torch.cat((tokens, next_tokens)							,				dim=1		)
													SCREAMING_SNAKE_CASE__			=      generated[next_tokens_source]
													SCREAMING_SNAKE_CASE__			=      scores_sum_average * seq_lengths
													SCREAMING_SNAKE_CASE__			=      is_stopped[next_tokens_source]

											SCREAMING_SNAKE_CASE__			=      self.transformer.transformer.wte(next_tokens.squeeze()		).view(generated.shape[0]							,				1							,				-1		)
											SCREAMING_SNAKE_CASE__			=      torch.cat((generated, next_token_embed)							,				dim=1		)
											SCREAMING_SNAKE_CASE__			=      is_stopped + next_tokens.eq(__A		).squeeze()
											if is_stopped.all():
													break

									SCREAMING_SNAKE_CASE__			=      scores / seq_lengths
									SCREAMING_SNAKE_CASE__			=      scores.argsort(descending=__A		)
									# tokens tensors are already padded to max_seq_length
									SCREAMING_SNAKE_CASE__			=      [tokens[i] for i in order]
									SCREAMING_SNAKE_CASE__			=      torch.stack(__A							,				dim=0		)
									SCREAMING_SNAKE_CASE__			=      torch.tensor([seq_lengths[i] for i in order]							,				dtype=seq_lengths.dtype		)
									return output_texts, seq_lengths






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


_lowerCamelCase   				=			{
    'configuration_layoutlmv3': [
        'LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'LayoutLMv3Config',
        'LayoutLMv3OnnxConfig',
    ],
    'processing_layoutlmv3': ['LayoutLMv3Processor'],
    'tokenization_layoutlmv3': ['LayoutLMv3Tokenizer'],
}

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase   				=			['LayoutLMv3TokenizerFast']

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase   				=			[
	    'LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST',
	    'LayoutLMv3ForQuestionAnswering',
	    'LayoutLMv3ForSequenceClassification',
	    'LayoutLMv3ForTokenClassification',
	    'LayoutLMv3Model',
	    'LayoutLMv3PreTrainedModel',
	]

try:
	if not is_tf_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase   				=			[
	    'TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST',
	    'TFLayoutLMv3ForQuestionAnswering',
	    'TFLayoutLMv3ForSequenceClassification',
	    'TFLayoutLMv3ForTokenClassification',
	    'TFLayoutLMv3Model',
	    'TFLayoutLMv3PreTrainedModel',
	]

try:
	if not is_vision_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase   				=			['LayoutLMv3FeatureExtractor']
	_lowerCamelCase   				=			['LayoutLMv3ImageProcessor']


if TYPE_CHECKING:
	from .configuration_layoutlmva import (
	    LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    LayoutLMvaConfig,
	    LayoutLMvaOnnxConfig,
	)
	from .processing_layoutlmva import LayoutLMvaProcessor
	from .tokenization_layoutlmva import LayoutLMvaTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_layoutlmva import (
		    LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
		    LayoutLMvaForQuestionAnswering,
		    LayoutLMvaForSequenceClassification,
		    LayoutLMvaForTokenClassification,
		    LayoutLMvaModel,
		    LayoutLMvaPreTrainedModel,
		)

	try:
		if not is_tf_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_tf_layoutlmva import (
		    TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFLayoutLMvaForQuestionAnswering,
		    TFLayoutLMvaForSequenceClassification,
		    TFLayoutLMvaForTokenClassification,
		    TFLayoutLMvaModel,
		    TFLayoutLMvaPreTrainedModel,
		)

	try:
		if not is_vision_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor
		from .image_processing_layoutlmva import LayoutLMvaImageProcessor

else:
	import sys

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