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



							def __init__(						self				,    lowercase_				,    ):
													_snake_case						:					Any    =   parent
													_snake_case						:					Tuple    =   13
													_snake_case						:					int    =   7
													_snake_case						:					Any    =   True
													_snake_case						:					Dict    =   True
													_snake_case						:					str    =   False
													_snake_case						:					Any    =   True
													_snake_case						:					int    =   99
													_snake_case						:					int    =   32
													_snake_case						:					int    =   2
													_snake_case						:					Any    =   4
													_snake_case						:					str    =   37
													_snake_case						:					List[str]    =   "gelu"
													_snake_case						:					str    =   0.1
													_snake_case						:					Optional[int]    =   0.1
													_snake_case						:					List[Any]    =   512
													_snake_case						:					Optional[int]    =   16
													_snake_case						:					List[str]    =   2
													_snake_case						:					Dict    =   0.02
													_snake_case						:					Any    =   3
													_snake_case						:					Dict    =   4
													_snake_case						:					Optional[int]    =   None



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Optional[int]    =   ids_tensor([self.batch_size, self.seq_length]				,    self.vocab_size    )

													_snake_case						:					Dict    =   None
													if self.use_input_mask:
																			_snake_case						:					int    =   random_attention_mask([self.batch_size, self.seq_length]    )

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

													_snake_case						:					int    =   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				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					Optional[Any]    =   TFDistilBertModel(config=lowercase_    )
													_snake_case						:					str    =   {"input_ids": input_ids, "attention_mask": input_mask}

													_snake_case						:					List[str]    =   model(lowercase_    )

													_snake_case						:					List[Any]    =   [input_ids, input_mask]

													_snake_case						:					Optional[int]    =   model(lowercase_    )

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



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					Tuple    =   TFDistilBertForMaskedLM(config=lowercase_    )
													_snake_case						:					Tuple    =   {"input_ids": input_ids, "attention_mask": input_mask}
													_snake_case						:					Tuple    =   model(lowercase_    )
													self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.seq_length, self.vocab_size)    )



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



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



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					int    =   self.num_labels
													_snake_case						:					Union[str, Any]    =   TFDistilBertForTokenClassification(lowercase_    )
													_snake_case						:					Dict    =   {"input_ids": input_ids, "attention_mask": input_mask}
													_snake_case						:					str    =   model(lowercase_    )
													self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.seq_length, self.num_labels)    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Optional[Any]    =   self.prepare_config_and_inputs()
													((_snake_case)    ,(_snake_case)    ,(_snake_case)    ,(_snake_case)    ,(_snake_case)    ,(_snake_case))						:					Tuple    =   config_and_inputs
													_snake_case						:					Dict    =   {"input_ids": input_ids, "attention_mask": input_mask}
													return config, inputs_dict




@require_tf
class        lowercase_   (					__snake_case						,			__snake_case						,			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 	UpperCamelCase	(						self    ):
													_snake_case						:					int    =   TFDistilBertModelTester(self    )
													_snake_case						:					Dict    =   ConfigTester(self				,    config_class=lowercase_				,    dim=37    )



							def 	UpperCamelCase	(						self    ):
													self.config_tester.run_common_tests()



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Optional[int]    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_distilbert_model(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Optional[Any]    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_distilbert_for_masked_lm(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Optional[Any]    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_distilbert_for_question_answering(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Tuple    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Optional[Any]    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					List[Any]    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_distilbert_for_token_classification(*lowercase_    )



							@slow
							def 	UpperCamelCase	(						self    ):
													for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]    ):
																			_snake_case						:					int    =   TFDistilBertModel.from_pretrained(lowercase_    )
																			self.assertIsNotNone(lowercase_    )




@require_tf
class        lowercase_   (					unittest.TestCase		):



							@slow
							def 	UpperCamelCase	(						self    ):
													_snake_case						:					str    =   TFDistilBertModel.from_pretrained("distilbert-base-uncased"    )
													_snake_case						:					Dict    =   tf.constant([[0, 1, 2, 3, 4, 5]]    )
													_snake_case						:					Tuple    =   model(lowercase_    )[0]

													_snake_case						:					Dict    =   [1, 6, 768]
													self.assertEqual(output.shape				,    lowercase_    )

													_snake_case						:					List[Any]    =   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]				,    lowercase_				,    atol=1e-4    )
import importlib
import shutil
import threading
import warnings
from typing import List

import fsspec
import fsspec.asyn

from . import compression
from .hffilesystem import HfFileSystem


__SCREAMING_SNAKE_CASE						:   Any         =			importlib.util.find_spec('s3fs') is not None

if _has_safs:
				from .safilesystem import SaFileSystem  # noqa: F401

__SCREAMING_SNAKE_CASE						:   List[compression.BaseCompressedFileFileSystem]         =			[
    compression.BzaFileSystem,
    compression.GzipFileSystem,
    compression.LzaFileSystem,
    compression.XzFileSystem,
    compression.ZstdFileSystem,
]

# Register custom filesystems
for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]:
				if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class:
								warnings.warn(F'''A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.''')
				fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True)
def  snake_case						(__lowercase						)	->	str:

						'''simple docstring'''



						if "://" in dataset_path:
												_snake_case						:					Tuple    =   dataset_path.split("://"						)[1]
						return dataset_path
def  snake_case						(__lowercase						)	->	bool:

						'''simple docstring'''



						if fs is not None and fs.protocol != "file":
												return True
						else:
												return False
def  snake_case						(__lowercase ,      __lowercase ,      __lowercase						)	->	str:

						'''simple docstring'''



						_snake_case						:					Optional[int]    =   not is_remote_filesystem(__lowercase						)
						if is_local:
												# LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory
												shutil.move(fs._strip_protocol(__lowercase						) ,      fs._strip_protocol(__lowercase						)						)
						else:
												fs.mv(__lowercase ,      __lowercase ,      recursive=__lowercase						)

def  snake_case						()	->	None:

						'''simple docstring'''



						if hasattr(fsspec.asyn ,      "reset_lock"						):
												# for future fsspec>2022.05.0
												fsspec.asyn.reset_lock()
						else:
												_snake_case						:					List[str]    =   None
												_snake_case						:					str    =   None
												_snake_case						:					List[Any]    =   threading.Lock()