import json
import os
import re
import unicodedata
from typing import Dict, List, Optional, Tuple

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


lowerCamelCase__   = logging.get_logger(__name__)

lowerCamelCase__   = {
    '''src_vocab_file''': '''vocab-src.json''',
    '''tgt_vocab_file''': '''vocab-tgt.json''',
    '''merges_file''': '''merges.txt''',
}

lowerCamelCase__   = {
    '''src_vocab_file''': {
        '''stas/tiny-wmt19-en-de''': '''https://huggingface.co/stas/tiny-wmt19-en-de/resolve/main/vocab-src.json'''
    },
    '''tgt_vocab_file''': {
        '''stas/tiny-wmt19-en-de''': '''https://huggingface.co/stas/tiny-wmt19-en-de/resolve/main/vocab-tgt.json'''
    },
    '''merges_file''': {'''stas/tiny-wmt19-en-de''': '''https://huggingface.co/stas/tiny-wmt19-en-de/resolve/main/merges.txt'''},
}

lowerCamelCase__   = {'''stas/tiny-wmt19-en-de''': 1024}
lowerCamelCase__   = {
    '''stas/tiny-wmt19-en-de''': {
        '''langs''': ['''en''', '''de'''],
        '''model_max_length''': 1024,
        '''special_tokens_map_file''': None,
        '''full_tokenizer_file''': None,
    }
}


def 							lowerCAmelCase__    (       a__      )	->List[Any]:




  '''simple docstring'''

  _UpperCamelCase				=  set()
  _UpperCamelCase				=  word[0]
  for char in word[1:]:
    pairs.add((prev_char, char)      )
    _UpperCamelCase				=  char
  return pairs


def 							lowerCAmelCase__    (       a__      )	->List[str]:




  '''simple docstring'''

  _UpperCamelCase				=  text.replace("，"  ,			","      )
  _UpperCamelCase				=  re.sub(r"。\s*"  ,			". "  ,			a__      )
  _UpperCamelCase				=  text.replace("、"  ,			","      )
  _UpperCamelCase				=  text.replace("”"  ,			"\""      )
  _UpperCamelCase				=  text.replace("“"  ,			"\""      )
  _UpperCamelCase				=  text.replace("∶"  ,			":"      )
  _UpperCamelCase				=  text.replace("："  ,			":"      )
  _UpperCamelCase				=  text.replace("？"  ,			"?"      )
  _UpperCamelCase				=  text.replace("《"  ,			"\""      )
  _UpperCamelCase				=  text.replace("》"  ,			"\""      )
  _UpperCamelCase				=  text.replace("）"  ,			")"      )
  _UpperCamelCase				=  text.replace("！"  ,			"!"      )
  _UpperCamelCase				=  text.replace("（"  ,			"("      )
  _UpperCamelCase				=  text.replace("；"  ,			";"      )
  _UpperCamelCase				=  text.replace("１"  ,			"1"      )
  _UpperCamelCase				=  text.replace("」"  ,			"\""      )
  _UpperCamelCase				=  text.replace("「"  ,			"\""      )
  _UpperCamelCase				=  text.replace("０"  ,			"0"      )
  _UpperCamelCase				=  text.replace("３"  ,			"3"      )
  _UpperCamelCase				=  text.replace("２"  ,			"2"      )
  _UpperCamelCase				=  text.replace("５"  ,			"5"      )
  _UpperCamelCase				=  text.replace("６"  ,			"6"      )
  _UpperCamelCase				=  text.replace("９"  ,			"9"      )
  _UpperCamelCase				=  text.replace("７"  ,			"7"      )
  _UpperCamelCase				=  text.replace("８"  ,			"8"      )
  _UpperCamelCase				=  text.replace("４"  ,			"4"      )
  _UpperCamelCase				=  re.sub(r"．\s*"  ,			". "  ,			a__      )
  _UpperCamelCase				=  text.replace("～"  ,			"~"      )
  _UpperCamelCase				=  text.replace("’"  ,			"'"      )
  _UpperCamelCase				=  text.replace("…"  ,			"..."      )
  _UpperCamelCase				=  text.replace("━"  ,			"-"      )
  _UpperCamelCase				=  text.replace("〈"  ,			"<"      )
  _UpperCamelCase				=  text.replace("〉"  ,			">"      )
  _UpperCamelCase				=  text.replace("【"  ,			"["      )
  _UpperCamelCase				=  text.replace("】"  ,			"]"      )
  _UpperCamelCase				=  text.replace("％"  ,			"%"      )
  return text


def 							lowerCAmelCase__    (       a__      )	->str:




  '''simple docstring'''

  _UpperCamelCase				=  []
  for char in text:
    _UpperCamelCase				=  unicodedata.category(a__      )
    if cat.startswith("C"      ):
      continue
    output.append(a__      )
  return "".join(a__      )







class  _UpperCAmelCase       (			lowerCAmelCase   ):



 '''simple docstring'''




 __A           = VOCAB_FILES_NAMES
 __A           = PRETRAINED_VOCAB_FILES_MAP
 __A           = PRETRAINED_INIT_CONFIGURATION
 __A           = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 __A           = ['''input_ids''', '''attention_mask''']







 def __init__(						self							:					List[str]				,      lowercase_							:					List[str]=None				,      lowercase_							:					List[Any]=None				,      lowercase_							:					Any=None				,      lowercase_							:					int=None				,      lowercase_							:					List[str]=False				,      lowercase_							:					int="<unk>"				,      lowercase_							:					str="<s>"				,      lowercase_							:					Any="</s>"				,      lowercase_							:					int="<pad>"				,      **lowercase_							:					int				,      )		->     Optional[Any]:


   """simple docstring"""







   super().__init__(
       langs=lowercase_				,      src_vocab_file=lowercase_				,      tgt_vocab_file=lowercase_				,      merges_file=lowercase_				,      do_lower_case=lowercase_				,      unk_token=lowercase_				,      bos_token=lowercase_				,      sep_token=lowercase_				,      pad_token=lowercase_				,      **lowercase_				,      )

   try:
     import sacremoses
   except ImportError:
     raise ImportError(
         "You need to install sacremoses to use XLMTokenizer. "
         "See https://pypi.org/project/sacremoses/ for installation.")

   _UpperCamelCase				=  sacremoses

   _UpperCamelCase				=  src_vocab_file
   _UpperCamelCase				=  tgt_vocab_file
   _UpperCamelCase				=  merges_file
   _UpperCamelCase				=  do_lower_case

   # cache of sm.MosesPunctNormalizer instance
   _UpperCamelCase				=  {}
   # cache of sm.MosesTokenizer instance
   _UpperCamelCase				=  {}
   _UpperCamelCase				=  {}

   if langs and len(lowercase_) == 2:
     _UpperCamelCase	,		_UpperCamelCase				=  langs
   else:
     raise ValueError(
         f'arg `langs` needs to be a list of 2 langs, e.g. [\'en\', \'ru\'], but got {langs}. '
         "Usually that means that tokenizer can't find a mapping for the given model path "
         "in PRETRAINED_VOCAB_FILES_MAP, and other maps of this tokenizer.")

   with open(lowercase_				,      encoding="utf-8") as src_vocab_handle:
     _UpperCamelCase				=  json.load(lowercase_)
   with open(lowercase_				,      encoding="utf-8") as tgt_vocab_handle:
     _UpperCamelCase				=  json.load(lowercase_)
     _UpperCamelCase				=  {v: k for k, v in tgt_vocab.items()}
   with open(lowercase_				,      encoding="utf-8") as merges_handle:
     _UpperCamelCase				=  merges_handle.read().split("\n")[:-1]
   _UpperCamelCase				=  [tuple(merge.split()[:2]) for merge in merges]
   _UpperCamelCase				=  dict(zip(lowercase_				,      range(len(lowercase_))))
   _UpperCamelCase				=  {}







 def 						__UpperCAmelCase							(						self							:					List[str])		->     Dict[str, int]:


   """simple docstring"""







   return self.get_src_vocab()







 @property
 def 						__UpperCAmelCase							(						self							:					Dict)		->     int:


   """simple docstring"""







   return self.src_vocab_size







 def 						__UpperCAmelCase							(						self							:					List[str]				,      lowercase_							:					Dict				,      lowercase_							:					str)		->     Optional[Any]:


   """simple docstring"""







   if lang not in self.cache_moses_punct_normalizer:
     _UpperCamelCase				=  self.sm.MosesPunctNormalizer(lang=lowercase_)
     _UpperCamelCase				=  punct_normalizer
   return self.cache_moses_punct_normalizer[lang].normalize(lowercase_)







 def 						__UpperCAmelCase							(						self							:					int				,      lowercase_							:					Dict				,      lowercase_							:					Tuple)		->     List[str]:


   """simple docstring"""







   if lang not in self.cache_moses_tokenizer:
     _UpperCamelCase				=  self.sm.MosesTokenizer(lang=lowercase_)
     _UpperCamelCase				=  moses_tokenizer
   return self.cache_moses_tokenizer[lang].tokenize(
       lowercase_				,      aggressive_dash_splits=lowercase_				,      return_str=lowercase_				,      escape=lowercase_)







 def 						__UpperCAmelCase							(						self							:					Optional[Any]				,      lowercase_							:					Union[str, Any]				,      lowercase_							:					Any)		->     str:


   """simple docstring"""







   if lang not in self.cache_moses_detokenizer:
     _UpperCamelCase				=  self.sm.MosesDetokenizer(lang=lowercase_)
     _UpperCamelCase				=  moses_detokenizer
   return self.cache_moses_detokenizer[lang].detokenize(lowercase_)







 def 						__UpperCAmelCase							(						self							:					Tuple				,      lowercase_							:					Dict				,      lowercase_							:					int)		->     Optional[Any]:


   """simple docstring"""







   _UpperCamelCase				=  replace_unicode_punct(lowercase_)
   _UpperCamelCase				=  self.moses_punct_norm(lowercase_				,      lowercase_)
   _UpperCamelCase				=  remove_non_printing_char(lowercase_)
   return text







 @property
 def 						__UpperCAmelCase							(						self							:					List[Any])		->     str:


   """simple docstring"""







   return len(self.encoder)







 @property
 def 						__UpperCAmelCase							(						self							:					Union[str, Any])		->     Optional[Any]:


   """simple docstring"""







   return len(self.decoder)







 def 						__UpperCAmelCase							(						self							:					str)		->     List[Any]:


   """simple docstring"""







   return dict(self.encoder				,      **self.added_tokens_encoder)







 def 						__UpperCAmelCase							(						self							:					Any)		->     Optional[Any]:


   """simple docstring"""







   return dict(self.decoder				,      **self.added_tokens_decoder)







 def 						__UpperCAmelCase							(						self							:					int				,      lowercase_							:					Union[str, Any])		->     Union[str, Any]:


   """simple docstring"""







   _UpperCamelCase				=  tuple(token[:-1]) + (token[-1] + "</w>",)
   if token in self.cache:
     return self.cache[token]
   _UpperCamelCase				=  get_pairs(lowercase_)

   if not pairs:
     return token + "</w>"

   while True:
     _UpperCamelCase				=  min(lowercase_				,      key=lambda lowercase_: self.bpe_ranks.get(lowercase_				,      float("inf")))
     if bigram not in self.bpe_ranks:
       break
     _UpperCamelCase	,		_UpperCamelCase				=  bigram
     _UpperCamelCase				=  []
     _UpperCamelCase				=  0
     while i < len(lowercase_):
       try:
         _UpperCamelCase				=  word.index(lowercase_				,      lowercase_)
       except ValueError:
         new_word.extend(word[i:])
         break
       else:
         new_word.extend(word[i:j])
         _UpperCamelCase				=  j

       if word[i] == first and i < len(lowercase_) - 1 and word[i + 1] == second:
         new_word.append(first + second)
         i += 2
       else:
         new_word.append(word[i])
         i += 1
     _UpperCamelCase				=  tuple(lowercase_)
     _UpperCamelCase				=  new_word
     if len(lowercase_) == 1:
       break
     else:
       _UpperCamelCase				=  get_pairs(lowercase_)
   _UpperCamelCase				=  " ".join(lowercase_)
   if word == "\n  </w>":
     _UpperCamelCase				=  "\n</w>"
   _UpperCamelCase				=  word
   return word







 def 						__UpperCAmelCase							(						self							:					Optional[int]				,      lowercase_							:					Tuple				,      lowercase_							:					Tuple="en"				,      lowercase_							:					int=False)		->     int:


   """simple docstring"""







   _UpperCamelCase				=  self.src_lang

   if self.do_lower_case:
     _UpperCamelCase				=  text.lower()

   if bypass_tokenizer:
     _UpperCamelCase				=  text.split()
   else:
     _UpperCamelCase				=  self.moses_pipeline(lowercase_				,      lang=lowercase_)
     _UpperCamelCase				=  self.moses_tokenize(lowercase_				,      lang=lowercase_)

   _UpperCamelCase				=  []
   for token in text:
     if token:
       split_tokens.extend(list(self.bpe(lowercase_).split(" ")))

   return split_tokens







 def 						__UpperCAmelCase							(						self							:					Any				,      lowercase_							:					Dict)		->     List[str]:


   """simple docstring"""







   return self.encoder.get(lowercase_				,      self.encoder.get(self.unk_token))







 def 						__UpperCAmelCase							(						self							:					int				,      lowercase_							:					Optional[int])		->     Any:


   """simple docstring"""







   return self.decoder.get(lowercase_				,      self.unk_token)







 def 						__UpperCAmelCase							(						self							:					Dict				,      lowercase_							:					Dict)		->     List[str]:


   """simple docstring"""







   _UpperCamelCase				=  [t.replace(" "				,      "").replace("</w>"				,      " ") for t in tokens]
   _UpperCamelCase				=  "".join(lowercase_).split()
   # detokenize
   _UpperCamelCase				=  self.moses_detokenize(lowercase_				,      self.tgt_lang)
   return text







 def 						__UpperCAmelCase							(						self							:					Optional[Any]				,      lowercase_							:					List[int]				,      lowercase_							:					Optional[List[int]] = None)		->     List[int]:


   """simple docstring"""







   _UpperCamelCase				=  [self.sep_token_id]

   # no bos used in fairseq
   if token_ids_a is None:
     return token_ids_a + sep
   return token_ids_a + sep + token_ids_a + sep







 def 						__UpperCAmelCase							(						self							:					str				,      lowercase_							:					List[int]				,      lowercase_							:					Optional[List[int]] = None				,      lowercase_							:					bool = False)		->     List[int]:


   """simple docstring"""







   if already_has_special_tokens:
     return super().get_special_tokens_mask(
         token_ids_a=lowercase_				,      token_ids_a=lowercase_				,      already_has_special_tokens=lowercase_)
   # no bos used in fairseq
   if token_ids_a is not None:
     return ([0] * len(lowercase_)) + [1] + ([0] * len(lowercase_)) + [1]
   return ([0] * len(lowercase_)) + [1]







 def 						__UpperCAmelCase							(						self							:					Optional[Any]				,      lowercase_							:					List[int]				,      lowercase_							:					Optional[List[int]] = None)		->     List[int]:


   """simple docstring"""







   _UpperCamelCase				=  [self.sep_token_id]

   # no bos used in fairseq
   if token_ids_a is None:
     return len(token_ids_a + sep) * [0]
   return len(token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1]







 def 						__UpperCAmelCase							(						self							:					Optional[int]				,      lowercase_							:					str				,      lowercase_							:					Optional[str] = None)		->     Tuple[str]:


   """simple docstring"""







   if not os.path.isdir(lowercase_):
     logger.error(f'Vocabulary path ({save_directory}) should be a directory')
     return

   _UpperCamelCase				=  os.path.join(
       lowercase_				,      (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["src_vocab_file"])
   _UpperCamelCase				=  os.path.join(
       lowercase_				,      (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["tgt_vocab_file"])
   _UpperCamelCase				=  os.path.join(
       lowercase_				,      (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"])

   with open(lowercase_				,      "w"				,      encoding="utf-8") as f:
     f.write(json.dumps(self.encoder				,      indent=2				,      sort_keys=lowercase_				,      ensure_ascii=lowercase_) + "\n")

   with open(lowercase_				,      "w"				,      encoding="utf-8") as f:
     _UpperCamelCase				=  {v: k for k, v in self.decoder.items()}
     f.write(json.dumps(lowercase_				,      indent=2				,      sort_keys=lowercase_				,      ensure_ascii=lowercase_) + "\n")

   _UpperCamelCase				=  0
   with open(lowercase_				,      "w"				,      encoding="utf-8") as writer:
     for bpe_tokens, token_index in sorted(self.bpe_ranks.items()				,      key=lambda lowercase_: kv[1]):
       if index != token_index:
         logger.warning(
             f'Saving vocabulary to {merges_file}: BPE merge indices are not consecutive.'
             " Please check that the tokenizer is not corrupted!")
         _UpperCamelCase				=  token_index
       writer.write(" ".join(lowercase_) + "\n")
       index += 1

   return src_vocab_file, tgt_vocab_file, merges_file







 def __getstate__(						self							:					Any)		->     Dict:


   """simple docstring"""







   _UpperCamelCase				=  self.__dict__.copy()
   _UpperCamelCase				=  None
   return state







 def __setstate__(						self							:					Any				,      lowercase_							:					Tuple)		->     Union[str, Any]:


   """simple docstring"""







   _UpperCamelCase				=  d

   try:
     import sacremoses
   except ImportError:
     raise ImportError(
         "You need to install sacremoses to use XLMTokenizer. "
         "See https://pypi.org/project/sacremoses/ for installation.")

   _UpperCamelCase				=  sacremoses





import unittest

from transformers import LayoutLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import (
       LayoutLMForMaskedLM,
       LayoutLMForQuestionAnswering,
       LayoutLMForSequenceClassification,
       LayoutLMForTokenClassification,
       LayoutLMModel,
   )




class  _UpperCAmelCase       :



 '''simple docstring'''




 def __init__(						self							:					Tuple				,      lowercase_							:					int				,      lowercase_							:					Union[str, Any]=13				,      lowercase_							:					Optional[int]=7				,      lowercase_							:					Optional[Any]=True				,      lowercase_							:					Tuple=True				,      lowercase_							:					str=True				,      lowercase_							:					Union[str, Any]=True				,      lowercase_							:					int=99				,      lowercase_							:					Dict=32				,      lowercase_							:					Optional[int]=5				,      lowercase_							:					List[Any]=4				,      lowercase_							:					List[str]=37				,      lowercase_							:					Dict="gelu"				,      lowercase_							:					Union[str, Any]=0.1				,      lowercase_							:					List[Any]=0.1				,      lowercase_							:					int=512				,      lowercase_							:					int=16				,      lowercase_							:					Dict=2				,      lowercase_							:					Union[str, Any]=0.02				,      lowercase_							:					List[Any]=3				,      lowercase_							:					List[Any]=4				,      lowercase_							:					Optional[Any]=None				,      lowercase_							:					List[Any]=1000				,      )		->     Optional[int]:


   """simple docstring"""







   _UpperCamelCase				=  parent
   _UpperCamelCase				=  batch_size
   _UpperCamelCase				=  seq_length
   _UpperCamelCase				=  is_training
   _UpperCamelCase				=  use_input_mask
   _UpperCamelCase				=  use_token_type_ids
   _UpperCamelCase				=  use_labels
   _UpperCamelCase				=  vocab_size
   _UpperCamelCase				=  hidden_size
   _UpperCamelCase				=  num_hidden_layers
   _UpperCamelCase				=  num_attention_heads
   _UpperCamelCase				=  intermediate_size
   _UpperCamelCase				=  hidden_act
   _UpperCamelCase				=  hidden_dropout_prob
   _UpperCamelCase				=  attention_probs_dropout_prob
   _UpperCamelCase				=  max_position_embeddings
   _UpperCamelCase				=  type_vocab_size
   _UpperCamelCase				=  type_sequence_label_size
   _UpperCamelCase				=  initializer_range
   _UpperCamelCase				=  num_labels
   _UpperCamelCase				=  num_choices
   _UpperCamelCase				=  scope
   _UpperCamelCase				=  range_bbox







 def 						__UpperCAmelCase							(						self							:					Union[str, Any])		->     Optional[Any]:


   """simple docstring"""







   _UpperCamelCase				=  ids_tensor([self.batch_size, self.seq_length]				,      self.vocab_size)

   _UpperCamelCase				=  ids_tensor([self.batch_size, self.seq_length, 4]				,      self.range_bbox)
   # Ensure that bbox is legal
   for i in range(bbox.shape[0]):
     for j in range(bbox.shape[1]):
       if bbox[i, j, 3] < bbox[i, j, 1]:
         _UpperCamelCase				=  bbox[i, j, 3]
         _UpperCamelCase				=  bbox[i, j, 1]
         _UpperCamelCase				=  t
       if bbox[i, j, 2] < bbox[i, j, 0]:
         _UpperCamelCase				=  bbox[i, j, 2]
         _UpperCamelCase				=  bbox[i, j, 0]
         _UpperCamelCase				=  t

   _UpperCamelCase				=  None
   if self.use_input_mask:
     _UpperCamelCase				=  ids_tensor([self.batch_size, self.seq_length]				,      vocab_size=2)

   _UpperCamelCase				=  None
   if self.use_token_type_ids:
     _UpperCamelCase				=  ids_tensor([self.batch_size, self.seq_length]				,      self.type_vocab_size)

   _UpperCamelCase				=  None
   _UpperCamelCase				=  None
   _UpperCamelCase				=  None
   if self.use_labels:
     _UpperCamelCase				=  ids_tensor([self.batch_size]				,      self.type_sequence_label_size)
     _UpperCamelCase				=  ids_tensor([self.batch_size, self.seq_length]				,      self.num_labels)
     _UpperCamelCase				=  ids_tensor([self.batch_size]				,      self.num_choices)

   _UpperCamelCase				=  self.get_config()

   return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







 def 						__UpperCAmelCase							(						self							:					Union[str, Any])		->     List[Any]:


   """simple docstring"""







   return LayoutLMConfig(
       vocab_size=self.vocab_size				,      hidden_size=self.hidden_size				,      num_hidden_layers=self.num_hidden_layers				,      num_attention_heads=self.num_attention_heads				,      intermediate_size=self.intermediate_size				,      hidden_act=self.hidden_act				,      hidden_dropout_prob=self.hidden_dropout_prob				,      attention_probs_dropout_prob=self.attention_probs_dropout_prob				,      max_position_embeddings=self.max_position_embeddings				,      type_vocab_size=self.type_vocab_size				,      initializer_range=self.initializer_range				,      )







 def 						__UpperCAmelCase							(						self							:					List[str]				,      lowercase_							:					Optional[Any]				,      lowercase_							:					str				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Any				,      lowercase_							:					Dict				,      lowercase_							:					List[str]				,      lowercase_							:					Dict				,      lowercase_							:					List[str])		->     List[Any]:


   """simple docstring"""







   _UpperCamelCase				=  LayoutLMModel(config=lowercase_)
   model.to(lowercase_)
   model.eval()
   _UpperCamelCase				=  model(lowercase_				,      lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_)
   _UpperCamelCase				=  model(lowercase_				,      lowercase_				,      token_type_ids=lowercase_)
   _UpperCamelCase				=  model(lowercase_				,      lowercase_)
   self.parent.assertEqual(result.last_hidden_state.shape				,      (self.batch_size, self.seq_length, self.hidden_size))
   self.parent.assertEqual(result.pooler_output.shape				,      (self.batch_size, self.hidden_size))







 def 						__UpperCAmelCase							(						self							:					Dict				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Dict				,      lowercase_							:					Tuple				,      lowercase_							:					int				,      lowercase_							:					List[Any]				,      lowercase_							:					int				,      lowercase_							:					Optional[int]				,      lowercase_							:					Tuple)		->     Tuple:


   """simple docstring"""







   _UpperCamelCase				=  LayoutLMForMaskedLM(config=lowercase_)
   model.to(lowercase_)
   model.eval()
   _UpperCamelCase				=  model(lowercase_				,      lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_				,      labels=lowercase_)
   self.parent.assertEqual(result.logits.shape				,      (self.batch_size, self.seq_length, self.vocab_size))







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


   """simple docstring"""







   _UpperCamelCase				=  self.num_labels
   _UpperCamelCase				=  LayoutLMForSequenceClassification(lowercase_)
   model.to(lowercase_)
   model.eval()
   _UpperCamelCase				=  model(
       lowercase_				,      lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_				,      labels=lowercase_)
   self.parent.assertEqual(result.logits.shape				,      (self.batch_size, self.num_labels))







 def 						__UpperCAmelCase							(						self							:					List[Any]				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Dict				,      lowercase_							:					Dict				,      lowercase_							:					Union[str, Any]				,      lowercase_							:					str				,      lowercase_							:					Tuple)		->     List[Any]:


   """simple docstring"""







   _UpperCamelCase				=  self.num_labels
   _UpperCamelCase				=  LayoutLMForTokenClassification(config=lowercase_)
   model.to(lowercase_)
   model.eval()
   _UpperCamelCase				=  model(lowercase_				,      lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_				,      labels=lowercase_)
   self.parent.assertEqual(result.logits.shape				,      (self.batch_size, self.seq_length, self.num_labels))







 def 						__UpperCAmelCase							(						self							:					str				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Tuple				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Tuple				,      lowercase_							:					str				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Optional[Any])		->     Tuple:


   """simple docstring"""







   _UpperCamelCase				=  LayoutLMForQuestionAnswering(config=lowercase_)
   model.to(lowercase_)
   model.eval()
   _UpperCamelCase				=  model(
       lowercase_				,      bbox=lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_				,      start_positions=lowercase_				,      end_positions=lowercase_				,      )
   self.parent.assertEqual(result.start_logits.shape				,      (self.batch_size, self.seq_length))
   self.parent.assertEqual(result.end_logits.shape				,      (self.batch_size, self.seq_length))







 def 						__UpperCAmelCase							(						self							:					Dict)		->     List[str]:


   """simple docstring"""







   _UpperCamelCase				=  self.prepare_config_and_inputs()
   (
       (
       _UpperCamelCase
   )	,		(
       _UpperCamelCase
   )	,		(
       _UpperCamelCase
   )	,		(
       _UpperCamelCase
   )	,		(
       _UpperCamelCase
   )	,		(
       _UpperCamelCase
   )	,		(
       _UpperCamelCase
   )	,		(
       _UpperCamelCase
   )	,		
   )				=  config_and_inputs
   _UpperCamelCase				=  {
       "input_ids": input_ids,
       "bbox": bbox,
       "token_type_ids": token_type_ids,
       "attention_mask": input_mask,
   }
   return config, inputs_dict




@require_torch
class  _UpperCAmelCase       (			lowerCAmelCase,       lowerCAmelCase,       unittest.TestCase   ):



 '''simple docstring'''




 __A           = (
     (
         LayoutLMModel,
         LayoutLMForMaskedLM,
         LayoutLMForSequenceClassification,
         LayoutLMForTokenClassification,
         LayoutLMForQuestionAnswering,
     )
     if is_torch_available()
     else None
 )
 __A           = (
     {
         '''document-question-answering''': LayoutLMForQuestionAnswering,
         '''feature-extraction''': LayoutLMModel,
         '''fill-mask''': LayoutLMForMaskedLM,
         '''text-classification''': LayoutLMForSequenceClassification,
         '''token-classification''': LayoutLMForTokenClassification,
         '''zero-shot''': LayoutLMForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 __A           = True







 def 						__UpperCAmelCase							(						self							:					List[Any])		->     List[str]:


   """simple docstring"""







   _UpperCamelCase				=  LayoutLMModelTester(self)
   _UpperCamelCase				=  ConfigTester(self				,      config_class=lowercase_				,      hidden_size=37)







 def 						__UpperCAmelCase							(						self							:					List[Any])		->     Union[str, Any]:


   """simple docstring"""







   self.config_tester.run_common_tests()







 def 						__UpperCAmelCase							(						self							:					Optional[int])		->     str:


   """simple docstring"""







   _UpperCamelCase				=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*lowercase_)







 def 						__UpperCAmelCase							(						self							:					List[Any])		->     Dict:


   """simple docstring"""







   _UpperCamelCase				=  self.model_tester.prepare_config_and_inputs()
   for type in ["absolute", "relative_key", "relative_key_query"]:
     _UpperCamelCase				=  type
     self.model_tester.create_and_check_model(*lowercase_)







 def 						__UpperCAmelCase							(						self							:					Any)		->     Optional[Any]:


   """simple docstring"""







   _UpperCamelCase				=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_masked_lm(*lowercase_)







 def 						__UpperCAmelCase							(						self							:					Dict)		->     str:


   """simple docstring"""







   _UpperCamelCase				=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_sequence_classification(*lowercase_)







 def 						__UpperCAmelCase							(						self							:					Tuple)		->     Dict:


   """simple docstring"""







   _UpperCamelCase				=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_token_classification(*lowercase_)







 def 						__UpperCAmelCase							(						self							:					List[str])		->     Optional[int]:


   """simple docstring"""







   _UpperCamelCase				=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_question_answering(*lowercase_)







 @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests.")
 def 						__UpperCAmelCase							(						self							:					Tuple)		->     List[str]:


   """simple docstring"""







   pass


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




  '''simple docstring'''

  _UpperCamelCase				=  torch.tensor([[101,1_019,1_014,1_016,1_037,12_849,4_747,1_004,14_246,2_278,5_439,4_524,5_002,2_930,2_193,2_930,4_341,3_208,1_005,1_055,2_171,2_848,11_300,3_531,102],[101,4_070,4_034,7_020,1_024,3_058,1_015,1_013,2_861,1_013,6_070,19_274,2_772,6_205,27_814,16_147,16_147,4_343,2_047,10_283,10_969,14_389,1_012,2_338,102]]  ,			device=a__      )  # noqa: E231
  _UpperCamelCase				=  torch.tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],]  ,			device=a__      )  # noqa: E231
  _UpperCamelCase				=  torch.tensor([[[0,0,0,0],[423,237,440,251],[427,272,441,287],[419,115,437,129],[961,885,992,912],[256,38,330,58],[256,38,330,58],[336,42,353,57],[360,39,401,56],[360,39,401,56],[411,39,471,59],[479,41,528,59],[533,39,630,60],[67,113,134,131],[141,115,209,132],[68,149,133,166],[141,149,187,164],[195,148,287,165],[195,148,287,165],[195,148,287,165],[295,148,349,165],[441,149,492,166],[497,149,546,164],[64,201,125,218],[1_000,1_000,1_000,1_000]],[[0,0,0,0],[662,150,754,166],[665,199,742,211],[519,213,554,228],[519,213,554,228],[134,433,187,454],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[314,469,376,482],[504,684,582,706],[941,825,973,900],[941,825,973,900],[941,825,973,900],[941,825,973,900],[610,749,652,765],[130,659,168,672],[176,657,237,672],[238,657,312,672],[443,653,628,672],[443,653,628,672],[716,301,825,317],[1_000,1_000,1_000,1_000]]]  ,			device=a__      )  # noqa: E231
  _UpperCamelCase				=  torch.tensor([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]  ,			device=a__      )  # noqa: E231
  # these are sequence labels (i.e. at the token level)
  _UpperCamelCase				=  torch.tensor([[-100,10,10,10,9,1,-100,7,7,-100,7,7,4,2,5,2,8,8,-100,-100,5,0,3,2,-100],[-100,12,12,12,-100,12,10,-100,-100,-100,-100,10,12,9,-100,-100,-100,10,10,10,9,12,-100,10,-100]]  ,			device=a__      )  # noqa: E231
  # fmt: on

  return input_ids, attention_mask, bbox, token_type_ids, labels







@require_torch
class  _UpperCAmelCase       (			unittest.TestCase   ):



 '''simple docstring'''




 @slow
 def 						__UpperCAmelCase							(						self							:					Any)		->     Union[str, Any]:


   """simple docstring"""







   _UpperCamelCase				=  LayoutLMModel.from_pretrained("microsoft/layoutlm-base-uncased").to(lowercase_)

   _UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase				=  prepare_layoutlm_batch_inputs()

   # forward pass
   _UpperCamelCase				=  model(input_ids=lowercase_				,      bbox=lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_)

   # test the sequence output on [0, :3, :3]
   _UpperCamelCase				=  torch.tensor(
       [[0.17_85, -0.19_47, -0.04_25], [-0.32_54, -0.28_07, 0.25_53], [-0.53_91, -0.33_22, 0.33_64]]				,      device=lowercase_				,      )

   self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]				,      lowercase_				,      atol=1e-3))

   # test the pooled output on [1, :3]
   _UpperCamelCase				=  torch.tensor([-0.65_80, -0.02_14, 0.85_52]				,      device=lowercase_)

   self.assertTrue(torch.allclose(outputs.pooler_output[1, :3]				,      lowercase_				,      atol=1e-3))







 @slow
 def 						__UpperCAmelCase							(						self							:					Optional[int])		->     List[str]:


   """simple docstring"""







   _UpperCamelCase				=  LayoutLMForSequenceClassification.from_pretrained("microsoft/layoutlm-base-uncased"				,      num_labels=2).to(
       lowercase_)

   _UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase				=  prepare_layoutlm_batch_inputs()

   # forward pass
   _UpperCamelCase				=  model(
       input_ids=lowercase_				,      bbox=lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_				,      labels=torch.tensor([1, 1]				,      device=lowercase_)				,      )

   # test whether we get a loss as a scalar
   _UpperCamelCase				=  outputs.loss
   _UpperCamelCase				=  torch.Size([])
   self.assertEqual(loss.shape				,      lowercase_)

   # test the shape of the logits
   _UpperCamelCase				=  outputs.logits
   _UpperCamelCase				=  torch.Size((2, 2))
   self.assertEqual(logits.shape				,      lowercase_)







 @slow
 def 						__UpperCAmelCase							(						self							:					Tuple)		->     Any:


   """simple docstring"""







   _UpperCamelCase				=  LayoutLMForTokenClassification.from_pretrained("microsoft/layoutlm-base-uncased"				,      num_labels=13).to(
       lowercase_)

   _UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase				=  prepare_layoutlm_batch_inputs()

   # forward pass
   _UpperCamelCase				=  model(
       input_ids=lowercase_				,      bbox=lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_				,      labels=lowercase_)

   # test the loss calculation to be around 2.65
   # expected_loss = torch.tensor(2.65, device=torch_device)

   # The loss is currently somewhat random and can vary between 0.1-0.3 atol.
   # self.assertTrue(torch.allclose(outputs.loss, expected_loss, atol=0.1))

   # test the shape of the logits
   _UpperCamelCase				=  outputs.logits
   _UpperCamelCase				=  torch.Size((2, 25, 13))
   self.assertEqual(logits.shape				,      lowercase_)







 @slow
 def 						__UpperCAmelCase							(						self							:					Any)		->     int:


   """simple docstring"""







   _UpperCamelCase				=  LayoutLMForQuestionAnswering.from_pretrained("microsoft/layoutlm-base-uncased").to(lowercase_)

   _UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase				=  prepare_layoutlm_batch_inputs()

   # forward pass
   _UpperCamelCase				=  model(input_ids=lowercase_				,      bbox=lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_)

   # test the shape of the logits
   _UpperCamelCase				=  torch.Size((2, 25))
   self.assertEqual(outputs.start_logits.shape				,      lowercase_)
   self.assertEqual(outputs.end_logits.shape				,      lowercase_)




