

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




  """simple docstring"""




  def __init__( self  :	List[Any]   ,lowercase__  :	Union[str, Any]   ,):
   __lowercase				    =					parent
   __lowercase				    =					1_3
   __lowercase				    =					7
   __lowercase				    =					True
   __lowercase				    =					True
   __lowercase				    =					False
   __lowercase				    =					True
   __lowercase				    =					9_9
   __lowercase				    =					3_2
   __lowercase				    =					2
   __lowercase				    =					4
   __lowercase				    =					3_7
   __lowercase				    =					'''gelu'''
   __lowercase				    =					0.1
   __lowercase				    =					0.1
   __lowercase				    =					5_1_2
   __lowercase				    =					1_6
   __lowercase				    =					2
   __lowercase				    =					0.0_2
   __lowercase				    =					3
   __lowercase				    =					4
   __lowercase				    =					None






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

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

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

   __lowercase				    =					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    SCREAMING_SNAKE_CASE ( self  :	int   ,lowercase__  :	str   ,lowercase__  :	Tuple   ,lowercase__  :	int   ,lowercase__  :	Optional[int]   ,lowercase__  :	Optional[Any]   ,lowercase__  :	str     ):
   __lowercase				    =					TFDistilBertModel(config=lowercase__     )
   __lowercase				    =					{'''input_ids''': input_ids, '''attention_mask''': input_mask}

   __lowercase				    =					model(lowercase__     )

   __lowercase				    =					[input_ids, input_mask]

   __lowercase				    =					model(lowercase__     )

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






  def    SCREAMING_SNAKE_CASE ( self  :	List[Any]   ,lowercase__  :	List[str]   ,lowercase__  :	int   ,lowercase__  :	Union[str, Any]   ,lowercase__  :	str   ,lowercase__  :	str   ,lowercase__  :	Union[str, Any]     ):
   __lowercase				    =					TFDistilBertForMaskedLM(config=lowercase__     )
   __lowercase				    =					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
   __lowercase				    =					model(lowercase__     )
   self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.seq_length, self.vocab_size)     )






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






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






  def    SCREAMING_SNAKE_CASE ( self  :	Tuple   ,lowercase__  :	Tuple   ,lowercase__  :	Tuple   ,lowercase__  :	List[str]   ,lowercase__  :	List[Any]   ,lowercase__  :	List[str]   ,lowercase__  :	int     ):
   __lowercase				    =					self.num_labels
   __lowercase				    =					TFDistilBertForTokenClassification(lowercase__     )
   __lowercase				    =					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
   __lowercase				    =					model(lowercase__     )
   self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.seq_length, self.num_labels)     )






  def    SCREAMING_SNAKE_CASE ( self  :	int     ):
   __lowercase				    =					self.prepare_config_and_inputs()
   ((__lowercase)    ,      (__lowercase)    ,      (__lowercase)    ,      (__lowercase)    ,      (__lowercase)    ,      (__lowercase))				    =					config_and_inputs
   __lowercase				    =					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
   return config, inputs_dict






@require_tf
class      lowercase_      (lowerCamelCase__							,			lowerCamelCase__							,			unittest.TestCase ):




  """simple docstring"""




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






  def    SCREAMING_SNAKE_CASE ( self  :	Dict     ):
   __lowercase				    =					TFDistilBertModelTester(self     )
   __lowercase				    =					ConfigTester(self   ,config_class=lowercase__   ,dim=3_7     )






  def    SCREAMING_SNAKE_CASE ( self  :	Any     ):
   self.config_tester.run_common_tests()






  def    SCREAMING_SNAKE_CASE ( self  :	Optional[Any]     ):
   __lowercase				    =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_distilbert_model(*lowercase__     )






  def    SCREAMING_SNAKE_CASE ( self  :	Union[str, Any]     ):
   __lowercase				    =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_distilbert_for_masked_lm(*lowercase__     )






  def    SCREAMING_SNAKE_CASE ( self  :	str     ):
   __lowercase				    =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_distilbert_for_question_answering(*lowercase__     )






  def    SCREAMING_SNAKE_CASE ( self  :	str     ):
   __lowercase				    =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowercase__     )






  def    SCREAMING_SNAKE_CASE ( self  :	List[str]     ):
   __lowercase				    =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowercase__     )






  def    SCREAMING_SNAKE_CASE ( self  :	List[Any]     ):
   __lowercase				    =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_distilbert_for_token_classification(*lowercase__     )






  @slow
  def    SCREAMING_SNAKE_CASE ( self  :	Dict     ):
   for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]     ):
    __lowercase				    =					TFDistilBertModel.from_pretrained(lowercase__     )
    self.assertIsNotNone(lowercase__     )






@require_tf
class      lowercase_      (unittest.TestCase ):




  """simple docstring"""




  @slow
  def    SCREAMING_SNAKE_CASE ( self  :	Union[str, Any]     ):
   __lowercase				    =					TFDistilBertModel.from_pretrained('''distilbert-base-uncased'''     )
   __lowercase				    =					tf.constant([[0, 1, 2, 3, 4, 5]]     )
   __lowercase				    =					model(lowercase__     )[0]

   __lowercase				    =					[1, 6, 7_6_8]
   self.assertEqual(output.shape   ,lowercase__     )

   __lowercase				    =					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]   ,lowercase__   ,atol=1e-4     )





'''simple docstring'''
import pytest
import requests

from datasets.utils.file_utils import http_head

from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline







@pytest.mark.integration
def       a_				(	):
							with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ):
														with pytest.raises(UpperCamelCase_ ):
																					requests.request("GET"						,  "https://huggingface.co" )
														with pytest.raises(requests.exceptions.ConnectTimeout ):
																					requests.request("GET"						,  "https://huggingface.co"						,  timeout=1.0 )







@pytest.mark.integration
def       a_				(	):
							with offline(OfflineSimulationMode.CONNECTION_FAILS ):
														with pytest.raises(requests.exceptions.ConnectionError ):
																					requests.request("GET"						,  "https://huggingface.co" )







def       a_				(	):
							with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ):
														with pytest.raises(UpperCamelCase_ ):
																					http_head("https://huggingface.co" )





