






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     _lowerCAmelCase(	unittest.TestCase ):





       """simple docstring"""







       a    :		Union[str, Any]									=MODEL_FOR_CAUSAL_LM_MAPPING
       a    :		List[str]									=TF_MODEL_FOR_CAUSAL_LM_MAPPING





       @require_torch
       def       _a   ( self							):
         UpperCamelCase_: Dict   				=    pipeline(task='text-generation'  ,      model='sshleifer/tiny-ctrl'  ,      framework='pt'							)
         # Using `do_sample=False` to force deterministic output
         UpperCamelCase_: str   				=    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@@'
                     )
                 }
             ]  ,      )

         UpperCamelCase_: Optional[Any]   				=    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@@'
                         )
                     }
                 ],
             ]  ,      )

         UpperCamelCase_: Optional[Any]   				=    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							)},
             ]  ,      )
         UpperCamelCase_: List[Any]   				=    text_generator.model.config.eos_token_id
         UpperCamelCase_: Dict   				=    '<pad>'
         UpperCamelCase_: Tuple   				=    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							):
         UpperCamelCase_: Tuple   				=    pipeline(task='text-generation'  ,      model='sshleifer/tiny-ctrl'  ,      framework='tf'							)

         # Using `do_sample=False` to force deterministic output
         UpperCamelCase_: Dict   				=    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,'
                     )
                 }
             ]  ,      )

         UpperCamelCase_: str   				=    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							):
         UpperCamelCase_: Optional[Any]   				=    TextGenerationPipeline(model=_lowerCamelCase  ,      tokenizer=_lowerCamelCase							)
         return text_generator, ["This is a test", "Another test"]





       def       _a   ( self							):
         UpperCamelCase_: Optional[Any]   				=    'Hello I believe in'
         UpperCamelCase_: int   				=    pipeline('text-generation'  ,      model='hf-internal-testing/tiny-random-gpt2'							)
         UpperCamelCase_: str   				=    text_generator(_lowerCamelCase							)
         self.assertEqual(
             _lowerCamelCase  ,      [{'generated_text': 'Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe'}]  ,      )

         UpperCamelCase_: Any   				=    text_generator(_lowerCamelCase  ,      stop_sequence=' fe'							)
         self.assertEqual(_lowerCamelCase  ,      [{'generated_text': 'Hello I believe in fe'}]							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: Optional[int]   				=    text_generator.model
         UpperCamelCase_: List[str]   				=    text_generator.tokenizer

         UpperCamelCase_: Optional[int]   				=    text_generator('This is a test'							)
         self.assertEqual(_lowerCamelCase  ,      [{'generated_text': ANY(_lowerCamelCase							)}]							)
         self.assertTrue(outputs[0]['generated_text'].startswith('This is a test'							)							)

         UpperCamelCase_: Tuple   				=    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']							)

         UpperCamelCase_: Union[str, Any]   				=    pipeline(task='text-generation'  ,      model=_lowerCamelCase  ,      tokenizer=_lowerCamelCase  ,      return_full_text=_lowerCamelCase							)
         UpperCamelCase_: Dict   				=    text_generator('This is a test'							)
         self.assertEqual(_lowerCamelCase  ,      [{'generated_text': ANY(_lowerCamelCase							)}]							)
         self.assertNotIn('This is a test'  ,      outputs[0]['generated_text']							)

         UpperCamelCase_: Dict   				=    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'							)							)

         UpperCamelCase_: Any   				=    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:
           UpperCamelCase_: Union[str, Any]   				=    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							):
           UpperCamelCase_: Union[str, Any]   				=    text_generator('test'  ,      return_full_text=_lowerCamelCase  ,      return_text=_lowerCamelCase							)
         with self.assertRaises(_lowerCamelCase							):
           UpperCamelCase_: List[str]   				=    text_generator('test'  ,      return_full_text=_lowerCamelCase  ,      return_tensors=_lowerCamelCase							)
         with self.assertRaises(_lowerCamelCase							):
           UpperCamelCase_: Union[str, Any]   				=    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__
         ):
           UpperCamelCase_: Optional[int]   				=    text_generator(''							)
           self.assertEqual(_lowerCamelCase  ,      [{'generated_text': ANY(_lowerCamelCase							)}]							)
         else:
           with self.assertRaises((ValueError, AssertionError)							):
             UpperCamelCase_: Optional[int]   				=    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_: Any   				=    ['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							)

           UpperCamelCase_: int   				=    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							):
         import torch

         # Classic `model_kwargs`
         UpperCamelCase_: Dict   				=    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_: Optional[Any]   				=    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.)
         UpperCamelCase_: str   				=    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(
             _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
         UpperCamelCase_: Optional[int]   				=    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_: Tuple   				=    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							):
         import torch

         UpperCamelCase_: Optional[int]   				=    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							):
         import torch

         UpperCamelCase_: int   				=    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							):
         UpperCamelCase_: Optional[Any]   				=    'Hello world'
         UpperCamelCase_: Dict   				=    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_: str   				=    logging.get_logger('transformers.generation.utils'							)
         UpperCamelCase_: 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(_lowerCamelCase							) as cl:
           UpperCamelCase_: List[str]   				=    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:
           UpperCamelCase_: Tuple   				=    text_generator(_lowerCamelCase  ,      max_new_tokens=1							)
         self.assertNotIn(_lowerCamelCase  ,      cl.out							)

         with CaptureLogger(_lowerCamelCase							) as cl:
           UpperCamelCase_: Tuple   				=    text_generator(_lowerCamelCase  ,      max_length=1_0							)
         self.assertNotIn(_lowerCamelCase  ,      cl.out							)







from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_batched,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


A_		:		int		     =			logging.get_logger(__name__)




class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       a    :		str									=['''pixel_values''']





       def __init__( self  ,      _lowerCamelCase = True  ,      _lowerCamelCase = None  ,      _lowerCamelCase = PILImageResampling.BICUBIC  ,      _lowerCamelCase = True  ,      _lowerCamelCase = True  ,      _lowerCamelCase = 1 / 2_5_5  ,      _lowerCamelCase = None  ,      _lowerCamelCase = True  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      **_lowerCamelCase  ,      ):
         super().__init__(**_lowerCamelCase							)
         UpperCamelCase_: Any   				=    size if size is not None else {'height': 2_2_4, 'width': 2_2_4}
         UpperCamelCase_: Any   				=    get_size_dict(_lowerCamelCase							)
         UpperCamelCase_: Any   				=    crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4}
         UpperCamelCase_: List[Any]   				=    get_size_dict(_lowerCamelCase  ,      default_to_square=_lowerCamelCase  ,      param_name='crop_size'							)

         UpperCamelCase_: Optional[int]   				=    do_resize
         UpperCamelCase_: Tuple   				=    do_rescale
         UpperCamelCase_: Dict   				=    do_normalize
         UpperCamelCase_: Optional[int]   				=    do_center_crop
         UpperCamelCase_: Tuple   				=    crop_size
         UpperCamelCase_: Optional[int]   				=    size
         UpperCamelCase_: Dict   				=    resample
         UpperCamelCase_: Tuple   				=    rescale_factor
         UpperCamelCase_: Tuple   				=    image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
         UpperCamelCase_: Dict   				=    image_std if image_std is not None else IMAGENET_DEFAULT_STD





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase = PILImageResampling.BILINEAR  ,      _lowerCamelCase = None  ,      **_lowerCamelCase  ,      ):
         UpperCamelCase_: Optional[Any]   				=    get_size_dict(_lowerCamelCase							)

         if "shortest_edge" in size:
           UpperCamelCase_: str   				=    get_resize_output_image_size(_lowerCamelCase  ,      size=size['shortest_edge']  ,      default_to_square=_lowerCamelCase							)
           # size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"])
         elif "height" in size and "width" in size:
           UpperCamelCase_: Any   				=    (size['height'], size['width'])
         else:
           raise ValueError(f'''Size must contain \'height\' and \'width\' keys or \'shortest_edge\' key. Got {size.keys()}'''							)
         return resize(_lowerCamelCase  ,      size=_lowerCamelCase  ,      resample=_lowerCamelCase  ,      data_format=_lowerCamelCase  ,      **_lowerCamelCase							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase = None  ,      **_lowerCamelCase  ,      ):
         UpperCamelCase_: List[str]   				=    get_size_dict(_lowerCamelCase							)
         if "height" not in size or "width" not in size:
           raise ValueError(f'''The `size` parameter must contain the keys (height, width). Got {size.keys()}'''							)
         return center_crop(_lowerCamelCase  ,      size=(size['height'], size['width'])  ,      data_format=_lowerCamelCase  ,      **_lowerCamelCase							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase = None  ,      **_lowerCamelCase							):
         return rescale(_lowerCamelCase  ,      scale=_lowerCamelCase  ,      data_format=_lowerCamelCase  ,      **_lowerCamelCase							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase = None  ,      **_lowerCamelCase  ,      ):
         return normalize(_lowerCamelCase  ,      mean=_lowerCamelCase  ,      std=_lowerCamelCase  ,      data_format=_lowerCamelCase  ,      **_lowerCamelCase							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = ChannelDimension.FIRST  ,      **_lowerCamelCase  ,      ):
         UpperCamelCase_: List[str]   				=    do_resize if do_resize is not None else self.do_resize
         UpperCamelCase_: str   				=    do_rescale if do_rescale is not None else self.do_rescale
         UpperCamelCase_: Dict   				=    do_normalize if do_normalize is not None else self.do_normalize
         UpperCamelCase_: List[str]   				=    do_center_crop if do_center_crop is not None else self.do_center_crop
         UpperCamelCase_: Optional[Any]   				=    crop_size if crop_size is not None else self.crop_size
         UpperCamelCase_: List[str]   				=    get_size_dict(_lowerCamelCase  ,      param_name='crop_size'  ,      default_to_square=_lowerCamelCase							)
         UpperCamelCase_: Tuple   				=    resample if resample is not None else self.resample
         UpperCamelCase_: str   				=    rescale_factor if rescale_factor is not None else self.rescale_factor
         UpperCamelCase_: Dict   				=    image_mean if image_mean is not None else self.image_mean
         UpperCamelCase_: Dict   				=    image_std if image_std is not None else self.image_std

         UpperCamelCase_: Any   				=    size if size is not None else self.size
         UpperCamelCase_: Optional[Any]   				=    get_size_dict(_lowerCamelCase							)

         if not is_batched(_lowerCamelCase							):
           UpperCamelCase_: Dict   				=    [images]

         if not valid_images(_lowerCamelCase							):
           raise ValueError(
               'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
               'torch.Tensor, tf.Tensor or jax.ndarray.'							)

         if do_resize and size is None:
           raise ValueError('Size must be specified if do_resize is True.'							)

         if do_center_crop and crop_size is None:
           raise ValueError('Crop size must be specified if do_center_crop is True.'							)

         if do_rescale and rescale_factor is None:
           raise ValueError('Rescale factor must be specified if do_rescale is True.'							)

         # All transformations expect numpy arrays.
         UpperCamelCase_: List[str]   				=    [to_numpy_array(_lowerCamelCase							) for image in images]

         if do_resize:
           UpperCamelCase_: List[str]   				=    [self.resize(image=_lowerCamelCase  ,      size=_lowerCamelCase  ,      resample=_lowerCamelCase							) for image in images]

         if do_center_crop:
           UpperCamelCase_: List[str]   				=    [self.center_crop(image=_lowerCamelCase  ,      size=_lowerCamelCase							) for image in images]

         if do_rescale:
           UpperCamelCase_: int   				=    [self.rescale(image=_lowerCamelCase  ,      scale=_lowerCamelCase							) for image in images]

         if do_normalize:
           UpperCamelCase_: Optional[Any]   				=    [self.normalize(image=_lowerCamelCase  ,      mean=_lowerCamelCase  ,      std=_lowerCamelCase							) for image in images]

         UpperCamelCase_: List[str]   				=    [to_channel_dimension_format(_lowerCamelCase  ,      _lowerCamelCase							) for image in images]

         UpperCamelCase_: Optional[int]   				=    {'pixel_values': images}
         return BatchFeature(data=_lowerCamelCase  ,      tensor_type=_lowerCamelCase							)