



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							:     Tuple     ,		__SCREAMING_SNAKE_CASE							:     Dict     ,		__SCREAMING_SNAKE_CASE							:     Dict=13     ,		__SCREAMING_SNAKE_CASE							:     int=7     ,		__SCREAMING_SNAKE_CASE							:     Tuple=True     ,		__SCREAMING_SNAKE_CASE							:     Dict=True     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]=True     ,		__SCREAMING_SNAKE_CASE							:     List[str]=True     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]=99     ,		__SCREAMING_SNAKE_CASE							:     Any=24     ,		__SCREAMING_SNAKE_CASE							:     Dict=2     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]=6     ,		__SCREAMING_SNAKE_CASE							:     List[Any]=37     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]="gelu"     ,		__SCREAMING_SNAKE_CASE							:     str=0.1     ,		__SCREAMING_SNAKE_CASE							:     List[Any]=0.1     ,		__SCREAMING_SNAKE_CASE							:     Tuple=512     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]=16     ,		__SCREAMING_SNAKE_CASE							:     Any=2     ,		__SCREAMING_SNAKE_CASE							:     Dict=0.02     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]=3     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]=None     ,		__SCREAMING_SNAKE_CASE							:     List[str]=1_000     ,		):


           '''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 		_lowerCamelCase			(		self							:     Tuple):


           '''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 		_lowerCamelCase			(		self							:     Optional[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 		_lowerCamelCase			(		self							:     Optional[int]     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     int     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]     ,		__SCREAMING_SNAKE_CASE							:     Any     ,		__SCREAMING_SNAKE_CASE							:     Tuple     ,		__SCREAMING_SNAKE_CASE							:     List[str]     ,		):


           '''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 		_lowerCamelCase			(		self							:     Tuple     ,		__SCREAMING_SNAKE_CASE							:     List[Any]     ,		__SCREAMING_SNAKE_CASE							:     List[Any]     ,		__SCREAMING_SNAKE_CASE							:     List[Any]     ,		__SCREAMING_SNAKE_CASE							:     int     ,		__SCREAMING_SNAKE_CASE							:     int     ,		__SCREAMING_SNAKE_CASE							:     List[str]     ,		__SCREAMING_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 		_lowerCamelCase			(		self							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     Dict     ,		__SCREAMING_SNAKE_CASE							:     Any     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]     ,		__SCREAMING_SNAKE_CASE							:     Dict     ,		__SCREAMING_SNAKE_CASE							:     List[Any]     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]     ,		__SCREAMING_SNAKE_CASE							:     Dict     ,		):


           '''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 		_lowerCamelCase			(		self							:     Dict):


           '''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    ):
       UpperCamelCase__  :    Optional[Any]											=						(
           (
               LiltModel,
               LiltForSequenceClassification,
               LiltForTokenClassification,
               LiltForQuestionAnswering,
           )
           if is_torch_available()
           else ()
       )
       UpperCamelCase__  :    List[Any]											=						(
           {
               '''feature-extraction''': LiltModel,
               '''question-answering''': LiltForQuestionAnswering,
               '''text-classification''': LiltForSequenceClassification,
               '''token-classification''': LiltForTokenClassification,
               '''zero-shot''': LiltForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       UpperCamelCase__  :    Any											=						False
       UpperCamelCase__  :    Optional[Any]											=						False



       def 		_lowerCamelCase			(		self							:     str     ,		__SCREAMING_SNAKE_CASE							:     Dict     ,		__SCREAMING_SNAKE_CASE							:     str     ,		__SCREAMING_SNAKE_CASE							:     int     ,		__SCREAMING_SNAKE_CASE							:     Tuple     ,		__SCREAMING_SNAKE_CASE							:     Dict):


           '''simple docstring'''

           return True



       def 		_lowerCamelCase			(		self							:     Dict):


           '''simple docstring'''

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



       def 		_lowerCamelCase			(		self							:     str):


           '''simple docstring'''

           self.config_tester.run_common_tests()



       def 		_lowerCamelCase			(		self							:     Optional[Any]):


           '''simple docstring'''

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



       def 		_lowerCamelCase			(		self							:     int):


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


           '''simple docstring'''

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



       def 		_lowerCamelCase			(		self							:     Any):


           '''simple docstring'''

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





       @slow
       def 		_lowerCamelCase			(		self							:     str):


           '''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 		_lowerCamelCase			(		self							:     List[Any]):


           '''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.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))










import unicodedata
from dataclasses import dataclass
from typing import Optional, Union

import numpy as np

from transformers.data.data_collator import DataCollatorMixin
from transformers.file_utils import PaddingStrategy
from transformers.tokenization_utils_base import PreTrainedTokenizerBase





def      __snake_case	(			_UpperCAmelCase   , _UpperCAmelCase   , _UpperCAmelCase   , _UpperCAmelCase				):
    if isinstance(_UpperCAmelCase   , _UpperCAmelCase				):
        __a						       =				np.full((len(_UpperCAmelCase				), sequence_length, 2)   , _UpperCAmelCase				)
    else:
        __a						       =				np.full((len(_UpperCAmelCase				), sequence_length)   , _UpperCAmelCase				)

    for i, tensor in enumerate(_UpperCAmelCase				):
        if padding_side == "right":
            if isinstance(_UpperCAmelCase   , _UpperCAmelCase				):
                __a						       =				tensor[:sequence_length]
            else:
                __a						       =				tensor[:sequence_length]
        else:
            if isinstance(_UpperCAmelCase   , _UpperCAmelCase				):
                __a						       =				tensor[:sequence_length]
            else:
                __a						       =				tensor[:sequence_length]

    return out_tensor.tolist()





def      __snake_case	(			_UpperCAmelCase				):
    __a						       =				ord(_UpperCAmelCase				)
    if (cp >= 33 and cp <= 47) or (cp >= 58 and cp <= 64) or (cp >= 91 and cp <= 96) or (cp >= 123 and cp <= 126):
        return True
    __a						       =				unicodedata.category(_UpperCAmelCase				)
    if cat.startswith('''P'''				):
        return True
    return False

@dataclass
class     _A       (	__UpperCAmelCase    ):
       UpperCamelCase__  :    PreTrainedTokenizerBase
       UpperCamelCase__  :    Union[bool, str, PaddingStrategy]											=						True
       UpperCamelCase__  :    Optional[int]											=						None
       UpperCamelCase__  :    Optional[int]											=						None
       UpperCamelCase__  :    int											=						-100
       UpperCamelCase__  :    str											=						"pt"



       def 		_lowerCamelCase			(		self							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]):


           '''simple docstring'''

           import torch

           __a						       =				'''label''' if '''label''' in features[0].keys() else '''labels'''
           __a						       =				[feature[label_name] for feature in features] if label_name in features[0].keys() else None
           __a						       =				self.tokenizer.pad(
               __SCREAMING_SNAKE_CASE     ,		padding=self.padding     ,		max_length=self.max_length     ,		pad_to_multiple_of=self.pad_to_multiple_of     ,		return_tensors='''pt''' if labels is None else None     ,		)

           if labels is None:
               return batch

           __a						       =				torch.tensor(batch['''entity_ids''']).shape[1]
           __a						       =				self.tokenizer.padding_side
           if padding_side == "right":
               __a						       =				[
                   list(__SCREAMING_SNAKE_CASE) + [self.label_pad_token_id] * (sequence_length - len(__SCREAMING_SNAKE_CASE)) for label in labels
               ]
           else:
               __a						       =				[
                   [self.label_pad_token_id] * (sequence_length - len(__SCREAMING_SNAKE_CASE)) + list(__SCREAMING_SNAKE_CASE) for label in labels
               ]

           __a						       =				[feature['''ner_tags'''] for feature in features]
           __a						       =				padding_tensor(__SCREAMING_SNAKE_CASE     ,		-1     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE)
           __a						       =				[feature['''original_entity_spans'''] for feature in features]
           __a						       =				padding_tensor(__SCREAMING_SNAKE_CASE     ,		(-1, -1)     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE)
           __a						       =				{k: torch.tensor(__SCREAMING_SNAKE_CASE     ,		dtype=torch.intaa) for k, v in batch.items()}

           return batch





