

'''simple docstring'''


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__						:       Union[str, Any]										=						logging.get_logger(__name__)




class     a			(				lowercase__       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    ["""input_features""", """attention_mask"""]







						def __init__(							self					:      str			,			snake_case_					:      str=8_0			,			snake_case_					:      Optional[int]=1_6_0_0_0			,			snake_case_					:      Optional[int]=8_0			,			snake_case_					:      Dict=0.0			,			snake_case_					:      List[Any]=True			,			snake_case_					:      Tuple=True			,			snake_case_					:      Optional[int]=True			,			**snake_case_					:      Optional[int]			,			):



								'''simple docstring'''



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







						def       __magic_name__							(							self					:      List[Any]			,			snake_case_					:      np.ndarray			,			):



								'''simple docstring'''



								snake_case__						:       int       						=  waveform * (2**1_5)  # Kaldi compliance: 16-bit signed integers
								snake_case__						:       Dict       						=  torch.from_numpy(__lowerCamelCase  ).unsqueeze(0  )
								snake_case__						:       Optional[Any]       						=  ta_kaldi.fbank(__lowerCamelCase			,			num_mel_bins=self.num_mel_bins			,			sample_frequency=self.sampling_rate  )
								return features.numpy()







						@staticmethod
						def       __magic_name__							(							snake_case_					:      np.ndarray			,			snake_case_					:      int			,			snake_case_					:      Optional[bool] = True			,			snake_case_					:      Optional[bool] = True			,			snake_case_					:      float = 0.0			,			):



								'''simple docstring'''



								if normalize_means:
										snake_case__						:       int       						=  x[:input_length].mean(axis=0  )
										snake_case__						:       int       						=  np.subtract(__lowerCamelCase			,			__lowerCamelCase  )
								if normalize_vars:
										snake_case__						:       int       						=  x[:input_length].std(axis=0  )
										snake_case__						:       List[Any]       						=  np.divide(__lowerCamelCase			,			__lowerCamelCase  )

								if input_length < x.shape[0]:
										snake_case__						:       Optional[int]       						=  padding_value

								# make sure array is in float32
								snake_case__						:       List[Any]       						=  x.astype(np.floataa  )

								return x







						def       __magic_name__							(							self					:      Any			,			snake_case_					:      List[np.ndarray]			,			snake_case_					:      Optional[np.ndarray] = None  ):



								'''simple docstring'''



								snake_case__						:       Tuple       						=  attention_mask.sum(-1  ) if attention_mask is not None else [x.shape[0] for x in input_features]
								return [
								    self.utterance_cmvn(__lowerCamelCase			,			__lowerCamelCase			,			self.normalize_means			,			self.normalize_vars			,			self.padding_value  )
								    for x, n in zip(__lowerCamelCase			,			__lowerCamelCase  )
								]







						def __call__(							self					:      Dict			,			snake_case_					:      Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]			,			snake_case_					:      Union[bool, str, PaddingStrategy] = False			,			snake_case_					:      Optional[int] = None			,			snake_case_					:      bool = False			,			snake_case_					:      Optional[int] = None			,			snake_case_					:      Optional[Union[str, TensorType]] = None			,			snake_case_					:      Optional[int] = None			,			snake_case_					:      Optional[bool] = None			,			**snake_case_					:      List[str]			,			):



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

								snake_case__						:       int       						=  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}"""  )
								snake_case__						:       int       						=  is_batched_numpy or (
								    isinstance(__lowerCamelCase			,			(list, tuple)  ) and (isinstance(raw_speech[0]			,			(np.ndarray, tuple, list)  ))
								)

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

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

								# extract fbank features
								snake_case__						:       Dict       						=  [self._extract_fbank_features(__lowerCamelCase  ) for waveform in raw_speech]

								# convert into correct format for padding
								snake_case__						:       Optional[Any]       						=  BatchFeature({'''input_features''': features}  )

								snake_case__						:       Dict       						=  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
								snake_case__						:       Tuple       						=  padded_inputs.get('''input_features'''  )
								if isinstance(input_features[0]			,			__lowerCamelCase  ):
										snake_case__						:       List[str]       						=  [np.asarray(__lowerCamelCase			,			dtype=np.floataa  ) for feature in input_features]

								snake_case__						:       Optional[int]       						=  padded_inputs.get('''attention_mask'''  )
								if attention_mask is not None:
										snake_case__						:       Tuple       						=  [np.asarray(__lowerCamelCase			,			dtype=np.intaa  ) for array in attention_mask]

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

								if return_tensors is not None:
										snake_case__						:       Optional[Any]       						=  padded_inputs.convert_to_tensors(__lowerCamelCase  )

								return padded_inputs






'''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     a			:






						"""simple docstring"""




						def __init__(							self					:      Dict			,			snake_case_					:      Tuple			,			snake_case_					:      List[str]=1_3			,			snake_case_					:      Dict=7			,			snake_case_					:      int=True			,			snake_case_					:      Tuple=True			,			snake_case_					:      int=True			,			snake_case_					:      Any=True			,			snake_case_					:      Union[str, Any]=9_9			,			snake_case_					:      List[Any]=2_4			,			snake_case_					:      Optional[Any]=2			,			snake_case_					:      Union[str, Any]=6			,			snake_case_					:      Any=3_7			,			snake_case_					:      Any="gelu"			,			snake_case_					:      Dict=0.1			,			snake_case_					:      List[str]=0.1			,			snake_case_					:      Dict=5_1_2			,			snake_case_					:      Any=1_6			,			snake_case_					:      Optional[int]=2			,			snake_case_					:      Union[str, Any]=0.0_2			,			snake_case_					:      int=3			,			snake_case_					:      int=None			,			snake_case_					:      Union[str, Any]=1_0_0_0			,			):



								'''simple docstring'''



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







						def       __magic_name__							(							self					:      Union[str, Any]  ):



								'''simple docstring'''



								snake_case__						:       Any       						=  ids_tensor([self.batch_size, self.seq_length]			,			self.vocab_size  )

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

								snake_case__						:       int       						=  None
								if self.use_input_mask:
										snake_case__						:       List[Any]       						=  ids_tensor([self.batch_size, self.seq_length]			,			vocab_size=2  )

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

								snake_case__						:       Tuple       						=  None
								snake_case__						:       Union[str, Any]       						=  None
								if self.use_labels:
										snake_case__						:       int       						=  ids_tensor([self.batch_size]			,			self.type_sequence_label_size  )
										snake_case__						:       List[str]       						=  ids_tensor([self.batch_size, self.seq_length]			,			self.num_labels  )

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

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







						def       __magic_name__							(							self					:      List[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       __magic_name__							(							self					:      Optional[int]			,			snake_case_					:      Any			,			snake_case_					:      List[Any]			,			snake_case_					:      Union[str, Any]			,			snake_case_					:      str			,			snake_case_					:      Optional[Any]			,			snake_case_					:      Union[str, Any]			,			snake_case_					:      Tuple			,			):



								'''simple docstring'''



								snake_case__						:       Optional[Any]       						=  LiltModel(config=snake_case_  )
								model.to(snake_case_  )
								model.eval()
								snake_case__						:       Dict       						=  model(snake_case_			,			bbox=snake_case_			,			attention_mask=snake_case_			,			token_type_ids=snake_case_  )
								snake_case__						:       Optional[Any]       						=  model(snake_case_			,			bbox=snake_case_			,			token_type_ids=snake_case_  )
								snake_case__						:       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       __magic_name__							(							self					:      Union[str, Any]			,			snake_case_					:      Union[str, Any]			,			snake_case_					:      Optional[Any]			,			snake_case_					:      List[Any]			,			snake_case_					:      Any			,			snake_case_					:      int			,			snake_case_					:      int			,			snake_case_					:      Tuple			,			):



								'''simple docstring'''



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



								'''simple docstring'''



								snake_case__						:       Optional[int]       						=  LiltForQuestionAnswering(config=snake_case_  )
								model.to(snake_case_  )
								model.eval()
								snake_case__						:       Optional[int]       						=  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       __magic_name__							(							self					:      Union[str, Any]  ):



								'''simple docstring'''



								snake_case__						:       Dict       						=  self.prepare_config_and_inputs()
								(
								    (
								    snake_case__
								)						,       (
								    snake_case__
								)						,       (
								    snake_case__
								)						,       (
								    snake_case__
								)						,       (
								    snake_case__
								)						,       (
								    snake_case__
								)						,       (
								    snake_case__
								)						,       
								)						:       Tuple       						=  config_and_inputs
								snake_case__						:       List[Any]       						=  {
								    '''input_ids''': input_ids,
								    '''bbox''': bbox,
								    '''token_type_ids''': token_type_ids,
								    '''attention_mask''': input_mask,
								}
								return config, inputs_dict





@require_torch
class     a			(				SCREAMING_SNAKE_CASE			,			SCREAMING_SNAKE_CASE			,			SCREAMING_SNAKE_CASE			,			unittest.TestCase       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    (
						    (
						        LiltModel,
						        LiltForSequenceClassification,
						        LiltForTokenClassification,
						        LiltForQuestionAnswering,
						    )
						    if is_torch_available()
						    else ()
						)
						__UpperCAmelCase			     =    (
						    {
						        """feature-extraction""": LiltModel,
						        """question-answering""": LiltForQuestionAnswering,
						        """text-classification""": LiltForSequenceClassification,
						        """token-classification""": LiltForTokenClassification,
						        """zero-shot""": LiltForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						__UpperCAmelCase			     =    False
						__UpperCAmelCase			     =    False







						def       __magic_name__							(							self					:      List[str]			,			snake_case_					:      Union[str, Any]			,			snake_case_					:      Dict			,			snake_case_					:      Dict			,			snake_case_					:      str			,			snake_case_					:      Any  ):



								'''simple docstring'''



								return True







						def       __magic_name__							(							self					:      Dict  ):



								'''simple docstring'''



								snake_case__						:       List[Any]       						=  LiltModelTester(self  )
								snake_case__						:       Dict       						=  ConfigTester(self			,			config_class=snake_case_			,			hidden_size=3_7  )







						def       __magic_name__							(							self					:      List[str]  ):



								'''simple docstring'''



								self.config_tester.run_common_tests()







						def       __magic_name__							(							self					:      List[str]  ):



								'''simple docstring'''



								snake_case__						:       List[str]       						=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*snake_case_  )







						def       __magic_name__							(							self					:      Optional[Any]  ):



								'''simple docstring'''



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







						def       __magic_name__							(							self					:      Optional[int]  ):



								'''simple docstring'''



								snake_case__						:       Dict       						=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*snake_case_  )







						def       __magic_name__							(							self					:      Optional[Any]  ):



								'''simple docstring'''



								snake_case__						:       List[Any]       						=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*snake_case_  )







						@slow
						def       __magic_name__							(							self					:      str  ):



								'''simple docstring'''



								for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										snake_case__						:       List[str]       						=  LiltModel.from_pretrained(snake_case_  )
										self.assertIsNotNone(snake_case_  )





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






						"""simple docstring"""




						def       __magic_name__							(							self					:      List[str]  ):



								'''simple docstring'''



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

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

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

								snake_case__						:       Tuple       						=  torch.Size([1, 2, 7_6_8]  )
								snake_case__						:       Dict       						=  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  )  )



