





'''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					:     int												=['''\nclass''', '''\ndef''', '''\n#''', '''\n@''', '''\nprint''', '''\nif''']
class       a_   (				_lowerCAmelCase							):





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





        """simple docstring"""




        lowercase_			:Optional[int]    			= tokenizer
        lowercase_			:int    			= dataset
        lowercase_			:str    			= len(lowercase						) if n_tasks is None else n_tasks
        lowercase_			:Tuple    			= n_copies







     def __iter__( self		:					Tuple						):





        """simple docstring"""




        lowercase_			: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()						)
        lowercase_			:Tuple    			= self.tokenizer(lowercase	,      padding=lowercase	,      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       a_   (				_lowerCAmelCase							):





     def __init__( self		:					List[str]	,      lowercase		:					List[str]	,      lowercase		:					List[Any]	,      lowercase		:					Dict						):





        """simple docstring"""




        lowercase_			:int    			= start_length
        lowercase_			:Optional[int]    			= eof_strings
        lowercase_			:int    			= tokenizer







     def __call__( self		:					Union[str, Any]	,      lowercase		:					Optional[int]	,      lowercase		:					Any	,      **lowercase		:					List[Any]						):





        """simple docstring"""




        lowercase_			:Optional[int]    			= self.tokenizer.batch_decode(input_ids[:, self.start_length :]						)
        lowercase_			:Union[str, Any]    			= []
        for decoded_generation in decoded_generations:
           done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings						)						)
        return all(lowercase						)







def 							UpperCAmelCase_  (						__lowerCamelCase					:					List[str] ):
   lowercase_			:Optional[Any]    			= re.split("(%s)" % "|".join(__lowerCamelCase )	,__lowerCamelCase )
   # last string should be ""
   return "".join(string_list[:-2] )
def 							UpperCAmelCase_  (						__lowerCamelCase					:					str	,__lowerCamelCase					:					Dict	,__lowerCamelCase					:					Optional[int]	,__lowerCamelCase					:					List[str]	,__lowerCamelCase					:					str	,__lowerCamelCase					:					List[Any]=20	,**__lowerCamelCase					:					Union[str, Any] ):
   lowercase_			:int    			= defaultdict(__lowerCamelCase )  # dict of list of generated tokens
   for step, batch in tqdm(enumerate(__lowerCamelCase ) ):
      with torch.no_grad():
         lowercase_			:List[Any]    			= batch["ids"].shape[-1]
         lowercase_			:List[str]    			= accelerator.unwrap_model(__lowerCamelCase ).generate(
             input_ids=batch["ids"][:, : batch["input_len"]]	,num_return_sequences=__lowerCamelCase	,**__lowerCamelCase )
         # each task is generated batch_size times
         lowercase_			:List[str]    			= batch["task_id"].repeat(__lowerCamelCase )
         lowercase_			:Optional[Any]    			= accelerator.pad_across_processes(
             __lowerCamelCase	,dim=1	,pad_index=tokenizer.pad_token_id )

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

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

   lowercase_			:Tuple    			= [[] for _ in range(__lowerCamelCase )]
   for task, generated_tokens in gen_token_dict.items():
      for s in generated_tokens:
         lowercase_			:Tuple    			= tokenizer.decode(__lowerCamelCase	,skip_special_tokens=__lowerCamelCase	,clean_up_tokenization_spaces=__lowerCamelCase )
         code_gens[task].append(remove_last_block(__lowerCamelCase ) )
   return code_gens
def 							UpperCAmelCase_  (						):
   # Setup configuration
   lowercase_			:Optional[int]    			= HfArgumentParser(__lowerCamelCase )
   lowercase_			:int    			= parser.parse_args()

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

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

   # Use dataset load to feed to accelerate
   lowercase_			:Dict    			= Accelerator()
   set_seed(args.seed	,device_specific=__lowerCamelCase )

   # Load model and tokenizer
   lowercase_			:Optional[Any]    			= AutoTokenizer.from_pretrained(args.model_ckpt )
   lowercase_			:Any    			= tokenizer.eos_token
   lowercase_			:Optional[Any]    			= AutoModelForCausalLM.from_pretrained(args.model_ckpt )

   # Generation settings
   lowercase_			:Optional[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	,__lowerCamelCase	,__lowerCamelCase )] ),
   }

   # Load evaluation dataset and metric
   lowercase_			:Any    			= load_dataset("openai_humaneval" )
   lowercase_			:Dict    			= load_metric("code_eval" )

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

   lowercase_			:Union[str, Any]    			= TokenizedDataset(__lowerCamelCase	,human_eval["test"]	,n_copies=__lowerCamelCase	,n_tasks=__lowerCamelCase )
   # do not confuse args.batch_size, which is actually the num_return_sequences
   lowercase_			:Any    			= DataLoader(__lowerCamelCase	,batch_size=1 )

   # Run a quick test to see if code evaluation is enabled
   try:
      lowercase_			:str    			= 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_			:List[Any]    			= accelerator.prepare(__lowerCamelCase	,__lowerCamelCase )

   lowercase_			:Optional[int]    			= complete_code(
       __lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,n_tasks=__lowerCamelCase	,batch_size=args.batch_size	,**__lowerCamelCase	,)

   if accelerator.is_main_process:
      lowercase_			:Dict    			= []

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

      # Evaluate completions with "code_eval" metric
      lowercase_			,							lowercase_			:List[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()









'''simple docstring'''


from typing import List, Optional

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


lowerCAmelCase					:     Optional[int]												=logging.get_logger(__name__)

lowerCAmelCase					:     Tuple												={
    '''huggingface/autoformer-tourism-monthly''': '''https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json''',
}
class       a_   (				_lowerCAmelCase							):
     __A      =			"autoformer"
     __A      =			{
         "hidden_size": "d_model",
         "num_attention_heads": "encoder_attention_heads",
         "num_hidden_layers": "encoder_layers",
     }





     def __init__( self		:					Any	,      lowercase		:					Optional[int] = None	,      lowercase		:					Optional[int] = None	,      lowercase		:					str = "student_t"	,      lowercase		:					str = "nll"	,      lowercase		:					int = 1	,      lowercase		:					List[int] = [1, 2, 3, 4, 5, 6, 7]	,      lowercase		:					bool = True	,      lowercase		:					int = 0	,      lowercase		:					int = 0	,      lowercase		:					int = 0	,      lowercase		:					int = 0	,      lowercase		:					Optional[List[int]] = None	,      lowercase		:					Optional[List[int]] = None	,      lowercase		:					int = 64	,      lowercase		:					int = 2	,      lowercase		:					int = 2	,      lowercase		:					int = 2	,      lowercase		:					int = 2	,      lowercase		:					int = 32	,      lowercase		:					int = 32	,      lowercase		:					str = "gelu"	,      lowercase		:					float = 0.1	,      lowercase		:					float = 0.1	,      lowercase		:					float = 0.1	,      lowercase		:					float = 0.1	,      lowercase		:					float = 0.1	,      lowercase		:					int = 100	,      lowercase		:					float = 0.02	,      lowercase		:					bool = True	,      lowercase		:					int=True	,      lowercase		:					int = 10	,      lowercase		:					int = 25	,      lowercase		:					int = 3	,      **lowercase		:					Dict	,      ):





        """simple docstring"""




        lowercase_			:str    			= prediction_length
        lowercase_			:Dict    			= context_length if context_length is not None else prediction_length
        lowercase_			:Any    			= distribution_output
        lowercase_			:Tuple    			= loss
        lowercase_			:Dict    			= input_size
        lowercase_			:Tuple    			= num_time_features
        lowercase_			:int    			= lags_sequence
        lowercase_			:Tuple    			= scaling
        lowercase_			:List[Any]    			= num_dynamic_real_features
        lowercase_			:Union[str, Any]    			= num_static_real_features
        lowercase_			:str    			= num_static_categorical_features
        if cardinality is not None and num_static_categorical_features > 0:
           if len(lowercase						) != num_static_categorical_features:
              raise ValueError(
                  "The cardinality should be a list of the same length as `num_static_categorical_features`"						)
           lowercase_			:Optional[int]    			= cardinality
        else:
           lowercase_			:Optional[int]    			= [0]
        if embedding_dimension is not None and num_static_categorical_features > 0:
           if len(lowercase						) != num_static_categorical_features:
              raise ValueError(
                  "The embedding dimension should be a list of the same length as `num_static_categorical_features`"						)
           lowercase_			:Tuple    			= embedding_dimension
        else:
           lowercase_			:Dict    			= [min(50	,      (cat + 1) // 2						) for cat in self.cardinality]
        lowercase_			:Any    			= num_parallel_samples

        # Transformer architecture configuration
        lowercase_			:Tuple    			= input_size * len(self.lags_sequence						) + self._number_of_features
        lowercase_			:Union[str, Any]    			= d_model
        lowercase_			:Optional[Any]    			= encoder_attention_heads
        lowercase_			:Optional[Any]    			= decoder_attention_heads
        lowercase_			:Optional[int]    			= encoder_ffn_dim
        lowercase_			:int    			= decoder_ffn_dim
        lowercase_			:Any    			= encoder_layers
        lowercase_			:Optional[Any]    			= decoder_layers

        lowercase_			:Dict    			= dropout
        lowercase_			:Dict    			= attention_dropout
        lowercase_			:str    			= activation_dropout
        lowercase_			:int    			= encoder_layerdrop
        lowercase_			:Dict    			= decoder_layerdrop

        lowercase_			:List[str]    			= activation_function
        lowercase_			:int    			= init_std

        lowercase_			:Optional[int]    			= use_cache

        # Autoformer
        lowercase_			:List[str]    			= label_length
        lowercase_			:List[str]    			= moving_average
        lowercase_			:Optional[int]    			= autocorrelation_factor

        super().__init__(is_encoder_decoder=lowercase	,      **lowercase						)







     @property
     def 							lowercase__      ( self		:					int						):





        """simple docstring"""




        return (
            sum(self.embedding_dimension						)
            + self.num_dynamic_real_features
            + self.num_time_features
            + self.num_static_real_features
            + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
        )


