




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

from ...tokenization_utils_fast import AddedToken, PreTrainedTokenizerFast
from ...utils import logging


SCREAMING_SNAKE_CASE__ :   Optional[Any]						=     logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__ :   Dict						=     {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'}

SCREAMING_SNAKE_CASE__ :   Optional[Any]						=     {
    'vocab_file': {
        'TsinghuaAI/CPM-Generate': 'https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model',
    },
    'tokenizer_file': {
        'TsinghuaAI/CPM-Generate': 'https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/tokenizer.json',
    },
}




class 	UpperCamelCase__			(lowerCAmelCase__  ):

    '''simple docstring'''



    def __init__(					self  ,						UpperCamelCase__=None  ,						UpperCamelCase__=None  ,						UpperCamelCase__=False  ,						UpperCamelCase__=True  ,						UpperCamelCase__=False  ,						UpperCamelCase__="<s>"  ,						UpperCamelCase__="</s>"  ,						UpperCamelCase__="<unk>"  ,						UpperCamelCase__="<sep>"  ,						UpperCamelCase__="<pad>"  ,						UpperCamelCase__="<cls>"  ,						UpperCamelCase__="<mask>"  ,						UpperCamelCase__=["<eop>", "<eod>"]  ,						**UpperCamelCase__  ,						)  ->   Tuple:
           lowerCamelCase     :   Union[str, Any]    				=      AddedToken(UpperCamelCase__  ,						lstrip=UpperCamelCase__  ,						rstrip=UpperCamelCase__  ) if isinstance(UpperCamelCase__  ,						UpperCamelCase__  ) else mask_token

           super().__init__(
               vocab_file=UpperCamelCase__  ,						tokenizer_file=UpperCamelCase__  ,						do_lower_case=UpperCamelCase__  ,						remove_space=UpperCamelCase__  ,						keep_accents=UpperCamelCase__  ,						bos_token=UpperCamelCase__  ,						eos_token=UpperCamelCase__  ,						unk_token=UpperCamelCase__  ,						sep_token=UpperCamelCase__  ,						pad_token=UpperCamelCase__  ,						cls_token=UpperCamelCase__  ,						mask_token=UpperCamelCase__  ,						additional_special_tokens=UpperCamelCase__  ,						**UpperCamelCase__  ,						)

           lowerCamelCase     :   Dict    				=      3
           lowerCamelCase     :   Optional[int]    				=      do_lower_case
           lowerCamelCase     :   Union[str, Any]    				=      remove_space
           lowerCamelCase     :   Union[str, Any]    				=      keep_accents
           lowerCamelCase     :   Optional[int]    				=      vocab_file
           lowerCamelCase     :   Dict    				=      False if not self.vocab_file else True

           try:
                  import jieba
           except ModuleNotFoundError as error:
                  raise error.__class__(
                      "You need to install jieba to use CpmTokenizer or CpmTokenizerFast. "
                      "See https://pypi.org/project/jieba/ for installation."  )
           lowerCamelCase     :   List[Any]    				=      jieba
           lowerCamelCase     :   Union[str, Any]    				=      str.maketrans(" \n"  ,						"\u2582\u2583"  )







    def 					_lowercase  (					self  ,						UpperCamelCase__  ,						UpperCamelCase__ = None  )  ->   List[int]:
           lowerCamelCase     :   str    				=      [self.sep_token_id]
           lowerCamelCase     :   Dict    				=      [self.cls_token_id]
           if token_ids_a is None:
                  return token_ids_a + sep + cls
           return token_ids_a + sep + token_ids_a + sep + cls







    def 					_lowercase  (					self  ,						UpperCamelCase__  ,						UpperCamelCase__ = None  )  ->   List[int]:
           lowerCamelCase     :   Any    				=      [self.sep_token_id]
           lowerCamelCase     :   str    				=      [2]

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







    def 					_lowercase  (					self  ,						UpperCamelCase__  ,						UpperCamelCase__ = None  )  ->   Tuple[str]:
           if not self.can_save_slow_tokenizer:
                  raise ValueError(
                      "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow "
                      "tokenizer."  )

           if not os.path.isdir(UpperCamelCase__  ):
                  logger.error(F'''Vocabulary path ({save_directory}) should be a directory'''  )
                  return
           lowerCamelCase     :   Optional[Any]    				=      os.path.join(
               UpperCamelCase__  ,						(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]  )

           if os.path.abspath(self.vocab_file  ) != os.path.abspath(UpperCamelCase__  ):
                  copyfile(self.vocab_file  ,						UpperCamelCase__  )

           return (out_vocab_file,)







    def 					_lowercase  (					self  ,						UpperCamelCase__  ,						*UpperCamelCase__  ,						**UpperCamelCase__  )  ->   List[Any]:
           lowerCamelCase     :   Optional[Any]    				=      [
               " ".join([x.translate(self.translator  ) for x in self.jieba.cut(UpperCamelCase__  ,						cut_all=UpperCamelCase__  )]  )
               for text in batch_text_or_text_pairs
           ]
           return super()._batch_encode_plus(UpperCamelCase__  ,						*UpperCamelCase__  ,						**UpperCamelCase__  )







    def 					_lowercase  (					self  ,						*UpperCamelCase__  ,						**UpperCamelCase__  )  ->   Optional[int]:
           lowerCamelCase     :   int    				=      super()._decode(*UpperCamelCase__  ,						**UpperCamelCase__  )
           lowerCamelCase     :   List[str]    				=      text.replace(" "  ,						""  ).replace("\u2582"  ,						" "  ).replace("\u2583"  ,						"\n"  )
           return text












import timeit
from typing import Callable, Optional

from ..configuration_utils import PretrainedConfig
from ..models.auto.modeling_auto import MODEL_MAPPING, MODEL_WITH_LM_HEAD_MAPPING
from ..utils import is_pyanvml_available, is_torch_available, logging
from .benchmark_utils import (
    Benchmark,
    Memory,
    MemorySummary,
    measure_peak_memory_cpu,
    start_memory_tracing,
    stop_memory_tracing,
)


if is_torch_available():
       import torch

       from .benchmark_args import PyTorchBenchmarkArguments


if is_pyanvml_available():
       import pyanvml.pyanvml as nvml


SCREAMING_SNAKE_CASE__ :   List[Any]						=     logging.get_logger(__name__)




class 	UpperCamelCase__			(lowerCAmelCase__  ):

    '''simple docstring'''



    lowerCamelCase_   :			PyTorchBenchmarkArguments
    lowerCamelCase_   :			PretrainedConfig
    lowerCamelCase_   :			str			  =   "PyTorch"







    @property
    def 					_lowercase  (					self  )  ->   Optional[int]:
           return torch.__version__







    def 					_lowercase  (					self  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  )  ->   float:
           lowerCamelCase     :   List[Any]    				=      self._prepare_inference_func(UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  )
           return self._measure_speed(_inference  )







    def 					_lowercase  (					self  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  )  ->   [Memory, Optional[MemorySummary]]:
           lowerCamelCase     :   List[Any]    				=      self._prepare_inference_func(UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  )
           return self._measure_memory(_inference  )







    def 					_lowercase  (					self  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  )  ->   float:
           lowerCamelCase     :   Dict    				=      self._prepare_train_func(UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  )
           return self._measure_speed(_train  )







    def 					_lowercase  (					self  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  )  ->   [Memory, Optional[MemorySummary]]:
           lowerCamelCase     :   Optional[Any]    				=      self._prepare_train_func(UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  )
           return self._measure_memory(_train  )







    def 					_lowercase  (					self  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  )  ->   Callable[[], None]:
           lowerCamelCase     :   Optional[Any]    				=      self.config_dict[model_name]

           if self.args.torchscript:
                  lowerCamelCase     :   Optional[Any]    				=      True

           lowerCamelCase     :   Dict    				=      (
               hasattr(UpperCamelCase__  ,						"architectures"  )
               and isinstance(config.architectures  ,						UpperCamelCase__  )
               and len(config.architectures  ) > 0
           )
           if not self.args.only_pretrain_model and has_model_class_in_config:
                  try:
                         lowerCamelCase     :   Optional[Any]    				=      config.architectures[0]
                         lowerCamelCase     :   Optional[int]    				=      __import__("transformers"  ,						fromlist=[model_class]  )
                         lowerCamelCase     :   Optional[int]    				=      getattr(UpperCamelCase__  ,						UpperCamelCase__  )
                         lowerCamelCase     :   List[str]    				=      model_cls(UpperCamelCase__  )
                  except ImportError:
                         raise ImportError(
                             F'''{model_class} does not exist. If you just want to test the pretrained model, you might want to'''
                             " set `--only_pretrain_model` or `args.only_pretrain_model=True`."  )
           else:
                  lowerCamelCase     :   Tuple    				=      MODEL_MAPPING[config.__class__](UpperCamelCase__  )

           model.eval()
           model.to(self.args.device  )

           # encoder-decoder has vocab size saved differently
           lowerCamelCase     :   str    				=      config.vocab_size if hasattr(UpperCamelCase__  ,						"vocab_size"  ) else config.encoder.vocab_size
           lowerCamelCase     :   int    				=      torch.randint(UpperCamelCase__  ,						(batch_size, sequence_length)  ,						dtype=torch.long  ,						device=self.args.device  )

           if self.args.fpaa:
                  logger.info("Running training in Mixed Precision..."  )
                  if not self.args.is_gpu:
                         raise ValueError("Mixed precision is possible only for GPU."  )
                  # amp seems to have memory leaks so that memory usage
                  # is measured using .half() for now https://github.com/NVIDIA/apex/issues/439
                  model.half()

           if self.args.torchscript:
                  with torch.no_grad():
                         lowerCamelCase     :   Optional[int]    				=      torch.jit.trace(UpperCamelCase__  ,						UpperCamelCase__  )
           else:
                  lowerCamelCase     :   Dict    				=      model

           def encoder_decoder_forward():
                  with torch.no_grad():
                         lowerCamelCase     :   Dict    				=      inference_model(UpperCamelCase__  ,						decoder_input_ids=UpperCamelCase__  )
                  return outputs

           def encoder_forward():
                  with torch.no_grad():
                         lowerCamelCase     :   Union[str, Any]    				=      inference_model(UpperCamelCase__  )
                  return outputs

           lowerCamelCase     :   Dict    				=      encoder_decoder_forward if config.is_encoder_decoder else encoder_forward
           return _forward







    def 					_lowercase  (					self  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  )  ->   Callable[[], None]:
           lowerCamelCase     :   Optional[int]    				=      self.config_dict[model_name]

           lowerCamelCase     :   List[Any]    				=      (
               hasattr(UpperCamelCase__  ,						"architectures"  )
               and isinstance(config.architectures  ,						UpperCamelCase__  )
               and len(config.architectures  ) > 0
           )
           if not self.args.only_pretrain_model and has_model_class_in_config:
                  try:
                         lowerCamelCase     :   Union[str, Any]    				=      config.architectures[0]
                         lowerCamelCase     :   int    				=      __import__("transformers"  ,						fromlist=[model_class]  )
                         lowerCamelCase     :   Tuple    				=      getattr(UpperCamelCase__  ,						UpperCamelCase__  )
                         lowerCamelCase     :   Tuple    				=      model_cls(UpperCamelCase__  )
                  except ImportError:
                         raise ImportError(
                             F'''{model_class} does not exist. If you just want to test the pretrained model, you might want to'''
                             " set `--only_pretrain_model` or `args.only_pretrain_model=True`."  )
           else:
                  lowerCamelCase     :   List[str]    				=      MODEL_WITH_LM_HEAD_MAPPING[config.__class__](UpperCamelCase__  )

           if self.args.torchscript:
                  raise NotImplementedError("Training for torchscript is currently not implemented"  )
           else:
                  lowerCamelCase     :   Dict    				=      model

           model.train()
           model.to(self.args.device  )

           # encoder-decoder has vocab size saved differently
           lowerCamelCase     :   Union[str, Any]    				=      config.vocab_size if hasattr(UpperCamelCase__  ,						"vocab_size"  ) else config.encoder.vocab_size
           lowerCamelCase     :   List[str]    				=      torch.randint(UpperCamelCase__  ,						(batch_size, sequence_length)  ,						dtype=torch.long  ,						device=self.args.device  )

           if self.args.fpaa:
                  logger.info("Running training in Mixed Precision..."  )
                  if not self.args.is_gpu:
                         raise ValueError("Mixed precision is possible only for GPU."  )

                  # amp seems to have memory leaks so that memory usage
                  # is measured using .half() for now https://github.com/NVIDIA/apex/issues/439
                  model.half()

           def compute_loss_and_backprob_encoder():
                  lowerCamelCase     :   Tuple    				=      train_model(UpperCamelCase__  ,						labels=UpperCamelCase__  )[0]
                  loss.backward()
                  return loss

           def compute_loss_and_backprob_encoder_decoder():
                  lowerCamelCase     :   Optional[int]    				=      train_model(UpperCamelCase__  ,						decoder_input_ids=UpperCamelCase__  ,						labels=UpperCamelCase__  )[0]
                  loss.backward()
                  return loss

           lowerCamelCase     :   Union[str, Any]    				=      (
               compute_loss_and_backprob_encoder_decoder
               if config.is_encoder_decoder
               else compute_loss_and_backprob_encoder
           )
           return _train







    def 					_lowercase  (					self  ,						UpperCamelCase__  )  ->   float:
           try:
                  if self.args.is_tpu or self.args.torchscript:
                         # run additional 10 times to stabilize compilation for tpu and torchscript
                         logger.info("Do inference on TPU or torchscript. Running model 5 times to stabilize compilation"  )
                         timeit.repeat(
                             UpperCamelCase__  ,						repeat=1  ,						number=5  ,						)

                  # as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average
                  lowerCamelCase     :   List[str]    				=      timeit.repeat(
                      UpperCamelCase__  ,						repeat=self.args.repeat  ,						number=10  ,						)

                  if self.args.is_tpu and self.args.torch_xla_tpu_print_metrics:
                         import torch_xla.debug.metrics as met

                         self.print_fn(met.metrics_report()  )

                  return min(UpperCamelCase__  ) / 10.0
           except RuntimeError as e:
                  self.print_fn(F'''Doesn\'t fit on GPU. {e}'''  )
                  return "N/A"







    def 					_lowercase  (					self  ,						UpperCamelCase__  )  ->   [Memory, MemorySummary]:
           try:
                  if self.args.trace_memory_line_by_line:
                         lowerCamelCase     :   str    				=      start_memory_tracing("transformers"  )

                  if self.args.is_tpu:
                         # tpu
                         raise NotImplementedError(
                             "Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking with"
                             " `--no-memory` or `args.memory=False`"  )
                  elif self.args.is_gpu:
                         if not is_pyanvml_available():
                                logger.warning(
                                    "py3nvml not installed, we won't log GPU memory usage. "
                                    "Install py3nvml (pip install py3nvml) to log information about GPU."  )
                                lowerCamelCase     :   Optional[Any]    				=      "N/A"
                         else:
                                logger.info(
                                    "Measuring total GPU usage on GPU device. Make sure to not have additional processes running"
                                    " on the same GPU."  )
                                # init nvml
                                nvml.nvmlInit()
                                func()
                                lowerCamelCase     :   int    				=      nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx  )
                                lowerCamelCase     :   Dict    				=      nvml.nvmlDeviceGetMemoryInfo(UpperCamelCase__  )
                                lowerCamelCase     :   int    				=      meminfo.used
                                lowerCamelCase     :   Dict    				=      Memory(UpperCamelCase__  )
                                # shutdown nvml
                                nvml.nvmlShutdown()
                  else:
                         # cpu
                         lowerCamelCase     :   Union[str, Any]    				=      measure_peak_memory_cpu(UpperCamelCase__  )
                         lowerCamelCase     :   int    				=      Memory(UpperCamelCase__  ) if isinstance(UpperCamelCase__  ,						UpperCamelCase__  ) else memory_bytes

                  if self.args.trace_memory_line_by_line:
                         lowerCamelCase     :   List[str]    				=      stop_memory_tracing(UpperCamelCase__  )
                  else:
                         lowerCamelCase     :   int    				=      None

                  return memory, summary
           except RuntimeError as e:
                  self.print_fn(F'''Doesn\'t fit on GPU. {e}'''  )
                  return "N/A", None






