|
| 1 | +import torch |
| 2 | +from datasets import load_dataset |
| 3 | +from tqdm import tqdm |
| 4 | +import pydra |
| 5 | +import multiprocessing |
| 6 | +import random |
| 7 | +import requests |
| 8 | +from functools import partial |
| 9 | +import pandas as pd |
| 10 | + |
| 11 | +import sys |
| 12 | +sys.path.append('/n/netscratch/pehlevan_lab/Everyone/indranilhalder/language_model_inference/iLLM') # Update path to iLLM |
| 13 | + |
| 14 | +from generate.prompts import MATH_COT_PROMPT |
| 15 | +from utils import save_yaml, GenerateScriptConfig |
| 16 | +from generate.vllm_utils import vllm_manager |
| 17 | + |
| 18 | +def run_inference(item, config: GenerateScriptConfig): |
| 19 | + outpath = config.save_dir / f"{item['id']}.yaml" |
| 20 | + if outpath.exists(): |
| 21 | + return |
| 22 | + |
| 23 | + prompt = MATH_COT_PROMPT + f"\n\nProblem:\n{item['problem']}\n\nSolution:" |
| 24 | + |
| 25 | + url = f"http://localhost:{config.vllm_port}/generate" |
| 26 | + |
| 27 | + num_samples = config.num_samples |
| 28 | + batch_size = config.batch_size |
| 29 | + |
| 30 | + assert num_samples % batch_size == 0 |
| 31 | + |
| 32 | + samples = [] |
| 33 | + for _ in tqdm(range(num_samples // batch_size), desc=f"Item {item['id']}"): |
| 34 | + |
| 35 | + body = { |
| 36 | + "prompt": prompt, |
| 37 | + "max_tokens": config.max_tokens, |
| 38 | + "n": batch_size, |
| 39 | + "temperature": config.temperature, |
| 40 | + "top_p": config.top_p, |
| 41 | + "stop": config.stop_strings, |
| 42 | + "logprobs": 1, |
| 43 | + } |
| 44 | + |
| 45 | + response = requests.post(url, json=body) |
| 46 | + respj = response.json() |
| 47 | + samples.extend(respj["text"]) |
| 48 | + |
| 49 | + out = { |
| 50 | + "level": item["level"], |
| 51 | + "type": item["type"], |
| 52 | + "prompt": prompt, |
| 53 | + "question": item["problem"], |
| 54 | + "samples": samples, |
| 55 | + "gt_answer": item["solution"], |
| 56 | + } |
| 57 | + |
| 58 | + save_yaml(outpath, out) |
| 59 | + |
| 60 | + |
| 61 | +@pydra.main(GenerateScriptConfig) |
| 62 | +def main( |
| 63 | + config: GenerateScriptConfig, |
| 64 | +): |
| 65 | + |
| 66 | + print('Test case with LLM temperature: ', config.temperature) |
| 67 | + test_dataset = list( |
| 68 | + load_dataset( |
| 69 | + "hendrycks/competition_math", "main", split="test", trust_remote_code=True |
| 70 | + ) |
| 71 | + ) |
| 72 | + df=pd.DataFrame(test_dataset) |
| 73 | + df =df[df['type'].str.contains('algebra', case=False, na=False)]# Mention problem type |
| 74 | + algebra_problems =df[df['level'].str.contains('Level 1', case=False, na=False)]# Mention problem level |
| 75 | + test_dataset=algebra_problems.to_dict('records') |
| 76 | + |
| 77 | + train_dataset = list( |
| 78 | + load_dataset( |
| 79 | + "hendrycks/competition_math", "main", split="train", trust_remote_code=True |
| 80 | + ) |
| 81 | + ) |
| 82 | + |
| 83 | + print(f"Number of test items: {len(test_dataset)}") |
| 84 | + print(f"Number of train items: {len(train_dataset)}") |
| 85 | + |
| 86 | + random.seed(config.seed) |
| 87 | + |
| 88 | + for i, data in enumerate(train_dataset): |
| 89 | + data["id"] = i |
| 90 | + |
| 91 | + for i, data in enumerate(test_dataset): |
| 92 | + few_shot_items = random.sample(train_dataset, config.num_few_shot) |
| 93 | + data["id"] = i |
| 94 | + data["few_shot_items"] = few_shot_items |
| 95 | + |
| 96 | + if config.randomize: |
| 97 | + random.shuffle(test_dataset) |
| 98 | + |
| 99 | + |
| 100 | + shuffled_limit = test_dataset |
| 101 | + |
| 102 | + if config.limit is not None: |
| 103 | + limit = config.limit |
| 104 | + else: |
| 105 | + limit = len(shuffled_limit) |
| 106 | + |
| 107 | + if config.stride is not None: |
| 108 | + stride = config.stride |
| 109 | + else: |
| 110 | + stride = 1 |
| 111 | + |
| 112 | + if config.offset is not None: |
| 113 | + offset = config.offset |
| 114 | + else: |
| 115 | + offset = 0 |
| 116 | + |
| 117 | + shuffled_limit = shuffled_limit[offset:limit:stride] |
| 118 | + |
| 119 | + print(f"Total number of items to process: {len(shuffled_limit)}") |
| 120 | + |
| 121 | + with vllm_manager(config) as vllm_port: |
| 122 | + config.vllm_port = vllm_port |
| 123 | + |
| 124 | + go_func = partial(run_inference, config=config) |
| 125 | + |
| 126 | + if config.num_workers not in [0, None]: |
| 127 | + with multiprocessing.Pool(config.num_workers) as pool: |
| 128 | + predictions = list( |
| 129 | + tqdm( |
| 130 | + pool.imap_unordered(go_func, test_dataset), |
| 131 | + total=len(test_dataset), |
| 132 | + ) |
| 133 | + ) |
| 134 | + else: |
| 135 | + predictions = [] |
| 136 | + for item in tqdm(test_dataset): |
| 137 | + predictions.append(go_func(item)) |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + main() |
| 142 | + |
0 commit comments