





"""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   ,      lowercase   ,      lowercase=13   ,      lowercase=7   ,      lowercase=True   ,      lowercase=True   ,      lowercase=True   ,      lowercase=True   ,      lowercase=99   ,      lowercase=24   ,      lowercase=2   ,      lowercase=6   ,      lowercase=37   ,      lowercase="gelu"   ,      lowercase=0.1   ,      lowercase=0.1   ,      lowercase=5_12   ,      lowercase=16   ,      lowercase=2   ,      lowercase=0.02   ,      lowercase=3   ,      lowercase=None   ,      lowercase=10_00   ,      )     ->      Any:





        '''simple docstring'''





        a__:					Optional[Any]      							=     parent
        a__:					int      							=     batch_size
        a__:					Optional[int]      							=     seq_length
        a__:					int      							=     is_training
        a__:					Dict      							=     use_input_mask
        a__:					Union[str, Any]      							=     use_token_type_ids
        a__:					List[str]      							=     use_labels
        a__:					Optional[Any]      							=     vocab_size
        a__:					str      							=     hidden_size
        a__:					Optional[Any]      							=     num_hidden_layers
        a__:					Tuple      							=     num_attention_heads
        a__:					Dict      							=     intermediate_size
        a__:					Any      							=     hidden_act
        a__:					str      							=     hidden_dropout_prob
        a__:					str      							=     attention_probs_dropout_prob
        a__:					List[str]      							=     max_position_embeddings
        a__:					Union[str, Any]      							=     type_vocab_size
        a__:					Any      							=     type_sequence_label_size
        a__:					Tuple      							=     initializer_range
        a__:					str      							=     num_labels
        a__:					int      							=     scope
        a__:					Any      							=     range_bbox



      def   lowerCamelCase_       (						self)     ->      Any:





        '''simple docstring'''





        a__:					Union[str, Any]      							=     ids_tensor([self.batch_size, self.seq_length]   ,      self.vocab_size)

        a__:					Optional[Any]      							=     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__:					List[str]      							=     bbox[i, j, 3]
              a__:					List[str]      							=     bbox[i, j, 1]
              a__:					str      							=     t
            if bbox[i, j, 2] < bbox[i, j, 0]:
              a__:					str      							=     bbox[i, j, 2]
              a__:					Union[str, Any]      							=     bbox[i, j, 0]
              a__:					Dict      							=     t

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

        a__:					Optional[Any]      							=     None
        if self.use_token_type_ids:
          a__:					int      							=     ids_tensor([self.batch_size, self.seq_length]   ,      self.type_vocab_size)

        a__:					Any      							=     None
        a__:					Any      							=     None
        if self.use_labels:
          a__:					Any      							=     ids_tensor([self.batch_size]   ,      self.type_sequence_label_size)
          a__:					Optional[int]      							=     ids_tensor([self.batch_size, self.seq_length]   ,      self.num_labels)

        a__:					str      							=     self.get_config()

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



      def   lowerCamelCase_       (						self)     ->      Dict:





        '''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   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      )     ->      List[Any]:





        '''simple docstring'''





        a__:					List[Any]      							=     LiltModel(config=snake_case__)
        model.to(snake_case__)
        model.eval()
        a__:					Tuple      							=     model(snake_case__   ,      bbox=snake_case__   ,      attention_mask=snake_case__   ,      token_type_ids=snake_case__)
        a__:					Union[str, Any]      							=     model(snake_case__   ,      bbox=snake_case__   ,      token_type_ids=snake_case__)
        a__:					Union[str, Any]      							=     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   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      )     ->      Union[str, Any]:





        '''simple docstring'''





        a__:					List[str]      							=     self.num_labels
        a__:					List[str]      							=     LiltForTokenClassification(config=snake_case__)
        model.to(snake_case__)
        model.eval()
        a__:					Union[str, Any]      							=     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   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      )     ->      int:





        '''simple docstring'''





        a__:					Optional[Any]      							=     LiltForQuestionAnswering(config=snake_case__)
        model.to(snake_case__)
        model.eval()
        a__:					Any      							=     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)     ->      Optional[Any]:





        '''simple docstring'''





        a__:					List[str]      							=     self.prepare_config_and_inputs()
        (
            a__ 
        ):					Optional[int]      							=     config_and_inputs
        a__:					int      							=     {
            "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    ):
      a__				=		(
          (
              LiltModel,
              LiltForSequenceClassification,
              LiltForTokenClassification,
              LiltForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      a__				=		(
          {
              "feature-extraction": LiltModel,
              "question-answering": LiltForQuestionAnswering,
              "text-classification": LiltForSequenceClassification,
              "token-classification": LiltForTokenClassification,
              "zero-shot": LiltForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      a__				=		False
      a__				=		False



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase)     ->      List[Any]:





        '''simple docstring'''





        return True



      def   lowerCamelCase_       (						self)     ->      str:





        '''simple docstring'''





        a__:					Tuple      							=     LiltModelTester(self)
        a__:					Dict      							=     ConfigTester(self   ,      config_class=snake_case__   ,      hidden_size=37)



      def   lowerCamelCase_       (						self)     ->      Dict:





        '''simple docstring'''





        self.config_tester.run_common_tests()



      def   lowerCamelCase_       (						self)     ->      int:





        '''simple docstring'''





        a__:					Tuple      							=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*snake_case__)



      def   lowerCamelCase_       (						self)     ->      int:





        '''simple docstring'''





        a__:					Union[str, Any]      							=     self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
          a__:					Optional[Any]      							=     type
          self.model_tester.create_and_check_model(*snake_case__)



      def   lowerCamelCase_       (						self)     ->      int:





        '''simple docstring'''





        a__:					Union[str, Any]      							=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*snake_case__)



      def   lowerCamelCase_       (						self)     ->      Optional[int]:





        '''simple docstring'''





        a__:					Union[str, Any]      							=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*snake_case__)



      @slow
      def   lowerCamelCase_       (						self)     ->      str:





        '''simple docstring'''





        for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          a__:					Optional[int]      							=     LiltModel.from_pretrained(snake_case__)
          self.assertIsNotNone(snake_case__)







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



      def   lowerCamelCase_       (						self)     ->      Any:





        '''simple docstring'''





        a__:					Dict      							=     LiltModel.from_pretrained('SCUT-DLVCLab/lilt-roberta-en-base').to(snake_case__)

        a__:					str      							=     torch.tensor([[1, 2]]   ,      device=snake_case__)
        a__:					Any      							=     torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]   ,      device=snake_case__)

        # forward pass
        with torch.no_grad():
          a__:					Dict      							=     model(input_ids=snake_case__   ,      bbox=snake_case__)

        a__:					List[str]      							=     torch.Size([1, 2, 7_68])
        a__:					Optional[int]      							=     torch.tensor(
            [[-0.0653, 0.0950, -0.0061], [-0.0545, 0.0926, -0.0324]]   ,      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))



"""simple docstring"""
# Lint as: python3
# pylint: enable=line-too-long
# pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position

lowercase__ 	=     '2.13.1'

import platform

import pyarrow
from packaging import version


if version.parse(platform.python_version()) < version.parse('3.7'):
 raise ImportWarning(
     'To use `datasets`, Python>=3.7 is required, and the current version of Python doesn\'t match this condition.'
 )

if version.parse(pyarrow.__version__).major < 8:
 raise ImportWarning(
     'To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn\'t match this condition.\n'
     'If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`.'
 )

del platform
del pyarrow
del version

from .arrow_dataset import Dataset
from .arrow_reader import ReadInstruction
from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder
from .combine import concatenate_datasets, interleave_datasets
from .dataset_dict import DatasetDict, IterableDatasetDict
from .download import *
from .features import *
from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled
from .info import DatasetInfo, MetricInfo
from .inspect import (
    get_dataset_config_info,
    get_dataset_config_names,
    get_dataset_infos,
    get_dataset_split_names,
    inspect_dataset,
    inspect_metric,
    list_datasets,
    list_metrics,
)
from .iterable_dataset import IterableDataset
from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric
from .metric import Metric
from .splits import (
    NamedSplit,
    NamedSplitAll,
    Split,
    SplitBase,
    SplitDict,
    SplitGenerator,
    SplitInfo,
    SubSplitInfo,
    percent,
)
from .tasks import *
from .utils import *
from .utils import logging


# deprecated modules
from datasets import arrow_dataset as _arrow_dataset  # isort:skip
from datasets import utils as _utils  # isort:skip
from datasets.utils import download_manager as _deprecated_download_manager  # isort:skip

lowercase__ 	=     concatenate_datasets
lowercase__ 	=     DownloadConfig
lowercase__ 	=     DownloadManager
lowercase__ 	=     DownloadMode
lowercase__ 	=     DownloadConfig
lowercase__ 	=     DownloadMode
lowercase__ 	=     DownloadManager

del _arrow_dataset, _utils, _deprecated_download_manager


