






from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch
@require_torch
@require_tf
class        a       (					lowercase__  ):



      """simple docstring"""


      def       UpperCAmelCase ( self   :     Tuple   )				-> Union[str, Any]:
        __UpperCAmelCase				:      Tuple	 =    SMALL_MODEL_IDENTIFIER
        __UpperCAmelCase				:      List[Any]	 =    """pt"""
        __UpperCAmelCase				:      List[Any]	 =    """tf"""
      def       UpperCAmelCase ( self   :     Tuple						, __lowercase   :     List[str]   )				-> Union[str, Any]:
        __UpperCAmelCase				:      Dict	 =    AutoModel.from_pretrained(self.test_model   )
        model_pt.save_pretrained(__lowercase   )
      def       UpperCAmelCase ( self   :     Any						, __lowercase   :     Optional[int]   )				-> Optional[Any]:
        __UpperCAmelCase				:      Any	 =    TFAutoModel.from_pretrained(self.test_model						, from_pt=__lowercase   )
        model_tf.save_pretrained(__lowercase   )
      def       UpperCAmelCase ( self   :     Tuple   )				-> Tuple:
        __UpperCAmelCase				:      Optional[int]	 =    """mock_framework"""

        # Framework provided - return whatever the user provides
        __UpperCAmelCase				:      int	 =    FeaturesManager.determine_framework(self.test_model						, __lowercase   )
        self.assertEqual(__lowercase						, __lowercase   )

        # Local checkpoint and framework provided - return provided framework
        # PyTorch checkpoint
        with TemporaryDirectory() as local_pt_ckpt:
          self._setup_pt_ckpt(__lowercase   )
          __UpperCAmelCase				:      List[str]	 =    FeaturesManager.determine_framework(__lowercase						, __lowercase   )
          self.assertEqual(__lowercase						, __lowercase   )

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
          self._setup_tf_ckpt(__lowercase   )
          __UpperCAmelCase				:      List[str]	 =    FeaturesManager.determine_framework(__lowercase						, __lowercase   )
          self.assertEqual(__lowercase						, __lowercase   )
      def       UpperCAmelCase ( self   :     List[str]   )				-> int:
        # PyTorch checkpoint
        with TemporaryDirectory() as local_pt_ckpt:
          self._setup_pt_ckpt(__lowercase   )
          __UpperCAmelCase				:      Optional[Any]	 =    FeaturesManager.determine_framework(__lowercase   )
          self.assertEqual(__lowercase						, self.framework_pt   )

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
          self._setup_tf_ckpt(__lowercase   )
          __UpperCAmelCase				:      str	 =    FeaturesManager.determine_framework(__lowercase   )
          self.assertEqual(__lowercase						, self.framework_tf   )

        # Invalid local checkpoint
        with TemporaryDirectory() as local_invalid_ckpt:
          with self.assertRaises(__lowercase   ):
            __UpperCAmelCase				:      Any	 =    FeaturesManager.determine_framework(__lowercase   )




      def       UpperCAmelCase ( self   :     Optional[Any]   )				-> Optional[int]:
        __UpperCAmelCase				:      Dict	 =    MagicMock(return_value=__lowercase   )
        with patch("""transformers.onnx.features.is_tf_available"""						, __lowercase   ):
          __UpperCAmelCase				:      List[str]	 =    FeaturesManager.determine_framework(self.test_model   )
          self.assertEqual(__lowercase						, self.framework_pt   )

        # PyTorch not in environment -> use TensorFlow
        __UpperCAmelCase				:      Dict	 =    MagicMock(return_value=__lowercase   )
        with patch("""transformers.onnx.features.is_torch_available"""						, __lowercase   ):
          __UpperCAmelCase				:      Tuple	 =    FeaturesManager.determine_framework(self.test_model   )
          self.assertEqual(__lowercase						, self.framework_tf   )

        # Both in environment -> use PyTorch
        __UpperCAmelCase				:      List[str]	 =    MagicMock(return_value=__lowercase   )
        __UpperCAmelCase				:      int	 =    MagicMock(return_value=__lowercase   )
        with patch("""transformers.onnx.features.is_tf_available"""						, __lowercase   ), patch(
            """transformers.onnx.features.is_torch_available"""						, __lowercase   ):
          __UpperCAmelCase				:      Optional[Any]	 =    FeaturesManager.determine_framework(self.test_model   )
          self.assertEqual(__lowercase						, self.framework_pt   )

        # Both not in environment -> raise error
        __UpperCAmelCase				:      Optional[int]	 =    MagicMock(return_value=__lowercase   )
        __UpperCAmelCase				:      Tuple	 =    MagicMock(return_value=__lowercase   )
        with patch("""transformers.onnx.features.is_tf_available"""						, __lowercase   ), patch(
            """transformers.onnx.features.is_torch_available"""						, __lowercase   ):
          with self.assertRaises(__lowercase   ):
            __UpperCAmelCase				:      Tuple	 =    FeaturesManager.determine_framework(self.test_model   )






import unittest

import numpy as np

from transformers import DistilBertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
     import jax.numpy as jnp

     from transformers.models.distilbert.modeling_flax_distilbert import (
         FlaxDistilBertForMaskedLM,
         FlaxDistilBertForMultipleChoice,
         FlaxDistilBertForQuestionAnswering,
         FlaxDistilBertForSequenceClassification,
         FlaxDistilBertForTokenClassification,
         FlaxDistilBertModel,
     )







class 						_snake_case    ( unittest.TestCase				):




 def __init__(						self   ,    _lowerCamelCase   ,    _lowerCamelCase=13   ,    _lowerCamelCase=7   ,    _lowerCamelCase=True   ,    _lowerCamelCase=True   ,    _lowerCamelCase=True   ,    _lowerCamelCase=True   ,    _lowerCamelCase=99   ,    _lowerCamelCase=32   ,    _lowerCamelCase=5   ,    _lowerCamelCase=4   ,    _lowerCamelCase=37   ,    _lowerCamelCase="gelu"   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=512   ,    _lowerCamelCase=16   ,    _lowerCamelCase=2   ,    _lowerCamelCase=0.02   ,    _lowerCamelCase=4   ,    ):
       a		:Optional[Any]									=			parent
       a		:str									=			batch_size
       a		:Tuple									=			seq_length
       a		:List[Any]									=			is_training
       a		:Optional[int]									=			use_attention_mask
       a		:List[str]									=			use_token_type_ids
       a		:str									=			use_labels
       a		:Optional[Any]									=			vocab_size
       a		:Optional[int]									=			hidden_size
       a		:Tuple									=			num_hidden_layers
       a		:Union[str, Any]									=			num_attention_heads
       a		:int									=			intermediate_size
       a		:int									=			hidden_act
       a		:int									=			hidden_dropout_prob
       a		:Union[str, Any]									=			attention_probs_dropout_prob
       a		:str									=			max_position_embeddings
       a		:Dict									=			type_vocab_size
       a		:str									=			type_sequence_label_size
       a		:List[str]									=			initializer_range
       a		:Optional[Any]									=			num_choices




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Tuple									=			ids_tensor([self.batch_size, self.seq_length]   ,    self.vocab_size       )

       a		:Any									=			None
       if self.use_attention_mask:
             a		:Any									=			random_attention_mask([self.batch_size, self.seq_length]       )

       a		:Any									=			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   ,    tie_weights_=_lowerCamelCase   ,    )

       return config, input_ids, attention_mask




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[str]									=			self.prepare_config_and_inputs()
       a				,							a				,							a		:str									=			config_and_inputs
       a		:List[Any]									=			{'''input_ids''': input_ids, '''attention_mask''': attention_mask}
       return config, inputs_dict







@require_flax
class 						_snake_case    ( _snake_case		,       unittest.TestCase				):
 SCREAMING_SNAKE_CASE__ =			(
     (
         FlaxDistilBertModel,
         FlaxDistilBertForMaskedLM,
         FlaxDistilBertForMultipleChoice,
         FlaxDistilBertForQuestionAnswering,
         FlaxDistilBertForSequenceClassification,
         FlaxDistilBertForTokenClassification,
         FlaxDistilBertForQuestionAnswering,
     )
     if is_flax_available()
     else ()
 )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[Any]									=			FlaxDistilBertModelTester(self       )




 @slow
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       for model_class_name in self.all_model_classes:
             a		:int									=			model_class_name.from_pretrained('''distilbert-base-uncased'''       )
             a		:List[str]									=			model(np.ones((1, 1)       )       )
             self.assertIsNotNone(_lowerCamelCase       )







@require_flax
class 						_snake_case    ( unittest.TestCase				):




 @slow
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[str]									=			FlaxDistilBertModel.from_pretrained('''distilbert-base-uncased'''       )
       a		:Optional[Any]									=			np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]       )
       a		:List[Any]									=			np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
       a		:List[str]									=			model(_lowerCamelCase   ,    attention_mask=_lowerCamelCase       )[0]
       a		:Union[str, Any]									=			(1, 11, 768)
       self.assertEqual(output.shape   ,    _lowerCamelCase       )
       a		:int									=			np.array([[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]]       )

       self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]   ,    _lowerCamelCase   ,    atol=1e-4       )       )





