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		(   UpperCamelCase_    ):
    def 						UpperCamelCase_				(  self   )       ->					List[str]:
     SCREAMING_SNAKE_CASE__:   Any=      SMALL_MODEL_IDENTIFIER
     SCREAMING_SNAKE_CASE__:   List[str]=      '''pt'''
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      '''tf'''
    def 						UpperCamelCase_				(  self , lowerCAmelCase   )       ->					List[Any]:
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      AutoModel.from_pretrained(self.test_model   )
     model_pt.save_pretrained(lowerCAmelCase   )
    def 						UpperCamelCase_				(  self , lowerCAmelCase   )       ->					Dict:
     SCREAMING_SNAKE_CASE__:   Optional[int]=      TFAutoModel.from_pretrained(self.test_model , from_pt=lowerCAmelCase   )
     model_tf.save_pretrained(lowerCAmelCase   )
    def 						UpperCamelCase_				(  self   )       ->					Dict:
     SCREAMING_SNAKE_CASE__:   str=      '''mock_framework'''

     # Framework provided - return whatever the user provides
     SCREAMING_SNAKE_CASE__:   Dict=      FeaturesManager.determine_framework(self.test_model , lowerCAmelCase   )
     self.assertEqual(lowerCAmelCase , lowerCAmelCase   )

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

     # TensorFlow checkpoint
     with TemporaryDirectory() as local_tf_ckpt:
      self._setup_tf_ckpt(lowerCAmelCase   )
      SCREAMING_SNAKE_CASE__:   str=      FeaturesManager.determine_framework(lowerCAmelCase , lowerCAmelCase   )
      self.assertEqual(lowerCAmelCase , lowerCAmelCase   )
    def 						UpperCamelCase_				(  self   )       ->					Union[str, Any]:
     # PyTorch checkpoint
     with TemporaryDirectory() as local_pt_ckpt:
      self._setup_pt_ckpt(lowerCAmelCase   )
      SCREAMING_SNAKE_CASE__:   Tuple=      FeaturesManager.determine_framework(lowerCAmelCase   )
      self.assertEqual(lowerCAmelCase , self.framework_pt   )

     # TensorFlow checkpoint
     with TemporaryDirectory() as local_tf_ckpt:
      self._setup_tf_ckpt(lowerCAmelCase   )
      SCREAMING_SNAKE_CASE__:   Tuple=      FeaturesManager.determine_framework(lowerCAmelCase   )
      self.assertEqual(lowerCAmelCase , self.framework_tf   )

     # Invalid local checkpoint
     with TemporaryDirectory() as local_invalid_ckpt:
      with self.assertRaises(lowerCAmelCase   ):
       SCREAMING_SNAKE_CASE__:   List[str]=      FeaturesManager.determine_framework(lowerCAmelCase   )



    def 						UpperCamelCase_				(  self   )       ->					str:
     SCREAMING_SNAKE_CASE__:   int=      MagicMock(return_value=lowerCAmelCase   )
     with patch('''transformers.onnx.features.is_tf_available''' , lowerCAmelCase   ):
      SCREAMING_SNAKE_CASE__:   int=      FeaturesManager.determine_framework(self.test_model   )
      self.assertEqual(lowerCAmelCase , self.framework_pt   )

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

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

     # Both not in environment -> raise error
     SCREAMING_SNAKE_CASE__:   int=      MagicMock(return_value=lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   List[str]=      MagicMock(return_value=lowerCAmelCase   )
     with patch('''transformers.onnx.features.is_tf_available''' , lowerCAmelCase   ), patch(
         '''transformers.onnx.features.is_torch_available''' , lowerCAmelCase   ):
      with self.assertRaises(lowerCAmelCase   ):
       SCREAMING_SNAKE_CASE__:   str=      FeaturesManager.determine_framework(self.test_model   )


from ..utils import DummyObject, requires_backends






class 		_lowerCamelCase		(   metaclass=UpperCamelCase_    ):
    __a  =      ["torch", "scipy"]
    def __init__(  self , *lowerCAmelCase , **lowerCAmelCase   )       ->					Any:
     requires_backends(self , ['''torch''', '''scipy''']   )
    @classmethod
    def 						UpperCamelCase_				(  cls , *lowerCAmelCase , **lowerCAmelCase   )       ->					List[str]:
     requires_backends(cls , ['''torch''', '''scipy''']   )



    @classmethod
    def 						UpperCamelCase_				(  cls , *lowerCAmelCase , **lowerCAmelCase   )       ->					Tuple:
     requires_backends(cls , ['''torch''', '''scipy''']   )

