'''simple docstring'''






import unittest

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







class lowercase_       (		unittest.TestCase			):







   """simple docstring"""

   def 	__UpperCAmelCase   (      self      :			int     )  ->			Dict:
        _A    		=  ['a', 'b', 'c']

        # Defaults to last layer if both are None
        _A		,       _A    		=  get_aligned_output_features_output_indices(UpperCamelCase__, UpperCamelCase__, UpperCamelCase__     )
        self.assertEqual(UpperCamelCase__, ['c']     )
        self.assertEqual(UpperCamelCase__, [2]     )

        # Out indices set to match out features
        _A		,       _A    		=  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
        _A		,       _A    		=  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
        _A		,       _A    		=  get_aligned_output_features_output_indices(UpperCamelCase__, [-3, -1], UpperCamelCase__     )
        self.assertEqual(UpperCamelCase__, ['a', 'c']     )
        self.assertEqual(UpperCamelCase__, [-3, -1]     )
   def 	__UpperCAmelCase   (      self      :			Union[str, Any]     )  ->			Tuple:
        # Stage names must be set
        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 	__UpperCAmelCase   (      self      :			Any     )  ->			Optional[int]:
        _A    		=  BackboneMixin()

        _A    		=  ['a', 'b', 'c']
        _A    		=  ['a', 'c']
        _A    		=  [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
        _A    		=  ['a', 'b']
        self.assertEqual(backbone.out_features, ['a', 'b']     )
        self.assertEqual(backbone.out_indices, [0, 1]     )

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




'''simple docstring'''






import unittest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TextaTextGenerationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch
from transformers.utils import is_torch_available

from .test_pipelines_common import ANY


if is_torch_available():
   import torch







@is_pipeline_test
class lowercase_       (		unittest.TestCase			):







   """simple docstring"""

   __lowerCAmelCase         =   MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
   __lowerCAmelCase         =   TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
   def 	__UpperCAmelCase   (      self      :			Any, UpperCamelCase__      :			str, UpperCamelCase__      :			Optional[int], UpperCamelCase__      :			Any     )  ->			List[str]:
        _A    		=  TextaTextGenerationPipeline(model=UpperCamelCase__, tokenizer=UpperCamelCase__     )
        return generator, ["Something to write", "Something else"]
   def 	__UpperCAmelCase   (      self      :			Any, UpperCamelCase__      :			Tuple, UpperCamelCase__      :			Optional[Any]     )  ->			List[str]:
        _A    		=  generator('Something there'     )
        self.assertEqual(UpperCamelCase__, [{'generated_text': ANY(UpperCamelCase__     )}]     )
        # These are encoder decoder, they don't just append to incoming string
        self.assertFalse(outputs[0]['generated_text'].startswith('Something there'     )     )

        _A    		=  generator(['This is great !', 'Something else'], num_return_sequences=2, do_sample=UpperCamelCase__     )
        self.assertEqual(
            UpperCamelCase__, [
                [{'generated_text': ANY(UpperCamelCase__     )}, {'generated_text': ANY(UpperCamelCase__     )}],
                [{'generated_text': ANY(UpperCamelCase__     )}, {'generated_text': ANY(UpperCamelCase__     )}],
            ], )

        _A    		=  generator(
            ['This is great !', 'Something else'], num_return_sequences=2, batch_size=2, do_sample=UpperCamelCase__     )
        self.assertEqual(
            UpperCamelCase__, [
                [{'generated_text': ANY(UpperCamelCase__     )}, {'generated_text': ANY(UpperCamelCase__     )}],
                [{'generated_text': ANY(UpperCamelCase__     )}, {'generated_text': ANY(UpperCamelCase__     )}],
            ], )

        with self.assertRaises(UpperCamelCase__     ):
             generator(4     )
   @require_torch
   def 	__UpperCAmelCase   (      self      :			Union[str, Any]     )  ->			Optional[Any]:
        _A    		=  pipeline('text2text-generation', model='patrickvonplaten/t5-tiny-random', framework='pt'     )
        # do_sample=False necessary for reproducibility
        _A    		=  generator('Something there', do_sample=UpperCamelCase__     )
        self.assertEqual(UpperCamelCase__, [{'generated_text': ''}]     )

        _A    		=  3
        _A    		=  generator(
            'Something there', num_return_sequences=UpperCamelCase__, num_beams=UpperCamelCase__, )
        _A    		=  [
            {'generated_text': 'Beide Beide Beide Beide Beide Beide Beide Beide Beide'},
            {'generated_text': 'Beide Beide Beide Beide Beide Beide Beide Beide'},
            {'generated_text': ''},
        ]
        self.assertEqual(UpperCamelCase__, UpperCamelCase__     )

        _A    		=  generator('This is a test', do_sample=UpperCamelCase__, num_return_sequences=2, return_tensors=UpperCamelCase__     )
        self.assertEqual(
            UpperCamelCase__, [
                {'generated_token_ids': ANY(torch.Tensor     )},
                {'generated_token_ids': ANY(torch.Tensor     )},
            ], )
        _A    		=  generator.model.config.eos_token_id
        _A    		=  '<pad>'
        _A    		=  generator(
            ['This is a test', 'This is a second test'], do_sample=UpperCamelCase__, num_return_sequences=2, batch_size=2, return_tensors=UpperCamelCase__, )
        self.assertEqual(
            UpperCamelCase__, [
                [
                    {'generated_token_ids': ANY(torch.Tensor     )},
                    {'generated_token_ids': ANY(torch.Tensor     )},
                ],
                [
                    {'generated_token_ids': ANY(torch.Tensor     )},
                    {'generated_token_ids': ANY(torch.Tensor     )},
                ],
            ], )

   @require_tf
   def 	__UpperCAmelCase   (      self      :			Optional[Any]     )  ->			Optional[int]:
        _A    		=  pipeline('text2text-generation', model='patrickvonplaten/t5-tiny-random', framework='tf'     )
        # do_sample=False necessary for reproducibility
        _A    		=  generator('Something there', do_sample=UpperCamelCase__     )
        self.assertEqual(UpperCamelCase__, [{'generated_text': ''}]     )



