
"""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  snake_case     :

							def __init__(self       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_=13       ,       SCREAMING_SNAKE_CASE_=7       ,       SCREAMING_SNAKE_CASE_=True       ,       SCREAMING_SNAKE_CASE_=True       ,       SCREAMING_SNAKE_CASE_=True       ,       SCREAMING_SNAKE_CASE_=True       ,       SCREAMING_SNAKE_CASE_=99       ,       SCREAMING_SNAKE_CASE_=24       ,       SCREAMING_SNAKE_CASE_=2       ,       SCREAMING_SNAKE_CASE_=6       ,       SCREAMING_SNAKE_CASE_=37       ,       SCREAMING_SNAKE_CASE_="gelu"       ,       SCREAMING_SNAKE_CASE_=0.1       ,       SCREAMING_SNAKE_CASE_=0.1       ,       SCREAMING_SNAKE_CASE_=5_12       ,       SCREAMING_SNAKE_CASE_=16       ,       SCREAMING_SNAKE_CASE_=2       ,       SCREAMING_SNAKE_CASE_=0.02       ,       SCREAMING_SNAKE_CASE_=3       ,       SCREAMING_SNAKE_CASE_=None       ,       SCREAMING_SNAKE_CASE_=10_00       ,       ):

									"""simple docstring"""







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

							def _lowercase					(self    ):

									"""simple docstring"""







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

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

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

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

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

									SCREAMING_SNAKE_CASE_						 = self.get_config()

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

							def _lowercase					(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 _lowercase					(self       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = LiltModel(config=SCREAMING_SNAKE_CASE_    )
									model.to(SCREAMING_SNAKE_CASE_    )
									model.eval()
									SCREAMING_SNAKE_CASE_						 = model(SCREAMING_SNAKE_CASE_       ,       bbox=SCREAMING_SNAKE_CASE_       ,       attention_mask=SCREAMING_SNAKE_CASE_       ,       token_type_ids=SCREAMING_SNAKE_CASE_    )
									SCREAMING_SNAKE_CASE_						 = model(SCREAMING_SNAKE_CASE_       ,       bbox=SCREAMING_SNAKE_CASE_       ,       token_type_ids=SCREAMING_SNAKE_CASE_    )
									SCREAMING_SNAKE_CASE_						 = 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 _lowercase					(self       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = self.num_labels
									SCREAMING_SNAKE_CASE_						 = LiltForTokenClassification(config=SCREAMING_SNAKE_CASE_    )
									model.to(SCREAMING_SNAKE_CASE_    )
									model.eval()
									SCREAMING_SNAKE_CASE_						 = 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 _lowercase					(self       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = LiltForQuestionAnswering(config=SCREAMING_SNAKE_CASE_    )
									model.to(SCREAMING_SNAKE_CASE_    )
									model.eval()
									SCREAMING_SNAKE_CASE_						 = 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 _lowercase					(self    ):

									"""simple docstring"""







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







@require_torch
class  snake_case     (							__lowercase       ,			__lowercase       ,			__lowercase       ,			unittest.TestCase   ):
							UpperCAmelCase__						     =      (
							    (
							        LiltModel,
							        LiltForSequenceClassification,
							        LiltForTokenClassification,
							        LiltForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							UpperCAmelCase__						     =      (
							    {
							        '''feature-extraction''': LiltModel,
							        '''question-answering''': LiltForQuestionAnswering,
							        '''text-classification''': LiltForSequenceClassification,
							        '''token-classification''': LiltForTokenClassification,
							        '''zero-shot''': LiltForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							UpperCAmelCase__						     =      False
							UpperCAmelCase__						     =      False

							def _lowercase					(self       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    ):

									"""simple docstring"""







									return True

							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = LiltModelTester(self    )
									SCREAMING_SNAKE_CASE_						 = ConfigTester(self       ,       config_class=SCREAMING_SNAKE_CASE_       ,       hidden_size=37    )

							def _lowercase					(self    ):

									"""simple docstring"""







									self.config_tester.run_common_tests()

							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_    )

							def _lowercase					(self    ):

									"""simple docstring"""







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

							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE_    )

							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE_    )







							@slow
							def _lowercase					(self    ):

									"""simple docstring"""







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







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

							def _lowercase					(self    ):

									"""simple docstring"""







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

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

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

									SCREAMING_SNAKE_CASE_						 = torch.Size([1, 2, 7_68]    )
									SCREAMING_SNAKE_CASE_						 = torch.tensor(
									    [[-0.06_53, 0.09_50, -0.00_61], [-0.05_45, 0.09_26, -0.03_24]]       ,       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    )    )

"""simple docstring"""




from dataclasses import dataclass
from typing import Optional, Tuple

import torch
from torch import nn
from transformers import RobertaPreTrainedModel, XLMRobertaConfig, XLMRobertaModel
from transformers.utils import ModelOutput





@dataclass
class  snake_case     (							__lowercase   ):
							UpperCAmelCase__						     =      None
							UpperCAmelCase__						     =      None
							UpperCAmelCase__						     =      None
							UpperCAmelCase__						     =      None







class  snake_case     (							__lowercase   ):

							def __init__(self       ,       SCREAMING_SNAKE_CASE_=1       ,       SCREAMING_SNAKE_CASE_=0       ,       SCREAMING_SNAKE_CASE_=2       ,       SCREAMING_SNAKE_CASE_=5_12       ,       SCREAMING_SNAKE_CASE_="cls"       ,       SCREAMING_SNAKE_CASE_=False       ,       SCREAMING_SNAKE_CASE_=True       ,       **SCREAMING_SNAKE_CASE_       ,       ):

									"""simple docstring"""







									super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_       ,       bos_token_id=SCREAMING_SNAKE_CASE_       ,       eos_token_id=SCREAMING_SNAKE_CASE_       ,       **SCREAMING_SNAKE_CASE_    )
									SCREAMING_SNAKE_CASE_						 = project_dim
									SCREAMING_SNAKE_CASE_						 = pooler_fn
									SCREAMING_SNAKE_CASE_						 = learn_encoder
									SCREAMING_SNAKE_CASE_						 = use_attention_mask







class  snake_case     (							__lowercase   ):
							UpperCAmelCase__						     =      [R'''pooler''', R'''logit_scale''']
							UpperCAmelCase__						     =      [R'''position_ids''', R'''predictions.decoder.bias''']
							UpperCAmelCase__						     =      '''roberta'''
							UpperCAmelCase__						     =      RobertaSeriesConfig

							def __init__(self       ,       SCREAMING_SNAKE_CASE_    ):

									"""simple docstring"""







									super().__init__(SCREAMING_SNAKE_CASE_    )
									SCREAMING_SNAKE_CASE_						 = XLMRobertaModel(SCREAMING_SNAKE_CASE_    )
									SCREAMING_SNAKE_CASE_						 = nn.Linear(config.hidden_size       ,       config.project_dim    )
									SCREAMING_SNAKE_CASE_						 = getattr(SCREAMING_SNAKE_CASE_       ,       '''has_pre_transformation'''       ,       SCREAMING_SNAKE_CASE_    )
									if self.has_pre_transformation:
											SCREAMING_SNAKE_CASE_						 = nn.Linear(config.hidden_size       ,       config.project_dim    )
											SCREAMING_SNAKE_CASE_						 = nn.LayerNorm(config.hidden_size       ,       eps=config.layer_norm_eps    )
									self.post_init()







							def _lowercase					(self       ,       SCREAMING_SNAKE_CASE_ = None       ,       SCREAMING_SNAKE_CASE_ = None       ,       SCREAMING_SNAKE_CASE_ = None       ,       SCREAMING_SNAKE_CASE_ = None       ,       SCREAMING_SNAKE_CASE_ = None       ,       SCREAMING_SNAKE_CASE_ = None       ,       SCREAMING_SNAKE_CASE_ = None       ,       SCREAMING_SNAKE_CASE_ = None       ,       SCREAMING_SNAKE_CASE_ = None       ,       SCREAMING_SNAKE_CASE_ = None       ,       SCREAMING_SNAKE_CASE_ = None       ,       ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = return_dict if return_dict is not None else self.config.use_return_dict

									SCREAMING_SNAKE_CASE_						 = self.base_model(
									    input_ids=SCREAMING_SNAKE_CASE_       ,       attention_mask=SCREAMING_SNAKE_CASE_       ,       token_type_ids=SCREAMING_SNAKE_CASE_       ,       position_ids=SCREAMING_SNAKE_CASE_       ,       head_mask=SCREAMING_SNAKE_CASE_       ,       inputs_embeds=SCREAMING_SNAKE_CASE_       ,       encoder_hidden_states=SCREAMING_SNAKE_CASE_       ,       encoder_attention_mask=SCREAMING_SNAKE_CASE_       ,       output_attentions=SCREAMING_SNAKE_CASE_       ,       output_hidden_states=True if self.has_pre_transformation else output_hidden_states       ,       return_dict=SCREAMING_SNAKE_CASE_       ,       )

									if self.has_pre_transformation:
											SCREAMING_SNAKE_CASE_						 = outputs['''hidden_states'''][-2]
											SCREAMING_SNAKE_CASE_						 = self.pre_LN(SCREAMING_SNAKE_CASE_    )
											SCREAMING_SNAKE_CASE_						 = self.transformation_pre(SCREAMING_SNAKE_CASE_    )

											return TransformationModelOutput(
											    projection_state=SCREAMING_SNAKE_CASE_       ,       last_hidden_state=outputs.last_hidden_state       ,       hidden_states=outputs.hidden_states       ,       attentions=outputs.attentions       ,       )
									else:
											SCREAMING_SNAKE_CASE_						 = self.transformation(outputs.last_hidden_state    )
											return TransformationModelOutput(
											    projection_state=SCREAMING_SNAKE_CASE_       ,       last_hidden_state=outputs.last_hidden_state       ,       hidden_states=outputs.hidden_states       ,       attentions=outputs.attentions       ,       )