





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCAmelCase__						:			str									={
    '''configuration_mobilebert''': [
        '''MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''MobileBertConfig''',
        '''MobileBertOnnxConfig''',
    ],
    '''tokenization_mobilebert''': ['''MobileBertTokenizer'''],
}

try:
							if not is_tokenizers_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							UpperCAmelCase__						:			str									=['''MobileBertTokenizerFast''']

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							UpperCAmelCase__						:			Union[str, Any]									=[
							    '''MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''MobileBertForMaskedLM''',
							    '''MobileBertForMultipleChoice''',
							    '''MobileBertForNextSentencePrediction''',
							    '''MobileBertForPreTraining''',
							    '''MobileBertForQuestionAnswering''',
							    '''MobileBertForSequenceClassification''',
							    '''MobileBertForTokenClassification''',
							    '''MobileBertLayer''',
							    '''MobileBertModel''',
							    '''MobileBertPreTrainedModel''',
							    '''load_tf_weights_in_mobilebert''',
							]

try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							UpperCAmelCase__						:			Tuple									=[
							    '''TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''TFMobileBertForMaskedLM''',
							    '''TFMobileBertForMultipleChoice''',
							    '''TFMobileBertForNextSentencePrediction''',
							    '''TFMobileBertForPreTraining''',
							    '''TFMobileBertForQuestionAnswering''',
							    '''TFMobileBertForSequenceClassification''',
							    '''TFMobileBertForTokenClassification''',
							    '''TFMobileBertMainLayer''',
							    '''TFMobileBertModel''',
							    '''TFMobileBertPreTrainedModel''',
							]


if TYPE_CHECKING:
							from .configuration_mobilebert import (
							    MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
							    MobileBertConfig,
							    MobileBertOnnxConfig,
							)
							from .tokenization_mobilebert import MobileBertTokenizer

							try:
														if not is_tokenizers_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_mobilebert_fast import MobileBertTokenizerFast

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_mobilebert import (
														    MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    MobileBertForMaskedLM,
														    MobileBertForMultipleChoice,
														    MobileBertForNextSentencePrediction,
														    MobileBertForPreTraining,
														    MobileBertForQuestionAnswering,
														    MobileBertForSequenceClassification,
														    MobileBertForTokenClassification,
														    MobileBertLayer,
														    MobileBertModel,
														    MobileBertPreTrainedModel,
														    load_tf_weights_in_mobilebert,
														)

							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_mobilebert import (
														    TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    TFMobileBertForMaskedLM,
														    TFMobileBertForMultipleChoice,
														    TFMobileBertForNextSentencePrediction,
														    TFMobileBertForPreTraining,
														    TFMobileBertForQuestionAnswering,
														    TFMobileBertForSequenceClassification,
														    TFMobileBertForTokenClassification,
														    TFMobileBertMainLayer,
														    TFMobileBertModel,
														    TFMobileBertPreTrainedModel,
														)

else:
							import sys

							UpperCAmelCase__						:			Tuple									=_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)












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     (						a    ):


				def _snake_case       (		self							):
								lowerCamelCase	 =SMALL_MODEL_IDENTIFIER
								lowerCamelCase	 ="""pt"""
								lowerCamelCase	 ="""tf"""


				def _snake_case       (		self   , UpperCAmelCase_							):
								lowerCamelCase	 =AutoModel.from_pretrained(self.test_model							)
								model_pt.save_pretrained(UpperCAmelCase_							)


				def _snake_case       (		self   , UpperCAmelCase_							):
								lowerCamelCase	 =TFAutoModel.from_pretrained(self.test_model   , from_pt=UpperCAmelCase_							)
								model_tf.save_pretrained(UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 ="""mock_framework"""

								# Framework provided - return whatever the user provides
								lowerCamelCase	 =FeaturesManager.determine_framework(self.test_model   , UpperCAmelCase_							)
								self.assertEqual(UpperCAmelCase_   , UpperCAmelCase_							)

								# Local checkpoint and framework provided - return provided framework
								# PyTorch checkpoint
								with TemporaryDirectory() as local_pt_ckpt:
												self._setup_pt_ckpt(UpperCAmelCase_							)
												lowerCamelCase	 =FeaturesManager.determine_framework(UpperCAmelCase_   , UpperCAmelCase_							)
												self.assertEqual(UpperCAmelCase_   , UpperCAmelCase_							)

								# TensorFlow checkpoint
								with TemporaryDirectory() as local_tf_ckpt:
												self._setup_tf_ckpt(UpperCAmelCase_							)
												lowerCamelCase	 =FeaturesManager.determine_framework(UpperCAmelCase_   , UpperCAmelCase_							)
												self.assertEqual(UpperCAmelCase_   , UpperCAmelCase_							)


				def _snake_case       (		self							):
								# PyTorch checkpoint
								with TemporaryDirectory() as local_pt_ckpt:
												self._setup_pt_ckpt(UpperCAmelCase_							)
												lowerCamelCase	 =FeaturesManager.determine_framework(UpperCAmelCase_							)
												self.assertEqual(UpperCAmelCase_   , self.framework_pt							)

								# TensorFlow checkpoint
								with TemporaryDirectory() as local_tf_ckpt:
												self._setup_tf_ckpt(UpperCAmelCase_							)
												lowerCamelCase	 =FeaturesManager.determine_framework(UpperCAmelCase_							)
												self.assertEqual(UpperCAmelCase_   , self.framework_tf							)

								# Invalid local checkpoint
								with TemporaryDirectory() as local_invalid_ckpt:
												with self.assertRaises(UpperCAmelCase_							):
																lowerCamelCase	 =FeaturesManager.determine_framework(UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								with patch("""transformers.onnx.features.is_tf_available"""   , UpperCAmelCase_							):
												lowerCamelCase	 =FeaturesManager.determine_framework(self.test_model							)
												self.assertEqual(UpperCAmelCase_   , self.framework_pt							)

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

								# Both in environment -> use PyTorch
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								with patch("""transformers.onnx.features.is_tf_available"""   , UpperCAmelCase_							), patch(
								    """transformers.onnx.features.is_torch_available"""   , UpperCAmelCase_							):
												lowerCamelCase	 =FeaturesManager.determine_framework(self.test_model							)
												self.assertEqual(UpperCAmelCase_   , self.framework_pt							)

								# Both not in environment -> raise error
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								with patch("""transformers.onnx.features.is_tf_available"""   , UpperCAmelCase_							), patch(
								    """transformers.onnx.features.is_torch_available"""   , UpperCAmelCase_							):
												with self.assertRaises(UpperCAmelCase_							):
																lowerCamelCase	 =FeaturesManager.determine_framework(self.test_model							)





