


'''simple docstring'''


import warnings
from typing import List, Optional, Tuple, Union

import numpy as np
import PIL
import torch

from ...models import UNetaDModel
from ...schedulers import RePaintScheduler
from ...utils import PIL_INTERPOLATION, logging, randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput


lowerCamelCase__    	=     logging.get_logger(__name__)  # pylint: disable=invalid-name






def  __lowerCAmelCase (__lowerCAmelCase   ):
		warnings.warn(
		    "The preprocess method is deprecated and will be removed in a future version. Please"
		    " use VaeImageProcessor.preprocess instead"      , __lowerCAmelCase      , )
		if isinstance(__lowerCAmelCase      , torch.Tensor   ):
				return image
		elif isinstance(__lowerCAmelCase      , PIL.Image.Image   ):
				_UpperCAmelCase		:       Union[str, Any]								=      [image]

		if isinstance(image[0]      , PIL.Image.Image   ):
				_UpperCAmelCase		:       List[Any]								=      image[0].size
				_UpperCAmelCase		:       str								=      (x - x % 8 for x in (w, h))  # resize to integer multiple of 8

				_UpperCAmelCase		:       int								=      [np.array(i.resize((w, h)      , resample=PIL_INTERPOLATION["lanczos"]   )   )[None, :] for i in image]
				_UpperCAmelCase		:       List[Any]								=      np.concatenate(__lowerCAmelCase      , axis=0   )
				_UpperCAmelCase		:       str								=      np.array(__lowerCAmelCase   ).astype(np.floataa   ) / 255.0
				_UpperCAmelCase		:       Optional[Any]								=      image.transpose(0      , 3      , 1      , 2   )
				_UpperCAmelCase		:       Optional[int]								=      2.0 * image - 1.0
				_UpperCAmelCase		:       Union[str, Any]								=      torch.from_numpy(__lowerCAmelCase   )
		elif isinstance(image[0]      , torch.Tensor   ):
				_UpperCAmelCase		:       Union[str, Any]								=      torch.cat(__lowerCAmelCase      , dim=0   )
		return image






def  __lowerCAmelCase (__lowerCAmelCase   ):
		if isinstance(__lowerCAmelCase      , torch.Tensor   ):
				return mask
		elif isinstance(__lowerCAmelCase      , PIL.Image.Image   ):
				_UpperCAmelCase		:       Optional[int]								=      [mask]

		if isinstance(mask[0]      , PIL.Image.Image   ):
				_UpperCAmelCase		:       str								=      mask[0].size
				_UpperCAmelCase		:       str								=      (x - x % 32 for x in (w, h))  # resize to integer multiple of 32
				_UpperCAmelCase		:       Tuple								=      [np.array(m.convert("L"   ).resize((w, h)      , resample=PIL_INTERPOLATION["nearest"]   )   )[None, :] for m in mask]
				_UpperCAmelCase		:       int								=      np.concatenate(__lowerCAmelCase      , axis=0   )
				_UpperCAmelCase		:       Optional[int]								=      mask.astype(np.floataa   ) / 255.0
				_UpperCAmelCase		:       Union[str, Any]								=      0
				_UpperCAmelCase		:       List[Any]								=      1
				_UpperCAmelCase		:       List[Any]								=      torch.from_numpy(__lowerCAmelCase   )
		elif isinstance(mask[0]      , torch.Tensor   ):
				_UpperCAmelCase		:       Tuple								=      torch.cat(__lowerCAmelCase      , dim=0   )
		return mask






class 	lowerCAmelCase__			(			UpperCAmelCase__	):
	lowerCAmelCase :	UNetaDModel
	lowerCAmelCase :	RePaintScheduler




	def __init__(		self  :     List[str]	,  lowerCamelCase__  :     int	,  lowerCamelCase__  :     List[Any] )   ->Optional[int]:



			'''simple docstring'''







			super().__init__()
			self.register_modules(unet=lowerCamelCase__	,  scheduler=lowerCamelCase__ )






	@torch.no_grad()
	def __call__(		self  :     Union[str, Any]	,  lowerCamelCase__  :     Union[torch.Tensor, PIL.Image.Image]	,  lowerCamelCase__  :     Union[torch.Tensor, PIL.Image.Image]	,  lowerCamelCase__  :     int = 2_50	,  lowerCamelCase__  :     float = 0.0	,  lowerCamelCase__  :     int = 10	,  lowerCamelCase__  :     int = 10	,  lowerCamelCase__  :     Optional[Union[torch.Generator, List[torch.Generator]]] = None	,  lowerCamelCase__  :     Optional[str] = "pil"	,  lowerCamelCase__  :     bool = True	,  )   ->Union[ImagePipelineOutput, Tuple]:



			'''simple docstring'''







			_UpperCAmelCase		:       Optional[Any]								=      image

			_UpperCAmelCase		:       Tuple								=      _preprocess_image(lowerCamelCase__ )
			_UpperCAmelCase		:       List[Any]								=      original_image.to(device=self.device	,  dtype=self.unet.dtype )
			_UpperCAmelCase		:       int								=      _preprocess_mask(lowerCamelCase__ )
			_UpperCAmelCase		:       List[Any]								=      mask_image.to(device=self.device	,  dtype=self.unet.dtype )

			_UpperCAmelCase		:       Union[str, Any]								=      original_image.shape[0]

			# sample gaussian noise to begin the loop
			if isinstance(lowerCamelCase__	,  lowerCamelCase__ ) and len(lowerCamelCase__ ) != batch_size:
					raise ValueError(
					    F"""You have passed a list of generators of length {len(lowerCamelCase__ )}, but requested an effective batch"""
					    F""" size of {batch_size}. Make sure the batch size matches the length of the generators.""" )

			_UpperCAmelCase		:       int								=      original_image.shape
			_UpperCAmelCase		:       List[str]								=      randn_tensor(lowerCamelCase__	,  generator=lowerCamelCase__	,  device=self.device	,  dtype=self.unet.dtype )

			# set step values
			self.scheduler.set_timesteps(lowerCamelCase__	,  lowerCamelCase__	,  lowerCamelCase__	,  self.device )
			_UpperCAmelCase		:       str								=      eta

			_UpperCAmelCase		:       Optional[int]								=      self.scheduler.timesteps[0] + 1
			_UpperCAmelCase		:       Optional[int]								=      generator[0] if isinstance(lowerCamelCase__	,  lowerCamelCase__ ) else generator
			for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ):
					if t < t_last:
							# predict the noise residual
							_UpperCAmelCase		:       Any								=      self.unet(lowerCamelCase__	,  lowerCamelCase__ ).sample
							# compute previous image: x_t -> x_t-1
							_UpperCAmelCase		:       Optional[int]								=      self.scheduler.step(lowerCamelCase__	,  lowerCamelCase__	,  lowerCamelCase__	,  lowerCamelCase__	,  lowerCamelCase__	,  lowerCamelCase__ ).prev_sample

					else:
							# compute the reverse: x_t-1 -> x_t
							_UpperCAmelCase		:       Union[str, Any]								=      self.scheduler.undo_step(lowerCamelCase__	,  lowerCamelCase__	,  lowerCamelCase__ )
					_UpperCAmelCase		:       Tuple								=      t

			_UpperCAmelCase		:       Optional[Any]								=      (image / 2 + 0.5).clamp(0	,  1 )
			_UpperCAmelCase		:       List[Any]								=      image.cpu().permute(0	,  2	,  3	,  1 ).numpy()
			if output_type == "pil":
					_UpperCAmelCase		:       Union[str, Any]								=      self.numpy_to_pil(lowerCamelCase__ )

			if not return_dict:
					return (image,)

			return ImagePipelineOutput(images=lowerCamelCase__ )







'''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


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






class 	lowerCAmelCase__			(			UpperCAmelCase__	):




			def __init__(		self  :     Tuple	,  lowerCamelCase__  :     List[Any]	,  lowerCamelCase__  :     Optional[int]	,  lowerCamelCase__  :     Optional[Any]=None	,  lowerCamelCase__  :     int=1 )   ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:       List[Any]								=      tokenizer
					_UpperCAmelCase		:       Tuple								=      dataset
					_UpperCAmelCase		:       Union[str, Any]								=      len(lowerCamelCase__ ) if n_tasks is None else n_tasks
					_UpperCAmelCase		:       Any								=      n_copies






			def __iter__(		self  :     Any )   ->Dict:



					'''simple docstring'''







					_UpperCAmelCase		:       Optional[int]								=      []
					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		:       Optional[Any]								=      self.tokenizer(lowerCamelCase__	,  padding=lowerCamelCase__	,  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 	lowerCAmelCase__			(			UpperCAmelCase__	):




			def __init__(		self  :     Optional[int]	,  lowerCamelCase__  :     Tuple	,  lowerCamelCase__  :     Any	,  lowerCamelCase__  :     Dict )   ->Any:



					'''simple docstring'''







					_UpperCAmelCase		:       List[str]								=      start_length
					_UpperCAmelCase		:       Union[str, Any]								=      eof_strings
					_UpperCAmelCase		:       Union[str, Any]								=      tokenizer






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



					'''simple docstring'''







					_UpperCAmelCase		:       Tuple								=      self.tokenizer.batch_decode(input_ids[:, self.start_length :] )
					_UpperCAmelCase		:       Optional[int]								=      []
					for decoded_generation in decoded_generations:
							done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) )
					return all(lowerCamelCase__ )






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






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

						_UpperCAmelCase					,    _UpperCAmelCase		:       int								=      accelerator.gather((generated_tokens, generated_tasks)   )
						_UpperCAmelCase		:       Dict								=      generated_tokens.cpu().numpy()
						_UpperCAmelCase		:       Dict								=      generated_tasks.cpu().numpy()

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

		_UpperCAmelCase		:       int								=      [[] for _ in range(__lowerCAmelCase   )]
		for task, generated_tokens in gen_token_dict.items():
				for s in generated_tokens:
						_UpperCAmelCase		:       List[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 ():
		# Setup configuration
		_UpperCAmelCase		:       List[str]								=      HfArgumentParser(__lowerCAmelCase   )
		_UpperCAmelCase		:       Tuple								=      parser.parse_args()

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

		if args.num_workers is None:
				_UpperCAmelCase		:       List[str]								=      multiprocessing.cpu_count()

		# Use dataset load to feed to accelerate
		_UpperCAmelCase		:       List[Any]								=      Accelerator()
		set_seed(args.seed      , device_specific=__lowerCAmelCase   )

		# Load model and tokenizer
		_UpperCAmelCase		:       Tuple								=      AutoTokenizer.from_pretrained(args.model_ckpt   )
		_UpperCAmelCase		:       List[str]								=      tokenizer.eos_token
		_UpperCAmelCase		:       str								=      AutoModelForCausalLM.from_pretrained(args.model_ckpt   )

		# Generation settings
		_UpperCAmelCase		:       Tuple								=      {
		    "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		:       Union[str, Any]								=      load_dataset("openai_humaneval"   )
		_UpperCAmelCase		:       List[Any]								=      load_metric("code_eval"   )

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

		_UpperCAmelCase		:       Tuple								=      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		:       List[str]								=      DataLoader(__lowerCAmelCase      , batch_size=1   )

		# Run a quick test to see if code evaluation is enabled
		try:
				_UpperCAmelCase		:       Optional[int]								=      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		:       Any								=      accelerator.prepare(__lowerCAmelCase      , __lowerCAmelCase   )

		_UpperCAmelCase		:       Dict								=      complete_code(
		    __lowerCAmelCase      , __lowerCAmelCase      , __lowerCAmelCase      , __lowerCAmelCase      , n_tasks=__lowerCAmelCase      , batch_size=args.batch_size      , **__lowerCAmelCase      , )

		if accelerator.is_main_process:
				_UpperCAmelCase		:       List[Any]								=      []

				for task in tqdm(range(__lowerCAmelCase   )   ):
						_UpperCAmelCase		:       str								=      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		:       str								=      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()



