I have a script here that does batch inference on an LLM based on a set of prompts in a…
- User
I have a script here that does batch inference on an LLM based on a set of prompts in a text file. #! /usr/bin/env python
import time from collections import namedtuple from typing import List
import torch import vllm from loguru import logger as log
Customize these.
MODEL_NAME = "teknium/OpenHermes-7B" SHAREGPT = True # If False, will use Alpaca format PROMPT_FILE = "prompts.txt" BATCH_SIZE = 100 TEMPERATURE = 0.8 TOP_P = 0.95 MAX_TOKENS = 2000 PRESENCE_PENALTY = 0.0 FREQUENCY_PENALTY = 0.0 SEED = 42
End customize.
SHAREGPT_FORMAT = "USER:\n{prompt}\nASSISTANT:\n" ALPACA_FORMAT = "### Instruction:\n{prompt}\nResponse:\n"
GPU_NAME = ( torch.cuda.get_device_name(0) if torch.cuda.is_available() else "None" )
InferenceResult = namedtuple("InferenceResult", ["prompt", "response"])
def load_prompts(path: str) -> List[str]: with open(path, "r") as f: return f.read().splitlines()
def format_prompts(prompts: List[str]) -> List[str]: if SHAREGPT == True: return [SHAREGPT_FORMAT.format(prompt=prompt) for prompt in prompts] else: return [ALPACA_FORMAT.format(prompt=prompt) for prompt in prompts]
def batch_prompts(prompts: List[str], batch_size: int) -> List[List[str]]: return [ prompts[i : i + batch_size] for i in range(0, len(prompts), batch_size) ]
You can modify this to write to a file, make your HTML, whatever.
def display_results(results: List[InferenceResult]): for result in results: print("-" * 10) print(result.prompt) print(result.response) print("-" * 10)
if name == "main": log.info(f"GPU: {GPU_NAME}") log.info(f"model: {MODEL_NAME}") log.info(f"batch_size: {BATCH_SIZE}") log.info(f"temperature: {TEMPERATURE}") log.info(f"top_p: {TOP_P}") log.info(f"max_tokens: {MAX_TOKENS}") log.info(f"presence_penalty: {PRESENCE_PENALTY}") log.info(f"frequency_penalty: {FREQUENCY_PENALTY}") log.info(f"seed: {SEED}")
code - Assistant
Here's a modification to the provided script that writes the results to a JSON file instead of displaying them:
- You'll need to add the
jsonlibrary import. - Replace the
display_resultsfunction withwrite_results_to_json. - Update the
__main__block to call the new function.
Here's the modified code:
python - You'll need to add the