




"""simple docstring"""






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


UpperCamelCase__						=			load('''accuracy''')





def 							UpperCAmelCase		(		snake_case			:					Tuple    ):
					_lowerCAmelCase							,   _lowerCAmelCase:Tuple									=  eval_pred
					_lowerCAmelCase:Optional[int]									=  np.argmax(snake_case      ,       axis=1    )
					return metric.compute(predictions=snake_case      ,       references=snake_case    )




class   a__			(       UpperCamelCase_  ):





						def __init__(							self      :	str     ,a__      :	Optional[Any])						->       None:




											"""simple docstring"""







											super().__init__()
											_lowerCAmelCase:Optional[int]									=  trainer







						def       __UpperCamelCase     (							self      :	int     ,a__      :	Optional[int]     ,a__      :	Dict     ,a__      :	Any     ,**a__      :	Union[str, Any])						->       Union[str, Any]:




											"""simple docstring"""







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





def 							UpperCAmelCase		(		):
					_lowerCAmelCase:Dict									=  get_args()
					set_seed(args.seed    )

					_lowerCAmelCase:Dict									=  load_dataset('''codeparrot/codecomplex'''      ,       split='''train'''    )
					_lowerCAmelCase:str									=  dataset.train_test_split(test_size=0.2    )
					_lowerCAmelCase:List[Any]									=  train_test['''test'''].train_test_split(test_size=0.5    )
					_lowerCAmelCase:Optional[int]									=  DatasetDict(
					    {
					        '''train''': train_test['''train'''],
					        '''test''': test_validation['''train'''],
					        '''valid''': test_validation['''test'''],
					    }    )

					print('''Loading tokenizer and model'''    )
					_lowerCAmelCase:Tuple									=  AutoTokenizer.from_pretrained(args.model_ckpt    )
					_lowerCAmelCase:List[Any]									=  tokenizer.eos_token
					_lowerCAmelCase:Tuple									=  AutoModelForSequenceClassification.from_pretrained(args.model_ckpt      ,       num_labels=7    )
					_lowerCAmelCase:List[Any]									=  model.config.eos_token_id

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

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

					def tokenize(snake_case			:					Union[str, Any]    ):
										_lowerCAmelCase:Dict									=  tokenizer(example['''src''']      ,       truncation=snake_case      ,       max_length=1024    )
										_lowerCAmelCase:Optional[Any]									=  labels.straint(example['''complexity''']    )
										return {
										    "input_ids": inputs["input_ids"],
										    "attention_mask": inputs["attention_mask"],
										    "label": label,
										}

					_lowerCAmelCase:Tuple									=  train_test_validation.map(
					    snake_case      ,       batched=snake_case      ,       remove_columns=train_test_validation['''train'''].column_names      ,       )
					_lowerCAmelCase:List[Any]									=  DataCollatorWithPadding(tokenizer=snake_case    )

					_lowerCAmelCase:Dict									=  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'''      ,       )

					_lowerCAmelCase:Optional[Any]									=  Trainer(
					    model=snake_case      ,       args=snake_case      ,       train_dataset=tokenized_datasets['''train''']      ,       eval_dataset=tokenized_datasets['''valid''']      ,       tokenizer=snake_case      ,       data_collator=snake_case      ,       compute_metrics=snake_case      ,       )

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


if __name__ == "__main__":
				main()












"""simple docstring"""






import importlib
import sys
from argparse import REMAINDER, ArgumentParser
from pathlib import Path

import torch_xla.distributed.xla_multiprocessing as xmp





def 							UpperCAmelCase		(		):
					_lowerCAmelCase:Optional[int]									=  ArgumentParser(
					    description=(
					        '''PyTorch TPU distributed training launch helper utility that will spawn up multiple distributed processes'''
					    )    )

					# Optional arguments for the launch helper
					parser.add_argument('''--num_cores'''      ,       type=snake_case      ,       default=1      ,       help='''Number of TPU cores to use (1 or 8).'''    )

					# positional
					parser.add_argument(
					    '''training_script'''      ,       type=snake_case      ,       help=(
					        '''The full path to the single TPU training '''
					        '''program/script to be launched in parallel, '''
					        '''followed by all the arguments for the '''
					        '''training script'''
					    )      ,       )

					# rest from the training program
					parser.add_argument('''training_script_args'''      ,       nargs=snake_case    )

					return parser.parse_args()





def 							UpperCAmelCase		(		):
					_lowerCAmelCase:int									=  parse_args()

					# Import training_script as a module.
					_lowerCAmelCase:Optional[int]									=  Path(args.training_script    )
					sys.path.append(str(script_fpath.parent.resolve()    )    )
					_lowerCAmelCase:Optional[int]									=  script_fpath.stem
					_lowerCAmelCase:Any									=  importlib.import_module(snake_case    )

					# Patch sys.argv
					_lowerCAmelCase:Dict									=  [args.training_script] + args.training_script_args + ['''--tpu_num_cores''', str(args.num_cores    )]

					xmp.spawn(mod._mp_fn      ,       args=()      ,       nprocs=args.num_cores    )


if __name__ == "__main__":
				main()






