'''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 lowercase_       (		_UpperCamelCase			):







   """simple docstring"""

   def 	__UpperCAmelCase   (      self      :			Dict     )  ->			List[str]:
        _A    		=  SMALL_MODEL_IDENTIFIER
        _A    		=  'pt'
        _A    		=  'tf'
   def 	__UpperCAmelCase   (      self      :			Optional[int], UpperCamelCase__      :			Any     )  ->			Union[str, Any]:
        _A    		=  AutoModel.from_pretrained(self.test_model     )
        model_pt.save_pretrained(UpperCamelCase__     )
   def 	__UpperCAmelCase   (      self      :			Any, UpperCamelCase__      :			Optional[int]     )  ->			List[Any]:
        _A    		=  TFAutoModel.from_pretrained(self.test_model, from_pt=UpperCamelCase__     )
        model_tf.save_pretrained(UpperCamelCase__     )
   def 	__UpperCAmelCase   (      self      :			Dict     )  ->			Tuple:
        _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 	__UpperCAmelCase   (      self      :			Dict     )  ->			List[str]:
        # 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 	__UpperCAmelCase   (      self      :			Optional[int]     )  ->			Union[str, Any]:
        _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     )




'''simple docstring'''






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 numpy as np
   import tensorflow as tf

   from transformers import TFCamembertModel







@require_tf
@require_sentencepiece
@require_tokenizers
class lowercase_       (		unittest.TestCase			):







   """simple docstring"""

   @slow
   def 	__UpperCAmelCase   (      self      :			List[str]     )  ->			Any:
        _A    		=  TFCamembertModel.from_pretrained('jplu/tf-camembert-base'     )

        _A    		=  tf.convert_to_tensor(
            [[5, 1_21, 11, 6_60, 16, 7_30, 2_55_43, 1_10, 83, 6]], dtype=tf.intaa, )  # J'aime le camembert !"

        _A    		=  model(UpperCamelCase__     )['last_hidden_state']
        _A    		=  tf.TensorShape((1, 10, 7_68)     )
        self.assertEqual(output.shape, UpperCamelCase__     )
        # compare the actual values for a slice.
        _A    		=  tf.convert_to_tensor(
            [[[-0.0_254, 0.0_235, 0.1_027], [0.0_606, -0.1_811, -0.0_418], [-0.1_561, -0.1_127, 0.2_687]]], dtype=tf.floataa, )
        # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
        # camembert.eval()
        # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach()

        self.assertTrue(np.allclose(output[:, :3, :3].numpy(), expected_slice.numpy(), atol=1e-4     )     )



