






"""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        _UpperCamelCase		(			unittest.TestCase):
    __lowerCamelCase    =	MODEL_FOR_CAUSAL_LM_MAPPING
    __lowerCamelCase    =	TF_MODEL_FOR_CAUSAL_LM_MAPPING






    @require_torch
    def 			A     (self ):


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

      A__           =				text_generator(["""This is a test""", """This is a second test"""] )
      self.assertEqual(
          lowerCamelCase__    ,   [
              [
                  {
                      """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@@"""
                      )
                  }
              ],
          ]    ,   )

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






    @require_tf
    def 			A     (self ):


      """simple docstring"""
      A__           =				pipeline(task="""text-generation"""    ,   model="""sshleifer/tiny-ctrl"""    ,   framework="""tf""" )

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

      A__           =				text_generator(["""This is a test""", """This is a second test"""]    ,   do_sample=lowerCamelCase__ )
      self.assertEqual(
          lowerCamelCase__    ,   [
              [
                  {
                      """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    ,   lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__ ):


      """simple docstring"""
      A__           =				TextGenerationPipeline(model=lowerCamelCase__    ,   tokenizer=lowerCamelCase__ )
      return text_generator, ["This is a test", "Another test"]






    def 			A     (self ):


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

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






    def 			A     (self    ,   lowerCamelCase__    ,   lowerCamelCase__ ):


      """simple docstring"""
      A__           =				text_generator.model
      A__           =				text_generator.tokenizer

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

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

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

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

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

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

      with self.assertRaises(lowerCamelCase__ ):
        A__           =				text_generator("""test"""    ,   return_full_text=lowerCamelCase__    ,   return_text=lowerCamelCase__ )
      with self.assertRaises(lowerCamelCase__ ):
        A__           =				text_generator("""test"""    ,   return_full_text=lowerCamelCase__    ,   return_tensors=lowerCamelCase__ )
      with self.assertRaises(lowerCamelCase__ ):
        A__           =				text_generator("""test"""    ,   return_text=lowerCamelCase__    ,   return_tensors=lowerCamelCase__ )

      # 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__
      ):
        A__           =				text_generator("""""" )
        self.assertEqual(lowerCamelCase__    ,   [{"""generated_text""": ANY(lowerCamelCase__ )}] )
      else:
        with self.assertRaises((ValueError, AssertionError) ):
          A__           =				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.
      A__           =				["""RwkvForCausalLM""", """XGLMForCausalLM""", """GPTNeoXForCausalLM"""]
      if (
          tokenizer.model_max_length < 1_0_0_0_0
          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_0_0    ,   max_new_tokens=2_0 )

        A__           =				text_generator("""This is a test""" * 5_0_0    ,   handle_long_generation="""hole"""    ,   max_new_tokens=2_0 )
        # Hole strategy cannot work
        with self.assertRaises(lowerCamelCase__ ):
          text_generator(
              """This is a test""" * 5_0_0    ,   handle_long_generation="""hole"""    ,   max_new_tokens=tokenizer.model_max_length + 1_0    ,   )






    @require_torch
    @require_accelerate
    @require_torch_gpu
    def 			A     (self ):


      """simple docstring"""
      import torch

      # Classic `model_kwargs`
      A__           =				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 )
      A__           =				pipe("""This is a test""" )
      self.assertEqual(
          lowerCamelCase__    ,   [
              {
                  """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.)
      A__           =				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 )
      A__           =				pipe("""This is a test""" )
      self.assertEqual(
          lowerCamelCase__    ,   [
              {
                  """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
      A__           =				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 )
      A__           =				pipe("""This is a test""" )
      self.assertEqual(
          lowerCamelCase__    ,   [
              {
                  """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 ):


      """simple docstring"""
      import torch

      A__           =				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 ):


      """simple docstring"""
      import torch

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






    def 			A     (self ):


      """simple docstring"""
      A__           =				"""Hello world"""
      A__           =				pipeline("""text-generation"""    ,   model="""hf-internal-testing/tiny-random-gpt2""" )
      if text_generator.model.framework == "tf":
        A__           =				logging.get_logger("""transformers.generation.tf_utils""" )
      else:
        A__           =				logging.get_logger("""transformers.generation.utils""" )
      A__           =				"""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(lowerCamelCase__ ) as cl:
        A__           =				text_generator(lowerCamelCase__    ,   max_length=1_0    ,   max_new_tokens=1 )
      self.assertIn(lowerCamelCase__    ,   cl.out )

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

      with CaptureLogger(lowerCamelCase__ ) as cl:
        A__           =				text_generator(lowerCamelCase__    ,   max_length=1_0 )
      self.assertNotIn(lowerCamelCase__    ,   cl.out )











'''simple docstring'''

from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

from packaging import version


if TYPE_CHECKING:
    from ... import PreTrainedTokenizer, TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import is_torch_available, logging


a   	=    logging.get_logger(__name__)

a   	=    {
    "bigscience/bloom": "https://huggingface.co/bigscience/bloom/resolve/main/config.json",
    "bigscience/bloom-560m": "https://huggingface.co/bigscience/bloom-560m/blob/main/config.json",
    "bigscience/bloom-1b1": "https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json",
    "bigscience/bloom-1b7": "https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json",
    "bigscience/bloom-3b": "https://huggingface.co/bigscience/bloom-3b/blob/main/config.json",
    "bigscience/bloom-7b1": "https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json",
}







class __a			( _snake_case      ):
   __UpperCamelCase :			List[str]						   =						'bloom'
   __UpperCamelCase :			Optional[Any]						   =						['past_key_values']
   __UpperCamelCase :			Optional[int]						   =						{
       'num_hidden_layers': 'n_layer',
       'num_attention_heads': 'n_head',
   }





   def __init__(       self					:      Union[str, Any]					,lowerCamelCase					:      str=25_0880					,lowerCamelCase					:      List[Any]=64					,lowerCamelCase					:      Optional[Any]=2					,lowerCamelCase					:      Tuple=8					,lowerCamelCase					:      Union[str, Any]=1E-5					,lowerCamelCase					:      Optional[Any]=0.02					,lowerCamelCase					:      str=True					,lowerCamelCase					:      List[Any]=1					,lowerCamelCase					:      Union[str, Any]=2					,lowerCamelCase					:      Optional[int]=False					,lowerCamelCase					:      Optional[int]=0.0					,lowerCamelCase					:      List[Any]=0.0					,lowerCamelCase					:      Optional[Any]=1					,lowerCamelCase					:      Tuple=False					,**lowerCamelCase					:      str					,):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							vocab_size
      # Backward compatibility with n_embed kwarg
      __SCREAMING_SNAKE_CASE   				=							kwargs.pop("""n_embed"""					,lowerCamelCase     )
      __SCREAMING_SNAKE_CASE   				=							hidden_size if n_embed is None else n_embed
      __SCREAMING_SNAKE_CASE   				=							n_layer
      __SCREAMING_SNAKE_CASE   				=							n_head
      __SCREAMING_SNAKE_CASE   				=							layer_norm_epsilon
      __SCREAMING_SNAKE_CASE   				=							initializer_range
      __SCREAMING_SNAKE_CASE   				=							use_cache
      __SCREAMING_SNAKE_CASE   				=							pretraining_tp
      __SCREAMING_SNAKE_CASE   				=							apply_residual_connection_post_layernorm
      __SCREAMING_SNAKE_CASE   				=							hidden_dropout
      __SCREAMING_SNAKE_CASE   				=							attention_dropout

      __SCREAMING_SNAKE_CASE   				=							bos_token_id
      __SCREAMING_SNAKE_CASE   				=							eos_token_id
      __SCREAMING_SNAKE_CASE   				=							slow_but_exact

      super().__init__(bos_token_id=lowerCamelCase					,eos_token_id=lowerCamelCase					,**lowerCamelCase     )







class __a			( _snake_case      ):
   __UpperCamelCase :			int						   =						version.parse('1.12'      )





   def __init__(       self					:      Optional[int]					,lowerCamelCase					:      PretrainedConfig					,lowerCamelCase					:      str = "default"					,lowerCamelCase					:      List[PatchingSpec] = None					,lowerCamelCase					:      bool = False					,):

      '''simple docstring'''

      super().__init__(lowerCamelCase					,task=lowerCamelCase					,patching_specs=lowerCamelCase					,use_past=lowerCamelCase     )
      if not getattr(self._config					,"""pad_token_id"""					,lowerCamelCase     ):
         # TODO: how to do that better?
         __SCREAMING_SNAKE_CASE   				=							0





   @property
   def   UpperCAmelCase__  (       self					:      int     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}     )
      if self.use_past:
         # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
         self.fill_with_past_key_values_(lowerCamelCase					,direction="""inputs"""					,inverted_values_shape=lowerCamelCase     )
         __SCREAMING_SNAKE_CASE   				=							{0: """batch""", 1: """past_sequence + sequence"""}
      else:
         __SCREAMING_SNAKE_CASE   				=							{0: """batch""", 1: """sequence"""}

      return common_inputs





   @property
   def   UpperCAmelCase__  (       self					:      Tuple     ):

      '''simple docstring'''

      return self._config.n_layer





   @property
   def   UpperCAmelCase__  (       self					:      Dict     ):

      '''simple docstring'''

      return self._config.n_head





   @property
   def   UpperCAmelCase__  (       self					:      str     ):

      '''simple docstring'''

      return 1E-3





   def   UpperCAmelCase__  (       self					:      str					,lowerCamelCase					:      "PreTrainedTokenizer"					,lowerCamelCase					:      int = -1					,lowerCamelCase					:      int = -1					,lowerCamelCase					:      bool = False					,lowerCamelCase					:      Optional["TensorType"] = None					,):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							super(lowerCamelCase					,self     ).generate_dummy_inputs(
          lowerCamelCase					,batch_size=lowerCamelCase					,seq_length=lowerCamelCase					,is_pair=lowerCamelCase					,framework=lowerCamelCase     )

      # We need to order the input in the way they appears in the forward()
      __SCREAMING_SNAKE_CASE   				=							OrderedDict({"""input_ids""": common_inputs["""input_ids"""]}     )

      # Need to add the past_keys
      if self.use_past:
         if not is_torch_available():
            raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""     )
         else:
            import torch

            __SCREAMING_SNAKE_CASE				,  __SCREAMING_SNAKE_CASE   				=							common_inputs["""input_ids"""].shape
            # Not using the same length for past_key_values
            __SCREAMING_SNAKE_CASE   				=							seqlen + 2
            __SCREAMING_SNAKE_CASE   				=							self._config.hidden_size // self.num_attention_heads
            __SCREAMING_SNAKE_CASE   				=							(
                batch * self.num_attention_heads,
                head_dim,
                past_key_values_length,
            )
            __SCREAMING_SNAKE_CASE   				=							(
                batch * self.num_attention_heads,
                past_key_values_length,
                head_dim,
            )
            __SCREAMING_SNAKE_CASE   				=							[
                (torch.zeros(lowerCamelCase     ), torch.zeros(lowerCamelCase     )) for _ in range(self.num_layers     )
            ]

      __SCREAMING_SNAKE_CASE   				=							common_inputs["""attention_mask"""]
      if self.use_past:
         __SCREAMING_SNAKE_CASE   				=							ordered_inputs["""attention_mask"""].dtype
         __SCREAMING_SNAKE_CASE   				=							torch.cat(
             [ordered_inputs["""attention_mask"""], torch.ones(lowerCamelCase					,lowerCamelCase					,dtype=lowerCamelCase     )]					,dim=1     )

      return ordered_inputs





   @property
   def   UpperCAmelCase__  (       self					:      str     ):

      '''simple docstring'''

      return 13





