



import os
import tempfile
import unittest

from transformers import DistilBertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

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


if is_torch_available():
							import torch

							from transformers import (
							    DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
							    DistilBertForMaskedLM,
							    DistilBertForMultipleChoice,
							    DistilBertForQuestionAnswering,
							    DistilBertForSequenceClassification,
							    DistilBertForTokenClassification,
							    DistilBertModel,
							)


class      __lowerCAmelCase     (		a__      ):
					"""simple docstring"""






					def __init__(     self					:       str				,					_snake_case					:       Dict				,					_snake_case					:       int=13				,					_snake_case					:       str=7				,					_snake_case					:       Tuple=True				,					_snake_case					:       Dict=True				,					_snake_case					:       int=False				,					_snake_case					:       Tuple=True				,					_snake_case					:       Union[str, Any]=99				,					_snake_case					:       Union[str, Any]=32				,					_snake_case					:       Tuple=5				,					_snake_case					:       int=4				,					_snake_case					:       Dict=37				,					_snake_case					:       Any="gelu"				,					_snake_case					:       Union[str, Any]=0.1				,					_snake_case					:       Any=0.1				,					_snake_case					:       List[Any]=5_12				,					_snake_case					:       Dict=16				,					_snake_case					:       Dict=2				,					_snake_case					:       List[Any]=0.02				,					_snake_case					:       int=3				,					_snake_case					:       Union[str, Any]=4				,					_snake_case					:       List[str]=None				,					):






								"""simple docstring"""


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



					def     _a							(     self					:       List[str]					):






								"""simple docstring"""


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

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

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

								A__       =    self.get_config()

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



					def     _a							(     self					:       Optional[int]					):






								"""simple docstring"""


								return 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				,					)



					def     _a							(     self					:       List[Any]				,					_snake_case					:       List[Any]				,					_snake_case					:       Dict				,					_snake_case					:       Union[str, Any]				,					_snake_case					:       Any				,					_snake_case					:       Union[str, Any]				,					_snake_case					:       Optional[Any]					):






								"""simple docstring"""


								A__       =    DistilBertModel(config=_A					)
								model.to(_A					)
								model.eval()
								A__       =    model(_A				,					_A					)
								A__       =    model(_A					)
								self.parent.assertEqual(result.last_hidden_state.shape				,					(self.batch_size, self.seq_length, self.hidden_size)					)



					def     _a							(     self					:       Optional[Any]				,					_snake_case					:       Optional[int]				,					_snake_case					:       int				,					_snake_case					:       Any				,					_snake_case					:       str				,					_snake_case					:       Tuple				,					_snake_case					:       Any					):






								"""simple docstring"""


								A__       =    DistilBertForMaskedLM(config=_A					)
								model.to(_A					)
								model.eval()
								A__       =    model(_A				,					attention_mask=_A				,					labels=_A					)
								self.parent.assertEqual(result.logits.shape				,					(self.batch_size, self.seq_length, self.vocab_size)					)



					def     _a							(     self					:       Optional[int]				,					_snake_case					:       str				,					_snake_case					:       Optional[int]				,					_snake_case					:       Union[str, Any]				,					_snake_case					:       Optional[int]				,					_snake_case					:       Optional[int]				,					_snake_case					:       Union[str, Any]					):






								"""simple docstring"""


								A__       =    DistilBertForQuestionAnswering(config=_A					)
								model.to(_A					)
								model.eval()
								A__       =    model(
								    _A				,					attention_mask=_A				,					start_positions=_A				,					end_positions=_A					)
								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     _a							(     self					:       int				,					_snake_case					:       List[str]				,					_snake_case					:       List[Any]				,					_snake_case					:       Union[str, Any]				,					_snake_case					:       Optional[int]				,					_snake_case					:       Tuple				,					_snake_case					:       Tuple					):






								"""simple docstring"""


								A__       =    self.num_labels
								A__       =    DistilBertForSequenceClassification(_A					)
								model.to(_A					)
								model.eval()
								A__       =    model(_A				,					attention_mask=_A				,					labels=_A					)
								self.parent.assertEqual(result.logits.shape				,					(self.batch_size, self.num_labels)					)



					def     _a							(     self					:       List[Any]				,					_snake_case					:       int				,					_snake_case					:       str				,					_snake_case					:       Any				,					_snake_case					:       int				,					_snake_case					:       Any				,					_snake_case					:       Optional[Any]					):






								"""simple docstring"""


								A__       =    self.num_labels
								A__       =    DistilBertForTokenClassification(config=_A					)
								model.to(_A					)
								model.eval()

								A__       =    model(_A				,					attention_mask=_A				,					labels=_A					)
								self.parent.assertEqual(result.logits.shape				,					(self.batch_size, self.seq_length, self.num_labels)					)



					def     _a							(     self					:       Optional[Any]				,					_snake_case					:       Optional[Any]				,					_snake_case					:       List[Any]				,					_snake_case					:       Union[str, Any]				,					_snake_case					:       List[str]				,					_snake_case					:       Optional[Any]				,					_snake_case					:       Optional[Any]					):






								"""simple docstring"""


								A__       =    self.num_choices
								A__       =    DistilBertForMultipleChoice(config=_A					)
								model.to(_A					)
								model.eval()
								A__       =    input_ids.unsqueeze(1					).expand(-1				,					self.num_choices				,					-1					).contiguous()
								A__       =    input_mask.unsqueeze(1					).expand(-1				,					self.num_choices				,					-1					).contiguous()
								A__       =    model(
								    _A				,					attention_mask=_A				,					labels=_A				,					)
								self.parent.assertEqual(result.logits.shape				,					(self.batch_size, self.num_choices)					)



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






								"""simple docstring"""


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







@require_torch
class      __lowerCAmelCase     (		a__							,     a__							,     unittest.TestCase      ):
					"""simple docstring"""






					A__				:		Optional[int]			       =    (
					    (
					        DistilBertModel,
					        DistilBertForMaskedLM,
					        DistilBertForMultipleChoice,
					        DistilBertForQuestionAnswering,
					        DistilBertForSequenceClassification,
					        DistilBertForTokenClassification,
					    )
					    if is_torch_available()
					    else None
					)
					A__				:		List[str]			       =    (
					    {
					        "feature-extraction": DistilBertModel,
					        "fill-mask": DistilBertForMaskedLM,
					        "question-answering": DistilBertForQuestionAnswering,
					        "text-classification": DistilBertForSequenceClassification,
					        "token-classification": DistilBertForTokenClassification,
					        "zero-shot": DistilBertForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					A__				:		int			       =    True
					A__				:		str			       =    True
					A__				:		Optional[int]			       =    True
					A__				:		Union[str, Any]			       =    True



					def     _a							(     self					:       Tuple					):






								"""simple docstring"""


								A__       =    DistilBertModelTester(self					)
								A__       =    ConfigTester(self				,					config_class=_A				,					dim=37					)



					def     _a							(     self					:       List[Any]					):






								"""simple docstring"""


								self.config_tester.run_common_tests()



					def     _a							(     self					:       List[Any]					):






								"""simple docstring"""


								A__       =    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_model(*_A					)



					def     _a							(     self					:       Optional[Any]					):






								"""simple docstring"""


								A__       =    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_masked_lm(*_A					)



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






								"""simple docstring"""


								A__       =    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_question_answering(*_A					)



					def     _a							(     self					:       Any					):






								"""simple docstring"""


								A__       =    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_sequence_classification(*_A					)



					def     _a							(     self					:       Optional[int]					):






								"""simple docstring"""


								A__       =    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_token_classification(*_A					)



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






								"""simple docstring"""


								A__       =    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_multiple_choice(*_A					)



					@slow
					def     _a							(     self					:       Optional[Any]					):






								"""simple docstring"""


								for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											A__       =    DistilBertModel.from_pretrained(_A					)
											self.assertIsNotNone(_A					)



					@slow
					@require_torch_gpu
					def     _a							(     self					:       Dict					):






								"""simple docstring"""


								A__       =    self.model_tester.prepare_config_and_inputs_for_common()
								for model_class in self.all_model_classes:
											# BertForMultipleChoice behaves incorrectly in JIT environments.
											if model_class == DistilBertForMultipleChoice:
														return

											A__       =    True
											A__       =    model_class(config=_A					)

											A__       =    self._prepare_for_class(_A				,					_A					)
											A__       =    torch.jit.trace(
											    _A				,					(inputs_dict['input_ids'].to('cpu'					), inputs_dict['attention_mask'].to('cpu'					))					)

											with tempfile.TemporaryDirectory() as tmp:
														torch.jit.save(_A				,					os.path.join(_A				,					'traced_model.pt'					)					)
														A__       =    torch.jit.load(os.path.join(_A				,					'traced_model.pt'					)				,					map_location=_A					)
														loaded(inputs_dict['input_ids'].to(_A					)				,					inputs_dict['attention_mask'].to(_A					)					)







@require_torch
class      __lowerCAmelCase     (		unittest.TestCase      ):
					"""simple docstring"""






					@slow
					def     _a							(     self					:       Any					):






								"""simple docstring"""


								A__       =    DistilBertModel.from_pretrained('distilbert-base-uncased'					)
								A__       =    torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]					)
								A__       =    torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
								with torch.no_grad():
											A__       =    model(_A				,					attention_mask=_A					)[0]
								A__       =    torch.Size((1, 11, 7_68)					)
								self.assertEqual(output.shape				,					_A					)
								A__       =    torch.tensor(
								    [[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]]					)

								self.assertTrue(torch.allclose(output[:, 1:4, 1:4]				,					_A				,					atol=1E-4					)					)









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

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


if is_sentencepiece_available():
							from .tokenization_rembert import RemBertTokenizer
else:
							SCREAMING_SNAKE_CASE__       =      None

SCREAMING_SNAKE_CASE__       =      logging.get_logger(__name__)
SCREAMING_SNAKE_CASE__       =      {'''vocab_file''': '''sentencepiece.model''', '''tokenizer_file''': '''tokenizer.json'''}

SCREAMING_SNAKE_CASE__       =      {
    '''vocab_file''': {
        '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/sentencepiece.model''',
    },
    '''tokenizer_file''': {
        '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/tokenizer.json''',
    },
}

SCREAMING_SNAKE_CASE__       =      {
    '''google/rembert''': 2_5_6,
}

SCREAMING_SNAKE_CASE__       =      '''▁'''


class      __lowerCAmelCase     (		UpperCAmelCase_      ):
					"""simple docstring"""






					A__				:		Any			       =    VOCAB_FILES_NAMES
					A__				:		str			       =    PRETRAINED_VOCAB_FILES_MAP
					A__				:		str			       =    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
					A__				:		int			       =    RemBertTokenizer



					def __init__(     self					:       Union[str, Any]				,					_snake_case					:       Any=None				,					_snake_case					:       Optional[Any]=None				,					_snake_case					:       Any=True				,					_snake_case					:       Optional[int]=True				,					_snake_case					:       Dict=False				,					_snake_case					:       Dict="[CLS]"				,					_snake_case					:       List[Any]="[SEP]"				,					_snake_case					:       Union[str, Any]="<unk>"				,					_snake_case					:       List[str]="[SEP]"				,					_snake_case					:       List[str]="<pad>"				,					_snake_case					:       str="[CLS]"				,					_snake_case					:       Any="[MASK]"				,					**_snake_case					:       Any				,					):






								"""simple docstring"""


								A__       =    AddedToken(_snake_case				,					lstrip=_snake_case				,					rstrip=_snake_case					) if isinstance(_snake_case				,					_snake_case					) else mask_token

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

								A__       =    do_lower_case
								A__       =    remove_space
								A__       =    keep_accents
								A__       =    vocab_file
								A__       =    False if not self.vocab_file else True



					def     _a							(     self					:       Any				,					_snake_case					:       List[int]				,					_snake_case					:       Optional[List[int]] = None					):






								"""simple docstring"""


								A__       =    [self.sep_token_id]
								A__       =    [self.cls_token_id]
								if token_ids_a is None:
											return cls + token_ids_a + sep
								return cls + token_ids_a + sep + token_ids_a + sep



					def     _a							(     self					:       Tuple				,					_snake_case					:       List[int]				,					_snake_case					:       Optional[List[int]] = None				,					_snake_case					:       bool = False					):






								"""simple docstring"""


								if already_has_special_tokens:
											if token_ids_a is not None:
														raise ValueError(
														    'You should not supply a second sequence if the provided sequence of '
														    'ids is already formatted with special tokens for the model.'					)
											return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a]

								if token_ids_a is not None:
											return [1] + ([0] * len(_snake_case					)) + [1] + ([0] * len(_snake_case					)) + [1]
								return [1] + ([0] * len(_snake_case					)) + [1]



					def     _a							(     self					:       Dict				,					_snake_case					:       List[int]				,					_snake_case					:       Optional[List[int]] = None					):






								"""simple docstring"""


								A__       =    [self.sep_token_id]
								A__       =    [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     _a							(     self					:       Any				,					_snake_case					:       str				,					_snake_case					:       Optional[str] = None					):






								"""simple docstring"""


								if not os.path.isdir(_snake_case					):
											logger.error('Vocabulary path ({}) should be a directory'.format(_snake_case					)					)
											return
								A__       =    os.path.join(
								    _snake_case				,					(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']					)

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

								return (out_vocab_file,)




