


'''simple docstring'''






from dataclasses import asdict, dataclass
from typing import Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__A					:  int											=      logging.get_logger(__name__)

# TODO Update this
__A					:  Optional[Any]											=      {
    "facebook/esm-1b": "https://huggingface.co/facebook/esm-1b/resolve/main/config.json",
    # See all ESM models at https://huggingface.co/models?filter=esm
}






class    __snake_case       (   _SCREAMING_SNAKE_CASE):







    """simple docstring"""



    lowercase    =					'esm'

    def __init__(   self    :  Union[str, Any]    ,						lowerCamelCase    :  Union[str, Any]=None    ,						lowerCamelCase    :  int=None    ,						lowerCamelCase    :  Any=None    ,						lowerCamelCase    :  Union[str, Any]=7_68    ,						lowerCamelCase    :  int=12    ,						lowerCamelCase    :  Dict=12    ,						lowerCamelCase    :  Dict=30_72    ,						lowerCamelCase    :  int=0.1    ,						lowerCamelCase    :  Union[str, Any]=0.1    ,						lowerCamelCase    :  Dict=10_26    ,						lowerCamelCase    :  Dict=0.02    ,						lowerCamelCase    :  Dict=1E-12    ,						lowerCamelCase    :  Any="absolute"    ,						lowerCamelCase    :  Any=True    ,						lowerCamelCase    :  Tuple=None    ,						lowerCamelCase    :  str=False    ,						lowerCamelCase    :  Dict=False    ,						lowerCamelCase    :  Tuple=None    ,						lowerCamelCase    :  Optional[int]=None    ,						**lowerCamelCase    :  Any    ,						)   ->  Tuple:
      super().__init__(pad_token_id=lowerCamelCase    ,						mask_token_id=lowerCamelCase    ,						**lowerCamelCase		)

      lowerCAmelCase_			:      Optional[int]			=   vocab_size
      lowerCAmelCase_			:      List[str]			=   hidden_size
      lowerCAmelCase_			:      int			=   num_hidden_layers
      lowerCAmelCase_			:      List[Any]			=   num_attention_heads
      lowerCAmelCase_			:      Optional[int]			=   intermediate_size
      lowerCAmelCase_			:      str			=   hidden_dropout_prob
      lowerCAmelCase_			:      int			=   attention_probs_dropout_prob
      lowerCAmelCase_			:      List[str]			=   max_position_embeddings
      lowerCAmelCase_			:      List[str]			=   initializer_range
      lowerCAmelCase_			:      str			=   layer_norm_eps
      lowerCAmelCase_			:      str			=   position_embedding_type
      lowerCAmelCase_			:      str			=   use_cache
      lowerCAmelCase_			:      Union[str, Any]			=   emb_layer_norm_before
      lowerCAmelCase_			:      Any			=   token_dropout
      lowerCAmelCase_			:      List[Any]			=   is_folding_model
      if is_folding_model:
        if esmfold_config is None:
          logger.info("""No esmfold_config supplied for folding model, using default values."""		)
          lowerCAmelCase_			:      Tuple			=   EsmFoldConfig()
        elif isinstance(lowerCamelCase    ,						lowerCamelCase		):
          lowerCAmelCase_			:      Tuple			=   EsmFoldConfig(**lowerCamelCase		)
        lowerCAmelCase_			:      int			=   esmfold_config
        if vocab_list is None:
          logger.warning("""No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!"""		)
          lowerCAmelCase_			:      List[Any]			=   get_default_vocab_list()
        else:
          lowerCAmelCase_			:      List[Any]			=   vocab_list
      else:
        lowerCAmelCase_			:      Dict			=   None
        lowerCAmelCase_			:      Optional[int]			=   None
      if self.esmfold_config is not None and getattr(self.esmfold_config    ,						"""use_esm_attn_map"""    ,						lowerCamelCase		):
        raise ValueError("""The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!"""		)





    def __lowercase   (   self    :  Dict		)   ->  List[str]:
      lowerCAmelCase_			:      Union[str, Any]			=   super().to_dict()
      if isinstance(self.esmfold_config    ,						lowerCamelCase		):
        lowerCAmelCase_			:      str			=   self.esmfold_config.to_dict()
      return output







@dataclass
class    __snake_case       :







    """simple docstring"""



    lowercase    =					None
    lowercase    =					True
    lowercase    =					False
    lowercase    =					False
    lowercase    =					False
    lowercase    =					0

    lowercase    =					True
    lowercase    =					False

    lowercase    =					1_28
    lowercase    =					None

    def __lowercase   (   self    :  Any		)   ->  Union[str, Any]:
      if self.trunk is None:
        lowerCAmelCase_			:      Union[str, Any]			=   TrunkConfig()
      elif isinstance(self.trunk    ,						lowerCamelCase		):
        lowerCAmelCase_			:      Tuple			=   TrunkConfig(**self.trunk		)





    def __lowercase   (   self    :  List[str]		)   ->  Tuple:
      lowerCAmelCase_			:      Optional[Any]			=   asdict(self		)
      lowerCAmelCase_			:      Optional[int]			=   self.trunk.to_dict()
      return output







@dataclass
class    __snake_case       :







    """simple docstring"""



    lowercase    =					48
    lowercase    =					10_24
    lowercase    =					1_28
    lowercase    =					32
    lowercase    =					32
    lowercase    =					32
    lowercase    =					0
    lowercase    =					0
    lowercase    =					False
    lowercase    =					4
    lowercase    =					1_28
    lowercase    =					None

    def __lowercase   (   self    :  Union[str, Any]		)   ->  List[str]:
      if self.structure_module is None:
        lowerCAmelCase_			:      int			=   StructureModuleConfig()
      elif isinstance(self.structure_module    ,						lowerCamelCase		):
        lowerCAmelCase_			:      List[Any]			=   StructureModuleConfig(**self.structure_module		)

      if self.max_recycles <= 0:
        raise ValueError(F'`max_recycles` should be positive, got {self.max_recycles}.'		)
      if self.sequence_state_dim % self.sequence_state_dim != 0:
        raise ValueError(
            """`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got"""
            F' {self.sequence_state_dim} and {self.sequence_state_dim}.'		)
      if self.pairwise_state_dim % self.pairwise_state_dim != 0:
        raise ValueError(
            """`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got"""
            F' {self.pairwise_state_dim} and {self.pairwise_state_dim}.'		)

      lowerCAmelCase_			:      int			=   self.sequence_state_dim // self.sequence_head_width
      lowerCAmelCase_			:      Dict			=   self.pairwise_state_dim // self.pairwise_head_width

      if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width:
        raise ValueError(
            """`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got"""
            F' {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.'		)
      if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width:
        raise ValueError(
            """`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got"""
            F' {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.'		)
      if self.pairwise_state_dim % 2 != 0:
        raise ValueError(F'`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.'		)

      if self.dropout >= 0.4:
        raise ValueError(F'`dropout` should not be greater than 0.4, got {self.dropout}.'		)





    def __lowercase   (   self    :  Dict		)   ->  Dict:
      lowerCAmelCase_			:      str			=   asdict(self		)
      lowerCAmelCase_			:      int			=   self.structure_module.to_dict()
      return output







@dataclass
class    __snake_case       :







    """simple docstring"""



    lowercase    =					3_84
    lowercase    =					1_28
    lowercase    =					16
    lowercase    =					1_28
    lowercase    =					12
    lowercase    =					4
    lowercase    =					8
    lowercase    =					0.1
    lowercase    =					8
    lowercase    =					1
    lowercase    =					2
    lowercase    =					7
    lowercase    =					10
    lowercase    =					1E-8
    lowercase    =					1E5

    def __lowercase   (   self    :  Dict		)   ->  List[Any]:
      return asdict(self		)







def 				UpperCamelCase_			(    ):
  '''simple docstring'''





  return (
      "<cls>",
      "<pad>",
      "<eos>",
      "<unk>",
      "L",
      "A",
      "G",
      "V",
      "S",
      "E",
      "R",
      "T",
      "I",
      "D",
      "P",
      "K",
      "Q",
      "N",
      "F",
      "Y",
      "M",
      "H",
      "W",
      "C",
      "X",
      "B",
      "U",
      "Z",
      "O",
      ".",
      "-",
      "<null_1>",
      "<mask>",
  )







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







class       lowerCamelCase__   (       lowerCAmelCase):





     def __init__(self		,     UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase=None		,     UpperCAmelCase=1       )     ->   Dict:
      _lowercase							      =tokenizer
      _lowercase							      =dataset
      _lowercase							      =len(UpperCAmelCase       ) if n_tasks is None else n_tasks
      _lowercase							      =n_copies





     def __iter__(self       )     ->   Optional[Any]:
      _lowercase							      =[]
      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()       )
      _lowercase							      =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       lowerCamelCase__   (       lowerCAmelCase):





     def __init__(self		,     UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase       )     ->   Optional[Any]:
      _lowercase							      =start_length
      _lowercase							      =eof_strings
      _lowercase							      =tokenizer





     def __call__(self		,     UpperCAmelCase		,     UpperCAmelCase		,     **UpperCAmelCase       )     ->   Dict:
      _lowercase							      =self.tokenizer.batch_decode(input_ids[:, self.start_length :]       )
      _lowercase							      =[]
      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 				UpperCAmelCase_					(							__snake_case						)     ->			Optional[Any]:







 """simple docstring"""


 _lowercase							      =re.split('''(%s)''' % '''|'''.join(__snake_case						)     ,						__snake_case						)
 # last string should be ""
 return "".join(string_list[:-2]						)
def 				UpperCAmelCase_					(							__snake_case     ,						__snake_case     ,						__snake_case     ,						__snake_case     ,						__snake_case     ,						__snake_case=20     ,						**__snake_case						)     ->			Tuple:







 """simple docstring"""


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

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

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

 _lowercase							      =[[] for _ in range(__snake_case						)]
 for task, generated_tokens in gen_token_dict.items():
  for s in generated_tokens:
   _lowercase							      =tokenizer.decode(__snake_case     ,						skip_special_tokens=__snake_case     ,						clean_up_tokenization_spaces=__snake_case						)
   code_gens[task].append(remove_last_block(__snake_case						)						)
 return code_gens
def 				UpperCAmelCase_					(							)     ->			str:







 """simple docstring"""


 _lowercase							      =HfArgumentParser(__snake_case						)
 _lowercase							      =parser.parse_args()

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

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

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

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

 # Generation settings
 _lowercase							      ={
     '''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     ,						__snake_case     ,						__snake_case						)]						),
 }

 # Load evaluation dataset and metric
 _lowercase							      =load_dataset('''openai_humaneval'''						)
 _lowercase							      =load_metric('''code_eval'''						)

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

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

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

 _lowercase      ,    _lowercase							      =accelerator.prepare(__snake_case     ,						__snake_case						)

 _lowercase							      =complete_code(
     __snake_case     ,						__snake_case     ,						__snake_case     ,						__snake_case     ,						n_tasks=__snake_case     ,						batch_size=args.batch_size     ,						**__snake_case     ,						)

 if accelerator.is_main_process:
  _lowercase							      =[]

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

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


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

