



"""simple docstring"""







import os
import re
import sys
import traceback
import warnings
from pathlib import Path
from typing import Dict, Optional, Union
from uuid import uuida

from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami
from huggingface_hub.file_download import REGEX_COMMIT_HASH
from huggingface_hub.utils import (
    EntryNotFoundError,
    RepositoryNotFoundError,
    RevisionNotFoundError,
    is_jinja_available,
)
from packaging import version
from requests import HTTPError

from .. import __version__
from .constants import (
    DEPRECATED_REVISION_ARGS,
    DIFFUSERS_CACHE,
    HUGGINGFACE_CO_RESOLVE_ENDPOINT,
    SAFETENSORS_WEIGHTS_NAME,
    WEIGHTS_NAME,
)
from .import_utils import (
    ENV_VARS_TRUE_VALUES,
    _flax_version,
    _jax_version,
    _onnxruntime_version,
    _torch_version,
    is_flax_available,
    is_onnx_available,
    is_torch_available,
)
from .logging import get_logger


lowercase__		:    Any				   =     get_logger(__name__)


lowercase__		:    Optional[Any]				   =     Path(__file__).parent / """model_card_template.md"""
lowercase__		:    List[Any]				   =     uuida().hex
lowercase__		:    Dict				   =     os.getenv("""HF_HUB_OFFLINE""", """""").upper() in ENV_VARS_TRUE_VALUES
lowercase__		:    int				   =     os.getenv("""DISABLE_TELEMETRY""", """""").upper() in ENV_VARS_TRUE_VALUES
lowercase__		:    List[Any]				   =     HUGGINGFACE_CO_RESOLVE_ENDPOINT + """/api/telemetry/"""

def     UpperCamelCase_   (   lowerCAmelCase__				:    Union[Dict, str, None] = None						)						->       str:

  """simple docstring"""




  lowerCAmelCase_       :    str								=			f"diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}"
  if DISABLE_TELEMETRY or HF_HUB_OFFLINE:
    return ua + "; telemetry/off"
  if is_torch_available():
    ua += f"; torch/{_torch_version}"
  if is_flax_available():
    ua += f"; jax/{_jax_version}"
    ua += f"; flax/{_flax_version}"
  if is_onnx_available():
    ua += f"; onnxruntime/{_onnxruntime_version}"
  # CI will set this value to True
  if os.environ.get('DIFFUSERS_IS_CI'   ,       ''						).upper() in ENV_VARS_TRUE_VALUES:
    ua += "; is_ci/true"
  if isinstance(lowerCAmelCase__   ,       lowerCAmelCase__						):
    ua += "; " + "; ".join(f"{k}/{v}" for k, v in user_agent.items()						)
  elif isinstance(lowerCAmelCase__   ,       lowerCAmelCase__						):
    ua += "; " + user_agent
  return ua

def     UpperCamelCase_   (   lowerCAmelCase__				:    str   ,       lowerCAmelCase__				:    Optional[str] = None   ,       lowerCAmelCase__				:    Optional[str] = None						)						->       Any:

  """simple docstring"""




  if token is None:
    lowerCAmelCase_       :    Union[str, Any]								=			HfFolder.get_token()
  if organization is None:
    lowerCAmelCase_       :    List[str]								=			whoami(lowerCAmelCase__						)['name']
    return f"{username}/{model_id}"
  else:
    return f"{organization}/{model_id}"

def     UpperCamelCase_   (   lowerCAmelCase__				:    Optional[Any]   ,       lowerCAmelCase__				:    Optional[Any]						)						->       Optional[Any]:

  """simple docstring"""




  if not is_jinja_available():
    raise ValueError(
        'Modelcard rendering is based on Jinja templates.'
        ' Please make sure to have `jinja` installed before using `create_model_card`.'
        ' To install it, please run `pip install Jinja2`.'						)

  if hasattr(lowerCAmelCase__   ,       'local_rank'						) and args.local_rank not in [-1, 0]:
    return

  lowerCAmelCase_       :    Optional[int]								=			args.hub_token if hasattr(lowerCAmelCase__   ,       'hub_token'						) else None
  lowerCAmelCase_       :    Optional[Any]								=			get_full_repo_name(lowerCAmelCase__   ,       token=lowerCAmelCase__						)

  lowerCAmelCase_       :    Any								=			ModelCard.from_template(
      card_data=ModelCardData(  # Card metadata object that will be converted to YAML block
          language='en'   ,       license='apache-2.0'   ,       library_name='diffusers'   ,       tags=[]   ,       datasets=args.dataset_name   ,       metrics=[]   ,       )   ,       template_path=lowerCAmelCase__   ,       model_name=lowerCAmelCase__   ,       repo_name=lowerCAmelCase__   ,       dataset_name=args.dataset_name if hasattr(lowerCAmelCase__   ,       'dataset_name'						) else None   ,       learning_rate=args.learning_rate   ,       train_batch_size=args.train_batch_size   ,       eval_batch_size=args.eval_batch_size   ,       gradient_accumulation_steps=(
          args.gradient_accumulation_steps if hasattr(lowerCAmelCase__   ,       'gradient_accumulation_steps'						) else None
      )   ,       adam_betaa=args.adam_betaa if hasattr(lowerCAmelCase__   ,       'adam_beta1'						) else None   ,       adam_betaa=args.adam_betaa if hasattr(lowerCAmelCase__   ,       'adam_beta2'						) else None   ,       adam_weight_decay=args.adam_weight_decay if hasattr(lowerCAmelCase__   ,       'adam_weight_decay'						) else None   ,       adam_epsilon=args.adam_epsilon if hasattr(lowerCAmelCase__   ,       'adam_epsilon'						) else None   ,       lr_scheduler=args.lr_scheduler if hasattr(lowerCAmelCase__   ,       'lr_scheduler'						) else None   ,       lr_warmup_steps=args.lr_warmup_steps if hasattr(lowerCAmelCase__   ,       'lr_warmup_steps'						) else None   ,       ema_inv_gamma=args.ema_inv_gamma if hasattr(lowerCAmelCase__   ,       'ema_inv_gamma'						) else None   ,       ema_power=args.ema_power if hasattr(lowerCAmelCase__   ,       'ema_power'						) else None   ,       ema_max_decay=args.ema_max_decay if hasattr(lowerCAmelCase__   ,       'ema_max_decay'						) else None   ,       mixed_precision=args.mixed_precision   ,       )

  lowerCAmelCase_       :    Optional[Any]								=			os.path.join(args.output_dir   ,       'README.md'						)
  model_card.save(lowerCAmelCase__						)

def     UpperCamelCase_   (   lowerCAmelCase__				:    Optional[str]   ,       lowerCAmelCase__				:    Optional[str] = None						)						->       int:

  """simple docstring"""




  if resolved_file is None or commit_hash is not None:
    return commit_hash
  lowerCAmelCase_       :    Any								=			str(Path(lowerCAmelCase__						).as_posix()						)
  lowerCAmelCase_       :    Union[str, Any]								=			re.search(R'snapshots/([^/]+)/'   ,       lowerCAmelCase__						)
  if search is None:
    return None
  lowerCAmelCase_       :    Tuple								=			search.groups()[0]
  return commit_hash if REGEX_COMMIT_HASH.match(lowerCAmelCase__						) else None


# Old default cache path, potentially to be migrated.
# This logic was more or less taken from `transformers`, with the following differences:
# - Diffusers doesn't use custom environment variables to specify the cache path.
# - There is no need to migrate the cache format, just move the files to the new location.
lowercase__		:    Optional[int]				   =     os.path.expanduser(
    os.getenv("""HF_HOME""", os.path.join(os.getenv("""XDG_CACHE_HOME""", """~/.cache"""), """huggingface"""))
)
lowercase__		:    Tuple				   =     os.path.join(hf_cache_home, """diffusers""")

def     UpperCamelCase_   (   lowerCAmelCase__				:    Optional[str] = None   ,       lowerCAmelCase__				:    Optional[str] = None						)						->       None:

  """simple docstring"""




  if new_cache_dir is None:
    lowerCAmelCase_       :    Union[str, Any]								=			DIFFUSERS_CACHE
  if old_cache_dir is None:
    lowerCAmelCase_       :    List[str]								=			old_diffusers_cache

  lowerCAmelCase_       :    List[str]								=			Path(lowerCAmelCase__						).expanduser()
  lowerCAmelCase_       :    Tuple								=			Path(lowerCAmelCase__						).expanduser()
  for old_blob_path in old_cache_dir.glob('**/blobs/*'						):
    if old_blob_path.is_file() and not old_blob_path.is_symlink():
      lowerCAmelCase_       :    Union[str, Any]								=			new_cache_dir / old_blob_path.relative_to(lowerCAmelCase__						)
      new_blob_path.parent.mkdir(parents=lowerCAmelCase__   ,       exist_ok=lowerCAmelCase__						)
      os.replace(lowerCAmelCase__   ,       lowerCAmelCase__						)
      try:
        os.symlink(lowerCAmelCase__   ,       lowerCAmelCase__						)
      except OSError:
        logger.warning(
            'Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.'						)
  # At this point, old_cache_dir contains symlinks to the new cache (it can still be used).


lowercase__		:    Tuple				   =     os.path.join(DIFFUSERS_CACHE, """version_diffusers_cache.txt""")
if not os.path.isfile(cache_version_file):
     lowercase__		:    str				   =     0
else:
     with open(cache_version_file) as f:
          try:
               lowercase__		:    Dict				   =     int(f.read())
          except ValueError:
               lowercase__		:    Optional[Any]				   =     0

if cache_version < 1:
     lowercase__		:    Optional[int]				   =     os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0
     if old_cache_is_not_empty:
          logger.warning(
              """The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your """
              """existing cached models. This is a one-time operation, you can interrupt it or run it """
              """later by calling `diffusers.utils.hub_utils.move_cache()`."""
          )
          try:
               move_cache()
          except Exception as e:
               lowercase__		:    List[Any]				   =     """\n""".join(traceback.format_tb(e.__traceback__))
               logger.error(
                   f'There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease '
                   """file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole """
                   """message and we will do our best to help."""
               )

if cache_version < 1:
     try:
          os.makedirs(DIFFUSERS_CACHE, exist_ok=True)
          with open(cache_version_file, """w""") as f:
               f.write("""1""")
     except Exception:
          logger.warning(
              f'There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure '
              """the directory exists and can be written to."""
          )

def     UpperCamelCase_   (   lowerCAmelCase__				:    str   ,       lowerCAmelCase__				:    Optional[str] = None						)						->       str:

  """simple docstring"""




  if variant is not None:
    lowerCAmelCase_       :    Dict								=			weights_name.split('.'						)
    lowerCAmelCase_       :    Union[str, Any]								=			splits[:-1] + [variant] + splits[-1:]
    lowerCAmelCase_       :    Union[str, Any]								=			'.'.join(lowerCAmelCase__						)

  return weights_name

def     UpperCamelCase_   (   lowerCAmelCase__				:    Union[str, Any]   ,       *,
    lowerCAmelCase__				:    Any   ,       lowerCAmelCase__				:    Any   ,       lowerCAmelCase__				:    Dict   ,       lowerCAmelCase__				:    Union[str, Any]   ,       lowerCAmelCase__				:    List[Any]   ,       lowerCAmelCase__				:    List[Any]   ,       lowerCAmelCase__				:    Optional[Any]   ,       lowerCAmelCase__				:    Dict   ,       lowerCAmelCase__				:    Tuple   ,       lowerCAmelCase__				:    Optional[int]   ,       lowerCAmelCase__				:    Optional[Any]=None   ,       )						->       Optional[Any]:

  """simple docstring"""




  lowerCAmelCase_       :    Any								=			str(lowerCAmelCase__						)
  if os.path.isfile(lowerCAmelCase__						):
    return pretrained_model_name_or_path
  elif os.path.isdir(lowerCAmelCase__						):
    if os.path.isfile(os.path.join(lowerCAmelCase__   ,       lowerCAmelCase__						)						):
      # Load from a PyTorch checkpoint
      lowerCAmelCase_       :    Optional[int]								=			os.path.join(lowerCAmelCase__   ,       lowerCAmelCase__						)
      return model_file
    elif subfolder is not None and os.path.isfile(
        os.path.join(lowerCAmelCase__   ,       lowerCAmelCase__   ,       lowerCAmelCase__						)						):
      lowerCAmelCase_       :    List[Any]								=			os.path.join(lowerCAmelCase__   ,       lowerCAmelCase__   ,       lowerCAmelCase__						)
      return model_file
    else:
      raise EnvironmentError(
          f"Error no file named {weights_name} found in directory {pretrained_model_name_or_path}."						)
  else:
    # 1. First check if deprecated way of loading from branches is used
    if (
        revision in DEPRECATED_REVISION_ARGS
        and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME)
        and version.parse(version.parse(lowerCAmelCase__						).base_version						) >= version.parse('0.20.0'						)
    ):
      try:
        lowerCAmelCase_       :    Any								=			hf_hub_download(
            lowerCAmelCase__   ,       filename=_add_variant(lowerCAmelCase__   ,       lowerCAmelCase__						)   ,       cache_dir=lowerCAmelCase__   ,       force_download=lowerCAmelCase__   ,       proxies=lowerCAmelCase__   ,       resume_download=lowerCAmelCase__   ,       local_files_only=lowerCAmelCase__   ,       use_auth_token=lowerCAmelCase__   ,       user_agent=lowerCAmelCase__   ,       subfolder=lowerCAmelCase__   ,       revision=revision or commit_hash   ,       )
        warnings.warn(
            f"Loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'` is deprecated. Loading instead from `revision='main'` with `variant={revision}`. Loading model variants via `revision='{revision}'` will be removed in diffusers v1. Please use `variant='{revision}'` instead."   ,       lowerCAmelCase__   ,       )
        return model_file
      except:  # noqa: E722
        warnings.warn(
            f"You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant='{revision}'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(lowerCAmelCase__   ,       lowerCAmelCase__						)} file in the 'main' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title '{pretrained_model_name_or_path} is missing {_add_variant(lowerCAmelCase__   ,       lowerCAmelCase__						)}' so that the correct variant file can be added."   ,       lowerCAmelCase__   ,       )
    try:
      # 2. Load model file as usual
      lowerCAmelCase_       :    int								=			hf_hub_download(
          lowerCAmelCase__   ,       filename=lowerCAmelCase__   ,       cache_dir=lowerCAmelCase__   ,       force_download=lowerCAmelCase__   ,       proxies=lowerCAmelCase__   ,       resume_download=lowerCAmelCase__   ,       local_files_only=lowerCAmelCase__   ,       use_auth_token=lowerCAmelCase__   ,       user_agent=lowerCAmelCase__   ,       subfolder=lowerCAmelCase__   ,       revision=revision or commit_hash   ,       )
      return model_file

    except RepositoryNotFoundError:
      raise EnvironmentError(
          f"{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier "
          'listed on \'https://huggingface.co/models\'\nIf this is a private repository, make sure to pass a '
          'token having permission to this repo with `use_auth_token` or log in with `huggingface-cli '
          'login`.'						)
    except RevisionNotFoundError:
      raise EnvironmentError(
          f"{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for "
          'this model name. Check the model page at '
          f"'https://huggingface.co/{pretrained_model_name_or_path}' for available revisions."						)
    except EntryNotFoundError:
      raise EnvironmentError(
          f"{pretrained_model_name_or_path} does not appear to have a file named {weights_name}."						)
    except HTTPError as err:
      raise EnvironmentError(
          f"There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}"						)
    except ValueError:
      raise EnvironmentError(
          f"We couldn't connect to '{HUGGINGFACE_CO_RESOLVE_ENDPOINT}' to load this model, couldn't find it"
          f" in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a"
          f" directory containing a file named {weights_name} or"
          ' \nCheckout your internet connection or see how to run the library in'
          ' offline mode at \'https://huggingface.co/docs/diffusers/installation#offline-mode\'.'						)
    except EnvironmentError:
      raise EnvironmentError(
          f"Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it from "
          '\'https://huggingface.co/models\', make sure you don\'t have a local directory with the same name. '
          f"Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a directory "
          f"containing a file named {weights_name}"						)










"""simple docstring"""







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







class 				UpperCamelCase__       (     lowercase_  ):




      """simple docstring"""




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



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[Any]	):
        lowerCAmelCase_       :    Any								=			0
        lowerCAmelCase_       :    Optional[Any]								=			False
        while not completed:
          if counter == 1:
            self.reset()
          lowerCAmelCase_       :    Any								=			self.advance()
          if not self.does_advance(SCREAMING_SNAKE_CASE_	):
            raise Exception(
                'Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.'	)

          lowerCAmelCase_						,lowerCAmelCase_						,lowerCAmelCase_       :    List[str]								=			self.update(SCREAMING_SNAKE_CASE_	)
          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 					SCREAMING_SNAKE_CASE__			(				self    :				Tuple	):
        raise NotImplementedError(
            F"{self.__class__} is an abstract class. Only classes inheriting this class can be called."	)



      @abstractmethod
      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[Any]   ,      SCREAMING_SNAKE_CASE_    :				int	):
        raise NotImplementedError(
            F"{self.__class__} is an abstract class. Only classes inheriting this class can be called."	)



      @abstractmethod
      def 					SCREAMING_SNAKE_CASE__			(				self    :				int   ,      SCREAMING_SNAKE_CASE_    :				int	):
        raise NotImplementedError(
            F"{self.__class__} is an abstract class. Only classes inheriting this class can be called."	)



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



      @abstractmethod
      def 					SCREAMING_SNAKE_CASE__			(				self    :				str	):
        raise NotImplementedError(
            F"{self.__class__} is an abstract class. Only classes inheriting this class can be called."	)





      @abstractmethod
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[Any]   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=False	):
        raise NotImplementedError(
            F"{self.__class__} is an abstract class. Only classes inheriting this class can be called."	)







class 				UpperCamelCase__       (     lowercase_  ):




      """simple docstring"""




      def __init__(				self    :				Tuple   ,      SCREAMING_SNAKE_CASE_    :				List[int]	):
        super(SCREAMING_SNAKE_CASE_   ,      self	).__init__()

        if not isinstance(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	) or len(SCREAMING_SNAKE_CASE_	) == 0:
          raise ValueError(F"`token_ids` has to be a non-empty list, but is {token_ids}."	)
        if any((not isinstance(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	) 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_       :    Union[str, Any]								=			token_ids

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



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



      def 					SCREAMING_SNAKE_CASE__			(				self    :				str   ,      SCREAMING_SNAKE_CASE_    :				int	):
        if not isinstance(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	):
          raise ValueError(F"`token_id` has to be an `int`, but is {token_id} of type {type(SCREAMING_SNAKE_CASE_	)}"	)

        if self.completed:
          return False

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



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Union[str, Any]   ,      SCREAMING_SNAKE_CASE_    :				int	):
        if not isinstance(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	):
          raise ValueError(F"`token_id` has to be an `int`, but is {token_id} of type {type(SCREAMING_SNAKE_CASE_	)}"	)

        lowerCAmelCase_       :    List[Any]								=			False
        lowerCAmelCase_       :    Optional[int]								=			False
        lowerCAmelCase_       :    Union[str, Any]								=			False

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



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[Any]	):
        lowerCAmelCase_       :    Optional[int]								=			False
        lowerCAmelCase_       :    int								=			0



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





      def 					SCREAMING_SNAKE_CASE__			(				self    :				Dict   ,      SCREAMING_SNAKE_CASE_    :				Tuple=False	):
        lowerCAmelCase_       :    Any								=			PhrasalConstraint(self.token_ids	)

        if stateful:
          lowerCAmelCase_       :    int								=			self.seqlen
          lowerCAmelCase_       :    Dict								=			self.fulfilled_idx
          lowerCAmelCase_       :    Optional[Any]								=			self.completed

        return new_constraint







class 				UpperCamelCase__       :




      """simple docstring"""




      def __init__(				self    :				List[Any]   ,      SCREAMING_SNAKE_CASE_    :				List[List[int]]   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=True	):
        lowerCAmelCase_       :    Tuple								=			max([len(SCREAMING_SNAKE_CASE_	) for one in nested_token_ids]	)

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

            lowerCAmelCase_       :    Tuple								=			level[token_id]

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

        lowerCAmelCase_       :    Union[str, Any]								=			root



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[Any]   ,      SCREAMING_SNAKE_CASE_    :				Any	):
        lowerCAmelCase_       :    str								=			self.trie

        for current_token in current_seq:
          lowerCAmelCase_       :    Optional[int]								=			start[current_token]

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

        return next_tokens



      def 					SCREAMING_SNAKE_CASE__			(				self    :				str   ,      SCREAMING_SNAKE_CASE_    :				Any	):
        lowerCAmelCase_       :    Any								=			self.next_tokens(SCREAMING_SNAKE_CASE_	)

        return len(SCREAMING_SNAKE_CASE_	) == 0



      def 					SCREAMING_SNAKE_CASE__			(				self    :				int   ,      SCREAMING_SNAKE_CASE_    :				int	):
        lowerCAmelCase_       :    Tuple								=			list(root.values()	)
        if len(SCREAMING_SNAKE_CASE_	) == 0:
          return 1
        else:
          return sum([self.count_leaves(SCREAMING_SNAKE_CASE_	) for nn in next_nodes]	)





      def 					SCREAMING_SNAKE_CASE__			(				self    :				Union[str, Any]   ,      SCREAMING_SNAKE_CASE_    :				str   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]	):
        lowerCAmelCase_       :    Any								=			self.count_leaves(SCREAMING_SNAKE_CASE_	)
        return len(SCREAMING_SNAKE_CASE_	) != leaf_count







class 				UpperCamelCase__       (     lowercase_  ):




      """simple docstring"""




      def __init__(				self    :				List[Any]   ,      SCREAMING_SNAKE_CASE_    :				List[List[int]]	):
        super(SCREAMING_SNAKE_CASE_   ,      self	).__init__()

        if not isinstance(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	) or len(SCREAMING_SNAKE_CASE_	) == 0:
          raise ValueError(F"`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}."	)
        if any(not isinstance(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	) 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(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	) 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_       :    Dict								=			DisjunctiveTrie(SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    Optional[int]								=			nested_token_ids

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



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Dict	):
        lowerCAmelCase_       :    int								=			self.trie.next_tokens(self.current_seq	)

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



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Dict   ,      SCREAMING_SNAKE_CASE_    :				int	):
        if not isinstance(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	):
          raise ValueError(F"`token_id` is supposed to be type `int`, but is {token_id} of type {type(SCREAMING_SNAKE_CASE_	)}"	)

        lowerCAmelCase_       :    Optional[int]								=			self.trie.next_tokens(self.current_seq	)

        return token_id in next_tokens



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Any   ,      SCREAMING_SNAKE_CASE_    :				int	):
        if not isinstance(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	):
          raise ValueError(F"`token_id` is supposed to be type `int`, but is {token_id} of type {type(SCREAMING_SNAKE_CASE_	)}"	)

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

        if self.does_advance(SCREAMING_SNAKE_CASE_	):
          self.current_seq.append(SCREAMING_SNAKE_CASE_	)
          lowerCAmelCase_       :    List[Any]								=			True
        else:
          lowerCAmelCase_       :    List[str]								=			True
          self.reset()

        lowerCAmelCase_       :    Dict								=			self.trie.reached_leaf(self.current_seq	)
        lowerCAmelCase_       :    List[str]								=			completed

        return stepped, completed, reset



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[Any]	):
        lowerCAmelCase_       :    int								=			False
        lowerCAmelCase_       :    Optional[Any]								=			[]



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





      def 					SCREAMING_SNAKE_CASE__			(				self    :				Tuple   ,      SCREAMING_SNAKE_CASE_    :				Dict=False	):
        lowerCAmelCase_       :    Dict								=			DisjunctiveConstraint(self.token_ids	)

        if stateful:
          lowerCAmelCase_       :    Dict								=			self.seqlen
          lowerCAmelCase_       :    Optional[Any]								=			self.current_seq
          lowerCAmelCase_       :    List[Any]								=			self.completed

        return new_constraint







class 				UpperCamelCase__       :




      """simple docstring"""




      def __init__(				self    :				Optional[int]   ,      SCREAMING_SNAKE_CASE_    :				List[Constraint]	):
        lowerCAmelCase_       :    Optional[int]								=			constraints

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

        self.init_state()



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[Any]	):
        lowerCAmelCase_       :    Tuple								=			[]
        lowerCAmelCase_       :    int								=			None
        lowerCAmelCase_       :    Optional[int]								=			[constraint.copy(stateful=SCREAMING_SNAKE_CASE_	) for constraint in self.constraints]



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

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



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[Any]   ,      SCREAMING_SNAKE_CASE_    :				Optional[List[int]]	):
        self.init_state()

        if token_ids is not None:
          for token in token_ids:
            # completes or steps **one** constraint
            lowerCAmelCase_						,lowerCAmelCase_       :    Union[str, Any]								=			self.add(SCREAMING_SNAKE_CASE_	)

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



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Dict   ,      SCREAMING_SNAKE_CASE_    :				int	):
        if not isinstance(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	):
          raise ValueError(F"`token_id` should be an `int`, but is `{token_id}`."	)

        lowerCAmelCase_						,lowerCAmelCase_       :    Tuple								=			False, False

        if self.completed:
          lowerCAmelCase_       :    Any								=			True
          lowerCAmelCase_       :    Dict								=			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_       :    Any								=			self.inprogress_constraint.update(SCREAMING_SNAKE_CASE_	)
          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=SCREAMING_SNAKE_CASE_	)	)
            lowerCAmelCase_       :    Optional[int]								=			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_       :    str								=			None

            if len(self.pending_constraints	) == 0:
              # we're done!
              lowerCAmelCase_       :    Union[str, 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(SCREAMING_SNAKE_CASE_	):
              lowerCAmelCase_						,lowerCAmelCase_						,lowerCAmelCase_       :    str								=			pending_constraint.update(SCREAMING_SNAKE_CASE_	)

              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(SCREAMING_SNAKE_CASE_	)
                lowerCAmelCase_       :    Any								=			None

              if not complete and stepped:
                lowerCAmelCase_       :    Optional[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[int]								=			(
                    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_       :    Optional[Any]								=			True

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

        return complete, stepped





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

        if stateful:
          lowerCAmelCase_       :    Any								=			[
              constraint.copy(stateful=SCREAMING_SNAKE_CASE_	) for constraint in self.complete_constraints
          ]
          if self.inprogress_constraint is not None:
            lowerCAmelCase_       :    List[str]								=			self.inprogress_constraint.copy(stateful=SCREAMING_SNAKE_CASE_	)
          lowerCAmelCase_       :    List[Any]								=			[constraint.copy() for constraint in self.pending_constraints]

        return new_state





