






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

def     lowerCAmelCase__  (  UpperCamelCase__       ):



  '''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     lowerCAmelCase__  (  ):



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

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

  parser.add_argument("""--seed"""  ,     type=lowercase__  ,     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"""  ,     )
  _a						: str    =			parser.parse_args()

  _a						: int    =			torch.device("""cuda""" if torch.cuda.is_available() and not args.no_cuda else """cpu"""       )
  _a						: int    =			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(lowercase__       )

  # Initialize the model and tokenizer
  _a						: Optional[int]    =			AutoTokenizer.from_pretrained(args.model_name_or_path       )
  _a						: Dict    =			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(lowercase__       )
  _a						: List[Any]    =			args.prompt if args.prompt else input("""Model prompt >>> """       )

  _a						: Tuple    =			tokenizer(lowercase__  ,     return_tensors="""pt"""  ,     add_special_tokens=lowercase__       )
  _a						: List[Any]    =			{key: value.to(args.device       ) for key, value in inputs.items()}

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

  _a						: List[Any]    =			[]
  for generated_sequence_idx, generated_sequence in enumerate(lowercase__       ):
    print(F"""=== GENERATED SEQUENCE {generated_sequence_idx + 1} ==="""       )
    _a						: Any    =			generated_sequence.tolist()

    # Decode text
    _a						: Tuple    =			tokenizer.decode(lowercase__  ,     clean_up_tokenization_spaces=lowercase__  ,     add_special_tokens=lowercase__       )

    # Remove all text after the stop token
    _a						: 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
    _a						: Optional[int]    =			(
        prompt_text + text[len(tokenizer.decode(inputs["""input_ids"""][0]  ,     clean_up_tokenization_spaces=lowercase__       )       ) :]
    )

    generated_sequences.append(lowercase__       )
    print(lowercase__       )

  return generated_sequences


if __name__ == "__main__":
 main()









"""simple docstring"""




import json
import os
from dataclasses import dataclass

import numpy as np
import pyarrow as pa

import datasets
from utils import get_duration


_snake_case					   =     1000_0000_0000
_snake_case					   =     1_0000

_snake_case   ,				_snake_case					   =     os.path.split(__file__)
_snake_case					   =     os.path.join(RESULTS_BASEPATH, 'results', RESULTS_FILENAME.replace('.py', '.json'))

def     lowerCAmelCase__  (  UpperCamelCase__  ,     UpperCamelCase__       ):



  '''simple docstring'''
  _a						: Union[str, Any]    =			pa.Table.from_pydict({"""col""": [0] * chunk_size}       )
  _a						: Dict    =			pa.concat_tables([table] * (num_examples // chunk_size)       )
  return datasets.Dataset(UpperCamelCase__  ,     fingerprint="""table_100B"""       )






@dataclass
class  UpperCamelCase    :
 UpperCamelCase      : int
 UpperCamelCase      : int
 UpperCamelCase      : int
 UpperCamelCase      : int


 def _lowercase			(  self	:		Optional[int]							)      -> Dict:
   _a						: Tuple    =			np.random.default_rng(self.seed							)
   _a						: Dict    =			rng.integers(low=self.low							,		high=self.high							,		size=self.size							).tolist()


 def __iter__(  self	:		List[Any]							)      -> Optional[Any]:
   return iter(self._sampled_values							)







 def __len__(  self	:		Dict							)      -> Optional[Any]:
   return self.size







@get_duration
def     lowerCAmelCase__  (  UpperCamelCase__       ):



  '''simple docstring'''
  _a						: Union[str, Any]    =			dataset[0]

@get_duration
def     lowerCAmelCase__  (  UpperCamelCase__       ):



  '''simple docstring'''
  _a						: Optional[Any]    =			dataset[-1]

@get_duration
def     lowerCAmelCase__  (  UpperCamelCase__       ):



  '''simple docstring'''
  _a						: str    =			dataset[range(len(UpperCamelCase__       ) // 2  ,     len(UpperCamelCase__       ) // 2 + 1_0_2_4       )]

@get_duration
def     lowerCAmelCase__  (  UpperCamelCase__       ):



  '''simple docstring'''
  _a						: Tuple    =			dataset[RandIter(0  ,     len(UpperCamelCase__       )  ,     1_0_2_4  ,     seed=4_2       )]

def     lowerCAmelCase__  (  ):



  '''simple docstring'''
  _a						: str    =			{"""num examples""": SPEED_TEST_N_EXAMPLES}
  _a						: Dict    =			(get_first_row, get_last_row, get_batch_of_aaaa_rows, get_batch_of_aaaa_random_rows)
  print("""generating dataset"""       )
  _a						: List[str]    =			generate_100B_dataset(num_examples=UpperCamelCase__  ,     chunk_size=UpperCamelCase__       )
  print("""Functions"""       )
  for func in functions:
    print(func.__name__       )
    _a						: Optional[Any]    =			func(UpperCamelCase__       )

  with open(UpperCamelCase__  ,     """wb"""       ) as f:
    f.write(json.dumps(UpperCamelCase__       ).encode("""utf-8"""       )       )


if __name__ == "__main__":  # useful to run the profiler
 benchmark_table_100B()

