

'''simple docstring'''





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





class A__    ( __SCREAMING_SNAKE_CASE ):



 """simple docstring"""







 def __init__(		self :	Union[str, Any]       ):






      '''simple docstring'''




      self.test()

 def    __magic_name__       (		self :	List[str]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Optional[Any]        =    0
      _lowerCAmelCase   :					int        =    False
      while not completed:
           if counter == 1:
                self.reset()
           _lowerCAmelCase   :					Optional[int]        =    self.advance()
           if not self.does_advance(_a       ):
                raise Exception(
                    "Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true."       )

           _lowerCAmelCase      ,			_lowerCAmelCase      ,			_lowerCAmelCase   :					Union[str, Any]        =    self.update(_a       )
           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    __magic_name__       (		self :	int       ):






      '''simple docstring'''




      raise NotImplementedError(
          F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''       )

 @abstractmethod
 def    __magic_name__       (		self :	Optional[Any]     ,						A_ :	Any       ):






      '''simple docstring'''




      raise NotImplementedError(
          F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''       )

 @abstractmethod
 def    __magic_name__       (		self :	Optional[Any]     ,						A_ :	List[str]       ):






      '''simple docstring'''




      raise NotImplementedError(
          F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''       )

 @abstractmethod
 def    __magic_name__       (		self :	List[Any]       ):






      '''simple docstring'''




      raise NotImplementedError(
          F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''       )

 @abstractmethod
 def    __magic_name__       (		self :	Dict       ):






      '''simple docstring'''




      raise NotImplementedError(
          F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''       )


 @abstractmethod
 def    __magic_name__       (		self :	Union[str, Any]     ,						A_ :	str=False       ):






      '''simple docstring'''




      raise NotImplementedError(
          F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''       )





class A__    ( __SCREAMING_SNAKE_CASE ):



 """simple docstring"""







 def __init__(		self :	Any     ,						A_ :	Optional[int]       ):






      '''simple docstring'''




      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}.'''       )

      _lowerCAmelCase   :					List[str]        =    token_ids

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

 def    __magic_name__       (		self :	int       ):






      '''simple docstring'''




      if self.completed:
           return None
      return self.token_ids[self.fulfilled_idx + 1]

 def    __magic_name__       (		self :	List[str]     ,						A_ :	Any       ):






      '''simple docstring'''




      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    __magic_name__       (		self :	Optional[Any]     ,						A_ :	Tuple       ):






      '''simple docstring'''




      if not isinstance(_a     ,						_a       ):
           raise ValueError(F'''`token_id` has to be an `int`, but is {token_id} of type {type(_a       )}'''       )

      _lowerCAmelCase   :					Optional[int]        =    False
      _lowerCAmelCase   :					int        =    False
      _lowerCAmelCase   :					str        =    False

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

 def    __magic_name__       (		self :	Union[str, Any]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					int        =    False
      _lowerCAmelCase   :					int        =    0

 def    __magic_name__       (		self :	List[Any]       ):






      '''simple docstring'''




      return self.seqlen - (self.fulfilled_idx + 1)


 def    __magic_name__       (		self :	Dict     ,						A_ :	str=False       ):






      '''simple docstring'''




      _lowerCAmelCase   :					List[str]        =    PhrasalConstraint(self.token_ids       )

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

      return new_constraint





class A__    :



 """simple docstring"""







 def __init__(		self :	Dict     ,						A_ :	int     ,						A_ :	Dict=True       ):






      '''simple docstring'''




      _lowerCAmelCase   :					int        =    max([len(_a       ) for one in nested_token_ids]       )

      _lowerCAmelCase   :					List[str]        =    {}
      for token_ids in nested_token_ids:
           _lowerCAmelCase   :					List[str]        =    root
           for tidx, token_id in enumerate(_a       ):
                if token_id not in level:
                     _lowerCAmelCase   :					List[str]        =    {}

                _lowerCAmelCase   :					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}.'''       )

      _lowerCAmelCase   :					List[Any]        =    root

 def    __magic_name__       (		self :	Optional[int]     ,						A_ :	str       ):






      '''simple docstring'''




      _lowerCAmelCase   :					List[Any]        =    self.trie

      for current_token in current_seq:
           _lowerCAmelCase   :					Any        =    start[current_token]

      _lowerCAmelCase   :					Optional[Any]        =    list(start.keys()       )

      return next_tokens

 def    __magic_name__       (		self :	Dict     ,						A_ :	Any       ):






      '''simple docstring'''




      _lowerCAmelCase   :					str        =    self.next_tokens(_a       )

      return len(_a       ) == 0

 def    __magic_name__       (		self :	Optional[Any]     ,						A_ :	str       ):






      '''simple docstring'''




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


 def    __magic_name__       (		self :	Optional[int]     ,						A_ :	Tuple     ,						A_ :	Tuple       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Dict        =    self.count_leaves(_a       )
      return len(_a       ) != leaf_count





class A__    ( __SCREAMING_SNAKE_CASE ):



 """simple docstring"""







 def __init__(		self :	Dict     ,						A_ :	List[str]       ):






      '''simple docstring'''




      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}.'''       )

      _lowerCAmelCase   :					Optional[Any]        =    DisjunctiveTrie(_a       )
      _lowerCAmelCase   :					str        =    nested_token_ids

      _lowerCAmelCase   :					List[str]        =    self.trie.max_height
      _lowerCAmelCase   :					Optional[Any]        =    []
      _lowerCAmelCase   :					int        =    False

 def    __magic_name__       (		self :	Union[str, Any]       ):






      '''simple docstring'''




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

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

 def    __magic_name__       (		self :	Union[str, Any]     ,						A_ :	Union[str, Any]       ):






      '''simple docstring'''




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

      _lowerCAmelCase   :					Any        =    self.trie.next_tokens(self.current_seq       )

      return token_id in next_tokens

 def    __magic_name__       (		self :	Optional[Any]     ,						A_ :	Dict       ):






      '''simple docstring'''




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

      _lowerCAmelCase   :					Union[str, Any]        =    False
      _lowerCAmelCase   :					Tuple        =    False
      _lowerCAmelCase   :					List[Any]        =    False

      if self.does_advance(_a       ):
           self.current_seq.append(_a       )
           _lowerCAmelCase   :					Any        =    True
      else:
           _lowerCAmelCase   :					int        =    True
           self.reset()

      _lowerCAmelCase   :					Tuple        =    self.trie.reached_leaf(self.current_seq       )
      _lowerCAmelCase   :					Tuple        =    completed

      return stepped, completed, reset

 def    __magic_name__       (		self :	Dict       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Optional[Any]        =    False
      _lowerCAmelCase   :					str        =    []

 def    __magic_name__       (		self :	Tuple       ):






      '''simple docstring'''




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


 def    __magic_name__       (		self :	Optional[Any]     ,						A_ :	List[Any]=False       ):






      '''simple docstring'''




      _lowerCAmelCase   :					List[str]        =    DisjunctiveConstraint(self.token_ids       )

      if stateful:
           _lowerCAmelCase   :					int        =    self.seqlen
           _lowerCAmelCase   :					Union[str, Any]        =    self.current_seq
           _lowerCAmelCase   :					Union[str, Any]        =    self.completed

      return new_constraint





class A__    :



 """simple docstring"""







 def __init__(		self :	int     ,						A_ :	Optional[Any]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					List[Any]        =    constraints

      # max # of steps required to fulfill a given constraint
      _lowerCAmelCase   :					List[str]        =    max([c.seqlen for c in constraints]       )
      _lowerCAmelCase   :					int        =    len(_a       )
      _lowerCAmelCase   :					int        =    False

      self.init_state()

 def    __magic_name__       (		self :	Optional[Any]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Dict        =    []
      _lowerCAmelCase   :					Optional[int]        =    None
      _lowerCAmelCase   :					Tuple        =    [constraint.copy(stateful=_a       ) for constraint in self.constraints]

 def    __magic_name__       (		self :	Any       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Union[str, Any]        =    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    __magic_name__       (		self :	List[Any]       ):






      '''simple docstring'''




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






      '''simple docstring'''




      self.init_state()

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

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

 def    __magic_name__       (		self :	Optional[Any]     ,						A_ :	Tuple       ):






      '''simple docstring'''




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

      _lowerCAmelCase      ,			_lowerCAmelCase   :					str        =    False, False

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

           _lowerCAmelCase      ,			_lowerCAmelCase      ,			_lowerCAmelCase   :					Union[str, Any]        =    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       )       )
                _lowerCAmelCase   :					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       )
                _lowerCAmelCase   :					Any        =    None

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

                     if not complete and stepped:
                          _lowerCAmelCase   :					List[Any]        =    pending_constraint

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

                          _lowerCAmelCase   :					Optional[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.

                               _lowerCAmelCase   :					List[Any]        =    True

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

      return complete, stepped


 def    __magic_name__       (		self :	str     ,						A_ :	Dict=True       ):






      '''simple docstring'''




      _lowerCAmelCase   :					int        =    ConstraintListState(self.constraints       )  # we actually never though self.constraints objects
      # throughout this process. So it's at initialization state.

      if stateful:
           _lowerCAmelCase   :					List[Any]        =    [
               constraint.copy(stateful=_a       ) for constraint in self.complete_constraints
           ]
           if self.inprogress_constraint is not None:
                _lowerCAmelCase   :					Optional[Any]        =    self.inprogress_constraint.copy(stateful=_a       )
           _lowerCAmelCase   :					Tuple        =    [constraint.copy() for constraint in self.pending_constraints]

      return new_state









from dataclasses import dataclass, field
from typing import Tuple

from ..utils import cached_property, is_tf_available, logging, requires_backends
from .benchmark_args_utils import BenchmarkArguments


if is_tf_available():
  import tensorflow as tf


__UpperCAmelCase    				=     logging.get_logger(__name__)





@dataclass
class A__    ( A ):



 """simple docstring"""







 _lowercase     :					List[Any]      =						[
     '''no_inference''',
     '''no_cuda''',
     '''no_tpu''',
     '''no_speed''',
     '''no_memory''',
     '''no_env_print''',
     '''no_multi_process''',
 ]

 def __init__(		self :	List[Any]     ,						**A_ :	str       ):






      '''simple docstring'''




      for deprecated_arg in self.deprecated_args:
           if deprecated_arg in kwargs:
                _lowerCAmelCase   :					str        =    deprecated_arg[3:]
                _lowerCAmelCase   :					List[Any]        =    not kwargs.pop(A_       )
                logger.warning(
                    F'''{deprecated_arg} is depreciated. Please use --no-{positive_arg} or'''
                    F''' {positive_arg}={kwargs[positive_arg]}'''       )
      _lowerCAmelCase   :					Any        =    kwargs.pop("tpu_name"     ,						self.tpu_name       )
      _lowerCAmelCase   :					str        =    kwargs.pop("device_idx"     ,						self.device_idx       )
      _lowerCAmelCase   :					str        =    kwargs.pop("eager_mode"     ,						self.eager_mode       )
      _lowerCAmelCase   :					Tuple        =    kwargs.pop("use_xla"     ,						self.use_xla       )
      super().__init__(**A_       )

 _lowercase     :					str      =						field(
     default=A  ,		metadata={'''help''': '''Name of TPU'''}  ,		)
 _lowercase     :					int      =						field(
     default=0  ,		metadata={'''help''': '''CPU / GPU device index. Defaults to 0.'''}  ,		)
 _lowercase     :					bool      =						field(default=A  ,		metadata={'''help''': '''Benchmark models in eager model.'''} )
 _lowercase     :					bool      =						field(
     default=A  ,		metadata={
         '''help''': '''Benchmark models using XLA JIT compilation. Note that `eager_model` has to be set to `False`.'''
     }  ,		)

 @cached_property
 def    __magic_name__       (		self :	Tuple       ):






      '''simple docstring'''




      requires_backends(self     ,						["tf"]       )
      _lowerCAmelCase   :					Any        =    None
      if self.tpu:
           try:
                if self.tpu_name:
                     _lowerCAmelCase   :					Union[str, Any]        =    tf.distribute.cluster_resolver.TPUClusterResolver(self.tpu_name       )
                else:
                     _lowerCAmelCase   :					List[str]        =    tf.distribute.cluster_resolver.TPUClusterResolver()
           except ValueError:
                _lowerCAmelCase   :					Optional[int]        =    None
      return tpu

 @cached_property
 def    __magic_name__       (		self :	Optional[int]       ):






      '''simple docstring'''




      requires_backends(self     ,						["tf"]       )
      if self.is_tpu:
           tf.config.experimental_connect_to_cluster(self._setup_tpu       )
           tf.tpu.experimental.initialize_tpu_system(self._setup_tpu       )

           _lowerCAmelCase   :					List[Any]        =    tf.distribute.TPUStrategy(self._setup_tpu       )
      else:
           # currently no multi gpu is allowed
           if self.is_gpu:
                # TODO: Currently only single GPU is supported
                tf.config.set_visible_devices(self.gpu_list[self.device_idx]     ,						"GPU"       )
                _lowerCAmelCase   :					Dict        =    tf.distribute.OneDeviceStrategy(device=F'''/gpu:{self.device_idx}'''       )
           else:
                tf.config.set_visible_devices([]     ,						"GPU"       )  # disable GPU
                _lowerCAmelCase   :					Optional[Any]        =    tf.distribute.OneDeviceStrategy(device=F'''/cpu:{self.device_idx}'''       )

      return strategy

 @property
 def    __magic_name__       (		self :	int       ):






      '''simple docstring'''




      requires_backends(self     ,						["tf"]       )
      return self._setup_tpu is not None

 @property
 def    __magic_name__       (		self :	List[Any]       ):






      '''simple docstring'''




      requires_backends(self     ,						["tf"]       )
      return self._setup_strategy

 @property
 def    __magic_name__       (		self :	List[str]       ):






      '''simple docstring'''




      requires_backends(self     ,						["tf"]       )
      return tf.config.list_physical_devices("GPU"       )

 @property
 def    __magic_name__       (		self :	Optional[Any]       ):






      '''simple docstring'''




      requires_backends(self     ,						["tf"]       )
      if self.cuda:
           return len(self.gpu_list       )
      return 0


 @property
 def    __magic_name__       (		self :	Optional[Any]       ):






      '''simple docstring'''




      return self.n_gpu > 0






