
"""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		):
							lowercase_	:	Optional[Any]	 =MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
							lowercase_	:	List[Any]	 =TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING




							def 			A__				(    self       ,A__       ,A__       ,A__):
												lowercase						     =						TextaTextGenerationPipeline(model=A__       ,tokenizer=A__)
												return generator, ["Something to write", "Something else"]




							def 			A__				(    self       ,A__       ,A__):
												lowercase						     =						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'''))

												lowercase						     =						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__)}],
												    ]       ,)

												lowercase						     =						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 			A__				(    self):
												lowercase						     =						pipeline('''text2text-generation'''       ,model='''patrickvonplaten/t5-tiny-random'''       ,framework='''pt''')
												# do_sample=False necessary for reproducibility
												lowercase						     =						generator('''Something there'''       ,do_sample=A__)
												self.assertEqual(A__       ,[{'''generated_text''': ''''''}])

												lowercase						     =						3
												lowercase						     =						generator(
												    '''Something there'''       ,num_return_sequences=A__       ,num_beams=A__       ,)
												lowercase						     =						[
												    {'''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__)

												lowercase						     =						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)},
												    ]       ,)
												lowercase						     =						generator.model.config.eos_token_id
												lowercase						     =						'''<pad>'''
												lowercase						     =						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 			A__				(    self):
												lowercase						     =						pipeline('''text2text-generation'''       ,model='''patrickvonplaten/t5-tiny-random'''       ,framework='''tf''')
												# do_sample=False necessary for reproducibility
												lowercase						     =						generator('''Something there'''       ,do_sample=A__)
												self.assertEqual(A__       ,[{'''generated_text''': ''''''}])






import unittest
from dataclasses import dataclass

import pytest

from accelerate.commands.config.config_args import SageMakerConfig
from accelerate.utils import ComputeEnvironment
from accelerate.utils.launch import _convert_nargs_to_dict







@dataclass
class     lowercase		( SCREAMING_SNAKE_CASE__		):
							lowercase_	:	int	 =ComputeEnvironment.AMAZON_SAGEMAKER
							lowercase_	:	Optional[int]	 =True
							lowercase_	:	Any	 ='''ml.p3.2xlarge'''
							lowercase_	:	Any	 ='''accelerate_sagemaker_execution_role'''
							lowercase_	:	Union[str, Any]	 ='''hf-sm'''
							lowercase_	:	Any	 ='''us-east-1'''
							lowercase_	:	List[str]	 =1
							lowercase_	:	Any	 ='''accelerate-sagemaker-1'''
							lowercase_	:	Union[str, Any]	 ='''1.6'''
							lowercase_	:	Any	 ='''4.4'''
							lowercase_	:	Any	 ='''train.py'''
							lowercase_	:	int	 =[
							    '''--model_name_or_path''',
							    '''bert''',
							    '''--do_train''',
							    '''False''',
							    '''--epochs''',
							    '''3''',
							    '''--learning_rate''',
							    '''5e-5''',
							    '''--max_steps''',
							    '''50.5''',
							]
							lowercase_	:	List[Any]	 =[
							    '''--model_name_or_path''',
							    '''bert''',
							    '''--do_train''',
							    '''--do_test''',
							    '''False''',
							    '''--do_predict''',
							    '''--epochs''',
							    '''3''',
							    '''--learning_rate''',
							    '''5e-5''',
							    '''--max_steps''',
							    '''50.5''',
							]







class     lowercase		( unittest.TestCase		):




							def 			A__				(    self):
												# If no defaults are changed, `to_kwargs` returns an empty dict.
												lowercase						     =						_convert_nargs_to_dict(MockLaunchConfig.success_training_script_args)
												assert isinstance(converted_args['''model_name_or_path''']       ,A__)
												assert isinstance(converted_args['''do_train''']       ,A__)
												assert isinstance(converted_args['''epochs''']       ,A__)
												assert isinstance(converted_args['''learning_rate''']       ,A__)
												assert isinstance(converted_args['''max_steps''']       ,A__)

												with pytest.raises(A__):
																	_convert_nargs_to_dict(MockLaunchConfig.fail_training_script_args)




