





import argparse
from copy import deepcopy

import numpy as np
from datasets import ClassLabel, DatasetDict, load_dataset
from evaluate import load

from transformers import (
    AutoModelForSequenceClassification,
    AutoTokenizer,
    DataCollatorWithPadding,
    Trainer,
    TrainerCallback,
    TrainingArguments,
    set_seed,
)





def 						__SCREAMING_SNAKE_CASE  (				)  ->				Union[str, Any]:







			'''simple docstring'''

			UpperCAmelCase   		=				argparse.ArgumentParser()
			parser.add_argument('''--model_ckpt'''			,			type=UpperCamelCase__			,			default='''microsoft/unixcoder-base-nine''' )
			parser.add_argument('''--num_epochs'''			,			type=UpperCamelCase__			,			default=5 )
			parser.add_argument('''--batch_size'''			,			type=UpperCamelCase__			,			default=6 )
			parser.add_argument('''--gradient_accumulation_steps'''			,			type=UpperCamelCase__			,			default=1 )
			parser.add_argument('''--freeze'''			,			type=UpperCamelCase__			,			default=UpperCamelCase__ )
			parser.add_argument('''--learning_rate'''			,			type=UpperCamelCase__			,			default=5E-4 )
			parser.add_argument('''--seed'''			,			type=UpperCamelCase__			,			default=0 )
			parser.add_argument('''--lr_scheduler_type'''			,			type=UpperCamelCase__			,			default='''cosine''' )
			parser.add_argument('''--num_warmup_steps'''			,			type=UpperCamelCase__			,			default=10 )
			parser.add_argument('''--weight_decay'''			,			type=UpperCamelCase__			,			default=0.01 )
			parser.add_argument('''--output_dir'''			,			type=UpperCamelCase__			,			default='''./results''' )
			return parser.parse_args()


__A				:    List[Any]       =   load("accuracy")





def 						__SCREAMING_SNAKE_CASE  (				UpperCamelCase__ )  ->				List[str]:







			'''simple docstring'''

			UpperCAmelCase		,			UpperCAmelCase   		=				eval_pred
			UpperCAmelCase   		=				np.argmax(UpperCamelCase__			,			axis=1 )
			return metric.compute(predictions=UpperCamelCase__			,			references=UpperCamelCase__ )

class 	A_       (a_			):
	def __init__(    self							,					_A		):






				'''simple docstring'''







				super().__init__()
				UpperCAmelCase   		=				trainer







	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					**_A		):






				'''simple docstring'''







				if control.should_evaluate:
							UpperCAmelCase   		=				deepcopy(_A		)
							self._trainer.evaluate(eval_dataset=self._trainer.train_dataset							,					metric_key_prefix='''train'''		)
							return control_copy





def 						__SCREAMING_SNAKE_CASE  (				)  ->				int:







			'''simple docstring'''

			UpperCAmelCase   		=				get_args()
			set_seed(args.seed )

			UpperCAmelCase   		=				load_dataset('''codeparrot/codecomplex'''			,			split='''train''' )
			UpperCAmelCase   		=				dataset.train_test_split(test_size=0.2 )
			UpperCAmelCase   		=				train_test['''test'''].train_test_split(test_size=0.5 )
			UpperCAmelCase   		=				DatasetDict(
			    {
			        '''train''': train_test['''train'''],
			        '''test''': test_validation['''train'''],
			        '''valid''': test_validation['''test'''],
			    } )

			print('''Loading tokenizer and model''' )
			UpperCAmelCase   		=				AutoTokenizer.from_pretrained(args.model_ckpt )
			UpperCAmelCase   		=				tokenizer.eos_token
			UpperCAmelCase   		=				AutoModelForSequenceClassification.from_pretrained(args.model_ckpt			,			num_labels=7 )
			UpperCAmelCase   		=				model.config.eos_token_id

			if args.freeze:
						for param in model.roberta.parameters():
									UpperCAmelCase   		=				False

			UpperCAmelCase   		=				ClassLabel(num_classes=7			,			names=list(set(train_test_validation['''train''']['''complexity'''] ) ) )

			def tokenize(UpperCamelCase__ ):
						UpperCAmelCase   		=				tokenizer(example['''src''']			,			truncation=UpperCamelCase__			,			max_length=1024 )
						UpperCAmelCase   		=				labels.straint(example['''complexity'''] )
						return {
						    "input_ids": inputs["input_ids"],
						    "attention_mask": inputs["attention_mask"],
						    "label": label,
						}

			UpperCAmelCase   		=				train_test_validation.map(
			    UpperCamelCase__			,			batched=UpperCamelCase__			,			remove_columns=train_test_validation['''train'''].column_names			,			)
			UpperCAmelCase   		=				DataCollatorWithPadding(tokenizer=UpperCamelCase__ )

			UpperCAmelCase   		=				TrainingArguments(
			    output_dir=args.output_dir			,			learning_rate=args.learning_rate			,			lr_scheduler_type=args.lr_scheduler_type			,			evaluation_strategy='''epoch'''			,			save_strategy='''epoch'''			,			logging_strategy='''epoch'''			,			per_device_train_batch_size=args.batch_size			,			per_device_eval_batch_size=args.batch_size			,			num_train_epochs=args.num_epochs			,			gradient_accumulation_steps=args.gradient_accumulation_steps			,			weight_decay=0.01			,			metric_for_best_model='''accuracy'''			,			run_name='''complexity-java'''			,			report_to='''wandb'''			,			)

			UpperCAmelCase   		=				Trainer(
			    model=UpperCamelCase__			,			args=UpperCamelCase__			,			train_dataset=tokenized_datasets['''train''']			,			eval_dataset=tokenized_datasets['''valid''']			,			tokenizer=UpperCamelCase__			,			data_collator=UpperCamelCase__			,			compute_metrics=UpperCamelCase__			,			)

			print('''Training...''' )
			trainer.add_callback(CustomCallback(UpperCamelCase__ ) )
			trainer.train()


if __name__ == "__main__":
				main()










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

import pytest
from parameterized import 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.py''',
            '''model_name_or_path''': '''distilbert-base-cased''',
            '''instance_type''': '''ml.g4dn.xlarge''',
            '''results''': {'''train_runtime''': 6_5_0, '''eval_accuracy''': 0.6, '''eval_loss''': 0.9},
        },
        {
            '''framework''': '''tensorflow''',
            '''script''': '''run_tf.py''',
            '''model_name_or_path''': '''distilbert-base-cased''',
            '''instance_type''': '''ml.g4dn.xlarge''',
            '''results''': {'''train_runtime''': 6_0_0, '''eval_accuracy''': 0.3, '''eval_loss''': 0.9},
        },
    ]			)
class 	A_       (unittest.TestCase			):
	def      _lowercase				(    self		):






				'''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=_A							,					)
				assert hasattr(self							,					'''env'''		)
	def      _lowercase				(    self							,					_A=1		):






				'''simple docstring'''







				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}-single"""							,					instance_count=_A							,					instance_type=self.instance_type							,					debugger_hook_config=_A							,					hyperparameters={**self.env.hyperparameters, '''model_name_or_path''': self.model_name_or_path}							,					metric_definitions=self.env.metric_definitions							,					py_version='''py36'''							,					)
	def      _lowercase				(    self							,					_A		):






				'''simple docstring'''







				TrainingJobAnalytics(_A		).export_csv(F"""{self.env.test_path}/{job_name}_metrics.csv"""		)







	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.create_estimator()

				# run training
				estimator.fit()

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

				# extract kpis
				UpperCAmelCase   		=				list(result_metrics_df[result_metrics_df.metric_name == '''eval_accuracy''']['''value''']		)
				UpperCAmelCase   		=				list(result_metrics_df[result_metrics_df.metric_name == '''eval_loss''']['''value''']		)
				# get train time from SageMaker job, this includes starting, preprocessing, stopping
				UpperCAmelCase   		=				(
				    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}							,					_A		)



