






'''simple docstring'''

import unittest

from transformers import (
    MODEL_FOR_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
    TextGenerationPipeline,
    logging,
    pipeline,
)
from transformers.testing_utils import (
    CaptureLogger,
    is_pipeline_test,
    require_accelerate,
    require_tf,
    require_torch,
    require_torch_gpu,
    require_torch_or_tf,
)

from .test_pipelines_common import ANY







@is_pipeline_test
@require_torch_or_tf
class 	SCREAMING_SNAKE_CASE	(      unittest.TestCase  ):
       snake_case__    :     str   		=			MODEL_FOR_CAUSAL_LM_MAPPING
       snake_case__    :     Optional[Any]   		=			TF_MODEL_FOR_CAUSAL_LM_MAPPING




       @require_torch
       def 					a_					(					self      : Dict					):


            """simple docstring"""







            __lowerCamelCase						:       int				   =				pipeline(task="""text-generation"""	,    model="""sshleifer/tiny-ctrl"""	,    framework="""pt"""					)
            # Using `do_sample=False` to force deterministic output
            __lowerCamelCase						:       Union[str, Any]				   =				text_generator("""This is a test"""	,    do_sample=A__					)
            self.assertEqual(
                A__	,    [
                    {
                        """generated_text""": (
                            """This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope."""
                            """ oscope. FiliFili@@"""
                        )
                    }
                ]	,    )

            __lowerCamelCase						:       Optional[Any]				   =				text_generator(["""This is a test""", """This is a second test"""]					)
            self.assertEqual(
                A__	,    [
                    [
                        {
                            """generated_text""": (
                                """This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope."""
                                """ oscope. FiliFili@@"""
                            )
                        }
                    ],
                    [
                        {
                            """generated_text""": (
                                """This is a second test ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy"""
                                """ oscope. oscope. FiliFili@@"""
                            )
                        }
                    ],
                ]	,    )

            __lowerCamelCase						:       Optional[Any]				   =				text_generator("""This is a test"""	,    do_sample=A__	,    num_return_sequences=2	,    return_tensors=A__					)
            self.assertEqual(
                A__	,    [
                    {"""generated_token_ids""": ANY(A__					)},
                    {"""generated_token_ids""": ANY(A__					)},
                ]	,    )
            __lowerCamelCase						:       List[Any]				   =				text_generator.model.config.eos_token_id
            __lowerCamelCase						:       Any				   =				"""<pad>"""
            __lowerCamelCase						:       Optional[int]				   =				text_generator(
                ["""This is a test""", """This is a second test"""]	,    do_sample=A__	,    num_return_sequences=2	,    batch_size=2	,    return_tensors=A__	,    )
            self.assertEqual(
                A__	,    [
                    [
                        {"""generated_token_ids""": ANY(A__					)},
                        {"""generated_token_ids""": ANY(A__					)},
                    ],
                    [
                        {"""generated_token_ids""": ANY(A__					)},
                        {"""generated_token_ids""": ANY(A__					)},
                    ],
                ]	,    )




       @require_tf
       def 					a_					(					self      : Optional[int]					):


            """simple docstring"""







            __lowerCamelCase						:       str				   =				pipeline(task="""text-generation"""	,    model="""sshleifer/tiny-ctrl"""	,    framework="""tf"""					)

            # Using `do_sample=False` to force deterministic output
            __lowerCamelCase						:       Union[str, Any]				   =				text_generator("""This is a test"""	,    do_sample=A__					)
            self.assertEqual(
                A__	,    [
                    {
                        """generated_text""": (
                            """This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵"""
                            """ please,"""
                        )
                    }
                ]	,    )

            __lowerCamelCase						:       List[str]				   =				text_generator(["""This is a test""", """This is a second test"""]	,    do_sample=A__					)
            self.assertEqual(
                A__	,    [
                    [
                        {
                            """generated_text""": (
                                """This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵"""
                                """ please,"""
                            )
                        }
                    ],
                    [
                        {
                            """generated_text""": (
                                """This is a second test Chieftain Chieftain prefecture prefecture prefecture Cannes Cannes"""
                                """ Cannes 閲閲Cannes Cannes Cannes 攵 please,"""
                            )
                        }
                    ],
                ]	,    )




       def 					a_					(					self      : Any	,    A__      : Optional[int]	,    A__      : str	,    A__      : int					):


            """simple docstring"""







            __lowerCamelCase						:       str				   =				TextGenerationPipeline(model=A__	,    tokenizer=A__					)
            return text_generator, ["This is a test", "Another test"]




       def 					a_					(					self      : Dict					):


            """simple docstring"""







            __lowerCamelCase						:       Any				   =				"""Hello I believe in"""
            __lowerCamelCase						:       Union[str, Any]				   =				pipeline("""text-generation"""	,    model="""hf-internal-testing/tiny-random-gpt2"""					)
            __lowerCamelCase						:       str				   =				text_generator(A__					)
            self.assertEqual(
                A__	,    [{"""generated_text""": """Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe"""}]	,    )

            __lowerCamelCase						:       str				   =				text_generator(A__	,    stop_sequence=""" fe"""					)
            self.assertEqual(A__	,    [{"""generated_text""": """Hello I believe in fe"""}]					)




       def 					a_					(					self      : Tuple	,    A__      : Tuple	,    A__      : Union[str, Any]					):


            """simple docstring"""







            __lowerCamelCase						:       Optional[int]				   =				text_generator.model
            __lowerCamelCase						:       str				   =				text_generator.tokenizer

            __lowerCamelCase						:       Dict				   =				text_generator("""This is a test"""					)
            self.assertEqual(A__	,    [{"""generated_text""": ANY(A__					)}]					)
            self.assertTrue(outputs[0]["""generated_text"""].startswith("""This is a test"""					)					)

            __lowerCamelCase						:       List[Any]				   =				text_generator("""This is a test"""	,    return_full_text=A__					)
            self.assertEqual(A__	,    [{"""generated_text""": ANY(A__					)}]					)
            self.assertNotIn("""This is a test"""	,    outputs[0]["""generated_text"""]					)

            __lowerCamelCase						:       Optional[Any]				   =				pipeline(task="""text-generation"""	,    model=A__	,    tokenizer=A__	,    return_full_text=A__					)
            __lowerCamelCase						:       List[Any]				   =				text_generator("""This is a test"""					)
            self.assertEqual(A__	,    [{"""generated_text""": ANY(A__					)}]					)
            self.assertNotIn("""This is a test"""	,    outputs[0]["""generated_text"""]					)

            __lowerCamelCase						:       Optional[Any]				   =				text_generator("""This is a test"""	,    return_full_text=A__					)
            self.assertEqual(A__	,    [{"""generated_text""": ANY(A__					)}]					)
            self.assertTrue(outputs[0]["""generated_text"""].startswith("""This is a test"""					)					)

            __lowerCamelCase						:       Tuple				   =				text_generator(["""This is great !""", """Something else"""]	,    num_return_sequences=2	,    do_sample=A__					)
            self.assertEqual(
                A__	,    [
                    [{"""generated_text""": ANY(A__					)}, {"""generated_text""": ANY(A__					)}],
                    [{"""generated_text""": ANY(A__					)}, {"""generated_text""": ANY(A__					)}],
                ]	,    )

            if text_generator.tokenizer.pad_token is not None:
                 __lowerCamelCase						:       List[str]				   =				text_generator(
                     ["""This is great !""", """Something else"""]	,    num_return_sequences=2	,    batch_size=2	,    do_sample=A__					)
                 self.assertEqual(
                     A__	,    [
                         [{"""generated_text""": ANY(A__					)}, {"""generated_text""": ANY(A__					)}],
                         [{"""generated_text""": ANY(A__					)}, {"""generated_text""": ANY(A__					)}],
                     ]	,    )

            with self.assertRaises(A__					):
                 __lowerCamelCase						:       List[Any]				   =				text_generator("""test"""	,    return_full_text=A__	,    return_text=A__					)
            with self.assertRaises(A__					):
                 __lowerCamelCase						:       List[Any]				   =				text_generator("""test"""	,    return_full_text=A__	,    return_tensors=A__					)
            with self.assertRaises(A__					):
                 __lowerCamelCase						:       int				   =				text_generator("""test"""	,    return_text=A__	,    return_tensors=A__					)

            # Empty prompt is slighly special
            # it requires BOS token to exist.
            # Special case for Pegasus which will always append EOS so will
            # work even without BOS.
            if (
                text_generator.tokenizer.bos_token_id is not None
                or "Pegasus" in tokenizer.__class__.__name__
                or "Git" in model.__class__.__name__
            ):
                 __lowerCamelCase						:       Union[str, Any]				   =				text_generator(""""""					)
                 self.assertEqual(A__	,    [{"""generated_text""": ANY(A__					)}]					)
            else:
                 with self.assertRaises((ValueError, AssertionError)					):
                      __lowerCamelCase						:       Optional[Any]				   =				text_generator(""""""					)

            if text_generator.framework == "tf":
                 # TF generation does not support max_new_tokens, and it's impossible
                 # to control long generation with only max_length without
                 # fancy calculation, dismissing tests for now.
                 return
            # We don't care about infinite range models.
            # They already work.
            # Skip this test for XGLM, since it uses sinusoidal positional embeddings which are resized on-the-fly.
            __lowerCamelCase						:       str				   =				["""RwkvForCausalLM""", """XGLMForCausalLM""", """GPTNeoXForCausalLM"""]
            if (
                tokenizer.model_max_length < 10000
                and text_generator.model.__class__.__name__ not in EXTRA_MODELS_CAN_HANDLE_LONG_INPUTS
            ):
                 # Handling of large generations
                 with self.assertRaises((RuntimeError, IndexError, ValueError, AssertionError)					):
                      text_generator("""This is a test""" * 500	,    max_new_tokens=20					)

                 __lowerCamelCase						:       Tuple				   =				text_generator("""This is a test""" * 500	,    handle_long_generation="""hole"""	,    max_new_tokens=20					)
                 # Hole strategy cannot work
                 with self.assertRaises(A__					):
                      text_generator(
                          """This is a test""" * 500	,    handle_long_generation="""hole"""	,    max_new_tokens=tokenizer.model_max_length + 10	,    )




       @require_torch
       @require_accelerate
       @require_torch_gpu
       def 					a_					(					self      : Dict					):


            """simple docstring"""







            import torch

            # Classic `model_kwargs`
            __lowerCamelCase						:       int				   =				pipeline(
                model="""hf-internal-testing/tiny-random-bloom"""	,    model_kwargs={"""device_map""": """auto""", """torch_dtype""": torch.bfloataa}	,    )
            self.assertEqual(pipe.model.device	,    torch.device(0					)					)
            self.assertEqual(pipe.model.lm_head.weight.dtype	,    torch.bfloataa					)
            __lowerCamelCase						:       Dict				   =				pipe("""This is a test"""					)
            self.assertEqual(
                A__	,    [
                    {
                        """generated_text""": (
                            """This is a test test test test test test test test test test test test test test test test"""
                            """ test"""
                        )
                    }
                ]	,    )

            # Upgraded those two to real pipeline arguments (they just get sent for the model as they're unlikely to mean anything else.)
            __lowerCamelCase						:       Optional[int]				   =				pipeline(model="""hf-internal-testing/tiny-random-bloom"""	,    device_map="""auto"""	,    torch_dtype=torch.bfloataa					)
            self.assertEqual(pipe.model.device	,    torch.device(0					)					)
            self.assertEqual(pipe.model.lm_head.weight.dtype	,    torch.bfloataa					)
            __lowerCamelCase						:       int				   =				pipe("""This is a test"""					)
            self.assertEqual(
                A__	,    [
                    {
                        """generated_text""": (
                            """This is a test test test test test test test test test test test test test test test test"""
                            """ test"""
                        )
                    }
                ]	,    )

            # torch_dtype will be automatically set to float32 if not provided - check: https://github.com/huggingface/transformers/pull/20602
            __lowerCamelCase						:       Tuple				   =				pipeline(model="""hf-internal-testing/tiny-random-bloom"""	,    device_map="""auto"""					)
            self.assertEqual(pipe.model.device	,    torch.device(0					)					)
            self.assertEqual(pipe.model.lm_head.weight.dtype	,    torch.floataa					)
            __lowerCamelCase						:       Any				   =				pipe("""This is a test"""					)
            self.assertEqual(
                A__	,    [
                    {
                        """generated_text""": (
                            """This is a test test test test test test test test test test test test test test test test"""
                            """ test"""
                        )
                    }
                ]	,    )




       @require_torch
       @require_torch_gpu
       def 					a_					(					self      : Union[str, Any]					):


            """simple docstring"""







            import torch

            __lowerCamelCase						:       Optional[Any]				   =				pipeline(model="""hf-internal-testing/tiny-random-bloom"""	,    device=0	,    torch_dtype=torch.floataa					)
            pipe("""This is a test"""					)




       @require_torch
       @require_accelerate
       @require_torch_gpu
       def 					a_					(					self      : Any					):


            """simple docstring"""







            import torch

            __lowerCamelCase						:       List[str]				   =				pipeline(model="""hf-internal-testing/tiny-random-bloom"""	,    device_map="""auto"""	,    torch_dtype=torch.floataa					)
            pipe("""This is a test"""	,    do_sample=A__	,    top_p=0.5					)




       def 					a_					(					self      : int					):


            """simple docstring"""







            __lowerCamelCase						:       Tuple				   =				"""Hello world"""
            __lowerCamelCase						:       Optional[Any]				   =				pipeline("""text-generation"""	,    model="""hf-internal-testing/tiny-random-gpt2"""					)
            if text_generator.model.framework == "tf":
                 __lowerCamelCase						:       Optional[Any]				   =				logging.get_logger("""transformers.generation.tf_utils"""					)
            else:
                 __lowerCamelCase						:       Optional[Any]				   =				logging.get_logger("""transformers.generation.utils"""					)
            __lowerCamelCase						:       Union[str, Any]				   =				"""Both `max_new_tokens`"""  # The beggining of the message to be checked in this test

            # Both are set by the user -> log warning
            with CaptureLogger(A__					) as cl:
                 __lowerCamelCase						:       str				   =				text_generator(A__	,    max_length=10	,    max_new_tokens=1					)
            self.assertIn(A__	,    cl.out					)

            # The user only sets one -> no warning
            with CaptureLogger(A__					) as cl:
                 __lowerCamelCase						:       int				   =				text_generator(A__	,    max_new_tokens=1					)
            self.assertNotIn(A__	,    cl.out					)

            with CaptureLogger(A__					) as cl:
                 __lowerCamelCase						:       Optional[int]				   =				text_generator(A__	,    max_length=10					)
            self.assertNotIn(A__	,    cl.out					)











'''simple docstring'''

from ...processing_utils import ProcessorMixin







class 	SCREAMING_SNAKE_CASE	(      lowerCAmelCase_  ):
       snake_case__    :     List[Any]   		=			'SpeechT5FeatureExtractor'
       snake_case__    :     Union[str, Any]   		=			'SpeechT5Tokenizer'




       def __init__(					self      : Union[str, Any]	,    A__      : str	,    A__      : Tuple					):


            """simple docstring"""







            super().__init__(A__	,    A__					)




       def __call__(					self      : List[Any]	,    *A__      : Any	,    **A__      : Optional[int]					):


            """simple docstring"""







            __lowerCamelCase						:       Any				   =				kwargs.pop("""audio"""	,    A__					)
            __lowerCamelCase						:       Any				   =				kwargs.pop("""text"""	,    A__					)
            __lowerCamelCase						:       Union[str, Any]				   =				kwargs.pop("""text_target"""	,    A__					)
            __lowerCamelCase						:       Union[str, Any]				   =				kwargs.pop("""audio_target"""	,    A__					)
            __lowerCamelCase						:       Any				   =				kwargs.pop("""sampling_rate"""	,    A__					)

            if audio is not None and text is not None:
                 raise ValueError(
                     """Cannot process both `audio` and `text` inputs. Did you mean `audio_target` or `text_target`?"""					)
            if audio_target is not None and text_target is not None:
                 raise ValueError(
                     """Cannot process both `audio_target` and `text_target` inputs. Did you mean `audio` or `text`?"""					)
            if audio is None and audio_target is None and text is None and text_target is None:
                 raise ValueError(
                     """You need to specify either an `audio`, `audio_target`, `text`, or `text_target` input to process."""					)

            if audio is not None:
                 __lowerCamelCase						:       Optional[int]				   =				self.feature_extractor(A__	,    *A__	,    sampling_rate=A__	,    **A__					)
            elif text is not None:
                 __lowerCamelCase						:       Optional[int]				   =				self.tokenizer(A__	,    **A__					)
            else:
                 __lowerCamelCase						:       Tuple				   =				None

            if audio_target is not None:
                 __lowerCamelCase						:       Optional[int]				   =				self.feature_extractor(audio_target=A__	,    *A__	,    sampling_rate=A__	,    **A__					)
                 __lowerCamelCase						:       Union[str, Any]				   =				targets["""input_values"""]
            elif text_target is not None:
                 __lowerCamelCase						:       Tuple				   =				self.tokenizer(A__	,    **A__					)
                 __lowerCamelCase						:       List[str]				   =				targets["""input_ids"""]
            else:
                 __lowerCamelCase						:       Dict				   =				None

            if inputs is None:
                 return targets

            if targets is not None:
                 __lowerCamelCase						:       str				   =				labels

                 __lowerCamelCase						:       int				   =				targets.get("""attention_mask"""					)
                 if decoder_attention_mask is not None:
                      __lowerCamelCase						:       Optional[int]				   =				decoder_attention_mask

            return inputs




       def 					a_					(					self      : Any	,    *A__      : Union[str, Any]	,    **A__      : List[str]					):


            """simple docstring"""







            __lowerCamelCase						:       Optional[int]				   =				kwargs.pop("""input_values"""	,    A__					)
            __lowerCamelCase						:       List[Any]				   =				kwargs.pop("""input_ids"""	,    A__					)
            __lowerCamelCase						:       List[Any]				   =				kwargs.pop("""labels"""	,    A__					)

            if input_values is not None and input_ids is not None:
                 raise ValueError("""Cannot process both `input_values` and `input_ids` inputs."""					)
            if input_values is None and input_ids is None and labels is None:
                 raise ValueError(
                     """You need to specify either an `input_values`, `input_ids`, or `labels` input to be padded."""					)

            if input_values is not None:
                 __lowerCamelCase						:       List[Any]				   =				self.feature_extractor.pad(A__	,    *A__	,    **A__					)
            elif input_ids is not None:
                 __lowerCamelCase						:       Any				   =				self.tokenizer.pad(A__	,    **A__					)
            else:
                 __lowerCamelCase						:       Any				   =				None

            if labels is not None:
                 if "input_ids" in labels or (isinstance(A__	,    A__					) and "input_ids" in labels[0]):
                      __lowerCamelCase						:       List[Any]				   =				self.tokenizer.pad(A__	,    **A__					)
                      __lowerCamelCase						:       Tuple				   =				targets["""input_ids"""]
                 else:
                      __lowerCamelCase						:       Optional[int]				   =				self.feature_extractor.feature_size
                      __lowerCamelCase						:       Any				   =				self.feature_extractor.num_mel_bins
                      __lowerCamelCase						:       List[str]				   =				self.feature_extractor.pad(A__	,    *A__	,    **A__					)
                      __lowerCamelCase						:       Dict				   =				feature_size_hack
                      __lowerCamelCase						:       List[Any]				   =				targets["""input_values"""]
            else:
                 __lowerCamelCase						:       Optional[int]				   =				None

            if inputs is None:
                 return targets

            if targets is not None:
                 __lowerCamelCase						:       Dict				   =				labels

                 __lowerCamelCase						:       List[str]				   =				targets.get("""attention_mask"""					)
                 if decoder_attention_mask is not None:
                      __lowerCamelCase						:       int				   =				decoder_attention_mask

            return inputs




       def 					a_					(					self      : Optional[Any]	,    *A__      : List[Any]	,    **A__      : Optional[int]					):


            """simple docstring"""







            return self.tokenizer.batch_decode(*A__	,    **A__					)




       def 					a_					(					self      : Any	,    *A__      : int	,    **A__      : Dict					):


            """simple docstring"""







            return self.tokenizer.decode(*A__	,    **A__					)



