



'''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  ,UpperCAmelCase_  ,UpperCAmelCase_=13  ,UpperCAmelCase_=7  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=99  ,UpperCAmelCase_=24  ,UpperCAmelCase_=2  ,UpperCAmelCase_=6  ,UpperCAmelCase_=37  ,UpperCAmelCase_="gelu"  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=512  ,UpperCAmelCase_=16  ,UpperCAmelCase_=2  ,UpperCAmelCase_=0.02  ,UpperCAmelCase_=3  ,UpperCAmelCase_=None  ,UpperCAmelCase_=1_000  ,)	->				Dict:
           lowercase__									=    parent
           lowercase__									=    batch_size
           lowercase__									=    seq_length
           lowercase__									=    is_training
           lowercase__									=    use_input_mask
           lowercase__									=    use_token_type_ids
           lowercase__									=    use_labels
           lowercase__									=    vocab_size
           lowercase__									=    hidden_size
           lowercase__									=    num_hidden_layers
           lowercase__									=    num_attention_heads
           lowercase__									=    intermediate_size
           lowercase__									=    hidden_act
           lowercase__									=    hidden_dropout_prob
           lowercase__									=    attention_probs_dropout_prob
           lowercase__									=    max_position_embeddings
           lowercase__									=    type_vocab_size
           lowercase__									=    type_sequence_label_size
           lowercase__									=    initializer_range
           lowercase__									=    num_labels
           lowercase__									=    scope
           lowercase__									=    range_bbox



       def 				_a   (					self   )	->				List[Any]:
           lowercase__									=    ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size   )

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

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

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

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

           lowercase__									=    self.get_config()

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



       def 				_a   (					self   )	->				str:
           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  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,)	->				int:
           lowercase__									=    LiltModel(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    model(UpperCAmelCase_  ,bbox=UpperCAmelCase_  ,attention_mask=UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_   )
           lowercase__									=    model(UpperCAmelCase_  ,bbox=UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_   )
           lowercase__									=    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 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,)	->				Dict:
           lowercase__									=    self.num_labels
           lowercase__									=    LiltForTokenClassification(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    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 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,)	->				Optional[Any]:
           lowercase__									=    LiltForQuestionAnswering(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    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 				_a   (					self   )	->				Union[str, Any]:
           lowercase__									=    self.prepare_config_and_inputs()
           (
               (
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							
           )									=    config_and_inputs
           lowercase__									=    {
               "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			(UpperCamelCase		,   UpperCamelCase		,   UpperCamelCase		,   unittest.TestCase ):
       lowerCAmelCase__	:Optional[Any]     =       (
           (
               LiltModel,
               LiltForSequenceClassification,
               LiltForTokenClassification,
               LiltForQuestionAnswering,
           )
           if is_torch_available()
           else ()
       )
       lowerCAmelCase__	:int     =       (
           {
               "feature-extraction": LiltModel,
               "question-answering": LiltForQuestionAnswering,
               "text-classification": LiltForSequenceClassification,
               "token-classification": LiltForTokenClassification,
               "zero-shot": LiltForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       lowerCAmelCase__	:Any     =       False
       lowerCAmelCase__	:Tuple     =       False



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Union[str, Any]:
           return True



       def 				_a   (					self   )	->				List[Any]:
           lowercase__									=    LiltModelTester(self   )
           lowercase__									=    ConfigTester(self  ,config_class=UpperCAmelCase_  ,hidden_size=37   )



       def 				_a   (					self   )	->				int:
           self.config_tester.run_common_tests()



       def 				_a   (					self   )	->				List[str]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				Optional[Any]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
               lowercase__									=    type
               self.model_tester.create_and_check_model(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				str:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				Any:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase_   )







       @slow
       def 				_a   (					self   )	->				str:
           for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowercase__									=    LiltModel.from_pretrained(UpperCAmelCase_   )
               self.assertIsNotNone(UpperCAmelCase_   )





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



       def 				_a   (					self   )	->				Any:
           lowercase__									=    LiltModel.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base"   ).to(UpperCAmelCase_   )

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

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

           lowercase__									=    torch.Size([1, 2, 768]   )
           lowercase__									=    torch.tensor(
               [[-0.06_53, 0.09_50, -0.00_61], [-0.05_45, 0.09_26, -0.03_24]]  ,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 typing import Optional, Union

import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models.modeling_utils import ModelMixin


class 					snake_case			(UpperCamelCase		,   UpperCamelCase ):



       @register_to_config
       def __init__(					self  ,UpperCAmelCase_ = 768  ,)	->				List[Any]:
           super().__init__()

           lowercase__									=    nn.Parameter(torch.zeros(1  ,UpperCAmelCase_   )   )
           lowercase__									=    nn.Parameter(torch.ones(1  ,UpperCAmelCase_   )   )



       def 				_a   (					self  ,UpperCAmelCase_ = None  ,UpperCAmelCase_ = None  ,)	->				Any:
           lowercase__									=    nn.Parameter(self.mean.to(UpperCAmelCase_   ).to(UpperCAmelCase_   )   )
           lowercase__									=    nn.Parameter(self.std.to(UpperCAmelCase_   ).to(UpperCAmelCase_   )   )
           return self



       def 				_a   (					self  ,UpperCAmelCase_   )	->				Tuple:
           lowercase__									=    (embeds - self.mean) * 1.0 / self.std
           return embeds







       def 				_a   (					self  ,UpperCAmelCase_   )	->				List[str]:
           lowercase__									=    (embeds * self.std) + self.mean
           return embeds




