
"""simple docstring"""

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 __lowerCAmelCase		:





     def __init__(     self     ,					__UpperCAmelCase     ,					):

      '''simple docstring'''




      __UpperCamelCase      =	parent
      __UpperCamelCase      =	13
      __UpperCamelCase      =	7
      __UpperCamelCase      =	True
      __UpperCamelCase      =	True
      __UpperCamelCase      =	False
      __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





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




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

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

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

      __UpperCamelCase      =	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 						UpperCAmelCase (     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				):

      '''simple docstring'''




      __UpperCamelCase      =	TFDistilBertModel(config=__UpperCAmelCase				)
      __UpperCamelCase      =	{'input_ids': input_ids, 'attention_mask': input_mask}

      __UpperCamelCase      =	model(__UpperCAmelCase				)

      __UpperCamelCase      =	[input_ids, input_mask]

      __UpperCamelCase      =	model(__UpperCAmelCase				)

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





     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				):

      '''simple docstring'''




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





     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				):

      '''simple docstring'''




      __UpperCamelCase      =	TFDistilBertForQuestionAnswering(config=__UpperCAmelCase				)
      __UpperCamelCase      =	{
          'input_ids': input_ids,
          'attention_mask': input_mask,
      }
      __UpperCamelCase      =	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 						UpperCAmelCase (     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				):

      '''simple docstring'''




      __UpperCamelCase      =	self.num_labels
      __UpperCamelCase      =	TFDistilBertForSequenceClassification(__UpperCAmelCase				)
      __UpperCamelCase      =	{'input_ids': input_ids, 'attention_mask': input_mask}
      __UpperCamelCase      =	model(__UpperCAmelCase				)
      self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, self.num_labels)				)





     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				):

      '''simple docstring'''




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





     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				):

      '''simple docstring'''




      __UpperCamelCase      =	self.num_labels
      __UpperCamelCase      =	TFDistilBertForTokenClassification(__UpperCAmelCase				)
      __UpperCamelCase      =	{'input_ids': input_ids, 'attention_mask': input_mask}
      __UpperCamelCase      =	model(__UpperCAmelCase				)
      self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, self.seq_length, self.num_labels)				)







     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




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


@require_tf
class __lowerCAmelCase		(				__SCREAMING_SNAKE_CASE      ,  __SCREAMING_SNAKE_CASE      ,  unittest.TestCase      ):
     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 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	TFDistilBertModelTester(self				)
      __UpperCamelCase      =	ConfigTester(self     ,					config_class=__UpperCAmelCase     ,					dim=37				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      self.config_tester.run_common_tests()





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_model(*__UpperCAmelCase				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_masked_lm(*__UpperCAmelCase				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_question_answering(*__UpperCAmelCase				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_sequence_classification(*__UpperCAmelCase				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_multiple_choice(*__UpperCAmelCase				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_token_classification(*__UpperCAmelCase				)







     @slow
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




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


@require_tf
class __lowerCAmelCase		(				unittest.TestCase      ):





     @slow
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	TFDistilBertModel.from_pretrained('distilbert-base-uncased'				)
      __UpperCamelCase      =	tf.constant([[0, 1, 2, 3, 4, 5]]				)
      __UpperCamelCase      =	model(__UpperCAmelCase				)[0]

      __UpperCamelCase      =	[1, 6, 768]
      self.assertEqual(output.shape     ,					__UpperCAmelCase				)

      __UpperCamelCase      =	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				)







"""simple docstring"""

def 							A						(    snake_case				:list[list[int]]    , snake_case				:int    , snake_case				:int    , snake_case				:list[int]		)				->     bool:

 # 1. Validate that path exists between current and next vertices
 if graph[path[curr_ind - 1]][next_ver] == 0:
  return False

 # 2. Validate that next vertex is not already in path
 return not any(vertex == next_ver for vertex in path		)






def 							A						(    snake_case				:list[list[int]]    , snake_case				:list[int]    , snake_case				:int		)				->     bool:

 # Base Case
 if curr_ind == len(snake_case		):
  # return whether path exists between current and starting vertices
  return graph[path[curr_ind - 1]][path[0]] == 1

 # Recursive Step
 for next_ver in range(0    , len(snake_case		)		):
  if valid_connection(snake_case    , snake_case    , snake_case    , snake_case		):
   # Insert current vertex  into path as next transition
   __UpperCamelCase      =	next_ver
   # Validate created path
   if util_hamilton_cycle(snake_case    , snake_case    , curr_ind + 1		):
    return True
   # Backtrack
   __UpperCamelCase      =	-1
 return False






def 							A						(    snake_case				:list[list[int]]    , snake_case				:int = 0		)				->     list[int]:
 __UpperCamelCase      =	[-1] * (len(snake_case		) + 1)
 # initialize start and end of path with starting index
 __UpperCamelCase      =	__UpperCamelCase      =	start_index
 # evaluate and if we find answer return path either return empty array
 return path if util_hamilton_cycle(snake_case    , snake_case    , 1		) else []



