





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				(   __UpperCamelCase     ):
 def 	A_      ( self    : Dict      )     ->      Any:
     lowerCamelCase__       : List[str]   					=						SMALL_MODEL_IDENTIFIER
     lowerCamelCase__       : Any   					=						'pt'
     lowerCamelCase__       : Optional[Any]   					=						'tf'
 def 	A_      ( self    : str     ,	UpperCAmelCase    : Tuple      )     ->      int:
     lowerCamelCase__       : Union[str, Any]   					=						AutoModel.from_pretrained(self.test_model      )
     model_pt.save_pretrained(UpperCAmelCase      )
 def 	A_      ( self    : Tuple     ,	UpperCAmelCase    : Optional[Any]      )     ->      Optional[Any]:
     lowerCamelCase__       : Tuple   					=						TFAutoModel.from_pretrained(self.test_model     ,	from_pt=UpperCAmelCase      )
     model_tf.save_pretrained(UpperCAmelCase      )
 def 	A_      ( self    : Union[str, Any]      )     ->      Union[str, Any]:
     lowerCamelCase__       : str   					=						'mock_framework'

     # Framework provided - return whatever the user provides
     lowerCamelCase__       : 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      )
         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__       : Any   					=						FeaturesManager.determine_framework(UpperCAmelCase     ,	UpperCAmelCase      )
         self.assertEqual(UpperCAmelCase     ,	UpperCAmelCase      )
 def 	A_      ( self    : Dict      )     ->      Optional[Any]:
     # PyTorch checkpoint
     with TemporaryDirectory() as local_pt_ckpt:
         self._setup_pt_ckpt(UpperCAmelCase      )
         lowerCamelCase__       : List[Any]   					=						FeaturesManager.determine_framework(UpperCAmelCase      )
         self.assertEqual(UpperCAmelCase     ,	self.framework_pt      )

     # TensorFlow checkpoint
     with TemporaryDirectory() as local_tf_ckpt:
         self._setup_tf_ckpt(UpperCAmelCase      )
         lowerCamelCase__       : 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      ):
             lowerCamelCase__       : Optional[int]   					=						FeaturesManager.determine_framework(UpperCAmelCase      )







 def 	A_      ( self    : str      )     ->      List[str]:
     lowerCamelCase__       : List[str]   					=						MagicMock(return_value=UpperCAmelCase      )
     with patch('transformers.onnx.features.is_tf_available'     ,	UpperCAmelCase      ):
         lowerCamelCase__       : Optional[int]   					=						FeaturesManager.determine_framework(self.test_model      )
         self.assertEqual(UpperCAmelCase     ,	self.framework_pt      )

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

     # Both in environment -> use PyTorch
     lowerCamelCase__       : Optional[Any]   					=						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      ):
         lowerCamelCase__       : Optional[Any]   					=						FeaturesManager.determine_framework(self.test_model      )
         self.assertEqual(UpperCAmelCase     ,	self.framework_pt      )

     # Both not in environment -> raise error
     lowerCamelCase__       : int   					=						MagicMock(return_value=UpperCAmelCase      )
     lowerCamelCase__       : int   					=						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__       : int   					=						FeaturesManager.determine_framework(self.test_model      )












from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow


if is_tf_available():
    import tensorflow as tf

    from transformers import AutoTokenizer, TFAutoModelForSeqaSeqLM

@require_tf
@require_sentencepiece
@require_tokenizers
class   lowerCAmelCase				(   unittest.TestCase     ):
 @slow
 def 	A_      ( self    : int      )     ->      Optional[Any]:
     lowerCamelCase__       : Tuple   					=						TFAutoModelForSeqaSeqLM.from_pretrained('google/mt5-small'      )
     lowerCamelCase__       : Any   					=						AutoTokenizer.from_pretrained('google/mt5-small'      )

     lowerCamelCase__       : int   					=						tokenizer('Hello there'     ,	return_tensors='tf'      ).input_ids
     lowerCamelCase__       : int   					=						tokenizer('Hi I am'     ,	return_tensors='tf'      ).input_ids

     lowerCamelCase__       : List[str]   					=						model(UpperCAmelCase     ,	labels=UpperCAmelCase      ).loss
     lowerCamelCase__       : List[Any]   					=						-tf.math.reduce_mean(UpperCAmelCase      ).numpy()

     lowerCamelCase__       : Dict   					=						-2_1.2_2_8_1_6_8
     self.assertTrue(abs(mtf_score - EXPECTED_SCORE      ) < 2e-4      )





