






'''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				):




      """simple docstring"""




      def 				A__      ( self			:		Tuple		)  ->  str:


       '''simple docstring'''

       lowercase					:   Optional[int]				  =SMALL_MODEL_IDENTIFIER
       lowercase					:   int				  ='''pt'''
       lowercase					:   Tuple				  ='''tf'''






      def 				A__      ( self			:		Any     ,      UpperCAmelCase			:		Optional[int]		)  ->  int:


       '''simple docstring'''

       lowercase					:   List[Any]				  =AutoModel.from_pretrained(self.test_model		)
       model_pt.save_pretrained(UpperCAmelCase		)






      def 				A__      ( self			:		List[str]     ,      UpperCAmelCase			:		Optional[Any]		)  ->  int:


       '''simple docstring'''

       lowercase					:   Tuple				  =TFAutoModel.from_pretrained(self.test_model     ,      from_pt=UpperCAmelCase		)
       model_tf.save_pretrained(UpperCAmelCase		)






      def 				A__      ( self			:		List[Any]		)  ->  Dict:


       '''simple docstring'''

       lowercase					:   Optional[Any]				  ='''mock_framework'''

       # Framework provided - return whatever the user provides
       lowercase					:   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		)
        lowercase					:   Union[str, Any]				  =FeaturesManager.determine_framework(UpperCAmelCase     ,      UpperCAmelCase		)
        self.assertEqual(UpperCAmelCase     ,      UpperCAmelCase		)

       # TensorFlow checkpoint
       with TemporaryDirectory() as local_tf_ckpt:
        self._setup_tf_ckpt(UpperCAmelCase		)
        lowercase					:   Optional[Any]				  =FeaturesManager.determine_framework(UpperCAmelCase     ,      UpperCAmelCase		)
        self.assertEqual(UpperCAmelCase     ,      UpperCAmelCase		)






      def 				A__      ( self			:		int		)  ->  List[str]:


       '''simple docstring'''

       with TemporaryDirectory() as local_pt_ckpt:
        self._setup_pt_ckpt(UpperCAmelCase		)
        lowercase					:   List[str]				  =FeaturesManager.determine_framework(UpperCAmelCase		)
        self.assertEqual(UpperCAmelCase     ,      self.framework_pt		)

       # TensorFlow checkpoint
       with TemporaryDirectory() as local_tf_ckpt:
        self._setup_tf_ckpt(UpperCAmelCase		)
        lowercase					:   Optional[Any]				  =FeaturesManager.determine_framework(UpperCAmelCase		)
        self.assertEqual(UpperCAmelCase     ,      self.framework_tf		)

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






      def 				A__      ( self			:		List[Any]		)  ->  Tuple:


       '''simple docstring'''

       lowercase					:   List[str]				  =MagicMock(return_value=UpperCAmelCase		)
       with patch('''transformers.onnx.features.is_tf_available'''     ,      UpperCAmelCase		):
        lowercase					:   List[Any]				  =FeaturesManager.determine_framework(self.test_model		)
        self.assertEqual(UpperCAmelCase     ,      self.framework_pt		)

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

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

       # Both not in environment -> raise error
       lowercase					:   str				  =MagicMock(return_value=UpperCAmelCase		)
       lowercase					:   str				  =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		):
         lowercase					:   List[str]				  =FeaturesManager.determine_framework(self.test_model		)











'''simple docstring'''



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


SCREAMING_SNAKE_CASE						=	{
    'configuration_m2m_100': ['M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP', 'M2M100Config', 'M2M100OnnxConfig'],
    'tokenization_m2m_100': ['M2M100Tokenizer'],
}


try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE						=	[
          'M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST',
          'M2M100ForConditionalGeneration',
          'M2M100Model',
          'M2M100PreTrainedModel',
      ]


if TYPE_CHECKING:
      from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig
      from .tokenization_mam_aaa import MaMaaaTokenizer

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_mam_aaa import (
                M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST,
                MaMaaaForConditionalGeneration,
                MaMaaaModel,
                MaMaaaPreTrainedModel,
            )


else:
      import sys

      SCREAMING_SNAKE_CASE						=	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



