




from ..utils import DummyObject, requires_backends






class  _lowerCAmelCase     (  metaclass=_lowercase	):
      A__		     =		['flax', 'transformers']

      def __init__(   self      ,				*__UpperCAmelCase      ,				**__UpperCAmelCase		):
        requires_backends(self      ,				['''flax''', '''transformers''']		)

      @classmethod
      def 	__magic_name__(   cls      ,				*__UpperCAmelCase      ,				**__UpperCAmelCase		):
        requires_backends(cls      ,				['''flax''', '''transformers''']		)







      @classmethod
      def 	__magic_name__(   cls      ,				*__UpperCAmelCase      ,				**__UpperCAmelCase		):
        requires_backends(cls      ,				['''flax''', '''transformers''']		)






class  _lowerCAmelCase     (  metaclass=_lowercase	):
      A__		     =		['flax', 'transformers']

      def __init__(   self      ,				*__UpperCAmelCase      ,				**__UpperCAmelCase		):
        requires_backends(self      ,				['''flax''', '''transformers''']		)

      @classmethod
      def 	__magic_name__(   cls      ,				*__UpperCAmelCase      ,				**__UpperCAmelCase		):
        requires_backends(cls      ,				['''flax''', '''transformers''']		)







      @classmethod
      def 	__magic_name__(   cls      ,				*__UpperCAmelCase      ,				**__UpperCAmelCase		):
        requires_backends(cls      ,				['''flax''', '''transformers''']		)






class  _lowerCAmelCase     (  metaclass=_lowercase	):
      A__		     =		['flax', 'transformers']

      def __init__(   self      ,				*__UpperCAmelCase      ,				**__UpperCAmelCase		):
        requires_backends(self      ,				['''flax''', '''transformers''']		)

      @classmethod
      def 	__magic_name__(   cls      ,				*__UpperCAmelCase      ,				**__UpperCAmelCase		):
        requires_backends(cls      ,				['''flax''', '''transformers''']		)







      @classmethod
      def 	__magic_name__(   cls      ,				*__UpperCAmelCase      ,				**__UpperCAmelCase		):
        requires_backends(cls      ,				['''flax''', '''transformers''']		)






class  _lowerCAmelCase     (  metaclass=_lowercase	):
      A__		     =		['flax', 'transformers']

      def __init__(   self      ,				*__UpperCAmelCase      ,				**__UpperCAmelCase		):
        requires_backends(self      ,				['''flax''', '''transformers''']		)

      @classmethod
      def 	__magic_name__(   cls      ,				*__UpperCAmelCase      ,				**__UpperCAmelCase		):
        requires_backends(cls      ,				['''flax''', '''transformers''']		)







      @classmethod
      def 	__magic_name__(   cls      ,				*__UpperCAmelCase      ,				**__UpperCAmelCase		):
        requires_backends(cls      ,				['''flax''', '''transformers''']		)







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

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  Union[str, Any]		       =     SMALL_MODEL_IDENTIFIER
        lowerCAmelCase__				:  List[str]		       =     '''pt'''
        lowerCAmelCase__				:  int		       =     '''tf'''

      def 	__magic_name__(   self      ,				__UpperCAmelCase		):
        lowerCAmelCase__				:  Optional[Any]		       =     AutoModel.from_pretrained(self.test_model		)
        model_pt.save_pretrained(__UpperCAmelCase		)

      def 	__magic_name__(   self      ,				__UpperCAmelCase		):
        lowerCAmelCase__				:  str		       =     TFAutoModel.from_pretrained(self.test_model      ,				from_pt=__UpperCAmelCase		)
        model_tf.save_pretrained(__UpperCAmelCase		)

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  int		       =     '''mock_framework'''

        # Framework provided - return whatever the user provides
        lowerCAmelCase__				:  Tuple		       =     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__				:  int		       =     FeaturesManager.determine_framework(__UpperCAmelCase      ,				__UpperCAmelCase		)
          self.assertEqual(__UpperCAmelCase      ,				__UpperCAmelCase		)

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
          self._setup_tf_ckpt(__UpperCAmelCase		)
          lowerCAmelCase__				:  List[str]		       =     FeaturesManager.determine_framework(__UpperCAmelCase      ,				__UpperCAmelCase		)
          self.assertEqual(__UpperCAmelCase      ,				__UpperCAmelCase		)

      def 	__magic_name__(   self		):
        # PyTorch checkpoint
        with TemporaryDirectory() as local_pt_ckpt:
          self._setup_pt_ckpt(__UpperCAmelCase		)
          lowerCAmelCase__				:  Tuple		       =     FeaturesManager.determine_framework(__UpperCAmelCase		)
          self.assertEqual(__UpperCAmelCase      ,				self.framework_pt		)

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

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







      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  List[Any]		       =     MagicMock(return_value=__UpperCAmelCase		)
        with patch('''transformers.onnx.features.is_tf_available'''      ,				__UpperCAmelCase		):
          lowerCAmelCase__				:  int		       =     FeaturesManager.determine_framework(self.test_model		)
          self.assertEqual(__UpperCAmelCase      ,				self.framework_pt		)

        # PyTorch not in environment -> use TensorFlow
        lowerCAmelCase__				:  Union[str, Any]		       =     MagicMock(return_value=__UpperCAmelCase		)
        with patch('''transformers.onnx.features.is_torch_available'''      ,				__UpperCAmelCase		):
          lowerCAmelCase__				:  Optional[int]		       =     FeaturesManager.determine_framework(self.test_model		)
          self.assertEqual(__UpperCAmelCase      ,				self.framework_tf		)

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

        # Both not in environment -> raise error
        lowerCAmelCase__				:  Optional[int]		       =     MagicMock(return_value=__UpperCAmelCase		)
        lowerCAmelCase__				:  Dict		       =     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__				:  Tuple		       =     FeaturesManager.determine_framework(self.test_model		)

