






'''simple docstring'''

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






 """simple docstring"""





 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   List[Any]					    =							SMALL_MODEL_IDENTIFIER
       UpperCamelCase_				:   List[Any]					    =							"""pt"""
       UpperCamelCase_				:   List[Any]					    =							"""tf"""
 def   _UpperCAmelCase       (							self			,     __lowerCAmelCase ):
       UpperCamelCase_				:   Tuple					    =							AutoModel.from_pretrained(self.test_model )
       model_pt.save_pretrained(__lowerCAmelCase )
 def   _UpperCAmelCase       (							self			,     __lowerCAmelCase ):
       UpperCamelCase_				:   List[str]					    =							TFAutoModel.from_pretrained(self.test_model			,     from_pt=__lowerCAmelCase )
       model_tf.save_pretrained(__lowerCAmelCase )
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   Tuple					    =							"""mock_framework"""

       # Framework provided - return whatever the user provides
       UpperCamelCase_				:   Union[str, Any]					    =							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 )
             UpperCamelCase_				:   List[str]					    =							FeaturesManager.determine_framework(__lowerCAmelCase			,     __lowerCAmelCase )
             self.assertEqual(__lowerCAmelCase			,     __lowerCAmelCase )

       # TensorFlow checkpoint
       with TemporaryDirectory() as local_tf_ckpt:
             self._setup_tf_ckpt(__lowerCAmelCase )
             UpperCamelCase_				:   Dict					    =							FeaturesManager.determine_framework(__lowerCAmelCase			,     __lowerCAmelCase )
             self.assertEqual(__lowerCAmelCase			,     __lowerCAmelCase )
 def   _UpperCAmelCase       (							self ):
       # PyTorch checkpoint
       with TemporaryDirectory() as local_pt_ckpt:
             self._setup_pt_ckpt(__lowerCAmelCase )
             UpperCamelCase_				:   Tuple					    =							FeaturesManager.determine_framework(__lowerCAmelCase )
             self.assertEqual(__lowerCAmelCase			,     self.framework_pt )

       # TensorFlow checkpoint
       with TemporaryDirectory() as local_tf_ckpt:
             self._setup_tf_ckpt(__lowerCAmelCase )
             UpperCamelCase_				:   List[Any]					    =							FeaturesManager.determine_framework(__lowerCAmelCase )
             self.assertEqual(__lowerCAmelCase			,     self.framework_tf )

       # Invalid local checkpoint
       with TemporaryDirectory() as local_invalid_ckpt:
             with self.assertRaises(__lowerCAmelCase ):
                   UpperCamelCase_				:   Optional[Any]					    =							FeaturesManager.determine_framework(__lowerCAmelCase )





 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   Optional[int]					    =							MagicMock(return_value=__lowerCAmelCase )
       with patch("""transformers.onnx.features.is_tf_available"""			,     __lowerCAmelCase ):
             UpperCamelCase_				:   Dict					    =							FeaturesManager.determine_framework(self.test_model )
             self.assertEqual(__lowerCAmelCase			,     self.framework_pt )

       # PyTorch not in environment -> use TensorFlow
       UpperCamelCase_				:   Dict					    =							MagicMock(return_value=__lowerCAmelCase )
       with patch("""transformers.onnx.features.is_torch_available"""			,     __lowerCAmelCase ):
             UpperCamelCase_				:   Union[str, Any]					    =							FeaturesManager.determine_framework(self.test_model )
             self.assertEqual(__lowerCAmelCase			,     self.framework_tf )

       # Both in environment -> use PyTorch
       UpperCamelCase_				:   str					    =							MagicMock(return_value=__lowerCAmelCase )
       UpperCamelCase_				:   int					    =							MagicMock(return_value=__lowerCAmelCase )
       with patch("""transformers.onnx.features.is_tf_available"""			,     __lowerCAmelCase ), patch(
           """transformers.onnx.features.is_torch_available"""			,     __lowerCAmelCase ):
             UpperCamelCase_				:   Optional[int]					    =							FeaturesManager.determine_framework(self.test_model )
             self.assertEqual(__lowerCAmelCase			,     self.framework_pt )

       # Both not in environment -> raise error
       UpperCamelCase_				:   Optional[Any]					    =							MagicMock(return_value=__lowerCAmelCase )
       UpperCamelCase_				:   Optional[Any]					    =							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 ):
                   UpperCamelCase_				:   Union[str, Any]					    =							FeaturesManager.determine_framework(self.test_model )










'''simple docstring'''

import pytest

import datasets


# Import fixture modules as plugins
UpperCamelCase							=["tests.fixtures.files", "tests.fixtures.hub", "tests.fixtures.fsspec"]



def 							snake_case					(     a_							: List[str]    ,   a_							: Optional[Any]	)       ->				Union[str, Any]:





      """simple docstring"""

      for item in items:
            if any(marker in item.keywords for marker in ["""integration""", """unit"""]	):
                  continue
            item.add_marker(pytest.mark.unit	)



def 							snake_case					(     a_							: Optional[int]	)       ->				Optional[Any]:





      """simple docstring"""

      config.addinivalue_line("""markers"""    ,   """torchaudio_latest: mark test to run with torchaudio>=0.12"""	)



@pytest.fixture(autouse=a_	)
def 							snake_case					(     a_							: Any    ,   a_							: Optional[int]	)       ->				Tuple:





      """simple docstring"""

      UpperCamelCase_				:   Optional[Any]					    =							tmp_path_factory.getbasetemp() / """cache"""
      UpperCamelCase_				:   str					    =							test_hf_cache_home / """datasets"""
      UpperCamelCase_				:   Any					    =							test_hf_cache_home / """metrics"""
      UpperCamelCase_				:   List[Any]					    =							test_hf_cache_home / """modules"""
      monkeypatch.setattr("""datasets.config.HF_DATASETS_CACHE"""    ,   str(a_	)	)
      monkeypatch.setattr("""datasets.config.HF_METRICS_CACHE"""    ,   str(a_	)	)
      monkeypatch.setattr("""datasets.config.HF_MODULES_CACHE"""    ,   str(a_	)	)
      UpperCamelCase_				:   List[Any]					    =							test_hf_datasets_cache / """downloads"""
      monkeypatch.setattr("""datasets.config.DOWNLOADED_DATASETS_PATH"""    ,   str(a_	)	)
      UpperCamelCase_				:   Any					    =							test_hf_datasets_cache / """downloads""" / """extracted"""
      monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_PATH"""    ,   str(a_	)	)



@pytest.fixture(autouse=a_    ,   scope="""session"""	)
def 							snake_case					(     )       ->				Any:





      """simple docstring"""

      datasets.disable_progress_bar()



@pytest.fixture(autouse=a_	)
def 							snake_case					(     a_							: Optional[int]	)       ->				Union[str, Any]:





      """simple docstring"""

      monkeypatch.setattr("""datasets.config.HF_UPDATE_DOWNLOAD_COUNTS"""    ,   a_	)



@pytest.fixture
def 							snake_case					(     a_							: int	)       ->				Union[str, Any]:





      """simple docstring"""

      monkeypatch.setattr("""sqlalchemy.util.deprecations.SILENCE_UBER_WARNING"""    ,   a_	)






