






from __future__ import annotations

import unittest

from transformers import DistilBertConfig, 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.models.distilbert.modeling_tf_distilbert import (
							    TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
							    TFDistilBertForMaskedLM,
							    TFDistilBertForMultipleChoice,
							    TFDistilBertForQuestionAnswering,
							    TFDistilBertForSequenceClassification,
							    TFDistilBertForTokenClassification,
							    TFDistilBertModel,
							)
class    _UpperCAmelCase      :

				"""simple docstring"""




				def __init__(  self		:					Union[str, Any],						lowerCamelCase		:					Optional[Any],						):

								'''simple docstring'''
								lowercase__     				=    parent
								lowercase__     				=    13
								lowercase__     				=    7
								lowercase__     				=    True
								lowercase__     				=    True
								lowercase__     				=    False
								lowercase__     				=    True
								lowercase__     				=    99
								lowercase__     				=    32
								lowercase__     				=    2
								lowercase__     				=    4
								lowercase__     				=    37
								lowercase__     				=    '''gelu'''
								lowercase__     				=    0.1
								lowercase__     				=    0.1
								lowercase__     				=    512
								lowercase__     				=    16
								lowercase__     				=    2
								lowercase__     				=    0.02
								lowercase__     				=    3
								lowercase__     				=    4
								lowercase__     				=    None




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

								'''simple docstring'''
								lowercase__     				=    ids_tensor([self.batch_size, self.seq_length],						self.vocab_size						)

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

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

								lowercase__     				=    DistilBertConfig(
								    vocab_size=self.vocab_size,						dim=self.hidden_size,						n_layers=self.num_hidden_layers,						n_heads=self.num_attention_heads,						hidden_dim=self.intermediate_size,						hidden_act=self.hidden_act,						dropout=self.hidden_dropout_prob,						attention_dropout=self.attention_probs_dropout_prob,						max_position_embeddings=self.max_position_embeddings,						initializer_range=self.initializer_range,						)

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




				def        lowercase__     (  self		:					Union[str, Any],						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Dict,						lowerCamelCase		:					int,						lowerCamelCase		:					List[str],						lowerCamelCase		:					int,						lowerCamelCase		:					List[str]						):

								'''simple docstring'''
								lowercase__     				=    TFDistilBertModel(config=UpperCamelCase__						)
								lowercase__     				=    {'''input_ids''': input_ids, '''attention_mask''': input_mask}

								lowercase__     				=    model(UpperCamelCase__						)

								lowercase__     				=    [input_ids, input_mask]

								lowercase__     				=    model(UpperCamelCase__						)

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




				def        lowercase__     (  self		:					int,						lowerCamelCase		:					Any,						lowerCamelCase		:					str,						lowerCamelCase		:					str,						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Any,						lowerCamelCase		:					Dict						):

								'''simple docstring'''
								lowercase__     				=    TFDistilBertForMaskedLM(config=UpperCamelCase__						)
								lowercase__     				=    {'''input_ids''': input_ids, '''attention_mask''': input_mask}
								lowercase__     				=    model(UpperCamelCase__						)
								self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.seq_length, self.vocab_size)						)




				def        lowercase__     (  self		:					Optional[int],						lowerCamelCase		:					Any,						lowerCamelCase		:					Union[str, Any],						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Union[str, Any],						lowerCamelCase		:					Dict,						lowerCamelCase		:					Optional[int]						):

								'''simple docstring'''
								lowercase__     				=    TFDistilBertForQuestionAnswering(config=UpperCamelCase__						)
								lowercase__     				=    {
								    '''input_ids''': input_ids,
								    '''attention_mask''': input_mask,
								}
								lowercase__     				=    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        lowercase__     (  self		:					Any,						lowerCamelCase		:					str,						lowerCamelCase		:					Optional[int],						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Tuple,						lowerCamelCase		:					List[str],						lowerCamelCase		:					str						):

								'''simple docstring'''
								lowercase__     				=    self.num_labels
								lowercase__     				=    TFDistilBertForSequenceClassification(UpperCamelCase__						)
								lowercase__     				=    {'''input_ids''': input_ids, '''attention_mask''': input_mask}
								lowercase__     				=    model(UpperCamelCase__						)
								self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.num_labels)						)




				def        lowercase__     (  self		:					str,						lowerCamelCase		:					Dict,						lowerCamelCase		:					Dict,						lowerCamelCase		:					int,						lowerCamelCase		:					List[Any],						lowerCamelCase		:					int,						lowerCamelCase		:					Union[str, Any]						):

								'''simple docstring'''
								lowercase__     				=    self.num_choices
								lowercase__     				=    TFDistilBertForMultipleChoice(UpperCamelCase__						)
								lowercase__     				=    tf.tile(tf.expand_dims(UpperCamelCase__,						1						),						(1, self.num_choices, 1)						)
								lowercase__     				=    tf.tile(tf.expand_dims(UpperCamelCase__,						1						),						(1, self.num_choices, 1)						)
								lowercase__     				=    {
								    '''input_ids''': multiple_choice_inputs_ids,
								    '''attention_mask''': multiple_choice_input_mask,
								}
								lowercase__     				=    model(UpperCamelCase__						)
								self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.num_choices)						)




				def        lowercase__     (  self		:					Any,						lowerCamelCase		:					int,						lowerCamelCase		:					Tuple,						lowerCamelCase		:					Any,						lowerCamelCase		:					Union[str, Any],						lowerCamelCase		:					Optional[int],						lowerCamelCase		:					List[str]						):

								'''simple docstring'''
								lowercase__     				=    self.num_labels
								lowercase__     				=    TFDistilBertForTokenClassification(UpperCamelCase__						)
								lowercase__     				=    {'''input_ids''': input_ids, '''attention_mask''': input_mask}
								lowercase__     				=    model(UpperCamelCase__						)
								self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.seq_length, self.num_labels)						)




				def        lowercase__     (  self		:					List[str]						):

								'''simple docstring'''
								lowercase__     				=    self.prepare_config_and_inputs()
								((lowercase__)				,		(lowercase__)				,		(lowercase__)				,		(lowercase__)				,		(lowercase__)				,		(lowercase__))     				=    config_and_inputs
								lowercase__     				=    {'''input_ids''': input_ids, '''attention_mask''': input_mask}
								return config, inputs_dict



@require_tf
class    _UpperCAmelCase      ( A__	,A__	,unittest.TestCase			):

				"""simple docstring"""




				lowercase__       							= (
				    (
				        TFDistilBertModel,
				        TFDistilBertForMaskedLM,
				        TFDistilBertForQuestionAnswering,
				        TFDistilBertForSequenceClassification,
				        TFDistilBertForTokenClassification,
				        TFDistilBertForMultipleChoice,
				    )
				    if is_tf_available()
				    else None
				)
				lowercase__       							= (
				    {
				        "feature-extraction": TFDistilBertModel,
				        "fill-mask": TFDistilBertForMaskedLM,
				        "question-answering": TFDistilBertForQuestionAnswering,
				        "text-classification": TFDistilBertForSequenceClassification,
				        "token-classification": TFDistilBertForTokenClassification,
				        "zero-shot": TFDistilBertForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				lowercase__       							= False
				lowercase__       							= False




				def        lowercase__     (  self		:					Tuple						):

								'''simple docstring'''
								lowercase__     				=    TFDistilBertModelTester(self						)
								lowercase__     				=    ConfigTester(self,						config_class=UpperCamelCase__,						dim=37						)




				def        lowercase__     (  self		:					Dict						):

								'''simple docstring'''
								self.config_tester.run_common_tests()




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

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_model(*UpperCamelCase__						)




				def        lowercase__     (  self		:					List[Any]						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCamelCase__						)




				def        lowercase__     (  self		:					Dict						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCamelCase__						)




				def        lowercase__     (  self		:					List[Any]						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCamelCase__						)




				def        lowercase__     (  self		:					Dict						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCamelCase__						)




				def        lowercase__     (  self		:					Any						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCamelCase__						)




				@slow
				def        lowercase__     (  self		:					str						):

								'''simple docstring'''





								for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]						):
												lowercase__     				=    TFDistilBertModel.from_pretrained(UpperCamelCase__						)
												self.assertIsNotNone(UpperCamelCase__						)



@require_tf
class    _UpperCAmelCase      ( unittest.TestCase			):

				"""simple docstring"""




				@slow
				def        lowercase__     (  self		:					Optional[int]						):

								'''simple docstring'''
								lowercase__     				=    TFDistilBertModel.from_pretrained('''distilbert-base-uncased'''						)
								lowercase__     				=    tf.constant([[0, 1, 2, 3, 4, 5]]						)
								lowercase__     				=    model(UpperCamelCase__						)[0]

								lowercase__     				=    [1, 6, 768]
								self.assertEqual(output.shape,						UpperCamelCase__						)

								lowercase__     				=    tf.constant(
								    [
								        [
								            [0.19261885, -0.13732955, 0.4119799],
								            [0.22150156, -0.07422661, 0.39037204],
								            [0.22756018, -0.0896414, 0.3701467],
								        ]
								    ]						)
								tf.debugging.assert_near(output[:, :3, :3],						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_bert import BertTokenizer


A__			:    Dict         =				logging.get_logger(__name__)

A__			:    Dict         =				{'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

A__			:    Optional[int]         =				{
    'vocab_file': {
        'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/vocab.txt',
        'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/vocab.txt',
        'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/vocab.txt',
        'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/vocab.txt',
        'bert-base-multilingual-uncased': (
            'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/vocab.txt'
        ),
        'bert-base-multilingual-cased': 'https://huggingface.co/bert-base-multilingual-cased/resolve/main/vocab.txt',
        'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/vocab.txt',
        'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/vocab.txt',
        'bert-large-uncased-whole-word-masking': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/vocab.txt'
        ),
        'bert-large-cased-whole-word-masking': (
            'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/vocab.txt'
        ),
        'bert-large-uncased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt'
        ),
        'bert-large-cased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt'
        ),
        'bert-base-cased-finetuned-mrpc': (
            'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/vocab.txt'
        ),
        'bert-base-german-dbmdz-cased': 'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/vocab.txt',
        'bert-base-german-dbmdz-uncased': (
            'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/vocab.txt'
        ),
        'TurkuNLP/bert-base-finnish-cased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/vocab.txt'
        ),
        'TurkuNLP/bert-base-finnish-uncased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/vocab.txt'
        ),
        'wietsedv/bert-base-dutch-cased': (
            'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/vocab.txt'
        ),
    },
    'tokenizer_file': {
        'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/tokenizer.json',
        'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/tokenizer.json',
        'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/tokenizer.json',
        'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/tokenizer.json',
        'bert-base-multilingual-uncased': (
            'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/tokenizer.json'
        ),
        'bert-base-multilingual-cased': (
            'https://huggingface.co/bert-base-multilingual-cased/resolve/main/tokenizer.json'
        ),
        'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/tokenizer.json',
        'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/tokenizer.json',
        'bert-large-uncased-whole-word-masking': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/tokenizer.json'
        ),
        'bert-large-cased-whole-word-masking': (
            'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/tokenizer.json'
        ),
        'bert-large-uncased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json'
        ),
        'bert-large-cased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json'
        ),
        'bert-base-cased-finetuned-mrpc': (
            'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/tokenizer.json'
        ),
        'bert-base-german-dbmdz-cased': (
            'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/tokenizer.json'
        ),
        'bert-base-german-dbmdz-uncased': (
            'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/tokenizer.json'
        ),
        'TurkuNLP/bert-base-finnish-cased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/tokenizer.json'
        ),
        'TurkuNLP/bert-base-finnish-uncased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/tokenizer.json'
        ),
        'wietsedv/bert-base-dutch-cased': (
            'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/tokenizer.json'
        ),
    },
}

A__			:    List[str]         =				{
    'bert-base-uncased': 5_12,
    'bert-large-uncased': 5_12,
    'bert-base-cased': 5_12,
    'bert-large-cased': 5_12,
    'bert-base-multilingual-uncased': 5_12,
    'bert-base-multilingual-cased': 5_12,
    'bert-base-chinese': 5_12,
    'bert-base-german-cased': 5_12,
    'bert-large-uncased-whole-word-masking': 5_12,
    'bert-large-cased-whole-word-masking': 5_12,
    'bert-large-uncased-whole-word-masking-finetuned-squad': 5_12,
    'bert-large-cased-whole-word-masking-finetuned-squad': 5_12,
    'bert-base-cased-finetuned-mrpc': 5_12,
    'bert-base-german-dbmdz-cased': 5_12,
    'bert-base-german-dbmdz-uncased': 5_12,
    'TurkuNLP/bert-base-finnish-cased-v1': 5_12,
    'TurkuNLP/bert-base-finnish-uncased-v1': 5_12,
    'wietsedv/bert-base-dutch-cased': 5_12,
}

A__			:    Optional[int]         =				{
    'bert-base-uncased': {'do_lower_case': True},
    'bert-large-uncased': {'do_lower_case': True},
    'bert-base-cased': {'do_lower_case': False},
    'bert-large-cased': {'do_lower_case': False},
    'bert-base-multilingual-uncased': {'do_lower_case': True},
    'bert-base-multilingual-cased': {'do_lower_case': False},
    'bert-base-chinese': {'do_lower_case': False},
    'bert-base-german-cased': {'do_lower_case': False},
    'bert-large-uncased-whole-word-masking': {'do_lower_case': True},
    'bert-large-cased-whole-word-masking': {'do_lower_case': False},
    'bert-large-uncased-whole-word-masking-finetuned-squad': {'do_lower_case': True},
    'bert-large-cased-whole-word-masking-finetuned-squad': {'do_lower_case': False},
    'bert-base-cased-finetuned-mrpc': {'do_lower_case': False},
    'bert-base-german-dbmdz-cased': {'do_lower_case': False},
    'bert-base-german-dbmdz-uncased': {'do_lower_case': True},
    'TurkuNLP/bert-base-finnish-cased-v1': {'do_lower_case': False},
    'TurkuNLP/bert-base-finnish-uncased-v1': {'do_lower_case': True},
    'wietsedv/bert-base-dutch-cased': {'do_lower_case': False},
}
class    _UpperCAmelCase      ( A__			):

				"""simple docstring"""




				lowercase__       							= VOCAB_FILES_NAMES
				lowercase__       							= PRETRAINED_VOCAB_FILES_MAP
				lowercase__       							= PRETRAINED_INIT_CONFIGURATION
				lowercase__       							= PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				lowercase__       							= BertTokenizer




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

								'''simple docstring'''
								super().__init__(
								    lowerCamelCase,						tokenizer_file=lowerCamelCase,						do_lower_case=lowerCamelCase,						unk_token=lowerCamelCase,						sep_token=lowerCamelCase,						pad_token=lowerCamelCase,						cls_token=lowerCamelCase,						mask_token=lowerCamelCase,						tokenize_chinese_chars=lowerCamelCase,						strip_accents=lowerCamelCase,						**lowerCamelCase,						)

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

								lowercase__     				=    do_lower_case




				def        lowercase__     (  self		:					Any,						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Dict=None						):

								'''simple docstring'''
								lowercase__     				=    [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[Any],						lowerCamelCase		:					List[int],						lowerCamelCase		:					Optional[List[int]] = None						):

								'''simple docstring'''
								lowercase__     				=    [self.sep_token_id]
								lowercase__     				=    [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		:					Any,						lowerCamelCase		:					str,						lowerCamelCase		:					Optional[str] = None						):

								'''simple docstring'''
								lowercase__     				=    self._tokenizer.model.save(lowerCamelCase,						name=lowerCamelCase						)
								return tuple(lowerCamelCase						)




