





'''simple docstring'''


import json
import os
import subprocess
import unittest
from ast import literal_eval

import pytest
from parameterized import parameterized, parameterized_class

from . import is_sagemaker_available


if is_sagemaker_available():
		from sagemaker import Session, TrainingJobAnalytics
		from sagemaker.huggingface import HuggingFace






@pytest.mark.skipif(
    literal_eval(os.getenv("TEST_SAGEMAKER"	,"False"     )     ) is not True	,reason="Skipping test because should only be run when releasing minor transformers version"	,)
@pytest.mark.usefixtures("sm_env"     )
@parameterized_class(
    [
        {
            "framework": "pytorch",
            "script": "run_glue_model_parallelism.py",
            "model_name_or_path": "roberta-large",
            "instance_type": "ml.p3dn.24xlarge",
            "results": {"train_runtime": 1_600, "eval_accuracy": 0.3, "eval_loss": 1.2},
        },
        {
            "framework": "pytorch",
            "script": "run_glue.py",
            "model_name_or_path": "roberta-large",
            "instance_type": "ml.p3dn.24xlarge",
            "results": {"train_runtime": 1_600, "eval_accuracy": 0.3, "eval_loss": 1.2},
        },
    ]     )
class       UpperCamelCase__  (		unittest.TestCase     ):







	def 						__a   (				self			:   Dict		):




				'''simple docstring'''
				if self.framework == "pytorch":
							subprocess.run(
							    F'''cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py'''.split() , encoding="utf-8" , check=lowerCamelCase , )
				assert hasattr(self , "env"		)







	def 						__a   (				self			:   List[str] , lowerCamelCase			:   List[str]		):




				'''simple docstring'''
				# configuration for running training on smdistributed Model Parallel
				a__     = {
				    "enabled": True,
				    "processes_per_host": 8,
				}
				a__     = {
				    "enabled": True,
				    "parameters": {
				        "microbatches": 4,
				        "placement_strategy": "spread",
				        "pipeline": "interleaved",
				        "optimize": "speed",
				        "partitions": 4,
				        "ddp": True,
				    },
				}

				a__     = {"smdistributed": {"modelparallel": smp_options}, "mpi": mpi_options}

				a__     = "trainer" if self.script == "run_glue.py" else "smtrainer"
				# creates estimator
				return HuggingFace(
				    entry_point=self.script , source_dir=self.env.test_path , role=self.env.role , image_uri=self.env.image_uri , base_job_name=F'''{self.env.base_job_name}-{instance_count}-smp-{name_extension}''' , instance_count=lowerCamelCase , instance_type=self.instance_type , debugger_hook_config=lowerCamelCase , hyperparameters={
				        **self.env.hyperparameters,
				        "model_name_or_path": self.model_name_or_path,
				        "max_steps": 5_0_0,
				    } , metric_definitions=self.env.metric_definitions , distribution=lowerCamelCase , py_version="py36" , )







	def 						__a   (				self			:   Dict , lowerCamelCase			:   Dict		):




				'''simple docstring'''
				TrainingJobAnalytics(lowerCamelCase		).export_csv(F'''{self.env.test_path}/{job_name}_metrics.csv'''		)







	@parameterized.expand([(1,)]		)
	def 						__a   (				self			:   int , lowerCamelCase			:   List[str]		):




				'''simple docstring'''
				# create estimator
				a__     = self.create_estimator(lowerCamelCase		)

				# run training
				estimator.fit()

				# result dataframe
				a__     = TrainingJobAnalytics(estimator.latest_training_job.name		).dataframe()

				# extract kpis
				a__     = list(result_metrics_df[result_metrics_df.metric_name == "eval_accuracy"]["value"]		)
				a__     = list(result_metrics_df[result_metrics_df.metric_name == "eval_loss"]["value"]		)
				# get train time from SageMaker job, this includes starting, preprocessing, stopping
				a__     = (
				    Session().describe_training_job(estimator.latest_training_job.name		).get("TrainingTimeInSeconds" , 9_9_9_9_9_9		)
				)

				# assert kpis
				assert train_runtime <= self.results["train_runtime"]
				assert all(t >= self.results["eval_accuracy"] for t in eval_accuracy		)
				assert all(t <= self.results["eval_loss"] for t in eval_loss		)

				# dump tests result into json file to share in PR
				with open(F'''{estimator.latest_training_job.name}.json''' , "w"		) as outfile:
							json.dump({"train_time": train_runtime, "eval_accuracy": eval_accuracy, "eval_loss": eval_loss} , lowerCamelCase		)










'''simple docstring'''


import argparse
import os
import re

import packaging.version


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







def    _lowerCamelCase  (__lowerCamelCase  :     Dict					,  __lowerCamelCase  :     List[Any]					,  __lowerCamelCase  :     Optional[Any]     )   ->    str:
			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    _lowerCamelCase  (__lowerCamelCase  :     Union[str, Any]     )   ->    Dict:
			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    _lowerCamelCase  (__lowerCamelCase  :     int					,  __lowerCamelCase  :     Any=False     )   ->    Optional[Any]:
			for pattern, fname in REPLACE_FILES.items():
						update_version_in_file(__lowerCamelCase					,  __lowerCamelCase					,  __lowerCamelCase     )
			if not patch:
						update_version_in_examples(__lowerCamelCase     )







def    _lowerCamelCase  ()   ->    Tuple:
			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    _lowerCamelCase  ()   ->    Tuple:
			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    _lowerCamelCase  (__lowerCamelCase  :     Any=False     )   ->    int:
			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    _lowerCamelCase  ()   ->    Tuple:
			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_   :   List[str]       =					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()



