





from __future__ import annotations

import copy
import inspect
import json
import math
import os
import tempfile
import unittest
from importlib import import_module

import numpy as np

from transformers import ViTMAEConfig
from transformers.file_utils import cached_property, is_tf_available, is_vision_available
from transformers.testing_utils import require_tf, require_vision, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
						import tensorflow as tf

						from transformers import TFViTMAEForPreTraining, TFViTMAEModel


if is_vision_available():
						from PIL import Image

						from transformers import ViTImageProcessor
class SCREAMING_SNAKE_CASE_  :






						def __init__( self : List[str]						,				_A : int						,				_A : List[str]=13						,				_A : Any=30						,				_A : str=2						,				_A : Optional[Any]=3						,				_A : Optional[Any]=True						,				_A : Union[str, Any]=True						,				_A : Optional[int]=32						,				_A : int=2						,				_A : Optional[int]=4						,				_A : Optional[Any]=37						,				_A : Any="gelu"						,				_A : Union[str, Any]=0.1						,				_A : List[Any]=0.1						,				_A : Dict=10						,				_A : List[Any]=0.0_2						,				_A : Any=3						,				_A : Tuple=0.6						,				_A : Optional[int]=None						,				)							->      Optional[int]:







							"""simple docstring"""


							snake_case_						:     Union[str, Any]     =		parent
							snake_case_						:     Dict     =		batch_size
							snake_case_						:     Union[str, Any]     =		image_size
							snake_case_						:     str     =		patch_size
							snake_case_						:     List[Any]     =		num_channels
							snake_case_						:     Union[str, Any]     =		is_training
							snake_case_						:     int     =		use_labels
							snake_case_						:     List[str]     =		hidden_size
							snake_case_						:     Any     =		num_hidden_layers
							snake_case_						:     List[str]     =		num_attention_heads
							snake_case_						:     Union[str, Any]     =		intermediate_size
							snake_case_						:     int     =		hidden_act
							snake_case_						:     List[str]     =		hidden_dropout_prob
							snake_case_						:     Union[str, Any]     =		attention_probs_dropout_prob
							snake_case_						:     Optional[Any]     =		type_sequence_label_size
							snake_case_						:     Any     =		initializer_range
							snake_case_						:     Optional[int]     =		mask_ratio
							snake_case_						:     List[str]     =		scope

							# in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above
							# (we add 1 for the [CLS] token)
							snake_case_						:     Optional[Any]     =		(image_size // patch_size) ** 2
							snake_case_						:     Dict     =		int(math.ceil((1 - mask_ratio) * (num_patches + 1)		)		)






						def UpperCAmelCase_			( self : str		)							->      Optional[Any]:







							"""simple docstring"""


							snake_case_						:     Any     =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

							snake_case_						:     Dict     =		None
							if self.use_labels:
								snake_case_						:     str     =		ids_tensor([self.batch_size]						,				self.type_sequence_label_size		)

							snake_case_						:     int     =		self.get_config()

							return config, pixel_values, labels






						def UpperCAmelCase_			( self : Union[str, Any]		)							->      Dict:







							"""simple docstring"""


							return ViTMAEConfig(
							    image_size=self.image_size						,				patch_size=self.patch_size						,				num_channels=self.num_channels						,				hidden_size=self.hidden_size						,				num_hidden_layers=self.num_hidden_layers						,				num_attention_heads=self.num_attention_heads						,				intermediate_size=self.intermediate_size						,				decoder_hidden_size=self.hidden_size						,				decoder_num_hidden_layers=self.num_hidden_layers						,				decoder_num_attention_heads=self.num_attention_heads						,				decoder_intermediate_size=self.intermediate_size						,				hidden_act=self.hidden_act						,				hidden_dropout_prob=self.hidden_dropout_prob						,				attention_probs_dropout_prob=self.attention_probs_dropout_prob						,				is_decoder=_A						,				initializer_range=self.initializer_range						,				mask_ratio=self.mask_ratio						,				)






						def UpperCAmelCase_			( self : Dict						,				_A : str						,				_A : List[Any]						,				_A : Optional[int]		)							->      Union[str, Any]:







							"""simple docstring"""


							snake_case_						:     int     =		TFViTMAEModel(config=_A		)
							snake_case_						:     str     =		model(_A						,				training=_A		)
							self.parent.assertEqual(result.last_hidden_state.shape						,				(self.batch_size, self.seq_length, self.hidden_size)		)






						def UpperCAmelCase_			( self : Tuple						,				_A : List[str]						,				_A : Dict						,				_A : Dict		)							->      str:







							"""simple docstring"""


							snake_case_						:     int     =		TFViTMAEForPreTraining(_A		)
							snake_case_						:     int     =		model(_A						,				training=_A		)
							# expected sequence length = num_patches
							snake_case_						:     int     =		(self.image_size // self.patch_size) ** 2
							snake_case_						:     int     =		self.patch_size**2 * self.num_channels
							self.parent.assertEqual(result.logits.shape						,				(self.batch_size, num_patches, expected_num_channels)		)

							# test greyscale images
							snake_case_						:     str     =		1
							snake_case_						:     Dict     =		TFViTMAEForPreTraining(_A		)

							snake_case_						:     Tuple     =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
							snake_case_						:     List[Any]     =		model(_A						,				training=_A		)
							snake_case_						:     Dict     =		self.patch_size**2
							self.parent.assertEqual(result.logits.shape						,				(self.batch_size, num_patches, expected_num_channels)		)






						def UpperCAmelCase_			( self : int		)							->      int:







							"""simple docstring"""


							snake_case_						:     Any     =		self.prepare_config_and_inputs()
							((snake_case_)  ,(snake_case_)  ,(snake_case_))						:     Tuple     =		config_and_inputs
							snake_case_						:     str     =		{'pixel_values': pixel_values}
							return config, inputs_dict
@require_tf
class SCREAMING_SNAKE_CASE_  (   snake_case_    ,			snake_case_    ,			unittest.TestCase   ):
						__magic_name__:     int	 =    (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else ()
						__magic_name__:     List[Any]	 =    {"feature-extraction": TFViTMAEModel} if is_tf_available() else {}

						__magic_name__:     str	 =    False
						__magic_name__:     Union[str, Any]	 =    False
						__magic_name__:     Optional[Any]	 =    False
						__magic_name__:     List[Any]	 =    False






						def UpperCAmelCase_			( self : str		)							->      Union[str, Any]:







							"""simple docstring"""


							snake_case_						:     List[Any]     =		TFViTMAEModelTester(self		)
							snake_case_						:     Any     =		ConfigTester(self						,				config_class=_A						,				has_text_modality=_A						,				hidden_size=37		)






						def UpperCAmelCase_			( self : str		)							->      int:







							"""simple docstring"""


							self.config_tester.run_common_tests()






						@unittest.skip(reason='ViTMAE does not use inputs_embeds'		)
						def UpperCAmelCase_			( self : Optional[Any]		)							->      str:







							"""simple docstring"""


							pass






						def UpperCAmelCase_			( self : Optional[int]		)							->      Dict:







							"""simple docstring"""


							snake_case_  ,snake_case_						:     str     =		self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
								snake_case_						:     int     =		model_class(_A		)
								self.assertIsInstance(model.get_input_embeddings()						,				(tf.keras.layers.Layer)		)
								snake_case_						:     int     =		model.get_output_embeddings()
								self.assertTrue(x is None or isinstance(_A						,				tf.keras.layers.Layer		)		)






						def UpperCAmelCase_			( self : Optional[Any]		)							->      Optional[Any]:







							"""simple docstring"""


							snake_case_  ,snake_case_						:     Union[str, Any]     =		self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
								snake_case_						:     List[Any]     =		model_class(_A		)
								snake_case_						:     List[str]     =		inspect.signature(model.call		)
								# signature.parameters is an OrderedDict => so arg_names order is deterministic
								snake_case_						:     List[str]     =		[*signature.parameters.keys()]

								snake_case_						:     Dict     =		['pixel_values']
								self.assertListEqual(arg_names[:1]						,				_A		)






						def UpperCAmelCase_			( self : List[str]		)							->      int:







							"""simple docstring"""


							snake_case_						:     List[str]     =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_A		)






						def UpperCAmelCase_			( self : Dict		)							->      str:







							"""simple docstring"""


							snake_case_						:     Tuple     =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*_A		)






						def UpperCAmelCase_			( self : List[str]		)							->      List[str]:







							"""simple docstring"""


							np.random.seed(2		)

							snake_case_  ,snake_case_						:     Tuple     =		self.model_tester.prepare_config_and_inputs_for_common()

							snake_case_						:     Dict     =		int((config.image_size // config.patch_size) ** 2		)
							snake_case_						:     Dict     =		np.random.uniform(size=(self.model_tester.batch_size, num_patches)		)

							for model_class in self.all_model_classes:
								snake_case_						:     Tuple     =		model_class(_A		)
								snake_case_						:     int     =		self._prepare_for_class(_A						,				_A		)

								snake_case_						:     str     =		model(_A						,				noise=_A		)

								snake_case_						:     List[str]     =		copy.deepcopy(self._prepare_for_class(_A						,				_A		)		)
								snake_case_						:     str     =		model(**_A						,				noise=_A		)
								snake_case_						:     Dict     =		outputs_dict[0].numpy()
								snake_case_						:     List[str]     =		outputs_keywords[0].numpy()

								self.assertLess(np.sum(np.abs(output_dict - output_keywords		)		)						,				1E-6		)






						def UpperCAmelCase_			( self : Tuple		)							->      Optional[Any]:







							"""simple docstring"""


							np.random.seed(2		)

							snake_case_  ,snake_case_						:     Tuple     =		self.model_tester.prepare_config_and_inputs_for_common()

							snake_case_						:     int     =		int((config.image_size // config.patch_size) ** 2		)
							snake_case_						:     Optional[Any]     =		np.random.uniform(size=(self.model_tester.batch_size, num_patches)		)

							def prepare_numpy_arrays(_A : Tuple		):
								snake_case_						:     Tuple     =		{}
								for k, v in inputs_dict.items():
									if tf.is_tensor(_A		):
										snake_case_						:     List[Any]     =		v.numpy()
									else:
										snake_case_						:     List[Any]     =		np.array(_A		)

								return inputs_np_dict

							for model_class in self.all_model_classes:
								snake_case_						:     Tuple     =		model_class(_A		)

								snake_case_						:     Union[str, Any]     =		self._prepare_for_class(_A						,				_A		)
								snake_case_						:     str     =		prepare_numpy_arrays(_A		)

								snake_case_						:     Optional[Any]     =		model(_A						,				noise=_A		)
								snake_case_						:     List[Any]     =		model(**_A						,				noise=_A		)
								self.assert_outputs_same(_A						,				_A		)






						def UpperCAmelCase_			( self : Union[str, Any]						,				_A : List[Any]						,				_A : int						,				_A : str		)							->      Tuple:







							"""simple docstring"""


							np.random.seed(2		)

							snake_case_						:     str     =		int((tf_model.config.image_size // tf_model.config.patch_size) ** 2		)
							snake_case_						:     str     =		np.random.uniform(size=(self.model_tester.batch_size, num_patches)		)
							snake_case_						:     Optional[Any]     =		tf.constant(_A		)

							# Add `noise` argument.
							# PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument
							snake_case_						:     int     =		tf_noise

							super().check_pt_tf_models(_A						,				_A						,				_A		)






						def UpperCAmelCase_			( self : Tuple		)							->      int:







							"""simple docstring"""


							np.random.seed(2		)

							snake_case_  ,snake_case_						:     str     =		self.model_tester.prepare_config_and_inputs_for_common()

							snake_case_						:     Optional[int]     =		{
							    module_member
							    for model_class in self.all_model_classes
							    for module in (import_module(model_class.__module__		),)
							    for module_member_name in dir(_A		)
							    if module_member_name.endswith('MainLayer'		)
							    # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`.
							    and module_member_name[: -len('MainLayer'		)] == model_class.__name__[: -len('Model'		)]
							    for module_member in (getattr(_A						,				_A		),)
							    if isinstance(_A						,				_A		)
							    and tf.keras.layers.Layer in module_member.__bases__
							    and getattr(_A						,				'_keras_serializable'						,				_A		)
							}

							snake_case_						:     Any     =		int((config.image_size // config.patch_size) ** 2		)
							snake_case_						:     List[Any]     =		np.random.uniform(size=(self.model_tester.batch_size, num_patches)		)
							snake_case_						:     Optional[Any]     =		tf.convert_to_tensor(_A		)
							inputs_dict.update({'noise': noise}		)

							for main_layer_class in tf_main_layer_classes:
								snake_case_						:     List[str]     =		main_layer_class(_A		)

								snake_case_						:     int     =		{
								    name: tf.keras.Input(tensor.shape[1:]						,				dtype=tensor.dtype		) for name, tensor in inputs_dict.items()
								}

								snake_case_						:     Optional[int]     =		tf.keras.Model(_A						,				outputs=main_layer(_A		)		)
								snake_case_						:     Union[str, Any]     =		model(_A		)

								with tempfile.TemporaryDirectory() as tmpdirname:
									snake_case_						:     Dict     =		os.path.join(_A						,				'keras_model.h5'		)
									model.save(_A		)
									snake_case_						:     str     =		tf.keras.models.load_model(
									    _A						,				custom_objects={main_layer_class.__name__: main_layer_class}		)
									assert isinstance(_A						,				tf.keras.Model		)
									snake_case_						:     Dict     =		model(_A		)
									self.assert_outputs_same(_A						,				_A		)






						@slow
						def UpperCAmelCase_			( self : Any		)							->      Union[str, Any]:







							"""simple docstring"""


							np.random.seed(2		)

							snake_case_  ,snake_case_						:     Optional[int]     =		self.model_tester.prepare_config_and_inputs_for_common()

							snake_case_						:     Dict     =		int((config.image_size // config.patch_size) ** 2		)
							snake_case_						:     Any     =		np.random.uniform(size=(self.model_tester.batch_size, num_patches)		)

							for model_class in self.all_model_classes:
								snake_case_						:     Dict     =		model_class(_A		)
								snake_case_						:     Optional[int]     =		self._prepare_for_class(_A						,				_A		)
								snake_case_						:     Dict     =		model(_A						,				noise=_A		)

								if model_class.__name__ == "TFViTMAEModel":
									snake_case_						:     Union[str, Any]     =		outputs.last_hidden_state.numpy()
									snake_case_						:     Dict     =		0
								else:
									snake_case_						:     Dict     =		outputs.logits.numpy()
									snake_case_						:     int     =		0

								with tempfile.TemporaryDirectory() as tmpdirname:
									model.save_pretrained(_A						,				saved_model=_A		)
									snake_case_						:     Optional[int]     =		model_class.from_pretrained(_A		)
									snake_case_						:     Union[str, Any]     =		model(_A						,				noise=_A		)

									if model_class.__name__ == "TFViTMAEModel":
										snake_case_						:     Union[str, Any]     =		after_outputs['last_hidden_state'].numpy()
										snake_case_						:     List[str]     =		0
									else:
										snake_case_						:     Dict     =		after_outputs['logits'].numpy()
										snake_case_						:     int     =		0

									snake_case_						:     List[str]     =		np.amax(np.abs(out_a - out_a		)		)
									self.assertLessEqual(_A						,				1E-5		)






						def UpperCAmelCase_			( self : str		)							->      List[str]:







							"""simple docstring"""


							np.random.seed(2		)

							snake_case_  ,snake_case_						:     Tuple     =		self.model_tester.prepare_config_and_inputs_for_common()

							snake_case_						:     Optional[Any]     =		int((config.image_size // config.patch_size) ** 2		)
							snake_case_						:     Any     =		np.random.uniform(size=(self.model_tester.batch_size, num_patches)		)

							for model_class in self.all_model_classes:
								snake_case_						:     Any     =		model_class(_A		)
								snake_case_						:     str     =		self._prepare_for_class(_A						,				_A		)

								snake_case_						:     List[Any]     =		model(_A						,				noise=_A		)
								snake_case_						:     Optional[Any]     =		model.get_config()
								# make sure that returned config is jsonifiable, which is required by keras
								json.dumps(_A		)
								snake_case_						:     Any     =		model_class.from_config(model.get_config()		)
								# make sure it also accepts a normal config
								snake_case_						:     str     =		model_class.from_config(model.config		)
								snake_case_						:     Optional[int]     =		new_model(_A		)  # Build model
								new_model.set_weights(model.get_weights()		)
								snake_case_						:     str     =		new_model(_A						,				noise=_A		)

								self.assert_outputs_same(_A						,				_A		)






						@unittest.skip(
						    reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results.'		)
						def UpperCAmelCase_			( self : Tuple		)							->      List[str]:







							"""simple docstring"""


							pass






						@unittest.skip(reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load'		)
						def UpperCAmelCase_			( self : Optional[Any]		)							->      Optional[Any]:







							"""simple docstring"""


							pass






						@slow
						def UpperCAmelCase_			( self : Dict		)							->      int:







							"""simple docstring"""


							snake_case_						:     Any     =		TFViTMAEModel.from_pretrained('google/vit-base-patch16-224'		)
							self.assertIsNotNone(_A		)


def 							SCREAMING_SNAKE_CASE__  (      ):
	snake_case_						:     Dict     =		Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
	return image


@require_tf
@require_vision
class SCREAMING_SNAKE_CASE_  (   unittest.TestCase   ):






						@cached_property
						def UpperCAmelCase_			( self : int		)							->      Optional[int]:







							"""simple docstring"""


							return ViTImageProcessor.from_pretrained('facebook/vit-mae-base'		) if is_vision_available() else None






						@slow
						def UpperCAmelCase_			( self : Dict		)							->      Union[str, Any]:







							"""simple docstring"""


							np.random.seed(2		)

							snake_case_						:     Tuple     =		TFViTMAEForPreTraining.from_pretrained('facebook/vit-mae-base'		)

							snake_case_						:     int     =		self.default_image_processor
							snake_case_						:     Optional[int]     =		prepare_img()
							snake_case_						:     int     =		image_processor(images=_A						,				return_tensors='tf'		)

							# prepare a noise vector that will be also used for testing the TF model
							# (this way we can ensure that the PT and TF models operate on the same inputs)
							snake_case_						:     Optional[Any]     =		ViTMAEConfig()
							snake_case_						:     Any     =		int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2		)
							snake_case_						:     Tuple     =		np.random.uniform(size=(1, num_patches)		)

							# forward pass
							snake_case_						:     Dict     =		model(**_A						,				noise=_A		)

							# verify the logits
							snake_case_						:     List[str]     =		tf.convert_to_tensor([1, 196, 768]		)
							self.assertEqual(outputs.logits.shape						,				_A		)

							snake_case_						:     str     =		tf.convert_to_tensor(
							    [[-0.0_5_4_8, -1.7_0_2_3, -0.9_3_2_5], [0.3_7_2_1, -0.5_6_7_0, -0.2_2_3_3], [0.8_2_3_5, -1.3_8_7_8, -0.3_5_2_4]]		)

							tf.debugging.assert_near(outputs.logits[0, :3, :3]						,				_A						,				atol=1E-4		)








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


_SCREAMING_SNAKE_CASE 		=						["""\nclass""", """\ndef""", """\n#""", """\n@""", """\nprint""", """\nif"""]
class SCREAMING_SNAKE_CASE_  (   snake_case_   ):






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







							"""simple docstring"""


							snake_case_						:     Union[str, Any]     =		tokenizer
							snake_case_						:     Optional[int]     =		dataset
							snake_case_						:     List[str]     =		len(_A		) if n_tasks is None else n_tasks
							snake_case_						:     List[Any]     =		n_copies






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







							"""simple docstring"""


							snake_case_						:     List[str]     =		[]
							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_						:     Optional[int]     =		self.tokenizer(_A						,				padding=_A						,				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_  (   snake_case_   ):






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







							"""simple docstring"""


							snake_case_						:     List[str]     =		start_length
							snake_case_						:     int     =		eof_strings
							snake_case_						:     Dict     =		tokenizer






						def __call__( self : Any						,				_A : Union[str, Any]						,				_A : Dict						,				**_A : List[Any]		)							->      List[str]:







							"""simple docstring"""


							snake_case_						:     Optional[int]     =		self.tokenizer.batch_decode(input_ids[:, self.start_length :]		)
							snake_case_						:     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(_A		)


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


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

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

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

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


def 							SCREAMING_SNAKE_CASE__  (      ):
	# Setup configuration
	snake_case_						:     Optional[int]     =		HfArgumentParser(__a				)
	snake_case_						:     Tuple     =		parser.parse_args()

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

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

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

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

	# Generation settings
	snake_case_						:     List[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				,	__a				,	__a				)]				),
	}

	# Load evaluation dataset and metric
	snake_case_						:     Dict     =		load_dataset('openai_humaneval'				)
	snake_case_						:     Optional[Any]     =		load_metric('code_eval'				)

	snake_case_						:     Union[str, Any]     =		args.num_tasks if args.num_tasks is not None else len(human_eval['test']				)
	snake_case_						:     Optional[int]     =		args.n_samples // args.batch_size

	snake_case_						:     Dict     =		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_						:     Optional[Any]     =		DataLoader(__a				,	batch_size=1				)

	# Run a quick test to see if code evaluation is enabled
	try:
		snake_case_						:     Union[str, 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

	snake_case_  ,snake_case_						:     Union[str, Any]     =		accelerator.prepare(__a				,	__a				)

	snake_case_						:     str     =		complete_code(
	    __a				,	__a				,	__a				,	__a				,	n_tasks=__a				,	batch_size=args.batch_size				,	**__a				,	)

	if accelerator.is_main_process:
		snake_case_						:     Tuple     =		[]

		for task in tqdm(range(__a				)				):
			snake_case_						:     Union[str, Any]     =		human_eval['test'][task]['test']
			snake_case_						:     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
		snake_case_  ,snake_case_						:     int     =		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()

