





"""simple docstring"""





import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)






class 					_lowerCAmelCase   (     unittest.TestCase       ):
       """simple docstring"""




       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:List[str]											=							['a', 'b', 'c']

         # Defaults to last layer if both are None
         lowerCAmelCase__							,				lowerCAmelCase__			:Tuple											=							get_aligned_output_features_output_indices(__UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      )
         self.assertEqual(__UpperCAmelCase    , ['c']      )
         self.assertEqual(__UpperCAmelCase    , [2]      )

         # Out indices set to match out features
         lowerCAmelCase__							,				lowerCAmelCase__			:Optional[int]											=							get_aligned_output_features_output_indices(['a', 'c']    , __UpperCAmelCase    , __UpperCAmelCase      )
         self.assertEqual(__UpperCAmelCase    , ['a', 'c']      )
         self.assertEqual(__UpperCAmelCase    , [0, 2]      )

         # Out features set to match out indices
         lowerCAmelCase__							,				lowerCAmelCase__			:int											=							get_aligned_output_features_output_indices(__UpperCAmelCase    , [0, 2]    , __UpperCAmelCase      )
         self.assertEqual(__UpperCAmelCase    , ['a', 'c']      )
         self.assertEqual(__UpperCAmelCase    , [0, 2]      )

         # Out features selected from negative indices
         lowerCAmelCase__							,				lowerCAmelCase__			:Optional[Any]											=							get_aligned_output_features_output_indices(__UpperCAmelCase    , [-3, -1]    , __UpperCAmelCase      )
         self.assertEqual(__UpperCAmelCase    , ['a', 'c']      )
         self.assertEqual(__UpperCAmelCase    , [-3, -1]      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         with self.assertRaises(__UpperCAmelCase      ):
           verify_out_features_out_indices(['a', 'b']    , (0, 1)    , __UpperCAmelCase      )

         # Out features must be a list
         with self.assertRaises(__UpperCAmelCase      ):
           verify_out_features_out_indices(('a', 'b')    , (0, 1)    , ['a', 'b']      )

         # Out features must be a subset of stage names
         with self.assertRaises(__UpperCAmelCase      ):
           verify_out_features_out_indices(['a', 'b']    , (0, 1)    , ['a']      )

         # Out indices must be a list or tuple
         with self.assertRaises(__UpperCAmelCase      ):
           verify_out_features_out_indices(__UpperCAmelCase    , 0    , ['a', 'b']      )

         # Out indices must be a subset of stage names
         with self.assertRaises(__UpperCAmelCase      ):
           verify_out_features_out_indices(__UpperCAmelCase    , (0, 1)    , ['a']      )

         # Out features and out indices must be the same length
         with self.assertRaises(__UpperCAmelCase      ):
           verify_out_features_out_indices(['a', 'b']    , (0,)    , ['a', 'b', 'c']      )

         # Out features should match out indices
         with self.assertRaises(__UpperCAmelCase      ):
           verify_out_features_out_indices(['a', 'b']    , (0, 2)    , ['a', 'b', 'c']      )

         # Out features and out indices should be in order
         with self.assertRaises(__UpperCAmelCase      ):
           verify_out_features_out_indices(['b', 'a']    , (0, 1)    , ['a', 'b']      )

         # Check passes with valid inputs
         verify_out_features_out_indices(['a', 'b', 'd']    , (0, 1, -1)    , ['a', 'b', 'c', 'd']      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Optional[int]											=							BackboneMixin()

         lowerCAmelCase__			:Tuple											=							['a', 'b', 'c']
         lowerCAmelCase__			:Optional[Any]											=							['a', 'c']
         lowerCAmelCase__			:Union[str, Any]											=							[0, 2]

         # Check that the output features and indices are set correctly
         self.assertEqual(backbone.out_features    , ['a', 'c']      )
         self.assertEqual(backbone.out_indices    , [0, 2]      )

         # Check out features and indices are updated correctly
         lowerCAmelCase__			:List[str]											=							['a', 'b']
         self.assertEqual(backbone.out_features    , ['a', 'b']      )
         self.assertEqual(backbone.out_indices    , [0, 1]      )

         lowerCAmelCase__			:int											=							[-3, -1]
         self.assertEqual(backbone.out_features    , ['a', 'c']      )
         self.assertEqual(backbone.out_indices    , [-3, -1]      )







"""simple docstring"""





import tempfile
import unittest

from transformers import AutoModelForSeqaSeqLM, AutoTokenizer
from transformers.testing_utils import (
    is_torch_available,
    require_optimum,
    require_torch,
    slow,
)


if is_torch_available():
  import torch






@require_torch
@require_optimum
@slow
class 					_lowerCAmelCase   (     unittest.TestCase       ):
       """simple docstring"""




       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Optional[Any]											=							'hf-internal-testing/tiny-random-t5'
         lowerCAmelCase__			:List[Any]											=							AutoTokenizer.from_pretrained(__UpperCAmelCase      )
         lowerCAmelCase__			:str											=							AutoModelForSeqaSeqLM.from_pretrained(__UpperCAmelCase      )

         lowerCAmelCase__			:Any											=							tokenizer('This is me'    , return_tensors='pt'      )

         lowerCAmelCase__			:Dict											=							model.to_bettertransformer()

         self.assertTrue(any('BetterTransformer' in mod.__class__.__name__ for _, mod in model.named_modules()      )      )

         lowerCAmelCase__			:Optional[Any]											=							model.generate(**__UpperCAmelCase      )

         lowerCAmelCase__			:List[Any]											=							model.reverse_bettertransformer()

         self.assertFalse(any('BetterTransformer' in mod.__class__.__name__ for _, mod in model.named_modules()      )      )

         with tempfile.TemporaryDirectory() as tmpdirname:
           model.save_pretrained(__UpperCAmelCase      )

           lowerCAmelCase__			:Any											=							AutoModelForSeqaSeqLM.from_pretrained(__UpperCAmelCase      )

           self.assertFalse(
               any('BetterTransformer' in mod.__class__.__name__ for _, mod in model_reloaded.named_modules()      )      )

           lowerCAmelCase__			:Union[str, Any]											=							model_reloaded.generate(**__UpperCAmelCase      )
           self.assertTrue(torch.allclose(__UpperCAmelCase    , __UpperCAmelCase      )      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:int											=							'hf-internal-testing/tiny-random-t5'
         lowerCAmelCase__			:Union[str, Any]											=							AutoModelForSeqaSeqLM.from_pretrained(__UpperCAmelCase      )

         lowerCAmelCase__			:str											=							model.to_bettertransformer()

         with tempfile.TemporaryDirectory() as tmpdirname:
           with self.assertRaises(__UpperCAmelCase      ):
             model.save_pretrained(__UpperCAmelCase      )

           lowerCAmelCase__			:Optional[int]											=							model.reverse_bettertransformer()
           model.save_pretrained(__UpperCAmelCase      )
