


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

             # Framework provided - return whatever the user provides
             a_   :						Optional[int]				=    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	)
                    a_   :						Tuple				=    FeaturesManager.determine_framework(_UpperCamelCase	,				_UpperCamelCase	)
                    self.assertEqual(_UpperCamelCase	,				_UpperCamelCase	)

             # TensorFlow checkpoint
             with TemporaryDirectory() as local_tf_ckpt:
                    self._setup_tf_ckpt(_UpperCamelCase	)
                    a_   :						Optional[int]				=    FeaturesManager.determine_framework(_UpperCamelCase	,				_UpperCamelCase	)
                    self.assertEqual(_UpperCamelCase	,				_UpperCamelCase	)
      def 			SCREAMING_SNAKE_CASE				(			self							:			Tuple	)						->     List[str]:
             # PyTorch checkpoint
             with TemporaryDirectory() as local_pt_ckpt:
                    self._setup_pt_ckpt(_UpperCamelCase	)
                    a_   :						int				=    FeaturesManager.determine_framework(_UpperCamelCase	)
                    self.assertEqual(_UpperCamelCase	,				self.framework_pt	)

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

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

      def 			SCREAMING_SNAKE_CASE				(			self							:			List[Any]	)						->     Tuple:
             a_   :						Optional[Any]				=    MagicMock(return_value=_UpperCamelCase	)
             with patch('transformers.onnx.features.is_tf_available'	,				_UpperCamelCase	):
                    a_   :						str				=    FeaturesManager.determine_framework(self.test_model	)
                    self.assertEqual(_UpperCamelCase	,				self.framework_pt	)

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

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

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






'''simple docstring'''
# tests directory-specific settings - this file is run automatically
# by pytest before any tests are run

import doctest
import sys
import warnings
from os.path import abspath, dirname, join

import _pytest

from transformers.testing_utils import HfDoctestModule, HfDocTestParser


# allow having multiple repository checkouts and not needing to remember to rerun
# 'pip install -e .[dev]' when switching between checkouts and running tests.
lowerCAmelCase						=					abspath(join(dirname(__file__), """src"""))
sys.path.insert(1, git_repo_path)

# silence FutureWarning warnings in tests since often we can't act on them until
# they become normal warnings - i.e. the tests still need to test the current functionality
warnings.simplefilter(action="""ignore""", category=FutureWarning)

def 					lowerCamelCase_ ( __UpperCamelCase					:       Optional[Any]		)					->	str:







     """simple docstring"""
     config.addinivalue_line(
         'markers'							,    'is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested'		)
     config.addinivalue_line(
         'markers'							,    'is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested'		)
     config.addinivalue_line('markers'							,    'is_pipeline_test: mark test to run only when pipelines are tested'		)
     config.addinivalue_line('markers'							,    'is_staging_test: mark test to run only in the staging environment'		)
     config.addinivalue_line('markers'							,    'accelerate_tests: mark test that require accelerate'		)
     config.addinivalue_line('markers'							,    'tool_tests: mark the tool tests that are run on their specific schedule'		)

def 					lowerCamelCase_ ( __UpperCamelCase					:       Tuple		)					->	List[str]:







     """simple docstring"""
     from transformers.testing_utils import pytest_addoption_shared

     pytest_addoption_shared(__UpperCamelCase		)

def 					lowerCamelCase_ ( __UpperCamelCase					:       Union[str, Any]		)					->	str:







     """simple docstring"""
     from transformers.testing_utils import pytest_terminal_summary_main

     _A				=							terminalreporter.config.getoption('--make-reports'		)
     if make_reports:
          pytest_terminal_summary_main(__UpperCamelCase							,    id=__UpperCamelCase		)

def 					lowerCamelCase_ ( __UpperCamelCase					:       str							,    __UpperCamelCase					:       List[str]		)					->	Optional[int]:







     """simple docstring"""
     # If no tests are collected, pytest exists with code 5, which makes the CI fail.
     if exitstatus == 5:
          _A				=							0


# Doctest custom flag to ignore output.
lowerCAmelCase						=					doctest.register_optionflag("""IGNORE_RESULT""")

lowerCAmelCase						=					doctest.OutputChecker






class        lowerCAmelCase_  (     UpperCAmelCase	):






       def   UpperCamelCase						( self			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase   )->      Tuple:
            if IGNORE_RESULT & optionflags:
                 return True
            return OutputChecker.check_output(self			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase   )






lowerCAmelCase						=					CustomOutputChecker
lowerCAmelCase						=					HfDoctestModule
lowerCAmelCase						=					HfDocTestParser





