





'''simple docstring'''







from abc import ABC, abstractmethod
from typing import List, Optional
class 			_lowercase						(       __a					):







					def __init__( self    ) ->	List[str]:
						# test for the above condition
						self.test()







					def     UpperCamelCase			( self    ) ->	Dict:
						snake_case				=			0
						snake_case				=			False
						while not completed:
							if counter == 1:
								self.reset()
							snake_case				=			self.advance()
							if not self.does_advance(A__    ):
								raise Exception(
								    '''Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.'''    )

							snake_case				,		snake_case				,		snake_case				=			self.update(A__    )
							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    ) ->	List[str]:
						raise NotImplementedError(
						    F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""    )







					@abstractmethod
					def     UpperCamelCase			( self    ,   A__    ) ->	int:
						raise NotImplementedError(
						    F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""    )







					@abstractmethod
					def     UpperCamelCase			( self    ,   A__    ) ->	Tuple:
						raise NotImplementedError(
						    F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""    )







					@abstractmethod
					def     UpperCamelCase			( self    ) ->	Any:
						raise NotImplementedError(
						    F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""    )







					@abstractmethod
					def     UpperCamelCase			( self    ) ->	Tuple:
						raise NotImplementedError(
						    F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""    )







					@abstractmethod
					def     UpperCamelCase			( self    ,   A__=False    ) ->	Optional[Any]:
						raise NotImplementedError(
						    F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""    )




class 			_lowercase						(       __a					):







					def __init__( self    ,   A__    ) ->	Dict:
						super(A__    ,   self    ).__init__()

						if not isinstance(A__    ,   A__    ) or len(A__    ) == 0:
							raise ValueError(F"""`token_ids` has to be a non-empty list, but is {token_ids}."""    )
						if any((not isinstance(A__    ,   A__    ) 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}."""    )

						snake_case				=			token_ids

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







					def     UpperCamelCase			( self    ) ->	Any:
						if self.completed:
							return None
						return self.token_ids[self.fulfilled_idx + 1]







					def     UpperCamelCase			( self    ,   A__    ) ->	Optional[int]:
						if not isinstance(A__    ,   A__    ):
							raise ValueError(F"""`token_id` has to be an `int`, but is {token_id} of type {type(A__    )}"""    )

						if self.completed:
							return False

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







					def     UpperCamelCase			( self    ,   A__    ) ->	int:
						if not isinstance(A__    ,   A__    ):
							raise ValueError(F"""`token_id` has to be an `int`, but is {token_id} of type {type(A__    )}"""    )

						snake_case				=			False
						snake_case				=			False
						snake_case				=			False

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







					def     UpperCamelCase			( self    ) ->	int:
						snake_case				=			False
						snake_case				=			0







					def     UpperCamelCase			( self    ) ->	Tuple:
						return self.seqlen - (self.fulfilled_idx + 1)







					def     UpperCamelCase			( self    ,   A__=False    ) ->	int:
						snake_case				=			PhrasalConstraint(self.token_ids    )

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

						return new_constraint




class 			_lowercase						:







					def __init__( self    ,   A__    ,   A__=True    ) ->	Optional[Any]:
						snake_case				=			max([len(A__    ) for one in nested_token_ids]    )

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

								snake_case				=			level[token_id]

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

						snake_case				=			root







					def     UpperCamelCase			( self    ,   A__    ) ->	List[Any]:
						snake_case				=			self.trie

						for current_token in current_seq:
							snake_case				=			start[current_token]

						snake_case				=			list(start.keys()    )

						return next_tokens







					def     UpperCamelCase			( self    ,   A__    ) ->	Any:
						snake_case				=			self.next_tokens(A__    )

						return len(A__    ) == 0







					def     UpperCamelCase			( self    ,   A__    ) ->	Union[str, Any]:
						snake_case				=			list(root.values()    )
						if len(A__    ) == 0:
							return 1
						else:
							return sum([self.count_leaves(A__    ) for nn in next_nodes]    )







					def     UpperCamelCase			( self    ,   A__    ,   A__    ) ->	Optional[int]:
						snake_case				=			self.count_leaves(A__    )
						return len(A__    ) != leaf_count




class 			_lowercase						(       __a					):







					def __init__( self    ,   A__    ) ->	str:
						super(A__    ,   self    ).__init__()

						if not isinstance(A__    ,   A__    ) or len(A__    ) == 0:
							raise ValueError(F"""`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}."""    )
						if any(not isinstance(A__    ,   A__    ) 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(A__    ,   A__    ) 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}."""    )

						snake_case				=			DisjunctiveTrie(A__    )
						snake_case				=			nested_token_ids

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







					def     UpperCamelCase			( self    ) ->	List[Any]:
						snake_case				=			self.trie.next_tokens(self.current_seq    )

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







					def     UpperCamelCase			( self    ,   A__    ) ->	List[str]:
						if not isinstance(A__    ,   A__    ):
							raise ValueError(F"""`token_id` is supposed to be type `int`, but is {token_id} of type {type(A__    )}"""    )

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

						return token_id in next_tokens







					def     UpperCamelCase			( self    ,   A__    ) ->	Union[str, Any]:
						if not isinstance(A__    ,   A__    ):
							raise ValueError(F"""`token_id` is supposed to be type `int`, but is {token_id} of type {type(A__    )}"""    )

						snake_case				=			False
						snake_case				=			False
						snake_case				=			False

						if self.does_advance(A__    ):
							self.current_seq.append(A__    )
							snake_case				=			True
						else:
							snake_case				=			True
							self.reset()

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

						return stepped, completed, reset







					def     UpperCamelCase			( self    ) ->	List[Any]:
						snake_case				=			False
						snake_case				=			[]







					def     UpperCamelCase			( self    ) ->	Tuple:
						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    ,   A__=False    ) ->	Optional[int]:
						snake_case				=			DisjunctiveConstraint(self.token_ids    )

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

						return new_constraint




class 			_lowercase						:







					def __init__( self    ,   A__    ) ->	Union[str, Any]:
						snake_case				=			constraints

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

						self.init_state()







					def     UpperCamelCase			( self    ) ->	Optional[Any]:
						snake_case				=			[]
						snake_case				=			None
						snake_case				=			[constraint.copy(stateful=A__    ) for constraint in self.constraints]







					def     UpperCamelCase			( self    ) ->	Any:
						snake_case				=			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    ) ->	Optional[int]:
						snake_case				=			[]
						if self.inprogress_constraint is None:
							for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
								snake_case				=			constraint.advance()
								if isinstance(A__    ,   A__    ):
									token_list.append(A__    )
								elif isinstance(A__    ,   A__    ):
									token_list.extend(A__    )
						else:
							snake_case				=			self.inprogress_constraint.advance()
							if isinstance(A__    ,   A__    ):
								token_list.append(A__    )
							elif isinstance(A__    ,   A__    ):
								token_list.extend(A__    )

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







					def     UpperCamelCase			( self    ,   A__    ) ->	Dict:
						self.init_state()

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

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







					def     UpperCamelCase			( self    ,   A__    ) ->	Tuple:
						if not isinstance(A__    ,   A__    ):
							raise ValueError(F"""`token_id` should be an `int`, but is `{token_id}`."""    )

						snake_case				,		snake_case				=			False, False

						if self.completed:
							snake_case				=			True
							snake_case				=			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

							snake_case				,		snake_case				,		snake_case				=			self.inprogress_constraint.update(A__    )
							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=A__    )    )
								snake_case				=			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    )
								snake_case				=			None

								if len(self.pending_constraints    ) == 0:
									# we're done!
									snake_case				=			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(A__    ):
									snake_case				,		snake_case				,		snake_case				=			pending_constraint.update(A__    )

									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(A__    )
										snake_case				=			None

									if not complete and stepped:
										snake_case				=			pending_constraint

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

										snake_case				=			(
										    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.

											snake_case				=			True

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

						return complete, stepped







					def     UpperCamelCase			( self    ,   A__=True    ) ->	Union[str, Any]:
						snake_case				=			ConstraintListState(self.constraints    )  # we actually never though self.constraints objects
						# throughout this process. So it's at initialization state.

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

						return new_state








'''simple docstring'''







from typing import Dict, List, Optional, Union

import numpy as np

from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy


_lowercase       						=			logging.get_logger(__name__)
class 			_lowercase						(       __a					):







     def __init__( self    ,   A__    ,   A__    ,   A__    ,   **A__    ) ->	Union[str, Any]:
      snake_case				=			feature_size
      snake_case				=			sampling_rate
      snake_case				=			padding_value

      snake_case				=			kwargs.pop('''padding_side'''    ,   '''right'''    )
      snake_case				=			kwargs.pop('''return_attention_mask'''    ,   A__    )

      super().__init__(**A__    )







     def     UpperCamelCase			( self    ,   A__    ,   A__ = True    ,   A__ = None    ,   A__ = False    ,   A__ = None    ,   A__ = None    ,   A__ = None    ,   ) ->	BatchFeature:
      # If we have a list of dicts, let's convert it in a dict of lists
      # We do this to allow using this method as a collate_fn function in PyTorch Dataloader
      if isinstance(A__    ,   (list, tuple)    ) and isinstance(processed_features[0]    ,   (dict, BatchFeature)    ):
       snake_case				=			{
           key: [example[key] for example in processed_features] for key in processed_features[0].keys()
       }

      # The model's main input name, usually `input_values`, has be passed for padding
      if self.model_input_names[0] not in processed_features:
       raise ValueError(
           '''You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`'''
           F""" to this method that includes {self.model_input_names[0]}, but you provided"""
           F""" {list(processed_features.keys()    )}"""    )

      snake_case				=			processed_features[self.model_input_names[0]]
      snake_case				=			(
          return_attention_mask if return_attention_mask is not None else self.return_attention_mask
      )

      if len(A__    ) == 0:
       if return_attention_mask:
        snake_case				=			[]
       return processed_features

      # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays
      # and rebuild them afterwards if no return_tensors is specified
      # Note that we lose the specific device the tensor may be on for PyTorch

      snake_case				=			required_input[0]
      if isinstance(A__    ,   (list, tuple)    ):
       # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element.
       snake_case				=			0
       while len(required_input[index]    ) == 0:
        index += 1
       if index < len(A__    ):
        snake_case				=			required_input[index][0]

      if return_tensors is None:
       if is_tf_tensor(A__    ):
        snake_case				=			'''tf'''
       elif is_torch_tensor(A__    ):
        snake_case				=			'''pt'''
       elif isinstance(A__    ,   (int, float, list, tuple, np.ndarray)    ):
        snake_case				=			'''np'''
       else:
        raise ValueError(
            F"""type of {first_element} unknown: {type(A__    )}. """
            '''Should be one of a python, numpy, pytorch or tensorflow object.'''    )

      for key, value in processed_features.items():
       if isinstance(value[0]    ,   (int, float)    ):
        snake_case				=			to_numpy(A__    )
       else:
        snake_case				=			[to_numpy(A__    ) for v in value]

        # Convert padding_strategy in PaddingStrategy
      snake_case				=			self._get_padding_strategies(padding=A__    ,   max_length=A__    )

      snake_case				=			processed_features[self.model_input_names[0]]

      snake_case				=			len(A__    )
      if not all(len(A__    ) == batch_size for v in processed_features.values()    ):
       raise ValueError('''Some items in the output dictionary have a different batch size than others.'''    )

      snake_case				=			[]
      for i in range(A__    ):
       snake_case				=			{k: v[i] for k, v in processed_features.items()}
       # truncation
       snake_case				=			self._truncate(
           A__    ,   max_length=A__    ,   pad_to_multiple_of=A__    ,   truncation=A__    ,   )
       truncated_inputs.append(A__    )

      if padding_strategy == PaddingStrategy.LONGEST:
       # make sure that `max_length` cannot be longer than the longest truncated length
       snake_case				=			max(len(input_slice[self.model_input_names[0]]    ) for input_slice in truncated_inputs    )
       snake_case				=			PaddingStrategy.MAX_LENGTH

      snake_case				=			{}
      for i in range(A__    ):
       # padding
       snake_case				=			self._pad(
           truncated_inputs[i]    ,   max_length=A__    ,   padding_strategy=A__    ,   pad_to_multiple_of=A__    ,   return_attention_mask=A__    ,   )

       for key, value in outputs.items():
        if key not in batch_outputs:
         snake_case				=			[]
        if value.dtype is np.dtype(np.floataa    ):
         snake_case				=			value.astype(np.floataa    )
        batch_outputs[key].append(A__    )

      return BatchFeature(A__    ,   tensor_type=A__    )







     def     UpperCamelCase			( self    ,   A__    ,   A__ = None    ,   A__ = PaddingStrategy.DO_NOT_PAD    ,   A__ = None    ,   A__ = None    ,   ) ->	dict:
      snake_case				=			processed_features[self.model_input_names[0]]

      if padding_strategy == PaddingStrategy.LONGEST:
       snake_case				=			len(A__    )

      if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
       snake_case				=			((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

      snake_case				=			padding_strategy != PaddingStrategy.DO_NOT_PAD and len(A__    ) < max_length

      if return_attention_mask and "attention_mask" not in processed_features:
       snake_case				=			np.ones(len(A__    )    ,   dtype=np.intaa    )

      if needs_to_be_padded:
       snake_case				=			max_length - len(A__    )
       if self.padding_side == "right":
        if return_attention_mask:
         snake_case				=			np.pad(
             processed_features['''attention_mask''']    ,   (0, difference)    )
        snake_case				=			((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference)
        snake_case				=			np.pad(
            A__    ,   A__    ,   '''constant'''    ,   constant_values=self.padding_value    )
       elif self.padding_side == "left":
        if return_attention_mask:
         snake_case				=			np.pad(
             processed_features['''attention_mask''']    ,   (difference, 0)    )
        snake_case				=			((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0)
        snake_case				=			np.pad(
            A__    ,   A__    ,   '''constant'''    ,   constant_values=self.padding_value    )
       else:
        raise ValueError('''Invalid padding strategy:''' + str(self.padding_side    )    )

      return processed_features







     def     UpperCamelCase			( self    ,   A__    ,   A__ = None    ,   A__ = None    ,   A__ = None    ,   ) ->	Union[str, Any]:
      if not truncation:
       return processed_features
      elif truncation and max_length is None:
       raise ValueError('''When setting ``truncation=True``, make sure that ``max_length`` is defined.'''    )

      snake_case				=			processed_features[self.model_input_names[0]]

      # find `max_length` that fits `pad_to_multiple_of`
      if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
       snake_case				=			((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

      snake_case				=			len(A__    ) > max_length

      if needs_to_be_truncated:
       snake_case				=			processed_features[self.model_input_names[0]][:max_length]
       if "attention_mask" in processed_features:
        snake_case				=			processed_features['''attention_mask'''][:max_length]

      return processed_features







     def     UpperCamelCase			( self    ,   A__=False    ,   A__=None    ) ->	Union[str, Any]:

      # Get padding strategy
      if padding is not False:
       if padding is True:
        snake_case				=			PaddingStrategy.LONGEST  # Default to pad to the longest sequence in the batch
       elif not isinstance(A__    ,   A__    ):
        snake_case				=			PaddingStrategy(A__    )
       elif isinstance(A__    ,   A__    ):
        snake_case				=			padding
      else:
       snake_case				=			PaddingStrategy.DO_NOT_PAD

      # Set max length if needed
      if max_length is None:
       if padding_strategy == PaddingStrategy.MAX_LENGTH:
        raise ValueError(
            F"""When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined"""    )

        # Test if we have a padding value
      if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None):
       raise ValueError(
           '''Asking to pad but the feature_extractor does not have a padding value. Please select a value to use'''
           ''' as `padding_value`. For example: `feature_extractor.padding_value = 0.0`.'''    )

      return padding_strategy

