





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







_lowercase       						=			{
    'Pillow': 'Pillow',
    'accelerate': 'accelerate>=0.11.0',
    'compel': 'compel==0.1.8',
    'black': 'black~=23.1',
    'datasets': 'datasets',
    'filelock': 'filelock',
    'flax': 'flax>=0.4.1',
    'hf-doc-builder': 'hf-doc-builder>=0.3.0',
    'huggingface-hub': 'huggingface-hub>=0.13.2',
    'requests-mock': 'requests-mock==1.10.0',
    'importlib_metadata': 'importlib_metadata',
    'invisible-watermark': 'invisible-watermark',
    'isort': 'isort>=5.5.4',
    'jax': 'jax>=0.2.8,!=0.3.2',
    'jaxlib': 'jaxlib>=0.1.65',
    'Jinja2': 'Jinja2',
    'k-diffusion': 'k-diffusion>=0.0.12',
    'torchsde': 'torchsde',
    'note_seq': 'note_seq',
    'librosa': 'librosa',
    'numpy': 'numpy',
    'omegaconf': 'omegaconf',
    'parameterized': 'parameterized',
    'protobuf': 'protobuf>=3.20.3,<4',
    'pytest': 'pytest',
    'pytest-timeout': 'pytest-timeout',
    'pytest-xdist': 'pytest-xdist',
    'ruff': 'ruff>=0.0.241',
    'safetensors': 'safetensors',
    'sentencepiece': 'sentencepiece>=0.1.91,!=0.1.92',
    'scipy': 'scipy',
    'onnx': 'onnx',
    'regex': 'regex!=2019.12.17',
    'requests': 'requests',
    'tensorboard': 'tensorboard',
    'torch': 'torch>=1.4',
    'torchvision': 'torchvision',
    'transformers': 'transformers>=4.25.1',
    'urllib3': 'urllib3<=2.0.0',
}

