





from __future__ import annotations

import unittest

from transformers import DebertaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import tensorflow as tf

							from transformers import (
							    TFDebertaForMaskedLM,
							    TFDebertaForQuestionAnswering,
							    TFDebertaForSequenceClassification,
							    TFDebertaForTokenClassification,
							    TFDebertaModel,
							)

class    __A  :


				def __init__(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_=13 ,			UpperCAmelCase_=7 ,			UpperCAmelCase_=True ,			UpperCAmelCase_=True ,			UpperCAmelCase_=True ,			UpperCAmelCase_=True ,			UpperCAmelCase_=99 ,			UpperCAmelCase_=32 ,			UpperCAmelCase_=2 ,			UpperCAmelCase_=4 ,			UpperCAmelCase_=37 ,			UpperCAmelCase_="gelu" ,			UpperCAmelCase_=0.1 ,			UpperCAmelCase_=0.1 ,			UpperCAmelCase_=512 ,			UpperCAmelCase_=16 ,			UpperCAmelCase_=2 ,			UpperCAmelCase_=0.0_2 ,			UpperCAmelCase_=3 ,			UpperCAmelCase_=4 ,			UpperCAmelCase_=None ,			):
								lowerCamelCase									=parent
								lowerCamelCase									=13
								lowerCamelCase									=7
								lowerCamelCase									=True
								lowerCamelCase									=True
								lowerCamelCase									=True
								lowerCamelCase									=True
								lowerCamelCase									=99
								lowerCamelCase									=32
								lowerCamelCase									=2
								lowerCamelCase									=4
								lowerCamelCase									=37
								lowerCamelCase									="""gelu"""
								lowerCamelCase									=0.1
								lowerCamelCase									=0.1
								lowerCamelCase									=512
								lowerCamelCase									=16
								lowerCamelCase									=False
								lowerCamelCase									=-1
								lowerCamelCase									=True
								lowerCamelCase									=2
								lowerCamelCase									=0.0_2
								lowerCamelCase									=3
								lowerCamelCase									=4
								lowerCamelCase									=None


				def _snake_case							(		self				):
								lowerCamelCase									=ids_tensor([self.batch_size, self.seq_length] ,			self.vocab_size				)

								lowerCamelCase									=None
								if self.use_input_mask:
												lowerCamelCase									=random_attention_mask([self.batch_size, self.seq_length]				)

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

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

								lowerCamelCase									=DebertaConfig(
								    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 ,			relative_attention=self.relative_attention ,			max_relative_positions=self.max_relative_positions ,			position_biased_input=self.position_biased_input ,			initializer_range=self.initializer_range ,			return_dict=UpperCAmelCase_ ,			)

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


				def _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_				):
								lowerCamelCase									=TFDebertaModel(config=UpperCAmelCase_				)
								lowerCamelCase									={"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

								lowerCamelCase									=[input_ids, input_mask]
								lowerCamelCase									=model(UpperCAmelCase_				)

								lowerCamelCase									=model(UpperCAmelCase_				)

								self.parent.assertEqual(result.last_hidden_state.shape ,			(self.batch_size, self.seq_length, self.hidden_size)				)


				def _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_				):
								lowerCamelCase									=TFDebertaForMaskedLM(config=UpperCAmelCase_				)
								lowerCamelCase									={
								    """input_ids""": input_ids,
								    """attention_mask""": input_mask,
								    """token_type_ids""": token_type_ids,
								}
								lowerCamelCase									=model(UpperCAmelCase_				)
								self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.seq_length, self.vocab_size)				)


				def _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_				):
								lowerCamelCase									=self.num_labels
								lowerCamelCase									=TFDebertaForSequenceClassification(config=UpperCAmelCase_				)
								lowerCamelCase									={
								    """input_ids""": input_ids,
								    """attention_mask""": input_mask,
								    """token_type_ids""": token_type_ids,
								}

								lowerCamelCase									=model(UpperCAmelCase_				)
								self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.num_labels)				)


				def _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_				):
								lowerCamelCase									=self.num_labels
								lowerCamelCase									=TFDebertaForTokenClassification(config=UpperCAmelCase_				)
								lowerCamelCase									={
								    """input_ids""": input_ids,
								    """attention_mask""": input_mask,
								    """token_type_ids""": token_type_ids,
								}
								lowerCamelCase									=model(UpperCAmelCase_				)
								self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.seq_length, self.num_labels)				)


				def _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_				):
								lowerCamelCase									=TFDebertaForQuestionAnswering(config=UpperCAmelCase_				)
								lowerCamelCase									={
								    """input_ids""": input_ids,
								    """attention_mask""": input_mask,
								    """token_type_ids""": token_type_ids,
								}

								lowerCamelCase									=model(UpperCAmelCase_				)
								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 _snake_case							(		self				):
								lowerCamelCase									=self.prepare_config_and_inputs()
								(
								    (
								    lowerCamelCase
								)			,	(
								    lowerCamelCase
								)			,	(
								    lowerCamelCase
								)			,	(
								    lowerCamelCase
								)			,	(
								    lowerCamelCase
								)			,	(
								    lowerCamelCase
								)			,	(
								    lowerCamelCase
								)			,	
								)									=config_and_inputs
								lowerCamelCase									={"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
								return config, inputs_dict




@require_tf
class    __A  (    a     ,       a     ,       unittest.TestCase     ):
				__A  =					(
				    (
				        TFDebertaModel,
				        TFDebertaForMaskedLM,
				        TFDebertaForQuestionAnswering,
				        TFDebertaForSequenceClassification,
				        TFDebertaForTokenClassification,
				    )
				    if is_tf_available()
				    else ()
				)
				__A  =					(
				    {
				        """feature-extraction""": TFDebertaModel,
				        """fill-mask""": TFDebertaForMaskedLM,
				        """question-answering""": TFDebertaForQuestionAnswering,
				        """text-classification""": TFDebertaForSequenceClassification,
				        """token-classification""": TFDebertaForTokenClassification,
				        """zero-shot""": TFDebertaForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)

				__A  =					False
				__A  =					False


				def _snake_case							(		self				):
								lowerCamelCase									=TFDebertaModelTester(self				)
								lowerCamelCase									=ConfigTester(self ,			config_class=UpperCAmelCase_ ,			hidden_size=37				)


				def _snake_case							(		self				):
								self.config_tester.run_common_tests()


				def _snake_case							(		self				):
								lowerCamelCase									=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*UpperCAmelCase_				)


				def _snake_case							(		self				):
								lowerCamelCase									=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase_				)


				def _snake_case							(		self				):
								lowerCamelCase									=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase_				)


				def _snake_case							(		self				):
								lowerCamelCase									=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase_				)


				def _snake_case							(		self				):
								lowerCamelCase									=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase_				)


				@slow
				def _snake_case							(		self				):
								lowerCamelCase									=TFDebertaModel.from_pretrained("""kamalkraj/deberta-base"""				)
								self.assertIsNotNone(UpperCAmelCase_				)




@require_tf
class    __A  (    unittest.TestCase     ):


				@unittest.skip(reason="""Model not available yet"""				)
				def _snake_case							(		self				):
								pass


				@slow
				def _snake_case							(		self				):
								lowerCamelCase									=TFDebertaModel.from_pretrained("""kamalkraj/deberta-base"""				)
								lowerCamelCase									=tf.constant([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]				)
								lowerCamelCase									=tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
								lowerCamelCase									=model(UpperCAmelCase_ ,			attention_mask=UpperCAmelCase_				)[0]

								lowerCamelCase									=tf.constant(
								    [
								        [
								            [-0.5_9_8_5_5_8_9_6, -0.8_0_5_5_2_5_6_6, -0.8_4_6_2_1_3_5],
								            [1.4_4_8_4_0_2_5, -0.9_3_4_8_3_7_9_4, -0.8_0_5_9_3_0_8_5],
								            [0.3_1_2_2_7_4_1, 0.0_0_3_1_6_0_5_9, -1.4_1_3_1_3_7_7],
								        ]
								    ]				)
								tf.debugging.assert_near(output[:, 1:4, 1:4] ,			UpperCAmelCase_ ,			atol=1E-4				)












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


UpperCAmelCase__    :      int       					=logging.get_logger(__name__)

UpperCAmelCase__    :      Optional[Any]       					={'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''}

UpperCAmelCase__    :      List[str]       					={
    '''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'''
        ),
    },
}

UpperCAmelCase__    :      Dict       					={
    '''microsoft/layoutlm-base-uncased''': 5_12,
    '''microsoft/layoutlm-large-uncased''': 5_12,
}

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

class    __A  (    a     ):
				__A  =					VOCAB_FILES_NAMES
				__A  =					PRETRAINED_VOCAB_FILES_MAP
				__A  =					PRETRAINED_INIT_CONFIGURATION
				__A  =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				__A  =					LayoutLMTokenizer


				def __init__(		self ,			UpperCAmelCase_=None ,			UpperCAmelCase_=None ,			UpperCAmelCase_=True ,			UpperCAmelCase_="[UNK]" ,			UpperCAmelCase_="[SEP]" ,			UpperCAmelCase_="[PAD]" ,			UpperCAmelCase_="[CLS]" ,			UpperCAmelCase_="[MASK]" ,			UpperCAmelCase_=True ,			UpperCAmelCase_=None ,			**UpperCAmelCase_ ,			):
								super().__init__(
								    UpperCAmelCase_ ,			tokenizer_file=UpperCAmelCase_ ,			do_lower_case=UpperCAmelCase_ ,			unk_token=UpperCAmelCase_ ,			sep_token=UpperCAmelCase_ ,			pad_token=UpperCAmelCase_ ,			cls_token=UpperCAmelCase_ ,			mask_token=UpperCAmelCase_ ,			tokenize_chinese_chars=UpperCAmelCase_ ,			strip_accents=UpperCAmelCase_ ,			**UpperCAmelCase_ ,			)

								lowerCamelCase									=json.loads(self.backend_tokenizer.normalizer.__getstate__()				)
								if (
								    normalizer_state.get("""lowercase""" ,			UpperCAmelCase_				) != do_lower_case
								    or normalizer_state.get("""strip_accents""" ,			UpperCAmelCase_				) != strip_accents
								    or normalizer_state.get("""handle_chinese_chars""" ,			UpperCAmelCase_				) != tokenize_chinese_chars
								):
												lowerCamelCase									=getattr(UpperCAmelCase_ ,			normalizer_state.pop("""type"""				)				)
												lowerCamelCase									=do_lower_case
												lowerCamelCase									=strip_accents
												lowerCamelCase									=tokenize_chinese_chars
												lowerCamelCase									=normalizer_class(**UpperCAmelCase_				)

								lowerCamelCase									=do_lower_case


				def _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_=None				):
								lowerCamelCase									=[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 _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ = None				):
								lowerCamelCase									=[self.sep_token_id]
								lowerCamelCase									=[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 _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ = None				):
								lowerCamelCase									=self._tokenizer.model.save(UpperCAmelCase_ ,			name=UpperCAmelCase_				)
								return tuple(UpperCAmelCase_				)





