'''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 		__A       (unittest.TestCase   ):
       snake_case       :Dict				    =					MODEL_FOR_CAUSAL_LM_MAPPING
       snake_case       :Tuple				    =					TF_MODEL_FOR_CAUSAL_LM_MAPPING






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

              __UpperCAmelCase  :				List[Any]       				=		text_generator(["This is a test", "This is a second test"]      )
              self.assertEqual(
                  UpperCamelCase_				,     [
                      [
                          {
                              "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@@"
                              )
                          }
                      ],
                  ]				,     )

              __UpperCAmelCase  :				Union[str, Any]       				=		text_generator("This is a test"				,     do_sample=UpperCamelCase_				,     num_return_sequences=2				,     return_tensors=UpperCamelCase_      )
              self.assertEqual(
                  UpperCamelCase_				,     [
                      {"generated_token_ids": ANY(UpperCamelCase_      )},
                      {"generated_token_ids": ANY(UpperCamelCase_      )},
                  ]				,     )
              __UpperCAmelCase  :				List[Any]       				=		text_generator.model.config.eos_token_id
              __UpperCAmelCase  :				Dict       				=		"<pad>"
              __UpperCAmelCase  :				int       				=		text_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(UpperCamelCase_      )},
                          {"generated_token_ids": ANY(UpperCamelCase_      )},
                      ],
                      [
                          {"generated_token_ids": ANY(UpperCamelCase_      )},
                          {"generated_token_ids": ANY(UpperCamelCase_      )},
                      ],
                  ]				,     )






       @require_tf
       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				List[Any]       				=		pipeline(task="text-generation"				,     model="sshleifer/tiny-ctrl"				,     framework="tf"      )

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

              __UpperCAmelCase  :				Union[str, Any]       				=		text_generator(["This is a test", "This is a second test"]				,     do_sample=UpperCamelCase_      )
              self.assertEqual(
                  UpperCamelCase_				,     [
                      [
                          {
                              "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 			_snake_case						(      self				,     UpperCamelCase_				,     UpperCamelCase_				,     UpperCamelCase_      ):
              __UpperCAmelCase  :				Optional[Any]       				=		TextGenerationPipeline(model=UpperCamelCase_				,     tokenizer=UpperCamelCase_      )
              return text_generator, ["This is a test", "Another test"]






       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				List[Any]       				=		"Hello I believe in"
              __UpperCAmelCase  :				Dict       				=		pipeline("text-generation"				,     model="hf-internal-testing/tiny-random-gpt2"      )
              __UpperCAmelCase  :				Optional[Any]       				=		text_generator(UpperCamelCase_      )
              self.assertEqual(
                  UpperCamelCase_				,     [{"generated_text": "Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe"}]				,     )

              __UpperCAmelCase  :				Union[str, Any]       				=		text_generator(UpperCamelCase_				,     stop_sequence=" fe"      )
              self.assertEqual(UpperCamelCase_				,     [{"generated_text": "Hello I believe in fe"}]      )






       def 			_snake_case						(      self				,     UpperCamelCase_				,     UpperCamelCase_      ):
              __UpperCAmelCase  :				Optional[int]       				=		text_generator.model
              __UpperCAmelCase  :				Dict       				=		text_generator.tokenizer

              __UpperCAmelCase  :				List[str]       				=		text_generator("This is a test"      )
              self.assertEqual(UpperCamelCase_				,     [{"generated_text": ANY(UpperCamelCase_      )}]      )
              self.assertTrue(outputs[0]["generated_text"].startswith("This is a test"      )      )

              __UpperCAmelCase  :				Optional[int]       				=		text_generator("This is a test"				,     return_full_text=UpperCamelCase_      )
              self.assertEqual(UpperCamelCase_				,     [{"generated_text": ANY(UpperCamelCase_      )}]      )
              self.assertNotIn("This is a test"				,     outputs[0]["generated_text"]      )

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

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

              __UpperCAmelCase  :				Optional[int]       				=		text_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_      )}],
                  ]				,     )

              if text_generator.tokenizer.pad_token is not None:
                     __UpperCAmelCase  :				Dict       				=		text_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_      ):
                     __UpperCAmelCase  :				Tuple       				=		text_generator("test"				,     return_full_text=UpperCamelCase_				,     return_text=UpperCamelCase_      )
              with self.assertRaises(UpperCamelCase_      ):
                     __UpperCAmelCase  :				Optional[Any]       				=		text_generator("test"				,     return_full_text=UpperCamelCase_				,     return_tensors=UpperCamelCase_      )
              with self.assertRaises(UpperCamelCase_      ):
                     __UpperCAmelCase  :				Dict       				=		text_generator("test"				,     return_text=UpperCamelCase_				,     return_tensors=UpperCamelCase_      )

              # 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__
              ):
                     __UpperCAmelCase  :				Any       				=		text_generator(""      )
                     self.assertEqual(UpperCamelCase_				,     [{"generated_text": ANY(UpperCamelCase_      )}]      )
              else:
                     with self.assertRaises((ValueError, AssertionError)      ):
                            __UpperCAmelCase  :				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.
              __UpperCAmelCase  :				int       				=		["RwkvForCausalLM", "XGLMForCausalLM", "GPTNeoXForCausalLM"]
              if (
                  tokenizer.model_max_length < 1_00_00
                  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" * 5_00				,     max_new_tokens=20      )

                     __UpperCAmelCase  :				Dict       				=		text_generator("This is a test" * 5_00				,     handle_long_generation="hole"				,     max_new_tokens=20      )
                     # Hole strategy cannot work
                     with self.assertRaises(UpperCamelCase_      ):
                            text_generator(
                                "This is a test" * 5_00				,     handle_long_generation="hole"				,     max_new_tokens=tokenizer.model_max_length + 10				,     )






       @require_torch
       @require_accelerate
       @require_torch_gpu
       def 			_snake_case						(      self      ):
              import torch

              # Classic `model_kwargs`
              __UpperCAmelCase  :				str       				=		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      )
              __UpperCAmelCase  :				Union[str, Any]       				=		pipe("This is a test"      )
              self.assertEqual(
                  UpperCamelCase_				,     [
                      {
                          "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.)
              __UpperCAmelCase  :				Union[str, Any]       				=		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      )
              __UpperCAmelCase  :				Dict       				=		pipe("This is a test"      )
              self.assertEqual(
                  UpperCamelCase_				,     [
                      {
                          "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
              __UpperCAmelCase  :				Any       				=		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      )
              __UpperCAmelCase  :				Any       				=		pipe("This is a test"      )
              self.assertEqual(
                  UpperCamelCase_				,     [
                      {
                          "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 			_snake_case						(      self      ):
              import torch

              __UpperCAmelCase  :				str       				=		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 			_snake_case						(      self      ):
              import torch

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







       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				Dict       				=		"Hello world"
              __UpperCAmelCase  :				Tuple       				=		pipeline("text-generation"				,     model="hf-internal-testing/tiny-random-gpt2"      )
              if text_generator.model.framework == "tf":
                     __UpperCAmelCase  :				List[str]       				=		logging.get_logger("transformers.generation.tf_utils"      )
              else:
                     __UpperCAmelCase  :				List[Any]       				=		logging.get_logger("transformers.generation.utils"      )
              __UpperCAmelCase  :				Optional[int]       				=		"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(UpperCamelCase_      ) as cl:
                     __UpperCAmelCase  :				List[str]       				=		text_generator(UpperCamelCase_				,     max_length=10				,     max_new_tokens=1      )
              self.assertIn(UpperCamelCase_				,     cl.out      )

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

              with CaptureLogger(UpperCamelCase_      ) as cl:
                     __UpperCAmelCase  :				List[str]       				=		text_generator(UpperCamelCase_				,     max_length=10      )
              self.assertNotIn(UpperCamelCase_				,     cl.out      )







'''simple docstring'''




from ...configuration_utils import PretrainedConfig
from ...utils import logging


_a   :	Any 							=   logging.get_logger(__name__)

_a   :	int 							=   {
    "facebook/s2t-wav2vec2-large-en-de": (
        "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json"
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2
}






class 		__A       (__magic_name__   ):
       snake_case       :Optional[int]				    =					"speech_to_text_2"
       snake_case       :List[Any]				    =					["past_key_values"]
       snake_case       :str				    =					{"num_attention_heads": "decoder_attention_heads", "hidden_size": "d_model"}






       def __init__(      self				,     UpperCamelCase_=1_00_00				,     UpperCamelCase_=6				,     UpperCamelCase_=20_48				,     UpperCamelCase_=4				,     UpperCamelCase_=0.0				,     UpperCamelCase_=True				,     UpperCamelCase_="relu"				,     UpperCamelCase_=2_56				,     UpperCamelCase_=0.1				,     UpperCamelCase_=0.0				,     UpperCamelCase_=0.0				,     UpperCamelCase_=0.0_2				,     UpperCamelCase_=2				,     UpperCamelCase_=True				,     UpperCamelCase_=1				,     UpperCamelCase_=0				,     UpperCamelCase_=2				,     UpperCamelCase_=10_24				,     **UpperCamelCase_				,     ):
              __UpperCAmelCase  :				Any       				=		vocab_size
              __UpperCAmelCase  :				Optional[int]       				=		d_model
              __UpperCAmelCase  :				Tuple       				=		decoder_ffn_dim
              __UpperCAmelCase  :				List[str]       				=		decoder_layers
              __UpperCAmelCase  :				str       				=		decoder_attention_heads
              __UpperCAmelCase  :				Dict       				=		dropout
              __UpperCAmelCase  :				Optional[Any]       				=		attention_dropout
              __UpperCAmelCase  :				int       				=		activation_dropout
              __UpperCAmelCase  :				Dict       				=		activation_function
              __UpperCAmelCase  :				Tuple       				=		init_std
              __UpperCAmelCase  :				Any       				=		decoder_layerdrop
              __UpperCAmelCase  :				str       				=		use_cache
              __UpperCAmelCase  :				int       				=		decoder_layers
              __UpperCAmelCase  :				Any       				=		scale_embedding  # scale factor will be sqrt(d_model) if True
              __UpperCAmelCase  :				Union[str, Any]       				=		max_target_positions

              super().__init__(
                  pad_token_id=UpperCamelCase_				,     bos_token_id=UpperCamelCase_				,     eos_token_id=UpperCamelCase_				,     decoder_start_token_id=UpperCamelCase_				,     **UpperCamelCase_				,     )






