






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_				:







				def __init__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase=1_3       ,					__lowerCAmelCase=7       ,					__lowerCAmelCase=True       ,					__lowerCAmelCase=True       ,					__lowerCAmelCase=True       ,					__lowerCAmelCase=True       ,					__lowerCAmelCase=9_9       ,					__lowerCAmelCase=2_4       ,					__lowerCAmelCase=2       ,					__lowerCAmelCase=6       ,					__lowerCAmelCase=3_7       ,					__lowerCAmelCase="gelu"       ,					__lowerCAmelCase=0.1       ,					__lowerCAmelCase=0.1       ,					__lowerCAmelCase=5_1_2       ,					__lowerCAmelCase=1_6       ,					__lowerCAmelCase=2       ,					__lowerCAmelCase=0.02       ,					__lowerCAmelCase=3       ,					__lowerCAmelCase=None       ,					__lowerCAmelCase=1_0_0_0       ,					):
								"""simple docstring"""
								__magic_name__		:List[Any]        =      parent
								__magic_name__		:Union[str, Any]        =      batch_size
								__magic_name__		:Any        =      seq_length
								__magic_name__		:List[Any]        =      is_training
								__magic_name__		:Any        =      use_input_mask
								__magic_name__		:Tuple        =      use_token_type_ids
								__magic_name__		:Any        =      use_labels
								__magic_name__		:Dict        =      vocab_size
								__magic_name__		:Dict        =      hidden_size
								__magic_name__		:Optional[Any]        =      num_hidden_layers
								__magic_name__		:List[Any]        =      num_attention_heads
								__magic_name__		:Any        =      intermediate_size
								__magic_name__		:Dict        =      hidden_act
								__magic_name__		:List[str]        =      hidden_dropout_prob
								__magic_name__		:Tuple        =      attention_probs_dropout_prob
								__magic_name__		:int        =      max_position_embeddings
								__magic_name__		:Any        =      type_vocab_size
								__magic_name__		:Any        =      type_sequence_label_size
								__magic_name__		:Optional[Any]        =      initializer_range
								__magic_name__		:Any        =      num_labels
								__magic_name__		:Dict        =      scope
								__magic_name__		:Dict        =      range_bbox







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:List[str]        =      ids_tensor([self.batch_size, self.seq_length]       ,					self.vocab_size     )

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

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

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

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

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

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







				def 			A	(						self     ):
								"""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       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					):
								"""simple docstring"""
								__magic_name__		:Dict        =      LiltModel(config=__lowerCAmelCase     )
								model.to(__lowerCAmelCase     )
								model.eval()
								__magic_name__		:List[str]        =      model(__lowerCAmelCase       ,					bbox=__lowerCAmelCase       ,					attention_mask=__lowerCAmelCase       ,					token_type_ids=__lowerCAmelCase     )
								__magic_name__		:Any        =      model(__lowerCAmelCase       ,					bbox=__lowerCAmelCase       ,					token_type_ids=__lowerCAmelCase     )
								__magic_name__		:Union[str, Any]        =      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 			A	(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					):
								"""simple docstring"""
								__magic_name__		:List[str]        =      self.num_labels
								__magic_name__		:Optional[int]        =      LiltForTokenClassification(config=__lowerCAmelCase     )
								model.to(__lowerCAmelCase     )
								model.eval()
								__magic_name__		: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 			A	(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					):
								"""simple docstring"""
								__magic_name__		:str        =      LiltForQuestionAnswering(config=__lowerCAmelCase     )
								model.to(__lowerCAmelCase     )
								model.eval()
								__magic_name__		:List[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 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Any        =      self.prepare_config_and_inputs()
								(
								    (
								    __magic_name__
								)     ,				(
								    __magic_name__
								)     ,				(
								    __magic_name__
								)     ,				(
								    __magic_name__
								)     ,				(
								    __magic_name__
								)     ,				(
								    __magic_name__
								)     ,				(
								    __magic_name__
								)     ,				
								)		:Tuple        =      config_and_inputs
								__magic_name__		: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			,				lowerCamelCase			,				unittest.TestCase ):
				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 			A	(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								return True







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:List[str]        =      LiltModelTester(self     )
								__magic_name__		:str        =      ConfigTester(self       ,					config_class=__lowerCAmelCase       ,					hidden_size=3_7     )







				def 			A	(						self     ):
								"""simple docstring"""
								self.config_tester.run_common_tests()







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Tuple        =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__lowerCAmelCase     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Union[str, Any]        =      self.model_tester.prepare_config_and_inputs()
								for type in ["absolute", "relative_key", "relative_key_query"]:
												__magic_name__		:Dict        =      type
												self.model_tester.create_and_check_model(*__lowerCAmelCase     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:List[Any]        =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Dict        =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*__lowerCAmelCase     )







				@slow
				def 			A	(						self     ):
								"""simple docstring"""
								for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												__magic_name__		:str        =      LiltModel.from_pretrained(__lowerCAmelCase     )
												self.assertIsNotNone(__lowerCAmelCase     )







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







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:str        =      LiltModel.from_pretrained('''SCUT-DLVCLab/lilt-roberta-en-base'''     ).to(__lowerCAmelCase     )

								__magic_name__		:str        =      torch.tensor([[1, 2]]       ,					device=__lowerCAmelCase     )
								__magic_name__		:Optional[Any]        =      torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]       ,					device=__lowerCAmelCase     )

								# forward pass
								with torch.no_grad():
												__magic_name__		:Dict        =      model(input_ids=__lowerCAmelCase       ,					bbox=__lowerCAmelCase     )

								__magic_name__		:Any        =      torch.Size([1, 2, 7_6_8]     )
								__magic_name__		:Union[str, Any]        =      torch.tensor(
								    [[-0.0653, 0.0950, -0.0061], [-0.0545, 0.0926, -0.0324]]       ,					device=__lowerCAmelCase       ,					)

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














from __future__ import annotations

def      __lowercase					( snake_case,  snake_case ):







				"""simple docstring"""






				__magic_name__		:Tuple        =      0
				__magic_name__		:Tuple        =      len(snake_case ) - 1

				while i < j:
								if nums[i] + nums[j] == target:
												return [i, j]
								elif nums[i] + nums[j] < target:
												__magic_name__		:str        =      i + 1
								else:
												__magic_name__		:List[Any]        =      j - 1

				return []


if __name__ == "__main__":
					import doctest

					doctest.testmod()
					print(f"{two_pointer([2, 7, 11, 15], 9) = }")






