



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								=      logging.getLogger(__name__)







def  _snake_case			(							__snake_case     )  ->  List[Any]:






      '''simple docstring'''







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







def  _snake_case			(							)  ->  Any:






      '''simple docstring'''







      UpperCAmelCase_	:						Optional[int]					   =		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=2_0     )
      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=4_2       ,	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_	:						int					   =		parser.parse_args()

      UpperCAmelCase_	:						List[str]					   =		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(__snake_case     )

      # Initialize the model and tokenizer
      UpperCAmelCase_	:						Union[str, Any]					   =		AutoTokenizer.from_pretrained(args.model_name_or_path     )
      UpperCAmelCase_	:						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(__snake_case     )
      UpperCAmelCase_	:						Union[str, Any]					   =		args.prompt if args.prompt else input("Model prompt >>> "     )

      UpperCAmelCase_	:						str					   =		tokenizer(__snake_case       ,	return_tensors="pt"       ,	add_special_tokens=__snake_case     )
      UpperCAmelCase_	:						Optional[Any]					   =		{key: value.to(args.device     ) for key, value in inputs.items()}

      UpperCAmelCase_	:						str					   =		model.generate(
          **__snake_case       ,	max_length=args.length + len(inputs["input_ids"][0]     )       ,	penalty_alpha=args.penalty_alpha       ,	top_k=args.k       ,	)

      UpperCAmelCase_	:						int					   =		[]
      for generated_sequence_idx, generated_sequence in enumerate(__snake_case     ):
            print(F"""=== GENERATED SEQUENCE {generated_sequence_idx + 1} ==="""     )
            UpperCAmelCase_	:						Tuple					   =		generated_sequence.tolist()

            # Decode text
            UpperCAmelCase_	:						Union[str, Any]					   =		tokenizer.decode(__snake_case       ,	clean_up_tokenization_spaces=__snake_case       ,	add_special_tokens=__snake_case     )

            # Remove all text after the stop token
            UpperCAmelCase_	:						Optional[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_	:						str					   =		(
                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()





import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Optional

import transformers
from transformers import (
    AutoConfig,
    AutoModelForQuestionAnswering,
    AutoTokenizer,
    DataCollatorWithPadding,
    HfArgumentParser,
    SquadDataset,
    Trainer,
    TrainingArguments,
)
from transformers import SquadDataTrainingArguments as DataTrainingArguments
from transformers.trainer_utils import is_main_process


__lowerCamelCase								=      logging.getLogger(__name__)


@dataclass
class 	snake_case_			:




       """simple docstring"""






       _lowerCamelCase           =					field(
           metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}	)
       _lowerCamelCase           =					field(
           default=lowercase__			,			metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}	)
       _lowerCamelCase           =					field(
           default=lowercase__			,			metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}	)
       _lowerCamelCase           =					field(default=lowercase__			,			metadata={"""help""": """Set this flag to use fast tokenization."""}	)
       # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script,
       # or just modify its tokenizer_config.json.
       _lowerCamelCase           =					field(
           default=lowercase__			,			metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""}			,			)







def  _snake_case			(							)  ->  Union[str, Any]:






      '''simple docstring'''







      UpperCAmelCase_	:						Union[str, Any]					   =		HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)     )

      if len(sys.argv     ) == 2 and sys.argv[1].endswith(".json"     ):
            # If we pass only one argument to the script and it's the path to a json file,
            # let's parse it to get our arguments.
            UpperCAmelCase_   ,   UpperCAmelCase_   ,   UpperCAmelCase_	:						Any					   =		parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]     )     )
      else:
            UpperCAmelCase_   ,   UpperCAmelCase_   ,   UpperCAmelCase_	:						Union[str, Any]					   =		parser.parse_args_into_dataclasses()

      if (
          os.path.exists(training_args.output_dir     )
          and os.listdir(training_args.output_dir     )
          and training_args.do_train
          and not training_args.overwrite_output_dir
      ):
            raise ValueError(
                F"""Output directory ({training_args.output_dir}) already exists and is not empty. Use"""
                " --overwrite_output_dir to overcome."     )

      # Setup logging
      logging.basicConfig(
          format="%(asctime)s - %(levelname)s - %(name)s - %(message)s"       ,	datefmt="%m/%d/%Y %H:%M:%S"       ,	level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN       ,	)
      logger.warning(
          "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s"       ,	training_args.local_rank       ,	training_args.device       ,	training_args.n_gpu       ,	bool(training_args.local_rank != -1     )       ,	training_args.fpaa       ,	)
      # Set the verbosity to info of the Transformers logger (on main process only):
      if is_main_process(training_args.local_rank     ):
            transformers.utils.logging.set_verbosity_info()
            transformers.utils.logging.enable_default_handler()
            transformers.utils.logging.enable_explicit_format()
      logger.info("Training/evaluation parameters %s"       ,	__snake_case     )

      # Prepare Question-Answering task
      # Load pretrained model and tokenizer
      #
      # Distributed training:
      # The .from_pretrained methods guarantee that only one local process can concurrently
      # download model & vocab.

      UpperCAmelCase_	:						Any					   =		AutoConfig.from_pretrained(
          model_args.config_name if model_args.config_name else model_args.model_name_or_path       ,	cache_dir=model_args.cache_dir       ,	)
      UpperCAmelCase_	:						List[str]					   =		AutoTokenizer.from_pretrained(
          model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path       ,	cache_dir=model_args.cache_dir       ,	use_fast=__snake_case       ,	)
      UpperCAmelCase_	:						Optional[int]					   =		AutoModelForQuestionAnswering.from_pretrained(
          model_args.model_name_or_path       ,	from_tf=bool(".ckpt" in model_args.model_name_or_path     )       ,	config=__snake_case       ,	cache_dir=model_args.cache_dir       ,	)

      # Get datasets
      UpperCAmelCase_	:						str					   =		hasattr(model.config       ,	"lang2id"     )
      UpperCAmelCase_	:						Optional[Any]					   =		(
          SquadDataset(
              __snake_case       ,	tokenizer=__snake_case       ,	is_language_sensitive=__snake_case       ,	cache_dir=model_args.cache_dir     )
          if training_args.do_train
          else None
      )
      UpperCAmelCase_	:						Optional[Any]					   =		(
          SquadDataset(
              __snake_case       ,	tokenizer=__snake_case       ,	mode="dev"       ,	is_language_sensitive=__snake_case       ,	cache_dir=model_args.cache_dir       ,	)
          if training_args.do_eval
          else None
      )

      # Data collator
      UpperCAmelCase_	:						Optional[Any]					   =		DataCollatorWithPadding(__snake_case       ,	pad_to_multiple_of=8     ) if training_args.fpaa else None

      # Initialize our Trainer
      UpperCAmelCase_	:						Any					   =		Trainer(
          model=__snake_case       ,	args=__snake_case       ,	train_dataset=__snake_case       ,	eval_dataset=__snake_case       ,	data_collator=__snake_case       ,	)

      # Training
      if training_args.do_train:
            trainer.train(
                model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path     ) else None     )
            trainer.save_model()
            # For convenience, we also re-save the tokenizer to the same directory,
            # so that you can share your model easily on huggingface.co/models =)
            if trainer.is_world_master():
                  tokenizer.save_pretrained(training_args.output_dir     )







def  _snake_case			(							__snake_case     )  ->  List[str]:






      '''simple docstring'''







      main()


if __name__ == "__main__":
   main()
