
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 					_SCREAMING_SNAKE_CASE  :
      def __init__(			self		,	lowerCamelCase		,	):
          snake_case__						=				parent
          snake_case__						=				13
          snake_case__						=				7
          snake_case__						=				True
          snake_case__						=				True
          snake_case__						=				False
          snake_case__						=				True
          snake_case__						=				99
          snake_case__						=				32
          snake_case__						=				2
          snake_case__						=				4
          snake_case__						=				37
          snake_case__						=				"gelu"
          snake_case__						=				0.1
          snake_case__						=				0.1
          snake_case__						=				5_12
          snake_case__						=				16
          snake_case__						=				2
          snake_case__						=				0.0_2
          snake_case__						=				3
          snake_case__						=				4
          snake_case__						=				None
      def A_			(			self ):
          snake_case__						=				ids_tensor([self.batch_size, self.seq_length]		,	self.vocab_size )

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

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

          snake_case__						=				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 A_			(			self		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase ):
          snake_case__						=				TFDistilBertModel(config=lowerCamelCase )
          snake_case__						=				{"input_ids": input_ids, "attention_mask": input_mask}

          snake_case__						=				model(lowerCamelCase )

          snake_case__						=				[input_ids, input_mask]

          snake_case__						=				model(lowerCamelCase )

          self.parent.assertEqual(result.last_hidden_state.shape		,	(self.batch_size, self.seq_length, self.hidden_size) )
      def A_			(			self		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase ):
          snake_case__						=				TFDistilBertForMaskedLM(config=lowerCamelCase )
          snake_case__						=				{"input_ids": input_ids, "attention_mask": input_mask}
          snake_case__						=				model(lowerCamelCase )
          self.parent.assertEqual(result.logits.shape		,	(self.batch_size, self.seq_length, self.vocab_size) )
      def A_			(			self		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase ):
          snake_case__						=				TFDistilBertForQuestionAnswering(config=lowerCamelCase )
          snake_case__						=				{
              "input_ids": input_ids,
              "attention_mask": input_mask,
          }
          snake_case__						=				model(lowerCamelCase )
          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		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase ):
          snake_case__						=				self.num_labels
          snake_case__						=				TFDistilBertForSequenceClassification(lowerCamelCase )
          snake_case__						=				{"input_ids": input_ids, "attention_mask": input_mask}
          snake_case__						=				model(lowerCamelCase )
          self.parent.assertEqual(result.logits.shape		,	(self.batch_size, self.num_labels) )
      def A_			(			self		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase ):
          snake_case__						=				self.num_choices
          snake_case__						=				TFDistilBertForMultipleChoice(lowerCamelCase )
          snake_case__						=				tf.tile(tf.expand_dims(lowerCamelCase		,	1 )		,	(1, self.num_choices, 1) )
          snake_case__						=				tf.tile(tf.expand_dims(lowerCamelCase		,	1 )		,	(1, self.num_choices, 1) )
          snake_case__						=				{
              "input_ids": multiple_choice_inputs_ids,
              "attention_mask": multiple_choice_input_mask,
          }
          snake_case__						=				model(lowerCamelCase )
          self.parent.assertEqual(result.logits.shape		,	(self.batch_size, self.num_choices) )
      def A_			(			self		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase ):
          snake_case__						=				self.num_labels
          snake_case__						=				TFDistilBertForTokenClassification(lowerCamelCase )
          snake_case__						=				{"input_ids": input_ids, "attention_mask": input_mask}
          snake_case__						=				model(lowerCamelCase )
          self.parent.assertEqual(result.logits.shape		,	(self.batch_size, self.seq_length, self.num_labels) )



      def A_			(			self ):
          snake_case__						=				self.prepare_config_and_inputs()
          ((snake_case__)						,					(snake_case__)						,					(snake_case__)						,					(snake_case__)						,					(snake_case__)						,					(snake_case__))						=				config_and_inputs
          snake_case__						=				{"input_ids": input_ids, "attention_mask": input_mask}
          return config, inputs_dict





@require_tf
class 					_SCREAMING_SNAKE_CASE  (      __UpperCamelCase       ,					__UpperCamelCase       ,					unittest.TestCase ):
      _A						:		str    	=     (
          (
              TFDistilBertModel,
              TFDistilBertForMaskedLM,
              TFDistilBertForQuestionAnswering,
              TFDistilBertForSequenceClassification,
              TFDistilBertForTokenClassification,
              TFDistilBertForMultipleChoice,
          )
          if is_tf_available()
          else None
      )
      _A						:		str    	=     (
          {
              'feature-extraction': TFDistilBertModel,
              'fill-mask': TFDistilBertForMaskedLM,
              'question-answering': TFDistilBertForQuestionAnswering,
              'text-classification': TFDistilBertForSequenceClassification,
              'token-classification': TFDistilBertForTokenClassification,
              'zero-shot': TFDistilBertForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      _A						:		Optional[Any]    	=     False
      _A						:		str    	=     False
      def A_			(			self ):
          snake_case__						=				TFDistilBertModelTester(self )
          snake_case__						=				ConfigTester(self		,	config_class=lowerCamelCase		,	dim=37 )
      def A_			(			self ):
          self.config_tester.run_common_tests()
      def A_			(			self ):
          snake_case__						=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_model(*lowerCamelCase )
      def A_			(			self ):
          snake_case__						=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_masked_lm(*lowerCamelCase )
      def A_			(			self ):
          snake_case__						=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_question_answering(*lowerCamelCase )
      def A_			(			self ):
          snake_case__						=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowerCamelCase )
      def A_			(			self ):
          snake_case__						=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowerCamelCase )
      def A_			(			self ):
          snake_case__						=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_token_classification(*lowerCamelCase )



      @slow
      def A_			(			self ):
          for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1] ):
              snake_case__						=				TFDistilBertModel.from_pretrained(lowerCamelCase )
              self.assertIsNotNone(lowerCamelCase )





@require_tf
class 					_SCREAMING_SNAKE_CASE  (      unittest.TestCase ):
      @slow
      def A_			(			self ):
          snake_case__						=				TFDistilBertModel.from_pretrained("distilbert-base-uncased" )
          snake_case__						=				tf.constant([[0, 1, 2, 3, 4, 5]] )
          snake_case__						=				model(lowerCamelCase )[0]

          snake_case__						=				[1, 6, 7_68]
          self.assertEqual(output.shape		,	lowerCamelCase )

          snake_case__						=				tf.constant(
              [
                  [
                      [0.1_9_2_6_1_8_8_5, -0.1_3_7_3_2_9_5_5, 0.4_1_1_9_7_9_9],
                      [0.2_2_1_5_0_1_5_6, -0.0_7_4_2_2_6_6_1, 0.3_9_0_3_7_2_0_4],
                      [0.2_2_7_5_6_0_1_8, -0.0_8_9_6_4_1_4, 0.3_7_0_1_4_6_7],
                  ]
              ] )
          tf.debugging.assert_near(output[:, :3, :3]		,	lowerCamelCase		,	atol=1e-4 )





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

import sentencepiece as spm

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


__magic_name__            =  logging.get_logger(__name__)

__magic_name__            =  {'''vocab_file''': '''sentencepiece.model'''}

__magic_name__            =  {
    '''vocab_file''': {
        '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/sentencepiece.model''',
    },
}

__magic_name__            =  {
    '''google/rembert''': 256,
}




class 					_SCREAMING_SNAKE_CASE  (      __UpperCamelCase ):
      _A						:		Optional[Any]    	=     VOCAB_FILES_NAMES
      _A						:		Optional[int]    	=     PRETRAINED_VOCAB_FILES_MAP
      _A						:		Any    	=     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      def __init__(			self		,	lowerCamelCase		,	lowerCamelCase=False		,	lowerCamelCase=True		,	lowerCamelCase=True		,	lowerCamelCase="[CLS]"		,	lowerCamelCase="[SEP]"		,	lowerCamelCase="[UNK]"		,	lowerCamelCase="[SEP]"		,	lowerCamelCase="[PAD]"		,	lowerCamelCase="[CLS]"		,	lowerCamelCase="[MASK]"		,	**lowerCamelCase		,	):
          super().__init__(
              do_lower_case=lowerCamelCase		,	remove_space=lowerCamelCase		,	keep_accents=lowerCamelCase		,	bos_token=lowerCamelCase		,	eos_token=lowerCamelCase		,	unk_token=lowerCamelCase		,	sep_token=lowerCamelCase		,	pad_token=lowerCamelCase		,	cls_token=lowerCamelCase		,	mask_token=lowerCamelCase		,	**lowerCamelCase		,	)

          snake_case__						=				do_lower_case
          snake_case__						=				remove_space
          snake_case__						=				keep_accents
          snake_case__						=				vocab_file

          snake_case__						=				spm.SentencePieceProcessor()
          self.sp_model.Load(lowerCamelCase )
      @property
      def A_			(			self ):
          return len(self.sp_model )
      def A_			(			self ):
          snake_case__						=				{self.convert_ids_to_tokens(lowerCamelCase ): i for i in range(self.vocab_size )}
          vocab.update(self.added_tokens_encoder )
          return vocab
      def __getstate__(			self ):
          snake_case__						=				self.__dict__.copy()
          snake_case__						=				None
          return state
      def __setstate__(			self		,	lowerCamelCase ):
          snake_case__						=				d
          snake_case__						=				spm.SentencePieceProcessor()
          self.sp_model.Load(self.vocab_file )
      def A_			(			self		,	lowerCamelCase		,	lowerCamelCase=False ):
          snake_case__						=				self.sp_model.EncodeAsPieces(lowerCamelCase )
          return pieces
      def A_			(			self		,	lowerCamelCase ):
          return self.sp_model.PieceToId(lowerCamelCase )
      def A_			(			self		,	lowerCamelCase ):
          return self.sp_model.IdToPiece(lowerCamelCase )
      def A_			(			self		,	lowerCamelCase ):
          snake_case__						=				self.sp_model.decode_pieces(lowerCamelCase )
          return out_string
      def A_			(			self		,	lowerCamelCase		,	lowerCamelCase = None ):
          snake_case__						=				[self.sep_token_id]
          snake_case__						=				[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		,	lowerCamelCase		,	lowerCamelCase = None		,	lowerCamelCase = False ):

          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(lowerCamelCase )) + [1] + ([0] * len(lowerCamelCase )) + [1]
          return [1] + ([0] * len(lowerCamelCase )) + [1]
      def A_			(			self		,	lowerCamelCase		,	lowerCamelCase = None ):
          snake_case__						=				[self.sep_token_id]
          snake_case__						=				[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		,	lowerCamelCase		,	lowerCamelCase = None ):
          if not os.path.isdir(lowerCamelCase ):
              logger.error("Vocabulary path ({}) should be a directory".format(lowerCamelCase ) )
              return
          snake_case__						=				os.path.join(
              lowerCamelCase		,	(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] )

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

          return (out_vocab_file,)



