





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

         UpperCAmelCase_											=LiltModel(config=_lowerCAmelCase       )
         model.to(_lowerCAmelCase       )
         model.eval()
         UpperCAmelCase_											=model(_lowerCAmelCase		,  bbox=_lowerCAmelCase		,  attention_mask=_lowerCAmelCase		,  token_type_ids=_lowerCAmelCase       )
         UpperCAmelCase_											=model(_lowerCAmelCase		,  bbox=_lowerCAmelCase		,  token_type_ids=_lowerCAmelCase       )
         UpperCAmelCase_											=model(_lowerCAmelCase		,  bbox=_lowerCAmelCase       )

         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:						Optional[Any]		,  _lowerCAmelCase:						Union[str, Any]		,  _lowerCAmelCase:						int		,  _lowerCAmelCase:						str		,  _lowerCAmelCase:						Tuple		,  _lowerCAmelCase:						List[str]		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						Tuple		,  )						->    Tuple:
         '''simple docstring'''

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



      def 	lowerCAmelCase__    (	self:						List[Any]		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						List[str]		,  _lowerCAmelCase:						List[str]		,  _lowerCAmelCase:						List[str]		,  _lowerCAmelCase:						Tuple		,  _lowerCAmelCase:						int		,  _lowerCAmelCase:						Union[str, Any]		,  )						->    Optional[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=LiltForQuestionAnswering(config=_lowerCAmelCase       )
         model.to(_lowerCAmelCase       )
         model.eval()
         UpperCAmelCase_											=model(
             _lowerCAmelCase		,  bbox=_lowerCAmelCase		,  attention_mask=_lowerCAmelCase		,  token_type_ids=_lowerCAmelCase		,  start_positions=_lowerCAmelCase		,  end_positions=_lowerCAmelCase		,  )
         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:						List[Any]       )						->    Optional[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      A       (	__lowercase		,      __lowercase		,      __lowercase		,      unittest.TestCase						):
      _snake_case							     =(
          (
              LiltModel,
              LiltForSequenceClassification,
              LiltForTokenClassification,
              LiltForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      _snake_case							     =(
          {
              '''feature-extraction''': LiltModel,
              '''question-answering''': LiltForQuestionAnswering,
              '''text-classification''': LiltForSequenceClassification,
              '''token-classification''': LiltForTokenClassification,
              '''zero-shot''': LiltForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      _snake_case							     =False
      _snake_case							     =False



      def 	lowerCAmelCase__    (	self:						Any		,  _lowerCAmelCase:						Any		,  _lowerCAmelCase:						Dict		,  _lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						Tuple		,  _lowerCAmelCase:						Optional[Any]       )						->    Tuple:
         '''simple docstring'''

         return True



      def 	lowerCAmelCase__    (	self:						Any       )						->    List[Any]:
         '''simple docstring'''

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



      def 	lowerCAmelCase__    (	self:						Union[str, Any]       )						->    List[Any]:
         '''simple docstring'''

         self.config_tester.run_common_tests()



      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    str:
         '''simple docstring'''

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



      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    int:
         '''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(*_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Any       )						->    Tuple:
         '''simple docstring'''

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



      def 	lowerCAmelCase__    (	self:						Dict       )						->    Optional[Any]:
         '''simple docstring'''

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






      @slow
      def 	lowerCAmelCase__    (	self:						Tuple       )						->    int:
         '''simple docstring'''

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

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



      def 	lowerCAmelCase__    (	self:						str       )						->    Optional[Any]:
         '''simple docstring'''

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

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

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

         UpperCAmelCase_											=torch.Size([1, 2, 768]       )
         UpperCAmelCase_											=torch.tensor(
             [[-0.06_53, 0.09_50, -0.00_61], [-0.05_45, 0.09_26, -0.03_24]]		,  device=_lowerCAmelCase		,  )

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











from typing import List, Optional, Union

import numpy as np

from ....audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function
from ....feature_extraction_sequence_utils import SequenceFeatureExtractor
from ....feature_extraction_utils import BatchFeature
from ....file_utils import PaddingStrategy, TensorType
from ....utils import logging


__lowercase  :   Union[str, Any]						      =logging.get_logger(__name__)

class      A       (	__lowercase						):
      _snake_case							     =['''input_features''', '''attention_mask''']



      def __init__(	self:						Dict		,  _lowerCAmelCase:						List[str]=80		,  _lowerCAmelCase:						Optional[Any]=1_6000		,  _lowerCAmelCase:						Any=0.0		,  _lowerCAmelCase:						Optional[Any]=10		,  _lowerCAmelCase:						Optional[Any]=25		,  _lowerCAmelCase:						Union[str, Any]="hamming_window"		,  _lowerCAmelCase:						Optional[int]=3_27_68.0		,  _lowerCAmelCase:						Optional[Any]=0.97		,  _lowerCAmelCase:						Any=1.0		,  _lowerCAmelCase:						List[str]=True		,  _lowerCAmelCase:						Dict=True		,  _lowerCAmelCase:						Any=False		,  **_lowerCAmelCase:						Any		,  )						->    Dict:
         '''simple docstring'''

         super().__init__(feature_size=_lowerCAmelCase		,  sampling_rate=_lowerCAmelCase		,  padding_value=_lowerCAmelCase		,  **_lowerCAmelCase       )

         UpperCAmelCase_											=feature_size
         UpperCAmelCase_											=sampling_rate
         UpperCAmelCase_											=padding_value
         UpperCAmelCase_											=hop_length
         UpperCAmelCase_											=win_length
         UpperCAmelCase_											=frame_signal_scale
         UpperCAmelCase_											=preemphasis_coeff
         UpperCAmelCase_											=mel_floor
         UpperCAmelCase_											=normalize_means
         UpperCAmelCase_											=normalize_vars
         UpperCAmelCase_											=win_function
         UpperCAmelCase_											=return_attention_mask

         UpperCAmelCase_											=win_length * sampling_rate // 1000
         UpperCAmelCase_											=hop_length * sampling_rate // 1000

         UpperCAmelCase_											=optimal_fft_length(self.sample_size       )
         UpperCAmelCase_											=(self.n_fft // 2) + 1



      def 	lowerCAmelCase__    (	self:						Optional[Any]		,  _lowerCAmelCase:						np.array       )						->    np.ndarray:
         '''simple docstring'''

         if self.win_function == "hamming_window":
            UpperCAmelCase_											=window_function(window_length=self.sample_size		,  name=self.win_function		,  periodic=_lowerCAmelCase       )
         else:
            UpperCAmelCase_											=window_function(window_length=self.sample_size		,  name=self.win_function       )

         UpperCAmelCase_											=mel_filter_bank(
             num_frequency_bins=self.n_freqs		,  num_mel_filters=self.feature_size		,  min_frequency=0.0		,  max_frequency=self.sampling_rate / 2.0		,  sampling_rate=self.sampling_rate		,  )

         UpperCAmelCase_											=spectrogram(
             one_waveform * self.frame_signal_scale		,  window=_lowerCAmelCase		,  frame_length=self.sample_size		,  hop_length=self.sample_stride		,  fft_length=self.n_fft		,  center=_lowerCAmelCase		,  preemphasis=self.preemphasis_coeff		,  mel_filters=_lowerCAmelCase		,  mel_floor=self.mel_floor		,  log_mel="log"		,  )
         return msfc_features.T



      def 	lowerCAmelCase__    (	self:						Dict		,  _lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						Dict		,  _lowerCAmelCase:						Any       )						->    Any:
         '''simple docstring'''

         if self.normalize_means:
            UpperCAmelCase_											=x[:input_length].mean(axis=0       )
            UpperCAmelCase_											=np.subtract(_lowerCAmelCase		,  _lowerCAmelCase       )
         if self.normalize_vars:
            UpperCAmelCase_											=x[:input_length].std(axis=0       )
            UpperCAmelCase_											=np.divide(_lowerCAmelCase		,  _lowerCAmelCase       )

         if input_length < x.shape[0]:
            UpperCAmelCase_											=padding_value

         # make sure array is in float32
         UpperCAmelCase_											=x.astype(np.floataa       )

         return x



      def 	lowerCAmelCase__    (	self:						List[str]		,  _lowerCAmelCase:						List[np.ndarray]		,  _lowerCAmelCase:						Optional[np.ndarray] = None       )						->    List[np.ndarray]:
         '''simple docstring'''

         UpperCAmelCase_											=attention_mask.sum(-1       ) if attention_mask is not None else [x.shape[0] for x in input_features]
         return [self._normalize_one(_lowerCAmelCase		,  _lowerCAmelCase		,  self.padding_value       ) for x, n in zip(_lowerCAmelCase		,  _lowerCAmelCase       )]






      def __call__(	self:						int		,  _lowerCAmelCase:						Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]		,  _lowerCAmelCase:						Union[bool, str, PaddingStrategy] = False		,  _lowerCAmelCase:						Optional[int] = None		,  _lowerCAmelCase:						bool = False		,  _lowerCAmelCase:						Optional[int] = None		,  _lowerCAmelCase:						Optional[bool] = None		,  _lowerCAmelCase:						Optional[Union[str, TensorType]] = None		,  _lowerCAmelCase:						Optional[int] = None		,  **_lowerCAmelCase:						List[Any]		,  )						->    BatchFeature:
         '''simple docstring'''

         if sampling_rate is not None:
            if sampling_rate != self.sampling_rate:
               raise ValueError(
                   F'The model corresponding to this feature extractor: {self} was trained using a sampling rate of'
                   F' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with'
                   F' {self.sampling_rate} and not {sampling_rate}.'       )
         else:
            logger.warning(
                "It is strongly recommended to pass the ``sampling_rate`` argument to this function. "
                "Failing to do so can result in silent errors that might be hard to debug."       )

         UpperCAmelCase_											=isinstance(_lowerCAmelCase		,  np.ndarray       ) and len(raw_speech.shape       ) > 1
         if is_batched_numpy and len(raw_speech.shape       ) > 2:
            raise ValueError(F'Only mono-channel audio is supported for input to {self}'       )
         UpperCAmelCase_											=is_batched_numpy or (
             isinstance(_lowerCAmelCase		,  (list, tuple)       ) and (isinstance(raw_speech[0]		,  (np.ndarray, tuple, list)       ))
         )

         if is_batched:
            UpperCAmelCase_											=[np.asarray(_lowerCAmelCase		,  dtype=np.floataa       ) for speech in raw_speech]
         elif not is_batched and not isinstance(_lowerCAmelCase		,  np.ndarray       ):
            UpperCAmelCase_											=np.asarray(_lowerCAmelCase		,  dtype=np.floataa       )
         elif isinstance(_lowerCAmelCase		,  np.ndarray       ) and raw_speech.dtype is np.dtype(np.floataa       ):
            UpperCAmelCase_											=raw_speech.astype(np.floataa       )

         # always return batch
         if not is_batched:
            UpperCAmelCase_											=[raw_speech]

         # extract fbank features
         UpperCAmelCase_											=[self._extract_mfsc_features(_lowerCAmelCase       ) for one_waveform in raw_speech]

         # convert into correct format for padding
         UpperCAmelCase_											=BatchFeature({"input_features": features}       )

         UpperCAmelCase_											=self.pad(
             _lowerCAmelCase		,  padding=_lowerCAmelCase		,  max_length=_lowerCAmelCase		,  truncation=_lowerCAmelCase		,  pad_to_multiple_of=_lowerCAmelCase		,  return_attention_mask=_lowerCAmelCase		,  **_lowerCAmelCase		,  )
         # make sure list is in array format
         UpperCAmelCase_											=padded_inputs.get("input_features"       )
         if isinstance(input_features[0]		,  _lowerCAmelCase       ):
            UpperCAmelCase_											=[np.asarray(_lowerCAmelCase		,  dtype=np.floataa       ) for feature in input_features]

         UpperCAmelCase_											=padded_inputs.get("attention_mask"       )
         if attention_mask is not None:
            UpperCAmelCase_											=[np.asarray(_lowerCAmelCase		,  dtype=np.intaa       ) for array in attention_mask]

         if self.normalize_means or self.normalize_vars:
            UpperCAmelCase_											=(
                np.array(_lowerCAmelCase		,  dtype=np.intaa       )
                if self._get_padding_strategies(_lowerCAmelCase		,  max_length=_lowerCAmelCase       ) is not PaddingStrategy.DO_NOT_PAD
                and padding
                else None
            )
            UpperCAmelCase_											=self.normalize(
                padded_inputs["input_features"]		,  attention_mask=_lowerCAmelCase       )

         if return_tensors is not None:
            UpperCAmelCase_											=padded_inputs.convert_to_tensors(_lowerCAmelCase       )

         return padded_inputs




