

'''simple docstring'''







from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


__lowerCamelCase						    =      logging.get_logger(__name__)

__lowerCamelCase						    =      {
    '''microsoft/resnet-50''': '''https://huggingface.co/microsoft/resnet-50/blob/main/config.json''',
}


class    A__ ( A__  ,  A__     ):
    lowercase		   =						'resnet'
    lowercase		   =						['basic', 'bottleneck']



    def __init__(   self						,			UpperCamelCase__=3						,			UpperCamelCase__=64						,			UpperCamelCase__=[256, 512, 1024, 2048]						,			UpperCamelCase__=[3, 4, 6, 3]						,			UpperCamelCase__="bottleneck"						,			UpperCamelCase__="relu"						,			UpperCamelCase__=False						,			UpperCamelCase__=None						,			UpperCamelCase__=None						,			**UpperCamelCase__						,			)  -> str:





           '''simple docstring'''
           super().__init__(**lowerCamelCase__    )
           if layer_type not in self.layer_types:
                  raise ValueError(f'''layer_type={layer_type} is not one of {",".join(self.layer_types    )}'''    )
           A_		   = num_channels
           A_		   = embedding_size
           A_		   = hidden_sizes
           A_		   = depths
           A_		   = layer_type
           A_		   = hidden_act
           A_		   = downsample_in_first_stage
           A_		   = ["""stem"""] + [f'''stage{idx}''' for idx in range(1						,			len(lowerCamelCase__    ) + 1    )]
           A_   ,     A_		   = get_aligned_output_features_output_indices(
               out_features=lowerCamelCase__						,			out_indices=lowerCamelCase__						,			stage_names=self.stage_names    )


class    A__ ( A__     ):
    lowercase		   =						version.parse("1.11"     )



    @property
    def    snake_case_     (   self    )  -> Any:





           '''simple docstring'''
           return OrderedDict(
               [
                   ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
               ]    )







    @property
    def    snake_case_     (   self    )  -> Optional[Any]:





           '''simple docstring'''
           return 1e-3



'''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__ ( _snake_case     ):



    def    snake_case_     (   self    )  -> Dict:





           '''simple docstring'''
           A_		   = SMALL_MODEL_IDENTIFIER
           A_		   = """pt"""
           A_		   = """tf"""



    def    snake_case_     (   self						,			UpperCamelCase__    )  -> Tuple:





           '''simple docstring'''
           A_		   = AutoModel.from_pretrained(self.test_model    )
           model_pt.save_pretrained(UpperCamelCase__    )



    def    snake_case_     (   self						,			UpperCamelCase__    )  -> List[str]:





           '''simple docstring'''
           A_		   = TFAutoModel.from_pretrained(self.test_model						,			from_pt=UpperCamelCase__    )
           model_tf.save_pretrained(UpperCamelCase__    )



    def    snake_case_     (   self    )  -> Dict:





           '''simple docstring'''
           A_		   = """mock_framework"""

           # Framework provided - return whatever the user provides
           A_		   = 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_		   = FeaturesManager.determine_framework(UpperCamelCase__						,			UpperCamelCase__    )
                  self.assertEqual(UpperCamelCase__						,			UpperCamelCase__    )

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



    def    snake_case_     (   self    )  -> int:





           '''simple docstring'''
           # PyTorch checkpoint
           with TemporaryDirectory() as local_pt_ckpt:
                  self._setup_pt_ckpt(UpperCamelCase__    )
                  A_		   = FeaturesManager.determine_framework(UpperCamelCase__    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_pt    )

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

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







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





           '''simple docstring'''
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           with patch("""transformers.onnx.features.is_tf_available"""						,			UpperCamelCase__    ):
                  A_		   = FeaturesManager.determine_framework(self.test_model    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_pt    )

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

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

           # Both not in environment -> raise error
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           A_		   = 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_		   = FeaturesManager.determine_framework(self.test_model    )
