





"""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 					_UpperCAmelCase   (	a__ ):


					def 	lowerCamelCase       (					self			:Any		):
												A		=  SMALL_MODEL_IDENTIFIER
												A		=  """pt"""
												A		=  """tf"""


					def 	lowerCamelCase       (					self			:Dict     ,       __UpperCamelCase			:List[Any]		):
												A		=  AutoModel.from_pretrained(self.test_model		)
												model_pt.save_pretrained(SCREAMING_SNAKE_CASE__		)


					def 	lowerCamelCase       (					self			:Optional[int]     ,       __UpperCamelCase			:Union[str, Any]		):
												A		=  TFAutoModel.from_pretrained(self.test_model     ,       from_pt=SCREAMING_SNAKE_CASE__		)
												model_tf.save_pretrained(SCREAMING_SNAKE_CASE__		)


					def 	lowerCamelCase       (					self			:Optional[Any]		):
												A		=  """mock_framework"""

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

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

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


					def 	lowerCamelCase       (					self			:List[Any]		):




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

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

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


					def 	lowerCamelCase       (					self			:str		):
												A		=  MagicMock(return_value=SCREAMING_SNAKE_CASE__		)
												with patch("transformers.onnx.features.is_tf_available"     ,       SCREAMING_SNAKE_CASE__		):
																			A		=  FeaturesManager.determine_framework(self.test_model		)
																			self.assertEqual(SCREAMING_SNAKE_CASE__     ,       self.framework_pt		)

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

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

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




"""simple docstring"""




UpperCAmelCase__      :    Any    							= '\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n'

UpperCAmelCase__      :    Any    							= [{'type': 'code', 'content': INSTALL_CONTENT}]
UpperCAmelCase__      :    Optional[int]    							= {
    '{processor_class}': 'FakeProcessorClass',
    '{model_class}': 'FakeModelClass',
    '{object_class}': 'FakeObjectClass',
}




