
'''simple docstring'''





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


a__		: Optional[int]  							=							logging.get_logger(__name__)


class 			UpperCamelCase__	(     lowerCAmelCase__):
 UpperCAmelCase__     :     PyTorchBenchmarkArguments
 UpperCAmelCase__     :     PretrainedConfig
 UpperCAmelCase__     :     str    					=       "PyTorch"






 @property
 def 						lowercase_    (      self	:Dict      )					->      Optional[int]:







       '''simple docstring'''



       return torch.__version__






 def 						lowercase_    (      self	:Dict						,					_A	:str						,					_A	:int						,					_A	:int      )					->      Optional[Any]:







       '''simple docstring'''



       __A													=      self._prepare_inference_func(_A						,					_A						,					_A      )
       return self._measure_speed(_inference      )






 def 						lowercase_    (      self	:int						,					_A	:str						,					_A	:int						,					_A	:int      )					->      str:







       '''simple docstring'''



       __A													=      self._prepare_inference_func(_A						,					_A						,					_A      )
       return self._measure_memory(_inference      )






 def 						lowercase_    (      self	:int						,					_A	:str						,					_A	:int						,					_A	:int      )					->      Dict:







       '''simple docstring'''



       __A													=      self._prepare_train_func(_A						,					_A						,					_A      )
       return self._measure_speed(_train      )






 def 						lowercase_    (      self	:List[Any]						,					_A	:str						,					_A	:int						,					_A	:int      )					->      int:







       '''simple docstring'''



       __A													=      self._prepare_train_func(_A						,					_A						,					_A      )
       return self._measure_memory(_train      )






 def 						lowercase_    (      self	:Optional[int]						,					_A	:str						,					_A	:int						,					_A	:int      )					->      int:







       '''simple docstring'''



       __A													=      self.config_dict[model_name]

       if self.args.torchscript:
             __A													=      True

       __A													=      (
           hasattr(_A						,					'architectures'      )
           and isinstance(config.architectures						,					_A      )
           and len(config.architectures      ) > 0
       )
       if not self.args.only_pretrain_model and has_model_class_in_config:
             try:
                   __A													=      config.architectures[0]
                   __A													=      __import__('transformers'						,					fromlist=[model_class]      )
                   __A													=      getattr(_A						,					_A      )
                   __A													=      model_cls(_A      )
             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:
             __A													=      MODEL_MAPPING[config.__class__](_A      )

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

       # encoder-decoder has vocab size saved differently
       __A													=      config.vocab_size if hasattr(_A						,					'vocab_size'      ) else config.encoder.vocab_size
       __A													=      torch.randint(_A						,					(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():
                   __A													=      torch.jit.trace(_A						,					_A      )
       else:
             __A													=      model

       def encoder_decoder_forward():
             with torch.no_grad():
                   __A													=      inference_model(_A						,					decoder_input_ids=_A      )
             return outputs

       def encoder_forward():
             with torch.no_grad():
                   __A													=      inference_model(_A      )
             return outputs

       __A													=      encoder_decoder_forward if config.is_encoder_decoder else encoder_forward
       return _forward






 def 						lowercase_    (      self	:Optional[int]						,					_A	:str						,					_A	:int						,					_A	:int      )					->      int:







       '''simple docstring'''



       __A													=      self.config_dict[model_name]

       __A													=      (
           hasattr(_A						,					'architectures'      )
           and isinstance(config.architectures						,					_A      )
           and len(config.architectures      ) > 0
       )
       if not self.args.only_pretrain_model and has_model_class_in_config:
             try:
                   __A													=      config.architectures[0]
                   __A													=      __import__('transformers'						,					fromlist=[model_class]      )
                   __A													=      getattr(_A						,					_A      )
                   __A													=      model_cls(_A      )
             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:
             __A													=      MODEL_WITH_LM_HEAD_MAPPING[config.__class__](_A      )

       if self.args.torchscript:
             raise NotImplementedError('Training for torchscript is currently not implemented'      )
       else:
             __A													=      model

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

       # encoder-decoder has vocab size saved differently
       __A													=      config.vocab_size if hasattr(_A						,					'vocab_size'      ) else config.encoder.vocab_size
       __A													=      torch.randint(_A						,					(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():
             __A													=      train_model(_A						,					labels=_A      )[0]
             loss.backward()
             return loss

       def compute_loss_and_backprob_encoder_decoder():
             __A													=      train_model(_A						,					decoder_input_ids=_A						,					labels=_A      )[0]
             loss.backward()
             return loss

       __A													=      (
           compute_loss_and_backprob_encoder_decoder
           if config.is_encoder_decoder
           else compute_loss_and_backprob_encoder
       )
       return _train






 def 						lowercase_    (      self	:List[Any]						,					_A	:int      )					->      Dict:







       '''simple docstring'''



       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(
                       _A						,					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
             __A													=      timeit.repeat(
                 _A						,					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(_A      ) / 10.0
       except RuntimeError as e:
             self.print_fn(F'Doesn\'t fit on GPU. {e}'      )
             return "N/A"






 def 						lowercase_    (      self	:Optional[Any]						,					_A	:Callable[[], None]      )					->      str:







       '''simple docstring'''



       try:
             if self.args.trace_memory_line_by_line:
                   __A													=      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.'      )
                         __A													=      '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()
                         __A													=      nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx      )
                         __A													=      nvml.nvmlDeviceGetMemoryInfo(_A      )
                         __A													=      meminfo.used
                         __A													=      Memory(_A      )
                         # shutdown nvml
                         nvml.nvmlShutdown()
             else:
                   # cpu
                   __A													=      measure_peak_memory_cpu(_A      )
                   __A													=      Memory(_A      ) if isinstance(_A						,					_A      ) else memory_bytes

             if self.args.trace_memory_line_by_line:
                   __A													=      stop_memory_tracing(_A      )
             else:
                   __A													=      None

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











'''simple docstring'''







import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_layoutlm import LayoutLMTokenizer


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

SCREAMING_SNAKE_CASE__     : List[str]				=   {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

SCREAMING_SNAKE_CASE__     : Optional[int]				=   {
    """vocab_file""": {
        """microsoft/layoutlm-base-uncased""": (
            """https://huggingface.co/microsoft/layoutlm-base-uncased/resolve/main/vocab.txt"""
        ),
        """microsoft/layoutlm-large-uncased""": (
            """https://huggingface.co/microsoft/layoutlm-large-uncased/resolve/main/vocab.txt"""
        ),
    },
    """tokenizer_file""": {
        """microsoft/layoutlm-base-uncased""": (
            """https://huggingface.co/microsoft/layoutlm-base-uncased/resolve/main/tokenizer.json"""
        ),
        """microsoft/layoutlm-large-uncased""": (
            """https://huggingface.co/microsoft/layoutlm-large-uncased/resolve/main/tokenizer.json"""
        ),
    },
}

SCREAMING_SNAKE_CASE__     : Tuple				=   {
    """microsoft/layoutlm-base-uncased""": 5_12,
    """microsoft/layoutlm-large-uncased""": 5_12,
}

SCREAMING_SNAKE_CASE__     : List[Any]				=   {
    """microsoft/layoutlm-base-uncased""": {"""do_lower_case""": True},
    """microsoft/layoutlm-large-uncased""": {"""do_lower_case""": True},
}




class 				__lowerCAmelCase(							lowerCAmelCase__      ):
			__snake_case  :		List[str]		=      VOCAB_FILES_NAMES
			__snake_case  :		Dict		=      PRETRAINED_VOCAB_FILES_MAP
			__snake_case  :		Optional[int]		=      PRETRAINED_INIT_CONFIGURATION
			__snake_case  :		Any		=      PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			__snake_case  :		str		=      LayoutLMTokenizer






			def __init__(   self  :							Optional[int]      ,       SCREAMING_SNAKE_CASE  :							Dict=None      ,       SCREAMING_SNAKE_CASE  :							int=None      ,       SCREAMING_SNAKE_CASE  :							Tuple=True      ,       SCREAMING_SNAKE_CASE  :							Optional[int]="[UNK]"      ,       SCREAMING_SNAKE_CASE  :							Tuple="[SEP]"      ,       SCREAMING_SNAKE_CASE  :							int="[PAD]"      ,       SCREAMING_SNAKE_CASE  :							List[Any]="[CLS]"      ,       SCREAMING_SNAKE_CASE  :							Union[str, Any]="[MASK]"      ,       SCREAMING_SNAKE_CASE  :							Optional[Any]=True      ,       SCREAMING_SNAKE_CASE  :							Union[str, Any]=None      ,       **SCREAMING_SNAKE_CASE  :							Optional[int]      ,       ):




					"""simple docstring"""







					super().__init__(
					    SCREAMING_SNAKE_CASE      ,       tokenizer_file=SCREAMING_SNAKE_CASE      ,       do_lower_case=SCREAMING_SNAKE_CASE      ,       unk_token=SCREAMING_SNAKE_CASE      ,       sep_token=SCREAMING_SNAKE_CASE      ,       pad_token=SCREAMING_SNAKE_CASE      ,       cls_token=SCREAMING_SNAKE_CASE      ,       mask_token=SCREAMING_SNAKE_CASE      ,       tokenize_chinese_chars=SCREAMING_SNAKE_CASE      ,       strip_accents=SCREAMING_SNAKE_CASE      ,       **SCREAMING_SNAKE_CASE      ,       )

					SCREAMING_SNAKE_CASE_						:str										=      json.loads(self.backend_tokenizer.normalizer.__getstate__()	)
					if (
					    normalizer_state.get('lowercase'      ,       SCREAMING_SNAKE_CASE	) != do_lower_case
					    or normalizer_state.get('strip_accents'      ,       SCREAMING_SNAKE_CASE	) != strip_accents
					    or normalizer_state.get('handle_chinese_chars'      ,       SCREAMING_SNAKE_CASE	) != tokenize_chinese_chars
					):
							SCREAMING_SNAKE_CASE_						:List[str]										=      getattr(SCREAMING_SNAKE_CASE      ,       normalizer_state.pop('type'	)	)
							SCREAMING_SNAKE_CASE_						:Tuple										=      do_lower_case
							SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      strip_accents
							SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      tokenize_chinese_chars
							SCREAMING_SNAKE_CASE_						:Optional[int]										=      normalizer_class(**SCREAMING_SNAKE_CASE	)

					SCREAMING_SNAKE_CASE_						:List[str]										=      do_lower_case






			def 				_lowercase	(   self  :							Tuple      ,       SCREAMING_SNAKE_CASE  :							Optional[int]      ,       SCREAMING_SNAKE_CASE  :							str=None	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Optional[int]										=      [self.cls_token_id] + token_ids_a + [self.sep_token_id]

					if token_ids_a:
							output += token_ids_a + [self.sep_token_id]

					return output






			def 				_lowercase	(   self  :							List[str]      ,       SCREAMING_SNAKE_CASE  :							List[int]      ,       SCREAMING_SNAKE_CASE  :							Optional[List[int]] = None	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      [self.sep_token_id]
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      [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	) * [0] + len(token_ids_a + sep	) * [1]






			def 				_lowercase	(   self  :							str      ,       SCREAMING_SNAKE_CASE  :							str      ,       SCREAMING_SNAKE_CASE  :							Optional[str] = None	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Tuple										=      self._tokenizer.model.save(SCREAMING_SNAKE_CASE      ,       name=SCREAMING_SNAKE_CASE	)
					return tuple(SCREAMING_SNAKE_CASE	)
