

"""simple docstring"""



import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


SCREAMING_SNAKE_CASE_					:	int							  =						logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_					:	Dict							  =						'▁'

SCREAMING_SNAKE_CASE_					:	Tuple							  =						{'vocab_file': 'sentencepiece.bpe.model', 'monolingual_vocab_file': 'dict.txt'}

SCREAMING_SNAKE_CASE_					:	Union[str, Any]							  =						{
    'vocab_file': {
        'vinai/bartpho-syllable': 'https://huggingface.co/vinai/bartpho-syllable/resolve/main/sentencepiece.bpe.model',
    },
    'monolingual_vocab_file': {
        'vinai/bartpho-syllable': 'https://huggingface.co/vinai/bartpho-syllable/resolve/main/dict.txt',
    },
}

SCREAMING_SNAKE_CASE_					:	List[Any]							  =						{'vinai/bartpho-syllable': 1_0_2_4}



class 					a						(       _lowerCamelCase				):



    """simple docstring"""



    UpperCAmelCase						 =					VOCAB_FILES_NAMES
    UpperCAmelCase						 =					PRETRAINED_VOCAB_FILES_MAP
    UpperCAmelCase						 =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    UpperCAmelCase						 =					["input_ids", "attention_mask"]

    def __init__(     self: List[str]    ,						UpperCamelCase: Optional[Any]    ,						UpperCamelCase: List[str]    ,						UpperCamelCase: Tuple="<s>"    ,						UpperCamelCase: Any="</s>"    ,						UpperCamelCase: List[str]="</s>"    ,						UpperCamelCase: Tuple="<s>"    ,						UpperCamelCase: Tuple="<unk>"    ,						UpperCamelCase: Optional[int]="<pad>"    ,						UpperCamelCase: Optional[int]="<mask>"    ,						UpperCamelCase: Optional[Dict[str, Any]] = None    ,						**UpperCamelCase: Optional[Any]    ,						):






         """simple docstring"""


         A__										=  AddedToken(UpperCamelCase    ,						lstrip=UpperCamelCase    ,						rstrip=UpperCamelCase       ) if isinstance(UpperCamelCase    ,						UpperCamelCase       ) else mask_token

         A__										=  {} if sp_model_kwargs is None else sp_model_kwargs

         super().__init__(
             bos_token=UpperCamelCase    ,						eos_token=UpperCamelCase    ,						unk_token=UpperCamelCase    ,						sep_token=UpperCamelCase    ,						cls_token=UpperCamelCase    ,						pad_token=UpperCamelCase    ,						mask_token=UpperCamelCase    ,						sp_model_kwargs=self.sp_model_kwargs    ,						**UpperCamelCase    ,						)

         A__										=  vocab_file
         A__										=  monolingual_vocab_file
         A__										=  spm.SentencePieceProcessor(**self.sp_model_kwargs       )
         self.sp_model.Load(str(UpperCamelCase       )       )

         # Load the reduced vocab

         # Keep order of special tokens for backward compatibility
         A__										=  {}
         A__										=  0
         for token in [bos_token, pad_token, eos_token, unk_token, sep_token, cls_token]:
              if str(UpperCamelCase       ) not in self.fairseq_tokens_to_ids:
                   A__										=  cnt
                   cnt += 1
         with open(UpperCamelCase    ,						"""r"""    ,						encoding="""utf-8"""       ) as f:
              for line in f.readlines():
                   A__										=  line.strip().split()[0]
                   A__										=  len(self.fairseq_tokens_to_ids       )
         if str(UpperCamelCase       ) not in self.fairseq_tokens_to_ids:
              A__										=  len(self.fairseq_tokens_to_ids       )

         A__										=  {v: k for k, v in self.fairseq_tokens_to_ids.items()}

    def __getstate__(     self: List[str]       ):






         """simple docstring"""


         A__										=  self.__dict__.copy()
         A__										=  None
         A__										=  self.sp_model.serialized_model_proto()
         return state

    def __setstate__(     self: Optional[Any]    ,						UpperCamelCase: Union[str, Any]       ):






         """simple docstring"""


         A__										=  d

         # for backward compatibility
         if not hasattr(self    ,						"""sp_model_kwargs"""       ):
              A__										=  {}

         A__										=  spm.SentencePieceProcessor(**self.sp_model_kwargs       )
         self.sp_model.LoadFromSerializedProto(self.sp_model_proto       )

    def 				UpperCamelCase	(     self: Optional[Any]    ,						UpperCamelCase: List[int]    ,						UpperCamelCase: Optional[List[int]] = None       ):






         """simple docstring"""


         if token_ids_a is None:
              return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
         A__										=  [self.cls_token_id]
         A__										=  [self.sep_token_id]
         return cls + token_ids_a + sep + sep + token_ids_a + sep

    def 				UpperCamelCase	(     self: Any    ,						UpperCamelCase: List[int]    ,						UpperCamelCase: Optional[List[int]] = None    ,						UpperCamelCase: bool = False       ):






         """simple docstring"""


         if already_has_special_tokens:
              return super().get_special_tokens_mask(
                  token_ids_a=UpperCamelCase    ,						token_ids_a=UpperCamelCase    ,						already_has_special_tokens=UpperCamelCase       )

         if token_ids_a is None:
              return [1] + ([0] * len(UpperCamelCase       )) + [1]
         return [1] + ([0] * len(UpperCamelCase       )) + [1, 1] + ([0] * len(UpperCamelCase       )) + [1]

    def 				UpperCamelCase	(     self: Dict    ,						UpperCamelCase: List[int]    ,						UpperCamelCase: Optional[List[int]] = None       ):






         """simple docstring"""


         A__										=  [self.sep_token_id]
         A__										=  [self.cls_token_id]

         if token_ids_a is None:
              return len(cls + token_ids_a + sep       ) * [0]
         return len(cls + token_ids_a + sep + sep + token_ids_a + sep       ) * [0]

    @property
    def 				UpperCamelCase	(     self: Dict       ):






         """simple docstring"""


         return len(self.fairseq_ids_to_tokens       )

    def 				UpperCamelCase	(     self: Dict       ):






         """simple docstring"""


         A__										=  {self.convert_ids_to_tokens(UpperCamelCase       ): i for i in range(self.vocab_size       )}
         vocab.update(self.added_tokens_encoder       )
         return vocab

    def 				UpperCamelCase	(     self: Optional[Any]    ,						UpperCamelCase: str       ):






         """simple docstring"""


         return self.sp_model.encode(UpperCamelCase    ,						out_type=UpperCamelCase       )

    def 				UpperCamelCase	(     self: Union[str, Any]    ,						UpperCamelCase: Any       ):






         """simple docstring"""


         if token in self.fairseq_tokens_to_ids:
              return self.fairseq_tokens_to_ids[token]
         else:
              return self.unk_token_id

    def 				UpperCamelCase	(     self: Any    ,						UpperCamelCase: int       ):






         """simple docstring"""


         return self.fairseq_ids_to_tokens[index]

    def 				UpperCamelCase	(     self: List[Any]    ,						UpperCamelCase: Tuple       ):






         """simple docstring"""


         A__										=  """""".join(UpperCamelCase       ).replace(UpperCamelCase    ,						""" """       ).strip()
         return out_string





    def 				UpperCamelCase	(     self: Optional[int]    ,						UpperCamelCase: str    ,						UpperCamelCase: Optional[str] = None       ):






         """simple docstring"""


         if not os.path.isdir(UpperCamelCase       ):
              logger.error(f"""Vocabulary path ({save_directory}) should be a directory"""       )
              return
         A__										=  os.path.join(
             UpperCamelCase    ,						(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]       )
         A__										=  os.path.join(
             UpperCamelCase    ,						(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""monolingual_vocab_file"""]    ,						)

         if os.path.abspath(self.vocab_file       ) != os.path.abspath(UpperCamelCase       ) and os.path.isfile(self.vocab_file       ):
              copyfile(self.vocab_file    ,						UpperCamelCase       )
         elif not os.path.isfile(self.vocab_file       ):
              with open(UpperCamelCase    ,						"""wb"""       ) as fi:
                   A__										=  self.sp_model.serialized_model_proto()
                   fi.write(UpperCamelCase       )

         if os.path.abspath(self.monolingual_vocab_file       ) != os.path.abspath(
             UpperCamelCase       ) and os.path.isfile(self.monolingual_vocab_file       ):
              copyfile(self.monolingual_vocab_file    ,						UpperCamelCase       )
         elif not os.path.isfile(self.monolingual_vocab_file       ):
              with open(UpperCamelCase    ,						"""w"""    ,						encoding="""utf-8"""       ) as fp:
                   for token in self.fairseq_tokens_to_ids:
                        if token not in self.all_special_tokens:
                             fp.write(f"""{str(UpperCamelCase       )} \n"""       )

         return out_vocab_file, out_monolingual_vocab_file









"""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  _snake_case     (    ):
     A__										=  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_					:	Union[str, Any]							  =						load('accuracy')





def  _snake_case     (    UpperCAmelCase_  :					Tuple			):
     A__			,				A__										=  eval_pred
     A__										=  np.argmax(UpperCAmelCase_					,   axis=1			)
     return metric.compute(predictions=UpperCAmelCase_					,   references=UpperCAmelCase_			)



class 					a						(       _lowerCamelCase				):



    """simple docstring"""



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






         """simple docstring"""


         super().__init__()
         A__										=  trainer





    def 				UpperCamelCase	(     self: int    ,						UpperCamelCase: Tuple    ,						UpperCamelCase: Optional[int]    ,						UpperCamelCase: Dict    ,						**UpperCamelCase: int       ):






         """simple docstring"""


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





def  _snake_case     (    ):
     A__										=  get_args()
     set_seed(args.seed			)

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

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

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

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

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

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

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

     A__										=  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()






