


"""simple docstring"""
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 	lowercase__				(					unittest.TestCase				):
   __UpperCAmelCase							       =					MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
   __UpperCAmelCase							       =					TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING





   def 		UpperCamelCase_  (							self      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)  ->  Union[str, Any]:
      _lowerCamelCase	:					Tuple   =		TextaTextGenerationPipeline(model=SCREAMING_SNAKE_CASE      ,   tokenizer=SCREAMING_SNAKE_CASE)
      return generator, ["Something to write", "Something else"]





   def 		UpperCamelCase_  (							self      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)  ->  List[str]:
      _lowerCamelCase	:					str   =		generator("""Something there""")
      self.assertEqual(SCREAMING_SNAKE_CASE      ,   [{"""generated_text""": ANY(SCREAMING_SNAKE_CASE)}])
      # These are encoder decoder, they don't just append to incoming string
      self.assertFalse(outputs[0]["""generated_text"""].startswith("""Something there"""))

      _lowerCamelCase	:					Dict   =		generator(["""This is great !""", """Something else"""]      ,   num_return_sequences=2      ,   do_sample=SCREAMING_SNAKE_CASE)
      self.assertEqual(
          SCREAMING_SNAKE_CASE      ,   [
              [{"""generated_text""": ANY(SCREAMING_SNAKE_CASE)}, {"""generated_text""": ANY(SCREAMING_SNAKE_CASE)}],
              [{"""generated_text""": ANY(SCREAMING_SNAKE_CASE)}, {"""generated_text""": ANY(SCREAMING_SNAKE_CASE)}],
          ]      ,   )

      _lowerCamelCase	:					int   =		generator(
          ["""This is great !""", """Something else"""]      ,   num_return_sequences=2      ,   batch_size=2      ,   do_sample=SCREAMING_SNAKE_CASE)
      self.assertEqual(
          SCREAMING_SNAKE_CASE      ,   [
              [{"""generated_text""": ANY(SCREAMING_SNAKE_CASE)}, {"""generated_text""": ANY(SCREAMING_SNAKE_CASE)}],
              [{"""generated_text""": ANY(SCREAMING_SNAKE_CASE)}, {"""generated_text""": ANY(SCREAMING_SNAKE_CASE)}],
          ]      ,   )

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





   @require_torch
   def 		UpperCamelCase_  (							self)  ->  str:
      _lowerCamelCase	:					Union[str, Any]   =		pipeline("""text2text-generation"""      ,   model="""patrickvonplaten/t5-tiny-random"""      ,   framework="""pt""")
      # do_sample=False necessary for reproducibility
      _lowerCamelCase	:					Union[str, Any]   =		generator("""Something there"""      ,   do_sample=SCREAMING_SNAKE_CASE)
      self.assertEqual(SCREAMING_SNAKE_CASE      ,   [{"""generated_text""": """"""}])

      _lowerCamelCase	:					Dict   =		3
      _lowerCamelCase	:					List[str]   =		generator(
          """Something there"""      ,   num_return_sequences=SCREAMING_SNAKE_CASE      ,   num_beams=SCREAMING_SNAKE_CASE      ,   )
      _lowerCamelCase	:					Tuple   =		[
          {"""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(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)

      _lowerCamelCase	:					Optional[int]   =		generator("""This is a test"""      ,   do_sample=SCREAMING_SNAKE_CASE      ,   num_return_sequences=2      ,   return_tensors=SCREAMING_SNAKE_CASE)
      self.assertEqual(
          SCREAMING_SNAKE_CASE      ,   [
              {"""generated_token_ids""": ANY(torch.Tensor)},
              {"""generated_token_ids""": ANY(torch.Tensor)},
          ]      ,   )
      _lowerCamelCase	:					Union[str, Any]   =		generator.model.config.eos_token_id
      _lowerCamelCase	:					List[Any]   =		"""<pad>"""
      _lowerCamelCase	:					str   =		generator(
          ["""This is a test""", """This is a second test"""]      ,   do_sample=SCREAMING_SNAKE_CASE      ,   num_return_sequences=2      ,   batch_size=2      ,   return_tensors=SCREAMING_SNAKE_CASE      ,   )
      self.assertEqual(
          SCREAMING_SNAKE_CASE      ,   [
              [
                  {"""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 		UpperCamelCase_  (							self)  ->  Tuple:
      _lowerCamelCase	:					str   =		pipeline("""text2text-generation"""      ,   model="""patrickvonplaten/t5-tiny-random"""      ,   framework="""tf""")
      # do_sample=False necessary for reproducibility
      _lowerCamelCase	:					List[Any]   =		generator("""Something there"""      ,   do_sample=SCREAMING_SNAKE_CASE)
      self.assertEqual(SCREAMING_SNAKE_CASE      ,   [{"""generated_text""": """"""}])









'''simple docstring'''
import inspect
import os
import unittest
from pathlib import Path

import torch

import accelerate
from accelerate.test_utils import execute_subprocess_async
from accelerate.test_utils.testing import run_command


class 							_UpperCAmelCase    (		unittest.TestCase      ):
    __lowerCamelCase: List[Any]									=     inspect.getfile(accelerate.test_utils      )
    __lowerCamelCase: List[Any]									=     os.path.sep.join(mod_file.split(os.path.sep      )[:-1] + ['scripts', 'test_cli.py']      )

    __lowerCamelCase: Optional[Any]									=     ['accelerate', 'launch']
    __lowerCamelCase: List[str]									=     Path.home() / '.cache/huggingface/accelerate'
    __lowerCamelCase: Dict									=     'default_config.yaml'
    __lowerCamelCase: Union[str, Any]									=     config_folder / config_file
    __lowerCamelCase: Tuple									=     config_folder / '_default_config.yaml'

    __lowerCamelCase: Union[str, Any]									=     Path('tests/test_configs'      )




    @classmethod
    def 					lowerCAmelCase__					(      cls	:							List[Any]      ):






       '''simple docstring'''






       if cls.config_path.is_file():
          cls.config_path.rename(cls.changed_path      )




    @classmethod
    def 					lowerCAmelCase__					(      cls	:							str      ):






       '''simple docstring'''






       if cls.changed_path.is_file():
          cls.changed_path.rename(cls.config_path      )




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     List[str]							     =  self.base_cmd
       if torch.cuda.is_available() and (torch.cuda.device_count() > 1):
          cmd += ["--multi_gpu"]
       execute_subprocess_async(cmd + [self.test_file_path]	,  env=os.environ.copy()      )




    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       for config in sorted(self.test_config_path.glob("**/*.yaml"      )      ):
          with self.subTest(config_file=a      ):
             execute_subprocess_async(
                 self.base_cmd + ["--config_file", str(a      ), self.test_file_path]	,  env=os.environ.copy()      )





    def 					lowerCAmelCase__					(      self	:							List[str]      ):






       '''simple docstring'''






       execute_subprocess_async(["accelerate", "test"]	,  env=os.environ.copy()      )


class 							_UpperCAmelCase    (		unittest.TestCase      ):
    __lowerCamelCase: Tuple									=     'test-tpu'
    __lowerCamelCase: Dict									=     'us-central1-a'
    __lowerCamelCase: List[str]									=     'ls'
    __lowerCamelCase: Optional[int]									=     ['accelerate', 'tpu-config']
    __lowerCamelCase: Optional[Any]									=     'cd /usr/share'
    __lowerCamelCase: Any									=     'tests/test_samples/test_command_file.sh'
    __lowerCamelCase: Tuple									=     'Running gcloud compute tpus tpu-vm ssh'




    def 					lowerCAmelCase__					(      self	:							Tuple      ):






       '''simple docstring'''






       lowercase_						:     str							     =  run_command(
           self.cmd
           + ["--command", self.command, "--tpu_zone", self.tpu_zone, "--tpu_name", self.tpu_name, "--debug"]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     Optional[int]							     =  run_command(
           self.cmd
           + [
               "--config_file",
               "tests/test_configs/0_12_0.yaml",
               "--command",
               self.command,
               "--tpu_zone",
               self.tpu_zone,
               "--tpu_name",
               self.tpu_name,
               "--debug",
           ]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       lowercase_						:     Union[str, Any]							     =  run_command(
           self.cmd + ["--config_file", "tests/test_configs/latest.yaml", "--debug"]	,  return_stdout=a      )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       lowercase_						:     Any							     =  run_command(
           self.cmd + ["--config_file", "tests/test_configs/latest.yaml", "--command", self.command, "--debug"]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  run_command(
           self.cmd
           + [
               "--config_file",
               "tests/test_configs/latest.yaml",
               "--command",
               self.command,
               "--command",
               "echo \"Hello World\"",
               "--debug",
           ]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls; echo \"Hello World\" --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Any      ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  run_command(
           self.cmd
           + ["--config_file", "tests/test_configs/latest.yaml", "--command_file", self.command_file, "--debug"]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     List[Any]							     =  run_command(
           self.cmd
           + [
               "--config_file",
               "tests/test_configs/0_12_0.yaml",
               "--command_file",
               self.command_file,
               "--tpu_zone",
               self.tpu_zone,
               "--tpu_name",
               self.tpu_name,
               "--debug",
           ]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     Union[str, Any]							     =  run_command(
           self.cmd + ["--config_file", "tests/test_configs/latest.yaml", "--install_accelerate", "--debug"]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate -U; echo \"hello world\"; echo \"this is a second command\" --worker all"""	,  a	,  )





    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  run_command(
           self.cmd
           + [
               "--config_file",
               "tests/test_configs/latest.yaml",
               "--install_accelerate",
               "--accelerate_version",
               "12.0.0",
               "--debug",
           ]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate==12.0.0; echo \"hello world\"; echo \"this is a second command\" --worker all"""	,  a	,  )




