




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




class 					snake_case__							(_UpperCamelCase     ):
    """simple docstring"""



    def __init__(   self       :  Any      ,   __lowerCamelCase       :  List[str]      ,   __lowerCamelCase       :  Union[str, Any]      ,   __lowerCamelCase       :  Union[str, Any]=None      ,   __lowerCamelCase       :  Any=1							)      ->  Union[str, Any]:
       a					=    tokenizer
       a					=    dataset
       a					=    len(__lowerCamelCase							) if n_tasks is None else n_tasks
       a					=    n_copies





    def __iter__(   self       :  Tuple							)      ->  str:
       a					=    []
       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()							)
       a					=    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 					snake_case__							(_UpperCamelCase     ):
    """simple docstring"""



    def __init__(   self       :  int      ,   __lowerCamelCase       :  Dict      ,   __lowerCamelCase       :  Dict      ,   __lowerCamelCase       :  Tuple							)      ->  Optional[Any]:
       a					=    start_length
       a					=    eof_strings
       a					=    tokenizer





    def __call__(   self       :  int      ,   __lowerCamelCase       :  Dict      ,   __lowerCamelCase       :  List[str]      ,   **__lowerCamelCase       :  Optional[int]							)      ->  Optional[Any]:
       a					=    self.tokenizer.batch_decode(input_ids[:, self.start_length :]							)
       a					=    []
       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     __magic_name__       (				A	:      List[Any]     ):






   '''simple docstring'''







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




def     __magic_name__       (				A	:      Union[str, Any],						A	:      Optional[Any],						A	:      List[Any],						A	:      Optional[Any],						A	:      List[str],						A	:      List[Any]=20,						**A	:      Union[str, Any]     ):






   '''simple docstring'''







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

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

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

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




def     __magic_name__       (				):






   '''simple docstring'''







   a					=    HfArgumentParser(A     )
   a					=    parser.parse_args()

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

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

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

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

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

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

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

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

   a					,	a					=    accelerator.prepare(A,						A     )

   a					=    complete_code(
       A,						A,						A,						A,						n_tasks=A,						batch_size=args.batch_size,						**A,						)

   if accelerator.is_main_process:
      a					=    []

      for task in tqdm(range(A     )     ):
         a					=    human_eval["test"][task]["test"]
         a					=    F"""check({human_eval["test"][task]["entry_point"]})"""
         references.append("\n" + test_func + "\n" + entry_point     )

      # Evaluate completions with "code_eval" metric
      a					,	a					=    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 json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available


if is_vision_available():
		from PIL import Image

		from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor




@require_vision
class 					snake_case__							(unittest.TestCase     ):
				"""simple docstring"""



				def 							__UpperCAmelCase							(   self       :  int							)      ->  Dict:
							a					=    tempfile.mkdtemp()

							a					=    [
							    "[UNK]",
							    "[CLS]",
							    "[SEP]",
							    "[PAD]",
							    "[MASK]",
							    "的",
							    "价",
							    "格",
							    "是",
							    "15",
							    "便",
							    "alex",
							    "##andra",
							    "，",
							    "。",
							    "-",
							    "t",
							    "shirt",
							]
							a					=    os.path.join(self.tmpdirname      ,   VOCAB_FILES_NAMES["vocab_file"]							)
							with open(self.vocab_file      ,   "w"      ,   encoding="utf-8"							) as vocab_writer:
										vocab_writer.write("".join([x + "\n" for x in vocab_tokens]							)							)

							a					=    {
							    "do_resize": True,
							    "size": {"height": 2_24, "width": 2_24},
							    "do_center_crop": True,
							    "crop_size": {"height": 18, "width": 18},
							    "do_normalize": True,
							    "image_mean": [0.48_145_466, 0.4_578_275, 0.40_821_073],
							    "image_std": [0.26_862_954, 0.26_130_258, 0.27_577_711],
							    "do_convert_rgb": True,
							}
							a					=    os.path.join(self.tmpdirname      ,   __lowerCamelCase							)
							with open(self.image_processor_file      ,   "w"      ,   encoding="utf-8"							) as fp:
										json.dump(__lowerCamelCase      ,   __lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  Dict      ,   **__lowerCamelCase       :  Union[str, Any]							)      ->  List[Any]:
							return BertTokenizer.from_pretrained(self.tmpdirname      ,   **__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  str      ,   **__lowerCamelCase       :  Optional[int]							)      ->  str:
							return BertTokenizerFast.from_pretrained(self.tmpdirname      ,   **__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  List[str]      ,   **__lowerCamelCase       :  Optional[int]							)      ->  Tuple:
							return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname      ,   **__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  Optional[int]							)      ->  Optional[Any]:
							shutil.rmtree(self.tmpdirname							)





				def 							__UpperCAmelCase							(   self       :  List[str]							)      ->  Optional[int]:
							a					=    [np.random.randint(2_55      ,   size=(3, 30, 4_00)      ,   dtype=np.uinta							)]

							a					=    [Image.fromarray(np.moveaxis(__lowerCamelCase      ,   0      ,   -1							)							) for x in image_inputs]

							return image_inputs





				def 							__UpperCAmelCase							(   self       :  int							)      ->  List[str]:
							a					=    self.get_tokenizer()
							a					=    self.get_rust_tokenizer()
							a					=    self.get_image_processor()

							a					=    ChineseCLIPProcessor(tokenizer=__lowerCamelCase      ,   image_processor=__lowerCamelCase							)
							processor_slow.save_pretrained(self.tmpdirname							)
							a					=    ChineseCLIPProcessor.from_pretrained(self.tmpdirname      ,   use_fast=__lowerCamelCase							)

							a					=    ChineseCLIPProcessor(tokenizer=__lowerCamelCase      ,   image_processor=__lowerCamelCase							)
							processor_fast.save_pretrained(self.tmpdirname							)
							a					=    ChineseCLIPProcessor.from_pretrained(self.tmpdirname							)

							self.assertEqual(processor_slow.tokenizer.get_vocab()      ,   tokenizer_slow.get_vocab()							)
							self.assertEqual(processor_fast.tokenizer.get_vocab()      ,   tokenizer_fast.get_vocab()							)
							self.assertEqual(tokenizer_slow.get_vocab()      ,   tokenizer_fast.get_vocab()							)
							self.assertIsInstance(processor_slow.tokenizer      ,   __lowerCamelCase							)
							self.assertIsInstance(processor_fast.tokenizer      ,   __lowerCamelCase							)

							self.assertEqual(processor_slow.image_processor.to_json_string()      ,   image_processor.to_json_string()							)
							self.assertEqual(processor_fast.image_processor.to_json_string()      ,   image_processor.to_json_string()							)
							self.assertIsInstance(processor_slow.image_processor      ,   __lowerCamelCase							)
							self.assertIsInstance(processor_fast.image_processor      ,   __lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  Optional[int]							)      ->  List[Any]:
							a					=    ChineseCLIPProcessor(tokenizer=self.get_tokenizer()      ,   image_processor=self.get_image_processor()							)
							processor.save_pretrained(self.tmpdirname							)

							a					=    self.get_tokenizer(cls_token="(CLS)"      ,   sep_token="(SEP)"							)
							a					=    self.get_image_processor(do_normalize=__lowerCamelCase							)

							a					=    ChineseCLIPProcessor.from_pretrained(
							    self.tmpdirname      ,   cls_token="(CLS)"      ,   sep_token="(SEP)"      ,   do_normalize=__lowerCamelCase							)

							self.assertEqual(processor.tokenizer.get_vocab()      ,   tokenizer_add_kwargs.get_vocab()							)
							self.assertIsInstance(processor.tokenizer      ,   __lowerCamelCase							)

							self.assertEqual(processor.image_processor.to_json_string()      ,   image_processor_add_kwargs.to_json_string()							)
							self.assertIsInstance(processor.image_processor      ,   __lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  Tuple							)      ->  Dict:
							a					=    self.get_image_processor()
							a					=    self.get_tokenizer()

							a					=    ChineseCLIPProcessor(tokenizer=__lowerCamelCase      ,   image_processor=__lowerCamelCase							)

							a					=    self.prepare_image_inputs()

							a					=    image_processor(__lowerCamelCase      ,   return_tensors="np"							)
							a					=    processor(images=__lowerCamelCase      ,   return_tensors="np"							)

							for key in input_feat_extract.keys():
										self.assertAlmostEqual(input_feat_extract[key].sum()      ,   input_processor[key].sum()      ,   delta=1e-2							)





				def 							__UpperCAmelCase							(   self       :  str							)      ->  Optional[int]:
							a					=    self.get_image_processor()
							a					=    self.get_tokenizer()

							a					=    ChineseCLIPProcessor(tokenizer=__lowerCamelCase      ,   image_processor=__lowerCamelCase							)

							a					=    "Alexandra，T-shirt的价格是15便士。"

							a					=    processor(text=__lowerCamelCase							)

							a					=    tokenizer(__lowerCamelCase							)

							for key in encoded_tok.keys():
										self.assertListEqual(encoded_tok[key]      ,   encoded_processor[key]							)





				def 							__UpperCAmelCase							(   self       :  List[Any]							)      ->  Any:
							a					=    self.get_image_processor()
							a					=    self.get_tokenizer()

							a					=    ChineseCLIPProcessor(tokenizer=__lowerCamelCase      ,   image_processor=__lowerCamelCase							)

							a					=    "Alexandra，T-shirt的价格是15便士。"
							a					=    self.prepare_image_inputs()

							a					=    processor(text=__lowerCamelCase      ,   images=__lowerCamelCase							)

							self.assertListEqual(list(inputs.keys()							)      ,   ["input_ids", "token_type_ids", "attention_mask", "pixel_values"]							)

							# test if it raises when no input is passed
							with pytest.raises(__lowerCamelCase							):
										processor()





				def 							__UpperCAmelCase							(   self       :  List[str]							)      ->  Optional[int]:
							a					=    self.get_image_processor()
							a					=    self.get_tokenizer()

							a					=    ChineseCLIPProcessor(tokenizer=__lowerCamelCase      ,   image_processor=__lowerCamelCase							)

							a					=    [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

							a					=    processor.batch_decode(__lowerCamelCase							)
							a					=    tokenizer.batch_decode(__lowerCamelCase							)

							self.assertListEqual(__lowerCamelCase      ,   __lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  Dict							)      ->  List[str]:
							a					=    self.get_image_processor()
							a					=    self.get_tokenizer()

							a					=    ChineseCLIPProcessor(tokenizer=__lowerCamelCase      ,   image_processor=__lowerCamelCase							)

							a					=    "Alexandra，T-shirt的价格是15便士。"
							a					=    self.prepare_image_inputs()

							a					=    processor(text=__lowerCamelCase      ,   images=__lowerCamelCase							)

							self.assertListEqual(list(inputs.keys()							)      ,   processor.model_input_names							)






