





'''simple docstring'''

import shutil
import tempfile
import unittest
from unittest.mock import patch

from transformers import (
    DefaultFlowCallback,
    IntervalStrategy,
    PrinterCallback,
    ProgressCallback,
    Trainer,
    TrainerCallback,
    TrainingArguments,
    is_torch_available,
)
from transformers.testing_utils import require_torch


if is_torch_available():
  from transformers.trainer import DEFAULT_CALLBACKS

  from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel






class        UpperCAmelCase     (	__SCREAMING_SNAKE_CASE    ):
 def __init__(    self				:Dict							)->							Dict:
     A__    =      []
 def 					UpperCAmelCase_							(    self				:str						,	lowercase_				:int						,	lowercase_				:int						,	lowercase_				:List[str]						,	**lowercase_				:Tuple							)->							int:
     self.events.append("on_init_end"							)
 def 					UpperCAmelCase_							(    self				:Tuple						,	lowercase_				:Union[str, Any]						,	lowercase_				:Optional[int]						,	lowercase_				:Optional[Any]						,	**lowercase_				:Union[str, Any]							)->							Union[str, Any]:
     self.events.append("on_train_begin"							)
 def 					UpperCAmelCase_							(    self				:Union[str, Any]						,	lowercase_				:Any						,	lowercase_				:Optional[Any]						,	lowercase_				:Tuple						,	**lowercase_				:Dict							)->							str:
     self.events.append("on_train_end"							)
 def 					UpperCAmelCase_							(    self				:List[Any]						,	lowercase_				:Any						,	lowercase_				:Any						,	lowercase_				:Dict						,	**lowercase_				:Optional[Any]							)->							str:
     self.events.append("on_epoch_begin"							)
 def 					UpperCAmelCase_							(    self				:Dict						,	lowercase_				:Optional[Any]						,	lowercase_				:List[str]						,	lowercase_				:Optional[Any]						,	**lowercase_				:Optional[Any]							)->							List[Any]:
     self.events.append("on_epoch_end"							)
 def 					UpperCAmelCase_							(    self				:Dict						,	lowercase_				:Any						,	lowercase_				:int						,	lowercase_				:Optional[int]						,	**lowercase_				:int							)->							List[Any]:
     self.events.append("on_step_begin"							)
 def 					UpperCAmelCase_							(    self				:str						,	lowercase_				:Optional[int]						,	lowercase_				:Optional[int]						,	lowercase_				:int						,	**lowercase_				:Union[str, Any]							)->							List[str]:
     self.events.append("on_step_end"							)
 def 					UpperCAmelCase_							(    self				:Dict						,	lowercase_				:Any						,	lowercase_				:int						,	lowercase_				:Any						,	**lowercase_				:int							)->							List[Any]:
     self.events.append("on_evaluate"							)
 def 					UpperCAmelCase_							(    self				:Any						,	lowercase_				:str						,	lowercase_				:List[Any]						,	lowercase_				:Optional[Any]						,	**lowercase_				:str							)->							str:
     self.events.append("on_predict"							)
 def 					UpperCAmelCase_							(    self				:Optional[int]						,	lowercase_				:Any						,	lowercase_				:List[Any]						,	lowercase_				:List[str]						,	**lowercase_				:str							)->							List[Any]:
     self.events.append("on_save"							)
 def 					UpperCAmelCase_							(    self				:List[Any]						,	lowercase_				:Tuple						,	lowercase_				:List[Any]						,	lowercase_				:str						,	**lowercase_				:str							)->							Tuple:
     self.events.append("on_log"							)



 def 					UpperCAmelCase_							(    self				:List[str]						,	lowercase_				:Any						,	lowercase_				:str						,	lowercase_				:Optional[Any]						,	**lowercase_				:str							)->							Optional[Any]:
     self.events.append("on_prediction_step"							)






@require_torch
class        UpperCAmelCase     (	unittest.TestCase    ):
 def 					UpperCAmelCase_							(    self				:int							)->							Optional[int]:
     A__    =      tempfile.mkdtemp()
 def 					UpperCAmelCase_							(    self				:Optional[Any]							)->							str:
     shutil.rmtree(self.output_dir							)
 def 					UpperCAmelCase_							(    self				:Tuple						,	lowercase_				:Union[str, Any]=0						,	lowercase_				:Any=0						,	lowercase_				:Dict=64						,	lowercase_				:Optional[Any]=64						,	lowercase_				:List[str]=None						,	lowercase_				:Tuple=False						,	**lowercase_				:Union[str, Any]							)->							Tuple:
     A__    =      RegressionDataset(length=_SCREAMING_SNAKE_CASE							)
     A__    =      RegressionDataset(length=_SCREAMING_SNAKE_CASE							)
     A__    =      RegressionModelConfig(a=_SCREAMING_SNAKE_CASE						,	b=_SCREAMING_SNAKE_CASE							)
     A__    =      RegressionPreTrainedModel(_SCREAMING_SNAKE_CASE							)

     A__    =      TrainingArguments(self.output_dir						,	disable_tqdm=_SCREAMING_SNAKE_CASE						,	report_to=[]						,	**_SCREAMING_SNAKE_CASE							)
     return Trainer(
         _SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE						,	train_dataset=_SCREAMING_SNAKE_CASE						,	eval_dataset=_SCREAMING_SNAKE_CASE						,	callbacks=_SCREAMING_SNAKE_CASE						,	)
 def 					UpperCAmelCase_							(    self				:Tuple						,	lowercase_				:int						,	lowercase_				:List[Any]							)->							Dict:
     self.assertEqual(len(_SCREAMING_SNAKE_CASE							)						,	len(_SCREAMING_SNAKE_CASE							)							)

     # Order doesn't matter
     A__    =      sorted(_SCREAMING_SNAKE_CASE						,	key=lambda lowercase_							: cb.__name__ if isinstance(_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE							) else cb.__class__.__name__							)
     A__    =      sorted(_SCREAMING_SNAKE_CASE						,	key=lambda lowercase_							: cb.__name__ if isinstance(_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE							) else cb.__class__.__name__							)

     for cba, cba in zip(_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE							):
         if isinstance(_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE							) and isinstance(_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE							):
             self.assertEqual(_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE							)
         elif isinstance(_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE							) and not isinstance(_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE							):
             self.assertEqual(_SCREAMING_SNAKE_CASE						,	cba.__class__							)
         elif not isinstance(_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE							) and isinstance(_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE							):
             self.assertEqual(cba.__class__						,	_SCREAMING_SNAKE_CASE							)
         else:
             self.assertEqual(_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE							)
 def 					UpperCAmelCase_							(    self				:str						,	lowercase_				:str							)->							Tuple:
     A__    =      ["on_init_end", "on_train_begin"]
     A__    =      0
     A__    =      len(trainer.get_eval_dataloader()							)
     A__    =      ["on_prediction_step"] * len(trainer.get_eval_dataloader()							) + ["on_log", "on_evaluate"]
     for _ in range(trainer.state.num_train_epochs							):
         expected_events.append("on_epoch_begin"							)
         for _ in range(_SCREAMING_SNAKE_CASE							):
             step += 1
             expected_events += ["on_step_begin", "on_step_end"]
             if step % trainer.args.logging_steps == 0:
                 expected_events.append("on_log"							)
             if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0:
                 expected_events += evaluation_events.copy()
             if step % trainer.args.save_steps == 0:
                 expected_events.append("on_save"							)
         expected_events.append("on_epoch_end"							)
         if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH:
             expected_events += evaluation_events.copy()
     expected_events += ["on_log", "on_train_end"]
     return expected_events
 def 					UpperCAmelCase_							(    self				:Union[str, Any]							)->							List[Any]:
     A__    =      self.get_trainer()
     A__    =      DEFAULT_CALLBACKS.copy() + [ProgressCallback]
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	_SCREAMING_SNAKE_CASE							)

     # Callbacks passed at init are added to the default callbacks
     A__    =      self.get_trainer(callbacks=[MyTestTrainerCallback]							)
     expected_callbacks.append(_SCREAMING_SNAKE_CASE							)
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	_SCREAMING_SNAKE_CASE							)

     # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
     A__    =      self.get_trainer(disable_tqdm=_SCREAMING_SNAKE_CASE							)
     A__    =      DEFAULT_CALLBACKS.copy() + [PrinterCallback]
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	_SCREAMING_SNAKE_CASE							)
 def 					UpperCAmelCase_							(    self				:List[Any]							)->							Optional[Any]:
     A__    =      DEFAULT_CALLBACKS.copy() + [ProgressCallback]
     A__    =      self.get_trainer()

     # We can add, pop, or remove by class name
     trainer.remove_callback(_SCREAMING_SNAKE_CASE							)
     expected_callbacks.remove(_SCREAMING_SNAKE_CASE							)
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	_SCREAMING_SNAKE_CASE							)

     A__    =      self.get_trainer()
     A__    =      trainer.pop_callback(_SCREAMING_SNAKE_CASE							)
     self.assertEqual(cb.__class__						,	_SCREAMING_SNAKE_CASE							)
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	_SCREAMING_SNAKE_CASE							)

     trainer.add_callback(_SCREAMING_SNAKE_CASE							)
     expected_callbacks.insert(0						,	_SCREAMING_SNAKE_CASE							)
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	_SCREAMING_SNAKE_CASE							)

     # We can also add, pop, or remove by instance
     A__    =      self.get_trainer()
     A__    =      trainer.callback_handler.callbacks[0]
     trainer.remove_callback(_SCREAMING_SNAKE_CASE							)
     expected_callbacks.remove(_SCREAMING_SNAKE_CASE							)
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	_SCREAMING_SNAKE_CASE							)

     A__    =      self.get_trainer()
     A__    =      trainer.callback_handler.callbacks[0]
     A__    =      trainer.pop_callback(_SCREAMING_SNAKE_CASE							)
     self.assertEqual(_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE							)
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	_SCREAMING_SNAKE_CASE							)

     trainer.add_callback(_SCREAMING_SNAKE_CASE							)
     expected_callbacks.insert(0						,	_SCREAMING_SNAKE_CASE							)
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	_SCREAMING_SNAKE_CASE							)



 def 					UpperCAmelCase_							(    self				:Any							)->							List[str]:
     import warnings

     # XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested
     warnings.simplefilter(action="ignore"						,	category=_SCREAMING_SNAKE_CASE							)

     A__    =      self.get_trainer(callbacks=[MyTestTrainerCallback]							)
     trainer.train()
     A__    =      trainer.callback_handler.callbacks[-2].events
     self.assertEqual(_SCREAMING_SNAKE_CASE						,	self.get_expected_events(_SCREAMING_SNAKE_CASE							)							)

     # Independent log/save/eval
     A__    =      self.get_trainer(callbacks=[MyTestTrainerCallback]						,	logging_steps=5							)
     trainer.train()
     A__    =      trainer.callback_handler.callbacks[-2].events
     self.assertEqual(_SCREAMING_SNAKE_CASE						,	self.get_expected_events(_SCREAMING_SNAKE_CASE							)							)

     A__    =      self.get_trainer(callbacks=[MyTestTrainerCallback]						,	save_steps=5							)
     trainer.train()
     A__    =      trainer.callback_handler.callbacks[-2].events
     self.assertEqual(_SCREAMING_SNAKE_CASE						,	self.get_expected_events(_SCREAMING_SNAKE_CASE							)							)

     A__    =      self.get_trainer(callbacks=[MyTestTrainerCallback]						,	eval_steps=5						,	evaluation_strategy="steps"							)
     trainer.train()
     A__    =      trainer.callback_handler.callbacks[-2].events
     self.assertEqual(_SCREAMING_SNAKE_CASE						,	self.get_expected_events(_SCREAMING_SNAKE_CASE							)							)

     A__    =      self.get_trainer(callbacks=[MyTestTrainerCallback]						,	evaluation_strategy="epoch"							)
     trainer.train()
     A__    =      trainer.callback_handler.callbacks[-2].events
     self.assertEqual(_SCREAMING_SNAKE_CASE						,	self.get_expected_events(_SCREAMING_SNAKE_CASE							)							)

     # A bit of everything
     A__    =      self.get_trainer(
         callbacks=[MyTestTrainerCallback]						,	logging_steps=3						,	save_steps=10						,	eval_steps=5						,	evaluation_strategy="steps"						,	)
     trainer.train()
     A__    =      trainer.callback_handler.callbacks[-2].events
     self.assertEqual(_SCREAMING_SNAKE_CASE						,	self.get_expected_events(_SCREAMING_SNAKE_CASE							)							)

     # warning should be emitted for duplicated callbacks
     with patch("transformers.trainer_callback.logger.warning"							) as warn_mock:
         A__    =      self.get_trainer(
             callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]						,	)
         assert str(_SCREAMING_SNAKE_CASE							) in warn_mock.call_args[0][0]










'''simple docstring'''

import argparse
import os
import re

import packaging.version


__lowerCAmelCase							:     List[Any]		     ="examples/"
__lowerCAmelCase							:     Dict		     ={
    "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							:     List[str]		     ={
    "init": "src/transformers/__init__.py",
    "setup": "setup.py",
}
__lowerCAmelCase							:     str		     ="README.md"





def 						UpperCamelCase    (							_lowerCamelCase : Optional[Any]						,	_lowerCamelCase : Union[str, Any]						,	_lowerCamelCase : Optional[Any]	):
    with open(_lowerCamelCase						,	"r"						,	encoding="utf-8"						,	newline="\n"	) as f:
        A__    =      f.read()
    A__,     A__    =      REPLACE_PATTERNS[pattern]
    A__    =      replace.replace("VERSION"						,	_lowerCamelCase	)
    A__    =      re_pattern.sub(_lowerCamelCase						,	_lowerCamelCase	)
    with open(_lowerCamelCase						,	"w"						,	encoding="utf-8"						,	newline="\n"	) as f:
        f.write(_lowerCamelCase	)





def 						UpperCamelCase    (							_lowerCamelCase : int	):
    for folder, directories, fnames in os.walk(_lowerCamelCase	):
        # 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(_lowerCamelCase						,	_lowerCamelCase	)						,	_lowerCamelCase						,	pattern="examples"	)





def 						UpperCamelCase    (							_lowerCamelCase : int						,	_lowerCamelCase : Union[str, Any]=False	):
    for pattern, fname in REPLACE_FILES.items():
        update_version_in_file(_lowerCamelCase						,	_lowerCamelCase						,	_lowerCamelCase	)
    if not patch:
        update_version_in_examples(_lowerCamelCase	)





def 						UpperCamelCase    (							):
    A__    =      "🤗 Transformers currently provides the following architectures"
    A__    =      "1. Want to contribute a new model?"
    with open(_lowerCamelCase						,	"r"						,	encoding="utf-8"						,	newline="\n"	) as f:
        A__    =      f.readlines()

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

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

    with open(_lowerCamelCase						,	"w"						,	encoding="utf-8"						,	newline="\n"	) as f:
        f.writelines(_lowerCamelCase	)





def 						UpperCamelCase    (							):
    with open(REPLACE_FILES["init"]						,	"r"	) as f:
        A__    =      f.read()
    A__    =      REPLACE_PATTERNS["init"][0].search(_lowerCamelCase	).groups()[0]
    return packaging.version.parse(_lowerCamelCase	)





def 						UpperCamelCase    (							_lowerCamelCase : Dict=False	):
    A__    =      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__    =      default_version.base_version
    elif patch:
        A__    =      F"{default_version.major}.{default_version.minor}.{default_version.micro + 1}"
    else:
        A__    =      F"{default_version.major}.{default_version.minor + 1}.0"

    # Now let's ask nicely if that's the right one.
    A__    =      input(F"Which version are you releasing? [{default_version}]"	)
    if len(_lowerCamelCase	) == 0:
        A__    =      default_version

    print(F"Updating version to {version}."	)
    global_version_update(_lowerCamelCase						,	patch=_lowerCamelCase	)
    if not patch:
        print("Cleaning main README, don't forget to run `make fix-copies`."	)
        clean_main_ref_in_model_list()





def 						UpperCamelCase    (							):
    A__    =      get_version()
    A__    =      F"{current_version.major}.{current_version.minor + 1}.0.dev0"
    A__    =      current_version.base_version

    # Check with the user we got that right.
    A__    =      input(F"Which version are we developing now? [{dev_version}]"	)
    if len(_lowerCamelCase	) == 0:
        A__    =      dev_version

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


if __name__ == "__main__":
  __lowerCAmelCase							:     Union[str, Any]		     =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							:     int		     =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()



