



'''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      __lowerCAmelCase     :
					"""simple docstring"""






					def __init__(     self					:       List[str]				,					_snake_case					:       Optional[int]				,					_snake_case					:       Union[str, Any]=13				,					_snake_case					:       List[Any]=7				,					_snake_case					:       int=True				,					_snake_case					:       Optional[int]=True				,					_snake_case					:       int=True				,					_snake_case					:       List[Any]=True				,					_snake_case					:       List[Any]=99				,					_snake_case					:       Union[str, Any]=24				,					_snake_case					:       Any=2				,					_snake_case					:       str=6				,					_snake_case					:       Optional[Any]=37				,					_snake_case					:       str="gelu"				,					_snake_case					:       Dict=0.1				,					_snake_case					:       int=0.1				,					_snake_case					:       int=5_12				,					_snake_case					:       Dict=16				,					_snake_case					:       List[Any]=2				,					_snake_case					:       Dict=0.02				,					_snake_case					:       Union[str, Any]=3				,					_snake_case					:       str=None				,					_snake_case					:       str=10_00				,					):






								"""simple docstring"""


								A__       =    parent
								A__       =    batch_size
								A__       =    seq_length
								A__       =    is_training
								A__       =    use_input_mask
								A__       =    use_token_type_ids
								A__       =    use_labels
								A__       =    vocab_size
								A__       =    hidden_size
								A__       =    num_hidden_layers
								A__       =    num_attention_heads
								A__       =    intermediate_size
								A__       =    hidden_act
								A__       =    hidden_dropout_prob
								A__       =    attention_probs_dropout_prob
								A__       =    max_position_embeddings
								A__       =    type_vocab_size
								A__       =    type_sequence_label_size
								A__       =    initializer_range
								A__       =    num_labels
								A__       =    scope
								A__       =    range_bbox



					def     _a							(     self					:       List[Any]					):






								"""simple docstring"""


								A__       =    ids_tensor([self.batch_size, self.seq_length]				,					self.vocab_size					)

								A__       =    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]:
																	A__       =    bbox[i, j, 3]
																	A__       =    bbox[i, j, 1]
																	A__       =    t
														if bbox[i, j, 2] < bbox[i, j, 0]:
																	A__       =    bbox[i, j, 2]
																	A__       =    bbox[i, j, 0]
																	A__       =    t

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

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

								A__       =    None
								A__       =    None
								if self.use_labels:
											A__       =    ids_tensor([self.batch_size]				,					self.type_sequence_label_size					)
											A__       =    ids_tensor([self.batch_size, self.seq_length]				,					self.num_labels					)

								A__       =    self.get_config()

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



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






								"""simple docstring"""


								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					:       List[Any]				,					_snake_case					:       Dict				,					_snake_case					:       List[Any]				,					_snake_case					:       int				,					_snake_case					:       Tuple				,					_snake_case					:       str				,					_snake_case					:       str				,					_snake_case					:       Dict				,					):






								"""simple docstring"""


								A__       =    LiltModel(config=UpperCamelCase__					)
								model.to(UpperCamelCase__					)
								model.eval()
								A__       =    model(UpperCamelCase__				,					bbox=UpperCamelCase__				,					attention_mask=UpperCamelCase__				,					token_type_ids=UpperCamelCase__					)
								A__       =    model(UpperCamelCase__				,					bbox=UpperCamelCase__				,					token_type_ids=UpperCamelCase__					)
								A__       =    model(UpperCamelCase__				,					bbox=UpperCamelCase__					)

								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					:       Any				,					_snake_case					:       List[Any]				,					_snake_case					:       int				,					_snake_case					:       List[str]				,					_snake_case					:       List[Any]				,					_snake_case					:       str				,					_snake_case					:       Optional[Any]				,					_snake_case					:       Any				,					):






								"""simple docstring"""


								A__       =    self.num_labels
								A__       =    LiltForTokenClassification(config=UpperCamelCase__					)
								model.to(UpperCamelCase__					)
								model.eval()
								A__       =    model(
								    UpperCamelCase__				,					bbox=UpperCamelCase__				,					attention_mask=UpperCamelCase__				,					token_type_ids=UpperCamelCase__				,					labels=UpperCamelCase__					)
								self.parent.assertEqual(result.logits.shape				,					(self.batch_size, self.seq_length, self.num_labels)					)



					def     _a							(     self					:       Optional[Any]				,					_snake_case					:       List[Any]				,					_snake_case					:       Tuple				,					_snake_case					:       List[str]				,					_snake_case					:       Optional[int]				,					_snake_case					:       Union[str, Any]				,					_snake_case					:       str				,					_snake_case					:       Dict				,					):






								"""simple docstring"""


								A__       =    LiltForQuestionAnswering(config=UpperCamelCase__					)
								model.to(UpperCamelCase__					)
								model.eval()
								A__       =    model(
								    UpperCamelCase__				,					bbox=UpperCamelCase__				,					attention_mask=UpperCamelCase__				,					token_type_ids=UpperCamelCase__				,					start_positions=UpperCamelCase__				,					end_positions=UpperCamelCase__				,					)
								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					:       str					):






								"""simple docstring"""


								A__       =    self.prepare_config_and_inputs()
								(
								    A__						
								)       =    config_and_inputs
								A__       =    {
								    '''input_ids''': input_ids,
								    '''bbox''': bbox,
								    '''token_type_ids''': token_type_ids,
								    '''attention_mask''': input_mask,
								}
								return config, inputs_dict







@require_torch
class      __lowerCAmelCase     (		UpperCamelCase_							,     UpperCamelCase_							,     UpperCamelCase_							,     unittest.TestCase      ):
					"""simple docstring"""






					A__				:		Dict			       =    (
					    (
					        LiltModel,
					        LiltForSequenceClassification,
					        LiltForTokenClassification,
					        LiltForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					A__				:		Optional[int]			       =    (
					    {
					        "feature-extraction": LiltModel,
					        "question-answering": LiltForQuestionAnswering,
					        "text-classification": LiltForSequenceClassification,
					        "token-classification": LiltForTokenClassification,
					        "zero-shot": LiltForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					A__				:		int			       =    False
					A__				:		Union[str, Any]			       =    False



					def     _a							(     self					:       List[Any]				,					_snake_case					:       List[Any]				,					_snake_case					:       Optional[int]				,					_snake_case					:       str				,					_snake_case					:       List[str]				,					_snake_case					:       List[str]					):






								"""simple docstring"""


								return True



					def     _a							(     self					:       List[str]					):






								"""simple docstring"""


								A__       =    LiltModelTester(self					)
								A__       =    ConfigTester(self				,					config_class=UpperCamelCase__				,					hidden_size=37					)



					def     _a							(     self					:       Any					):






								"""simple docstring"""


								self.config_tester.run_common_tests()



					def     _a							(     self					:       Dict					):






								"""simple docstring"""


								A__       =    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*UpperCamelCase__					)



					def     _a							(     self					:       List[Any]					):






								"""simple docstring"""


								A__       =    self.model_tester.prepare_config_and_inputs()
								for type in ["absolute", "relative_key", "relative_key_query"]:
											A__       =    type
											self.model_tester.create_and_check_model(*UpperCamelCase__					)



					def     _a							(     self					:       Dict					):






								"""simple docstring"""


								A__       =    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__					)



					def     _a							(     self					:       Dict					):






								"""simple docstring"""


								A__       =    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__					)



					@slow
					def     _a							(     self					:       str					):






								"""simple docstring"""


								for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											A__       =    LiltModel.from_pretrained(UpperCamelCase__					)
											self.assertIsNotNone(UpperCamelCase__					)







@require_torch
@slow
class      __lowerCAmelCase     (		unittest.TestCase      ):
					"""simple docstring"""






					def     _a							(     self					:       str					):






								"""simple docstring"""


								A__       =    LiltModel.from_pretrained('SCUT-DLVCLab/lilt-roberta-en-base'					).to(UpperCamelCase__					)

								A__       =    torch.tensor([[1, 2]]				,					device=UpperCamelCase__					)
								A__       =    torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]				,					device=UpperCamelCase__					)

								# forward pass
								with torch.no_grad():
											A__       =    model(input_ids=UpperCamelCase__				,					bbox=UpperCamelCase__					)

								A__       =    torch.Size([1, 2, 7_68]					)
								A__       =    torch.tensor(
								    [[-0.0653, 0.0950, -0.0061], [-0.0545, 0.0926, -0.0324]]				,					device=UpperCamelCase__				,					)

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









import pytest

import datasets


# Import fixture modules as plugins
SCREAMING_SNAKE_CASE__       =      ['''tests.fixtures.files''', '''tests.fixtures.hub''', '''tests.fixtures.fsspec''']


def A						(       __UpperCamelCase ,	__UpperCamelCase					)						->      Optional[int]:
			# Mark tests as "unit" by default if not marked as "integration" (or already marked as "unit")
			for item in items:
						if any(marker in item.keywords for marker in ['integration', 'unit']					):
									continue
						item.add_marker(pytest.mark.unit					)


def A						(       __UpperCamelCase					)						->      str:
			config.addinivalue_line('markers' ,	'torchaudio_latest: mark test to run with torchaudio>=0.12'					)


@pytest.fixture(autouse=__UpperCamelCase					)
def A						(       __UpperCamelCase ,	__UpperCamelCase					)						->      Union[str, Any]:
			# test_hf_cache_home = tmp_path_factory.mktemp("cache")  # TODO: why a cache dir per test function does not work?
			A__       =    tmp_path_factory.getbasetemp() / 'cache'
			A__       =    test_hf_cache_home / 'datasets'
			A__       =    test_hf_cache_home / 'metrics'
			A__       =    test_hf_cache_home / 'modules'
			monkeypatch.setattr('datasets.config.HF_DATASETS_CACHE' ,	str(__UpperCamelCase					)					)
			monkeypatch.setattr('datasets.config.HF_METRICS_CACHE' ,	str(__UpperCamelCase					)					)
			monkeypatch.setattr('datasets.config.HF_MODULES_CACHE' ,	str(__UpperCamelCase					)					)
			A__       =    test_hf_datasets_cache / 'downloads'
			monkeypatch.setattr('datasets.config.DOWNLOADED_DATASETS_PATH' ,	str(__UpperCamelCase					)					)
			A__       =    test_hf_datasets_cache / 'downloads' / 'extracted'
			monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_PATH' ,	str(__UpperCamelCase					)					)


@pytest.fixture(autouse=__UpperCamelCase ,	scope='session'					)
def A						(       )						->      Union[str, Any]:
			datasets.disable_progress_bar()


@pytest.fixture(autouse=__UpperCamelCase					)
def A						(       __UpperCamelCase					)						->      int:
			# don't take tests into account when counting downloads
			monkeypatch.setattr('datasets.config.HF_UPDATE_DOWNLOAD_COUNTS' ,	__UpperCamelCase					)






@pytest.fixture
def A						(       __UpperCamelCase					)						->      Any:
			# Required to suppress RemovedIn20Warning when feature(s) are not compatible with SQLAlchemy 2.0
			# To be removed once SQLAlchemy 2.0 supported
			monkeypatch.setattr('sqlalchemy.util.deprecations.SILENCE_UBER_WARNING' ,	__UpperCamelCase					)




