



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 :							Tuple     ,  UpperCamelCase :							List[str]     ,  )->Union[str, Any]:
      __SCREAMING_SNAKE_CASE       :    int       =	parent
      __SCREAMING_SNAKE_CASE       :    Any       =	1_3
      __SCREAMING_SNAKE_CASE       :    Any       =	7
      __SCREAMING_SNAKE_CASE       :    Optional[int]       =	True
      __SCREAMING_SNAKE_CASE       :    Optional[int]       =	True
      __SCREAMING_SNAKE_CASE       :    Tuple       =	False
      __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	True
      __SCREAMING_SNAKE_CASE       :    Dict       =	9_9
      __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	3_2
      __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	2
      __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	4
      __SCREAMING_SNAKE_CASE       :    List[str]       =	3_7
      __SCREAMING_SNAKE_CASE       :    str       =	'''gelu'''
      __SCREAMING_SNAKE_CASE       :    Dict       =	0.1
      __SCREAMING_SNAKE_CASE       :    List[Any]       =	0.1
      __SCREAMING_SNAKE_CASE       :    List[str]       =	5_1_2
      __SCREAMING_SNAKE_CASE       :    Optional[int]       =	1_6
      __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	2
      __SCREAMING_SNAKE_CASE       :    List[str]       =	0.0_2
      __SCREAMING_SNAKE_CASE       :    Any       =	3
      __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	4
      __SCREAMING_SNAKE_CASE       :    int       =	None





     def 	__snake_case    (       self :							List[str]				)->Dict:
      __SCREAMING_SNAKE_CASE       :    Tuple       =	ids_tensor([self.batch_size, self.seq_length]     ,  self.vocab_size				)

      __SCREAMING_SNAKE_CASE       :    Any       =	None
      if self.use_input_mask:
       __SCREAMING_SNAKE_CASE       :    List[str]       =	random_attention_mask([self.batch_size, self.seq_length]				)

      __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	None
      __SCREAMING_SNAKE_CASE       :    Any       =	None
      __SCREAMING_SNAKE_CASE       :    str       =	None
      if self.use_labels:
       __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	ids_tensor([self.batch_size]     ,  self.type_sequence_label_size				)
       __SCREAMING_SNAKE_CASE       :    Tuple       =	ids_tensor([self.batch_size, self.seq_length]     ,  self.num_labels				)
       __SCREAMING_SNAKE_CASE       :    List[Any]       =	ids_tensor([self.batch_size]     ,  self.num_choices				)

      __SCREAMING_SNAKE_CASE       :    Dict       =	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 	__snake_case    (       self :							Any     ,  UpperCamelCase :							Dict     ,  UpperCamelCase :							List[str]     ,  UpperCamelCase :							Any     ,  UpperCamelCase :							List[str]     ,  UpperCamelCase :							Dict     ,  UpperCamelCase :							Optional[Any]				)->Union[str, Any]:
      __SCREAMING_SNAKE_CASE       :    int       =	TFDistilBertModel(config=UpperCamelCase				)
      __SCREAMING_SNAKE_CASE       :    int       =	{'''input_ids''': input_ids, '''attention_mask''': input_mask}

      __SCREAMING_SNAKE_CASE       :    List[str]       =	model(UpperCamelCase				)

      __SCREAMING_SNAKE_CASE       :    str       =	[input_ids, input_mask]

      __SCREAMING_SNAKE_CASE       :    Tuple       =	model(UpperCamelCase				)

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





     def 	__snake_case    (       self :							List[Any]     ,  UpperCamelCase :							Any     ,  UpperCamelCase :							int     ,  UpperCamelCase :							Dict     ,  UpperCamelCase :							List[str]     ,  UpperCamelCase :							List[str]     ,  UpperCamelCase :							Optional[Any]				)->Tuple:
      __SCREAMING_SNAKE_CASE       :    List[str]       =	TFDistilBertForMaskedLM(config=UpperCamelCase				)
      __SCREAMING_SNAKE_CASE       :    int       =	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
      __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	model(UpperCamelCase				)
      self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.seq_length, self.vocab_size)				)





     def 	__snake_case    (       self :							List[Any]     ,  UpperCamelCase :							int     ,  UpperCamelCase :							Union[str, Any]     ,  UpperCamelCase :							Union[str, Any]     ,  UpperCamelCase :							Union[str, Any]     ,  UpperCamelCase :							List[Any]     ,  UpperCamelCase :							Optional[int]				)->Any:
      __SCREAMING_SNAKE_CASE       :    str       =	TFDistilBertForQuestionAnswering(config=UpperCamelCase				)
      __SCREAMING_SNAKE_CASE       :    int       =	{
          '''input_ids''': input_ids,
          '''attention_mask''': input_mask,
      }
      __SCREAMING_SNAKE_CASE       :    List[str]       =	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 :							str     ,  UpperCamelCase :							int     ,  UpperCamelCase :							List[str]     ,  UpperCamelCase :							List[Any]     ,  UpperCamelCase :							Tuple     ,  UpperCamelCase :							Optional[Any]     ,  UpperCamelCase :							Dict				)->Optional[int]:
      __SCREAMING_SNAKE_CASE       :    Dict       =	self.num_labels
      __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	TFDistilBertForSequenceClassification(UpperCamelCase				)
      __SCREAMING_SNAKE_CASE       :    str       =	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
      __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	model(UpperCamelCase				)
      self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.num_labels)				)





     def 	__snake_case    (       self :							int     ,  UpperCamelCase :							str     ,  UpperCamelCase :							str     ,  UpperCamelCase :							Any     ,  UpperCamelCase :							str     ,  UpperCamelCase :							int     ,  UpperCamelCase :							Dict				)->List[str]:
      __SCREAMING_SNAKE_CASE       :    List[Any]       =	self.num_choices
      __SCREAMING_SNAKE_CASE       :    Optional[int]       =	TFDistilBertForMultipleChoice(UpperCamelCase				)
      __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	tf.tile(tf.expand_dims(UpperCamelCase     ,  1				)     ,  (1, self.num_choices, 1)				)
      __SCREAMING_SNAKE_CASE       :    List[Any]       =	tf.tile(tf.expand_dims(UpperCamelCase     ,  1				)     ,  (1, self.num_choices, 1)				)
      __SCREAMING_SNAKE_CASE       :    Tuple       =	{
          '''input_ids''': multiple_choice_inputs_ids,
          '''attention_mask''': multiple_choice_input_mask,
      }
      __SCREAMING_SNAKE_CASE       :    Tuple       =	model(UpperCamelCase				)
      self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.num_choices)				)





     def 	__snake_case    (       self :							List[Any]     ,  UpperCamelCase :							Optional[Any]     ,  UpperCamelCase :							Tuple     ,  UpperCamelCase :							Optional[Any]     ,  UpperCamelCase :							Dict     ,  UpperCamelCase :							List[Any]     ,  UpperCamelCase :							Union[str, Any]				)->Dict:
      __SCREAMING_SNAKE_CASE       :    Dict       =	self.num_labels
      __SCREAMING_SNAKE_CASE       :    Tuple       =	TFDistilBertForTokenClassification(UpperCamelCase				)
      __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
      __SCREAMING_SNAKE_CASE       :    str       =	model(UpperCamelCase				)
      self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.seq_length, self.num_labels)				)






     def 	__snake_case    (       self :							List[str]				)->Dict:
      __SCREAMING_SNAKE_CASE       :    int       =	self.prepare_config_and_inputs()
      (__SCREAMING_SNAKE_CASE)       :    Union[str, Any]       =	config_and_inputs
      __SCREAMING_SNAKE_CASE       :    Tuple       =	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
      return config, inputs_dict






@require_tf
class       _SCREAMING_SNAKE_CASE	(__A			,    __A			,    unittest.TestCase ):
     lowerCAmelCase					=			(
         (
             TFDistilBertModel,
             TFDistilBertForMaskedLM,
             TFDistilBertForQuestionAnswering,
             TFDistilBertForSequenceClassification,
             TFDistilBertForTokenClassification,
             TFDistilBertForMultipleChoice,
         )
         if is_tf_available()
         else None
     )
     lowerCAmelCase					=			(
         {
             """feature-extraction""": TFDistilBertModel,
             """fill-mask""": TFDistilBertForMaskedLM,
             """question-answering""": TFDistilBertForQuestionAnswering,
             """text-classification""": TFDistilBertForSequenceClassification,
             """token-classification""": TFDistilBertForTokenClassification,
             """zero-shot""": TFDistilBertForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     lowerCAmelCase					=			False
     lowerCAmelCase					=			False





     def 	__snake_case    (       self :							Dict				)->str:
      __SCREAMING_SNAKE_CASE       :    str       =	TFDistilBertModelTester(self				)
      __SCREAMING_SNAKE_CASE       :    int       =	ConfigTester(self     ,  config_class=UpperCamelCase     ,  dim=3_7				)





     def 	__snake_case    (       self :							Union[str, Any]				)->Dict:
      self.config_tester.run_common_tests()





     def 	__snake_case    (       self :							Optional[Any]				)->Tuple:
      __SCREAMING_SNAKE_CASE       :    List[Any]       =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_model(*UpperCamelCase				)





     def 	__snake_case    (       self :							Tuple				)->Any:
      __SCREAMING_SNAKE_CASE       :    str       =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCamelCase				)





     def 	__snake_case    (       self :							Optional[int]				)->List[Any]:
      __SCREAMING_SNAKE_CASE       :    Any       =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCamelCase				)





     def 	__snake_case    (       self :							Any				)->str:
      __SCREAMING_SNAKE_CASE       :    List[Any]       =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCamelCase				)





     def 	__snake_case    (       self :							Optional[Any]				)->List[str]:
      __SCREAMING_SNAKE_CASE       :    str       =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCamelCase				)





     def 	__snake_case    (       self :							str				)->Union[str, Any]:
      __SCREAMING_SNAKE_CASE       :    str       =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCamelCase				)






     @slow
     def 	__snake_case    (       self :							List[Any]				)->Dict:

      for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]				):
       __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	TFDistilBertModel.from_pretrained(UpperCamelCase				)
       self.assertIsNotNone(UpperCamelCase				)






@require_tf
class       _SCREAMING_SNAKE_CASE	(unittest.TestCase ):





     @slow
     def 	__snake_case    (       self :							List[str]				)->List[Any]:
      __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	TFDistilBertModel.from_pretrained("distilbert-base-uncased"				)
      __SCREAMING_SNAKE_CASE       :    Tuple       =	tf.constant([[0, 1, 2, 3, 4, 5]]				)
      __SCREAMING_SNAKE_CASE       :    List[Any]       =	model(UpperCamelCase				)[0]

      __SCREAMING_SNAKE_CASE       :    str       =	[1, 6, 7_6_8]
      self.assertEqual(output.shape     ,  UpperCamelCase				)

      __SCREAMING_SNAKE_CASE       :    Optional[int]       =	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]     ,  UpperCamelCase     ,  atol=1E-4				)











from collections.abc import Generator
from math import sin






def 	_lowerCAmelCase						(				__lowerCamelCase   :     bytes     ):



 """simple docstring"""





 if len(__lowerCamelCase     ) != 32:
  raise ValueError("Input must be of length 32"     )

 __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	b""
 for i in [3, 2, 1, 0]:
  little_endian += string_aa[8 * i : 8 * i + 8]
 return little_endian






def 	_lowerCAmelCase						(				__lowerCamelCase   :     int     ):



 """simple docstring"""





 if i < 0:
  raise ValueError("Input must be non-negative"     )

 __SCREAMING_SNAKE_CASE       :    Any       =	format(__lowerCamelCase		,	"08x"     )[-8:]
 __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	b""
 for i in [3, 2, 1, 0]:
  little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode("utf-8"     )
 return little_endian_hex






def 	_lowerCAmelCase						(				__lowerCamelCase   :     bytes     ):



 """simple docstring"""





 __SCREAMING_SNAKE_CASE       :    Any       =	b""
 for char in message:
  bit_string += format(__lowerCamelCase		,	"08b"     ).encode("utf-8"     )
 __SCREAMING_SNAKE_CASE       :    List[str]       =	format(len(__lowerCamelCase     )		,	"064b"     ).encode("utf-8"     )

 # Pad bit_string to a multiple of 512 chars
 bit_string += b"1"
 while len(__lowerCamelCase     ) % 512 != 448:
  bit_string += b"0"
 bit_string += to_little_endian(start_len[32:]     ) + to_little_endian(start_len[:32]     )

 return bit_string






def 	_lowerCAmelCase						(				__lowerCamelCase   :     bytes     ):



 """simple docstring"""





 if len(__lowerCamelCase     ) % 512 != 0:
  raise ValueError("Input must have length that's a multiple of 512"     )

 for pos in range(0		,	len(__lowerCamelCase     )		,	512     ):
  __SCREAMING_SNAKE_CASE       :    int       =	bit_string[pos : pos + 512]
  __SCREAMING_SNAKE_CASE       :    Tuple       =	[]
  for i in range(0		,	512		,	32     ):
   block_words.append(int(to_little_endian(block[i : i + 32]     )		,	2     )     )
  yield block_words






def 	_lowerCAmelCase						(				__lowerCamelCase   :     int     ):



 """simple docstring"""





 if i < 0:
  raise ValueError("Input must be non-negative"     )

 __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	format(__lowerCamelCase		,	"032b"     )
 __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	""
 for c in i_str:
  new_str += "1" if c == "0" else "0"
 return int(__lowerCamelCase		,	2     )






def 	_lowerCAmelCase						(				__lowerCamelCase   :     int		,	__lowerCamelCase   :     int     ):



 """simple docstring"""





 return (a + b) % 2**32






def 	_lowerCAmelCase						(				__lowerCamelCase   :     int		,	__lowerCamelCase   :     int     ):



 """simple docstring"""





 if i < 0:
  raise ValueError("Input must be non-negative"     )
 if shift < 0:
  raise ValueError("Shift must be non-negative"     )
 return ((i << shift) ^ (i >> (32 - shift))) % 2**32






def 	_lowerCAmelCase						(				__lowerCamelCase   :     bytes     ):



 """simple docstring"""





 __SCREAMING_SNAKE_CASE       :    Tuple       =	preprocess(__lowerCamelCase     )

 __SCREAMING_SNAKE_CASE       :    List[Any]       =	[int(2**32 * abs(sin(i + 1     )     )     ) for i in range(64     )]

 # Starting states
 __SCREAMING_SNAKE_CASE       :    Tuple       =	0X67452301
 __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	0Xefcdab89
 __SCREAMING_SNAKE_CASE       :    Optional[int]       =	0X98badcfe
 __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	0X10325476

 __SCREAMING_SNAKE_CASE       :    List[Any]       =	[
     7,
     12,
     17,
     22,
     7,
     12,
     17,
     22,
     7,
     12,
     17,
     22,
     7,
     12,
     17,
     22,
     5,
     9,
     14,
     20,
     5,
     9,
     14,
     20,
     5,
     9,
     14,
     20,
     5,
     9,
     14,
     20,
     4,
     11,
     16,
     23,
     4,
     11,
     16,
     23,
     4,
     11,
     16,
     23,
     4,
     11,
     16,
     23,
     6,
     10,
     15,
     21,
     6,
     10,
     15,
     21,
     6,
     10,
     15,
     21,
     6,
     10,
     15,
     21,
 ]

 # Process bit string in chunks, each with 16 32-char words
 for block_words in get_block_words(__lowerCamelCase     ):
  __SCREAMING_SNAKE_CASE       :    Any       =	aa
  __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	ba
  __SCREAMING_SNAKE_CASE       :    str       =	ca
  __SCREAMING_SNAKE_CASE       :    Dict       =	da

  # Hash current chunk
  for i in range(64     ):
   if i <= 15:
    # f = (b & c) | (not_32(b) & d)     # Alternate definition for f
    __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	d ^ (b & (c ^ d))
    __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	i
   elif i <= 31:
    # f = (d & b) | (not_32(d) & c)     # Alternate definition for f
    __SCREAMING_SNAKE_CASE       :    int       =	c ^ (d & (b ^ c))
    __SCREAMING_SNAKE_CASE       :    int       =	(5 * i + 1) % 16
   elif i <= 47:
    __SCREAMING_SNAKE_CASE       :    List[str]       =	b ^ c ^ d
    __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	(3 * i + 5) % 16
   else:
    __SCREAMING_SNAKE_CASE       :    Any       =	c ^ (b | not_aa(__lowerCamelCase     ))
    __SCREAMING_SNAKE_CASE       :    str       =	(7 * i) % 16
   __SCREAMING_SNAKE_CASE       :    List[str]       =	(f + a + added_consts[i] + block_words[g]) % 2**32
   __SCREAMING_SNAKE_CASE       :    Dict       =	d
   __SCREAMING_SNAKE_CASE       :    str       =	c
   __SCREAMING_SNAKE_CASE       :    Tuple       =	b
   __SCREAMING_SNAKE_CASE       :    Optional[int]       =	sum_aa(__lowerCamelCase		,	left_rotate_aa(__lowerCamelCase		,	shift_amounts[i]     )     )

  # Add hashed chunk to running total
  __SCREAMING_SNAKE_CASE       :    Dict       =	sum_aa(__lowerCamelCase		,	__lowerCamelCase     )
  __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	sum_aa(__lowerCamelCase		,	__lowerCamelCase     )
  __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	sum_aa(__lowerCamelCase		,	__lowerCamelCase     )
  __SCREAMING_SNAKE_CASE       :    str       =	sum_aa(__lowerCamelCase		,	__lowerCamelCase     )

 __SCREAMING_SNAKE_CASE       :    List[str]       =	reformat_hex(__lowerCamelCase     ) + reformat_hex(__lowerCamelCase     ) + reformat_hex(__lowerCamelCase     ) + reformat_hex(__lowerCamelCase     )
 return digest


if __name__ == "__main__":
      import doctest

      doctest.testmod()






