



from abc import ABC, abstractmethod
from typing import List, Optional



class       UpperCAmelCase__(							lowerCamelCase	):

   '''simple docstring'''




   def __init__(						self  :			Optional[Any])	->				Optional[Any]:




         """simple docstring"""

         self.test()


   def 			UpperCAmelCase		(						self  :			Union[str, Any])	->				Any:




         """simple docstring"""

         lowercase__     =  0
         lowercase__     =  False
         while not completed:
               if counter == 1:
                     self.reset()
               lowercase__     =  self.advance()
               if not self.does_advance(lowerCAmelCase):
                     raise Exception(
                         'Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.')

               lowercase__,	lowercase__,	lowercase__     =  self.update(lowerCAmelCase)
               counter += 1

               if counter > 1_00_00:
                     raise Exception('update() does not fulfill the constraint.')

         if self.remaining() != 0:
               raise Exception('Custom Constraint is not defined correctly.')


   @abstractmethod
   def 			UpperCAmelCase		(						self  :			Union[str, Any])	->				str:




         """simple docstring"""

         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''')


   @abstractmethod
   def 			UpperCAmelCase		(						self  :			Any			,				lowerCAmelCase  :			int)	->				List[str]:




         """simple docstring"""

         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''')


   @abstractmethod
   def 			UpperCAmelCase		(						self  :			int			,				lowerCAmelCase  :			int)	->				Optional[int]:




         """simple docstring"""

         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''')


   @abstractmethod
   def 			UpperCAmelCase		(						self  :			str)	->				List[Any]:




         """simple docstring"""

         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''')


   @abstractmethod
   def 			UpperCAmelCase		(						self  :			str)	->				Any:




         """simple docstring"""

         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''')



   @abstractmethod
   def 			UpperCAmelCase		(						self  :			Optional[Any]			,				lowerCAmelCase  :			Optional[Any]=False)	->				int:




         """simple docstring"""

         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''')



class       UpperCAmelCase__(							lowerCamelCase	):

   '''simple docstring'''




   def __init__(						self  :			Dict			,				lowerCAmelCase  :			List[int])	->				Union[str, Any]:




         """simple docstring"""

         super(lowerCAmelCase			,				self).__init__()

         if not isinstance(lowerCAmelCase			,				lowerCAmelCase) or len(lowerCAmelCase) == 0:
               raise ValueError(f'''`token_ids` has to be a non-empty list, but is {token_ids}.''')
         if any((not isinstance(lowerCAmelCase			,				lowerCAmelCase) or token_id < 0) for token_id in token_ids):
               raise ValueError(f'''Each list in `token_ids` has to be a list of positive integers, but is {token_ids}.''')

         lowercase__     =  token_ids

         lowercase__     =  len(self.token_ids)
         lowercase__     =  -1  # the index of the currently fulfilled step
         lowercase__     =  False


   def 			UpperCAmelCase		(						self  :			Tuple)	->				Any:




         """simple docstring"""

         if self.completed:
               return None
         return self.token_ids[self.fulfilled_idx + 1]


   def 			UpperCAmelCase		(						self  :			Union[str, Any]			,				lowerCAmelCase  :			int)	->				int:




         """simple docstring"""

         if not isinstance(lowerCAmelCase			,				lowerCAmelCase):
               raise ValueError(f'''`token_id` has to be an `int`, but is {token_id} of type {type(lowerCAmelCase)}''')

         if self.completed:
               return False

         return token_id == self.token_ids[self.fulfilled_idx + 1]


   def 			UpperCAmelCase		(						self  :			Any			,				lowerCAmelCase  :			int)	->				Optional[int]:




         """simple docstring"""

         if not isinstance(lowerCAmelCase			,				lowerCAmelCase):
               raise ValueError(f'''`token_id` has to be an `int`, but is {token_id} of type {type(lowerCAmelCase)}''')

         lowercase__     =  False
         lowercase__     =  False
         lowercase__     =  False

         if self.does_advance(lowerCAmelCase):
               self.fulfilled_idx += 1
               lowercase__     =  True
               if self.fulfilled_idx == (self.seqlen - 1):
                     lowercase__     =  True
               lowercase__     =  completed
         else:
               # failed to make progress.
               lowercase__     =  True
               self.reset()
         return stepped, completed, reset


   def 			UpperCAmelCase		(						self  :			Optional[Any])	->				List[str]:




         """simple docstring"""

         lowercase__     =  False
         lowercase__     =  0


   def 			UpperCAmelCase		(						self  :			Any)	->				str:




         """simple docstring"""

         return self.seqlen - (self.fulfilled_idx + 1)



   def 			UpperCAmelCase		(						self  :			List[Any]			,				lowerCAmelCase  :			List[str]=False)	->				Any:




         """simple docstring"""

         lowercase__     =  PhrasalConstraint(self.token_ids)

         if stateful:
               lowercase__     =  self.seqlen
               lowercase__     =  self.fulfilled_idx
               lowercase__     =  self.completed

         return new_constraint



class       UpperCAmelCase__:

   '''simple docstring'''




   def __init__(						self  :			Union[str, Any]			,				lowerCAmelCase  :			List[List[int]]			,				lowerCAmelCase  :			Any=True)	->				Tuple:




         """simple docstring"""

         lowercase__     =  max([len(lowerCAmelCase) for one in nested_token_ids])

         lowercase__     =  {}
         for token_ids in nested_token_ids:
               lowercase__     =  root
               for tidx, token_id in enumerate(lowerCAmelCase):
                     if token_id not in level:
                           lowercase__     =  {}

                     lowercase__     =  level[token_id]

         if no_subsets and self.has_subsets(lowerCAmelCase			,				lowerCAmelCase):
               raise ValueError(
                   'Each list in `nested_token_ids` can\'t be a complete subset of another list, but is'
                   f''' {nested_token_ids}.''')

         lowercase__     =  root


   def 			UpperCAmelCase		(						self  :			Optional[Any]			,				lowerCAmelCase  :			int)	->				List[str]:




         """simple docstring"""

         lowercase__     =  self.trie

         for current_token in current_seq:
               lowercase__     =  start[current_token]

         lowercase__     =  list(start.keys())

         return next_tokens


   def 			UpperCAmelCase		(						self  :			Tuple			,				lowerCAmelCase  :			int)	->				int:




         """simple docstring"""

         lowercase__     =  self.next_tokens(lowerCAmelCase)

         return len(lowerCAmelCase) == 0


   def 			UpperCAmelCase		(						self  :			List[Any]			,				lowerCAmelCase  :			Dict)	->				Any:




         """simple docstring"""

         lowercase__     =  list(root.values())
         if len(lowerCAmelCase) == 0:
               return 1
         else:
               return sum([self.count_leaves(lowerCAmelCase) for nn in next_nodes])



   def 			UpperCAmelCase		(						self  :			List[Any]			,				lowerCAmelCase  :			str			,				lowerCAmelCase  :			List[str])	->				Dict:




         """simple docstring"""

         lowercase__     =  self.count_leaves(lowerCAmelCase)
         return len(lowerCAmelCase) != leaf_count



class       UpperCAmelCase__(							lowerCamelCase	):

   '''simple docstring'''




   def __init__(						self  :			Optional[int]			,				lowerCAmelCase  :			List[List[int]])	->				int:




         """simple docstring"""

         super(lowerCAmelCase			,				self).__init__()

         if not isinstance(lowerCAmelCase			,				lowerCAmelCase) or len(lowerCAmelCase) == 0:
               raise ValueError(f'''`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}.''')
         if any(not isinstance(lowerCAmelCase			,				lowerCAmelCase) for token_ids in nested_token_ids):
               raise ValueError(f'''`nested_token_ids` has to be a list of lists, but is {nested_token_ids}.''')
         if any(
             any((not isinstance(lowerCAmelCase			,				lowerCAmelCase) or token_id < 0) for token_id in token_ids)
             for token_ids in nested_token_ids):
               raise ValueError(
                   f'''Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}.''')

         lowercase__     =  DisjunctiveTrie(lowerCAmelCase)
         lowercase__     =  nested_token_ids

         lowercase__     =  self.trie.max_height
         lowercase__     =  []
         lowercase__     =  False


   def 			UpperCAmelCase		(						self  :			Optional[Any])	->				Optional[Any]:




         """simple docstring"""

         lowercase__     =  self.trie.next_tokens(self.current_seq)

         if len(lowerCAmelCase) == 0:
               return None
         else:
               return token_list


   def 			UpperCAmelCase		(						self  :			Optional[int]			,				lowerCAmelCase  :			int)	->				List[Any]:




         """simple docstring"""

         if not isinstance(lowerCAmelCase			,				lowerCAmelCase):
               raise ValueError(f'''`token_id` is supposed to be type `int`, but is {token_id} of type {type(lowerCAmelCase)}''')

         lowercase__     =  self.trie.next_tokens(self.current_seq)

         return token_id in next_tokens


   def 			UpperCAmelCase		(						self  :			List[str]			,				lowerCAmelCase  :			int)	->				int:




         """simple docstring"""

         if not isinstance(lowerCAmelCase			,				lowerCAmelCase):
               raise ValueError(f'''`token_id` is supposed to be type `int`, but is {token_id} of type {type(lowerCAmelCase)}''')

         lowercase__     =  False
         lowercase__     =  False
         lowercase__     =  False

         if self.does_advance(lowerCAmelCase):
               self.current_seq.append(lowerCAmelCase)
               lowercase__     =  True
         else:
               lowercase__     =  True
               self.reset()

         lowercase__     =  self.trie.reached_leaf(self.current_seq)
         lowercase__     =  completed

         return stepped, completed, reset


   def 			UpperCAmelCase		(						self  :			str)	->				Optional[Any]:




         """simple docstring"""

         lowercase__     =  False
         lowercase__     =  []


   def 			UpperCAmelCase		(						self  :			int)	->				Dict:




         """simple docstring"""

         if self.completed:
               # since this can be completed without reaching max height
               return 0
         else:
               return self.seqlen - len(self.current_seq)



   def 			UpperCAmelCase		(						self  :			List[str]			,				lowerCAmelCase  :			Union[str, Any]=False)	->				str:




         """simple docstring"""

         lowercase__     =  DisjunctiveConstraint(self.token_ids)

         if stateful:
               lowercase__     =  self.seqlen
               lowercase__     =  self.current_seq
               lowercase__     =  self.completed

         return new_constraint



class       UpperCAmelCase__:

   '''simple docstring'''




   def __init__(						self  :			int			,				lowerCAmelCase  :			List[Constraint])	->				Tuple:




         """simple docstring"""

         lowercase__     =  constraints

         # max # of steps required to fulfill a given constraint
         lowercase__     =  max([c.seqlen for c in constraints])
         lowercase__     =  len(lowerCAmelCase)
         lowercase__     =  False

         self.init_state()


   def 			UpperCAmelCase		(						self  :			str)	->				Tuple:




         """simple docstring"""

         lowercase__     =  []
         lowercase__     =  None
         lowercase__     =  [constraint.copy(stateful=lowerCAmelCase) for constraint in self.constraints]


   def 			UpperCAmelCase		(						self  :			Any)	->				Optional[Any]:




         """simple docstring"""

         lowercase__     =  0
         if self.inprogress_constraint:
               # extra points for having a constraint mid-fulfilled
               add += self.max_seqlen - self.inprogress_constraint.remaining()

         return (len(self.complete_constraints) * self.max_seqlen) + add


   def 			UpperCAmelCase		(						self  :			int)	->				str:




         """simple docstring"""

         lowercase__     =  []
         if self.inprogress_constraint is None:
               for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
                     lowercase__     =  constraint.advance()
                     if isinstance(lowerCAmelCase			,				lowerCAmelCase):
                           token_list.append(lowerCAmelCase)
                     elif isinstance(lowerCAmelCase			,				lowerCAmelCase):
                           token_list.extend(lowerCAmelCase)
         else:
               lowercase__     =  self.inprogress_constraint.advance()
               if isinstance(lowerCAmelCase			,				lowerCAmelCase):
                     token_list.append(lowerCAmelCase)
               elif isinstance(lowerCAmelCase			,				lowerCAmelCase):
                     token_list.extend(lowerCAmelCase)

         if len(lowerCAmelCase) == 0:
               return None
         else:
               return token_list


   def 			UpperCAmelCase		(						self  :			Optional[Any]			,				lowerCAmelCase  :			Optional[List[int]])	->				int:




         """simple docstring"""

         self.init_state()

         if token_ids is not None:
               for token in token_ids:
                     # completes or steps **one** constraint
                     lowercase__,	lowercase__     =  self.add(lowerCAmelCase)

                     # the entire list of constraints are fulfilled
                     if self.completed:
                           break


   def 			UpperCAmelCase		(						self  :			Union[str, Any]			,				lowerCAmelCase  :			int)	->				Dict:




         """simple docstring"""

         if not isinstance(lowerCAmelCase			,				lowerCAmelCase):
               raise ValueError(f'''`token_id` should be an `int`, but is `{token_id}`.''')

         lowercase__,	lowercase__     =  False, False

         if self.completed:
               lowercase__     =  True
               lowercase__     =  False
               return complete, stepped

         if self.inprogress_constraint is not None:
               # In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current
               # job, simply update the state

               lowercase__,	lowercase__,	lowercase__     =  self.inprogress_constraint.update(lowerCAmelCase)
               if reset:
                     # 1. If the next token breaks the progress, then we must restart.
                     #     e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books".

                     #     But that doesn't mean we self.init_state(), since we only reset the state for this particular
                     #     constraint, not the full list of constraints.

                     self.pending_constraints.append(self.inprogress_constraint.copy(stateful=lowerCAmelCase))
                     lowercase__     =  None

               if complete:
                     # 2. If the next token completes the constraint, move it to completed list, set
                     #     inprogress to None. If there are no pending constraints either, then this full list of constraints
                     #     is complete.

                     self.complete_constraints.append(self.inprogress_constraint)
                     lowercase__     =  None

                     if len(self.pending_constraints) == 0:
                           # we're done!
                           lowercase__     =  True

         else:
               # Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list
               # of constraints?

               for cidx, pending_constraint in enumerate(self.pending_constraints):
                     if pending_constraint.does_advance(lowerCAmelCase):
                           lowercase__,	lowercase__,	lowercase__     =  pending_constraint.update(lowerCAmelCase)

                           if not stepped:
                                 raise Exception(
                                     '`constraint.update(token_id)` is not yielding incremental progress, '
                                     'even though `constraint.does_advance(token_id)` is true.')

                           if complete:
                                 self.complete_constraints.append(lowerCAmelCase)
                                 lowercase__     =  None

                           if not complete and stepped:
                                 lowercase__     =  pending_constraint

                           if complete or stepped:
                                 # If we made any progress at all, then it's at least not a "pending constraint".

                                 lowercase__     =  (
                                     self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :]
                                 )

                                 if len(self.pending_constraints) == 0 and self.inprogress_constraint is None:
                                       # If there's no longer any pending after this and no inprogress either, then we must be
                                       # complete.

                                       lowercase__     =  True

                                 break  # prevent accidentally stepping through multiple constraints with just one token.

         return complete, stepped



   def 			UpperCAmelCase		(						self  :			Dict			,				lowerCAmelCase  :			Dict=True)	->				List[str]:




         """simple docstring"""

         lowercase__     =  ConstraintListState(self.constraints)  # we actually never though self.constraints objects
         # throughout this process. So it's at initialization state.

         if stateful:
               lowercase__     =  [
                   constraint.copy(stateful=lowerCAmelCase) for constraint in self.complete_constraints
               ]
               if self.inprogress_constraint is not None:
                     lowercase__     =  self.inprogress_constraint.copy(stateful=lowerCAmelCase)
               lowercase__     =  [constraint.copy() for constraint in self.pending_constraints]

         return new_state





import numpy as np

from transformers import BatchFeature
from transformers.testing_utils import require_tf, require_torch

from .test_feature_extraction_common import FeatureExtractionSavingTestMixin



class       UpperCAmelCase__(							lowerCamelCase	):

   '''simple docstring'''




   A    :  List[Any]       				=    None
   A    :  Optional[int]       				=    None


   @property
   def 			UpperCAmelCase		(						self  :			str)	->				Union[str, Any]:




         """simple docstring"""

         return self.feat_extract_tester.prepare_feat_extract_dict()


   def 			UpperCAmelCase		(						self  :			int)	->				Any:




         """simple docstring"""

         lowercase__     =  self.feature_extraction_class(**self.feat_extract_dict)
         self.assertTrue(hasattr(lowerCAmelCase			,				'feature_size'))
         self.assertTrue(hasattr(lowerCAmelCase			,				'sampling_rate'))
         self.assertTrue(hasattr(lowerCAmelCase			,				'padding_value'))


   def 			UpperCAmelCase		(						self  :			Union[str, Any])	->				Dict:




         """simple docstring"""

         lowercase__     =  self.feat_extract_tester.prepare_inputs_for_common()
         lowercase__     =  self.feature_extraction_class(**self.feat_extract_dict)
         lowercase__     =  feat_extract.model_input_names[0]

         lowercase__     =  BatchFeature({input_name: speech_inputs})

         self.assertTrue(all(len(lowerCAmelCase) == len(lowerCAmelCase) for x, y in zip(lowerCAmelCase			,				processed_features[input_name])))

         lowercase__     =  self.feat_extract_tester.prepare_inputs_for_common(equal_length=lowerCAmelCase)
         lowercase__     =  BatchFeature({input_name: speech_inputs}			,				tensor_type='np')

         lowercase__     =  processed_features[input_name]

         if len(batch_features_input.shape) < 3:
               lowercase__     =  batch_features_input[:, :, None]

         self.assertTrue(
             batch_features_input.shape
             == (self.feat_extract_tester.batch_size, len(speech_inputs[0]), self.feat_extract_tester.feature_size))


   @require_torch
   def 			UpperCAmelCase		(						self  :			Dict)	->				int:




         """simple docstring"""

         lowercase__     =  self.feat_extract_tester.prepare_inputs_for_common(equal_length=lowerCAmelCase)
         lowercase__     =  self.feature_extraction_class(**self.feat_extract_dict)
         lowercase__     =  feat_extract.model_input_names[0]

         lowercase__     =  BatchFeature({input_name: speech_inputs}			,				tensor_type='pt')

         lowercase__     =  processed_features[input_name]

         if len(batch_features_input.shape) < 3:
               lowercase__     =  batch_features_input[:, :, None]

         self.assertTrue(
             batch_features_input.shape
             == (self.feat_extract_tester.batch_size, len(speech_inputs[0]), self.feat_extract_tester.feature_size))


   @require_tf
   def 			UpperCAmelCase		(						self  :			Optional[Any])	->				Optional[int]:




         """simple docstring"""

         lowercase__     =  self.feat_extract_tester.prepare_inputs_for_common(equal_length=lowerCAmelCase)
         lowercase__     =  self.feature_extraction_class(**self.feat_extract_dict)
         lowercase__     =  feat_extract.model_input_names[0]

         lowercase__     =  BatchFeature({input_name: speech_inputs}			,				tensor_type='tf')

         lowercase__     =  processed_features[input_name]

         if len(batch_features_input.shape) < 3:
               lowercase__     =  batch_features_input[:, :, None]

         self.assertTrue(
             batch_features_input.shape
             == (self.feat_extract_tester.batch_size, len(speech_inputs[0]), self.feat_extract_tester.feature_size))


   def 			UpperCAmelCase		(						self  :			str			,				lowerCAmelCase  :			str=False)	->				Union[str, Any]:




         """simple docstring"""

         def _inputs_have_equal_length(lowerCAmelCase  :			int):
               lowercase__     =  len(input[0])
               for input_slice in input[1:]:
                     if len(lowerCAmelCase) != length:
                           return False
               return True

         def _inputs_are_equal(lowerCAmelCase  :			Optional[Any]			,				lowerCAmelCase  :			Tuple):
               if len(lowerCAmelCase) != len(lowerCAmelCase):
                     return False

               for input_slice_a, input_slice_a in zip(lowerCAmelCase			,				lowerCAmelCase):
                     if not np.allclose(np.asarray(lowerCAmelCase)			,				np.asarray(lowerCAmelCase)			,				atol=1E-3):
                           return False
               return True

         lowercase__     =  self.feature_extraction_class(**self.feat_extract_dict)
         lowercase__     =  self.feat_extract_tester.prepare_inputs_for_common(numpify=lowerCAmelCase)
         lowercase__     =  feat_extract.model_input_names[0]

         lowercase__     =  BatchFeature({input_name: speech_inputs})

         lowercase__     =  self.feat_extract_tester.seq_length_diff
         lowercase__     =  self.feat_extract_tester.max_seq_length + pad_diff
         lowercase__     =  self.feat_extract_tester.min_seq_length
         lowercase__     =  self.feat_extract_tester.batch_size
         lowercase__     =  self.feat_extract_tester.feature_size

         # test padding for List[int] + numpy
         lowercase__     =  feat_extract.pad(lowerCAmelCase			,				padding=lowerCAmelCase)
         lowercase__     =  input_a[input_name]

         lowercase__     =  feat_extract.pad(lowerCAmelCase			,				padding='longest')
         lowercase__     =  input_a[input_name]

         lowercase__     =  feat_extract.pad(lowerCAmelCase			,				padding='max_length'			,				max_length=len(speech_inputs[-1]))
         lowercase__     =  input_a[input_name]

         lowercase__     =  feat_extract.pad(lowerCAmelCase			,				padding='longest'			,				return_tensors='np')
         lowercase__     =  input_a[input_name]

         # max_length parameter has to be provided when setting `padding="max_length"`
         with self.assertRaises(lowerCAmelCase):
               feat_extract.pad(lowerCAmelCase			,				padding='max_length')[input_name]

         lowercase__     =  feat_extract.pad(
             lowerCAmelCase			,				padding='max_length'			,				max_length=lowerCAmelCase			,				return_tensors='np')
         lowercase__     =  input_a[input_name]

         self.assertFalse(_inputs_have_equal_length(lowerCAmelCase))
         self.assertTrue(_inputs_have_equal_length(lowerCAmelCase))
         self.assertTrue(_inputs_have_equal_length(lowerCAmelCase))
         self.assertTrue(_inputs_are_equal(lowerCAmelCase			,				lowerCAmelCase))
         self.assertTrue(len(input_a[0]) == pad_min_length)
         self.assertTrue(len(input_a[1]) == pad_min_length + pad_diff)
         self.assertTrue(input_a.shape[:2] == (batch_size, len(input_a[0])))
         self.assertTrue(input_a.shape[:2] == (batch_size, pad_max_length))

         if feature_size > 1:
               self.assertTrue(input_a.shape[2] == input_a.shape[2] == feature_size)

         # test padding for `pad_to_multiple_of` for List[int] + numpy
         lowercase__     =  feat_extract.pad(lowerCAmelCase			,				pad_to_multiple_of=10)
         lowercase__     =  input_a[input_name]

         lowercase__     =  feat_extract.pad(lowerCAmelCase			,				padding='longest'			,				pad_to_multiple_of=10)
         lowercase__     =  input_a[input_name]

         lowercase__     =  feat_extract.pad(
             lowerCAmelCase			,				padding='max_length'			,				pad_to_multiple_of=10			,				max_length=lowerCAmelCase)
         lowercase__     =  input_a[input_name]

         lowercase__     =  feat_extract.pad(
             lowerCAmelCase			,				padding='max_length'			,				pad_to_multiple_of=10			,				max_length=lowerCAmelCase			,				return_tensors='np'			,				)
         lowercase__     =  input_a[input_name]

         self.assertTrue(all(len(lowerCAmelCase) % 10 == 0 for x in input_a))
         self.assertTrue(_inputs_are_equal(lowerCAmelCase			,				lowerCAmelCase))

         lowercase__     =  pad_max_length if pad_max_length % 10 == 0 else (pad_max_length // 10 + 1) * 10
         self.assertTrue(all(len(lowerCAmelCase) == expected_mult_pad_length for x in input_a))
         self.assertEqual(input_a.shape[:2]			,				(batch_size, expected_mult_pad_length))

         if feature_size > 1:
               self.assertTrue(input_a.shape[2] == feature_size)

         # Check padding value is correct
         lowercase__     =  (np.ones(self.feat_extract_tester.feature_size) * feat_extract.padding_value).sum()
         self.assertTrue(
             abs(np.asarray(input_a[0])[pad_min_length:].sum() - padding_vector_sum * (pad_max_length - pad_min_length))
             < 1E-3)
         self.assertTrue(
             abs(
                 np.asarray(input_a[1])[pad_min_length + pad_diff :].sum()
                 - padding_vector_sum * (pad_max_length - pad_min_length - pad_diff))
             < 1E-3)
         self.assertTrue(
             abs(
                 np.asarray(input_a[2])[pad_min_length + 2 * pad_diff :].sum()
                 - padding_vector_sum * (pad_max_length - pad_min_length - 2 * pad_diff))
             < 1E-3)
         self.assertTrue(
             abs(input_a[0, pad_min_length:].sum() - padding_vector_sum * (pad_max_length - pad_min_length)) < 1E-3)
         self.assertTrue(
             abs(input_a[0, pad_min_length:].sum() - padding_vector_sum * (expected_mult_pad_length - pad_min_length))
             < 1E-3)


   def 			UpperCAmelCase		(						self  :			Tuple			,				lowerCAmelCase  :			Dict=False)	->				str:




         """simple docstring"""

         def _inputs_have_equal_length(lowerCAmelCase  :			int):
               lowercase__     =  len(input[0])
               for input_slice in input[1:]:
                     if len(lowerCAmelCase) != length:
                           return False
               return True

         def _inputs_are_equal(lowerCAmelCase  :			str			,				lowerCAmelCase  :			Optional[Any]):
               if len(lowerCAmelCase) != len(lowerCAmelCase):
                     return False

               for input_slice_a, input_slice_a in zip(lowerCAmelCase			,				lowerCAmelCase):
                     if not np.allclose(np.asarray(lowerCAmelCase)			,				np.asarray(lowerCAmelCase)			,				atol=1E-3):
                           return False
               return True

         lowercase__     =  self.feature_extraction_class(**self.feat_extract_dict)
         lowercase__     =  self.feat_extract_tester.prepare_inputs_for_common(numpify=lowerCAmelCase)
         lowercase__     =  feat_extract.model_input_names[0]

         lowercase__     =  BatchFeature({input_name: speech_inputs})

         # truncate to smallest
         lowercase__     =  feat_extract.pad(
             lowerCAmelCase			,				padding='max_length'			,				max_length=len(speech_inputs[0])			,				truncation=lowerCAmelCase)
         lowercase__     =  input_a[input_name]

         lowercase__     =  feat_extract.pad(lowerCAmelCase			,				padding='max_length'			,				max_length=len(speech_inputs[0]))
         lowercase__     =  input_a[input_name]

         self.assertTrue(_inputs_have_equal_length(lowerCAmelCase))
         self.assertFalse(_inputs_have_equal_length(lowerCAmelCase))

         # truncate to smallest with np
         lowercase__     =  feat_extract.pad(
             lowerCAmelCase			,				padding='max_length'			,				max_length=len(speech_inputs[0])			,				return_tensors='np'			,				truncation=lowerCAmelCase			,				)
         lowercase__     =  input_a[input_name]

         lowercase__     =  feat_extract.pad(
             lowerCAmelCase			,				padding='max_length'			,				max_length=len(speech_inputs[0])			,				return_tensors='np')
         lowercase__     =  input_a[input_name]

         self.assertTrue(_inputs_have_equal_length(lowerCAmelCase))
         self.assertTrue(input_a.shape[1] == len(speech_inputs[0]))

         # since truncation forces padding to be smaller than longest input
         # function can't return `np.ndarray`, but has to return list
         self.assertFalse(_inputs_have_equal_length(lowerCAmelCase))

         # truncate to middle
         lowercase__     =  feat_extract.pad(
             lowerCAmelCase			,				padding='max_length'			,				max_length=len(speech_inputs[1])			,				truncation=lowerCAmelCase			,				return_tensors='np'			,				)
         lowercase__     =  input_a[input_name]

         lowercase__     =  feat_extract.pad(
             lowerCAmelCase			,				padding='max_length'			,				max_length=len(speech_inputs[1])			,				truncation=lowerCAmelCase)
         lowercase__     =  input_a[input_name]

         lowercase__     =  feat_extract.pad(
             lowerCAmelCase			,				padding='max_length'			,				max_length=len(speech_inputs[1])			,				return_tensors='np')
         lowercase__     =  input_a[input_name]

         self.assertTrue(input_a.shape[1] == len(speech_inputs[1]))
         self.assertTrue(_inputs_have_equal_length(lowerCAmelCase))
         self.assertTrue(_inputs_have_equal_length(lowerCAmelCase))
         self.assertTrue(_inputs_are_equal(lowerCAmelCase			,				lowerCAmelCase))

         # since truncation forces padding to be smaller than longest input
         # function can't return `np.ndarray`, but has to return list
         self.assertFalse(_inputs_have_equal_length(lowerCAmelCase))
         self.assertTrue(len(input_a[-1]) == len(speech_inputs[-1]))

         # padding has to be max_length when setting `truncation=True`
         with self.assertRaises(lowerCAmelCase):
               feat_extract.pad(lowerCAmelCase			,				truncation=lowerCAmelCase)[input_name]

         # padding has to be max_length when setting `truncation=True`
         with self.assertRaises(lowerCAmelCase):
               feat_extract.pad(lowerCAmelCase			,				padding='longest'			,				truncation=lowerCAmelCase)[input_name]

         # padding has to be max_length when setting `truncation=True`
         with self.assertRaises(lowerCAmelCase):
               feat_extract.pad(lowerCAmelCase			,				padding='longest'			,				truncation=lowerCAmelCase)[input_name]

         # max_length parameter has to be provided when setting `truncation=True` and padding="max_length"
         with self.assertRaises(lowerCAmelCase):
               feat_extract.pad(lowerCAmelCase			,				padding='max_length'			,				truncation=lowerCAmelCase)[input_name]

         # test truncation for `pad_to_multiple_of` for List[int] + numpy
         lowercase__     =  12
         lowercase__     =  feat_extract.pad(
             lowerCAmelCase			,				padding='max_length'			,				max_length=len(speech_inputs[0])			,				pad_to_multiple_of=lowerCAmelCase			,				truncation=lowerCAmelCase			,				)
         lowercase__     =  input_a[input_name]

         lowercase__     =  feat_extract.pad(
             lowerCAmelCase			,				padding='max_length'			,				max_length=len(speech_inputs[0])			,				pad_to_multiple_of=lowerCAmelCase			,				)
         lowercase__     =  input_a[input_name]

         # retrieve expected_length as multiple of pad_to_multiple_of
         lowercase__     =  len(speech_inputs[0])
         if expected_length % pad_to_multiple_of != 0:
               lowercase__     =  ((len(speech_inputs[0]) // pad_to_multiple_of) + 1) * pad_to_multiple_of

         self.assertTrue(len(input_a[0]) == expected_length)
         self.assertTrue(_inputs_have_equal_length(lowerCAmelCase))
         self.assertFalse(_inputs_have_equal_length(lowerCAmelCase))


   def 			UpperCAmelCase		(						self  :			List[str])	->				List[str]:




         """simple docstring"""

         self._check_padding(numpify=lowerCAmelCase)


   def 			UpperCAmelCase		(						self  :			Any)	->				Optional[Any]:




         """simple docstring"""

         self._check_padding(numpify=lowerCAmelCase)


   def 			UpperCAmelCase		(						self  :			List[Any])	->				int:




         """simple docstring"""

         self._check_truncation(numpify=lowerCAmelCase)


   def 			UpperCAmelCase		(						self  :			Union[str, Any])	->				Dict:




         """simple docstring"""

         self._check_truncation(numpify=lowerCAmelCase)


   @require_torch
   def 			UpperCAmelCase		(						self  :			Dict)	->				List[str]:




         """simple docstring"""

         lowercase__     =  self.feature_extraction_class(**self.feat_extract_dict)
         lowercase__     =  self.feat_extract_tester.prepare_inputs_for_common()
         lowercase__     =  feat_extract.model_input_names[0]

         lowercase__     =  BatchFeature({input_name: speech_inputs})

         lowercase__     =  feat_extract.pad(lowerCAmelCase			,				padding='longest'			,				return_tensors='np')[input_name]
         lowercase__     =  feat_extract.pad(lowerCAmelCase			,				padding='longest'			,				return_tensors='pt')[input_name]

         self.assertTrue(abs(input_np.astype(np.floataa).sum() - input_pt.numpy().astype(np.floataa).sum()) < 1E-2)


   @require_tf
   def 			UpperCAmelCase		(						self  :			str)	->				str:




         """simple docstring"""

         lowercase__     =  self.feature_extraction_class(**self.feat_extract_dict)
         lowercase__     =  self.feat_extract_tester.prepare_inputs_for_common()
         lowercase__     =  feat_extract.model_input_names[0]

         lowercase__     =  BatchFeature({input_name: speech_inputs})

         lowercase__     =  feat_extract.pad(lowerCAmelCase			,				padding='longest'			,				return_tensors='np')[input_name]
         lowercase__     =  feat_extract.pad(lowerCAmelCase			,				padding='longest'			,				return_tensors='tf')[input_name]

         self.assertTrue(abs(input_np.astype(np.floataa).sum() - input_tf.numpy().astype(np.floataa).sum()) < 1E-2)


   def 			UpperCAmelCase		(						self  :			Optional[Any])	->				Tuple:




         """simple docstring"""

         lowercase__     =  self.feat_extract_dict
         lowercase__     =  True
         lowercase__     =  self.feature_extraction_class(**lowerCAmelCase)
         lowercase__     =  self.feat_extract_tester.prepare_inputs_for_common()
         lowercase__     =  [len(lowerCAmelCase) for x in speech_inputs]
         lowercase__     =  feat_extract.model_input_names[0]

         lowercase__     =  BatchFeature({input_name: speech_inputs})

         lowercase__     =  feat_extract.pad(lowerCAmelCase			,				padding='longest'			,				return_tensors='np')
         self.assertIn('attention_mask'			,				lowerCAmelCase)
         self.assertListEqual(list(processed.attention_mask.shape)			,				list(processed[input_name].shape[:2]))
         self.assertListEqual(processed.attention_mask.sum(-1).tolist()			,				lowerCAmelCase)



   def 			UpperCAmelCase		(						self  :			str)	->				Tuple:




         """simple docstring"""

         lowercase__     =  self.feat_extract_dict
         lowercase__     =  True
         lowercase__     =  self.feature_extraction_class(**lowerCAmelCase)
         lowercase__     =  self.feat_extract_tester.prepare_inputs_for_common()
         lowercase__     =  [len(lowerCAmelCase) for x in speech_inputs]
         lowercase__     =  feat_extract.model_input_names[0]

         lowercase__     =  BatchFeature({input_name: speech_inputs})
         lowercase__     =  min(lowerCAmelCase)

         lowercase__     =  feat_extract.pad(
             lowerCAmelCase			,				padding='max_length'			,				max_length=lowerCAmelCase			,				truncation=lowerCAmelCase			,				return_tensors='np')
         self.assertIn('attention_mask'			,				lowerCAmelCase)
         self.assertListEqual(
             list(processed_pad.attention_mask.shape)			,				[processed_pad[input_name].shape[0], max_length])
         self.assertListEqual(
             processed_pad.attention_mask[:, :max_length].sum(-1).tolist()			,				[max_length for x in speech_inputs])
