




'''simple docstring'''

import datetime
import platform
import subprocess
from typing import Optional, Tuple, Union

import numpy as np


def 		__magic_name__    (	__UpperCAmelCase,      __UpperCAmelCase	)		->					np.array:

					'''simple docstring'''

					snake_case_									=				F"{sampling_rate}"
					snake_case_									=				'''1'''
					snake_case_									=				'''f32le'''
					snake_case_									=				[
					    '''ffmpeg''',
					    '''-i''',
					    '''pipe:0''',
					    '''-ac''',
					    ac,
					    '''-ar''',
					    ar,
					    '''-f''',
					    format_for_conversion,
					    '''-hide_banner''',
					    '''-loglevel''',
					    '''quiet''',
					    '''pipe:1''',
					]

					try:
										with subprocess.Popen(__UpperCAmelCase,      stdin=subprocess.PIPE,      stdout=subprocess.PIPE	) as ffmpeg_process:
															snake_case_									=				ffmpeg_process.communicate(__UpperCAmelCase	)
					except FileNotFoundError as error:
										raise ValueError('''ffmpeg was not found but is required to load audio files from filename'''	) from error
					snake_case_									=				output_stream[0]
					snake_case_									=				np.frombuffer(__UpperCAmelCase,      np.floataa	)
					if audio.shape[0] == 0:
										raise ValueError('''Malformed soundfile'''	)
					return audio


def 		__magic_name__    (	__UpperCAmelCase,      __UpperCAmelCase,      __UpperCAmelCase = "f32le",      )		->					List[Any]:

					'''simple docstring'''

					snake_case_									=				F"{sampling_rate}"
					snake_case_									=				'''1'''
					if format_for_conversion == "s16le":
										snake_case_									=				2
					elif format_for_conversion == "f32le":
										snake_case_									=				4
					else:
										raise ValueError(F"Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`"	)

					snake_case_									=				platform.system()
					if system == "Linux":
										snake_case_									=				'''alsa'''
										snake_case_									=				'''default'''
					elif system == "Darwin":
										snake_case_									=				'''avfoundation'''
										snake_case_									=				''':0'''
					elif system == "Windows":
										snake_case_									=				'''dshow'''
										snake_case_									=				'''default'''

					snake_case_									=				[
					    '''ffmpeg''',
					    '''-f''',
					    format_,
					    '''-i''',
					    input_,
					    '''-ac''',
					    ac,
					    '''-ar''',
					    ar,
					    '''-f''',
					    format_for_conversion,
					    '''-fflags''',
					    '''nobuffer''',
					    '''-hide_banner''',
					    '''-loglevel''',
					    '''quiet''',
					    '''pipe:1''',
					]
					snake_case_									=				int(round(sampling_rate * chunk_length_s	)	) * size_of_sample
					snake_case_									=				_ffmpeg_stream(__UpperCAmelCase,      __UpperCAmelCase	)
					for item in iterator:
										yield item


def 		__magic_name__    (	__UpperCAmelCase,      __UpperCAmelCase,      __UpperCAmelCase = None,      __UpperCAmelCase = None,      __UpperCAmelCase = "f32le",      )		->					Dict:

					'''simple docstring'''

					if stream_chunk_s is not None:
										snake_case_									=				stream_chunk_s
					else:
										snake_case_									=				chunk_length_s

					snake_case_									=				ffmpeg_microphone(__UpperCAmelCase,      __UpperCAmelCase,      format_for_conversion=__UpperCAmelCase	)
					if format_for_conversion == "s16le":
										snake_case_									=				np.intaa
										snake_case_									=				2
					elif format_for_conversion == "f32le":
										snake_case_									=				np.floataa
										snake_case_									=				4
					else:
										raise ValueError(F"Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`"	)

					if stride_length_s is None:
										snake_case_									=				chunk_length_s / 6
					snake_case_									=				int(round(sampling_rate * chunk_length_s	)	) * size_of_sample
					if isinstance(__UpperCAmelCase,      (int, float)	):
										snake_case_									=				[stride_length_s, stride_length_s]

					snake_case_									=				int(round(sampling_rate * stride_length_s[0]	)	) * size_of_sample
					snake_case_									=				int(round(sampling_rate * stride_length_s[1]	)	) * size_of_sample
					snake_case_									=				datetime.datetime.now()
					snake_case_									=				datetime.timedelta(seconds=__UpperCAmelCase	)
					for item in chunk_bytes_iter(__UpperCAmelCase,      __UpperCAmelCase,      stride=(stride_left, stride_right),      stream=__UpperCAmelCase	):
										# Put everything back in numpy scale
										snake_case_									=				np.frombuffer(item['''raw'''],      dtype=__UpperCAmelCase	)
										snake_case_									=				(
										    item['''stride'''][0] // size_of_sample,
										    item['''stride'''][1] // size_of_sample,
										)
										snake_case_									=				sampling_rate
										audio_time += delta
										if datetime.datetime.now() > audio_time + 10 * delta:
															# We're late !! SKIP
															continue
										yield item


def 		__magic_name__    (	__UpperCAmelCase,      __UpperCAmelCase,      __UpperCAmelCase,      __UpperCAmelCase = False	)		->					Any:

					'''simple docstring'''

					snake_case_									=				b''''''
					snake_case_			,snake_case_									=				stride
					if stride_left + stride_right >= chunk_len:
										raise ValueError(
										    F"Stride needs to be strictly smaller than chunk_len: ({stride_left}, {stride_right}) vs {chunk_len}"	)
					snake_case_									=				0
					for raw in iterator:
										acc += raw
										if stream and len(__UpperCAmelCase	) < chunk_len:
															snake_case_									=				(_stride_left, 0)
															yield {"raw": acc[:chunk_len], "stride": stride, "partial": True}
										else:
															while len(__UpperCAmelCase	) >= chunk_len:
																				# We are flushing the accumulator
																				snake_case_									=				(_stride_left, stride_right)
																				snake_case_									=				{'''raw''': acc[:chunk_len], '''stride''': stride}
																				if stream:
																									snake_case_									=				False
																				yield item
																				snake_case_									=				stride_left
																				snake_case_									=				acc[chunk_len - stride_left - stride_right :]
    # Last chunk
					if len(__UpperCAmelCase	) > stride_left:
										snake_case_									=				{'''raw''': acc, '''stride''': (_stride_left, 0)}
										if stream:
															snake_case_									=				False
										yield item


def 		__magic_name__    (	__UpperCAmelCase,      __UpperCAmelCase	)		->					Optional[int]:

					'''simple docstring'''

					snake_case_									=				2**24  # 16Mo
					try:
										with subprocess.Popen(__UpperCAmelCase,      stdout=subprocess.PIPE,      bufsize=__UpperCAmelCase	) as ffmpeg_process:
															while True:
																				snake_case_									=				ffmpeg_process.stdout.read(__UpperCAmelCase	)
																				if raw == b"":
																									break
																				yield raw
					except FileNotFoundError as error:
										raise ValueError('''ffmpeg was not found but is required to stream audio files from filename'''	) from error



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


__lowerCAmelCase			:     Optional[int]           =   ['\nclass', '\ndef', '\n#', '\n@', '\nprint', '\nif']




class        UpperCAmelCase_			( _A     ):


  '''simple docstring'''







  def __init__( self			: Union[str, Any]			,							UpperCamelCase__			: List[Any]			,							UpperCamelCase__			: Optional[int]			,							UpperCamelCase__			: Any=None			,							UpperCamelCase__			: Union[str, Any]=1    )			->	str:







    """simple docstring"""






    __magic_name__				     =       tokenizer
    __magic_name__				     =       dataset
    __magic_name__				     =       len(UpperCamelCase__    ) if n_tasks is None else n_tasks
    __magic_name__				     =       n_copies





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







    """simple docstring"""






    __magic_name__				     =       []
    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()    )
    __magic_name__				     =       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        UpperCAmelCase_			( _A     ):


  '''simple docstring'''







  def __init__( self			: Union[str, Any]			,							UpperCamelCase__			: Dict			,							UpperCamelCase__			: str			,							UpperCamelCase__			: str    )			->	List[str]:







    """simple docstring"""






    __magic_name__				     =       start_length
    __magic_name__				     =       eof_strings
    __magic_name__				     =       tokenizer





  def __call__( self			: List[Any]			,							UpperCamelCase__			: Tuple			,							UpperCamelCase__			: Optional[Any]			,							**UpperCamelCase__			: Optional[int]    )			->	List[str]:







    """simple docstring"""






    __magic_name__				     =       self.tokenizer.batch_decode(input_ids[:, self.start_length :]    )
    __magic_name__				     =       []
    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 a__    (       A_					):






  '''simple docstring'''





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

def a__    (       A_,     A_,     A_,     A_,     A_,     A_=20,     **A_					):






  '''simple docstring'''





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

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

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

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

def a__    (       ):






  '''simple docstring'''





  __magic_name__				     =       HfArgumentParser(A_					)
  __magic_name__				     =       parser.parse_args()

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

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

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

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

  # Generation settings
  __magic_name__				     =       {
      """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,     A_,     A_					)]					),
  }

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

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

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

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

  __magic_name__     ,    __magic_name__				     =       accelerator.prepare(A_,     A_					)

  __magic_name__				     =       complete_code(
      A_,     A_,     A_,     A_,     n_tasks=A_,     batch_size=args.batch_size,     **A_,     )

  if accelerator.is_main_process:
    __magic_name__				     =       []

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

    # Evaluate completions with "code_eval" metric
    __magic_name__     ,    __magic_name__				     =       code_eval_metric.compute(
        references=A_,     predictions=A_,     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(A_,     A_					)


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

