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





			"""simple docstring"""




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



				"""simple docstring"""



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

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



			def   lowerCAmelCase						(	self					:  List[Any]							):



				"""simple docstring"""



				_UpperCamelCase:     Optional[int]							= ids_tensor([self.batch_size, self.text_seq_length]		,  self.vocab_size							)

				_UpperCamelCase:     List[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]:
							_UpperCamelCase:     str							= bbox[i, j, 3]
							_UpperCamelCase:     List[str]							= bbox[i, j, 1]
							_UpperCamelCase:     Any							= t
						if bbox[i, j, 2] < bbox[i, j, 0]:
							_UpperCamelCase:     Any							= bbox[i, j, 2]
							_UpperCamelCase:     Any							= bbox[i, j, 0]
							_UpperCamelCase:     Optional[Any]							= t

				_UpperCamelCase:     Dict							= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

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

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

				_UpperCamelCase:     List[str]							= LayoutLMvaConfig(
				    vocab_size=self.vocab_size		,  hidden_size=self.hidden_size		,  num_hidden_layers=self.num_hidden_layers		,  num_attention_heads=self.num_attention_heads		,  intermediate_size=self.intermediate_size		,  hidden_act=self.hidden_act		,  hidden_dropout_prob=self.hidden_dropout_prob		,  attention_probs_dropout_prob=self.attention_probs_dropout_prob		,  max_position_embeddings=self.max_position_embeddings		,  type_vocab_size=self.type_vocab_size		,  initializer_range=self.initializer_range		,  coordinate_size=self.coordinate_size		,  shape_size=self.shape_size		,  input_size=self.image_size		,  patch_size=self.patch_size		,  )

				return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels



			def   lowerCAmelCase						(	self					:  int		,  _lowercase					:  Union[str, Any]		,  _lowercase					:  int		,  _lowercase					:  str		,  _lowercase					:  int		,  _lowercase					:  Union[str, Any]		,  _lowercase					:  Any		,  _lowercase					:  Dict		,  _lowercase					:  Union[str, Any]							):



				"""simple docstring"""



				_UpperCamelCase:     Dict							= LayoutLMvaModel(config=lowerCamelCase_							)
				model.to(lowerCamelCase_							)
				model.eval()

				# text + image
				_UpperCamelCase:     Optional[Any]							= model(lowerCamelCase_		,  pixel_values=lowerCamelCase_							)
				_UpperCamelCase:     List[str]							= model(
				    lowerCamelCase_		,  bbox=lowerCamelCase_		,  pixel_values=lowerCamelCase_		,  attention_mask=lowerCamelCase_		,  token_type_ids=lowerCamelCase_							)
				_UpperCamelCase:     List[Any]							= model(lowerCamelCase_		,  bbox=lowerCamelCase_		,  pixel_values=lowerCamelCase_		,  token_type_ids=lowerCamelCase_							)
				_UpperCamelCase:     Optional[Any]							= model(lowerCamelCase_		,  bbox=lowerCamelCase_		,  pixel_values=lowerCamelCase_							)

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

				# text only
				_UpperCamelCase:     List[str]							= model(lowerCamelCase_							)
				self.parent.assertEqual(
				    result.last_hidden_state.shape		,  (self.batch_size, self.text_seq_length, self.hidden_size)							)

				# image only
				_UpperCamelCase:     List[str]							= model(pixel_values=lowerCamelCase_							)
				self.parent.assertEqual(
				    result.last_hidden_state.shape		,  (self.batch_size, self.image_seq_length, self.hidden_size)							)



			def   lowerCAmelCase						(	self					:  str		,  _lowercase					:  Union[str, Any]		,  _lowercase					:  Union[str, Any]		,  _lowercase					:  Tuple		,  _lowercase					:  List[str]		,  _lowercase					:  Any		,  _lowercase					:  int		,  _lowercase					:  List[Any]		,  _lowercase					:  List[str]							):



				"""simple docstring"""



				_UpperCamelCase:     Optional[Any]							= self.num_labels
				_UpperCamelCase:     Dict							= LayoutLMvaForSequenceClassification(lowerCamelCase_							)
				model.to(lowerCamelCase_							)
				model.eval()
				_UpperCamelCase:     int							= model(
				    lowerCamelCase_		,  bbox=lowerCamelCase_		,  pixel_values=lowerCamelCase_		,  attention_mask=lowerCamelCase_		,  token_type_ids=lowerCamelCase_		,  labels=lowerCamelCase_		,  )
				self.parent.assertEqual(result.logits.shape		,  (self.batch_size, self.num_labels)							)



			def   lowerCAmelCase						(	self					:  Union[str, Any]		,  _lowercase					:  Tuple		,  _lowercase					:  Any		,  _lowercase					:  Optional[int]		,  _lowercase					:  Any		,  _lowercase					:  Optional[int]		,  _lowercase					:  int		,  _lowercase					:  str		,  _lowercase					:  Any							):



				"""simple docstring"""



				_UpperCamelCase:     int							= self.num_labels
				_UpperCamelCase:     int							= LayoutLMvaForTokenClassification(config=lowerCamelCase_							)
				model.to(lowerCamelCase_							)
				model.eval()
				_UpperCamelCase:     Optional[Any]							= model(
				    lowerCamelCase_		,  bbox=lowerCamelCase_		,  pixel_values=lowerCamelCase_		,  attention_mask=lowerCamelCase_		,  token_type_ids=lowerCamelCase_		,  labels=lowerCamelCase_		,  )
				self.parent.assertEqual(result.logits.shape		,  (self.batch_size, self.text_seq_length, self.num_labels)							)



			def   lowerCAmelCase						(	self					:  Optional[Any]		,  _lowercase					:  Dict		,  _lowercase					:  Dict		,  _lowercase					:  Dict		,  _lowercase					:  int		,  _lowercase					:  List[str]		,  _lowercase					:  int		,  _lowercase					:  str		,  _lowercase					:  Optional[int]							):



				"""simple docstring"""



				_UpperCamelCase:     Optional[int]							= LayoutLMvaForQuestionAnswering(config=lowerCamelCase_							)
				model.to(lowerCamelCase_							)
				model.eval()
				_UpperCamelCase:     List[Any]							= model(
				    lowerCamelCase_		,  bbox=lowerCamelCase_		,  pixel_values=lowerCamelCase_		,  attention_mask=lowerCamelCase_		,  token_type_ids=lowerCamelCase_		,  start_positions=lowerCamelCase_		,  end_positions=lowerCamelCase_		,  )
				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					:  Optional[int]							):



				"""simple docstring"""



				_UpperCamelCase:     Optional[int]							= self.prepare_config_and_inputs()
				(
				    _UpperCamelCase					
				):     Tuple							= config_and_inputs
				_UpperCamelCase:     Dict							= {
				    '''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  __magic_name__  (		_UpperCAmelCase      ,	_UpperCAmelCase      ,	unittest.TestCase    ):





			"""simple docstring"""




			lowerCAmelCase      :    int  			=   False
			lowerCAmelCase      :    Tuple  			=   False
			lowerCAmelCase      :    List[str]  			=   False

			lowerCAmelCase      :    Tuple  			=   (
			    (
			        LayoutLMvaModel,
			        LayoutLMvaForSequenceClassification,
			        LayoutLMvaForTokenClassification,
			        LayoutLMvaForQuestionAnswering,
			    )
			    if is_torch_available()
			    else ()
			)
			lowerCAmelCase      :    List[str]  			=   (
			    {'''document-question-answering''': LayoutLMvaForQuestionAnswering, '''feature-extraction''': LayoutLMvaModel}
			    if is_torch_available()
			    else {}
			)



			def   lowerCAmelCase						(	self					:  int		,  _lowercase					:  Union[str, Any]		,  _lowercase					:  str		,  _lowercase					:  str		,  _lowercase					:  Optional[Any]		,  _lowercase					:  List[str]							):



				"""simple docstring"""



				return True



			def   lowerCAmelCase						(	self					:  List[str]							):



				"""simple docstring"""



				_UpperCamelCase:     str							= LayoutLMvaModelTester(self							)
				_UpperCamelCase:     List[Any]							= ConfigTester(self		,  config_class=lowerCamelCase_		,  hidden_size=37							)



			def   lowerCAmelCase						(	self					:  Any		,  _lowercase					:  Dict		,  _lowercase					:  List[str]		,  _lowercase					:  str=False							):



				"""simple docstring"""



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

				return inputs_dict



			def   lowerCAmelCase						(	self					:  Optional[Any]							):



				"""simple docstring"""



				self.config_tester.run_common_tests()



			def   lowerCAmelCase						(	self					:  str							):



				"""simple docstring"""



				_UpperCamelCase:     int							= self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowerCamelCase_							)



			def   lowerCAmelCase						(	self					:  Any							):



				"""simple docstring"""



				_UpperCamelCase:     int							= self.model_tester.prepare_config_and_inputs()
				for type in ["absolute", "relative_key", "relative_key_query"]:
					_UpperCamelCase:     str							= type
					self.model_tester.create_and_check_model(*lowerCamelCase_							)



			def   lowerCAmelCase						(	self					:  Union[str, Any]							):



				"""simple docstring"""



				_UpperCamelCase:     List[Any]							= self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_							)



			def   lowerCAmelCase						(	self					:  str							):



				"""simple docstring"""



				_UpperCamelCase:     Optional[Any]							= self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_							)



			def   lowerCAmelCase						(	self					:  List[str]							):



				"""simple docstring"""



				_UpperCamelCase:     int							= self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_							)



			@slow
			def   lowerCAmelCase						(	self					:  int							):



				"""simple docstring"""



				for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					_UpperCamelCase:     Optional[int]							= LayoutLMvaModel.from_pretrained(lowerCamelCase_							)
					self.assertIsNotNone(lowerCamelCase_							)




def       lowerCAmelCase_     (		)    ->				Optional[int]:







	'''simple docstring'''
	_UpperCamelCase:     Tuple							= Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
	return image





@require_torch
class  __magic_name__  (		unittest.TestCase    ):





			"""simple docstring"""




			@cached_property
			def   lowerCAmelCase						(	self					:  str							):



				"""simple docstring"""



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



			@slow
			def   lowerCAmelCase						(	self					:  Optional[Any]							):



				"""simple docstring"""



				_UpperCamelCase:     Union[str, Any]							= LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''							).to(lowerCamelCase_							)

				_UpperCamelCase:     Any							= self.default_image_processor
				_UpperCamelCase:     List[Any]							= prepare_img()
				_UpperCamelCase:     Tuple							= image_processor(images=lowerCamelCase_		,  return_tensors='''pt'''							).pixel_values.to(lowerCamelCase_							)

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

				# forward pass
				_UpperCamelCase:     Tuple							= model(
				    input_ids=input_ids.to(lowerCamelCase_							)		,  bbox=bbox.to(lowerCamelCase_							)		,  pixel_values=pixel_values.to(lowerCamelCase_							)		,  )

				# verify the logits
				_UpperCamelCase:     Union[str, Any]							= torch.Size((1, 199, 768)							)
				self.assertEqual(outputs.last_hidden_state.shape		,  lowerCamelCase_							)

				_UpperCamelCase:     List[Any]							= torch.tensor(
				    [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]							).to(lowerCamelCase_							)

				self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]		,  lowerCamelCase_		,  atol=1E-4							)							)
from typing import TYPE_CHECKING

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


UpperCAmelCase_			    =							{
    '''configuration_blenderbot''': [
        '''BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''BlenderbotConfig''',
        '''BlenderbotOnnxConfig''',
    ],
    '''tokenization_blenderbot''': ['''BlenderbotTokenizer'''],
}

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

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_			    =							[
      '''BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''BlenderbotForCausalLM''',
      '''BlenderbotForConditionalGeneration''',
      '''BlenderbotModel''',
      '''BlenderbotPreTrainedModel''',
  ]


try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_			    =							[
      '''TFBlenderbotForConditionalGeneration''',
      '''TFBlenderbotModel''',
      '''TFBlenderbotPreTrainedModel''',
  ]


try:
  if not is_flax_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_			    =							[
      '''FlaxBlenderbotForConditionalGeneration''',
      '''FlaxBlenderbotModel''',
      '''FlaxBlenderbotPreTrainedModel''',
  ]


if TYPE_CHECKING:
  from .configuration_blenderbot import (
      BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP,
      BlenderbotConfig,
      BlenderbotOnnxConfig,
  )
  from .tokenization_blenderbot import BlenderbotTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_blenderbot_fast import BlenderbotTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_blenderbot import (
        BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST,
        BlenderbotForCausalLM,
        BlenderbotForConditionalGeneration,
        BlenderbotModel,
        BlenderbotPreTrainedModel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_blenderbot import (
        TFBlenderbotForConditionalGeneration,
        TFBlenderbotModel,
        TFBlenderbotPreTrainedModel,
    )

  try:
    if not is_flax_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_flax_blenderbot import (
        FlaxBlenderbotForConditionalGeneration,
        FlaxBlenderbotModel,
        FlaxBlenderbotPreTrainedModel,
    )

else:
  import sys

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