




"""simple docstring"""

from __future__ import annotations

import unittest

from transformers import OpenAIGPTConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
		import tensorflow as tf

		from transformers.models.openai.modeling_tf_openai import (
		    TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFOpenAIGPTDoubleHeadsModel,
		    TFOpenAIGPTForSequenceClassification,
		    TFOpenAIGPTLMHeadModel,
		    TFOpenAIGPTModel,
		)
class      A__   :




	def __init__( self	:						Union[str, Any]   ,     lowercase	:						Union[str, Any]   ,     )       ->			List[Any]:
						__snake_case        =						parent
						__snake_case        =						13
						__snake_case        =						7
						__snake_case        =						True
						__snake_case        =						True
						__snake_case        =						True
						__snake_case        =						True
						__snake_case        =						True
						__snake_case        =						99
						__snake_case        =						32
						__snake_case        =						2
						__snake_case        =						4
						__snake_case        =						37
						__snake_case        =						"""gelu"""
						__snake_case        =						0.1
						__snake_case        =						0.1
						__snake_case        =						512
						__snake_case        =						16
						__snake_case        =						2
						__snake_case        =						0.02
						__snake_case        =						3
						__snake_case        =						4
						__snake_case        =						None
						__snake_case        =						self.vocab_size - 1




	def 				a_		( self	:						Dict						)       ->			Dict:
						__snake_case        =						ids_tensor([self.batch_size, self.seq_length]   ,     self.vocab_size						)

						__snake_case        =						None
						if self.use_input_mask:
											__snake_case        =						random_attention_mask([self.batch_size, self.seq_length]						)

						__snake_case        =						None
						if self.use_token_type_ids:
											__snake_case        =						ids_tensor([self.batch_size, self.seq_length]   ,     self.type_vocab_size						)

						__snake_case        =						None
						if self.use_mc_token_ids:
											__snake_case        =						ids_tensor([self.batch_size, self.num_choices]   ,     self.seq_length						)

						__snake_case        =						None
						__snake_case        =						None
						__snake_case        =						None
						if self.use_labels:
											__snake_case        =						ids_tensor([self.batch_size]   ,     self.type_sequence_label_size						)
											__snake_case        =						ids_tensor([self.batch_size, self.seq_length]   ,     self.num_labels						)
											__snake_case        =						ids_tensor([self.batch_size]   ,     self.num_choices						)

						__snake_case        =						OpenAIGPTConfig(
						    vocab_size=self.vocab_size   ,     n_embd=self.hidden_size   ,     n_layer=self.num_hidden_layers   ,     n_head=self.num_attention_heads   ,     n_positions=self.max_position_embeddings   ,     pad_token_id=self.pad_token_id   ,     )

						__snake_case        =						ids_tensor([self.num_hidden_layers, self.num_attention_heads]   ,     2						)

						return (
						    config,
						    input_ids,
						    input_mask,
						    head_mask,
						    token_type_ids,
						    mc_token_ids,
						    sequence_labels,
						    token_labels,
						    choice_labels,
						)




	def 				a_		( self	:						Union[str, Any]   ,     lowercase	:						Dict   ,     lowercase	:						str   ,     lowercase	:						str   ,     lowercase	:						Optional[int]   ,     lowercase	:						Optional[int]   ,     *lowercase	:						Any						)       ->			Tuple:
						__snake_case        =						TFOpenAIGPTModel(config=__UpperCamelCase						)
						__snake_case        =						{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
						__snake_case        =						model(__UpperCamelCase						)

						__snake_case        =						[input_ids, input_mask]
						__snake_case        =						model(__UpperCamelCase						)

						__snake_case        =						model(__UpperCamelCase						)

						self.parent.assertEqual(result.last_hidden_state.shape   ,     (self.batch_size, self.seq_length, self.hidden_size)						)




	def 				a_		( self	:						Tuple   ,     lowercase	:						List[str]   ,     lowercase	:						Dict   ,     lowercase	:						Tuple   ,     lowercase	:						Any   ,     lowercase	:						Any   ,     *lowercase	:						int						)       ->			Tuple:
						__snake_case        =						TFOpenAIGPTLMHeadModel(config=__UpperCamelCase						)
						__snake_case        =						{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
						__snake_case        =						model(__UpperCamelCase						)
						self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length, self.vocab_size)						)




	def 				a_		( self	:						int   ,     lowercase	:						Optional[int]   ,     lowercase	:						List[Any]   ,     lowercase	:						List[str]   ,     lowercase	:						Optional[int]   ,     lowercase	:						str   ,     lowercase	:						Optional[Any]   ,     *lowercase	:						Dict						)       ->			Any:
						__snake_case        =						TFOpenAIGPTDoubleHeadsModel(config=__UpperCamelCase						)

						__snake_case        =						tf.tile(tf.expand_dims(__UpperCamelCase   ,     1						)   ,     (1, self.num_choices, 1)						)
						__snake_case        =						tf.tile(tf.expand_dims(__UpperCamelCase   ,     1						)   ,     (1, self.num_choices, 1)						)
						__snake_case        =						tf.tile(tf.expand_dims(__UpperCamelCase   ,     1						)   ,     (1, self.num_choices, 1)						)

						__snake_case        =						{
						    """input_ids""": multiple_choice_inputs_ids,
						    """mc_token_ids""": mc_token_ids,
						    """attention_mask""": multiple_choice_input_mask,
						    """token_type_ids""": multiple_choice_token_type_ids,
						}
						__snake_case        =						model(__UpperCamelCase						)
						self.parent.assertEqual(
						    result.logits.shape   ,     (self.batch_size, self.num_choices, self.seq_length, self.vocab_size)						)
						self.parent.assertEqual(result.mc_logits.shape   ,     (self.batch_size, self.num_choices)						)




	def 				a_		( self	:						int   ,     lowercase	:						Optional[int]   ,     lowercase	:						List[Any]   ,     lowercase	:						Union[str, Any]   ,     lowercase	:						Dict   ,     lowercase	:						int   ,     *lowercase	:						Dict						)       ->			int:
						__snake_case        =						self.num_labels
						__snake_case        =						ids_tensor([self.batch_size]   ,     self.type_sequence_label_size						)
						__snake_case        =						{
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						    """labels""": sequence_labels,
						}
						__snake_case        =						TFOpenAIGPTForSequenceClassification(__UpperCamelCase						)
						__snake_case        =						model(__UpperCamelCase						)
						self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.num_labels)						)




	def 				a_		( self	:						List[Any]						)       ->			Dict:
						__snake_case        =						self.prepare_config_and_inputs()

						(
						    (
						    __snake_case	
						)   ,   (
						    __snake_case	
						)   ,   (
						    __snake_case	
						)   ,   (
						    __snake_case	
						)   ,   (
						    __snake_case	
						)   ,   (
						    __snake_case	
						)   ,   (
						    __snake_case	
						)   ,   (
						    __snake_case	
						)   ,   (
						    __snake_case	
						)   ,   	
						)        =						config_and_inputs

						__snake_case        =						{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
						return config, inputs_dict

@require_tf
class      A__   (   __snake_case  ,						__snake_case  ,						unittest.TestCase  ):
	A_				:					Optional[Any]      =   (
	    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
	    if is_tf_available()
	    else ()
	)
	A_				:					Union[str, Any]      =   (
	    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
	)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
	A_				:					Tuple      =   (
	    {
	        """feature-extraction""": TFOpenAIGPTModel,
	        """text-classification""": TFOpenAIGPTForSequenceClassification,
	        """text-generation""": TFOpenAIGPTLMHeadModel,
	        """zero-shot""": TFOpenAIGPTForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)
	A_				:					int      =   False
	A_				:					Any      =   False




	def 				a_		( self	:						Tuple   ,     lowercase	:						int   ,     lowercase	:						Tuple   ,     lowercase	:						Dict   ,     lowercase	:						List[str]   ,     lowercase	:						Union[str, Any]						)       ->			Optional[int]:
						if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
											# Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
											# `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
											# tiny config could not be created.
											return True

						return False




	def 				a_		( self	:						List[str]						)       ->			Dict:
						__snake_case        =						TFOpenAIGPTModelTester(self						)
						__snake_case        =						ConfigTester(self   ,     config_class=__UpperCamelCase   ,     n_embd=37						)




	def 				a_		( self	:						Optional[Any]						)       ->			Optional[Any]:
						self.config_tester.run_common_tests()




	def 				a_		( self	:						Any						)       ->			Union[str, Any]:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_model(*__UpperCamelCase						)




	def 				a_		( self	:						Union[str, Any]						)       ->			Optional[Any]:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_lm_head(*__UpperCamelCase						)




	def 				a_		( self	:						Union[str, Any]						)       ->			Union[str, Any]:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_double_head(*__UpperCamelCase						)




	def 				a_		( self	:						List[str]						)       ->			str:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__UpperCamelCase						)




	@slow
	def 				a_		( self	:						int						)       ->			Union[str, Any]:
						for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__snake_case        =						TFOpenAIGPTModel.from_pretrained(__UpperCamelCase						)
											self.assertIsNotNone(__UpperCamelCase						)

@require_tf
class      A__   (   unittest.TestCase  ):




	@slow
	def 				a_		( self	:						List[Any]						)       ->			Any:
						__snake_case        =						TFOpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""						)
						__snake_case        =						tf.convert_to_tensor([[481, 4_735, 544]]   ,     dtype=tf.intaa						)  # the president is
						__snake_case        =						[
						    481,
						    4_735,
						    544,
						    246,
						    963,
						    870,
						    762,
						    239,
						    244,
						    40_477,
						    244,
						    249,
						    719,
						    881,
						    487,
						    544,
						    240,
						    244,
						    603,
						    481,
						]  # the president is a very good man. " \n " i\'m sure he is, " said the

						__snake_case        =						model.generate(__UpperCamelCase   ,     do_sample=__UpperCamelCase						)
						self.assertListEqual(output_ids[0].numpy().tolist()   ,     __UpperCamelCase						)




import argparse
import logging

import numpy as np
import torch

from transformers import AutoModelForCausalLM, AutoTokenizer


logging.basicConfig(
    format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
    datefmt='%m/%d/%Y %H:%M:%S',
    level=logging.INFO,
)
__magic_name__  :  List[str]     			=   logging.getLogger(__name__)





def        lowerCAmelCase       (				snake_case__   :      Union[str, Any] )->			Optional[Any]:
    np.random.seed(args.seed )
    torch.manual_seed(args.seed )
    if args.n_gpu > 0:
        torch.cuda.manual_seed_all(args.seed )





def        lowerCAmelCase       (				)->			int:
    A_								=					argparse.ArgumentParser()
    parser.add_argument(
        "--model_name_or_path"				,					default=snake_case__				,					type=snake_case__				,					required=snake_case__				,					)
    parser.add_argument("--prompt"				,					type=snake_case__				,					default="" )
    parser.add_argument("--length"				,					type=snake_case__				,					default=20 )
    parser.add_argument("--stop_token"				,					type=snake_case__				,					default=snake_case__				,					help="Token at which text generation is stopped" )
    parser.add_argument(
        "--temperature"				,					type=snake_case__				,					default=1.0				,					help="temperature of 1.0 has no effect, lower tend toward greedy sampling"				,					)
    parser.add_argument(
        "--repetition_penalty"				,					type=snake_case__				,					default=1.0				,					help="primarily useful for CTRL model; in that case, use 1.2" )
    parser.add_argument("--k"				,					type=snake_case__				,					default=0 )
    parser.add_argument("--penalty_alpha"				,					type=snake_case__				,					default=0.0 )
    parser.add_argument("--p"				,					type=snake_case__				,					default=0.9 )

    parser.add_argument("--prefix"				,					type=snake_case__				,					default=""				,					help="Text added prior to input." )
    parser.add_argument("--padding_text"				,					type=snake_case__				,					default=""				,					help="Deprecated, the use of `--prefix` is preferred." )
    parser.add_argument("--xlm_language"				,					type=snake_case__				,					default=""				,					help="Optional language when used with the XLM model." )

    parser.add_argument("--seed"				,					type=snake_case__				,					default=42				,					help="random seed for initialization" )
    parser.add_argument("--no_cuda"				,					action="store_true"				,					help="Avoid using CUDA when available" )
    parser.add_argument(
        "--fp16"				,					action="store_true"				,					help="Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit"				,					)
    A_								=					parser.parse_args()

    A_								=					torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu" )
    A_								=					0 if args.no_cuda else torch.cuda.device_count()

    logger.warning(f'device: {args.device}, n_gpu: {args.n_gpu}, 16-bits training: {args.fpaa}' )

    set_seed(snake_case__ )

    # Initialize the model and tokenizer
    A_								=					AutoTokenizer.from_pretrained(args.model_name_or_path )
    A_								=					AutoModelForCausalLM.from_pretrained(args.model_name_or_path )

    # tokenizer = GPT2Tokenizer.from_pretrained(args.model_name_or_path)
    # model = OPTForCausalLM.from_pretrained(args.model_name_or_path)
    model.to(args.device )

    if args.fpaa:
        model.half()

    logger.info(snake_case__ )
    A_								=					args.prompt if args.prompt else input("Model prompt >>> " )

    A_								=					tokenizer(snake_case__				,					return_tensors="pt"				,					add_special_tokens=snake_case__ )
    A_								=					{key: value.to(args.device ) for key, value in inputs.items()}

    A_								=					model.generate(
        **snake_case__				,					max_length=args.length + len(inputs["input_ids"][0] )				,					penalty_alpha=args.penalty_alpha				,					top_k=args.k				,					)

    A_								=					[]
    for generated_sequence_idx, generated_sequence in enumerate(snake_case__ ):
        print(f'=== GENERATED SEQUENCE {generated_sequence_idx + 1} ===' )
        A_								=					generated_sequence.tolist()

        # Decode text
        A_								=					tokenizer.decode(snake_case__				,					clean_up_tokenization_spaces=snake_case__				,					add_special_tokens=snake_case__ )

        # Remove all text after the stop token
        A_								=					text[: text.find(args.stop_token ) if args.stop_token else None]

        # Add the prompt at the beginning of the sequence. Remove the excess text that was used for pre-processing
        A_								=					(
            prompt_text + text[len(tokenizer.decode(inputs["input_ids"][0]				,					clean_up_tokenization_spaces=snake_case__ ) ) :]
        )

        generated_sequences.append(snake_case__ )
        print(snake_case__ )

    return generated_sequences


if __name__ == "__main__":
     main()






