





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





						def __init__(			self	,							A_	,							):
							'''simple docstring'''




							UpperCamelCase     : Any	     =       parent
							UpperCamelCase     : Union[str, Any]	     =       13
							UpperCamelCase     : Union[str, Any]	     =       7
							UpperCamelCase     : List[str]	     =       True
							UpperCamelCase     : str	     =       True
							UpperCamelCase     : str	     =       False
							UpperCamelCase     : Union[str, Any]	     =       True
							UpperCamelCase     : Any	     =       99
							UpperCamelCase     : Any	     =       32
							UpperCamelCase     : int	     =       2
							UpperCamelCase     : Tuple	     =       4
							UpperCamelCase     : Tuple	     =       37
							UpperCamelCase     : Optional[Any]	     =       'gelu'
							UpperCamelCase     : Optional[int]	     =       0.1
							UpperCamelCase     : Optional[int]	     =       0.1
							UpperCamelCase     : Optional[Any]	     =       512
							UpperCamelCase     : str	     =       16
							UpperCamelCase     : Optional[Any]	     =       2
							UpperCamelCase     : Any	     =       0.02
							UpperCamelCase     : int	     =       3
							UpperCamelCase     : Dict	     =       4
							UpperCamelCase     : List[Any]	     =       None





						def 	__UpperCamelCase(			self       ):
							'''simple docstring'''




							UpperCamelCase     : Any	     =       ids_tensor([self.batch_size, self.seq_length]	,							self.vocab_size       )

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

							UpperCamelCase     : List[str]	     =       None
							UpperCamelCase     : Dict	     =       None
							UpperCamelCase     : Dict	     =       None
							if self.use_labels:
								UpperCamelCase     : List[Any]	     =       ids_tensor([self.batch_size]	,							self.type_sequence_label_size       )
								UpperCamelCase     : int	     =       ids_tensor([self.batch_size, self.seq_length]	,							self.num_labels       )
								UpperCamelCase     : List[str]	     =       ids_tensor([self.batch_size]	,							self.num_choices       )

							UpperCamelCase     : 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 	__UpperCamelCase(			self	,							A_	,							A_	,							A_	,							A_	,							A_	,							A_       ):
							'''simple docstring'''




							UpperCamelCase     : Tuple	     =       TFDistilBertModel(config=_A       )
							UpperCamelCase     : Union[str, Any]	     =       {'input_ids': input_ids, 'attention_mask': input_mask}

							UpperCamelCase     : Optional[int]	     =       model(_A       )

							UpperCamelCase     : Union[str, Any]	     =       [input_ids, input_mask]

							UpperCamelCase     : Any	     =       model(_A       )

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





						def 	__UpperCamelCase(			self	,							A_	,							A_	,							A_	,							A_	,							A_	,							A_       ):
							'''simple docstring'''




							UpperCamelCase     : Optional[Any]	     =       TFDistilBertForMaskedLM(config=_A       )
							UpperCamelCase     : Union[str, Any]	     =       {'input_ids': input_ids, 'attention_mask': input_mask}
							UpperCamelCase     : Dict	     =       model(_A       )
							self.parent.assertEqual(result.logits.shape	,							(self.batch_size, self.seq_length, self.vocab_size)       )





						def 	__UpperCamelCase(			self	,							A_	,							A_	,							A_	,							A_	,							A_	,							A_       ):
							'''simple docstring'''




							UpperCamelCase     : int	     =       TFDistilBertForQuestionAnswering(config=_A       )
							UpperCamelCase     : str	     =       {
							    'input_ids': input_ids,
							    'attention_mask': input_mask,
							}
							UpperCamelCase     : Dict	     =       model(_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 	__UpperCamelCase(			self	,							A_	,							A_	,							A_	,							A_	,							A_	,							A_       ):
							'''simple docstring'''




							UpperCamelCase     : int	     =       self.num_labels
							UpperCamelCase     : str	     =       TFDistilBertForSequenceClassification(_A       )
							UpperCamelCase     : Any	     =       {'input_ids': input_ids, 'attention_mask': input_mask}
							UpperCamelCase     : Optional[int]	     =       model(_A       )
							self.parent.assertEqual(result.logits.shape	,							(self.batch_size, self.num_labels)       )





						def 	__UpperCamelCase(			self	,							A_	,							A_	,							A_	,							A_	,							A_	,							A_       ):
							'''simple docstring'''




							UpperCamelCase     : str	     =       self.num_choices
							UpperCamelCase     : List[Any]	     =       TFDistilBertForMultipleChoice(_A       )
							UpperCamelCase     : int	     =       tf.tile(tf.expand_dims(_A	,							1       )	,							(1, self.num_choices, 1)       )
							UpperCamelCase     : Union[str, Any]	     =       tf.tile(tf.expand_dims(_A	,							1       )	,							(1, self.num_choices, 1)       )
							UpperCamelCase     : int	     =       {
							    'input_ids': multiple_choice_inputs_ids,
							    'attention_mask': multiple_choice_input_mask,
							}
							UpperCamelCase     : str	     =       model(_A       )
							self.parent.assertEqual(result.logits.shape	,							(self.batch_size, self.num_choices)       )





						def 	__UpperCamelCase(			self	,							A_	,							A_	,							A_	,							A_	,							A_	,							A_       ):
							'''simple docstring'''




							UpperCamelCase     : Any	     =       self.num_labels
							UpperCamelCase     : Optional[int]	     =       TFDistilBertForTokenClassification(_A       )
							UpperCamelCase     : int	     =       {'input_ids': input_ids, 'attention_mask': input_mask}
							UpperCamelCase     : Optional[Any]	     =       model(_A       )
							self.parent.assertEqual(result.logits.shape	,							(self.batch_size, self.seq_length, self.num_labels)       )





						def 	__UpperCamelCase(			self       ):
							'''simple docstring'''




							UpperCamelCase     : Union[str, Any]	     =       self.prepare_config_and_inputs()
							(UpperCamelCase)     : Optional[Any]	     =       config_and_inputs
							UpperCamelCase     : Tuple	     =       {'input_ids': input_ids, 'attention_mask': input_mask}
							return config, inputs_dict



@require_tf
class  A__       (	__snake_case	,       __snake_case	,       unittest.TestCase      ):
						_UpperCAmelCase     :Optional[Any]						   =       (
						    (
						        TFDistilBertModel,
						        TFDistilBertForMaskedLM,
						        TFDistilBertForQuestionAnswering,
						        TFDistilBertForSequenceClassification,
						        TFDistilBertForTokenClassification,
						        TFDistilBertForMultipleChoice,
						    )
						    if is_tf_available()
						    else None
						)
						_UpperCAmelCase     :str						   =       (
						    {
						        """feature-extraction""": TFDistilBertModel,
						        """fill-mask""": TFDistilBertForMaskedLM,
						        """question-answering""": TFDistilBertForQuestionAnswering,
						        """text-classification""": TFDistilBertForSequenceClassification,
						        """token-classification""": TFDistilBertForTokenClassification,
						        """zero-shot""": TFDistilBertForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)
						_UpperCAmelCase     :Tuple						   =       False
						_UpperCAmelCase     :List[str]						   =       False





						def 	__UpperCamelCase(			self       ):
							'''simple docstring'''




							UpperCamelCase     : Dict	     =       TFDistilBertModelTester(self       )
							UpperCamelCase     : List[Any]	     =       ConfigTester(self	,							config_class=_A	,							dim=37       )





						def 	__UpperCamelCase(			self       ):
							'''simple docstring'''




							self.config_tester.run_common_tests()





						def 	__UpperCamelCase(			self       ):
							'''simple docstring'''




							UpperCamelCase     : List[str]	     =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_model(*_A       )





						def 	__UpperCamelCase(			self       ):
							'''simple docstring'''




							UpperCamelCase     : Optional[int]	     =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_masked_lm(*_A       )





						def 	__UpperCamelCase(			self       ):
							'''simple docstring'''




							UpperCamelCase     : Dict	     =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_question_answering(*_A       )





						def 	__UpperCamelCase(			self       ):
							'''simple docstring'''




							UpperCamelCase     : Optional[Any]	     =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_sequence_classification(*_A       )





						def 	__UpperCamelCase(			self       ):
							'''simple docstring'''




							UpperCamelCase     : Union[str, Any]	     =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_multiple_choice(*_A       )





						def 	__UpperCamelCase(			self       ):
							'''simple docstring'''




							UpperCamelCase     : Union[str, Any]	     =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_token_classification(*_A       )





						@slow
						def 	__UpperCamelCase(			self       ):
							'''simple docstring'''




							for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]       ):
								UpperCamelCase     : int	     =       TFDistilBertModel.from_pretrained(_A       )
								self.assertIsNotNone(_A       )



@require_tf
class  A__       (	unittest.TestCase      ):





						@slow
						def 	__UpperCamelCase(			self       ):
							'''simple docstring'''




							UpperCamelCase     : Dict	     =       TFDistilBertModel.from_pretrained("distilbert-base-uncased"       )
							UpperCamelCase     : List[Any]	     =       tf.constant([[0, 1, 2, 3, 4, 5]]       )
							UpperCamelCase     : Dict	     =       model(_A       )[0]

							UpperCamelCase     : Optional[Any]	     =       [1, 6, 768]
							self.assertEqual(output.shape	,							_A       )

							UpperCamelCase     : str	     =       tf.constant(
							    [
							        [
							            [0.19_26_18_85, -0.13_73_29_55, 0.4_11_97_99],
							            [0.22_15_01_56, -0.07_42_26_61, 0.39_03_72_04],
							            [0.22_75_60_18, -0.0_89_64_14, 0.3_70_14_67],
							        ]
							    ]       )
							tf.debugging.assert_near(output[:, :3, :3]	,							_A	,							atol=1e-4       )







from __future__ import annotations



def 		A_    (					_lowerCAmelCase							,       _lowerCAmelCase					)						->			list[str]:
 if partitions <= 0:
  raise ValueError("partitions must be a positive number!"					)
 if partitions > number_of_bytes:
  raise ValueError("partitions can not > number_of_bytes!"					)
 UpperCamelCase     : str	     =       number_of_bytes // partitions
 UpperCamelCase     : List[Any]	     =       []
 for i in range(_lowerCAmelCase					):
  UpperCamelCase     : Optional[Any]	     =       i * bytes_per_partition + 1
  UpperCamelCase     : Any	     =       (
      number_of_bytes if i == partitions - 1 else (i + 1) * bytes_per_partition
  )
  allocation_list.append(F"""{start_bytes}-{end_bytes}"""					)
 return allocation_list


if __name__ == "__main__":
    import doctest

    doctest.testmod()
