'''simple docstring'''







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




class _SCREAMING_SNAKE_CASE(							_SCREAMING_SNAKE_CASE					):




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




						def      __lowerCamelCase       (    self :       int      )     ->				int:
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = 0
													SCREAMING_SNAKE_CASE__     :Tuple						    = False
													while not completed:
																				if counter == 1:
																											self.reset()
																				SCREAMING_SNAKE_CASE__     :Optional[int]						    = self.advance()
																				if not self.does_advance(UpperCamelCase_      ):
																											raise Exception(
																											    'Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.'      )

																				SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__     :List[str]						    = self.update(UpperCamelCase_      )
																				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      __lowerCamelCase       (    self :       Any      )     ->				Union[str, Any]:
													raise NotImplementedError(
													    f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''      )




						@abstractmethod
						def      __lowerCamelCase       (    self :       Dict							,							UpperCamelCase_ :       int      )     ->				Union[str, Any]:
													raise NotImplementedError(
													    f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''      )




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




						@abstractmethod
						def      __lowerCamelCase       (    self :       Union[str, Any]      )     ->				Union[str, Any]:
													raise NotImplementedError(
													    f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''      )




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




						@abstractmethod
						def      __lowerCamelCase       (    self :       Tuple							,							UpperCamelCase_ :       Union[str, Any]=False      )     ->				Optional[Any]:
													raise NotImplementedError(
													    f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''      )




class _SCREAMING_SNAKE_CASE(							_SCREAMING_SNAKE_CASE					):




						def __init__(    self :       Optional[Any]							,							UpperCamelCase_ :       List[int]      )     ->				List[str]:
													super(UpperCamelCase_							,							self      ).__init__()

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

													SCREAMING_SNAKE_CASE__     :Tuple						    = token_ids

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




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




						def      __lowerCamelCase       (    self :       Dict							,							UpperCamelCase_ :       int      )     ->				str:
													if not isinstance(UpperCamelCase_							,							UpperCamelCase_      ):
																				raise ValueError(f'''`token_id` has to be an `int`, but is {token_id} of type {type(UpperCamelCase_      )}'''      )

													if self.completed:
																				return False

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




						def      __lowerCamelCase       (    self :       List[Any]							,							UpperCamelCase_ :       int      )     ->				Tuple:
													if not isinstance(UpperCamelCase_							,							UpperCamelCase_      ):
																				raise ValueError(f'''`token_id` has to be an `int`, but is {token_id} of type {type(UpperCamelCase_      )}'''      )

													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = False
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = False
													SCREAMING_SNAKE_CASE__     :List[str]						    = False

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




						def      __lowerCamelCase       (    self :       int      )     ->				Union[str, Any]:
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = False
													SCREAMING_SNAKE_CASE__     :Any						    = 0




						def      __lowerCamelCase       (    self :       str      )     ->				int:
													return self.seqlen - (self.fulfilled_idx + 1)




						def      __lowerCamelCase       (    self :       Optional[Any]							,							UpperCamelCase_ :       Tuple=False      )     ->				Any:
													SCREAMING_SNAKE_CASE__     :Any						    = PhrasalConstraint(self.token_ids      )

													if stateful:
																				SCREAMING_SNAKE_CASE__     :Tuple						    = self.seqlen
																				SCREAMING_SNAKE_CASE__     :int						    = self.fulfilled_idx
																				SCREAMING_SNAKE_CASE__     :str						    = self.completed

													return new_constraint




class _SCREAMING_SNAKE_CASE:




						def __init__(    self :       Dict							,							UpperCamelCase_ :       List[List[int]]							,							UpperCamelCase_ :       str=True      )     ->				str:
													SCREAMING_SNAKE_CASE__     :str						    = max([len(UpperCamelCase_      ) for one in nested_token_ids]      )

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

																											SCREAMING_SNAKE_CASE__     :str						    = level[token_id]

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

													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = root




						def      __lowerCamelCase       (    self :       int							,							UpperCamelCase_ :       Tuple      )     ->				List[str]:
													SCREAMING_SNAKE_CASE__     :List[str]						    = self.trie

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

													SCREAMING_SNAKE_CASE__     :Tuple						    = list(start.keys()      )

													return next_tokens




						def      __lowerCamelCase       (    self :       Tuple							,							UpperCamelCase_ :       Dict      )     ->				Any:
													SCREAMING_SNAKE_CASE__     :Any						    = self.next_tokens(UpperCamelCase_      )

													return len(UpperCamelCase_      ) == 0




						def      __lowerCamelCase       (    self :       Dict							,							UpperCamelCase_ :       int      )     ->				Optional[int]:
													SCREAMING_SNAKE_CASE__     :List[Any]						    = list(root.values()      )
													if len(UpperCamelCase_      ) == 0:
																				return 1
													else:
																				return sum([self.count_leaves(UpperCamelCase_      ) for nn in next_nodes]      )




						def      __lowerCamelCase       (    self :       List[str]							,							UpperCamelCase_ :       int							,							UpperCamelCase_ :       Optional[Any]      )     ->				Any:
													SCREAMING_SNAKE_CASE__     :List[Any]						    = self.count_leaves(UpperCamelCase_      )
													return len(UpperCamelCase_      ) != leaf_count




class _SCREAMING_SNAKE_CASE(							_SCREAMING_SNAKE_CASE					):




						def __init__(    self :       Optional[int]							,							UpperCamelCase_ :       List[List[int]]      )     ->				List[str]:
													super(UpperCamelCase_							,							self      ).__init__()

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

													SCREAMING_SNAKE_CASE__     :List[Any]						    = DisjunctiveTrie(UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :Dict						    = nested_token_ids

													SCREAMING_SNAKE_CASE__     :str						    = self.trie.max_height
													SCREAMING_SNAKE_CASE__     :Tuple						    = []
													SCREAMING_SNAKE_CASE__     :Tuple						    = False




						def      __lowerCamelCase       (    self :       str      )     ->				Optional[int]:
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = self.trie.next_tokens(self.current_seq      )

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




						def      __lowerCamelCase       (    self :       Optional[Any]							,							UpperCamelCase_ :       int      )     ->				List[Any]:
													if not isinstance(UpperCamelCase_							,							UpperCamelCase_      ):
																				raise ValueError(f'''`token_id` is supposed to be type `int`, but is {token_id} of type {type(UpperCamelCase_      )}'''      )

													SCREAMING_SNAKE_CASE__     :int						    = self.trie.next_tokens(self.current_seq      )

													return token_id in next_tokens




						def      __lowerCamelCase       (    self :       Dict							,							UpperCamelCase_ :       int      )     ->				List[str]:
													if not isinstance(UpperCamelCase_							,							UpperCamelCase_      ):
																				raise ValueError(f'''`token_id` is supposed to be type `int`, but is {token_id} of type {type(UpperCamelCase_      )}'''      )

													SCREAMING_SNAKE_CASE__     :Any						    = False
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = False
													SCREAMING_SNAKE_CASE__     :List[str]						    = False

													if self.does_advance(UpperCamelCase_      ):
																				self.current_seq.append(UpperCamelCase_      )
																				SCREAMING_SNAKE_CASE__     :Dict						    = True
													else:
																				SCREAMING_SNAKE_CASE__     :Any						    = True
																				self.reset()

													SCREAMING_SNAKE_CASE__     :str						    = self.trie.reached_leaf(self.current_seq      )
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = completed

													return stepped, completed, reset




						def      __lowerCamelCase       (    self :       Union[str, Any]      )     ->				List[Any]:
													SCREAMING_SNAKE_CASE__     :Dict						    = False
													SCREAMING_SNAKE_CASE__     :Dict						    = []




						def      __lowerCamelCase       (    self :       str      )     ->				List[Any]:
													if self.completed:
																				# since this can be completed without reaching max height
																				return 0
													else:
																				return self.seqlen - len(self.current_seq      )




						def      __lowerCamelCase       (    self :       Any							,							UpperCamelCase_ :       Tuple=False      )     ->				Dict:
													SCREAMING_SNAKE_CASE__     :Dict						    = DisjunctiveConstraint(self.token_ids      )

													if stateful:
																				SCREAMING_SNAKE_CASE__     :Optional[int]						    = self.seqlen
																				SCREAMING_SNAKE_CASE__     :List[str]						    = self.current_seq
																				SCREAMING_SNAKE_CASE__     :Tuple						    = self.completed

													return new_constraint




class _SCREAMING_SNAKE_CASE:




						def __init__(    self :       int							,							UpperCamelCase_ :       List[Constraint]      )     ->				str:
													SCREAMING_SNAKE_CASE__     :Any						    = constraints

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

													self.init_state()




						def      __lowerCamelCase       (    self :       str      )     ->				str:
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = []
													SCREAMING_SNAKE_CASE__     :Tuple						    = None
													SCREAMING_SNAKE_CASE__     :int						    = [constraint.copy(stateful=UpperCamelCase_      ) for constraint in self.constraints]




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

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




						def      __lowerCamelCase       (    self :       Optional[int]							,							UpperCamelCase_ :       Optional[List[int]]      )     ->				int:
													self.init_state()

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

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




						def      __lowerCamelCase       (    self :       List[Any]							,							UpperCamelCase_ :       int      )     ->				str:
													if not isinstance(UpperCamelCase_							,							UpperCamelCase_      ):
																				raise ValueError(f'''`token_id` should be an `int`, but is `{token_id}`.'''      )

													SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__     :Optional[int]						    = False, False

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

																				SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__     :Dict						    = self.inprogress_constraint.update(UpperCamelCase_      )
																				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=UpperCamelCase_      )      )
																											SCREAMING_SNAKE_CASE__     :Union[str, 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      )
																											SCREAMING_SNAKE_CASE__     :str						    = None

																											if len(self.pending_constraints      ) == 0:
																																		# we're done!
																																		SCREAMING_SNAKE_CASE__     :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(UpperCamelCase_      ):
																																		SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__     :Optional[int]						    = pending_constraint.update(UpperCamelCase_      )

																																		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(UpperCamelCase_      )
																																									SCREAMING_SNAKE_CASE__     :Tuple						    = None

																																		if not complete and stepped:
																																									SCREAMING_SNAKE_CASE__     :str						    = pending_constraint

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

																																									SCREAMING_SNAKE_CASE__     :str						    = (
																																									    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.

																																																SCREAMING_SNAKE_CASE__     :Tuple						    = True

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

													return complete, stepped




						def      __lowerCamelCase       (    self :       int							,							UpperCamelCase_ :       Optional[Any]=True      )     ->				List[str]:
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = ConstraintListState(self.constraints      )  # we actually never though self.constraints objects
													# throughout this process. So it's at initialization state.

													if stateful:
																				SCREAMING_SNAKE_CASE__     :List[Any]						    = [
																				    constraint.copy(stateful=UpperCamelCase_      ) for constraint in self.complete_constraints
																				]
																				if self.inprogress_constraint is not None:
																											SCREAMING_SNAKE_CASE__     :str						    = self.inprogress_constraint.copy(stateful=UpperCamelCase_      )
																				SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = [constraint.copy() for constraint in self.pending_constraints]

													return new_state







'''simple docstring'''







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


UpperCamelCase_      	=	{
    '''configuration_m2m_100''': ['''M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''M2M100Config''', '''M2M100OnnxConfig'''],
    '''tokenization_m2m_100''': ['''M2M100Tokenizer'''],
}


try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		UpperCamelCase_      	=	[
		    '''M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''M2M100ForConditionalGeneration''',
		    '''M2M100Model''',
		    '''M2M100PreTrainedModel''',
		]


if TYPE_CHECKING:
		from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig
		from .tokenization_mam_aaa import MaMaaaTokenizer

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_mam_aaa import (
				    M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST,
				    MaMaaaForConditionalGeneration,
				    MaMaaaModel,
				    MaMaaaPreTrainedModel,
				)


else:
		import sys

		UpperCamelCase_      	=	_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)






