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






			'''simple docstring'''

			__UpperCamelCase :  Dict    =	MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
			__UpperCamelCase :  List[str]    =	TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

			def  lowerCAmelCase__		(    self	:    Tuple  ,		snake_case_	:    Dict  ,		snake_case_	:    Optional[Any]  ,		snake_case_	:    List[str] ):
							UpperCamelCase_:							Any		=  TextaTextGenerationPipeline(model=snake_case_  ,		tokenizer=snake_case_ )
							return generator, ["Something to write", "Something else"]

			def  lowerCAmelCase__		(    self	:    str  ,		snake_case_	:    Dict  ,		snake_case_	:    Tuple ):
							UpperCamelCase_:							int		=  generator("""Something there""" )
							self.assertEqual(snake_case_  ,		[{"""generated_text""": ANY(snake_case_ )}] )
							# These are encoder decoder, they don't just append to incoming string
							self.assertFalse(outputs[0]["""generated_text"""].startswith("""Something there""" ) )

							UpperCamelCase_:							Dict		=  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_ )}],
							    ]  ,		)

							UpperCamelCase_:							Optional[Any]		=  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_ ):
											generator(4 )

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

							UpperCamelCase_:							Dict		=  3
							UpperCamelCase_:							Union[str, Any]		=  generator(
							    """Something there"""  ,		num_return_sequences=snake_case_  ,		num_beams=snake_case_  ,		)
							UpperCamelCase_:							Optional[Any]		=  [
							    {"""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(snake_case_  ,		snake_case_ )

							UpperCamelCase_:							Any		=  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(torch.Tensor )},
							        {"""generated_token_ids""": ANY(torch.Tensor )},
							    ]  ,		)
							UpperCamelCase_:							int		=  generator.model.config.eos_token_id
							UpperCamelCase_:							Any		=  """<pad>"""
							UpperCamelCase_:							Any		=  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(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	:    Dict ):
							UpperCamelCase_:							int		=  pipeline("""text2text-generation"""  ,		model="""patrickvonplaten/t5-tiny-random"""  ,		framework="""tf""" )
							# do_sample=False necessary for reproducibility
							UpperCamelCase_:							Tuple		=  generator("""Something there"""  ,		do_sample=snake_case_ )
							self.assertEqual(snake_case_  ,		[{"""generated_text""": """"""}] )








import argparse

import torch

from transformers import GPTaConfig, GPTaModel, load_tf_weights_in_gpta
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()







def     A__   (  lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase  )					->		Union[str, Any]:
				# Construct model
				if gpta_config_file == "":
								UpperCamelCase_:							Union[str, Any]		=  GPTaConfig()
				else:
								UpperCamelCase_:							Optional[Any]		=  GPTaConfig.from_json_file(lowerCamelCase  )
				UpperCamelCase_:							List[Any]		=  GPTaModel(lowerCamelCase  )

				# Load weights from numpy
				load_tf_weights_in_gpta(lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase  )

				# Save pytorch-model
				UpperCamelCase_:							int		=  pytorch_dump_folder_path + """/""" + WEIGHTS_NAME
				UpperCamelCase_:							Any		=  pytorch_dump_folder_path + """/""" + CONFIG_NAME
				print(F'''Save PyTorch model to {pytorch_weights_dump_path}'''  )
				torch.save(model.state_dict()       ,      lowerCamelCase  )
				print(F'''Save configuration file to {pytorch_config_dump_path}'''  )
				with open(lowerCamelCase       ,      """w"""       ,      encoding="""utf-8"""  ) as f:
								f.write(config.to_json_string()  )


if __name__ == "__main__":
							lowerCamelCase_  :	int 				=					argparse.ArgumentParser()
							# Required parameters
							parser.add_argument(
							    """--gpt2_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path."""
							)
							parser.add_argument(
							    """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
							)
							parser.add_argument(
							    """--gpt2_config_file""",
							    default="""""",
							    type=str,
							    help=(
							        """An optional config json file corresponding to the pre-trained OpenAI model. \n"""
							        """This specifies the model architecture."""
							    ),
							)
							lowerCamelCase_  :	List[str] 				=					parser.parse_args()
							convert_gpta_checkpoint_to_pytorch(args.gpta_checkpoint_path, args.gpta_config_file, args.pytorch_dump_folder_path)






