bench_sglang.py 2.95 KB
Newer Older
Lianmin Zheng's avatar
Lianmin Zheng committed
1
2
3
import argparse
import json
import os
Liangsheng Yin's avatar
Liangsheng Yin committed
4
import time
Lianmin Zheng's avatar
Lianmin Zheng committed
5
6

import tqdm
Liangsheng Yin's avatar
Liangsheng Yin committed
7
8
9
10
11
12
13

import sglang as sgl
from sglang.test.test_utils import (
    add_common_sglang_args_and_parse,
    select_sglang_backend,
)
from sglang.utils import dump_state_text, read_jsonl
Lianmin Zheng's avatar
Lianmin Zheng committed
14
15
16
17
18
19
20
21
22


@sgl.function
def image_qa(s, image_file, question):
    s += sgl.user(sgl.image(image_file) + question)
    s += sgl.assistant(sgl.gen("answer", max_tokens=args.max_tokens))


def main(args):
Liangsheng Yin's avatar
Liangsheng Yin committed
23
    lines = read_jsonl(args.question_file)[: args.num_questions]
Lianmin Zheng's avatar
Lianmin Zheng committed
24
    arguments = [
Liangsheng Yin's avatar
Liangsheng Yin committed
25
26
27
28
29
        {
            "image_file": os.path.abspath(args.image_folder + "/" + l["image"]),
            "question": l["text"],
        }
        for l in lines
Lianmin Zheng's avatar
Lianmin Zheng committed
30
    ]
Liangsheng Yin's avatar
Liangsheng Yin committed
31
    # arguments = [
Lianmin Zheng's avatar
Lianmin Zheng committed
32
33
34
    #    {"image_file":
    #        Image.open(os.path.abspath(args.image_folder + "/" + l["image"])),
    #      "question": l["text"]} for l in lines
Liangsheng Yin's avatar
Liangsheng Yin committed
35
    # ]
Lianmin Zheng's avatar
Lianmin Zheng committed
36
37
38
39
40
41
42
43
44
45
46
47
48

    states = [None] * len(lines)

    # Select backend
    backend = select_sglang_backend(args)
    sgl.set_default_backend(backend)

    # Run requests
    tic = time.time()
    if args.parallel == 1:
        for i in tqdm.tqdm(range(len(lines))):
            image_file = arguments[i]["image_file"]
            question = arguments[i]["question"]
Liangsheng Yin's avatar
Liangsheng Yin committed
49
            ret = image_qa.run(image_file=image_file, question=question, temperature=0)
Lianmin Zheng's avatar
Lianmin Zheng committed
50
51
52
            states[i] = ret
    else:
        states = image_qa.run_batch(
Liangsheng Yin's avatar
Liangsheng Yin committed
53
54
            arguments, temperature=0, num_threads=args.parallel, progress_bar=True
        )
Lianmin Zheng's avatar
Lianmin Zheng committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    latency = time.time() - tic

    print(f"Latency: {latency:.3f}")

    # Write results
    dump_state_text(f"tmp_output_{args.backend}.txt", states)

    print(f"Write output to {args.answer_file}")
    with open(args.answer_file, "w") as fout:
        for i in range(len(lines)):
            value = {
                "question_id": lines[i]["question_id"],
                "prompt": lines[i]["text"],
                "text": states[i]["answer"].strip(),
                "model_id": backend.model_info["model_path"],
                "answer_id": i,
                "metadata": {},
            }
            fout.write(json.dumps(value) + "\n")

    with open(args.result_file, "a") as fout:
        value = {
            "task": "llava_bench",
            "backend": args.backend,
            "num_gpus": 1,
            "latency": round(latency, 3),
            "num_requests": len(lines),
            "parallel": args.parallel,
        }
        fout.write(json.dumps(value) + "\n")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--question-file", type=str, default="questions.jsonl")
    parser.add_argument("--answer-file", type=str, default="answers.jsonl")
    parser.add_argument("--image-folder", type=str, default="./images")
    parser.add_argument("--temperature", type=float, default=0.0)
    parser.add_argument("--num-questions", type=int, default=None)
    parser.add_argument("--max-tokens", type=int, default=768)
    args = add_common_sglang_args_and_parse(parser)
    main(args)