




'''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      A       :







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


														"""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   __lowerCAmelCase     (    self			:    List[Any]   )     ->				Union[str, 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   __lowerCAmelCase     (    self			:    Union[str, Any]   )     ->				Optional[int]:


														"""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   __lowerCAmelCase     (    self			:    List[str]				,     lowerCAmelCase_			:    int				,     lowerCAmelCase_			:    List[Any]				,     lowerCAmelCase_			:    List[str]				,     lowerCAmelCase_			:    Tuple				,     lowerCAmelCase_			:    List[str]				,     lowerCAmelCase_			:    Union[str, Any]				,     lowerCAmelCase_			:    Any				,     )     ->				Optional[int]:


														"""simple docstring"""


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


														"""simple docstring"""


														_a      	=							self.num_labels
														_a      	=							LiltForTokenClassification(config=lowerCAmelCase_   )
														model.to(lowerCAmelCase_   )
														model.eval()
														_a      	=							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   __lowerCAmelCase     (    self			:    Any				,     lowerCAmelCase_			:    Optional[Any]				,     lowerCAmelCase_			:    Dict				,     lowerCAmelCase_			:    Optional[Any]				,     lowerCAmelCase_			:    List[Any]				,     lowerCAmelCase_			:    Any				,     lowerCAmelCase_			:    Union[str, Any]				,     lowerCAmelCase_			:    Union[str, Any]				,     )     ->				Dict:


														"""simple docstring"""


														_a      	=							LiltForQuestionAnswering(config=lowerCAmelCase_   )
														model.to(lowerCAmelCase_   )
														model.eval()
														_a      	=							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   __lowerCAmelCase     (    self			:    Union[str, Any]   )     ->				Tuple:


														"""simple docstring"""


														_a      	=							self.prepare_config_and_inputs()
														(
														    (
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _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      A       (						_a	,_a	,_a	,unittest.TestCase       ):
							lowercase_							     =							(
							    (
							        LiltModel,
							        LiltForSequenceClassification,
							        LiltForTokenClassification,
							        LiltForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							lowercase_							     =							(
							    {
							        'feature-extraction': LiltModel,
							        'question-answering': LiltForQuestionAnswering,
							        'text-classification': LiltForSequenceClassification,
							        'token-classification': LiltForTokenClassification,
							        'zero-shot': LiltForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							lowercase_							     =							False
							lowercase_							     =							False







							def   __lowerCAmelCase     (    self			:    Optional[Any]				,     lowerCAmelCase_			:    int				,     lowerCAmelCase_			:    List[Any]				,     lowerCAmelCase_			:    Optional[int]				,     lowerCAmelCase_			:    Any				,     lowerCAmelCase_			:    List[str]   )     ->				Optional[int]:


														"""simple docstring"""


														return True







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


														"""simple docstring"""


														_a      	=							LiltModelTester(self   )
														_a      	=							ConfigTester(self				,     config_class=lowerCAmelCase_				,     hidden_size=37   )







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


														"""simple docstring"""


														self.config_tester.run_common_tests()







							def   __lowerCAmelCase     (    self			:    List[Any]   )     ->				int:


														"""simple docstring"""


														_a      	=							self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCAmelCase_   )







							def   __lowerCAmelCase     (    self			:    Tuple   )     ->				Tuple:


														"""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(*lowerCAmelCase_   )







							def   __lowerCAmelCase     (    self			:    str   )     ->				Dict:


														"""simple docstring"""


														_a      	=							self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase_   )







							def   __lowerCAmelCase     (    self			:    List[Any]   )     ->				Dict:


														"""simple docstring"""


														_a      	=							self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase_   )







							@slow
							def   __lowerCAmelCase     (    self			:    str   )     ->				List[Any]:


														"""simple docstring"""


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



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







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


														"""simple docstring"""


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

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

														# forward pass
														with torch.no_grad():
																					_a      	=							model(input_ids=lowerCAmelCase_				,     bbox=lowerCAmelCase_   )

														_a      	=							torch.Size([1, 2, 7_68]   )
														_a      	=							torch.tensor(
														    [[-0.0_6_5_3, 0.0_9_5_0, -0.0_0_6_1], [-0.0_5_4_5, 0.0_9_2_6, -0.0_3_2_4]]				,     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 unittest import TestCase

from datasets import Sequence, Value
from datasets.arrow_dataset import Dataset




class  _A (					__lowercase		):

		def 	lowercase__     (   self					:   Any			)					->    str:


					"""simple docstring"""

					return [
					    {"col_1": 3, "col_2": "a"},
					    {"col_1": 2, "col_2": "b"},
					    {"col_1": 1, "col_2": "c"},
					    {"col_1": 0, "col_2": "d"},
					]

		def 	lowercase__     (   self					:   str			)					->    int:


					"""simple docstring"""

					__snake_case					:      Union[str, Any]							      =     {"""col_1""": [3, 2, 1, 0], """col_2""": ["""a""", """b""", """c""", """d"""]}
					return Dataset.from_dict(__magic_name__			)

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


					"""simple docstring"""

					__snake_case					:      Any							      =     self._create_example_records()
					__snake_case					:      str							      =     Dataset.from_list(__magic_name__			)
					self.assertListEqual(dset.column_names  ,      ["""col_1""", """col_2"""]			)
					for i, r in enumerate(__magic_name__			):
								self.assertDictEqual(__magic_name__  ,      example_records[i]			)

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


					"""simple docstring"""

					__snake_case					:      List[Any]							      =     self._create_example_records()
					__snake_case					:      Dict							      =     Dataset.from_list(__magic_name__			)
					__snake_case					:      List[Any]							      =     Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]}			)
					self.assertEqual(dset.info  ,      dset_from_dict.info			)

		def 	lowercase__     (   self					:   str			)					->    List[Any]:  # checks what happens with missing columns


					"""simple docstring"""

					__snake_case					:      Union[str, Any]							      =     [{"""col_1""": 1}, {"""col_2""": """x"""}]
					__snake_case					:      Optional[int]							      =     Dataset.from_list(__magic_name__			)
					self.assertDictEqual(dset[0]  ,      {"""col_1""": 1}			)
					self.assertDictEqual(dset[1]  ,      {"""col_1""": None}			)  # NB: first record is used for columns

		def 	lowercase__     (   self					:   List[str]			)					->    Optional[Any]:  # checks if the type can be inferred from the second record


					"""simple docstring"""

					__snake_case					:      List[Any]							      =     [{"""col_1""": []}, {"""col_1""": [1, 2]}]
					__snake_case					:      int							      =     Dataset.from_list(__magic_name__			)
					self.assertEqual(dset.info.features["""col_1"""]  ,      Sequence(Value("""int64"""			)			)			)

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


					"""simple docstring"""

					__snake_case					:      Tuple							      =     Dataset.from_list([]			)
					self.assertEqual(len(__magic_name__			)  ,      0			)
					self.assertListEqual(dset.column_names  ,      []			)


