




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		:						Tuple					,						_snake_case		:						Tuple					,						_snake_case		:						List[Any]=13					,						_snake_case		:						Dict=7					,						_snake_case		:						List[str]=True					,						_snake_case		:						Dict=True					,						_snake_case		:						int=True					,						_snake_case		:						Tuple=True					,						_snake_case		:						Any=99					,						_snake_case		:						Dict=24					,						_snake_case		:						List[Any]=2					,						_snake_case		:						Optional[int]=6					,						_snake_case		:						str=37					,						_snake_case		:						List[str]="gelu"					,						_snake_case		:						int=0.1					,						_snake_case		:						Any=0.1					,						_snake_case		:						Optional[Any]=512					,						_snake_case		:						List[Any]=16					,						_snake_case		:						Dict=2					,						_snake_case		:						Dict=0.0_2					,						_snake_case		:						Optional[int]=3					,						_snake_case		:						str=None					,						_snake_case		:						List[str]=1000					,						):

        """simple docstring"""


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







   def lowerCamelCase						(      self		:						Tuple):

        """simple docstring"""


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

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

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

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

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

        UpperCAmelCase_          =							self.get_config()

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







   def lowerCamelCase						(      self		:						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		:						str					,						_snake_case		:						Dict					,						_snake_case		:						int					,						_snake_case		:						Union[str, Any]					,						_snake_case		:						List[Any]					,						_snake_case		:						Tuple					,						_snake_case		:						Dict					,						_snake_case		:						int					,						):

        """simple docstring"""


        UpperCAmelCase_          =							LiltModel(config=_snake_case)
        model.to(_snake_case)
        model.eval()
        UpperCAmelCase_          =							model(_snake_case					,						bbox=_snake_case					,						attention_mask=_snake_case					,						token_type_ids=_snake_case)
        UpperCAmelCase_          =							model(_snake_case					,						bbox=_snake_case					,						token_type_ids=_snake_case)
        UpperCAmelCase_          =							model(_snake_case					,						bbox=_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		:						List[str]					,						_snake_case		:						Tuple					,						_snake_case		:						Dict					,						_snake_case		:						Tuple					,						_snake_case		:						int					,						_snake_case		:						Tuple					,						_snake_case		:						str					,						_snake_case		:						Tuple					,						):

        """simple docstring"""


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







   def lowerCamelCase						(      self		:						Any					,						_snake_case		:						List[Any]					,						_snake_case		:						List[str]					,						_snake_case		:						str					,						_snake_case		:						int					,						_snake_case		:						str					,						_snake_case		:						Optional[Any]					,						_snake_case		:						Optional[int]					,						):

        """simple docstring"""


        UpperCAmelCase_          =							LiltForQuestionAnswering(config=_snake_case)
        model.to(_snake_case)
        model.eval()
        UpperCAmelCase_          =							model(
            _snake_case					,						bbox=_snake_case					,						attention_mask=_snake_case					,						token_type_ids=_snake_case					,						start_positions=_snake_case					,						end_positions=_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		:						int):

        """simple docstring"""


        UpperCAmelCase_          =							self.prepare_config_and_inputs()
        (
            (
            UpperCAmelCase_
        )     ,       (
            UpperCAmelCase_
        )     ,       (
            UpperCAmelCase_
        )     ,       (
            UpperCAmelCase_
        )     ,       (
            UpperCAmelCase_
        )     ,       (
            UpperCAmelCase_
        )     ,       (
            UpperCAmelCase_
        )     ,       
        )          =							config_and_inputs
        UpperCAmelCase_          =							{
            '''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			(			a ,  a ,  a ,  unittest.TestCase		):
   UpperCAmelCase__  :    Tuple          =  (
       (
           LiltModel,
           LiltForSequenceClassification,
           LiltForTokenClassification,
           LiltForQuestionAnswering,
       )
       if is_torch_available()
       else ()
   )
   UpperCAmelCase__  :    Tuple          =  (
       {
           '''feature-extraction''': LiltModel,
           '''question-answering''': LiltForQuestionAnswering,
           '''text-classification''': LiltForSequenceClassification,
           '''token-classification''': LiltForTokenClassification,
           '''zero-shot''': LiltForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   UpperCAmelCase__  :    Union[str, Any]          =  False
   UpperCAmelCase__  :    str          =  False







   def lowerCamelCase						(      self		:						Optional[int]					,						_snake_case		:						List[Any]					,						_snake_case		:						List[Any]					,						_snake_case		:						str					,						_snake_case		:						Dict					,						_snake_case		:						Any):

        """simple docstring"""


        return True







   def lowerCamelCase						(      self		:						Dict):

        """simple docstring"""


        UpperCAmelCase_          =							LiltModelTester(self)
        UpperCAmelCase_          =							ConfigTester(self					,						config_class=_snake_case					,						hidden_size=37)







   def lowerCamelCase						(      self		:						str):

        """simple docstring"""


        self.config_tester.run_common_tests()







   def lowerCamelCase						(      self		:						List[Any]):

        """simple docstring"""


        UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_snake_case)







   def lowerCamelCase						(      self		:						str):

        """simple docstring"""


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







   def lowerCamelCase						(      self		:						Any):

        """simple docstring"""


        UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*_snake_case)







   def lowerCamelCase						(      self		:						Tuple):

        """simple docstring"""


        UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*_snake_case)







   @slow
   def lowerCamelCase						(      self		:						int):

        """simple docstring"""


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







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







   def lowerCamelCase						(      self		:						int):

        """simple docstring"""


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

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

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

        UpperCAmelCase_          =							torch.Size([1, 2, 768])
        UpperCAmelCase_          =							torch.tensor(
            [[-0.0_6_5_3, 0.0_9_5_0, -0.0_0_6_1], [-0.0_5_4_5, 0.0_9_2_6, -0.0_3_2_4]]					,						device=_snake_case					,						)

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












from typing import List, Optional, TypeVar

from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets
from .dataset_dict import DatasetDict, IterableDatasetDict
from .info import DatasetInfo
from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets
from .splits import NamedSplit
from .utils import logging
from .utils.py_utils import Literal


snake_case_ : Any    		=       logging.get_logger(__name__)


snake_case_ : Optional[int]    		=       TypeVar("DatasetType", Dataset, IterableDataset)


def    A						(__A   :    List[DatasetType]   ,				__A   :    Optional[List[float]] = None   ,				__A   :    Optional[int] = None   ,				__A   :    Optional[DatasetInfo] = None   ,				__A   :    Optional[NamedSplit] = None   ,				__A   :    Literal["first_exhausted", "all_exhausted"] = "first_exhausted"   ,				)   ->      DatasetType:







     """simple docstring"""


     from .arrow_dataset import Dataset
     from .iterable_dataset import IterableDataset

     if not datasets:
          raise ValueError('''Unable to interleave an empty list of datasets.'''       )
     for i, dataset in enumerate(__A       ):
          if not isinstance(__A   ,				(Dataset, IterableDataset)       ):
               if isinstance(__A   ,				(DatasetDict, IterableDatasetDict)       ):
                    if not dataset:
                         raise ValueError(
                             F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} """
                             '''is an empty dataset dictionary.'''       )
                    raise ValueError(
                        F"""Dataset at position {i} has at least one split: {list(__A       )}\n"""
                        F"""Please pick one to interleave with the other datasets, for example: dataset['{next(iter(__A       )       )}']"""       )
               raise ValueError(
                   F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(__A       ).__name__}."""       )
          if i == 0:
               UpperCAmelCase_     ,       UpperCAmelCase_          =							(
                   (Dataset, IterableDataset) if isinstance(__A   ,				__A       ) else (IterableDataset, Dataset)
               )
          elif not isinstance(__A   ,				__A       ):
               raise ValueError(
                   F"""Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects."""       )
     if stopping_strategy not in ["first_exhausted", "all_exhausted"]:
          raise ValueError(F"""{stopping_strategy} is not supported. Please enter a valid stopping_strategy."""       )
     if dataset_type is Dataset:
          return _interleave_map_style_datasets(
              __A   ,				__A   ,				__A   ,				info=__A   ,				split=__A   ,				stopping_strategy=__A       )
     else:
          return _interleave_iterable_datasets(
              __A   ,				__A   ,				__A   ,				info=__A   ,				split=__A   ,				stopping_strategy=__A       )


def    A						(__A   :    List[DatasetType]   ,				__A   :    Optional[DatasetInfo] = None   ,				__A   :    Optional[NamedSplit] = None   ,				__A   :    int = 0   ,				)   ->      DatasetType:







     """simple docstring"""


     if not dsets:
          raise ValueError('''Unable to concatenate an empty list of datasets.'''       )
     for i, dataset in enumerate(__A       ):
          if not isinstance(__A   ,				(Dataset, IterableDataset)       ):
               if isinstance(__A   ,				(DatasetDict, IterableDatasetDict)       ):
                    if not dataset:
                         raise ValueError(
                             F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} """
                             '''is an empty dataset dictionary.'''       )
                    raise ValueError(
                        F"""Dataset at position {i} has at least one split: {list(__A       )}\n"""
                        F"""Please pick one to interleave with the other datasets, for example: dataset['{next(iter(__A       )       )}']"""       )
               raise ValueError(
                   F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(__A       ).__name__}."""       )
          if i == 0:
               UpperCAmelCase_     ,       UpperCAmelCase_          =							(
                   (Dataset, IterableDataset) if isinstance(__A   ,				__A       ) else (IterableDataset, Dataset)
               )
          elif not isinstance(__A   ,				__A       ):
               raise ValueError(
                   F"""Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects."""       )
     if dataset_type is Dataset:
          return _concatenate_map_style_datasets(__A   ,				info=__A   ,				split=__A   ,				axis=__A       )
     else:
          return _concatenate_iterable_datasets(__A   ,				info=__A   ,				split=__A   ,				axis=__A       )






