
"""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 						lowerCamelCase__      (				)->				List[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()


SCREAMING_SNAKE_CASE			  =  load("""accuracy""")



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







       """simple docstring"""
       UpperCamelCase			,							UpperCamelCase       			=		eval_pred
       UpperCamelCase       			=		np.argmax(_UpperCamelCase		,   axis=1					)
       return metric.compute(predictions=_UpperCamelCase		,   references=_UpperCamelCase					)



class 	__a	(   _lowerCAmelCase						):






       def __init__(	self			:				Any							,   UpperCAmelCase_			:				Optional[Any]      )->	Optional[Any]:







              """simple docstring"""
              super().__init__()
              UpperCamelCase       			=		trainer






       def 						_SCREAMING_SNAKE_CASE							(	self			:				int							,   UpperCAmelCase_			:				int							,   UpperCAmelCase_			:				List[str]							,   UpperCAmelCase_			:				Tuple							,   **UpperCAmelCase_			:				Union[str, Any]      )->	str:







              """simple docstring"""






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





def 						lowerCamelCase__      (				)->				List[str]:







       """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=10_24					)
              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()








# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse

from ...utils.dataclasses import (
    ComputeEnvironment,
    DistributedType,
    DynamoBackend,
    PrecisionType,
    SageMakerDistributedType,
)
from ..menu import BulletMenu


a_     :    str     		=   [
    "EAGER",
    "AOT_EAGER",
    "INDUCTOR",
    "NVFUSER",
    "AOT_NVFUSER",
    "AOT_CUDAGRAPHS",
    "OFI",
    "FX2TRT",
    "ONNXRT",
    "IPEX",
]


def 							__lowerCAmelCase     (							_UpperCamelCase		:	int		,				_UpperCamelCase		:	List[Any]=None		,				_UpperCamelCase		:	Dict=None		,				_UpperCamelCase		:	str=None						)		->				Any:
 '''simple docstring'''


 SCREAMING_SNAKE_CASE		    =      True
 while ask_again:
  SCREAMING_SNAKE_CASE		    =      input(_UpperCamelCase						)
  try:
   if default is not None and len(_UpperCamelCase						) == 0:
    return default
   return convert_value(_UpperCamelCase						) if convert_value is not None else result
  except Exception:
   if error_message is not None:
    print(_UpperCamelCase						)


def 							__lowerCAmelCase     (							_UpperCamelCase		:	Optional[Any]		,				_UpperCamelCase		:	Union[str, Any]=[]		,				_UpperCamelCase		:	Optional[int]=None		,				_UpperCamelCase		:	Optional[Any]=0						)		->				List[Any]:
 '''simple docstring'''


 SCREAMING_SNAKE_CASE		    =      BulletMenu(_UpperCamelCase		,				_UpperCamelCase						)
 SCREAMING_SNAKE_CASE		    =      menu.run(default_choice=_UpperCamelCase						)
 return convert_value(_UpperCamelCase						) if convert_value is not None else result


def 							__lowerCAmelCase     (							_UpperCamelCase		:	List[str]						)		->				int:
 '''simple docstring'''


 SCREAMING_SNAKE_CASE		    =      int(_UpperCamelCase						)
 return ComputeEnvironment(['LOCAL_MACHINE', 'AMAZON_SAGEMAKER'][value]						)


def 							__lowerCAmelCase     (							_UpperCamelCase		:	Tuple						)		->				Tuple:
 '''simple docstring'''


 SCREAMING_SNAKE_CASE		    =      int(_UpperCamelCase						)
 return DistributedType(['NO', 'MULTI_CPU', 'MULTI_XPU', 'MULTI_GPU', 'MULTI_NPU', 'TPU'][value]						)


def 							__lowerCAmelCase     (							_UpperCamelCase		:	List[Any]						)		->				List[str]:
 '''simple docstring'''


 SCREAMING_SNAKE_CASE		    =      int(_UpperCamelCase						)
 return DynamoBackend(DYNAMO_BACKENDS[value]						).value


def 							__lowerCAmelCase     (							_UpperCamelCase		:	Union[str, Any]						)		->				Dict:
 '''simple docstring'''


 SCREAMING_SNAKE_CASE		    =      int(_UpperCamelCase						)
 return PrecisionType(['no', 'fp16', 'bf16', 'fp8'][value]						)


def 							__lowerCAmelCase     (							_UpperCamelCase		:	Tuple						)		->				str:
 '''simple docstring'''


 SCREAMING_SNAKE_CASE		    =      int(_UpperCamelCase						)
 return SageMakerDistributedType(['NO', 'DATA_PARALLEL', 'MODEL_PARALLEL'][value]						)


def 							__lowerCAmelCase     (							_UpperCamelCase		:	Dict						)		->				str:
 '''simple docstring'''


 return {"yes": True, "no": False}[value.lower()]




class   UpperCamelCase			(  argparse.RawDescriptionHelpFormatter ):

    def UpperCamelCase      (      self			:   Union[str, Any]				,     snake_case__			:   Tuple				,     snake_case__			:   Optional[Any]				,     snake_case__			:   List[str]				,     snake_case__			:   Dict     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      super()._format_usage(snake_case__				,     snake_case__				,     snake_case__				,     snake_case__     )
     SCREAMING_SNAKE_CASE		    =      usage.replace('<command> [<args>] '				,     ''     )
     return usage

