File size: 2,037 Bytes
23c39b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from typing import Optional
from tqdm import tqdm

from simple_inference_engine import inference_load_checkpoint, inference_generate

class BaseEngine:
    def generate(self, prompts: list[str]) -> list[str]:
        raise NotImplementedError

class VLLMEngine(BaseEngine):
    def __init__(self, ckpt_path: str, **kwargs):
        # Lazy import: vllm is only needed for external-baseline evaluation,
        # and the docker image's vllm build is broken (linked against CUDA 13's
        # libcudart while the image ships CUDA 12.8). Keep SimpleEngine usable.
        from vllm import LLM
        self.llm = LLM(model=ckpt_path, **kwargs)

    def generate(self, prompts: list[str], temperature: float = 0.0, max_tokens: Optional[int] = None, stop: Optional[str | list[str]] = None) -> list[str]:
        from vllm import SamplingParams
        outputs = self.llm.generate(prompts, SamplingParams(
            temperature=temperature,
            max_tokens=max_tokens,
            stop=stop
        ))
        return [out.outputs[0].text for out in outputs]

class SimpleEngine(BaseEngine):
    def __init__(self, ckpt_path: str, ckpt_epoch: Optional[int] = None, ckpt_use_ema: bool = True):
        self.ckpt = inference_load_checkpoint(ckpt_path, ckpt_epoch, ckpt_use_ema)

    def generate(self, prompts: list[str], batch_size: int = 100, max_context: int = 1024, max_tokens: Optional[int] = None, temperature: float = 0.0, condition: str = "direct") -> list[str]:
        if max_tokens is None:
            max_tokens = max_context

        # Launch generation
        engine_prompts = [(i, (condition, p.strip())) for i, p in enumerate(prompts)]
        outputs = [""] * len(engine_prompts)

        pbar = tqdm(total=len(outputs), desc="generation")
        for gen_id, generated_text in inference_generate(
            self.ckpt, iter(engine_prompts), max_context, max_tokens, batch_size, temperature
        ):
            outputs[gen_id] = generated_text
            pbar.update()
        pbar.close()

        return outputs