




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available


UpperCAmelCase_									=	{}


try:
       if not is_sentencepiece_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       UpperCAmelCase_									=	['MLukeTokenizer']

if TYPE_CHECKING:
       try:
              if not is_sentencepiece_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_mluke import MLukeTokenizer


else:
       import sys

       UpperCAmelCase_									=	_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     lowerCamelCase__(					__lowerCamelCase):
      def 			lowerCAmelCase__						(				self: List[Any]   ):
             __lowerCamelCase   =						SMALL_MODEL_IDENTIFIER
             __lowerCamelCase   =						"""pt"""
             __lowerCamelCase   =						"""tf"""
      def 			lowerCAmelCase__						(				self: Optional[Any]    ,       UpperCamelCase_: Optional[Any]   ):
             __lowerCamelCase   =						AutoModel.from_pretrained(self.test_model   )
             model_pt.save_pretrained(UpperCamelCase_   )
      def 			lowerCAmelCase__						(				self: Tuple    ,       UpperCamelCase_: Union[str, Any]   ):
             __lowerCamelCase   =						TFAutoModel.from_pretrained(self.test_model    ,       from_pt=UpperCamelCase_   )
             model_tf.save_pretrained(UpperCamelCase_   )
      def 			lowerCAmelCase__						(				self: Union[str, Any]   ):
             __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 			lowerCAmelCase__						(				self: int   ):
             # 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 			lowerCAmelCase__						(				self: Optional[int]   ):
             __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   )

