"""simple docstring"""




from __future__ import annotations

import random
import unittest

from transformers import TransfoXLConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers import (
         TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
         TFTransfoXLForSequenceClassification,
         TFTransfoXLLMHeadModel,
         TFTransfoXLModel,
     )
class 						__A							:






     def __init__(		self    ,					a__    ,					):
       _lowerCAmelCase						:	str        =					parent
       _lowerCAmelCase						:	Any        =					13
       _lowerCAmelCase						:	str        =					7
       _lowerCAmelCase						:	Dict        =					30
       _lowerCAmelCase						:	Dict        =					self.seq_length + self.mem_len
       _lowerCAmelCase						:	Dict        =					15
       _lowerCAmelCase						:	Optional[Any]        =					True
       _lowerCAmelCase						:	str        =					True
       _lowerCAmelCase						:	List[str]        =					99
       _lowerCAmelCase						:	Optional[Any]        =					[10, 50, 80]
       _lowerCAmelCase						:	Union[str, Any]        =					32
       _lowerCAmelCase						:	Tuple        =					32
       _lowerCAmelCase						:	Union[str, Any]        =					4
       _lowerCAmelCase						:	Tuple        =					8
       _lowerCAmelCase						:	Tuple        =					128
       _lowerCAmelCase						:	Optional[int]        =					2
       _lowerCAmelCase						:	Tuple        =					2
       _lowerCAmelCase						:	List[str]        =					None
       _lowerCAmelCase						:	str        =					1
       _lowerCAmelCase						:	List[Any]        =					0
       _lowerCAmelCase						:	Optional[int]        =					3
       _lowerCAmelCase						:	List[Any]        =					self.vocab_size - 1
       _lowerCAmelCase						:	int        =					0.0_1






     def    __A  (		self  ):
       _lowerCAmelCase						:	Any        =					ids_tensor([self.batch_size, self.seq_length]    ,					self.vocab_size  )
       _lowerCAmelCase						:	str        =					ids_tensor([self.batch_size, self.seq_length]    ,					self.vocab_size  )

       _lowerCAmelCase						:	Optional[int]        =					None
       if self.use_labels:
         _lowerCAmelCase						:	Optional[int]        =					ids_tensor([self.batch_size, self.seq_length]    ,					self.vocab_size  )

       _lowerCAmelCase						:	List[str]        =					TransfoXLConfig(
           vocab_size=self.vocab_size    ,					mem_len=self.mem_len    ,					clamp_len=self.clamp_len    ,					cutoffs=self.cutoffs    ,					d_model=self.hidden_size    ,					d_embed=self.d_embed    ,					n_head=self.num_attention_heads    ,					d_head=self.d_head    ,					d_inner=self.d_inner    ,					div_val=self.div_val    ,					n_layer=self.num_hidden_layers    ,					eos_token_id=self.eos_token_id    ,					pad_token_id=self.vocab_size - 1    ,					init_range=self.init_range    ,					num_labels=self.num_labels    ,					)

       return (config, input_ids_a, input_ids_a, lm_labels)






     def    __A  (		self  ):
       random.seed(self.seed  )
       tf.random.set_seed(self.seed  )






     def    __A  (		self    ,					a__    ,					a__    ,					a__    ,					a__  ):
       _lowerCAmelCase						:	List[str]        =					TFTransfoXLModel(lowercase_  )

       _lowerCAmelCase      ,     _lowerCAmelCase						:	Tuple        =					model(lowercase_  ).to_tuple()

       _lowerCAmelCase						:	str        =					{"""input_ids""": input_ids_a, """mems""": mems_a}

       _lowerCAmelCase      ,     _lowerCAmelCase						:	List[str]        =					model(lowercase_  ).to_tuple()

       self.parent.assertEqual(hidden_states_a.shape    ,					(self.batch_size, self.seq_length, self.hidden_size)  )
       self.parent.assertEqual(hidden_states_a.shape    ,					(self.batch_size, self.seq_length, self.hidden_size)  )
       self.parent.assertListEqual(
           [mem.shape for mem in mems_a]    ,					[(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers    ,					)
       self.parent.assertListEqual(
           [mem.shape for mem in mems_a]    ,					[(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers    ,					)






     def    __A  (		self    ,					a__    ,					a__    ,					a__    ,					a__  ):
       _lowerCAmelCase						:	str        =					TFTransfoXLLMHeadModel(lowercase_  )

       _lowerCAmelCase      ,     _lowerCAmelCase						:	Dict        =					model(lowercase_  ).to_tuple()

       _lowerCAmelCase						:	str        =					{"""input_ids""": input_ids_a, """labels""": lm_labels}
       _lowerCAmelCase      ,     _lowerCAmelCase						:	List[Any]        =					model(lowercase_  ).to_tuple()

       _lowerCAmelCase      ,     _lowerCAmelCase						:	List[Any]        =					model([input_ids_a, mems_a]  ).to_tuple()

       _lowerCAmelCase						:	Union[str, Any]        =					{"""input_ids""": input_ids_a, """mems""": mems_a, """labels""": lm_labels}

       _lowerCAmelCase      ,     _lowerCAmelCase						:	Optional[int]        =					model(lowercase_  ).to_tuple()

       self.parent.assertEqual(lm_logits_a.shape    ,					(self.batch_size, self.seq_length, self.vocab_size)  )
       self.parent.assertListEqual(
           [mem.shape for mem in mems_a]    ,					[(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers    ,					)

       self.parent.assertEqual(lm_logits_a.shape    ,					(self.batch_size, self.seq_length, self.vocab_size)  )
       self.parent.assertListEqual(
           [mem.shape for mem in mems_a]    ,					[(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers    ,					)






     def    __A  (		self    ,					a__    ,					a__    ,					a__    ,					a__  ):
       _lowerCAmelCase						:	List[Any]        =					TFTransfoXLForSequenceClassification(lowercase_  )
       _lowerCAmelCase						:	Any        =					model(lowercase_  )
       self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.num_labels)  )






     def    __A  (		self  ):
       _lowerCAmelCase						:	Optional[int]        =					self.prepare_config_and_inputs()
       ((_lowerCAmelCase)      ,     (_lowerCAmelCase)      ,     (_lowerCAmelCase)      ,     (_lowerCAmelCase))						:	List[str]        =					config_and_inputs
       _lowerCAmelCase						:	Tuple        =					{"""input_ids""": input_ids_a}
       return config, inputs_dict







@require_tf
class 						__A							(			SCREAMING_SNAKE_CASE_   ,     SCREAMING_SNAKE_CASE_   ,     unittest.TestCase		):
     _UpperCamelCase							:      Union[str, Any]		 =		(
         (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else ()
     )
     _UpperCamelCase							:      Tuple		 =		() if is_tf_available() else ()
     _UpperCamelCase							:      Optional[Any]		 =		(
         {
             "feature-extraction": TFTransfoXLModel,
             "text-classification": TFTransfoXLForSequenceClassification,
             "text-generation": TFTransfoXLLMHeadModel,
             "zero-shot": TFTransfoXLForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     # TODO: add this test when TFTransfoXLLMHead has a linear output layer implemented
     _UpperCamelCase							:      Union[str, Any]		 =		False
     _UpperCamelCase							:      int		 =		False
     _UpperCamelCase							:      Union[str, Any]		 =		False
     _UpperCamelCase							:      Union[str, Any]		 =		False






     def    __A  (		self    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__  ):







       if pipeline_test_casse_name == "TextGenerationPipelineTests":
         # Get `ValueError: AttributeError: 'NoneType' object has no attribute 'new_ones'` or `AssertionError`.
         # `TransfoXLConfig` was never used in pipeline tests: cannot create a simple
         # tokenizer.
         return True

       return False






     def    __A  (		self  ):
       _lowerCAmelCase						:	Union[str, Any]        =					TFTransfoXLModelTester(self  )
       _lowerCAmelCase						:	Optional[int]        =					ConfigTester(self    ,					config_class=lowercase_    ,					d_embed=37  )






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






     def    __A  (		self  ):
       self.model_tester.set_seed()
       _lowerCAmelCase						:	List[Any]        =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_transfo_xl_model(*lowercase_  )






     def    __A  (		self  ):
       self.model_tester.set_seed()
       _lowerCAmelCase						:	Union[str, Any]        =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_transfo_xl_lm_head(*lowercase_  )






     def    __A  (		self  ):
       _lowerCAmelCase						:	Dict        =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*lowercase_  )






     def    __A  (		self  ):
       _lowerCAmelCase      ,     _lowerCAmelCase						:	Optional[int]        =					self.model_tester.prepare_config_and_inputs_for_common()
       _lowerCAmelCase						:	int        =					[TFTransfoXLForSequenceClassification]

       for model_class in self.all_model_classes:
         _lowerCAmelCase						:	Dict        =					model_class(lowercase_  )
         assert isinstance(model.get_input_embeddings()    ,					tf.keras.layers.Layer  )
         if model_class in list_other_models_with_output_ebd:
           _lowerCAmelCase						:	Dict        =					model.get_output_embeddings()
           assert isinstance(lowercase_    ,					tf.keras.layers.Layer  )
           _lowerCAmelCase						:	List[Any]        =					model.get_bias()
           assert name is None
         else:
           _lowerCAmelCase						:	Dict        =					model.get_output_embeddings()
           assert x is None
           _lowerCAmelCase						:	List[Any]        =					model.get_bias()
           assert name is None






     def    __A  (		self  ):
       pass






     @slow
     def    __A  (		self  ):







       for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _lowerCAmelCase						:	Optional[int]        =					TFTransfoXLModel.from_pretrained(lowercase_  )
         self.assertIsNotNone(lowercase_  )






     @unittest.skip(reason="""This model doesn't play well with fit() due to not returning a single loss."""  )
     def    __A  (		self  ):
       pass







@require_tf
class 						__A							(			unittest.TestCase		):






     @unittest.skip("""Skip test until #12651 is resolved."""  )
     @slow
     def    __A  (		self  ):
       _lowerCAmelCase						:	Union[str, Any]        =					TFTransfoXLLMHeadModel.from_pretrained("""transfo-xl-wt103"""  )
       # fmt: off
       _lowerCAmelCase						:	List[Any]        =					tf.convert_to_tensor([[33,1297,2,1,1009,4,1109,11739,4762,358,5,25,245,22,1706,17,20098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,6224,831,16002,2,8,603,78967,29546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,29546,54,8,3609,5,57211,49,4,1,277,18,8,1755,15691,3,341,25,416,693,42573,71,17,401,94,31,17919,2,29546,7873,18,1,435,23,11011,755,5,5167,3,7983,98,84,2,29546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,29546,824,1400,1868,2,19,160,2,311,8,5496,2,20920,17,25,15097,3,24,24,0]]    ,					dtype=tf.intaa  )  # noqa: E231
       # fmt: on
       #  In 1991 , the remains of Russian Tsar Nicholas II and his family
       #  ( except for Alexei and Maria ) are discovered .
       #  The voice of Nicholas's young son , Tsarevich Alexei Nikolaevich , narrates the
       #  remainder of the story . 1883 Western Siberia ,
       #  a young Grigori Rasputin is asked by his father and a group of men to perform magic .
       #  Rasputin has a vision and denounces one of the men as a horse thief . Although his
       #  father initially slaps him for making such an accusation , Rasputin watches as the
       #  man is chased outside and beaten . Twenty years later , Rasputin sees a vision of
       #  the Virgin Mary , prompting him to become a priest . Rasputin quickly becomes famous ,
       #  with people , even a bishop , begging for his blessing . <eod> </s> <eos>

       # fmt: off
       _lowerCAmelCase						:	Any        =					[33,1297,2,1,1009,4,1109,11739,4762,358,5,25,245,22,1706,17,20098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,6224,831,16002,2,8,603,78967,29546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,29546,54,8,3609,5,57211,49,4,1,277,18,8,1755,15691,3,341,25,416,693,42573,71,17,401,94,31,17919,2,29546,7873,18,1,435,23,11011,755,5,5167,3,7983,98,84,2,29546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,29546,824,1400,1868,2,19,160,2,311,8,5496,2,20920,17,25,15097,3,24,24,0,33,1,1857,2,1,1009,4,1109,11739,4762,358,5,25,245,28,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,0]  # noqa: E231
       # fmt: on
       #  In 1991, the remains of Russian Tsar Nicholas II and his family (
       #  except for Alexei and Maria ) are discovered. The voice of young son,
       #  Tsarevich Alexei Nikolaevich, narrates the remainder of the story.
       #  1883 Western Siberia, a young Grigori Rasputin is asked by his father
       #  and a group of men to perform magic. Rasputin has a vision and
       #  denounces one of the men as a horse thief. Although his father initially
       #  slaps him for making such an accusation, Rasputin watches as the man
       #  is chased outside and beaten. Twenty years later, Rasputin sees a vision
       #  of the Virgin Mary, prompting him to become a priest.
       #  Rasputin quickly becomes famous, with people, even a bishop, begging for
       #  his blessing. <unk> <unk> <eos> In the 1990s, the remains of Russian Tsar
       # Nicholas II and his family were discovered. The voice of <unk> young son,
       # Tsarevich Alexei Nikolaevich, narrates the remainder of the story.<eos>

       _lowerCAmelCase						:	int        =					model.generate(lowercase_    ,					max_length=200    ,					do_sample=lowercase_  )
       self.assertListEqual(output_ids[0].numpy().tolist()    ,					lowercase_  )





"""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 						__A							:






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

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






     def    __A  (		self  ):
       _lowerCAmelCase						:	Tuple        =					ids_tensor([self.batch_size, self.text_seq_length]    ,					self.vocab_size  )

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

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

       _lowerCAmelCase						:	List[str]        =					None
       if self.use_input_mask:
         _lowerCAmelCase						:	int        =					random_attention_mask([self.batch_size, self.text_seq_length]  )

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

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

       _lowerCAmelCase						:	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    __A  (		self    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__  ):
       _lowerCAmelCase						:	int        =					LayoutLMvaModel(config=a__  )
       model.to(a__  )
       model.eval()

       # text + image
       _lowerCAmelCase						:	Optional[Any]        =					model(a__    ,					pixel_values=a__  )
       _lowerCAmelCase						:	Any        =					model(
           a__    ,					bbox=a__    ,					pixel_values=a__    ,					attention_mask=a__    ,					token_type_ids=a__  )
       _lowerCAmelCase						:	List[Any]        =					model(a__    ,					bbox=a__    ,					pixel_values=a__    ,					token_type_ids=a__  )
       _lowerCAmelCase						:	Tuple        =					model(a__    ,					bbox=a__    ,					pixel_values=a__  )

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

       # text only
       _lowerCAmelCase						:	Dict        =					model(a__  )
       self.parent.assertEqual(
           result.last_hidden_state.shape    ,					(self.batch_size, self.text_seq_length, self.hidden_size)  )

       # image only
       _lowerCAmelCase						:	Optional[Any]        =					model(pixel_values=a__  )
       self.parent.assertEqual(
           result.last_hidden_state.shape    ,					(self.batch_size, self.image_seq_length, self.hidden_size)  )






     def    __A  (		self    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__  ):
       _lowerCAmelCase						:	int        =					self.num_labels
       _lowerCAmelCase						:	int        =					LayoutLMvaForSequenceClassification(a__  )
       model.to(a__  )
       model.eval()
       _lowerCAmelCase						:	Tuple        =					model(
           a__    ,					bbox=a__    ,					pixel_values=a__    ,					attention_mask=a__    ,					token_type_ids=a__    ,					labels=a__    ,					)
       self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.num_labels)  )






     def    __A  (		self    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__  ):
       _lowerCAmelCase						:	Optional[Any]        =					self.num_labels
       _lowerCAmelCase						:	str        =					LayoutLMvaForTokenClassification(config=a__  )
       model.to(a__  )
       model.eval()
       _lowerCAmelCase						:	List[str]        =					model(
           a__    ,					bbox=a__    ,					pixel_values=a__    ,					attention_mask=a__    ,					token_type_ids=a__    ,					labels=a__    ,					)
       self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.text_seq_length, self.num_labels)  )






     def    __A  (		self    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__  ):
       _lowerCAmelCase						:	Dict        =					LayoutLMvaForQuestionAnswering(config=a__  )
       model.to(a__  )
       model.eval()
       _lowerCAmelCase						:	str        =					model(
           a__    ,					bbox=a__    ,					pixel_values=a__    ,					attention_mask=a__    ,					token_type_ids=a__    ,					start_positions=a__    ,					end_positions=a__    ,					)
       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  ):
       _lowerCAmelCase						:	List[str]        =					self.prepare_config_and_inputs()
       (
           (
           _lowerCAmelCase
       )      ,     (
           _lowerCAmelCase
       )      ,     (
           _lowerCAmelCase
       )      ,     (
           _lowerCAmelCase
       )      ,     (
           _lowerCAmelCase
       )      ,     (
           _lowerCAmelCase
       )      ,     (
           _lowerCAmelCase
       )      ,     (
           _lowerCAmelCase
       )      ,     
       )						:	Any        =					config_and_inputs
       _lowerCAmelCase						:	Union[str, Any]        =					{
           """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 						__A							(			SCREAMING_SNAKE_CASE_   ,     SCREAMING_SNAKE_CASE_   ,     unittest.TestCase		):
     _UpperCamelCase							:      Tuple		 =		False
     _UpperCamelCase							:      Any		 =		False
     _UpperCamelCase							:      Any		 =		False

     _UpperCamelCase							:      int		 =		(
         (
             LayoutLMvaModel,
             LayoutLMvaForSequenceClassification,
             LayoutLMvaForTokenClassification,
             LayoutLMvaForQuestionAnswering,
         )
         if is_torch_available()
         else ()
     )
     _UpperCamelCase							:      Union[str, Any]		 =		(
         {"document-question-answering": LayoutLMvaForQuestionAnswering, "feature-extraction": LayoutLMvaModel}
         if is_torch_available()
         else {}
     )






     def    __A  (		self    ,					a__    ,					a__    ,					a__    ,					a__    ,					a__  ):
       # `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    __A  (		self  ):
       _lowerCAmelCase						:	Any        =					LayoutLMvaModelTester(self  )
       _lowerCAmelCase						:	int        =					ConfigTester(self    ,					config_class=a__    ,					hidden_size=37  )






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

       return inputs_dict






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






     def    __A  (		self  ):
       _lowerCAmelCase						:	Optional[int]        =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*a__  )






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






     def    __A  (		self  ):
       _lowerCAmelCase						:	Optional[Any]        =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*a__  )






     def    __A  (		self  ):
       _lowerCAmelCase						:	Tuple        =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*a__  )






     def    __A  (		self  ):
       _lowerCAmelCase						:	Optional[int]        =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*a__  )






     @slow
     def    __A  (		self  ):
       for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _lowerCAmelCase						:	str        =					LayoutLMvaModel.from_pretrained(a__  )
         self.assertIsNotNone(a__  )







def 							SCREAMING_SNAKE_CASE					(      )	->				str:
  _lowerCAmelCase						:	Any        =					Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     )
  return image






@require_torch
class 						__A							(			unittest.TestCase		):






     @cached_property
     def    __A  (		self  ):
       return LayoutLMvaImageProcessor(apply_ocr=a__  ) if is_vision_available() else None






     @slow
     def    __A  (		self  ):
       _lowerCAmelCase						:	Optional[int]        =					LayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base"""  ).to(a__  )

       _lowerCAmelCase						:	Dict        =					self.default_image_processor
       _lowerCAmelCase						:	Optional[int]        =					prepare_img()
       _lowerCAmelCase						:	Dict        =					image_processor(images=a__    ,					return_tensors="""pt"""  ).pixel_values.to(a__  )

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

       # forward pass
       _lowerCAmelCase						:	str        =					model(
           input_ids=input_ids.to(a__  )    ,					bbox=bbox.to(a__  )    ,					pixel_values=pixel_values.to(a__  )    ,					)

       # verify the logits
       _lowerCAmelCase						:	Optional[int]        =					torch.Size((1, 199, 768)  )
       self.assertEqual(outputs.last_hidden_state.shape    ,					a__  )

       _lowerCAmelCase						:	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(a__  )

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




