





import unittest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TextaTextGenerationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch
from transformers.utils import is_torch_available

from .test_pipelines_common import ANY


if is_torch_available():
       import torch





@is_pipeline_test
class A_					(			unittest.TestCase   ):
   lowerCAmelCase__        = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
   lowerCAmelCase__        = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING




   def _lowerCAmelCase      (self	:Any    ,   _UpperCamelCase	:Optional[int]    ,   _UpperCamelCase	:int    ,   _UpperCamelCase	:str )->					int:
         __A          =				TextaTextGenerationPipeline(model=_a    ,   tokenizer=_a )
         return generator, ["Something to write", "Something else"]




   def _lowerCAmelCase      (self	:int    ,   _UpperCamelCase	:Any    ,   _UpperCamelCase	:Optional[Any] )->					Dict:
         __A          =				generator('''Something there''' )
         self.assertEqual(_a    ,   [{'''generated_text''': ANY(_a )}] )
         # These are encoder decoder, they don't just append to incoming string
         self.assertFalse(outputs[0]['''generated_text'''].startswith('''Something there''' ) )

         __A          =				generator(['''This is great !''', '''Something else''']    ,   num_return_sequences=2    ,   do_sample=_a )
         self.assertEqual(
             _a    ,   [
                 [{'''generated_text''': ANY(_a )}, {'''generated_text''': ANY(_a )}],
                 [{'''generated_text''': ANY(_a )}, {'''generated_text''': ANY(_a )}],
             ]    ,   )

         __A          =				generator(
             ['''This is great !''', '''Something else''']    ,   num_return_sequences=2    ,   batch_size=2    ,   do_sample=_a )
         self.assertEqual(
             _a    ,   [
                 [{'''generated_text''': ANY(_a )}, {'''generated_text''': ANY(_a )}],
                 [{'''generated_text''': ANY(_a )}, {'''generated_text''': ANY(_a )}],
             ]    ,   )

         with self.assertRaises(_a ):
               generator(4 )




   @require_torch
   def _lowerCAmelCase      (self	:Optional[int] )->					List[str]:
         __A          =				pipeline('''text2text-generation'''    ,   model='''patrickvonplaten/t5-tiny-random'''    ,   framework='''pt''' )
         # do_sample=False necessary for reproducibility
         __A          =				generator('''Something there'''    ,   do_sample=_a )
         self.assertEqual(_a    ,   [{'''generated_text''': ''''''}] )

         __A          =				3
         __A          =				generator(
             '''Something there'''    ,   num_return_sequences=_a    ,   num_beams=_a    ,   )
         __A          =				[
             {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide Beide"""},
             {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide"""},
             {"""generated_text""": """"""},
         ]
         self.assertEqual(_a    ,   _a )

         __A          =				generator('''This is a test'''    ,   do_sample=_a    ,   num_return_sequences=2    ,   return_tensors=_a )
         self.assertEqual(
             _a    ,   [
                 {'''generated_token_ids''': ANY(torch.Tensor )},
                 {'''generated_token_ids''': ANY(torch.Tensor )},
             ]    ,   )
         __A          =				generator.model.config.eos_token_id
         __A          =				"""<pad>"""
         __A          =				generator(
             ['''This is a test''', '''This is a second test''']    ,   do_sample=_a    ,   num_return_sequences=2    ,   batch_size=2    ,   return_tensors=_a    ,   )
         self.assertEqual(
             _a    ,   [
                 [
                     {'''generated_token_ids''': ANY(torch.Tensor )},
                     {'''generated_token_ids''': ANY(torch.Tensor )},
                 ],
                 [
                     {'''generated_token_ids''': ANY(torch.Tensor )},
                     {'''generated_token_ids''': ANY(torch.Tensor )},
                 ],
             ]    ,   )






   @require_tf
   def _lowerCAmelCase      (self	:int )->					int:
         __A          =				pipeline('''text2text-generation'''    ,   model='''patrickvonplaten/t5-tiny-random'''    ,   framework='''tf''' )
         # do_sample=False necessary for reproducibility
         __A          =				generator('''Something there'''    ,   do_sample=_a )
         self.assertEqual(_a    ,   [{'''generated_text''': ''''''}] )











def 				lowerCAmelCase_	(     snake_case_     ):
      if number < 0:
            raise ValueError("""number must not be negative"""     )
      return number & (number - 1) == 0


if __name__ == "__main__":
  import doctest

  doctest.testmod()

