

"""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 			lowercase    (	lowercase_      ):




				def    a							( self	):
											snake_case_          =   SMALL_MODEL_IDENTIFIER
											snake_case_          =   'pt'
											snake_case_          =   'tf'




				def    a							( self				,       snake_case	):
											snake_case_          =   AutoModel.from_pretrained(self.test_model	)
											model_pt.save_pretrained(snake_case	)




				def    a							( self				,       snake_case	):
											snake_case_          =   TFAutoModel.from_pretrained(self.test_model				,       from_pt=snake_case	)
											model_tf.save_pretrained(snake_case	)




				def    a							( self	):
											snake_case_          =   'mock_framework'

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

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

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




				def    a							( self	):
											# PyTorch checkpoint
											with TemporaryDirectory() as local_pt_ckpt:
																		self._setup_pt_ckpt(snake_case	)
																		snake_case_          =   FeaturesManager.determine_framework(snake_case	)
																		self.assertEqual(snake_case				,       self.framework_pt	)

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

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




				def    a							( self	):
											snake_case_          =   MagicMock(return_value=snake_case	)
											with patch('transformers.onnx.features.is_tf_available'				,       snake_case	):
																		snake_case_          =   FeaturesManager.determine_framework(self.test_model	)
																		self.assertEqual(snake_case				,       self.framework_pt	)

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

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

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







from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
					if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.25.0""")):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline
else:
					from .pipeline_unclip import UnCLIPPipeline
					from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline
					from .text_proj import UnCLIPTextProjModel




