



"""simple docstring"""






import unittest

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

from ...generation.test_utils import GenerationTesterMixin
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 (
		    LiltForQuestionAnswering,
		    LiltForSequenceClassification,
		    LiltForTokenClassification,
		    LiltModel,
		)
		from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST

class 	__SCREAMING_SNAKE_CASE     :


					'''simple docstring'''





					def __init__(	self							:  List[str],  lowerCamelCase							:  Optional[int],  lowerCamelCase							:  int=13,  lowerCamelCase							:  int=7,  lowerCamelCase							:  Optional[Any]=True,  lowerCamelCase							:  Dict=True,  lowerCamelCase							:  List[str]=True,  lowerCamelCase							:  Optional[int]=True,  lowerCamelCase							:  int=99,  lowerCamelCase							:  List[str]=24,  lowerCamelCase							:  Union[str, Any]=2,  lowerCamelCase							:  Optional[Any]=6,  lowerCamelCase							:  Union[str, Any]=37,  lowerCamelCase							:  Tuple="gelu",  lowerCamelCase							:  Any=0.1,  lowerCamelCase							:  int=0.1,  lowerCamelCase							:  str=512,  lowerCamelCase							:  List[str]=16,  lowerCamelCase							:  List[Any]=2,  lowerCamelCase							:  Dict=0.02,  lowerCamelCase							:  Optional[Any]=3,  lowerCamelCase							:  Any=None,  lowerCamelCase							:  List[str]=1000,  )-> Optional[Any]:
								lowerCamelCase__		:		List[Any]		     =parent
								lowerCamelCase__		:		Dict		     =batch_size
								lowerCamelCase__		:		Union[str, Any]		     =seq_length
								lowerCamelCase__		:		Optional[int]		     =is_training
								lowerCamelCase__		:		Dict		     =use_input_mask
								lowerCamelCase__		:		int		     =use_token_type_ids
								lowerCamelCase__		:		Any		     =use_labels
								lowerCamelCase__		:		str		     =vocab_size
								lowerCamelCase__		:		Optional[Any]		     =hidden_size
								lowerCamelCase__		:		str		     =num_hidden_layers
								lowerCamelCase__		:		Optional[Any]		     =num_attention_heads
								lowerCamelCase__		:		List[str]		     =intermediate_size
								lowerCamelCase__		:		Optional[Any]		     =hidden_act
								lowerCamelCase__		:		Optional[int]		     =hidden_dropout_prob
								lowerCamelCase__		:		Union[str, Any]		     =attention_probs_dropout_prob
								lowerCamelCase__		:		Optional[Any]		     =max_position_embeddings
								lowerCamelCase__		:		List[Any]		     =type_vocab_size
								lowerCamelCase__		:		int		     =type_sequence_label_size
								lowerCamelCase__		:		Optional[Any]		     =initializer_range
								lowerCamelCase__		:		Any		     =num_labels
								lowerCamelCase__		:		Optional[int]		     =scope
								lowerCamelCase__		:		Tuple		     =range_bbox



					def 		snake_case	(	self							:  Any					)-> List[Any]:
								lowerCamelCase__		:		Optional[int]		     =ids_tensor([self.batch_size, self.seq_length],  self.vocab_size					)

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

								lowerCamelCase__		:		Dict		     =None
								if self.use_input_mask:
											lowerCamelCase__		:		Optional[Any]		     =ids_tensor([self.batch_size, self.seq_length],  vocab_size=2					)

								lowerCamelCase__		:		str		     =None
								if self.use_token_type_ids:
											lowerCamelCase__		:		int		     =ids_tensor([self.batch_size, self.seq_length],  self.type_vocab_size					)

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

								lowerCamelCase__		:		Union[str, Any]		     =self.get_config()

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



					def 		snake_case	(	self							:  List[Any]					)-> int:
								return LiltConfig(
								    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 		snake_case	(	self							:  List[Any],  lowerCamelCase							:  Optional[int],  lowerCamelCase							:  List[str],  lowerCamelCase							:  Any,  lowerCamelCase							:  Tuple,  lowerCamelCase							:  Optional[int],  lowerCamelCase							:  List[str],  lowerCamelCase							:  List[Any],  )-> Union[str, Any]:
								lowerCamelCase__		:		Dict		     =LiltModel(config=lowerCamelCase					)
								model.to(lowerCamelCase					)
								model.eval()
								lowerCamelCase__		:		int		     =model(lowerCamelCase,  bbox=lowerCamelCase,  attention_mask=lowerCamelCase,  token_type_ids=lowerCamelCase					)
								lowerCamelCase__		:		Tuple		     =model(lowerCamelCase,  bbox=lowerCamelCase,  token_type_ids=lowerCamelCase					)
								lowerCamelCase__		:		List[str]		     =model(lowerCamelCase,  bbox=lowerCamelCase					)

								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 		snake_case	(	self							:  List[str],  lowerCamelCase							:  Optional[Any],  lowerCamelCase							:  List[str],  lowerCamelCase							:  Any,  lowerCamelCase							:  Optional[int],  lowerCamelCase							:  int,  lowerCamelCase							:  Union[str, Any],  lowerCamelCase							:  str,  )-> List[Any]:
								lowerCamelCase__		:		Optional[int]		     =self.num_labels
								lowerCamelCase__		:		Union[str, Any]		     =LiltForTokenClassification(config=lowerCamelCase					)
								model.to(lowerCamelCase					)
								model.eval()
								lowerCamelCase__		:		List[Any]		     =model(
								    lowerCamelCase,  bbox=lowerCamelCase,  attention_mask=lowerCamelCase,  token_type_ids=lowerCamelCase,  labels=lowerCamelCase					)
								self.parent.assertEqual(result.logits.shape,  (self.batch_size, self.seq_length, self.num_labels)					)



					def 		snake_case	(	self							:  Optional[Any],  lowerCamelCase							:  List[Any],  lowerCamelCase							:  List[Any],  lowerCamelCase							:  Optional[Any],  lowerCamelCase							:  Optional[int],  lowerCamelCase							:  List[Any],  lowerCamelCase							:  Optional[int],  lowerCamelCase							:  str,  )-> Optional[int]:
								lowerCamelCase__		:		int		     =LiltForQuestionAnswering(config=lowerCamelCase					)
								model.to(lowerCamelCase					)
								model.eval()
								lowerCamelCase__		:		Union[str, Any]		     =model(
								    lowerCamelCase,  bbox=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 		snake_case	(	self							:  Union[str, Any]					)-> int:
								lowerCamelCase__		:		int		     =self.prepare_config_and_inputs()
								(
								    (
								    lowerCamelCase__
								)     ,       (
								    lowerCamelCase__
								)     ,       (
								    lowerCamelCase__
								)     ,       (
								    lowerCamelCase__
								)     ,       (
								    lowerCamelCase__
								)     ,       (
								    lowerCamelCase__
								)     ,       (
								    lowerCamelCase__
								)     ,       
								)		:		Dict		     =config_and_inputs
								lowerCamelCase__		:		Tuple		     ={
								    '''input_ids''': input_ids,
								    '''bbox''': bbox,
								    '''token_type_ids''': token_type_ids,
								    '''attention_mask''': input_mask,
								}
								return config, inputs_dict





@require_torch
class 	__SCREAMING_SNAKE_CASE     (				lowerCAmelCase_		,       lowerCAmelCase_		,       lowerCAmelCase_		,       unittest.TestCase       ):


					'''simple docstring'''





					_a             =							(
					    (
					        LiltModel,
					        LiltForSequenceClassification,
					        LiltForTokenClassification,
					        LiltForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					_a             =							(
					    {
					        'feature-extraction': LiltModel,
					        'question-answering': LiltForQuestionAnswering,
					        'text-classification': LiltForSequenceClassification,
					        'token-classification': LiltForTokenClassification,
					        'zero-shot': LiltForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					_a             =							False
					_a             =							False



					def 		snake_case	(	self							:  Optional[Any],  lowerCamelCase							:  Optional[int],  lowerCamelCase							:  Optional[Any],  lowerCamelCase							:  int,  lowerCamelCase							:  List[str],  lowerCamelCase							:  Optional[int]					)-> str:
								return True



					def 		snake_case	(	self							:  int					)-> Union[str, Any]:
								lowerCamelCase__		:		List[str]		     =LiltModelTester(self					)
								lowerCamelCase__		:		Any		     =ConfigTester(self,  config_class=lowerCamelCase,  hidden_size=37					)



					def 		snake_case	(	self							:  str					)-> Optional[int]:
								self.config_tester.run_common_tests()



					def 		snake_case	(	self							:  List[str]					)-> List[str]:
								lowerCamelCase__		:		Any		     =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowerCamelCase					)



					def 		snake_case	(	self							:  Any					)-> int:
								lowerCamelCase__		:		Tuple		     =self.model_tester.prepare_config_and_inputs()
								for type in ["absolute", "relative_key", "relative_key_query"]:
											lowerCamelCase__		:		Optional[Any]		     =type
											self.model_tester.create_and_check_model(*lowerCamelCase					)



					def 		snake_case	(	self							:  str					)-> Optional[int]:
								lowerCamelCase__		:		Dict		     =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*lowerCamelCase					)



					def 		snake_case	(	self							:  Union[str, Any]					)-> List[str]:
								lowerCamelCase__		:		Optional[Any]		     =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*lowerCamelCase					)




					@slow
					def 		snake_case	(	self							:  Tuple					)-> Union[str, Any]:
								for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowerCamelCase__		:		Optional[int]		     =LiltModel.from_pretrained(lowerCamelCase					)
											self.assertIsNotNone(lowerCamelCase					)





@require_torch
@slow
class 	__SCREAMING_SNAKE_CASE     (				unittest.TestCase       ):


					'''simple docstring'''





					def 		snake_case	(	self							:  Dict					)-> List[str]:
								lowerCamelCase__		:		Any		     =LiltModel.from_pretrained('''SCUT-DLVCLab/lilt-roberta-en-base'''					).to(lowerCamelCase					)

								lowerCamelCase__		:		str		     =torch.tensor([[1, 2]],  device=lowerCamelCase					)
								lowerCamelCase__		:		List[Any]		     =torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]],  device=lowerCamelCase					)

								# forward pass
								with torch.no_grad():
											lowerCamelCase__		:		Any		     =model(input_ids=lowerCamelCase,  bbox=lowerCamelCase					)

								lowerCamelCase__		:		int		     =torch.Size([1, 2, 768]					)
								lowerCamelCase__		:		Optional[int]		     =torch.tensor(
								    [[-0.0_653, 0.0_950, -0.0_061], [-0.0_545, 0.0_926, -0.0_324]],  device=lowerCamelCase,  )

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









"""simple docstring"""






from ..utils import DummyObject, requires_backends

class 	__SCREAMING_SNAKE_CASE     (				metaclass=lowerCAmelCase_       ):


					'''simple docstring'''





					_a             =							['torch', 'torchsde']



					def __init__(	self							:  Union[str, Any],  *lowerCamelCase							:  str,  **lowerCamelCase							:  int					)-> Tuple:
								requires_backends(self,  ['''torch''', '''torchsde''']					)



					@classmethod
					def 		snake_case	(	cls							:  List[str],  *lowerCamelCase							:  Optional[Any],  **lowerCamelCase							:  Dict					)-> str:
								requires_backends(cls,  ['''torch''', '''torchsde''']					)




					@classmethod
					def 		snake_case	(	cls							:  Tuple,  *lowerCamelCase							:  Dict,  **lowerCamelCase							:  Tuple					)-> List[str]:
								requires_backends(cls,  ['''torch''', '''torchsde''']					)




