






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


A        =			['\nclass', '\ndef', '\n#', '\n@', '\nprint', '\nif']


class 							__a							(  __A						):





		'''simple docstring'''



		def __init__(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__=None ,    UpperCamelCase__=1       ):
						SCREAMING_SNAKE_CASE_   :	int													=   tokenizer
						SCREAMING_SNAKE_CASE_   :	str													=   dataset
						SCREAMING_SNAKE_CASE_   :	int													=   len(UpperCamelCase__       ) if n_tasks is None else n_tasks
						SCREAMING_SNAKE_CASE_   :	Dict													=   n_copies






		def __iter__(						self       ):
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   []
						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()       )
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   self.tokenizer(UpperCamelCase__ ,    padding=UpperCamelCase__ ,    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 							__a							(  __A						):





		'''simple docstring'''



		def __init__(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	Any													=   start_length
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   eof_strings
						SCREAMING_SNAKE_CASE_   :	Tuple													=   tokenizer






		def __call__(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    **UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	str													=   self.tokenizer.batch_decode(input_ids[:, self.start_length :]       )
						SCREAMING_SNAKE_CASE_   :	Tuple													=   []
						for decoded_generation in decoded_generations:
										done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings       )       )
						return all(UpperCamelCase__       )






def 					_lowerCamelCase(			lowerCAmelCase__				:		Any      ):





				'''simple docstring'''







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



def 					_lowerCamelCase(			lowerCAmelCase__				:		str    ,    lowerCAmelCase__				:		int    ,    lowerCAmelCase__				:		List[Any]    ,    lowerCAmelCase__				:		Optional[Any]    ,    lowerCAmelCase__				:		Optional[int]    ,    lowerCAmelCase__				:		str=20    ,    **lowerCAmelCase__				:		int      ):





				'''simple docstring'''







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

												SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_   :	Tuple													=   accelerator.gather((generated_tokens, generated_tasks)      )
												SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   generated_tokens.cpu().numpy()
												SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   generated_tasks.cpu().numpy()

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

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



def 					_lowerCamelCase(			):





				'''simple docstring'''







				SCREAMING_SNAKE_CASE_   :	Tuple													=   HfArgumentParser(lowerCAmelCase__      )
				SCREAMING_SNAKE_CASE_   :	List[str]													=   parser.parse_args()

				transformers.logging.set_verbosity_error()
				# enables code execution in code_eval metric
				SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   args.HF_ALLOW_CODE_EVAL
				# make sure tokenizer plays nice with multiprocessing
				SCREAMING_SNAKE_CASE_   :	Dict													=   'false'

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

				# Use dataset load to feed to accelerate
				SCREAMING_SNAKE_CASE_   :	Dict													=   Accelerator()
				set_seed(args.seed    ,    device_specific=lowerCAmelCase__      )

				# Load model and tokenizer
				SCREAMING_SNAKE_CASE_   :	Optional[int]													=   AutoTokenizer.from_pretrained(args.model_ckpt      )
				SCREAMING_SNAKE_CASE_   :	Dict													=   tokenizer.eos_token
				SCREAMING_SNAKE_CASE_   :	int													=   AutoModelForCausalLM.from_pretrained(args.model_ckpt      )

				# Generation settings
				SCREAMING_SNAKE_CASE_   :	int													=   {
				    '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    ,    lowerCAmelCase__    ,    lowerCAmelCase__      )]      ),
				}

				# Load evaluation dataset and metric
				SCREAMING_SNAKE_CASE_   :	Tuple													=   load_dataset('openai_humaneval'      )
				SCREAMING_SNAKE_CASE_   :	Tuple													=   load_metric('code_eval'      )

				SCREAMING_SNAKE_CASE_   :	Dict													=   args.num_tasks if args.num_tasks is not None else len(human_eval['test']      )
				SCREAMING_SNAKE_CASE_   :	List[str]													=   args.n_samples // args.batch_size

				SCREAMING_SNAKE_CASE_   :	int													=   TokenizedDataset(lowerCAmelCase__    ,    human_eval['test']    ,    n_copies=lowerCAmelCase__    ,    n_tasks=lowerCAmelCase__      )
				# do not confuse args.batch_size, which is actually the num_return_sequences
				SCREAMING_SNAKE_CASE_   :	Any													=   DataLoader(lowerCAmelCase__    ,    batch_size=1      )

				# Run a quick test to see if code evaluation is enabled
				try:
								SCREAMING_SNAKE_CASE_   :	List[Any]													=   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

				SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_   :	List[str]													=   accelerator.prepare(lowerCAmelCase__    ,    lowerCAmelCase__      )

				SCREAMING_SNAKE_CASE_   :	int													=   complete_code(
				    lowerCAmelCase__    ,    lowerCAmelCase__    ,    lowerCAmelCase__    ,    lowerCAmelCase__    ,    n_tasks=lowerCAmelCase__    ,    batch_size=args.batch_size    ,    **lowerCAmelCase__    ,    )

				if accelerator.is_main_process:
								SCREAMING_SNAKE_CASE_   :	str													=   []

								for task in tqdm(range(lowerCAmelCase__      )      ):
												SCREAMING_SNAKE_CASE_   :	int													=   human_eval['test'][task]['test']
												SCREAMING_SNAKE_CASE_   :	Optional[int]													=   F'''check({human_eval['test'][task]['entry_point']})'''
												references.append('\n' + test_func + '\n' + entry_point      )

								# Evaluate completions with "code_eval" metric
								SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   code_eval_metric.compute(
								    references=lowerCAmelCase__    ,    predictions=lowerCAmelCase__    ,    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(lowerCAmelCase__    ,    lowerCAmelCase__      )


# 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()







def 					_lowerCamelCase(			):





				'''simple docstring'''







				return 1



def 					_lowerCamelCase(			lowerCAmelCase__				:		int      ):





				'''simple docstring'''







				return 0 if x < 0 else two_pence(x - 2      ) + one_pence()



def 					_lowerCamelCase(			lowerCAmelCase__				:		int      ):





				'''simple docstring'''







				return 0 if x < 0 else five_pence(x - 5      ) + two_pence(lowerCAmelCase__      )



def 					_lowerCamelCase(			lowerCAmelCase__				:		int      ):





				'''simple docstring'''







				return 0 if x < 0 else ten_pence(x - 10      ) + five_pence(lowerCAmelCase__      )



def 					_lowerCamelCase(			lowerCAmelCase__				:		int      ):





				'''simple docstring'''







				return 0 if x < 0 else twenty_pence(x - 20      ) + ten_pence(lowerCAmelCase__      )



def 					_lowerCamelCase(			lowerCAmelCase__				:		int      ):





				'''simple docstring'''







				return 0 if x < 0 else fifty_pence(x - 50      ) + twenty_pence(lowerCAmelCase__      )



def 					_lowerCamelCase(			lowerCAmelCase__				:		int      ):





				'''simple docstring'''







				return 0 if x < 0 else one_pound(x - 100      ) + fifty_pence(lowerCAmelCase__      )



def 					_lowerCamelCase(			lowerCAmelCase__				:		int      ):





				'''simple docstring'''







				return 0 if x < 0 else two_pound(x - 200      ) + one_pound(lowerCAmelCase__      )



def 					_lowerCamelCase(			lowerCAmelCase__				:		int = 200      ):





				'''simple docstring'''







				return two_pound(lowerCAmelCase__      )


if __name__ == "__main__":
				print(solution(int(input().strip())))