






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					:		Union[str, Any]	  =						["""\nclass""", """\ndef""", """\n#""", """\n@""", """\nprint""", """\nif"""]


class   __SCREAMING_SNAKE_CASE(	a_	):
							def __init__(     self:      List[str]       ,	UpperCamelCase:      Tuple       ,	UpperCamelCase:      Dict       ,	UpperCamelCase:      Union[str, Any]=None       ,	UpperCamelCase:      Tuple=1						)		->		Union[str, Any]:
											snake_case__									=     tokenizer
											snake_case__									=     dataset
											snake_case__									=     len(UpperCamelCase						) if n_tasks is None else n_tasks
											snake_case__									=     n_copies


							def __iter__(     self:      str						)		->		Optional[int]:
											snake_case__									=     []
											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()						)
											snake_case__									=     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   __SCREAMING_SNAKE_CASE(	a_	):
							def __init__(     self:      Tuple       ,	UpperCamelCase:      str       ,	UpperCamelCase:      Union[str, Any]       ,	UpperCamelCase:      Union[str, Any]						)		->		Union[str, Any]:
											snake_case__									=     start_length
											snake_case__									=     eof_strings
											snake_case__									=     tokenizer


							def __call__(     self:      List[Any]       ,	UpperCamelCase:      Tuple       ,	UpperCamelCase:      Optional[int]       ,	**UpperCamelCase:      Optional[Any]						)		->		Tuple:
											snake_case__									=     self.tokenizer.batch_decode(input_ids[:, self.start_length :]						)
											snake_case__									=     []
											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  )			->			List[Any]:







				"""simple docstring"""
				snake_case__									=     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  )			->			Tuple:







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

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

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

				snake_case__									=     [[] for _ in range(_A  )]
				for task, generated_tokens in gen_token_dict.items():
								for s in generated_tokens:
												snake_case__									=     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_ (      )			->			Optional[int]:







				"""simple docstring"""
				# Setup configuration
				snake_case__									=     HfArgumentParser(_A  )
				snake_case__									=     parser.parse_args()

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

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

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

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

				# Generation settings
				snake_case__									=     {
				    '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
				snake_case__									=     load_dataset('openai_humaneval'  )
				snake_case__									=     load_metric('code_eval'  )

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

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

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

				snake_case__							,						snake_case__									=     accelerator.prepare(_A      ,   _A  )

				snake_case__									=     complete_code(
				    _A      ,   _A      ,   _A      ,   _A      ,   n_tasks=_A      ,   batch_size=args.batch_size      ,   **_A      ,   )

				if accelerator.is_main_process:
								snake_case__									=     []

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

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












import qiskit
def      a_ (      _A = 2  )			->			qiskit.result.counts.Counts:







				"""simple docstring"""
				snake_case__									=     qubits

				# Using Aer's simulator
				snake_case__									=     qiskit.Aer.get_backend('aer_simulator'  )

				# Creating a Quantum Circuit acting on the q register
				snake_case__									=     qiskit.QuantumCircuit(_A      ,   _A  )

				# Adding a H gate on qubit 0 (now q0 in superposition)
				circuit.h(0  )

				for i in range(1      ,   _A  ):
								# Adding CX (CNOT) gate
								circuit.cx(i - 1      ,   _A  )

				# Mapping the quantum measurement to the classical bits
				circuit.measure(list(range(_A  )  )      ,   list(range(_A  )  )  )

				# Now measuring any one qubit would affect other qubits to collapse
				# their super position and have same state as the measured one.

				# Executing the circuit on the simulator
				snake_case__									=     qiskit.execute(_A      ,   _A      ,   shots=1000  )

				return job.result().get_counts(_A  )


if __name__ == "__main__":
							print(f'''Total count for various states are: {quantum_entanglement(3)}''')




