






"""simple docstring"""




import json
import multiprocessing
import os
import re
from collections import defaultdict

import torch
from accelerate import Accelerator
from accelerate.utils import set_seed
from arguments import HumanEvalArguments
from datasets import load_dataset, load_metric
from torch.utils.data import IterableDataset
from torch.utils.data.dataloader import DataLoader
from tqdm import tqdm

import transformers
from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList


UpperCamelCase							       =    ["\nclass", "\ndef", "\n#", "\n@", "\nprint", "\nif"]




class    lowercase_    (_UpperCAmelCase     ):






      def __init__(    self     ,     a_     ,     a_     ,     a_=None     ,     a_=1	)	->List[Any]:







        '''simple docstring'''


        _a							=	tokenizer
        _a							=	dataset
        _a							=	len(lowercase_	) if n_tasks is None else n_tasks
        _a							=	n_copies






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







        '''simple docstring'''


        _a							=	[]
        for task in range(self.n_tasks	):
          # without strip, the model generate commented codes ...
          prompts.append(self.tokenizer.eos_token + self.dataset[task]["prompt"].strip()	)
        _a							=	self.tokenizer(lowercase_     ,     padding=lowercase_     ,     return_tensors="pt"	)
        for task in range(self.n_tasks	):
          for _ in range(self.n_copies	):
            yield {
                "ids": outputs.input_ids[task],
                "task_id": task,
                "input_len": outputs.attention_mask[task].sum(),
            }







class    lowercase_    (_UpperCAmelCase     ):






      def __init__(    self     ,     a_     ,     a_     ,     a_	)	->List[Any]:







        '''simple docstring'''


        _a							=	start_length
        _a							=	eof_strings
        _a							=	tokenizer






      def __call__(    self     ,     a_     ,     a_     ,     **a_	)	->Optional[int]:







        '''simple docstring'''


        _a							=	self.tokenizer.batch_decode(input_ids[:, self.start_length :]	)
        _a							=	[]
        for decoded_generation in decoded_generations:
          done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings	)	)
        return all(lowercase_	)







def        lowerCAmelCase (      UpperCamelCase_:							str					)       ->							List[str]:







  '''simple docstring'''







  _a							=	re.split("(%s)" % "|".join(UpperCAmelCase__					)     , UpperCAmelCase__					)
  # last string should be ""
  return "".join(string_list[:-2]					)






def        lowerCAmelCase (      UpperCamelCase_:							Union[str, Any]     , UpperCamelCase_:							int     , UpperCamelCase_:							List[Any]     , UpperCamelCase_:							Any     , UpperCamelCase_:							Optional[int]     , UpperCamelCase_:							str=20     , **UpperCamelCase_:							List[Any]					)       ->							Dict:







  '''simple docstring'''







  _a							=	defaultdict(UpperCAmelCase__					)  # dict of list of generated tokens
  for step, batch in tqdm(enumerate(UpperCAmelCase__					)					):
    with torch.no_grad():
      _a							=	batch["""ids"""].shape[-1]
      _a							=	accelerator.unwrap_model(UpperCAmelCase__					).generate(
          input_ids=batch["ids"][:, : batch["input_len"]]     , num_return_sequences=UpperCAmelCase__     , **UpperCAmelCase__					)
      # each task is generated batch_size times
      _a							=	batch["""task_id"""].repeat(UpperCAmelCase__					)
      _a							=	accelerator.pad_across_processes(
          UpperCAmelCase__     , dim=1     , pad_index=tokenizer.pad_token_id					)

      _a							=	accelerator.gather((generated_tokens, generated_tasks)					)
      _a							=	generated_tokens.cpu().numpy()
      _a							=	generated_tasks.cpu().numpy()

      for task, generated_tokens in zip(UpperCAmelCase__     , UpperCAmelCase__					):
        gen_token_dict[task].append(UpperCAmelCase__					)

  _a							=	[[] for _ in range(UpperCAmelCase__					)]
  for task, generated_tokens in gen_token_dict.items():
    for s in generated_tokens:
      _a							=	tokenizer.decode(UpperCAmelCase__     , skip_special_tokens=UpperCAmelCase__     , clean_up_tokenization_spaces=UpperCAmelCase__					)
      code_gens[task].append(remove_last_block(UpperCAmelCase__					)					)
  return code_gens






def        lowerCAmelCase (      )       ->							List[str]:







  '''simple docstring'''







  _a							=	HfArgumentParser(UpperCAmelCase__					)
  _a							=	parser.parse_args()

  transformers.logging.set_verbosity_error()
  # enables code execution in code_eval metric
  _a							=	args.HF_ALLOW_CODE_EVAL
  # make sure tokenizer plays nice with multiprocessing
  _a							=	"""false"""

  if args.num_workers is None:
    _a							=	multiprocessing.cpu_count()

  # Use dataset load to feed to accelerate
  _a							=	Accelerator()
  set_seed(args.seed     , device_specific=UpperCAmelCase__					)

  # Load model and tokenizer
  _a							=	AutoTokenizer.from_pretrained(args.model_ckpt					)
  _a							=	tokenizer.eos_token
  _a							=	AutoModelForCausalLM.from_pretrained(args.model_ckpt					)

  # Generation settings
  _a							=	{
      """do_sample""": args.do_sample,
      """temperature""": args.temperature,
      """max_new_tokens""": args.max_new_tokens,
      """top_p""": args.top_p,
      """top_k""": args.top_k,
      """stopping_criteria""": StoppingCriteriaList([EndOfFunctionCriteria(0     , UpperCAmelCase__     , UpperCAmelCase__					)]					),
  }

  # Load evaluation dataset and metric
  _a							=	load_dataset("openai_humaneval"					)
  _a							=	load_metric("code_eval"					)

  _a							=	args.num_tasks if args.num_tasks is not None else len(human_eval["test"]					)
  _a							=	args.n_samples // args.batch_size

  _a							=	TokenizedDataset(UpperCAmelCase__     , human_eval["test"]     , n_copies=UpperCAmelCase__     , n_tasks=UpperCAmelCase__					)
  # do not confuse args.batch_size, which is actually the num_return_sequences
  _a							=	DataLoader(UpperCAmelCase__     , batch_size=1					)

  # Run a quick test to see if code evaluation is enabled
  try:
    _a							=	code_eval_metric.compute(references=[""]     , predictions=[[""]]					)
  except ValueError as exception:
    print(
        "Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL=\"1\"`"
        " flag to enable code evaluation."					)
    raise exception

  _a							=	accelerator.prepare(UpperCAmelCase__     , UpperCAmelCase__					)

  _a							=	complete_code(
      UpperCAmelCase__     , UpperCAmelCase__     , UpperCAmelCase__     , UpperCAmelCase__     , n_tasks=UpperCAmelCase__     , batch_size=args.batch_size     , **UpperCAmelCase__     , )

  if accelerator.is_main_process:
    _a							=	[]

    for task in tqdm(range(UpperCAmelCase__					)					):
      _a							=	human_eval["""test"""][task]["""test"""]
      _a							=	f'''check({human_eval['test'][task]['entry_point']})'''
      references.append("\n" + test_func + "\n" + entry_point					)

    # Evaluate completions with "code_eval" metric
    _a							=	code_eval_metric.compute(
        references=UpperCAmelCase__     , predictions=UpperCAmelCase__     , num_workers=args.num_workers					)
    print(f'''Results: {pass_at_k}'''					)

    # Save results to json file
    with open(args.output_file     , "w"					) as fp:
      json.dump(UpperCAmelCase__     , UpperCAmelCase__					)


# For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing
# https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script
if __name__ == "__main__":
       main()










"""simple docstring"""




def        lowerCAmelCase (      UpperCamelCase_:							Optional[int]     , UpperCamelCase_:							str					)       ->							List[Any]:







  '''simple docstring'''







  _a							=	(boundary[1] - boundary[0]) / steps
  _a							=	boundary[0]
  _a							=	boundary[1]
  _a							=	make_points(UpperCamelCase_     , UpperCamelCase_     , UpperCamelCase_					)
  _a							=	0.0
  y += (h / 2.0) * f(UpperCamelCase_					)
  for i in x_i:
    # print(i)
    y += h * f(UpperCamelCase_					)
  y += (h / 2.0) * f(UpperCamelCase_					)
  return y






def        lowerCAmelCase (      UpperCamelCase_:							str     , UpperCamelCase_:							Any     , UpperCamelCase_:							Any					)       ->							str:







  '''simple docstring'''







  _a							=	a + h
  while x < (b - h):
    yield x
    _a							=	x + h






def        lowerCAmelCase (      UpperCamelCase_:							List[str]					)       ->							int:  # enter your function here







  '''simple docstring'''







  _a							=	(x - 0) * (x - 0)
  return y






def        lowerCAmelCase (      )       ->							Tuple:







  '''simple docstring'''







  _a							=	0.0  # Lower bound of integration
  _a							=	1.0  # Upper bound of integration
  _a							=	10.0  # define number of steps or resolution
  _a							=	[a, b]  # define boundary of integration
  _a							=	method_a(UpperCamelCase_     , UpperCamelCase_					)
  print(f'''y = {y}'''					)


if __name__ == "__main__":
     main()


