



'''simple docstring'''






import math
from enum import Enum
from typing import Optional, Union

from torch.optim import Optimizer
from torch.optim.lr_scheduler import LambdaLR

from .utils import logging


lowerCAmelCase: Any							=						logging.get_logger(__name__)

class 							a__( _a     ):
    lowercase__			=   """linear"""
    lowercase__			=   """cosine"""
    lowercase__			=   """cosine_with_restarts"""
    lowercase__			=   """polynomial"""
    lowercase__			=   """constant"""
    lowercase__			=   """constant_with_warmup"""
    lowercase__			=   """piecewise_constant"""







def 				lowerCamelCase__	(						_A			,      _A = -1    ):
  return LambdaLR(a__			,      lambda _A    : 1			,      last_epoch=a__    )
def 				lowerCamelCase__	(						_A			,      _A			,      _A = -1    ):





  def lr_lambda(_A    ):
    if current_step < num_warmup_steps:
      return float(a__    ) / float(max(1.0			,      a__    )    )
    return 1.0

  return LambdaLR(a__			,      a__			,      last_epoch=a__    )
def 				lowerCamelCase__	(						_A			,      _A			,      _A = -1    ):
  a						: Optional[Any]					       =					{}
  a						: Tuple					       =					step_rules.split(','    )
  for rule_str in rule_list[:-1]:
    a       ,     a						: Tuple					       =					rule_str.split(':'    )
    a						: str					       =					int(a__    )
    a						: str					       =					float(a__    )
    a						: str					       =					value
  a						: int					       =					float(rule_list[-1]    )

  def create_rules_function(_A			,      _A    ):
    def rule_func(_A    ) -> float:
      a						: Dict					       =					sorted(rules_dict.keys()    )
      for i, sorted_step in enumerate(a__    ):
        if steps < sorted_step:
          return rules_dict[sorted_steps[i]]
      return last_lr_multiple

    return rule_func

  a						: Optional[Any]					       =					create_rules_function(a__			,      a__    )

  return LambdaLR(a__			,      a__			,      last_epoch=a__    )
def 				lowerCamelCase__	(						_A			,      _A			,      _A			,      _A=-1    ):





  def lr_lambda(_A    ):
    if current_step < num_warmup_steps:
      return float(a__    ) / float(max(1			,      a__    )    )
    return max(
        0.0			,      float(num_training_steps - current_step    ) / float(max(1			,      num_training_steps - num_warmup_steps    )    )    )

  return LambdaLR(a__			,      a__			,      a__    )
def 				lowerCamelCase__	(						_A			,      _A			,      _A			,      _A = 0.5			,      _A = -1    ):





  def lr_lambda(_A    ):
    if current_step < num_warmup_steps:
      return float(a__    ) / float(max(1			,      a__    )    )
    a						: Optional[int]					       =					float(current_step - num_warmup_steps    ) / float(max(1			,      num_training_steps - num_warmup_steps    )    )
    return max(0.0			,      0.5 * (1.0 + math.cos(math.pi * float(a__    ) * 2.0 * progress    ))    )

  return LambdaLR(a__			,      a__			,      a__    )
def 				lowerCamelCase__	(						_A			,      _A			,      _A			,      _A = 1			,      _A = -1    ):





  def lr_lambda(_A    ):
    if current_step < num_warmup_steps:
      return float(a__    ) / float(max(1			,      a__    )    )
    a						: Tuple					       =					float(current_step - num_warmup_steps    ) / float(max(1			,      num_training_steps - num_warmup_steps    )    )
    if progress >= 1.0:
      return 0.0
    return max(0.0			,      0.5 * (1.0 + math.cos(math.pi * ((float(a__    ) * progress) % 1.0)    ))    )

  return LambdaLR(a__			,      a__			,      a__    )
def 				lowerCamelCase__	(						_A			,      _A			,      _A			,      _A=1E-7			,      _A=1.0			,      _A=-1    ):
  a						: Optional[Any]					       =					optimizer.defaults['lr']
  if not (lr_init > lr_end):
    raise ValueError(f"""lr_end ({lr_end}) must be be smaller than initial lr ({lr_init})"""    )

  def lr_lambda(_A    ):
    if current_step < num_warmup_steps:
      return float(a__    ) / float(max(1			,      a__    )    )
    elif current_step > num_training_steps:
      return lr_end / lr_init  # as LambdaLR multiplies by lr_init
    else:
      a						: Optional[Any]					       =					lr_init - lr_end
      a						: Optional[int]					       =					num_training_steps - num_warmup_steps
      a						: Tuple					       =					1 - (current_step - num_warmup_steps) / decay_steps
      a						: Dict					       =					lr_range * pct_remaining**power + lr_end
      return decay / lr_init  # as LambdaLR multiplies by lr_init

  return LambdaLR(a__			,      a__			,      a__    )


lowerCAmelCase: Optional[int]							=						{
    SchedulerType.LINEAR: get_linear_schedule_with_warmup,
    SchedulerType.COSINE: get_cosine_schedule_with_warmup,
    SchedulerType.COSINE_WITH_RESTARTS: get_cosine_with_hard_restarts_schedule_with_warmup,
    SchedulerType.POLYNOMIAL: get_polynomial_decay_schedule_with_warmup,
    SchedulerType.CONSTANT: get_constant_schedule,
    SchedulerType.CONSTANT_WITH_WARMUP: get_constant_schedule_with_warmup,
    SchedulerType.PIECEWISE_CONSTANT: get_piecewise_constant_schedule,
}

def 				lowerCamelCase__	(						_A			,      _A			,      _A = None			,      _A = None			,      _A = None			,      _A = 1			,      _A = 1.0			,      _A = -1			,      ):
  a						: Optional[int]					       =					SchedulerType(a__    )
  a						: Dict					       =					TYPE_TO_SCHEDULER_FUNCTION[name]
  if name == SchedulerType.CONSTANT:
    return schedule_func(a__			,      last_epoch=a__    )

  if name == SchedulerType.PIECEWISE_CONSTANT:
    return schedule_func(a__			,      step_rules=a__			,      last_epoch=a__    )

  # All other schedulers require `num_warmup_steps`
  if num_warmup_steps is None:
    raise ValueError(f"""{name} requires `num_warmup_steps`, please provide that argument."""    )

  if name == SchedulerType.CONSTANT_WITH_WARMUP:
    return schedule_func(a__			,      num_warmup_steps=a__			,      last_epoch=a__    )

  # All other schedulers require `num_training_steps`
  if num_training_steps is None:
    raise ValueError(f"""{name} requires `num_training_steps`, please provide that argument."""    )

  if name == SchedulerType.COSINE_WITH_RESTARTS:
    return schedule_func(
        a__			,      num_warmup_steps=a__			,      num_training_steps=a__			,      num_cycles=a__			,      last_epoch=a__			,      )

  if name == SchedulerType.POLYNOMIAL:
    return schedule_func(
        a__			,      num_warmup_steps=a__			,      num_training_steps=a__			,      power=a__			,      last_epoch=a__			,      )

  return schedule_func(
      a__			,      num_warmup_steps=a__			,      num_training_steps=a__			,      last_epoch=a__    )




'''simple docstring'''






import argparse
import os
import re

import packaging.version


lowerCAmelCase: List[str]							=						'examples/'
lowerCAmelCase: List[Any]							=						{
    'examples': (re.compile(r'^check_min_version\("[^"]+"\)\s*$', re.MULTILINE), 'check_min_version("VERSION")\n'),
    'init': (re.compile(r'^__version__\s+=\s+"([^"]+)"\s*$', re.MULTILINE), '__version__ = "VERSION"\n'),
    'setup': (re.compile(r'^(\s*)version\s*=\s*"[^"]+",', re.MULTILINE), r'\1version="VERSION",'),
    'doc': (re.compile(r'^(\s*)release\s*=\s*"[^"]+"$', re.MULTILINE), 'release = "VERSION"\n'),
}
lowerCAmelCase: str							=						{
    'init': 'src/transformers/__init__.py',
    'setup': 'setup.py',
}
lowerCAmelCase: str							=						'README.md'
def 				lowerCamelCase__	(						_A			,      _A			,      _A    ):
  with open(_A			,      'r'			,      encoding='utf-8'			,      newline='\n'    ) as f:
    a						: Tuple					       =					f.read()
  a       ,     a						: Tuple					       =					REPLACE_PATTERNS[pattern]
  a						: Dict					       =					replace.replace('VERSION'			,      _A    )
  a						: Dict					       =					re_pattern.sub(_A			,      _A    )
  with open(_A			,      'w'			,      encoding='utf-8'			,      newline='\n'    ) as f:
    f.write(_A    )
def 				lowerCamelCase__	(						_A    ):
  for folder, directories, fnames in os.walk(_A    ):
    # Removing some of the folders with non-actively maintained examples from the walk
    if "research_projects" in directories:
      directories.remove('research_projects'    )
    if "legacy" in directories:
      directories.remove('legacy'    )
    for fname in fnames:
      if fname.endswith('.py'    ):
        update_version_in_file(os.path.join(_A			,      _A    )			,      _A			,      pattern='examples'    )
def 				lowerCamelCase__	(						_A			,      _A=False    ):
  for pattern, fname in REPLACE_FILES.items():
    update_version_in_file(_A			,      _A			,      _A    )
  if not patch:
    update_version_in_examples(_A    )
def 				lowerCamelCase__	(						):
  a						: Tuple					       =					'🤗 Transformers currently provides the following architectures'
  a						: Any					       =					'1. Want to contribute a new model?'
  with open(_A			,      'r'			,      encoding='utf-8'			,      newline='\n'    ) as f:
    a						: Tuple					       =					f.readlines()

  # Find the start of the list.
  a						: Optional[int]					       =					0
  while not lines[start_index].startswith(_start_prompt    ):
    start_index += 1
  start_index += 1

  a						: Optional[int]					       =					start_index
  # Update the lines in the model list.
  while not lines[index].startswith(_end_prompt    ):
    if lines[index].startswith('1.'    ):
      a						: List[Any]					       =					lines[index].replace(
          'https://huggingface.co/docs/transformers/main/model_doc'			,      'https://huggingface.co/docs/transformers/model_doc'			,      )
    index += 1

  with open(_A			,      'w'			,      encoding='utf-8'			,      newline='\n'    ) as f:
    f.writelines(_A    )
def 				lowerCamelCase__	(						):
  with open(REPLACE_FILES['init']			,      'r'    ) as f:
    a						: Union[str, Any]					       =					f.read()
  a						: Tuple					       =					REPLACE_PATTERNS['init'][0].search(_A    ).groups()[0]
  return packaging.version.parse(_A    )
def 				lowerCamelCase__	(						_A=False    ):
  a						: int					       =					get_version()
  if patch and default_version.is_devrelease:
    raise ValueError('Can\'t create a patch version from the dev branch, checkout a released version!'    )
  if default_version.is_devrelease:
    a						: Any					       =					default_version.base_version
  elif patch:
    a						: Dict					       =					f"""{default_version.major}.{default_version.minor}.{default_version.micro + 1}"""
  else:
    a						: Union[str, Any]					       =					f"""{default_version.major}.{default_version.minor + 1}.0"""

  # Now let's ask nicely if that's the right one.
  a						: List[Any]					       =					input(f"""Which version are you releasing? [{default_version}]"""    )
  if len(_A    ) == 0:
    a						: Union[str, Any]					       =					default_version

  print(f"""Updating version to {version}."""    )
  global_version_update(_A			,      patch=_A    )
  if not patch:
    print('Cleaning main README, don\'t forget to run `make fix-copies`.'    )
    clean_main_ref_in_model_list()
def 				lowerCamelCase__	(						):
  a						: int					       =					get_version()
  a						: Any					       =					f"""{current_version.major}.{current_version.minor + 1}.0.dev0"""
  a						: int					       =					current_version.base_version

  # Check with the user we got that right.
  a						: Tuple					       =					input(f"""Which version are we developing now? [{dev_version}]"""    )
  if len(_A    ) == 0:
    a						: Optional[int]					       =					dev_version

  print(f"""Updating version to {version}."""    )
  global_version_update(_A    )
  print('Cleaning main README, don\'t forget to run `make fix-copies`.'    )
  clean_main_ref_in_model_list()


if __name__ == "__main__":
     lowerCAmelCase: Tuple							=						argparse.ArgumentParser()
     parser.add_argument('--post_release', action='store_true', help='Whether this is pre or post release.')
     parser.add_argument('--patch', action='store_true', help='Whether or not this is a patch release.')
     lowerCAmelCase: Optional[Any]							=						parser.parse_args()
     if not args.post_release:
          pre_release_work(patch=args.patch)
     elif args.patch:
          print('Nothing to do after a patch :-)')
     else:
          post_release_work()