






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 _UpperCAmelCase   (	):
							snake_case__	  =       argparse.ArgumentParser()
							parser.add_argument("""--model_ckpt"""  ,					type=a  ,					default="""microsoft/unixcoder-base-nine""" )
							parser.add_argument("""--num_epochs"""  ,					type=a  ,					default=5 )
							parser.add_argument("""--batch_size"""  ,					type=a  ,					default=6 )
							parser.add_argument("""--gradient_accumulation_steps"""  ,					type=a  ,					default=1 )
							parser.add_argument("""--freeze"""  ,					type=a  ,					default=a )
							parser.add_argument("""--learning_rate"""  ,					type=a  ,					default=5e-4 )
							parser.add_argument("""--seed"""  ,					type=a  ,					default=0 )
							parser.add_argument("""--lr_scheduler_type"""  ,					type=a  ,					default="""cosine""" )
							parser.add_argument("""--num_warmup_steps"""  ,					type=a  ,					default=10 )
							parser.add_argument("""--weight_decay"""  ,					type=a  ,					default=0.01 )
							parser.add_argument("""--output_dir"""  ,					type=a  ,					default="""./results""" )
							return parser.parse_args()


a__       =     load("""accuracy""")

def _UpperCAmelCase   (	a						:	Optional[Any] ):
							snake_case__  , snake_case__	  =       eval_pred
							snake_case__	  =       np.argmax(a  ,					axis=1 )
							return metric.compute(predictions=a  ,					references=a )


class    _lowerCAmelCase			(		lowercase_      ):



				"""simple docstring"""



				def __init__(      self    :    Optional[int]			,		UpperCamelCase__    :    Optional[Any]):






											'''simple docstring'''







											super().__init__()
											snake_case__	  =       trainer



				def 					__magic_name__							(      self    :    Optional[int]			,		UpperCamelCase__    :    Optional[int]			,		UpperCamelCase__    :    Optional[Any]			,		UpperCamelCase__    :    Tuple			,		**UpperCamelCase__    :    List[Any]):






											'''simple docstring'''







											if control.should_evaluate:
																		snake_case__	  =       deepcopy(UpperCamelCase__)
																		self._trainer.evaluate(eval_dataset=self._trainer.train_dataset			,		metric_key_prefix="""train""")
																		return control_copy


def _UpperCAmelCase   (	):
							snake_case__	  =       get_args()
							set_seed(args.seed )

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

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

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

							snake_case__	  =       ClassLabel(num_classes=7  ,					names=list(set(train_test_validation["""train"""]["""complexity"""] ) ) )

							def tokenize(a						:	Optional[Any] ):
														snake_case__	  =       tokenizer(example["""src"""]  ,					truncation=a  ,					max_length=1024 )
														snake_case__	  =       labels.straint(example["""complexity"""] )
														return {
														    "input_ids": inputs["input_ids"],
														    "attention_mask": inputs["attention_mask"],
														    "label": label,
														}

							snake_case__	  =       train_test_validation.map(
							    a  ,					batched=a  ,					remove_columns=train_test_validation["""train"""].column_names  ,					)
							snake_case__	  =       DataCollatorWithPadding(tokenizer=a )

							snake_case__	  =       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"""  ,					)

							snake_case__	  =       Trainer(
							    model=a  ,					args=a  ,					train_dataset=tokenized_datasets["""train"""]  ,					eval_dataset=tokenized_datasets["""valid"""]  ,					tokenizer=a  ,					data_collator=a  ,					compute_metrics=a  ,					)

							print("""Training...""" )
							trainer.add_callback(CustomCallback(a ) )
							trainer.train()


if __name__ == "__main__":
	main()








import argparse
import logging
import sys
from unittest.mock import patch

import run_glue_deebert

from transformers.testing_utils import TestCasePlus, get_gpu_count, require_torch_non_multi_gpu, slow


logging.basicConfig(level=logging.DEBUG)

a__       =     logging.getLogger()

def _UpperCAmelCase   (	):
							snake_case__	  =       argparse.ArgumentParser()
							parser.add_argument("""-f""" )
							snake_case__	  =       parser.parse_args()
							return args.f



class    _lowerCAmelCase			(		lowercase_      ):



				"""simple docstring"""



				def 					__magic_name__							(      self    :    int):






											'''simple docstring'''







											snake_case__	  =       logging.StreamHandler(sys.stdout)
											logger.addHandler(UpperCamelCase__)



				def 					__magic_name__							(      self    :    Union[str, Any]			,		UpperCamelCase__    :    List[str]):






											'''simple docstring'''







											snake_case__	  =       get_gpu_count()

											if n_gpu > 1:
																		pass
																		# XXX: doesn't quite work with n_gpu > 1 https://github.com/huggingface/transformers/issues/10560
																		# script = f"{self.examples_dir_str}/research_projects/deebert/run_glue_deebert.py"
																		# distributed_args = f"-m torch.distributed.launch --nproc_per_node={n_gpu} {script}".split()
																		# cmd = [sys.executable] + distributed_args + args
																		# execute_subprocess_async(cmd, env=self.get_env())
																		# XXX: test the results - need to save them first into .json file
											else:
																		args.insert(0			,		"""run_glue_deebert.py""")
																		with patch.object(UpperCamelCase__			,		"""argv"""			,		UpperCamelCase__):
																									snake_case__	  =       run_glue_deebert.main()
																									for value in result.values():
																																self.assertGreaterEqual(UpperCamelCase__			,		0.6_66)



				@slow
				@require_torch_non_multi_gpu
				def 					__magic_name__							(      self    :    str):






											'''simple docstring'''







											snake_case__	  =       """
            --model_type roberta
            --model_name_or_path roberta-base
            --task_name MRPC
            --do_train
            --do_eval
            --do_lower_case
            --data_dir ./tests/fixtures/tests_samples/MRPC/
            --max_seq_length 128
            --per_gpu_eval_batch_size=1
            --per_gpu_train_batch_size=8
            --learning_rate 2e-4
            --num_train_epochs 3
            --overwrite_output_dir
            --seed 42
            --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage
            --plot_data_dir ./examples/deebert/results/
            --save_steps 0
            --overwrite_cache
            --eval_after_first_stage
            """.split()
											self.run_and_check(UpperCamelCase__)

											snake_case__	  =       """
            --model_type roberta
            --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage
            --task_name MRPC
            --do_eval
            --do_lower_case
            --data_dir ./tests/fixtures/tests_samples/MRPC/
            --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage
            --plot_data_dir ./examples/deebert/results/
            --max_seq_length 128
            --eval_each_highway
            --eval_highway
            --overwrite_cache
            --per_gpu_eval_batch_size=1
            """.split()
											self.run_and_check(UpperCamelCase__)

											snake_case__	  =       """
            --model_type roberta
            --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage
            --task_name MRPC
            --do_eval
            --do_lower_case
            --data_dir ./tests/fixtures/tests_samples/MRPC/
            --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage
            --plot_data_dir ./examples/deebert/results/
            --max_seq_length 128
            --early_exit_entropy 0.1
            --eval_highway
            --overwrite_cache
            --per_gpu_eval_batch_size=1
            """.split()
											self.run_and_check(UpperCamelCase__)
