





'''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      ):
   __UpperCamelCase :			Union[str, Any]						   =						MODEL_FOR_CAUSAL_LM_MAPPING
   __UpperCamelCase :			Tuple						   =						TF_MODEL_FOR_CAUSAL_LM_MAPPING





   @require_torch
   def   UpperCAmelCase__  (       self					:      str     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							pipeline(task="""text-generation"""					,model="""sshleifer/tiny-ctrl"""					,framework="""pt"""     )
      # Using `do_sample=False` to force deterministic output
      __SCREAMING_SNAKE_CASE   				=							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@@"""
                  )
              }
          ]					,)

      __SCREAMING_SNAKE_CASE   				=							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@@"""
                      )
                  }
              ],
          ]					,)

      __SCREAMING_SNAKE_CASE   				=							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     )},
          ]					,)
      __SCREAMING_SNAKE_CASE   				=							text_generator.model.config.eos_token_id
      __SCREAMING_SNAKE_CASE   				=							"""<pad>"""
      __SCREAMING_SNAKE_CASE   				=							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   UpperCAmelCase__  (       self					:      int     ):

      '''simple docstring'''

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

      # Using `do_sample=False` to force deterministic output
      __SCREAMING_SNAKE_CASE   				=							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,"""
                  )
              }
          ]					,)

      __SCREAMING_SNAKE_CASE   				=							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   UpperCAmelCase__  (       self					:      Tuple					,lowerCamelCase					:      Union[str, Any]					,lowerCamelCase					:      Optional[int]					,lowerCamelCase					:      List[str]     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							TextGenerationPipeline(model=lowerCamelCase					,tokenizer=lowerCamelCase     )
      return text_generator, ["This is a test", "Another test"]





   def   UpperCAmelCase__  (       self					:      Dict     ):

      '''simple docstring'''

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

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





   def   UpperCAmelCase__  (       self					:      Any					,lowerCamelCase					:      str					,lowerCamelCase					:      str     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							text_generator.model
      __SCREAMING_SNAKE_CASE   				=							text_generator.tokenizer

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

      __SCREAMING_SNAKE_CASE   				=							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"""]     )

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

      __SCREAMING_SNAKE_CASE   				=							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"""     )     )

      __SCREAMING_SNAKE_CASE   				=							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:
         __SCREAMING_SNAKE_CASE   				=							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     ):
         __SCREAMING_SNAKE_CASE   				=							text_generator("""test"""					,return_full_text=lowerCamelCase					,return_text=lowerCamelCase     )
      with self.assertRaises(lowerCamelCase     ):
         __SCREAMING_SNAKE_CASE   				=							text_generator("""test"""					,return_full_text=lowerCamelCase					,return_tensors=lowerCamelCase     )
      with self.assertRaises(lowerCamelCase     ):
         __SCREAMING_SNAKE_CASE   				=							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__
      ):
         __SCREAMING_SNAKE_CASE   				=							text_generator(""""""     )
         self.assertEqual(lowerCamelCase					,[{"""generated_text""": ANY(lowerCamelCase     )}]     )
      else:
         with self.assertRaises((ValueError, AssertionError)     ):
            __SCREAMING_SNAKE_CASE   				=							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.
      __SCREAMING_SNAKE_CASE   				=							["""RwkvForCausalLM""", """XGLMForCausalLM""", """GPTNeoXForCausalLM"""]
      if (
          tokenizer.model_max_length < 1_0000
          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     )

         __SCREAMING_SNAKE_CASE   				=							text_generator("""This is a test""" * 500					,handle_long_generation="""hole"""					,max_new_tokens=20     )
         # Hole strategy cannot work
         with self.assertRaises(lowerCamelCase     ):
            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   UpperCAmelCase__  (       self					:      List[Any]     ):

      '''simple docstring'''

      import torch

      # Classic `model_kwargs`
      __SCREAMING_SNAKE_CASE   				=							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     )
      __SCREAMING_SNAKE_CASE   				=							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.)
      __SCREAMING_SNAKE_CASE   				=							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     )
      __SCREAMING_SNAKE_CASE   				=							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
      __SCREAMING_SNAKE_CASE   				=							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     )
      __SCREAMING_SNAKE_CASE   				=							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   UpperCAmelCase__  (       self					:      List[Any]     ):

      '''simple docstring'''

      import torch

      __SCREAMING_SNAKE_CASE   				=							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   UpperCAmelCase__  (       self					:      Optional[int]     ):

      '''simple docstring'''

      import torch

      __SCREAMING_SNAKE_CASE   				=							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   UpperCAmelCase__  (       self					:      Dict     ):

      '''simple docstring'''

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

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

      with CaptureLogger(lowerCamelCase     ) as cl:
         __SCREAMING_SNAKE_CASE   				=							text_generator(lowerCamelCase					,max_length=10     )
      self.assertNotIn(lowerCamelCase					,cl.out     )









'''simple docstring'''





import warnings
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__lowerCamelCase												=						logging.get_logger(__name__)

__lowerCamelCase												=						{
    '''nvidia/segformer-b0-finetuned-ade-512-512''': (
        '''https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json'''
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}




class 				UpperCAmelCase			(			_snake_case						):
 UpperCAmelCase				=					"segformer"


 def __init__(      self							:	Dict			,       __lowerCamelCase							:	Dict=3			,       __lowerCamelCase							:	List[str]=4			,       __lowerCamelCase							:	List[Any]=[2, 2, 2, 2]			,       __lowerCamelCase							:	List[str]=[8, 4, 2, 1]			,       __lowerCamelCase							:	Optional[Any]=[3_2, 6_4, 1_6_0, 2_5_6]			,       __lowerCamelCase							:	Tuple=[7, 3, 3, 3]			,       __lowerCamelCase							:	Tuple=[4, 2, 2, 2]			,       __lowerCamelCase							:	Any=[1, 2, 5, 8]			,       __lowerCamelCase							:	Optional[Any]=[4, 4, 4, 4]			,       __lowerCamelCase							:	Union[str, Any]="gelu"			,       __lowerCamelCase							:	Dict=0.0			,       __lowerCamelCase							:	Union[str, Any]=0.0			,       __lowerCamelCase							:	Tuple=0.1			,       __lowerCamelCase							:	Optional[Any]=0.02			,       __lowerCamelCase							:	Optional[int]=0.1			,       __lowerCamelCase							:	str=1e-6			,       __lowerCamelCase							:	List[Any]=2_5_6			,       __lowerCamelCase							:	int=2_5_5			,       **__lowerCamelCase							:	str			,       ):
    super().__init__(**__lowerCamelCase )

    if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
       warnings.warn(
           '''Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be'''
           ''' removed, as the behaviour will default to that of reshape_last_stage = True.'''			,       __lowerCamelCase			,       )

    UpperCAmelCase__	:Dict      =      num_channels
    UpperCAmelCase__	:List[Any]      =      num_encoder_blocks
    UpperCAmelCase__	:List[str]      =      depths
    UpperCAmelCase__	:str      =      sr_ratios
    UpperCAmelCase__	:Optional[Any]      =      hidden_sizes
    UpperCAmelCase__	:List[str]      =      patch_sizes
    UpperCAmelCase__	:List[str]      =      strides
    UpperCAmelCase__	:List[str]      =      mlp_ratios
    UpperCAmelCase__	:Dict      =      num_attention_heads
    UpperCAmelCase__	:Tuple      =      hidden_act
    UpperCAmelCase__	:List[str]      =      hidden_dropout_prob
    UpperCAmelCase__	:List[Any]      =      attention_probs_dropout_prob
    UpperCAmelCase__	:int      =      classifier_dropout_prob
    UpperCAmelCase__	:str      =      initializer_range
    UpperCAmelCase__	:List[str]      =      drop_path_rate
    UpperCAmelCase__	:Dict      =      layer_norm_eps
    UpperCAmelCase__	:List[str]      =      decoder_hidden_size
    UpperCAmelCase__	:Optional[int]      =      kwargs.get('''reshape_last_stage'''			,       __lowerCamelCase )
    UpperCAmelCase__	:str      =      semantic_loss_ignore_index




class 				UpperCAmelCase			(			_snake_case						):
 UpperCAmelCase				=					version.parse("1.11"						)


 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[Any] ):
    return OrderedDict(
        [
            ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
        ] )


 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[int] ):
    return 1e-4





 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	List[str] ):
    return 1_2



