


'''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    __magic_name__				(						unittest.TestCase							):
       UpperCAmelCase		 =MODEL_FOR_CAUSAL_LM_MAPPING
       UpperCAmelCase		 =TF_MODEL_FOR_CAUSAL_LM_MAPPING






       @require_torch
       def      lowerCAmelCase						(     self)		->    Dict:




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

          _UpperCAmelCase  :  int				       =text_generator(['This is a test', 'This is a second test'])
          self.assertEqual(
              snake_case    ,				[
                  [
                      {
                          '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  :  List[str]				       =text_generator('This is a test'    ,				do_sample=snake_case    ,				num_return_sequences=2    ,				return_tensors=snake_case)
          self.assertEqual(
              snake_case    ,				[
                  {'generated_token_ids': ANY(snake_case)},
                  {'generated_token_ids': ANY(snake_case)},
              ]    ,				)
          _UpperCAmelCase  :  Tuple				       =text_generator.model.config.eos_token_id
          _UpperCAmelCase  :  List[str]				       ='<pad>'
          _UpperCAmelCase  :  Optional[Any]				       =text_generator(
              ['This is a test', 'This is a second test']    ,				do_sample=snake_case    ,				num_return_sequences=2    ,				batch_size=2    ,				return_tensors=snake_case    ,				)
          self.assertEqual(
              snake_case    ,				[
                  [
                      {'generated_token_ids': ANY(snake_case)},
                      {'generated_token_ids': ANY(snake_case)},
                  ],
                  [
                      {'generated_token_ids': ANY(snake_case)},
                      {'generated_token_ids': ANY(snake_case)},
                  ],
              ]    ,				)






       @require_tf
       def      lowerCAmelCase						(     self)		->    Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase  :  Tuple				       =pipeline(task='text-generation'    ,				model='sshleifer/tiny-ctrl'    ,				framework='tf')

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

          _UpperCAmelCase  :  Optional[int]				       =text_generator(['This is a test', 'This is a second test']    ,				do_sample=snake_case)
          self.assertEqual(
              snake_case    ,				[
                  [
                      {
                          '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      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case)		->    Optional[Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  int				       =TextGenerationPipeline(model=snake_case    ,				tokenizer=snake_case)
          return text_generator, ["This is a test", "Another test"]






       def      lowerCAmelCase						(     self)		->    Tuple:




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

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






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  int				       =text_generator.model
          _UpperCAmelCase  :  Tuple				       =text_generator.tokenizer

          _UpperCAmelCase  :  List[Any]				       =text_generator('This is a test')
          self.assertEqual(snake_case    ,				[{'generated_text': ANY(snake_case)}])
          self.assertTrue(outputs[0]['generated_text'].startswith('This is a test'))

          _UpperCAmelCase  :  Tuple				       =text_generator('This is a test'    ,				return_full_text=snake_case)
          self.assertEqual(snake_case    ,				[{'generated_text': ANY(snake_case)}])
          self.assertNotIn('This is a test'    ,				outputs[0]['generated_text'])

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

          _UpperCAmelCase  :  int				       =text_generator('This is a test'    ,				return_full_text=snake_case)
          self.assertEqual(snake_case    ,				[{'generated_text': ANY(snake_case)}])
          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=snake_case)
          self.assertEqual(
              snake_case    ,				[
                  [{'generated_text': ANY(snake_case)}, {'generated_text': ANY(snake_case)}],
                  [{'generated_text': ANY(snake_case)}, {'generated_text': ANY(snake_case)}],
              ]    ,				)

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

          with self.assertRaises(snake_case):
             _UpperCAmelCase  :  Optional[Any]				       =text_generator('test'    ,				return_full_text=snake_case    ,				return_text=snake_case)
          with self.assertRaises(snake_case):
             _UpperCAmelCase  :  int				       =text_generator('test'    ,				return_full_text=snake_case    ,				return_tensors=snake_case)
          with self.assertRaises(snake_case):
             _UpperCAmelCase  :  Optional[int]				       =text_generator('test'    ,				return_text=snake_case    ,				return_tensors=snake_case)

          # 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[Any]				       =text_generator('')
             self.assertEqual(snake_case    ,				[{'generated_text': ANY(snake_case)}])
          else:
             with self.assertRaises((ValueError, AssertionError)):
                _UpperCAmelCase  :  List[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  :  List[str]				       =['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  :  Optional[Any]				       =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(snake_case):
                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      lowerCAmelCase						(     self)		->    List[Any]:




          '''simple docstring'''
          import torch

          # Classic `model_kwargs`
          _UpperCAmelCase  :  Optional[Any]				       =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(
              snake_case    ,				[
                  {
                      '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  :  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)
          _UpperCAmelCase  :  Optional[int]				       =pipe('This is a test')
          self.assertEqual(
              snake_case    ,				[
                  {
                      '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  :  Union[str, 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  :  Dict				       =pipe('This is a test')
          self.assertEqual(
              snake_case    ,				[
                  {
                      '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      lowerCAmelCase						(     self)		->    Any:




          '''simple docstring'''
          import torch

          _UpperCAmelCase  :  Tuple				       =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      lowerCAmelCase						(     self)		->    str:




          '''simple docstring'''
          import torch

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






       def      lowerCAmelCase						(     self)		->    List[Any]:




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

          # The user only sets one -> no warning
          with CaptureLogger(snake_case) as cl:
             _UpperCAmelCase  :  Union[str, Any]				       =text_generator(snake_case    ,				max_new_tokens=1)
          self.assertNotIn(snake_case    ,				cl.out)

          with CaptureLogger(snake_case) as cl:
             _UpperCAmelCase  :  Any				       =text_generator(snake_case    ,				max_length=1_0)
          self.assertNotIn(snake_case    ,				cl.out)





'''simple docstring'''






import os

import pytest

from datasets import (
    get_dataset_config_info,
    get_dataset_config_names,
    get_dataset_infos,
    get_dataset_split_names,
    inspect_dataset,
    inspect_metric,
)


lowercase				=pytest.mark.integration





@pytest.mark.parametrize('path'			,       ['paws', 'csv']				)
def lowerCamelCase__     (		__lowerCamelCase			:				List[Any]			,       __lowerCamelCase			:				Optional[Any]				):







   '''simple docstring'''




   inspect_dataset(__lowerCamelCase			,       __lowerCamelCase				)
   _UpperCAmelCase  :  List[str]				       =path + '.py'
   assert script_name in os.listdir(__lowerCamelCase				)
   assert "__pycache__" not in os.listdir(__lowerCamelCase				)





@pytest.mark.filterwarnings('ignore:inspect_metric is deprecated:FutureWarning'				)
@pytest.mark.filterwarnings('ignore:metric_module_factory is deprecated:FutureWarning'				)
@pytest.mark.parametrize('path'			,       ['accuracy']				)
def lowerCamelCase__     (		__lowerCamelCase			:				Dict			,       __lowerCamelCase			:				str				):







   '''simple docstring'''




   inspect_metric(__lowerCamelCase			,       __lowerCamelCase				)
   _UpperCAmelCase  :  List[Any]				       =path + '.py'
   assert script_name in os.listdir(__lowerCamelCase				)
   assert "__pycache__" not in os.listdir(__lowerCamelCase				)





@pytest.mark.parametrize(
    'path, config_name, expected_splits'			,       [
        ('squad', 'plain_text', ['train', 'validation']),
        ('dalle-mini/wit', 'dalle-mini--wit', ['train']),
        ('paws', 'labeled_final', ['train', 'test', 'validation']),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				Any			,       __lowerCamelCase			:				Optional[int]			,       __lowerCamelCase			:				str				):







   '''simple docstring'''




   _UpperCAmelCase  :  str				       =get_dataset_config_info(__lowerCamelCase			,       config_name=__lowerCamelCase				)
   assert info.config_name == config_name
   assert list(info.splits.keys()				) == expected_splits





@pytest.mark.parametrize(
    'path, config_name, expected_exception'			,       [
        ('paws', None, ValueError),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				List[Any]			,       __lowerCamelCase			:				Optional[Any]			,       __lowerCamelCase			:				List[str]				):







   '''simple docstring'''




   with pytest.raises(__lowerCamelCase				):
      get_dataset_config_info(__lowerCamelCase			,       config_name=__lowerCamelCase				)





@pytest.mark.parametrize(
    'path, expected'			,       [
        ('squad', 'plain_text'),
        ('acronym_identification', 'default'),
        ('lhoestq/squad', 'plain_text'),
        ('lhoestq/test', 'default'),
        ('lhoestq/demo1', 'lhoestq--demo1'),
        ('dalle-mini/wit', 'dalle-mini--wit'),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				Tuple			,       __lowerCamelCase			:				List[str]				):







   '''simple docstring'''




   _UpperCAmelCase  :  Dict				       =get_dataset_config_names(__lowerCamelCase				)
   assert expected in config_names





@pytest.mark.parametrize(
    'path, expected_configs, expected_splits_in_first_config'			,       [
        ('squad', ['plain_text'], ['train', 'validation']),
        ('dalle-mini/wit', ['dalle-mini--wit'], ['train']),
        ('paws', ['labeled_final', 'labeled_swap', 'unlabeled_final'], ['train', 'test', 'validation']),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				Tuple			,       __lowerCamelCase			:				List[Any]			,       __lowerCamelCase			:				List[Any]				):







   '''simple docstring'''




   _UpperCAmelCase  :  Optional[int]				       =get_dataset_infos(__lowerCamelCase				)
   assert list(infos.keys()				) == expected_configs
   _UpperCAmelCase  :  Optional[int]				       =expected_configs[0]
   assert expected_config in infos
   _UpperCAmelCase  :  Optional[int]				       =infos[expected_config]
   assert info.config_name == expected_config
   assert list(info.splits.keys()				) == expected_splits_in_first_config





@pytest.mark.parametrize(
    'path, expected_config, expected_splits'			,       [
        ('squad', 'plain_text', ['train', 'validation']),
        ('dalle-mini/wit', 'dalle-mini--wit', ['train']),
        ('paws', 'labeled_final', ['train', 'test', 'validation']),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				Tuple			,       __lowerCamelCase			:				Tuple			,       __lowerCamelCase			:				int				):







   '''simple docstring'''




   _UpperCAmelCase  :  str				       =get_dataset_infos(__lowerCamelCase				)
   assert expected_config in infos
   _UpperCAmelCase  :  Dict				       =infos[expected_config]
   assert info.config_name == expected_config
   assert list(info.splits.keys()				) == expected_splits





@pytest.mark.parametrize(
    'path, config_name, expected_exception'			,       [
        ('paws', None, ValueError),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				Union[str, Any]			,       __lowerCamelCase			:				Optional[Any]			,       __lowerCamelCase			:				Any				):







   '''simple docstring'''




   with pytest.raises(__lowerCamelCase				):
      get_dataset_split_names(__lowerCamelCase			,       config_name=__lowerCamelCase				)

