



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowercase    : List[Any]    	=  {
    "configuration_luke": ["LUKE_PRETRAINED_CONFIG_ARCHIVE_MAP", "LukeConfig"],
    "tokenization_luke": ["LukeTokenizer"],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       lowercase    : str    	=  [
           "LUKE_PRETRAINED_MODEL_ARCHIVE_LIST",
           "LukeForEntityClassification",
           "LukeForEntityPairClassification",
           "LukeForEntitySpanClassification",
           "LukeForMultipleChoice",
           "LukeForQuestionAnswering",
           "LukeForSequenceClassification",
           "LukeForTokenClassification",
           "LukeForMaskedLM",
           "LukeModel",
           "LukePreTrainedModel",
       ]


if TYPE_CHECKING:
       from .configuration_luke import LUKE_PRETRAINED_CONFIG_ARCHIVE_MAP, LukeConfig
       from .tokenization_luke import LukeTokenizer

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_luke import (
                  LUKE_PRETRAINED_MODEL_ARCHIVE_LIST,
                  LukeForEntityClassification,
                  LukeForEntityPairClassification,
                  LukeForEntitySpanClassification,
                  LukeForMaskedLM,
                  LukeForMultipleChoice,
                  LukeForQuestionAnswering,
                  LukeForSequenceClassification,
                  LukeForTokenClassification,
                  LukeModel,
                  LukePreTrainedModel,
              )

else:
       import sys

       lowercase    : int    	=  _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(						__UpperCAmelCase ):

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						SMALL_MODEL_IDENTIFIER
            _UpperCamelCase              =						'''pt'''
            _UpperCamelCase              =						'''tf'''

     def 	_UpperCamelCase     (      self,       A      ):





            """simple docstring"""

            _UpperCamelCase              =						AutoModel.from_pretrained(self.test_model      )
            model_pt.save_pretrained(A      )

     def 	_UpperCamelCase     (      self,       A      ):





            """simple docstring"""

            _UpperCamelCase              =						TFAutoModel.from_pretrained(self.test_model,       from_pt=A      )
            model_tf.save_pretrained(A      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						'''mock_framework'''

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

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

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

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            with TemporaryDirectory() as local_pt_ckpt:
                   self._setup_pt_ckpt(A      )
                   _UpperCamelCase              =						FeaturesManager.determine_framework(A      )
                   self.assertEqual(A,       self.framework_pt      )

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

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



     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						MagicMock(return_value=A      )
            with patch('''transformers.onnx.features.is_tf_available''',       A      ):
                   _UpperCamelCase              =						FeaturesManager.determine_framework(self.test_model      )
                   self.assertEqual(A,       self.framework_pt      )

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

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

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






