



"""simple docstring"""
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,
)
_lowerCAmelCase					:Optional[int]  =      logging.getLogger(__name__)





def 				lowerCamelCase_		(UpperCamelCase__	:	Dict							):
    np.random.seed(args.seed							)
    torch.manual_seed(args.seed							)
    if args.n_gpu > 0:
        torch.cuda.manual_seed_all(args.seed							)





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

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

    parser.add_argument('''--seed'''  , type=UpperCamelCase__  , 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'''  , )
    _UpperCAmelCase							:						str       							=					parser.parse_args()

    _UpperCAmelCase							:						Optional[Any]       							=					torch.device('''cuda''' if torch.cuda.is_available() and not args.no_cuda else '''cpu'''							)
    _UpperCAmelCase							:						Any       							=					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(UpperCamelCase__							)

    # Initialize the model and tokenizer
    _UpperCAmelCase							:						Tuple       							=					AutoTokenizer.from_pretrained(args.model_name_or_path							)
    _UpperCAmelCase							:						Optional[int]       							=					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(UpperCamelCase__							)
    _UpperCAmelCase							:						str       							=					args.prompt if args.prompt else input('''Model prompt >>> '''							)

    _UpperCAmelCase							:						Tuple       							=					tokenizer(UpperCamelCase__  , return_tensors='''pt'''  , add_special_tokens=UpperCamelCase__							)
    _UpperCAmelCase							:						Dict       							=					{key: value.to(args.device							) for key, value in inputs.items()}

    _UpperCAmelCase							:						List[Any]       							=					model.generate(
        **UpperCamelCase__  , max_length=args.length + len(inputs['''input_ids'''][0]							)  , penalty_alpha=args.penalty_alpha  , top_k=args.k  , )

    _UpperCAmelCase							:						Tuple       							=					[]
    for generated_sequence_idx, generated_sequence in enumerate(UpperCamelCase__							):
        print(F'=== GENERATED SEQUENCE {generated_sequence_idx + 1} ==='							)
        _UpperCAmelCase							:						Optional[Any]       							=					generated_sequence.tolist()

        # Decode text
        _UpperCAmelCase							:						Dict       							=					tokenizer.decode(UpperCamelCase__  , clean_up_tokenization_spaces=UpperCamelCase__  , add_special_tokens=UpperCamelCase__							)

        # Remove all text after the stop token
        _UpperCAmelCase							:						Any       							=					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
        _UpperCAmelCase							:						List[Any]       							=					(
            prompt_text + text[len(tokenizer.decode(inputs['''input_ids'''][0]  , clean_up_tokenization_spaces=UpperCamelCase__							)							) :]
        )

        generated_sequences.append(UpperCamelCase__							)
        print(UpperCamelCase__							)

    return generated_sequences


if __name__ == "__main__":
  main()







"""simple docstring"""
# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
  import tensorflow as tf
class 							_UpperCAmelCase							(				TensorFormatter[Mapping, '''tf.Tensor''', Mapping]   ):





     '''simple docstring'''



     def __init__(      self ,			A=None ,			**A	)							->      List[str]:
         super().__init__(features=A	)
         _UpperCAmelCase							:						Any       							=					tf_tensor_kwargs
         import tensorflow as tf  # noqa: import tf at initialization





     def 				__lowerCAmelCase			(      self ,			A	)							->      Optional[Any]:
         import tensorflow as tf

         if isinstance(A ,			A	) and column:
             if all(
                 isinstance(A ,			tf.Tensor	) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column	):
                 return tf.stack(A	)
             elif all(
                 isinstance(A ,			(tf.Tensor, tf.RaggedTensor)	) and x.ndim == 1 and x.dtype == column[0].dtype
                 for x in column	):
                 # only rag 1-D tensors, otherwise some dimensions become ragged even though they were consolidated
                 return tf.ragged.stack(A	)

         return column





     def 				__lowerCAmelCase			(      self ,			A	)							->      int:
         import tensorflow as tf

         if value is None:
             return value

         _UpperCAmelCase							:						Any       							=					{}

         if isinstance(A ,			(np.number, np.ndarray)	) and np.issubdtype(value.dtype ,			np.integer	):
             _UpperCAmelCase							:						Union[str, Any]       							=					{'''dtype''': tf.intaa}
         elif isinstance(A ,			(np.number, np.ndarray)	) and np.issubdtype(value.dtype ,			np.floating	):
             _UpperCAmelCase							:						Dict       							=					{'''dtype''': tf.floataa}
         elif config.PIL_AVAILABLE and "PIL" in sys.modules:
             import PIL.Image

             if isinstance(A ,			PIL.Image.Image	):
                 _UpperCAmelCase							:						Optional[int]       							=					np.asarray(A	)

         return tf.convert_to_tensor(A ,			**{**default_dtype, **self.tf_tensor_kwargs}	)





     def 				__lowerCAmelCase			(      self ,			A	)							->      Tuple:
         import tensorflow as tf

         # support for torch, tf, jax etc.
         if config.TORCH_AVAILABLE and "torch" in sys.modules:
             import torch

             if isinstance(A ,			torch.Tensor	):
                 return self._tensorize(data_struct.detach().cpu().numpy()[()]	)
         if hasattr(A ,			'''__array__'''	) and not isinstance(A ,			tf.Tensor	):
             _UpperCAmelCase							:						List[str]       							=					data_struct.__array__()
         # support for nested types like struct of list of struct
         if isinstance(A ,			np.ndarray	):
             if data_struct.dtype == object:  # tf tensors cannot be instantied from an array of objects
                 return self._consolidate([self.recursive_tensorize(A	) for substruct in data_struct]	)
         elif isinstance(A ,			(list, tuple)	):
             return self._consolidate([self.recursive_tensorize(A	) for substruct in data_struct]	)
         return self._tensorize(A	)





     def 				__lowerCAmelCase			(      self ,			A	)							->      Optional[int]:
         return map_nested(self._recursive_tensorize ,			A ,			map_list=A	)





     def 				__lowerCAmelCase			(      self ,			A	)							->      Mapping:
         _UpperCAmelCase							:						str       							=					self.numpy_arrow_extractor().extract_row(A	)
         _UpperCAmelCase							:						List[Any]       							=					self.python_features_decoder.decode_row(A	)
         return self.recursive_tensorize(A	)





     def 				__lowerCAmelCase			(      self ,			A	)							->      "tf.Tensor":
         _UpperCAmelCase							:						Union[str, Any]       							=					self.numpy_arrow_extractor().extract_column(A	)
         _UpperCAmelCase							:						Tuple       							=					self.python_features_decoder.decode_column(A ,			pa_table.column_names[0]	)
         _UpperCAmelCase							:						str       							=					self.recursive_tensorize(A	)
         _UpperCAmelCase							:						Optional[Any]       							=					self._consolidate(A	)
         return column





     def 				__lowerCAmelCase			(      self ,			A	)							->      Mapping:
         _UpperCAmelCase							:						Dict       							=					self.numpy_arrow_extractor().extract_batch(A	)
         _UpperCAmelCase							:						Tuple       							=					self.python_features_decoder.decode_batch(A	)
         _UpperCAmelCase							:						Union[str, Any]       							=					self.recursive_tensorize(A	)
         for column_name in batch:
             _UpperCAmelCase							:						str       							=					self._consolidate(batch[column_name]	)
         return batch


