




'''simple docstring'''







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







class 					_a (a__	):





     '''simple docstring'''



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







     def 			snake_case_      (				self							)      ->				Tuple:
          snake_case			:      Dict        =			0
          snake_case			:      Any        =			False
          while not completed:
               if counter == 1:
                    self.reset()
               snake_case			:      Union[str, Any]        =			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			:      int        =			self.update(__a							)
               counter += 1

               if counter > 10_000:
                    raise Exception("""update() does not fulfill the constraint."""							)

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







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







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







     @abstractmethod
     def 			snake_case_      (				self							,__a							)      ->				List[str]:
          raise NotImplementedError(
              F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''							)







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







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







     @abstractmethod
     def 			snake_case_      (				self							,__a=False							)      ->				Tuple:
          raise NotImplementedError(
              F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''							)







class 					_a (a__	):





     '''simple docstring'''



     def __init__(				self							,__a							)      ->				Optional[Any]:
          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			:      List[Any]        =			token_ids

          snake_case			:      Union[str, Any]        =			len(self.token_ids							)
          snake_case			:      Optional[Any]        =			-1  # the index of the currently fulfilled step
          snake_case			:      Any        =			False







     def 			snake_case_      (				self							)      ->				Optional[int]:
          if self.completed:
               return None
          return self.token_ids[self.fulfilled_idx + 1]







     def 			snake_case_      (				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							)}'''							)

          if self.completed:
               return False

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







     def 			snake_case_      (				self							,__a							)      ->				Optional[Any]:
          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			:      str        =			False
          snake_case			:      Tuple        =			False
          snake_case			:      str        =			False

          if self.does_advance(__a							):
               self.fulfilled_idx += 1
               snake_case			:      Union[str, Any]        =			True
               if self.fulfilled_idx == (self.seqlen - 1):
                    snake_case			:      Optional[Any]        =			True
               snake_case			:      Any        =			completed
          else:
               # failed to make progress.
               snake_case			:      Any        =			True
               self.reset()
          return stepped, completed, reset







     def 			snake_case_      (				self							)      ->				Any:
          snake_case			:      Optional[int]        =			False
          snake_case			:      Union[str, Any]        =			0







     def 			snake_case_      (				self							)      ->				Dict:
          return self.seqlen - (self.fulfilled_idx + 1)







     def 			snake_case_      (				self							,__a=False							)      ->				Tuple:
          snake_case			:      List[Any]        =			PhrasalConstraint(self.token_ids							)

          if stateful:
               snake_case			:      str        =			self.seqlen
               snake_case			:      Dict        =			self.fulfilled_idx
               snake_case			:      Any        =			self.completed

          return new_constraint







class 					_a :





     '''simple docstring'''



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

          snake_case			:      Optional[Any]        =			{}
          for token_ids in nested_token_ids:
               snake_case			:      Optional[int]        =			root
               for tidx, token_id in enumerate(__a							):
                    if token_id not in level:
                         snake_case			:      Tuple        =			{}

                    snake_case			:      Optional[int]        =			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			:      Optional[Any]        =			root







     def 			snake_case_      (				self							,__a							)      ->				int:
          snake_case			:      Any        =			self.trie

          for current_token in current_seq:
               snake_case			:      Tuple        =			start[current_token]

          snake_case			:      int        =			list(start.keys()							)

          return next_tokens







     def 			snake_case_      (				self							,__a							)      ->				List[Any]:
          snake_case			:      Tuple        =			self.next_tokens(__a							)

          return len(__a							) == 0







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







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







class 					_a (a__	):





     '''simple docstring'''



     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			:      Optional[int]        =			DisjunctiveTrie(__a							)
          snake_case			:      Optional[Any]        =			nested_token_ids

          snake_case			:      Any        =			self.trie.max_height
          snake_case			:      Optional[int]        =			[]
          snake_case			:      Optional[Any]        =			False







     def 			snake_case_      (				self							)      ->				Optional[int]:
          snake_case			:      Any        =			self.trie.next_tokens(self.current_seq							)

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







     def 			snake_case_      (				self							,__a							)      ->				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			:      int        =			self.trie.next_tokens(self.current_seq							)

          return token_id in next_tokens







     def 			snake_case_      (				self							,__a							)      ->				Tuple:
          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			:      Any        =			False
          snake_case			:      Dict        =			False
          snake_case			:      Optional[int]        =			False

          if self.does_advance(__a							):
               self.current_seq.append(__a							)
               snake_case			:      Optional[int]        =			True
          else:
               snake_case			:      Tuple        =			True
               self.reset()

          snake_case			:      List[str]        =			self.trie.reached_leaf(self.current_seq							)
          snake_case			:      Tuple        =			completed

          return stepped, completed, reset







     def 			snake_case_      (				self							)      ->				Dict:
          snake_case			:      Dict        =			False
          snake_case			:      str        =			[]







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







     def 			snake_case_      (				self							,__a=False							)      ->				Optional[Any]:
          snake_case			:      List[Any]        =			DisjunctiveConstraint(self.token_ids							)

          if stateful:
               snake_case			:      Tuple        =			self.seqlen
               snake_case			:      List[Any]        =			self.current_seq
               snake_case			:      str        =			self.completed

          return new_constraint







class 					_a :





     '''simple docstring'''



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

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

          self.init_state()







     def 			snake_case_      (				self							)      ->				Union[str, Any]:
          snake_case			:      Union[str, Any]        =			[]
          snake_case			:      List[str]        =			None
          snake_case			:      List[str]        =			[constraint.copy(stateful=__a							) for constraint in self.constraints]







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

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







     def 			snake_case_      (				self							,__a							)      ->				Dict:
          if not isinstance(__a							,__a							):
               raise ValueError(F'''`token_id` should be an `int`, but is `{token_id}`.'''							)

          snake_case ,		snake_case			:      List[Any]        =			False, False

          if self.completed:
               snake_case			:      Optional[int]        =			True
               snake_case			:      Any        =			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			:      Dict        =			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			:      Optional[Any]        =			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			:      Union[str, Any]        =			None

                    if len(self.pending_constraints							) == 0:
                         # we're done!
                         snake_case			:      Dict        =			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			:      Any        =			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			:      Dict        =			None

                         if not complete and stepped:
                              snake_case			:      int        =			pending_constraint

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

                              snake_case			:      Union[str, Any]        =			(
                                  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			:      str        =			True

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

          return complete, stepped







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

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

          return new_state









'''simple docstring'''







from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase      :   Optional[int]      =       logging.get_logger(__name__)

lowercase      :   Optional[Any]      =       {
    """google/pegasus-large""": """https://huggingface.co/google/pegasus-large/resolve/main/config.json""",
    # See all PEGASUS models at https://huggingface.co/models?filter=pegasus
}







class 					_a (a__	):





     '''simple docstring'''



     lowerCAmelCase_  :     Any          =					"""pegasus"""
     lowerCAmelCase_  :     Dict          =					["""past_key_values"""]
     lowerCAmelCase_  :     str          =					{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}







     def __init__(				self							,__a=50_265							,__a=1_024							,__a=12							,__a=4_096							,__a=16							,__a=12							,__a=4_096							,__a=16							,__a=0.0							,__a=0.0							,__a=True							,__a=True							,__a="gelu"							,__a=1_024							,__a=0.1							,__a=0.0							,__a=0.0							,__a=0.02							,__a=0							,__a=False							,__a=0							,__a=1							,__a=1							,**__a							,)      ->				int:
          snake_case			:      List[Any]        =			vocab_size
          snake_case			:      Optional[Any]        =			max_position_embeddings
          snake_case			:      List[Any]        =			d_model
          snake_case			:      str        =			encoder_ffn_dim
          snake_case			:      List[Any]        =			encoder_layers
          snake_case			:      Optional[Any]        =			encoder_attention_heads
          snake_case			:      Union[str, Any]        =			decoder_ffn_dim
          snake_case			:      Tuple        =			decoder_layers
          snake_case			:      Union[str, Any]        =			decoder_attention_heads
          snake_case			:      Union[str, Any]        =			dropout
          snake_case			:      int        =			attention_dropout
          snake_case			:      int        =			activation_dropout
          snake_case			:      Optional[Any]        =			activation_function
          snake_case			:      Tuple        =			init_std
          snake_case			:      Union[str, Any]        =			encoder_layerdrop
          snake_case			:      int        =			decoder_layerdrop
          snake_case			:      Dict        =			use_cache
          snake_case			:      Dict        =			encoder_layers
          snake_case			:      List[Any]        =			scale_embedding  # scale factor will be sqrt(d_model) if True
          super().__init__(
              pad_token_id=__a							,eos_token_id=__a							,is_encoder_decoder=__a							,decoder_start_token_id=__a							,forced_eos_token_id=__a							,**__a							,)







     @property
     def 			snake_case_      (				self							)      ->				int:
          return self.encoder_attention_heads







     @property
     def 			snake_case_      (				self							)      ->				int:
          return self.d_model



