


"""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      __A     :





							'''simple docstring'''

							def __init__(				self							:      Union[str, Any]		,       UpperCAmelCase_							:      Optional[int]		,       )					->Tuple:



														"""simple docstring"""



														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_           =		512
														snake_case_           =		16
														snake_case_           =		2
														snake_case_           =		0.02
														snake_case_           =		3
														snake_case_           =		4
														snake_case_           =		None



							def 		lowerCAmelCase       (				self							:      Tuple       )					->List[str]:



														"""simple docstring"""



														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 		lowerCAmelCase       (				self							:      int		,       UpperCAmelCase_							:      str		,       UpperCAmelCase_							:      Dict		,       UpperCAmelCase_							:      Union[str, Any]		,       UpperCAmelCase_							:      str		,       UpperCAmelCase_							:      List[str]		,       UpperCAmelCase_							:      int       )					->str:



														"""simple docstring"""



														snake_case_           =		TFDistilBertModel(config=UpperCAmelCase_       )
														snake_case_           =		{"""input_ids""": input_ids, """attention_mask""": input_mask}

														snake_case_           =		model(UpperCAmelCase_       )

														snake_case_           =		[input_ids, input_mask]

														snake_case_           =		model(UpperCAmelCase_       )

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



							def 		lowerCAmelCase       (				self							:      List[Any]		,       UpperCAmelCase_							:      Union[str, Any]		,       UpperCAmelCase_							:      List[Any]		,       UpperCAmelCase_							:      Any		,       UpperCAmelCase_							:      Dict		,       UpperCAmelCase_							:      str		,       UpperCAmelCase_							:      Union[str, Any]       )					->int:



														"""simple docstring"""



														snake_case_           =		TFDistilBertForMaskedLM(config=UpperCAmelCase_       )
														snake_case_           =		{"""input_ids""": input_ids, """attention_mask""": input_mask}
														snake_case_           =		model(UpperCAmelCase_       )
														self.parent.assertEqual(result.logits.shape		,       (self.batch_size, self.seq_length, self.vocab_size)       )



							def 		lowerCAmelCase       (				self							:      Union[str, Any]		,       UpperCAmelCase_							:      int		,       UpperCAmelCase_							:      str		,       UpperCAmelCase_							:      Optional[int]		,       UpperCAmelCase_							:      Optional[Any]		,       UpperCAmelCase_							:      Union[str, Any]		,       UpperCAmelCase_							:      str       )					->Optional[int]:



														"""simple docstring"""



														snake_case_           =		TFDistilBertForQuestionAnswering(config=UpperCAmelCase_       )
														snake_case_           =		{
														    """input_ids""": input_ids,
														    """attention_mask""": input_mask,
														}
														snake_case_           =		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 		lowerCAmelCase       (				self							:      Optional[int]		,       UpperCAmelCase_							:      str		,       UpperCAmelCase_							:      int		,       UpperCAmelCase_							:      List[str]		,       UpperCAmelCase_							:      List[str]		,       UpperCAmelCase_							:      Optional[Any]		,       UpperCAmelCase_							:      Optional[Any]       )					->int:



														"""simple docstring"""



														snake_case_           =		self.num_labels
														snake_case_           =		TFDistilBertForSequenceClassification(UpperCAmelCase_       )
														snake_case_           =		{"""input_ids""": input_ids, """attention_mask""": input_mask}
														snake_case_           =		model(UpperCAmelCase_       )
														self.parent.assertEqual(result.logits.shape		,       (self.batch_size, self.num_labels)       )



							def 		lowerCAmelCase       (				self							:      Union[str, Any]		,       UpperCAmelCase_							:      Any		,       UpperCAmelCase_							:      Optional[Any]		,       UpperCAmelCase_							:      Optional[int]		,       UpperCAmelCase_							:      Optional[int]		,       UpperCAmelCase_							:      Tuple		,       UpperCAmelCase_							:      Dict       )					->int:



														"""simple docstring"""



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



							def 		lowerCAmelCase       (				self							:      int		,       UpperCAmelCase_							:      Dict		,       UpperCAmelCase_							:      Optional[Any]		,       UpperCAmelCase_							:      Dict		,       UpperCAmelCase_							:      Tuple		,       UpperCAmelCase_							:      Optional[Any]		,       UpperCAmelCase_							:      str       )					->List[str]:



														"""simple docstring"""



														snake_case_           =		self.num_labels
														snake_case_           =		TFDistilBertForTokenClassification(UpperCAmelCase_       )
														snake_case_           =		{"""input_ids""": input_ids, """attention_mask""": input_mask}
														snake_case_           =		model(UpperCAmelCase_       )
														self.parent.assertEqual(result.logits.shape		,       (self.batch_size, self.seq_length, self.num_labels)       )



							def 		lowerCAmelCase       (				self							:      str       )					->Tuple:



														"""simple docstring"""



														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      __A     (snake_case__						,		snake_case__						,		unittest.TestCase):





							'''simple docstring'''

							__lowercase:		str							      =    (
							    (
							        TFDistilBertModel,
							        TFDistilBertForMaskedLM,
							        TFDistilBertForQuestionAnswering,
							        TFDistilBertForSequenceClassification,
							        TFDistilBertForTokenClassification,
							        TFDistilBertForMultipleChoice,
							    )
							    if is_tf_available()
							    else None
							)
							__lowercase:		int							      =    (
							    {
							        """feature-extraction""": TFDistilBertModel,
							        """fill-mask""": TFDistilBertForMaskedLM,
							        """question-answering""": TFDistilBertForQuestionAnswering,
							        """text-classification""": TFDistilBertForSequenceClassification,
							        """token-classification""": TFDistilBertForTokenClassification,
							        """zero-shot""": TFDistilBertForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							__lowercase:		Any							      =    False
							__lowercase:		Optional[int]							      =    False



							def 		lowerCAmelCase       (				self							:      Any       )					->Tuple:



														"""simple docstring"""



														snake_case_           =		TFDistilBertModelTester(self       )
														snake_case_           =		ConfigTester(self		,       config_class=UpperCAmelCase_		,       dim=37       )



							def 		lowerCAmelCase       (				self							:      Optional[Any]       )					->Tuple:



														"""simple docstring"""



														self.config_tester.run_common_tests()



							def 		lowerCAmelCase       (				self							:      Dict       )					->Tuple:



														"""simple docstring"""



														snake_case_           =		self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_distilbert_model(*UpperCAmelCase_       )



							def 		lowerCAmelCase       (				self							:      Optional[int]       )					->List[str]:



														"""simple docstring"""



														snake_case_           =		self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCAmelCase_       )



							def 		lowerCAmelCase       (				self							:      Union[str, Any]       )					->Dict:



														"""simple docstring"""



														snake_case_           =		self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCAmelCase_       )



							def 		lowerCAmelCase       (				self							:      Any       )					->Dict:



														"""simple docstring"""



														snake_case_           =		self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCAmelCase_       )



							def 		lowerCAmelCase       (				self							:      Optional[int]       )					->str:



														"""simple docstring"""



														snake_case_           =		self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCAmelCase_       )



							def 		lowerCAmelCase       (				self							:      Any       )					->Dict:



														"""simple docstring"""



														snake_case_           =		self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCAmelCase_       )



							@slow
							def 		lowerCAmelCase       (				self							:      List[Any]       )					->Optional[int]:



														"""simple docstring"""



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





@require_tf
class      __A     (unittest.TestCase):





							'''simple docstring'''

							@slow
							def 		lowerCAmelCase       (				self							:      Union[str, Any]       )					->List[str]:



														"""simple docstring"""



														snake_case_           =		TFDistilBertModel.from_pretrained("""distilbert-base-uncased"""       )
														snake_case_           =		tf.constant([[0, 1, 2, 3, 4, 5]]       )
														snake_case_           =		model(UpperCAmelCase_       )[0]

														snake_case_           =		[1, 6, 768]
														self.assertEqual(output.shape		,       UpperCAmelCase_       )

														snake_case_           =		tf.constant(
														    [
														        [
														            [0.19_261_885, -0.13_732_955, 0.4_119_799],
														            [0.22_150_156, -0.07_422_661, 0.39_037_204],
														            [0.22_756_018, -0.0_896_414, 0.3_701_467],
														        ]
														    ]       )
														tf.debugging.assert_near(output[:, :3, :3]		,       UpperCAmelCase_		,       atol=1E-4       )







"""simple docstring"""
def 						_a				(      _SCREAMING_SNAKE_CASE				, _SCREAMING_SNAKE_CASE				, _SCREAMING_SNAKE_CASE				, _SCREAMING_SNAKE_CASE				, _SCREAMING_SNAKE_CASE				)    ->				int:
							if index == number_of_items:
														return 0
							snake_case_           =		0
							snake_case_           =		0
							snake_case_           =		knapsack(_SCREAMING_SNAKE_CASE				, _SCREAMING_SNAKE_CASE				, _SCREAMING_SNAKE_CASE				, _SCREAMING_SNAKE_CASE				, index + 1				)
							if weights[index] <= max_weight:
														snake_case_           =		values[index] + knapsack(
														    _SCREAMING_SNAKE_CASE				, _SCREAMING_SNAKE_CASE				, _SCREAMING_SNAKE_CASE				, max_weight - weights[index]				, index + 1				)
							return max(_SCREAMING_SNAKE_CASE				, _SCREAMING_SNAKE_CASE				)


if __name__ == "__main__":
				import doctest

				doctest.testmod()



