






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




class 							_lowerCAmelCase(			UpperCAmelCase_   ):





       """simple docstring"""







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





       def      _a       (   self				):
         UpperCamelCase_:   List[str]        =							0
         UpperCamelCase_:   List[Any]        =							False
         while not completed:
           if counter == 1:
             self.reset()
           UpperCamelCase_:   Union[str, Any]        =							self.advance()
           if not self.does_advance(_lowerCamelCase				):
             raise Exception(
                 'Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.'				)

           UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_:   int        =							self.update(_lowerCamelCase				)
           counter += 1

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

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





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





       @abstractmethod
       def      _a       (   self    ,     _lowerCamelCase				):
         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''				)





       @abstractmethod
       def      _a       (   self    ,     _lowerCamelCase				):
         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''				)





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





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





       @abstractmethod
       def      _a       (   self    ,     _lowerCamelCase=False				):
         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''				)







class 							_lowerCAmelCase(			UpperCAmelCase_   ):





       """simple docstring"""







       def __init__(   self    ,     _lowerCamelCase				):
         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}.'''				)

         UpperCamelCase_:   Optional[Any]        =							token_ids

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





       def      _a       (   self				):
         if self.completed:
           return None
         return self.token_ids[self.fulfilled_idx + 1]





       def      _a       (   self    ,     _lowerCamelCase				):
         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      _a       (   self    ,     _lowerCamelCase				):
         if not isinstance(_lowerCamelCase    ,     _lowerCamelCase				):
           raise ValueError(f'''`token_id` has to be an `int`, but is {token_id} of type {type(_lowerCamelCase				)}'''				)

         UpperCamelCase_:   Dict        =							False
         UpperCamelCase_:   Optional[Any]        =							False
         UpperCamelCase_:   int        =							False

         if self.does_advance(_lowerCamelCase				):
           self.fulfilled_idx += 1
           UpperCamelCase_:   Tuple        =							True
           if self.fulfilled_idx == (self.seqlen - 1):
             UpperCamelCase_:   Tuple        =							True
           UpperCamelCase_:   List[Any]        =							completed
         else:
           # failed to make progress.
           UpperCamelCase_:   Tuple        =							True
           self.reset()
         return stepped, completed, reset





       def      _a       (   self				):
         UpperCamelCase_:   str        =							False
         UpperCamelCase_:   List[Any]        =							0





       def      _a       (   self				):
         return self.seqlen - (self.fulfilled_idx + 1)





       def      _a       (   self    ,     _lowerCamelCase=False				):
         UpperCamelCase_:   Union[str, Any]        =							PhrasalConstraint(self.token_ids				)

         if stateful:
           UpperCamelCase_:   Any        =							self.seqlen
           UpperCamelCase_:   Any        =							self.fulfilled_idx
           UpperCamelCase_:   List[Any]        =							self.completed

         return new_constraint







class 							_lowerCAmelCase:





       """simple docstring"""







       def __init__(   self    ,     _lowerCamelCase    ,     _lowerCamelCase=True				):
         UpperCamelCase_:   Dict        =							max([len(_lowerCamelCase				) for one in nested_token_ids]				)

         UpperCamelCase_:   Dict        =							{}
         for token_ids in nested_token_ids:
           UpperCamelCase_:   str        =							root
           for tidx, token_id in enumerate(_lowerCamelCase				):
             if token_id not in level:
               UpperCamelCase_:   Optional[Any]        =							{}

             UpperCamelCase_:   Dict        =							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}.'''				)

         UpperCamelCase_:   int        =							root





       def      _a       (   self    ,     _lowerCamelCase				):
         UpperCamelCase_:   Union[str, Any]        =							self.trie

         for current_token in current_seq:
           UpperCamelCase_:   Optional[Any]        =							start[current_token]

         UpperCamelCase_:   Dict        =							list(start.keys()				)

         return next_tokens





       def      _a       (   self    ,     _lowerCamelCase				):
         UpperCamelCase_:   Tuple        =							self.next_tokens(_lowerCamelCase				)

         return len(_lowerCamelCase				) == 0





       def      _a       (   self    ,     _lowerCamelCase				):
         UpperCamelCase_:   Union[str, Any]        =							list(root.values()				)
         if len(_lowerCamelCase				) == 0:
           return 1
         else:
           return sum([self.count_leaves(_lowerCamelCase				) for nn in next_nodes]				)





       def      _a       (   self    ,     _lowerCamelCase    ,     _lowerCamelCase				):
         UpperCamelCase_:   str        =							self.count_leaves(_lowerCamelCase				)
         return len(_lowerCamelCase				) != leaf_count







class 							_lowerCAmelCase(			UpperCAmelCase_   ):





       """simple docstring"""







       def __init__(   self    ,     _lowerCamelCase				):
         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}.'''				)

         UpperCamelCase_:   Any        =							DisjunctiveTrie(_lowerCamelCase				)
         UpperCamelCase_:   Dict        =							nested_token_ids

         UpperCamelCase_:   Union[str, Any]        =							self.trie.max_height
         UpperCamelCase_:   Any        =							[]
         UpperCamelCase_:   Optional[Any]        =							False





       def      _a       (   self				):
         UpperCamelCase_:   Optional[int]        =							self.trie.next_tokens(self.current_seq				)

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





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

         UpperCamelCase_:   Union[str, Any]        =							self.trie.next_tokens(self.current_seq				)

         return token_id in next_tokens





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

         UpperCamelCase_:   int        =							False
         UpperCamelCase_:   Dict        =							False
         UpperCamelCase_:   int        =							False

         if self.does_advance(_lowerCamelCase				):
           self.current_seq.append(_lowerCamelCase				)
           UpperCamelCase_:   List[str]        =							True
         else:
           UpperCamelCase_:   Tuple        =							True
           self.reset()

         UpperCamelCase_:   Any        =							self.trie.reached_leaf(self.current_seq				)
         UpperCamelCase_:   Union[str, Any]        =							completed

         return stepped, completed, reset





       def      _a       (   self				):
         UpperCamelCase_:   Dict        =							False
         UpperCamelCase_:   List[str]        =							[]





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





       def      _a       (   self    ,     _lowerCamelCase=False				):
         UpperCamelCase_:   Optional[int]        =							DisjunctiveConstraint(self.token_ids				)

         if stateful:
           UpperCamelCase_:   str        =							self.seqlen
           UpperCamelCase_:   Any        =							self.current_seq
           UpperCamelCase_:   Any        =							self.completed

         return new_constraint







class 							_lowerCAmelCase:





       """simple docstring"""







       def __init__(   self    ,     _lowerCamelCase				):
         UpperCamelCase_:   Tuple        =							constraints

         # max # of steps required to fulfill a given constraint
         UpperCamelCase_:   List[Any]        =							max([c.seqlen for c in constraints]				)
         UpperCamelCase_:   Optional[int]        =							len(_lowerCamelCase				)
         UpperCamelCase_:   Optional[Any]        =							False

         self.init_state()





       def      _a       (   self				):
         UpperCamelCase_:   str        =							[]
         UpperCamelCase_:   Union[str, Any]        =							None
         UpperCamelCase_:   Tuple        =							[constraint.copy(stateful=_lowerCamelCase				) for constraint in self.constraints]





       def      _a       (   self				):
         UpperCamelCase_:   Tuple        =							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      _a       (   self				):
         UpperCamelCase_:   List[str]        =							[]
         if self.inprogress_constraint is None:
           for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
             UpperCamelCase_:   Tuple        =							constraint.advance()
             if isinstance(_lowerCamelCase    ,     _lowerCamelCase				):
               token_list.append(_lowerCamelCase				)
             elif isinstance(_lowerCamelCase    ,     _lowerCamelCase				):
               token_list.extend(_lowerCamelCase				)
         else:
           UpperCamelCase_:   Dict        =							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      _a       (   self    ,     _lowerCamelCase				):
         self.init_state()

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

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





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

         UpperCamelCase_ ,UpperCamelCase_:   int        =							False, False

         if self.completed:
           UpperCamelCase_:   List[Any]        =							True
           UpperCamelCase_:   List[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

           UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_:   Optional[Any]        =							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				)				)
             UpperCamelCase_:   str        =							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				)
             UpperCamelCase_:   List[Any]        =							None

             if len(self.pending_constraints				) == 0:
               # we're done!
               UpperCamelCase_:   Any        =							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				):
               UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_:   int        =							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				)
                 UpperCamelCase_:   Union[str, Any]        =							None

               if not complete and stepped:
                 UpperCamelCase_:   List[str]        =							pending_constraint

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

                 UpperCamelCase_:   List[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.

                   UpperCamelCase_:   str        =							True

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

         return complete, stepped





       def      _a       (   self    ,     _lowerCamelCase=True				):
         UpperCamelCase_:   List[Any]        =							ConstraintListState(self.constraints				)  # we actually never though self.constraints objects
         # throughout this process. So it's at initialization state.

         if stateful:
           UpperCamelCase_:   Optional[int]        =							[
               constraint.copy(stateful=_lowerCamelCase				) for constraint in self.complete_constraints
           ]
           if self.inprogress_constraint is not None:
             UpperCamelCase_:   Tuple        =							self.inprogress_constraint.copy(stateful=_lowerCamelCase				)
           UpperCamelCase_:   Optional[int]        =							[constraint.copy() for constraint in self.pending_constraints]

         return new_state
from ...configuration_utils import PretrainedConfig
from ...utils import logging


a  :      List[Any]            =    logging.get_logger(__name__)

a  :      Any            =    {"""openai-gpt""": """https://huggingface.co/openai-gpt/resolve/main/config.json"""}

class 					_lowercase    (     UpperCAmelCase__			):






    '''simple docstring'''
    SCREAMING_SNAKE_CASE:				int          =    'openai-gpt'
    SCREAMING_SNAKE_CASE:				List[str]          =    {
        'max_position_embeddings': 'n_positions',
        'hidden_size': 'n_embd',
        'num_attention_heads': 'n_head',
        'num_hidden_layers': 'n_layer',
    }




    def __init__(    self       ,						lowerCamelCase__=40_478       ,						lowerCamelCase__=512       ,						lowerCamelCase__=768       ,						lowerCamelCase__=12       ,						lowerCamelCase__=12       ,						lowerCamelCase__="gelu"       ,						lowerCamelCase__=0.1       ,						lowerCamelCase__=0.1       ,						lowerCamelCase__=0.1       ,						lowerCamelCase__=1E-5       ,						lowerCamelCase__=0.0_2       ,						lowerCamelCase__="cls_index"       ,						lowerCamelCase__=True       ,						lowerCamelCase__=None       ,						lowerCamelCase__=True       ,						lowerCamelCase__=0.1       ,						**lowerCamelCase__       ,						):
       lowerCAmelCase_:							Union[str, Any]       =	vocab_size
       lowerCAmelCase_:							List[Any]       =	n_positions
       lowerCAmelCase_:							Tuple       =	n_embd
       lowerCAmelCase_:							Optional[int]       =	n_layer
       lowerCAmelCase_:							Optional[int]       =	n_head
       lowerCAmelCase_:							int       =	afn
       lowerCAmelCase_:							str       =	resid_pdrop
       lowerCAmelCase_:							Optional[int]       =	embd_pdrop
       lowerCAmelCase_:							Optional[int]       =	attn_pdrop
       lowerCAmelCase_:							Dict       =	layer_norm_epsilon
       lowerCAmelCase_:							List[Any]       =	initializer_range
       lowerCAmelCase_:							Union[str, Any]       =	summary_type
       lowerCAmelCase_:							Any       =	summary_use_proj
       lowerCAmelCase_:							Dict       =	summary_activation
       lowerCAmelCase_:							Dict       =	summary_first_dropout
       lowerCAmelCase_:							List[Any]       =	summary_proj_to_labels
       super().__init__(**lowerCamelCase__ )