





from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


__lowerCamelCase       :     List[str]   =     logging.get_logger(__name__)



class 					A__   (       __snake_case			):
      _UpperCAmelCase	:Union[str, Any]						    =   ['input_features', 'attention_mask']





      def __init__( self       ,							A_=80       ,							A_=1_6000       ,							A_=80       ,							A_=0.0       ,							A_=True       ,							A_=True       ,							A_=True       ,							**A_       ,							):
       '''simple docstring'''




       super().__init__(feature_size=A_       ,							sampling_rate=A_       ,							padding_value=A_       ,							**A_   )
       UpperCamelCase	:					List[Any]       	=   num_mel_bins
       UpperCamelCase	:					Optional[int]       	=   do_ceptral_normalize
       UpperCamelCase	:					Optional[Any]       	=   normalize_means
       UpperCamelCase	:					Optional[int]       	=   normalize_vars
       UpperCamelCase	:					str       	=   True





      def        __UpperCamelCase( self       ,							A_       ,							):
       '''simple docstring'''




       UpperCamelCase	:					List[str]       	=   waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
       UpperCamelCase	:					Dict       	=   torch.from_numpy(A_   ).unsqueeze(0   )
       UpperCamelCase	:					str       	=   ta_kaldi.fbank(A_       ,							num_mel_bins=self.num_mel_bins       ,							sample_frequency=self.sampling_rate   )
       return features.numpy()





      @staticmethod
      def        __UpperCamelCase( A_       ,							A_       ,							A_ = True       ,							A_ = True       ,							A_ = 0.0       ,							):
       '''simple docstring'''




       if normalize_means:
        UpperCamelCase	:					Tuple       	=   x[:input_length].mean(axis=0   )
        UpperCamelCase	:					str       	=   np.subtract(A_       ,							A_   )
       if normalize_vars:
        UpperCamelCase	:					List[str]       	=   x[:input_length].std(axis=0   )
        UpperCamelCase	:					Any       	=   np.divide(A_       ,							A_   )

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

       # make sure array is in float32
       UpperCamelCase	:					Optional[int]       	=   x.astype(np.floataa   )

       return x





      def        __UpperCamelCase( self       ,							A_       ,							A_ = None   ):
       '''simple docstring'''




       UpperCamelCase	:					Union[str, Any]       	=   attention_mask.sum(-1   ) if attention_mask is not None else [x.shape[0] for x in input_features]
       return [
           self.utterance_cmvn(A_       ,							A_       ,							self.normalize_means       ,							self.normalize_vars       ,							self.padding_value   )
           for x, n in zip(A_       ,							A_   )
       ]





      def __call__( self       ,							A_       ,							A_ = False       ,							A_ = None       ,							A_ = False       ,							A_ = None       ,							A_ = None       ,							A_ = None       ,							A_ = None       ,							**A_       ,							):
       '''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	:					List[Any]       	=   isinstance(A_       ,							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	:					Any       	=   is_batched_numpy or (
           isinstance(A_       ,							(list, tuple)   ) and (isinstance(raw_speech[0]       ,							(np.ndarray, tuple, list)   ))
       )

       if is_batched:
        UpperCamelCase	:					List[Any]       	=   [np.asarray(A_       ,							dtype=np.floataa   ) for speech in raw_speech]
       elif not is_batched and not isinstance(A_       ,							np.ndarray   ):
        UpperCamelCase	:					Tuple       	=   np.asarray(A_       ,							dtype=np.floataa   )
       elif isinstance(A_       ,							np.ndarray   ) and raw_speech.dtype is np.dtype(np.floataa   ):
        UpperCamelCase	:					Union[str, Any]       	=   raw_speech.astype(np.floataa   )

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

       # extract fbank features
       UpperCamelCase	:					Optional[int]       	=   [self._extract_fbank_features(A_   ) for waveform in raw_speech]

       # convert into correct format for padding
       UpperCamelCase	:					Optional[int]       	=   BatchFeature({"input_features": features}   )

       UpperCamelCase	:					int       	=   self.pad(
           A_       ,							padding=A_       ,							max_length=A_       ,							truncation=A_       ,							pad_to_multiple_of=A_       ,							return_attention_mask=A_       ,							**A_       ,							)

       # make sure list is in array format
       UpperCamelCase	:					str       	=   padded_inputs.get("input_features"   )
       if isinstance(input_features[0]       ,							A_   ):
        UpperCamelCase	:					int       	=   [np.asarray(A_       ,							dtype=np.floataa   ) for feature in input_features]

       UpperCamelCase	:					List[str]       	=   padded_inputs.get("attention_mask"   )
       if attention_mask is not None:
        UpperCamelCase	:					Any       	=   [np.asarray(A_       ,							dtype=np.intaa   ) for array in attention_mask]

       # Utterance-level cepstral mean and variance normalization
       if self.do_ceptral_normalize:
        UpperCamelCase	:					str       	=   (
            np.array(A_       ,							dtype=np.intaa   )
            if self._get_padding_strategies(A_       ,							max_length=A_   ) is not PaddingStrategy.DO_NOT_PAD
            else None
        )
        UpperCamelCase	:					Any       	=   self.normalize(
            padded_inputs["input_features"]       ,							attention_mask=A_   )

       if return_tensors is not None:
        UpperCamelCase	:					Dict       	=   padded_inputs.convert_to_tensors(A_   )

       return padded_inputs







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       ,							A_       ,							A_=13       ,							A_=7       ,							A_=True       ,							A_=True       ,							A_=True       ,							A_=True       ,							A_=99       ,							A_=24       ,							A_=2       ,							A_=6       ,							A_=37       ,							A_="gelu"       ,							A_=0.1       ,							A_=0.1       ,							A_=512       ,							A_=16       ,							A_=2       ,							A_=0.02       ,							A_=3       ,							A_=None       ,							A_=1000       ,							):
       '''simple docstring'''




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





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					int       	=   ids_tensor([self.batch_size, self.seq_length]       ,							self.vocab_size   )

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

       UpperCamelCase	:					Dict       	=   None
       if self.use_input_mask:
        UpperCamelCase	:					Optional[int]       	=   ids_tensor([self.batch_size, self.seq_length]       ,							vocab_size=2   )

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

       UpperCamelCase	:					Dict       	=   None
       UpperCamelCase	:					int       	=   None
       if self.use_labels:
        UpperCamelCase	:					List[Any]       	=   ids_tensor([self.batch_size]       ,							self.type_sequence_label_size   )
        UpperCamelCase	:					int       	=   ids_tensor([self.batch_size, self.seq_length]       ,							self.num_labels   )

       UpperCamelCase	:					List[Any]       	=   self.get_config()

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





      def        __UpperCamelCase( self   ):
       '''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        __UpperCamelCase( self       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							):
       '''simple docstring'''




       UpperCamelCase	:					Any       	=   LiltModel(config=A_   )
       model.to(A_   )
       model.eval()
       UpperCamelCase	:					str       	=   model(A_       ,							bbox=A_       ,							attention_mask=A_       ,							token_type_ids=A_   )
       UpperCamelCase	:					Optional[int]       	=   model(A_       ,							bbox=A_       ,							token_type_ids=A_   )
       UpperCamelCase	:					Any       	=   model(A_       ,							bbox=A_   )

       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        __UpperCamelCase( self       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							):
       '''simple docstring'''




       UpperCamelCase	:					Any       	=   self.num_labels
       UpperCamelCase	:					Dict       	=   LiltForTokenClassification(config=A_   )
       model.to(A_   )
       model.eval()
       UpperCamelCase	:					Dict       	=   model(
           A_       ,							bbox=A_       ,							attention_mask=A_       ,							token_type_ids=A_       ,							labels=A_   )
       self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.seq_length, self.num_labels)   )





      def        __UpperCamelCase( self       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							):
       '''simple docstring'''




       UpperCamelCase	:					Dict       	=   LiltForQuestionAnswering(config=A_   )
       model.to(A_   )
       model.eval()
       UpperCamelCase	:					List[str]       	=   model(
           A_       ,							bbox=A_       ,							attention_mask=A_       ,							token_type_ids=A_       ,							start_positions=A_       ,							end_positions=A_       ,							)
       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        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Any       	=   self.prepare_config_and_inputs()
       (
           (
           UpperCamelCase
       )	,    (
           UpperCamelCase
       )	,    (
           UpperCamelCase
       )	,    (
           UpperCamelCase
       )	,    (
           UpperCamelCase
       )	,    (
           UpperCamelCase
       )	,    (
           UpperCamelCase
       )	,    
       )	:					Tuple       	=   config_and_inputs
       UpperCamelCase	:					Tuple       	=   {
           "input_ids": input_ids,
           "bbox": bbox,
           "token_type_ids": token_type_ids,
           "attention_mask": input_mask,
       }
       return config, inputs_dict



@require_torch
class 					A__   (       __snake_case       ,   __snake_case       ,   __snake_case       ,   unittest.TestCase			):
      _UpperCAmelCase	:Union[str, Any]						    =   (
          (
              LiltModel,
              LiltForSequenceClassification,
              LiltForTokenClassification,
              LiltForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      _UpperCAmelCase	:Optional[Any]						    =   (
          {
              'feature-extraction': LiltModel,
              'question-answering': LiltForQuestionAnswering,
              'text-classification': LiltForSequenceClassification,
              'token-classification': LiltForTokenClassification,
              'zero-shot': LiltForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      _UpperCAmelCase	:Dict						    =   False
      _UpperCAmelCase	:Union[str, Any]						    =   False





      def        __UpperCamelCase( self       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_   ):
       '''simple docstring'''




       return True





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Optional[int]       	=   LiltModelTester(self   )
       UpperCamelCase	:					Optional[int]       	=   ConfigTester(self       ,							config_class=A_       ,							hidden_size=37   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       self.config_tester.run_common_tests()





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Tuple       	=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*A_   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Tuple       	=   self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
        UpperCamelCase	:					Union[str, Any]       	=   type
        self.model_tester.create_and_check_model(*A_   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					int       	=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*A_   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					List[Any]       	=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*A_   )





      @slow
      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCamelCase	:					Dict       	=   LiltModel.from_pretrained(A_   )
        self.assertIsNotNone(A_   )



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





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					int       	=   LiltModel.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base"   ).to(A_   )

       UpperCamelCase	:					Tuple       	=   torch.tensor([[1, 2]]       ,							device=A_   )
       UpperCamelCase	:					List[str]       	=   torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]       ,							device=A_   )

       # forward pass
       with torch.no_grad():
        UpperCamelCase	:					Optional[int]       	=   model(input_ids=A_       ,							bbox=A_   )

       UpperCamelCase	:					List[str]       	=   torch.Size([1, 2, 768]   )
       UpperCamelCase	:					Any       	=   torch.tensor(
           [[-0.06_53, 0.09_50, -0.00_61], [-0.05_45, 0.09_26, -0.03_24]]       ,							device=A_       ,							)

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