






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




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




							def 	lowerCamelCase_       (							self:       Any  ):
														lowerCamelCase__      :			Optional[int] 						= ids_tensor([self.batch_size, self.seq_length]    ,							self.vocab_size  )

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

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

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

														lowerCamelCase__      :			List[Any] 						= None
														lowerCamelCase__      :			Dict 						= None
														if self.use_labels:
																					lowerCamelCase__      :			List[Any] 						= ids_tensor([self.batch_size]    ,							self.type_sequence_label_size  )
																					lowerCamelCase__      :			List[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 	lowerCamelCase_       (							self:       Dict  ):
														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:       int    ,							UpperCamelCase__:       Union[str, Any]    ,							UpperCamelCase__:       Tuple    ,							UpperCamelCase__:       Optional[Any]    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       str    ,							UpperCamelCase__:       int    ,							UpperCamelCase__:       str    ,							):
														lowerCamelCase__      :			List[str] 						= LiltModel(config=__UpperCamelCase  )
														model.to(__UpperCamelCase  )
														model.eval()
														lowerCamelCase__      :			Tuple 						= model(__UpperCamelCase    ,							bbox=__UpperCamelCase    ,							attention_mask=__UpperCamelCase    ,							token_type_ids=__UpperCamelCase  )
														lowerCamelCase__      :			List[str] 						= model(__UpperCamelCase    ,							bbox=__UpperCamelCase    ,							token_type_ids=__UpperCamelCase  )
														lowerCamelCase__      :			Union[str, Any] 						= 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 	lowerCamelCase_       (							self:       Tuple    ,							UpperCamelCase__:       Dict    ,							UpperCamelCase__:       int    ,							UpperCamelCase__:       str    ,							UpperCamelCase__:       Optional[int]    ,							UpperCamelCase__:       Dict    ,							UpperCamelCase__:       Union[str, Any]    ,							UpperCamelCase__:       Any    ,							):
														lowerCamelCase__      :			int 						= self.num_labels
														lowerCamelCase__      :			Dict 						= LiltForTokenClassification(config=__UpperCamelCase  )
														model.to(__UpperCamelCase  )
														model.eval()
														lowerCamelCase__      :			Union[str, Any] 						= 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 	lowerCamelCase_       (							self:       Tuple    ,							UpperCamelCase__:       str    ,							UpperCamelCase__:       int    ,							UpperCamelCase__:       Any    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       Any    ,							UpperCamelCase__:       Optional[int]    ,							):
														lowerCamelCase__      :			int 						= LiltForQuestionAnswering(config=__UpperCamelCase  )
														model.to(__UpperCamelCase  )
														model.eval()
														lowerCamelCase__      :			int 						= 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 	lowerCamelCase_       (							self:       Union[str, Any]  ):
														lowerCamelCase__      :			Dict 						= self.prepare_config_and_inputs()
														(
														    (
														    lowerCamelCase__   
														)						,	(
														    lowerCamelCase__   
														)						,	(
														    lowerCamelCase__   
														)						,	(
														    lowerCamelCase__   
														)						,	(
														    lowerCamelCase__   
														)						,	(
														    lowerCamelCase__   
														)						,	(
														    lowerCamelCase__   
														)						,	   
														)      :			Union[str, Any] 						= config_and_inputs
														lowerCamelCase__      :			int 						= {
														    """input_ids""": input_ids,
														    """bbox""": bbox,
														    """token_type_ids""": token_type_ids,
														    """attention_mask""": input_mask,
														}
														return config, inputs_dict



@require_torch
class _lowercase			(       _lowercase       ,   _lowercase       ,   _lowercase       ,   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 	lowerCamelCase_       (							self:       str    ,							UpperCamelCase__:       Any    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       Optional[Any]    ,							UpperCamelCase__:       Tuple  ):
														return True




							def 	lowerCamelCase_       (							self:       Optional[int]  ):
														lowerCamelCase__      :			Any 						= LiltModelTester(self  )
														lowerCamelCase__      :			Dict 						= ConfigTester(self    ,							config_class=__UpperCamelCase    ,							hidden_size=37  )




							def 	lowerCamelCase_       (							self:       List[Any]  ):
														self.config_tester.run_common_tests()




							def 	lowerCamelCase_       (							self:       int  ):
														lowerCamelCase__      :			Tuple 						= self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*__UpperCamelCase  )




							def 	lowerCamelCase_       (							self:       str  ):
														lowerCamelCase__      :			Optional[Any] 						= self.model_tester.prepare_config_and_inputs()
														for type in ["absolute", "relative_key", "relative_key_query"]:
																					lowerCamelCase__      :			Dict 						= type
																					self.model_tester.create_and_check_model(*__UpperCamelCase  )




							def 	lowerCamelCase_       (							self:       Optional[Any]  ):
														lowerCamelCase__      :			int 						= self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_token_classification(*__UpperCamelCase  )




							def 	lowerCamelCase_       (							self:       Optional[int]  ):
														lowerCamelCase__      :			Any 						= self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_question_answering(*__UpperCamelCase  )




							@slow
							def 	lowerCamelCase_       (							self:       Optional[Any]  ):
														for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCamelCase__      :			List[Any] 						= LiltModel.from_pretrained(__UpperCamelCase  )
																					self.assertIsNotNone(__UpperCamelCase  )



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




							def 	lowerCamelCase_       (							self:       Optional[int]  ):
														lowerCamelCase__      :			str 						= LiltModel.from_pretrained("""SCUT-DLVCLab/lilt-roberta-en-base"""  ).to(__UpperCamelCase  )

														lowerCamelCase__      :			Union[str, Any] 						= torch.tensor([[1, 2]]    ,							device=__UpperCamelCase  )
														lowerCamelCase__      :			Optional[int] 						= torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]    ,							device=__UpperCamelCase  )

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

														lowerCamelCase__      :			List[str] 						= 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=__UpperCamelCase    ,							)

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









'''simple docstring'''







from ...configuration_utils import PretrainedConfig
from ...utils import logging


_A			:    str					  =logging.get_logger(__name__)

_A			:    int					  ={
    '''weiweishi/roc-bert-base-zh''': '''https://huggingface.co/weiweishi/roc-bert-base-zh/resolve/main/config.json''',
}

class _lowercase			(       _lowercase	):
							a          = """roc_bert"""




							def __init__(							self:       Optional[Any]    ,							UpperCamelCase__:       Any=30_522    ,							UpperCamelCase__:       Optional[Any]=768    ,							UpperCamelCase__:       Union[str, Any]=12    ,							UpperCamelCase__:       Tuple=12    ,							UpperCamelCase__:       Tuple=3_072    ,							UpperCamelCase__:       str="gelu"    ,							UpperCamelCase__:       List[Any]=0.1    ,							UpperCamelCase__:       List[str]=0.1    ,							UpperCamelCase__:       Dict=512    ,							UpperCamelCase__:       str=2    ,							UpperCamelCase__:       str=0.02    ,							UpperCamelCase__:       Tuple=1e-12    ,							UpperCamelCase__:       Any=True    ,							UpperCamelCase__:       Union[str, Any]=0    ,							UpperCamelCase__:       List[Any]="absolute"    ,							UpperCamelCase__:       Any=None    ,							UpperCamelCase__:       Any=True    ,							UpperCamelCase__:       Optional[int]=True    ,							UpperCamelCase__:       Union[str, Any]=768    ,							UpperCamelCase__:       int=910    ,							UpperCamelCase__:       Tuple=512    ,							UpperCamelCase__:       int=24_858    ,							UpperCamelCase__:       Optional[Any]=True    ,							**UpperCamelCase__:       Optional[Any]    ,							):
														lowerCamelCase__      :			Optional[Any] 						= vocab_size
														lowerCamelCase__      :			Tuple 						= max_position_embeddings
														lowerCamelCase__      :			List[Any] 						= hidden_size
														lowerCamelCase__      :			int 						= num_hidden_layers
														lowerCamelCase__      :			Tuple 						= num_attention_heads
														lowerCamelCase__      :			Any 						= intermediate_size
														lowerCamelCase__      :			List[str] 						= hidden_act
														lowerCamelCase__      :			str 						= hidden_dropout_prob
														lowerCamelCase__      :			Dict 						= attention_probs_dropout_prob
														lowerCamelCase__      :			Union[str, Any] 						= initializer_range
														lowerCamelCase__      :			Tuple 						= type_vocab_size
														lowerCamelCase__      :			Optional[Any] 						= layer_norm_eps
														lowerCamelCase__      :			List[Any] 						= use_cache
														lowerCamelCase__      :			Tuple 						= enable_pronunciation
														lowerCamelCase__      :			Union[str, Any] 						= enable_shape
														lowerCamelCase__      :			Union[str, Any] 						= pronunciation_embed_dim
														lowerCamelCase__      :			Any 						= pronunciation_vocab_size
														lowerCamelCase__      :			int 						= shape_embed_dim
														lowerCamelCase__      :			Tuple 						= shape_vocab_size
														lowerCamelCase__      :			Optional[Any] 						= concat_input
														lowerCamelCase__      :			str 						= position_embedding_type
														lowerCamelCase__      :			Dict 						= classifier_dropout
														super().__init__(pad_token_id=UpperCamelCase__    ,							**UpperCamelCase__  )

