
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		,     _snake_case		,     _snake_case=13		,     _snake_case=7		,     _snake_case=True		,     _snake_case=True		,     _snake_case=True		,     _snake_case=True		,     _snake_case=99		,     _snake_case=24		,     _snake_case=2		,     _snake_case=6		,     _snake_case=37		,     _snake_case="gelu"		,     _snake_case=0.1		,     _snake_case=0.1		,     _snake_case=512		,     _snake_case=16		,     _snake_case=2		,     _snake_case=0.02		,     _snake_case=3		,     _snake_case=None		,     _snake_case=1_000		,     )    ->      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 				SCREAMING_SNAKE_CASE_		(     self				)    ->      int:


								'''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 				SCREAMING_SNAKE_CASE_		(     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 				SCREAMING_SNAKE_CASE_		(     self		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     )    ->      Dict:


								'''simple docstring'''




								__a										=    LiltModel(config=__SCREAMING_SNAKE_CASE				)
								model.to(__SCREAMING_SNAKE_CASE				)
								model.eval()
								__a										=    model(__SCREAMING_SNAKE_CASE		,     bbox=__SCREAMING_SNAKE_CASE		,     attention_mask=__SCREAMING_SNAKE_CASE		,     token_type_ids=__SCREAMING_SNAKE_CASE				)
								__a										=    model(__SCREAMING_SNAKE_CASE		,     bbox=__SCREAMING_SNAKE_CASE		,     token_type_ids=__SCREAMING_SNAKE_CASE				)
								__a										=    model(__SCREAMING_SNAKE_CASE		,     bbox=__SCREAMING_SNAKE_CASE				)

								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 				SCREAMING_SNAKE_CASE_		(     self		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     )    ->      Union[str, Any]:


								'''simple docstring'''




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




				def 				SCREAMING_SNAKE_CASE_		(     self		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     )    ->      Optional[int]:


								'''simple docstring'''




								__a										=    LiltForQuestionAnswering(config=__SCREAMING_SNAKE_CASE				)
								model.to(__SCREAMING_SNAKE_CASE				)
								model.eval()
								__a										=    model(
								    __SCREAMING_SNAKE_CASE		,     bbox=__SCREAMING_SNAKE_CASE		,     attention_mask=__SCREAMING_SNAKE_CASE		,     token_type_ids=__SCREAMING_SNAKE_CASE		,     start_positions=__SCREAMING_SNAKE_CASE		,     end_positions=__SCREAMING_SNAKE_CASE		,     )
								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 				SCREAMING_SNAKE_CASE_		(     self				)    ->      List[Any]:


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




				def 				SCREAMING_SNAKE_CASE_		(     self		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case				)    ->      int:


								'''simple docstring'''




								return True




				def 				SCREAMING_SNAKE_CASE_		(     self				)    ->      str:


								'''simple docstring'''




								__a										=    LiltModelTester(self				)
								__a										=    ConfigTester(self		,     config_class=__SCREAMING_SNAKE_CASE		,     hidden_size=37				)




				def 				SCREAMING_SNAKE_CASE_		(     self				)    ->      Optional[Any]:


								'''simple docstring'''




								self.config_tester.run_common_tests()




				def 				SCREAMING_SNAKE_CASE_		(     self				)    ->      Tuple:


								'''simple docstring'''




								__a										=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE				)




				def 				SCREAMING_SNAKE_CASE_		(     self				)    ->      List[str]:


								'''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(*__SCREAMING_SNAKE_CASE				)




				def 				SCREAMING_SNAKE_CASE_		(     self				)    ->      List[str]:


								'''simple docstring'''




								__a										=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE				)




				def 				SCREAMING_SNAKE_CASE_		(     self				)    ->      Dict:


								'''simple docstring'''




								__a										=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE				)




				@slow
				def 				SCREAMING_SNAKE_CASE_		(     self				)    ->      Union[str, Any]:


								'''simple docstring'''




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







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




				def 				SCREAMING_SNAKE_CASE_		(     self				)    ->      str:


								'''simple docstring'''




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

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

								# forward pass
								with torch.no_grad():
												__a										=    model(input_ids=__SCREAMING_SNAKE_CASE		,     bbox=__SCREAMING_SNAKE_CASE				)

								__a										=    torch.Size([1, 2, 768]				)
								__a										=    torch.tensor(
								    [[-0.0653, 0.0950, -0.0061], [-0.0545, 0.0926, -0.0324]]		,     device=__SCREAMING_SNAKE_CASE		,     )

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




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


__snake_case						:Union[str, Any]      		=     logging.get_logger(__name__)

__snake_case						:Any      		=     {
    '''google/switch-base-8''': '''https://huggingface.co/google/switch-base-8/blob/main/config.json''',
}

class     _A       (	__UpperCAmelCase    ):
       UpperCamelCase__  :    Optional[int]											=						'''switch_transformers'''
       UpperCamelCase__  :    Optional[Any]											=						['''past_key_values''']
       UpperCamelCase__  :    Optional[Any]											=						{'''hidden_size''': '''d_model''', '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers'''}



       def __init__(		self							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     str=32_128     ,		__SCREAMING_SNAKE_CASE							:     int=768     ,		__SCREAMING_SNAKE_CASE							:     Any=64     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]=2_048     ,		__SCREAMING_SNAKE_CASE							:     List[str]=64     ,		__SCREAMING_SNAKE_CASE							:     int=12     ,		__SCREAMING_SNAKE_CASE							:     Any=3     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]=12     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]=3     ,		__SCREAMING_SNAKE_CASE							:     Any=12     ,		__SCREAMING_SNAKE_CASE							:     Tuple=8     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]=False     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]=0.01     ,		__SCREAMING_SNAKE_CASE							:     Dict="float32"     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]=False     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]=32     ,		__SCREAMING_SNAKE_CASE							:     int=128     ,		__SCREAMING_SNAKE_CASE							:     Any=0.1     ,		__SCREAMING_SNAKE_CASE							:     int=1E-6     ,		__SCREAMING_SNAKE_CASE							:     Dict=0.0_01     ,		__SCREAMING_SNAKE_CASE							:     List[str]=0.0_01     ,		__SCREAMING_SNAKE_CASE							:     List[Any]=1.0     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]="relu"     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]=True     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]=False     ,		__SCREAMING_SNAKE_CASE							:     str=True     ,		__SCREAMING_SNAKE_CASE							:     int=0     ,		__SCREAMING_SNAKE_CASE							:     List[Any]=1     ,		**__SCREAMING_SNAKE_CASE							:     Dict     ,		):


           '''simple docstring'''

           __a						       =				vocab_size
           __a						       =				d_model
           __a						       =				d_kv
           __a						       =				d_ff

           __a						       =				num_sparse_encoder_layers

           __a						       =				num_layers
           __a						       =				(
               num_decoder_layers if num_decoder_layers is not None else self.num_layers
           )  # default = symmetry
           __a						       =				num_sparse_decoder_layers

           # This tells us, each how many encoder layer we'll have to set a sparse layer.
           if self.num_sparse_encoder_layers > 0:
               __a						       =				self.num_layers // self.num_sparse_encoder_layers
           else:
               __a						       =				self.num_layers  # HACK: this will create 0 sparse layers

           # This tells us, each how many encoder layer we'll have to set a sparse layer.
           if self.num_sparse_decoder_layers > 0:
               __a						       =				self.num_decoder_layers // self.num_sparse_decoder_layers
           else:
               __a						       =				self.num_decoder_layers  # HACK: this will create 0 sparse layers

           __a						       =				num_heads
           __a						       =				num_experts
           __a						       =				expert_capacity
           __a						       =				router_bias
           __a						       =				router_jitter_noise
           if router_dtype not in ["float32", "float16", "bfloat16"]:
               raise ValueError(F'`router_dtype` must be one of \'float32\', \'float16\' or \'bfloat16\', got {router_dtype}')
           __a						       =				router_dtype

           __a						       =				router_ignore_padding_tokens
           __a						       =				relative_attention_num_buckets
           __a						       =				relative_attention_max_distance

           __a						       =				dropout_rate
           __a						       =				layer_norm_epsilon
           __a						       =				initializer_factor
           __a						       =				feed_forward_proj
           __a						       =				use_cache
           __a						       =				add_router_probs

           __a						       =				router_z_loss_coef
           __a						       =				router_aux_loss_coef

           __a						       =				self.feed_forward_proj.split('''-''')
           __a						       =				act_info[-1]
           __a						       =				act_info[0] == '''gated'''

           if len(__SCREAMING_SNAKE_CASE) > 1 and act_info[0] != "gated" or len(__SCREAMING_SNAKE_CASE) > 2:
               raise ValueError(
                   F'`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.'
                   '''Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. '''
                   '''\'gated-gelu\' or \'relu\'''')

           # for backwards compatibility
           if feed_forward_proj == "gated-gelu":
               __a						       =				'''gelu_new'''

           super().__init__(
               pad_token_id=__SCREAMING_SNAKE_CASE     ,		eos_token_id=__SCREAMING_SNAKE_CASE     ,		is_encoder_decoder=__SCREAMING_SNAKE_CASE     ,		**__SCREAMING_SNAKE_CASE     ,		)





