



'''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__:   Tuple								=			["\nclass", "\ndef", "\n#", "\n@", "\nprint", "\nif"]

class 				SCREAMING_SNAKE_CASE(							A__			):







	"""simple docstring"""






	def __init__(						self			:     List[Any]		,			__snake_case			:     Dict		,			__snake_case			:     str		,			__snake_case			:     Any=None		,			__snake_case			:     List[str]=1		)			->		List[str]:
								UpperCAmelCase :       str 							=		tokenizer
								UpperCAmelCase :       Tuple 							=		dataset
								UpperCAmelCase :       int 							=		len(__snake_case		) if n_tasks is None else n_tasks
								UpperCAmelCase :       str 							=		n_copies







	def __iter__(						self			:     Union[str, Any]		)			->		Dict:
								UpperCAmelCase :       Union[str, 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()		)
								UpperCAmelCase :       Any 							=		self.tokenizer(__snake_case		,			padding=__snake_case		,			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 				SCREAMING_SNAKE_CASE(							A__			):







	"""simple docstring"""






	def __init__(						self			:     Any		,			__snake_case			:     Dict		,			__snake_case			:     Optional[Any]		,			__snake_case			:     Optional[int]		)			->		List[Any]:
								UpperCAmelCase :       Dict 							=		start_length
								UpperCAmelCase :       Tuple 							=		eof_strings
								UpperCAmelCase :       Union[str, Any] 							=		tokenizer







	def __call__(						self			:     List[Any]		,			__snake_case			:     List[str]		,			__snake_case			:     List[Any]		,			**__snake_case			:     Union[str, Any]		)			->		Dict:
								UpperCAmelCase :       Tuple 							=		self.tokenizer.batch_decode(input_ids[:, self.start_length :]		)
								UpperCAmelCase :       List[Any] 							=		[]
								for decoded_generation in decoded_generations:
															done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings		)		)
								return all(__snake_case		)


def  snake_case_     (					_lowerCAmelCase  :						List[str]  )      ->   Optional[int]:
							UpperCAmelCase :       int 							=		re.split('''(%s)''' % '''|'''.join(_lowerCAmelCase  )   ,      _lowerCAmelCase  )
							# last string should be ""
							return "".join(string_list[:-2]  )


def  snake_case_     (					_lowerCAmelCase  :						Tuple   ,      _lowerCAmelCase  :						Dict   ,      _lowerCAmelCase  :						Union[str, Any]   ,      _lowerCAmelCase  :						Tuple   ,      _lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						int=20   ,      **_lowerCAmelCase  :						Tuple  )      ->   Union[str, Any]:
							UpperCAmelCase :       Optional[Any] 							=		defaultdict(_lowerCAmelCase  )  # dict of list of generated tokens
							for step, batch in tqdm(enumerate(_lowerCAmelCase  )  ):
														with torch.no_grad():
																					UpperCAmelCase :       Union[str, Any] 							=		batch['''ids'''].shape[-1]
																					UpperCAmelCase :       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
																					UpperCAmelCase :       Tuple 							=		batch['''task_id'''].repeat(_lowerCAmelCase  )
																					UpperCAmelCase :       Any 							=		accelerator.pad_across_processes(
																					    _lowerCAmelCase   ,      dim=1   ,      pad_index=tokenizer.pad_token_id  )

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

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

							UpperCAmelCase :       Dict 							=		[[] for _ in range(_lowerCAmelCase  )]
							for task, generated_tokens in gen_token_dict.items():
														for s in generated_tokens:
																					UpperCAmelCase :       Optional[int] 							=		tokenizer.decode(_lowerCAmelCase   ,      skip_special_tokens=_lowerCAmelCase   ,      clean_up_tokenization_spaces=_lowerCAmelCase  )
																					code_gens[task].append(remove_last_block(_lowerCAmelCase  )  )
							return code_gens


def  snake_case_     (					)      ->   List[Any]:
							# Setup configuration
							UpperCAmelCase :       Any 							=		HfArgumentParser(_lowerCAmelCase  )
							UpperCAmelCase :       Tuple 							=		parser.parse_args()

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

							if args.num_workers is None:
														UpperCAmelCase :       Optional[Any] 							=		multiprocessing.cpu_count()

							# Use dataset load to feed to accelerate
							UpperCAmelCase :       int 							=		Accelerator()
							set_seed(args.seed   ,      device_specific=_lowerCAmelCase  )

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

							# Generation settings
							UpperCAmelCase :       Any 							=		{
							    '''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
							UpperCAmelCase :       Optional[int] 							=		load_dataset('''openai_humaneval'''  )
							UpperCAmelCase :       Any 							=		load_metric('''code_eval'''  )

							UpperCAmelCase :       Optional[Any] 							=		args.num_tasks if args.num_tasks is not None else len(human_eval['''test''']  )
							UpperCAmelCase :       Tuple 							=		args.n_samples // args.batch_size

							UpperCAmelCase :       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
							UpperCAmelCase :       Dict 							=		DataLoader(_lowerCAmelCase   ,      batch_size=1  )

							# Run a quick test to see if code evaluation is enabled
							try:
														UpperCAmelCase :       Tuple 							=		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

							UpperCAmelCase							,		UpperCAmelCase :       str 							=		accelerator.prepare(_lowerCAmelCase   ,      _lowerCAmelCase  )

							UpperCAmelCase :       Union[str, Any] 							=		complete_code(
							    _lowerCAmelCase   ,      _lowerCAmelCase   ,      _lowerCAmelCase   ,      _lowerCAmelCase   ,      n_tasks=_lowerCAmelCase   ,      batch_size=args.batch_size   ,      **_lowerCAmelCase   ,      )

							if accelerator.is_main_process:
														UpperCAmelCase :       str 							=		[]

														for task in tqdm(range(_lowerCAmelCase  )  ):
																					UpperCAmelCase :       Union[str, Any] 							=		human_eval['''test'''][task]['''test''']
																					UpperCAmelCase :       Union[str, Any] 							=		f"""check({human_eval["test"][task]["entry_point"]})"""
																					references.append('''\n''' + test_func + '''\n''' + entry_point  )

														# Evaluate completions with "code_eval" metric
														UpperCAmelCase							,		UpperCAmelCase :       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()










'''simple docstring'''






from math import cos, sin, sqrt, tau

from audio_filters.iir_filter import IIRFilter


def  snake_case_     (					_lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						float = 1 / sqrt(2  )  )      ->   IIRFilter:
							UpperCAmelCase :       Optional[int] 							=		tau * frequency / samplerate
							UpperCAmelCase :       List[Any] 							=		sin(_lowerCAmelCase  )
							UpperCAmelCase :       Optional[Any] 							=		cos(_lowerCAmelCase  )
							UpperCAmelCase :       int 							=		_sin / (2 * q_factor)

							UpperCAmelCase :       Any 							=		(1 - _cos) / 2
							UpperCAmelCase :       List[Any] 							=		1 - _cos

							UpperCAmelCase :       Union[str, Any] 							=		1 + alpha
							UpperCAmelCase :       Any 							=		-2 * _cos
							UpperCAmelCase :       Dict 							=		1 - alpha

							UpperCAmelCase :       Union[str, Any] 							=		IIRFilter(2  )
							filt.set_coefficients([aa, aa, aa]   ,      [ba, ba, ba]  )
							return filt


def  snake_case_     (					_lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						float = 1 / sqrt(2  )  )      ->   IIRFilter:
							UpperCAmelCase :       Any 							=		tau * frequency / samplerate
							UpperCAmelCase :       Tuple 							=		sin(_lowerCAmelCase  )
							UpperCAmelCase :       Tuple 							=		cos(_lowerCAmelCase  )
							UpperCAmelCase :       Dict 							=		_sin / (2 * q_factor)

							UpperCAmelCase :       int 							=		(1 + _cos) / 2
							UpperCAmelCase :       List[Any] 							=		-1 - _cos

							UpperCAmelCase :       Tuple 							=		1 + alpha
							UpperCAmelCase :       List[str] 							=		-2 * _cos
							UpperCAmelCase :       Optional[Any] 							=		1 - alpha

							UpperCAmelCase :       Tuple 							=		IIRFilter(2  )
							filt.set_coefficients([aa, aa, aa]   ,      [ba, ba, ba]  )
							return filt


def  snake_case_     (					_lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						float = 1 / sqrt(2  )  )      ->   IIRFilter:
							UpperCAmelCase :       Optional[int] 							=		tau * frequency / samplerate
							UpperCAmelCase :       Optional[int] 							=		sin(_lowerCAmelCase  )
							UpperCAmelCase :       Tuple 							=		cos(_lowerCAmelCase  )
							UpperCAmelCase :       Optional[int] 							=		_sin / (2 * q_factor)

							UpperCAmelCase :       Union[str, Any] 							=		_sin / 2
							UpperCAmelCase :       Any 							=		0
							UpperCAmelCase :       int 							=		-ba

							UpperCAmelCase :       Optional[Any] 							=		1 + alpha
							UpperCAmelCase :       List[Any] 							=		-2 * _cos
							UpperCAmelCase :       Optional[Any] 							=		1 - alpha

							UpperCAmelCase :       int 							=		IIRFilter(2  )
							filt.set_coefficients([aa, aa, aa]   ,      [ba, ba, ba]  )
							return filt


def  snake_case_     (					_lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						float = 1 / sqrt(2  )  )      ->   IIRFilter:
							UpperCAmelCase :       List[str] 							=		tau * frequency / samplerate
							UpperCAmelCase :       Union[str, Any] 							=		sin(_lowerCAmelCase  )
							UpperCAmelCase :       str 							=		cos(_lowerCAmelCase  )
							UpperCAmelCase :       Optional[Any] 							=		_sin / (2 * q_factor)

							UpperCAmelCase :       List[str] 							=		1 - alpha
							UpperCAmelCase :       Any 							=		-2 * _cos
							UpperCAmelCase :       Optional[int] 							=		1 + alpha

							UpperCAmelCase :       Union[str, Any] 							=		IIRFilter(2  )
							filt.set_coefficients([ba, ba, ba]   ,      [ba, ba, ba]  )
							return filt


def  snake_case_     (					_lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						float   ,      _lowerCAmelCase  :						float = 1 / sqrt(2  )   ,      )      ->   IIRFilter:
							UpperCAmelCase :       Optional[Any] 							=		tau * frequency / samplerate
							UpperCAmelCase :       Union[str, Any] 							=		sin(_lowerCAmelCase  )
							UpperCAmelCase :       Optional[int] 							=		cos(_lowerCAmelCase  )
							UpperCAmelCase :       Dict 							=		_sin / (2 * q_factor)
							UpperCAmelCase :       str 							=		10 ** (gain_db / 40)

							UpperCAmelCase :       int 							=		1 + alpha * big_a
							UpperCAmelCase :       Union[str, Any] 							=		-2 * _cos
							UpperCAmelCase :       Optional[Any] 							=		1 - alpha * big_a
							UpperCAmelCase :       Union[str, Any] 							=		1 + alpha / big_a
							UpperCAmelCase :       Tuple 							=		-2 * _cos
							UpperCAmelCase :       Any 							=		1 - alpha / big_a

							UpperCAmelCase :       Optional[Any] 							=		IIRFilter(2  )
							filt.set_coefficients([aa, aa, aa]   ,      [ba, ba, ba]  )
							return filt


def  snake_case_     (					_lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						float   ,      _lowerCAmelCase  :						float = 1 / sqrt(2  )   ,      )      ->   IIRFilter:
							UpperCAmelCase :       Any 							=		tau * frequency / samplerate
							UpperCAmelCase :       Optional[int] 							=		sin(_lowerCAmelCase  )
							UpperCAmelCase :       Union[str, Any] 							=		cos(_lowerCAmelCase  )
							UpperCAmelCase :       str 							=		_sin / (2 * q_factor)
							UpperCAmelCase :       List[str] 							=		10 ** (gain_db / 40)
							UpperCAmelCase :       Optional[int] 							=		(big_a + 1) - (big_a - 1) * _cos
							UpperCAmelCase :       int 							=		(big_a + 1) + (big_a - 1) * _cos
							UpperCAmelCase :       int 							=		(big_a - 1) - (big_a + 1) * _cos
							UpperCAmelCase :       Optional[int] 							=		(big_a - 1) + (big_a + 1) * _cos
							UpperCAmelCase :       str 							=		2 * sqrt(_lowerCAmelCase  ) * alpha

							UpperCAmelCase :       Dict 							=		big_a * (pmc + aaa)
							UpperCAmelCase :       Any 							=		2 * big_a * mpc
							UpperCAmelCase :       Union[str, Any] 							=		big_a * (pmc - aaa)
							UpperCAmelCase :       Optional[int] 							=		ppmc + aaa
							UpperCAmelCase :       Optional[Any] 							=		-2 * pmpc
							UpperCAmelCase :       Optional[Any] 							=		ppmc - aaa

							UpperCAmelCase :       int 							=		IIRFilter(2  )
							filt.set_coefficients([aa, aa, aa]   ,      [ba, ba, ba]  )
							return filt




def  snake_case_     (					_lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						float   ,      _lowerCAmelCase  :						float = 1 / sqrt(2  )   ,      )      ->   IIRFilter:
							UpperCAmelCase :       int 							=		tau * frequency / samplerate
							UpperCAmelCase :       Union[str, Any] 							=		sin(_lowerCAmelCase  )
							UpperCAmelCase :       Union[str, Any] 							=		cos(_lowerCAmelCase  )
							UpperCAmelCase :       Any 							=		_sin / (2 * q_factor)
							UpperCAmelCase :       int 							=		10 ** (gain_db / 40)
							UpperCAmelCase :       List[str] 							=		(big_a + 1) - (big_a - 1) * _cos
							UpperCAmelCase :       Union[str, Any] 							=		(big_a + 1) + (big_a - 1) * _cos
							UpperCAmelCase :       Optional[Any] 							=		(big_a - 1) - (big_a + 1) * _cos
							UpperCAmelCase :       Union[str, Any] 							=		(big_a - 1) + (big_a + 1) * _cos
							UpperCAmelCase :       List[str] 							=		2 * sqrt(_lowerCAmelCase  ) * alpha

							UpperCAmelCase :       Any 							=		big_a * (ppmc + aaa)
							UpperCAmelCase :       str 							=		-2 * big_a * pmpc
							UpperCAmelCase :       List[Any] 							=		big_a * (ppmc - aaa)
							UpperCAmelCase :       Optional[Any] 							=		pmc + aaa
							UpperCAmelCase :       Any 							=		2 * mpc
							UpperCAmelCase :       str 							=		pmc - aaa

							UpperCAmelCase :       Union[str, Any] 							=		IIRFilter(2  )
							filt.set_coefficients([aa, aa, aa]   ,      [ba, ba, ba]  )
							return filt





