




from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase_					    = logging.get_logger(__name__)

lowerCAmelCase_					    = {
    """microsoft/trocr-base-handwritten""": (
        """https://huggingface.co/microsoft/trocr-base-handwritten/resolve/main/config.json"""
    ),
    # See all TrOCR models at https://huggingface.co/models?filter=trocr
}






class  _lowerCAmelCase     (  _lowercase	):
      A__		     =		'trocr'
      A__		     =		['past_key_values']
      A__		     =		{
          'num_attention_heads': 'decoder_attention_heads',
          'hidden_size': 'd_model',
          'num_hidden_layers': 'decoder_layers',
      }

      def __init__(   self      ,				__UpperCAmelCase=5_0265      ,				__UpperCAmelCase=1024      ,				__UpperCAmelCase=12      ,				__UpperCAmelCase=16      ,				__UpperCAmelCase=4096      ,				__UpperCAmelCase="gelu"      ,				__UpperCAmelCase=512      ,				__UpperCAmelCase=0.1      ,				__UpperCAmelCase=0.0      ,				__UpperCAmelCase=0.0      ,				__UpperCAmelCase=2      ,				__UpperCAmelCase=0.02      ,				__UpperCAmelCase=0.0      ,				__UpperCAmelCase=True      ,				__UpperCAmelCase=False      ,				__UpperCAmelCase=True      ,				__UpperCAmelCase=True      ,				__UpperCAmelCase=1      ,				__UpperCAmelCase=0      ,				__UpperCAmelCase=2      ,				**__UpperCAmelCase      ,				):
        lowerCAmelCase__				:  Tuple		       =     vocab_size
        lowerCAmelCase__				:  Any		       =     d_model
        lowerCAmelCase__				:  Optional[Any]		       =     decoder_layers
        lowerCAmelCase__				:  Tuple		       =     decoder_attention_heads
        lowerCAmelCase__				:  List[Any]		       =     decoder_ffn_dim
        lowerCAmelCase__				:  Any		       =     activation_function
        lowerCAmelCase__				:  Optional[int]		       =     max_position_embeddings
        lowerCAmelCase__				:  Union[str, Any]		       =     dropout
        lowerCAmelCase__				:  Dict		       =     attention_dropout
        lowerCAmelCase__				:  Any		       =     activation_dropout
        lowerCAmelCase__				:  Any		       =     init_std
        lowerCAmelCase__				:  int		       =     decoder_layerdrop
        lowerCAmelCase__				:  Optional[int]		       =     use_cache
        lowerCAmelCase__				:  Optional[Any]		       =     scale_embedding
        lowerCAmelCase__				:  List[str]		       =     use_learned_position_embeddings
        lowerCAmelCase__				:  List[Any]		       =     layernorm_embedding

        super().__init__(
            pad_token_id=__UpperCAmelCase      ,				bos_token_id=__UpperCAmelCase      ,				eos_token_id=__UpperCAmelCase      ,				decoder_start_token_id=__UpperCAmelCase      ,				**__UpperCAmelCase      ,				)







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

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  Union[str, Any]		       =     SMALL_MODEL_IDENTIFIER
        lowerCAmelCase__				:  List[str]		       =     '''pt'''
        lowerCAmelCase__				:  int		       =     '''tf'''

      def 	__magic_name__(   self      ,				__UpperCAmelCase		):
        lowerCAmelCase__				:  Optional[Any]		       =     AutoModel.from_pretrained(self.test_model		)
        model_pt.save_pretrained(__UpperCAmelCase		)

      def 	__magic_name__(   self      ,				__UpperCAmelCase		):
        lowerCAmelCase__				:  str		       =     TFAutoModel.from_pretrained(self.test_model      ,				from_pt=__UpperCAmelCase		)
        model_tf.save_pretrained(__UpperCAmelCase		)

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  int		       =     '''mock_framework'''

        # Framework provided - return whatever the user provides
        lowerCAmelCase__				:  Tuple		       =     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		)
          lowerCAmelCase__				:  int		       =     FeaturesManager.determine_framework(__UpperCAmelCase      ,				__UpperCAmelCase		)
          self.assertEqual(__UpperCAmelCase      ,				__UpperCAmelCase		)

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
          self._setup_tf_ckpt(__UpperCAmelCase		)
          lowerCAmelCase__				:  List[str]		       =     FeaturesManager.determine_framework(__UpperCAmelCase      ,				__UpperCAmelCase		)
          self.assertEqual(__UpperCAmelCase      ,				__UpperCAmelCase		)

      def 	__magic_name__(   self		):
        # PyTorch checkpoint
        with TemporaryDirectory() as local_pt_ckpt:
          self._setup_pt_ckpt(__UpperCAmelCase		)
          lowerCAmelCase__				:  Tuple		       =     FeaturesManager.determine_framework(__UpperCAmelCase		)
          self.assertEqual(__UpperCAmelCase      ,				self.framework_pt		)

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
          self._setup_tf_ckpt(__UpperCAmelCase		)
          lowerCAmelCase__				:  int		       =     FeaturesManager.determine_framework(__UpperCAmelCase		)
          self.assertEqual(__UpperCAmelCase      ,				self.framework_tf		)

        # Invalid local checkpoint
        with TemporaryDirectory() as local_invalid_ckpt:
          with self.assertRaises(__UpperCAmelCase		):
            lowerCAmelCase__				:  Tuple		       =     FeaturesManager.determine_framework(__UpperCAmelCase		)







      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  List[Any]		       =     MagicMock(return_value=__UpperCAmelCase		)
        with patch('''transformers.onnx.features.is_tf_available'''      ,				__UpperCAmelCase		):
          lowerCAmelCase__				:  int		       =     FeaturesManager.determine_framework(self.test_model		)
          self.assertEqual(__UpperCAmelCase      ,				self.framework_pt		)

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

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

        # Both not in environment -> raise error
        lowerCAmelCase__				:  Optional[int]		       =     MagicMock(return_value=__UpperCAmelCase		)
        lowerCAmelCase__				:  Dict		       =     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		):
            lowerCAmelCase__				:  Tuple		       =     FeaturesManager.determine_framework(self.test_model		)

