





'''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 lowerCamelCase						(a__					):







  def 				UpperCAmelCase_				(		self		)		->						List[Any]:






        """simple docstring"""





        _snake_case		:	Tuple					     =     SMALL_MODEL_IDENTIFIER
        _snake_case		:	Optional[int]					     =     '''pt'''
        _snake_case		:	Optional[Any]					     =     '''tf'''







  def 				UpperCAmelCase_				(		self     ,     lowercase__		)		->						Optional[Any]:






        """simple docstring"""





        _snake_case		:	List[str]					     =     AutoModel.from_pretrained(self.test_model		)
        model_pt.save_pretrained(lowercase__		)







  def 				UpperCAmelCase_				(		self     ,     lowercase__		)		->						Optional[int]:






        """simple docstring"""





        _snake_case		:	Dict					     =     TFAutoModel.from_pretrained(self.test_model     ,     from_pt=lowercase__		)
        model_tf.save_pretrained(lowercase__		)







  def 				UpperCAmelCase_				(		self		)		->						List[Any]:






        """simple docstring"""





        _snake_case		:	List[str]					     =     '''mock_framework'''

        # Framework provided - return whatever the user provides
        _snake_case		:	Tuple					     =     FeaturesManager.determine_framework(self.test_model     ,     lowercase__		)
        self.assertEqual(lowercase__     ,     lowercase__		)

        # Local checkpoint and framework provided - return provided framework
        # PyTorch checkpoint
        with TemporaryDirectory() as local_pt_ckpt:
              self._setup_pt_ckpt(lowercase__		)
              _snake_case		:	Optional[Any]					     =     FeaturesManager.determine_framework(lowercase__     ,     lowercase__		)
              self.assertEqual(lowercase__     ,     lowercase__		)

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
              self._setup_tf_ckpt(lowercase__		)
              _snake_case		:	Tuple					     =     FeaturesManager.determine_framework(lowercase__     ,     lowercase__		)
              self.assertEqual(lowercase__     ,     lowercase__		)







  def 				UpperCAmelCase_				(		self		)		->						Union[str, Any]:






        """simple docstring"""





        with TemporaryDirectory() as local_pt_ckpt:
              self._setup_pt_ckpt(lowercase__		)
              _snake_case		:	Dict					     =     FeaturesManager.determine_framework(lowercase__		)
              self.assertEqual(lowercase__     ,     self.framework_pt		)

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
              self._setup_tf_ckpt(lowercase__		)
              _snake_case		:	Optional[int]					     =     FeaturesManager.determine_framework(lowercase__		)
              self.assertEqual(lowercase__     ,     self.framework_tf		)

        # Invalid local checkpoint
        with TemporaryDirectory() as local_invalid_ckpt:
              with self.assertRaises(lowercase__		):
                    _snake_case		:	Union[str, Any]					     =     FeaturesManager.determine_framework(lowercase__		)







  def 				UpperCAmelCase_				(		self		)		->						List[str]:






        """simple docstring"""





        _snake_case		:	Optional[int]					     =     MagicMock(return_value=lowercase__		)
        with patch('''transformers.onnx.features.is_tf_available'''     ,     lowercase__		):
              _snake_case		:	Optional[Any]					     =     FeaturesManager.determine_framework(self.test_model		)
              self.assertEqual(lowercase__     ,     self.framework_pt		)

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

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

        # Both not in environment -> raise error
        _snake_case		:	Dict					     =     MagicMock(return_value=lowercase__		)
        _snake_case		:	Optional[int]					     =     MagicMock(return_value=lowercase__		)
        with patch('''transformers.onnx.features.is_tf_available'''     ,     lowercase__		), patch(
            '''transformers.onnx.features.is_torch_available'''     ,     lowercase__		):
              with self.assertRaises(lowercase__		):
                    _snake_case		:	Optional[Any]					     =     FeaturesManager.determine_framework(self.test_model		)









'''simple docstring'''

# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


UpperCAmelCase					:		Dict		=							{'configuration_timm_backbone': ['TimmBackboneConfig']}


try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   UpperCAmelCase					:		Union[str, Any]		=							['TimmBackbone']


if TYPE_CHECKING:
   from .configuration_timm_backbone import TimmBackboneConfig

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_timm_backbone import TimmBackbone

else:
   import sys

   UpperCAmelCase					:		Dict		=							_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)


