



"""simple docstring"""

import unittest

from transformers import LayoutLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
							import torch

							from transformers import (
							    LayoutLMForMaskedLM,
							    LayoutLMForQuestionAnswering,
							    LayoutLMForSequenceClassification,
							    LayoutLMForTokenClassification,
							    LayoutLMModel,
							)
class 						lowerCAmelCase__	:


	'''simple docstring'''







	def __init__(			self							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Dict=13    ,					_SCREAMING_SNAKE_CASE							:					Any=7    ,					_SCREAMING_SNAKE_CASE							:					str=True    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]=True    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]=True    ,					_SCREAMING_SNAKE_CASE							:					Dict=True    ,					_SCREAMING_SNAKE_CASE							:					List[Any]=99    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]=32    ,					_SCREAMING_SNAKE_CASE							:					int=5    ,					_SCREAMING_SNAKE_CASE							:					str=4    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]=37    ,					_SCREAMING_SNAKE_CASE							:					str="gelu"    ,					_SCREAMING_SNAKE_CASE							:					Dict=0.1    ,					_SCREAMING_SNAKE_CASE							:					Dict=0.1    ,					_SCREAMING_SNAKE_CASE							:					List[Any]=512    ,					_SCREAMING_SNAKE_CASE							:					Any=16    ,					_SCREAMING_SNAKE_CASE							:					str=2    ,					_SCREAMING_SNAKE_CASE							:					Any=0.0_2    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]=3    ,					_SCREAMING_SNAKE_CASE							:					List[str]=4    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]=None    ,					_SCREAMING_SNAKE_CASE							:					int=1_000    ,					)	->  Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    parent
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    batch_size
					SCREAMING_SNAKE_CASE	:       Any 							=    seq_length
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    is_training
					SCREAMING_SNAKE_CASE	:       List[str] 							=    use_input_mask
					SCREAMING_SNAKE_CASE	:       str 							=    use_token_type_ids
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    use_labels
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    vocab_size
					SCREAMING_SNAKE_CASE	:       int 							=    hidden_size
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    num_hidden_layers
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    num_attention_heads
					SCREAMING_SNAKE_CASE	:       Dict 							=    intermediate_size
					SCREAMING_SNAKE_CASE	:       int 							=    hidden_act
					SCREAMING_SNAKE_CASE	:       Dict 							=    hidden_dropout_prob
					SCREAMING_SNAKE_CASE	:       str 							=    attention_probs_dropout_prob
					SCREAMING_SNAKE_CASE	:       Dict 							=    max_position_embeddings
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    type_vocab_size
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    type_sequence_label_size
					SCREAMING_SNAKE_CASE	:       int 							=    initializer_range
					SCREAMING_SNAKE_CASE	:       Any 							=    num_labels
					SCREAMING_SNAKE_CASE	:       int 							=    num_choices
					SCREAMING_SNAKE_CASE	:       Dict 							=    scope
					SCREAMING_SNAKE_CASE	:       Dict 							=    range_bbox





	def  _lowerCAmelCase    (			self							:					List[str]						)	->  List[Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    ids_tensor([self.batch_size, self.seq_length]    ,					self.vocab_size						)

					SCREAMING_SNAKE_CASE	:       Any 							=    ids_tensor([self.batch_size, self.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]:
																	SCREAMING_SNAKE_CASE	:       List[str] 							=    bbox[i, j, 3]
																	SCREAMING_SNAKE_CASE	:       Dict 							=    bbox[i, j, 1]
																	SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    t
													if bbox[i, j, 2] < bbox[i, j, 0]:
																	SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    bbox[i, j, 2]
																	SCREAMING_SNAKE_CASE	:       Any 							=    bbox[i, j, 0]
																	SCREAMING_SNAKE_CASE	:       List[Any] 							=    t

					SCREAMING_SNAKE_CASE	:       int 							=    None
					if self.use_input_mask:
									SCREAMING_SNAKE_CASE	:       Optional[int] 							=    ids_tensor([self.batch_size, self.seq_length]    ,					vocab_size=2						)

					SCREAMING_SNAKE_CASE	:       Dict 							=    None
					if self.use_token_type_ids:
									SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    ids_tensor([self.batch_size, self.seq_length]    ,					self.type_vocab_size						)

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

					SCREAMING_SNAKE_CASE	:       Tuple 							=    self.get_config()

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





	def  _lowerCAmelCase    (			self							:					Any						)	->  Dict:





					"""simple docstring"""


					return LayoutLMConfig(
					    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    ,					)





	def  _lowerCAmelCase    (			self							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					int						)	->  str:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Tuple 							=    LayoutLMModel(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    model(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE						)
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    model(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE						)
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    model(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.last_hidden_state.shape    ,					(self.batch_size, self.seq_length, self.hidden_size)						)
					self.parent.assertEqual(result.pooler_output.shape    ,					(self.batch_size, self.hidden_size)						)





	def  _lowerCAmelCase    (			self							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Any    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]						)	->  List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[Any] 							=    LayoutLMForMaskedLM(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    model(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.seq_length, self.vocab_size)						)





	def  _lowerCAmelCase    (			self							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					str    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					Any    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]						)	->  Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[Any] 							=    self.num_labels
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    LayoutLMForSequenceClassification(_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Dict 							=    model(
					    _SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.num_labels)						)





	def  _lowerCAmelCase    (			self							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]						)	->  Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    self.num_labels
					SCREAMING_SNAKE_CASE	:       int 							=    LayoutLMForTokenClassification(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    model(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.seq_length, self.num_labels)						)





	def  _lowerCAmelCase    (			self							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					str    ,					_SCREAMING_SNAKE_CASE							:					str    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					List[Any]						)	->  Dict:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    LayoutLMForQuestionAnswering(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Dict 							=    model(
					    _SCREAMING_SNAKE_CASE    ,					bbox=_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					start_positions=_SCREAMING_SNAKE_CASE    ,					end_positions=_SCREAMING_SNAKE_CASE    ,					)
					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							:					List[str]						)	->  Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    self.prepare_config_and_inputs()
					(
					    SCREAMING_SNAKE_CASE	
					)	:       Optional[int] 							=    config_and_inputs
					SCREAMING_SNAKE_CASE	:       Dict 							=    {
					    'input_ids': input_ids,
					    'bbox': bbox,
					    'token_type_ids': token_type_ids,
					    'attention_mask': input_mask,
					}
					return config, inputs_dict




@require_torch
class 						lowerCAmelCase__	(  _lowerCamelCase       ,       _lowerCamelCase       ,       unittest.TestCase   ):


	'''simple docstring'''







	_SCREAMING_SNAKE_CASE  :		Optional[Any]     	=   (
	    (
	        LayoutLMModel,
	        LayoutLMForMaskedLM,
	        LayoutLMForSequenceClassification,
	        LayoutLMForTokenClassification,
	        LayoutLMForQuestionAnswering,
	    )
	    if is_torch_available()
	    else None
	)
	_SCREAMING_SNAKE_CASE  :		Union[str, Any]     	=   (
	    {
	        '''document-question-answering''': LayoutLMForQuestionAnswering,
	        '''feature-extraction''': LayoutLMModel,
	        '''fill-mask''': LayoutLMForMaskedLM,
	        '''text-classification''': LayoutLMForSequenceClassification,
	        '''token-classification''': LayoutLMForTokenClassification,
	        '''zero-shot''': LayoutLMForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	_SCREAMING_SNAKE_CASE  :		List[Any]     	=   True





	def  _lowerCAmelCase    (			self							:					Dict						)	->  Dict:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    LayoutLMModelTester(self						)
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    ConfigTester(self    ,					config_class=_SCREAMING_SNAKE_CASE    ,					hidden_size=37						)





	def  _lowerCAmelCase    (			self							:					List[str]						)	->  int:





					"""simple docstring"""


					self.config_tester.run_common_tests()





	def  _lowerCAmelCase    (			self							:					Any						)	->  List[Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Union[str, Any]						)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Dict 							=    self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
									SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    type
									self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Union[str, Any]						)	->  str:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Optional[Any]						)	->  str:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Dict 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Dict						)	->  List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Tuple 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					str						)	->  Dict:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       str 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE						)







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





					"""simple docstring"""


					pass




def    __snake_case       (  )    ->					List[Any]:






				'''simple docstring'''
				SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    torch.tensor([[101,1019,1014,1016,1037,12849,4747,1004,14246,2278,5439,4524,5002,2930,2193,2930,4341,3208,1005,1055,2171,2848,11300,3531,102],[101,4070,4034,7020,1024,3058,1015,1013,2861,1013,6070,19274,2772,6205,27814,16147,16147,4343,2047,10283,10969,14389,1012,2338,102]]       ,		device=__A    )  # noqa: E231
				SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    torch.tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],]       ,		device=__A    )  # noqa: E231
				SCREAMING_SNAKE_CASE	:       List[str] 							=    torch.tensor([[[0,0,0,0],[423,237,440,251],[427,272,441,287],[419,115,437,129],[961,885,992,912],[256,38,330,58],[256,38,330,58],[336,42,353,57],[360,39,401,56],[360,39,401,56],[411,39,471,59],[479,41,528,59],[533,39,630,60],[67,113,134,131],[141,115,209,132],[68,149,133,166],[141,149,187,164],[195,148,287,165],[195,148,287,165],[195,148,287,165],[295,148,349,165],[441,149,492,166],[497,149,546,164],[64,201,125,218],[1000,1000,1000,1000]],[[0,0,0,0],[662,150,754,166],[665,199,742,211],[519,213,554,228],[519,213,554,228],[134,433,187,454],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[314,469,376,482],[504,684,582,706],[941,825,973,900],[941,825,973,900],[941,825,973,900],[941,825,973,900],[610,749,652,765],[130,659,168,672],[176,657,237,672],[238,657,312,672],[443,653,628,672],[443,653,628,672],[716,301,825,317],[1000,1000,1000,1000]]]       ,		device=__A    )  # noqa: E231
				SCREAMING_SNAKE_CASE	:       Tuple 							=    torch.tensor([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]       ,		device=__A    )  # noqa: E231
				# these are sequence labels (i.e. at the token level)
				SCREAMING_SNAKE_CASE	:       str 							=    torch.tensor([[-100,10,10,10,9,1,-100,7,7,-100,7,7,4,2,5,2,8,8,-100,-100,5,0,3,2,-100],[-100,12,12,12,-100,12,10,-100,-100,-100,-100,10,12,9,-100,-100,-100,10,10,10,9,12,-100,10,-100]]       ,		device=__A    )  # noqa: E231
				# fmt: on

				return input_ids, attention_mask, bbox, token_type_ids, labels







@require_torch
class 						lowerCAmelCase__	(  unittest.TestCase   ):


	'''simple docstring'''







	@slow
	def  _lowerCAmelCase    (			self							:					Optional[Any]						)	->  Dict:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    LayoutLMModel.from_pretrained('microsoft/layoutlm-base-uncased'						).to(_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       Tuple 							=    prepare_layoutlm_batch_inputs()

					# forward pass
					SCREAMING_SNAKE_CASE	:       Any 							=    model(input_ids=_SCREAMING_SNAKE_CASE    ,					bbox=_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE						)

					# test the sequence output on [0, :3, :3]
					SCREAMING_SNAKE_CASE	:       int 							=    torch.tensor(
					    [[0.1_7_8_5, -0.1_9_4_7, -0.0_4_2_5], [-0.3_2_5_4, -0.2_8_0_7, 0.2_5_5_3], [-0.5_3_9_1, -0.3_3_2_2, 0.3_3_6_4]]    ,					device=_SCREAMING_SNAKE_CASE    ,					)

					self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]    ,					_SCREAMING_SNAKE_CASE    ,					atol=1E-3						)						)

					# test the pooled output on [1, :3]
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    torch.tensor([-0.6_5_8_0, -0.0_2_1_4, 0.8_5_5_2]    ,					device=_SCREAMING_SNAKE_CASE						)

					self.assertTrue(torch.allclose(outputs.pooler_output[1, :3]    ,					_SCREAMING_SNAKE_CASE    ,					atol=1E-3						)						)





	@slow
	def  _lowerCAmelCase    (			self							:					int						)	->  List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Tuple 							=    LayoutLMForSequenceClassification.from_pretrained('microsoft/layoutlm-base-uncased'    ,					num_labels=2						).to(
					    _SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       List[Any] 							=    prepare_layoutlm_batch_inputs()

					# forward pass
					SCREAMING_SNAKE_CASE	:       List[str] 							=    model(
					    input_ids=_SCREAMING_SNAKE_CASE    ,					bbox=_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					labels=torch.tensor([1, 1]    ,					device=_SCREAMING_SNAKE_CASE						)    ,					)

					# test whether we get a loss as a scalar
					SCREAMING_SNAKE_CASE	:       Any 							=    outputs.loss
					SCREAMING_SNAKE_CASE	:       Tuple 							=    torch.Size([]						)
					self.assertEqual(loss.shape    ,					_SCREAMING_SNAKE_CASE						)

					# test the shape of the logits
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    outputs.logits
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    torch.Size((2, 2)						)
					self.assertEqual(logits.shape    ,					_SCREAMING_SNAKE_CASE						)





	@slow
	def  _lowerCAmelCase    (			self							:					int						)	->  str:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Dict 							=    LayoutLMForTokenClassification.from_pretrained('microsoft/layoutlm-base-uncased'    ,					num_labels=13						).to(
					    _SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       Dict 							=    prepare_layoutlm_batch_inputs()

					# forward pass
					SCREAMING_SNAKE_CASE	:       Any 							=    model(
					    input_ids=_SCREAMING_SNAKE_CASE    ,					bbox=_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE						)

					# test the loss calculation to be around 2.65
					# expected_loss = torch.tensor(2.65, device=torch_device)

					# The loss is currently somewhat random and can vary between 0.1-0.3 atol.
					# self.assertTrue(torch.allclose(outputs.loss, expected_loss, atol=0.1))

					# test the shape of the logits
					SCREAMING_SNAKE_CASE	:       str 							=    outputs.logits
					SCREAMING_SNAKE_CASE	:       List[str] 							=    torch.Size((2, 25, 13)						)
					self.assertEqual(logits.shape    ,					_SCREAMING_SNAKE_CASE						)







	@slow
	def  _lowerCAmelCase    (			self							:					Union[str, Any]						)	->  List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       str 							=    LayoutLMForQuestionAnswering.from_pretrained('microsoft/layoutlm-base-uncased'						).to(_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       str 							=    prepare_layoutlm_batch_inputs()

					# forward pass
					SCREAMING_SNAKE_CASE	:       int 							=    model(input_ids=_SCREAMING_SNAKE_CASE    ,					bbox=_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE						)

					# test the shape of the logits
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    torch.Size((2, 25)						)
					self.assertEqual(outputs.start_logits.shape    ,					_SCREAMING_SNAKE_CASE						)
					self.assertEqual(outputs.end_logits.shape    ,					_SCREAMING_SNAKE_CASE						)






"""simple docstring"""

import json
import os
import random
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
							from PIL import Image

							from transformers import FlavaImageProcessor, FlavaProcessor
							from transformers.models.flava.image_processing_flava import (
							    FLAVA_CODEBOOK_MEAN,
							    FLAVA_CODEBOOK_STD,
							    FLAVA_IMAGE_MEAN,
							    FLAVA_IMAGE_STD,
							)
@require_vision
class 						lowerCAmelCase__	(  unittest.TestCase   ):


	'''simple docstring'''







	def  _lowerCAmelCase    (			self							:					int						)	->  Union[str, Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    tempfile.mkdtemp()

					# fmt: off
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    ['[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest']
					# fmt: on
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    os.path.join(self.tmpdirname    ,					VOCAB_FILES_NAMES['vocab_file']						)

					with open(self.vocab_file    ,					'w'    ,					encoding='utf-8'						) as fp:
									fp.write(''.join([x + '\n' for x in vocab_tokens]						)						)

					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    {
					    'image_mean': FLAVA_IMAGE_MEAN,
					    'image_std': FLAVA_IMAGE_STD,
					    'do_normalize': True,
					    'do_resize': True,
					    'size': 224,
					    'do_center_crop': True,
					    'crop_size': 224,
					    'input_size_patches': 14,
					    'total_mask_patches': 75,
					    'mask_group_max_patches': None,
					    'mask_group_min_patches': 16,
					    'mask_group_min_aspect_ratio': 0.3,
					    'mask_group_max_aspect_ratio': None,
					    'codebook_do_resize': True,
					    'codebook_size': 112,
					    'codebook_do_center_crop': True,
					    'codebook_crop_size': 112,
					    'codebook_do_map_pixels': True,
					    'codebook_do_normalize': True,
					    'codebook_image_mean': FLAVA_CODEBOOK_MEAN,
					    'codebook_image_std': FLAVA_CODEBOOK_STD,
					}

					SCREAMING_SNAKE_CASE	:       List[Any] 							=    os.path.join(self.tmpdirname    ,					_SCREAMING_SNAKE_CASE						)
					with open(self.image_processor_file    ,					'w'    ,					encoding='utf-8'						) as fp:
									json.dump(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Any    ,					**_SCREAMING_SNAKE_CASE							:					str						)	->  str:





					"""simple docstring"""


					return BertTokenizer.from_pretrained(self.tmpdirname    ,					**_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					int    ,					**_SCREAMING_SNAKE_CASE							:					Any						)	->  Union[str, Any]:





					"""simple docstring"""


					return BertTokenizerFast.from_pretrained(self.tmpdirname    ,					**_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Union[str, Any]    ,					**_SCREAMING_SNAKE_CASE							:					Dict						)	->  str:





					"""simple docstring"""


					return FlavaImageProcessor.from_pretrained(self.tmpdirname    ,					**_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Dict						)	->  str:





					"""simple docstring"""


					shutil.rmtree(self.tmpdirname						)





	def  _lowerCAmelCase    (			self							:					int						)	->  Optional[Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       int 							=    [np.random.randint(255    ,					size=(3, 30, 400)    ,					dtype=np.uinta						)]

					SCREAMING_SNAKE_CASE	:       str 							=    [Image.fromarray(np.moveaxis(_SCREAMING_SNAKE_CASE    ,					0    ,					-1						)						) for x in image_inputs]

					return image_inputs





	def  _lowerCAmelCase    (			self							:					Tuple						)	->  Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       int 							=    self.get_tokenizer()
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    self.get_rust_tokenizer()
					SCREAMING_SNAKE_CASE	:       str 							=    self.get_image_processor()

					SCREAMING_SNAKE_CASE	:       int 							=    FlavaProcessor(tokenizer=_SCREAMING_SNAKE_CASE    ,					image_processor=_SCREAMING_SNAKE_CASE						)
					processor_slow.save_pretrained(self.tmpdirname						)
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    FlavaProcessor.from_pretrained(self.tmpdirname    ,					use_fast=_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       str 							=    FlavaProcessor(tokenizer=_SCREAMING_SNAKE_CASE    ,					image_processor=_SCREAMING_SNAKE_CASE						)
					processor_fast.save_pretrained(self.tmpdirname						)
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    FlavaProcessor.from_pretrained(self.tmpdirname						)

					self.assertEqual(processor_slow.tokenizer.get_vocab()    ,					tokenizer_slow.get_vocab()						)
					self.assertEqual(processor_fast.tokenizer.get_vocab()    ,					tokenizer_fast.get_vocab()						)
					self.assertEqual(tokenizer_slow.get_vocab()    ,					tokenizer_fast.get_vocab()						)
					self.assertIsInstance(processor_slow.tokenizer    ,					_SCREAMING_SNAKE_CASE						)
					self.assertIsInstance(processor_fast.tokenizer    ,					_SCREAMING_SNAKE_CASE						)

					self.assertEqual(processor_slow.image_processor.to_json_string()    ,					image_processor.to_json_string()						)
					self.assertEqual(processor_fast.image_processor.to_json_string()    ,					image_processor.to_json_string()						)
					self.assertIsInstance(processor_slow.image_processor    ,					_SCREAMING_SNAKE_CASE						)
					self.assertIsInstance(processor_fast.image_processor    ,					_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Optional[Any]						)	->  List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Tuple 							=    FlavaProcessor(tokenizer=self.get_tokenizer()    ,					image_processor=self.get_image_processor()						)
					processor.save_pretrained(self.tmpdirname						)

					SCREAMING_SNAKE_CASE	:       Dict 							=    self.get_tokenizer(bos_token='(BOS)'    ,					eos_token='(EOS)'						)
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    self.get_image_processor(do_normalize=_SCREAMING_SNAKE_CASE    ,					padding_value=1.0						)

					SCREAMING_SNAKE_CASE	:       List[str] 							=    FlavaProcessor.from_pretrained(
					    self.tmpdirname    ,					bos_token='(BOS)'    ,					eos_token='(EOS)'    ,					do_normalize=_SCREAMING_SNAKE_CASE    ,					padding_value=1.0						)

					self.assertEqual(processor.tokenizer.get_vocab()    ,					tokenizer_add_kwargs.get_vocab()						)
					self.assertIsInstance(processor.tokenizer    ,					_SCREAMING_SNAKE_CASE						)

					self.assertEqual(processor.image_processor.to_json_string()    ,					image_processor_add_kwargs.to_json_string()						)
					self.assertIsInstance(processor.image_processor    ,					_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					List[Any]						)	->  Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    self.get_image_processor()
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    self.get_tokenizer()

					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    FlavaProcessor(tokenizer=_SCREAMING_SNAKE_CASE    ,					image_processor=_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    self.prepare_image_inputs()

					SCREAMING_SNAKE_CASE	:       Any 							=    image_processor(_SCREAMING_SNAKE_CASE    ,					return_tensors='np'						)
					SCREAMING_SNAKE_CASE	:       str 							=    processor(images=_SCREAMING_SNAKE_CASE    ,					return_tensors='np'						)

					for key in input_feat_extract.keys():
									self.assertAlmostEqual(input_feat_extract[key].sum()    ,					input_processor[key].sum()    ,					delta=1E-2						)

					# With rest of the args
					random.seed(1_234						)
					SCREAMING_SNAKE_CASE	:       str 							=    image_processor(
					    _SCREAMING_SNAKE_CASE    ,					return_image_mask=_SCREAMING_SNAKE_CASE    ,					return_codebook_pixels=_SCREAMING_SNAKE_CASE    ,					return_tensors='np'						)
					random.seed(1_234						)
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    processor(
					    images=_SCREAMING_SNAKE_CASE    ,					return_image_mask=_SCREAMING_SNAKE_CASE    ,					return_codebook_pixels=_SCREAMING_SNAKE_CASE    ,					return_tensors='np'						)

					for key in input_feat_extract.keys():
									self.assertAlmostEqual(input_feat_extract[key].sum()    ,					input_processor[key].sum()    ,					delta=1E-2						)





	def  _lowerCAmelCase    (			self							:					Any						)	->  Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Dict 							=    self.get_image_processor()
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    self.get_tokenizer()

					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    FlavaProcessor(tokenizer=_SCREAMING_SNAKE_CASE    ,					image_processor=_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       List[Any] 							=    'lower newer'

					SCREAMING_SNAKE_CASE	:       List[str] 							=    processor(text=_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       List[str] 							=    tokenizer(_SCREAMING_SNAKE_CASE						)

					for key in encoded_tok.keys():
									self.assertListEqual(encoded_tok[key]    ,					encoded_processor[key]						)





	def  _lowerCAmelCase    (			self							:					Tuple						)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    self.get_image_processor()
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    self.get_tokenizer()

					SCREAMING_SNAKE_CASE	:       str 							=    FlavaProcessor(tokenizer=_SCREAMING_SNAKE_CASE    ,					image_processor=_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    'lower newer'
					SCREAMING_SNAKE_CASE	:       List[str] 							=    self.prepare_image_inputs()

					SCREAMING_SNAKE_CASE	:       List[str] 							=    processor(text=_SCREAMING_SNAKE_CASE    ,					images=_SCREAMING_SNAKE_CASE						)

					self.assertListEqual(list(inputs.keys()						)    ,					['input_ids', 'token_type_ids', 'attention_mask', 'pixel_values']						)

					# add extra args
					SCREAMING_SNAKE_CASE	:       Any 							=    processor(text=_SCREAMING_SNAKE_CASE    ,					images=_SCREAMING_SNAKE_CASE    ,					return_codebook_pixels=_SCREAMING_SNAKE_CASE    ,					return_image_mask=_SCREAMING_SNAKE_CASE						)

					self.assertListEqual(
					    list(inputs.keys()						)    ,					[
					        'input_ids',
					        'token_type_ids',
					        'attention_mask',
					        'pixel_values',
					        'codebook_pixel_values',
					        'bool_masked_pos',
					    ]    ,					)

					# test if it raises when no input is passed
					with pytest.raises(_SCREAMING_SNAKE_CASE						):
									processor()





	def  _lowerCAmelCase    (			self							:					Optional[Any]						)	->  Union[str, Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[str] 							=    self.get_image_processor()
					SCREAMING_SNAKE_CASE	:       Tuple 							=    self.get_tokenizer()

					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    FlavaProcessor(tokenizer=_SCREAMING_SNAKE_CASE    ,					image_processor=_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    processor.batch_decode(_SCREAMING_SNAKE_CASE						)
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    tokenizer.batch_decode(_SCREAMING_SNAKE_CASE						)

					self.assertListEqual(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						)







	def  _lowerCAmelCase    (			self							:					int						)	->  Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    self.get_image_processor()
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    self.get_tokenizer()

					SCREAMING_SNAKE_CASE	:       int 							=    FlavaProcessor(tokenizer=_SCREAMING_SNAKE_CASE    ,					image_processor=_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       Any 							=    'lower newer'
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    self.prepare_image_inputs()

					SCREAMING_SNAKE_CASE	:       Dict 							=    processor(text=_SCREAMING_SNAKE_CASE    ,					images=_SCREAMING_SNAKE_CASE						)

					self.assertListEqual(list(inputs.keys()						)    ,					processor.model_input_names						)





