






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




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




					def   A  (				self     :						Optional[Any]   )  ->  Dict:
							lowercase_ : Tuple							       =   ids_tensor([self.batch_size, self.seq_length]    ,						self.vocab_size   )

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

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

							lowercase_ : Dict							       =   None
							if self.use_token_type_ids:
									lowercase_ : Union[str, Any]							       =   ids_tensor([self.batch_size, self.seq_length]    ,						self.type_vocab_size   )

							lowercase_ : Any							       =   None
							lowercase_ : Optional[int]							       =   None
							if self.use_labels:
									lowercase_ : List[Any]							       =   ids_tensor([self.batch_size]    ,						self.type_sequence_label_size   )
									lowercase_ : Dict							       =   ids_tensor([self.batch_size, self.seq_length]    ,						self.num_labels   )

							lowercase_ : Tuple							       =   self.get_config()

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




					def   A  (				self     :						int   )  ->  Optional[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   A  (				self     :						str    ,						A     :						str    ,						A     :						int    ,						A     :						Dict    ,						A     :						Optional[Any]    ,						A     :						Any    ,						A     :						Optional[int]    ,						A     :						int    ,						)  ->  Tuple:
							lowercase_ : str							       =   LiltModel(config=A   )
							model.to(A   )
							model.eval()
							lowercase_ : Any							       =   model(A    ,						bbox=A    ,						attention_mask=A    ,						token_type_ids=A   )
							lowercase_ : Dict							       =   model(A    ,						bbox=A    ,						token_type_ids=A   )
							lowercase_ : Any							       =   model(A    ,						bbox=A   )

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




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





@require_torch
class 					_UpperCAmelCase       ( _A   ,						_A   ,						_A   ,						unittest.TestCase							):
					SCREAMING_SNAKE_CASE_		:				Union[str, Any]							    =   (
					    (
					        LiltModel,
					        LiltForSequenceClassification,
					        LiltForTokenClassification,
					        LiltForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					SCREAMING_SNAKE_CASE_		:				Dict							    =   (
					    {
					        "feature-extraction": LiltModel,
					        "question-answering": LiltForQuestionAnswering,
					        "text-classification": LiltForSequenceClassification,
					        "token-classification": LiltForTokenClassification,
					        "zero-shot": LiltForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					SCREAMING_SNAKE_CASE_		:				Any							    =   False
					SCREAMING_SNAKE_CASE_		:				Tuple							    =   False




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




					def   A  (				self     :						Tuple   )  ->  List[Any]:
							lowercase_ : Dict							       =   LiltModelTester(self   )
							lowercase_ : Tuple							       =   ConfigTester(self    ,						config_class=A    ,						hidden_size=37   )




					def   A  (				self     :						Any   )  ->  int:
							self.config_tester.run_common_tests()




					def   A  (				self     :						Dict   )  ->  Tuple:
							lowercase_ : Tuple							       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*A   )




					def   A  (				self     :						str   )  ->  Dict:
							lowercase_ : List[str]							       =   self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
									lowercase_ : Dict							       =   type
									self.model_tester.create_and_check_model(*A   )




					def   A  (				self     :						Any   )  ->  List[str]:
							lowercase_ : str							       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*A   )




					def   A  (				self     :						List[Any]   )  ->  Any:
							lowercase_ : Tuple							       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*A   )






					@slow
					def   A  (				self     :						int   )  ->  str:
							for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowercase_ : List[Any]							       =   LiltModel.from_pretrained(A   )
									self.assertIsNotNone(A   )





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




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

							lowercase_ : int							       =   torch.tensor([[1, 2]]    ,						device=A   )
							lowercase_ : Any							       =   torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]    ,						device=A   )

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

							lowercase_ : Optional[int]							       =   torch.Size([1, 2, 7_68]   )
							lowercase_ : str							       =   torch.tensor(
							    [[-0.0653, 0.0950, -0.0061], [-0.0545, 0.0926, -0.0324]]    ,						device=A    ,						)

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











"""simple docstring"""



__A		:       Any  				=		{
    '''Pillow''': '''Pillow''',
    '''accelerate''': '''accelerate>=0.11.0''',
    '''compel''': '''compel==0.1.8''',
    '''black''': '''black~=23.1''',
    '''datasets''': '''datasets''',
    '''filelock''': '''filelock''',
    '''flax''': '''flax>=0.4.1''',
    '''hf-doc-builder''': '''hf-doc-builder>=0.3.0''',
    '''huggingface-hub''': '''huggingface-hub>=0.13.2''',
    '''requests-mock''': '''requests-mock==1.10.0''',
    '''importlib_metadata''': '''importlib_metadata''',
    '''invisible-watermark''': '''invisible-watermark''',
    '''isort''': '''isort>=5.5.4''',
    '''jax''': '''jax>=0.2.8,!=0.3.2''',
    '''jaxlib''': '''jaxlib>=0.1.65''',
    '''Jinja2''': '''Jinja2''',
    '''k-diffusion''': '''k-diffusion>=0.0.12''',
    '''torchsde''': '''torchsde''',
    '''note_seq''': '''note_seq''',
    '''librosa''': '''librosa''',
    '''numpy''': '''numpy''',
    '''omegaconf''': '''omegaconf''',
    '''parameterized''': '''parameterized''',
    '''protobuf''': '''protobuf>=3.20.3,<4''',
    '''pytest''': '''pytest''',
    '''pytest-timeout''': '''pytest-timeout''',
    '''pytest-xdist''': '''pytest-xdist''',
    '''ruff''': '''ruff>=0.0.241''',
    '''safetensors''': '''safetensors''',
    '''sentencepiece''': '''sentencepiece>=0.1.91,!=0.1.92''',
    '''scipy''': '''scipy''',
    '''onnx''': '''onnx''',
    '''regex''': '''regex!=2019.12.17''',
    '''requests''': '''requests''',
    '''tensorboard''': '''tensorboard''',
    '''torch''': '''torch>=1.4''',
    '''torchvision''': '''torchvision''',
    '''transformers''': '''transformers>=4.25.1''',
    '''urllib3''': '''urllib3<=2.0.0''',
}



