





import argparse

import torch

from transformers import FunnelBaseModel, FunnelConfig, FunnelModel, load_tf_weights_in_funnel
from transformers.utils import logging


logging.set_verbosity_info()


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase    ,       lowerCAmelCase    ,       lowerCAmelCase    ,       lowerCAmelCase					):
       # Initialise PyTorch model
       _UpperCamelCase              =						FunnelConfig.from_json_file(lowerCAmelCase					)
       print(f'''Building PyTorch model from configuration: {config}'''					)
       _UpperCamelCase              =						FunnelBaseModel(lowerCAmelCase					) if base_model else FunnelModel(lowerCAmelCase					)

       # Load weights from tf checkpoint
       load_tf_weights_in_funnel(lowerCAmelCase    ,       lowerCAmelCase    ,       lowerCAmelCase					)

       # Save pytorch-model
       print(f'''Save PyTorch model to {pytorch_dump_path}'''					)
       torch.save(model.state_dict()    ,       lowerCAmelCase					)


if __name__ == "__main__":
       lowercase    : Any    	=  argparse.ArgumentParser()
       # Required parameters
       parser.add_argument(
           """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path."""
       )
       parser.add_argument(
           """--config_file""",
           default=None,
           type=str,
           required=True,
           help="""The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.""",
       )
       parser.add_argument(
           """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
       )
       parser.add_argument(
           """--base_model""", action="""store_true""", help="""Whether you want just the base model (no decoder) or not."""
       )
       lowercase    : Any    	=  parser.parse_args()
       convert_tf_checkpoint_to_pytorch(
           args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path, args.base_model
       )













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(						__UpperCAmelCase ):

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						SMALL_MODEL_IDENTIFIER
            _UpperCamelCase              =						'''pt'''
            _UpperCamelCase              =						'''tf'''

     def 	_UpperCamelCase     (      self,       A      ):





            """simple docstring"""

            _UpperCamelCase              =						AutoModel.from_pretrained(self.test_model      )
            model_pt.save_pretrained(A      )

     def 	_UpperCamelCase     (      self,       A      ):





            """simple docstring"""

            _UpperCamelCase              =						TFAutoModel.from_pretrained(self.test_model,       from_pt=A      )
            model_tf.save_pretrained(A      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						'''mock_framework'''

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

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

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

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

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

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

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



     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						MagicMock(return_value=A      )
            with patch('''transformers.onnx.features.is_tf_available''',       A      ):
                   _UpperCamelCase              =						FeaturesManager.determine_framework(self.test_model      )
                   self.assertEqual(A,       self.framework_pt      )

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

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

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






