94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
152 lines
4.4 KiB
Python
152 lines
4.4 KiB
Python
"""
|
|
Usage:
|
|
python3 bench_hf.py --model-path meta-llama/Llama-2-7b-hf --data-dir data --ntrain 5
|
|
"""
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
import time
|
|
|
|
import numpy as np
|
|
import pandas as pd
|
|
import torch
|
|
from tqdm import tqdm
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
choices = ["A", "B", "C", "D"]
|
|
|
|
|
|
def format_subject(subject):
|
|
l = subject.split("_")
|
|
s = ""
|
|
for entry in l:
|
|
s += " " + entry
|
|
return s
|
|
|
|
|
|
def format_example(df, idx, include_answer=True):
|
|
prompt = df.iloc[idx, 0]
|
|
k = df.shape[1] - 2
|
|
for j in range(k):
|
|
prompt += "\n{}. {}".format(choices[j], df.iloc[idx, j + 1])
|
|
prompt += "\nAnswer:"
|
|
if include_answer:
|
|
prompt += " {}\n\n".format(df.iloc[idx, k + 1])
|
|
return prompt
|
|
|
|
|
|
def gen_prompt(train_df, subject, k=-1):
|
|
prompt = "The following are multiple choice questions (with answers) about{}.\n\n".format(
|
|
format_subject(subject)
|
|
)
|
|
if k == -1:
|
|
k = train_df.shape[0]
|
|
for i in range(k):
|
|
prompt += format_example(train_df, i)
|
|
return prompt
|
|
|
|
|
|
@torch.no_grad()
|
|
def main(args):
|
|
print(f"Loading model: {args.model_path}")
|
|
tokenizer = AutoTokenizer.from_pretrained(args.model_path, trust_remote_code=True)
|
|
model = AutoModelForCausalLM.from_pretrained(
|
|
args.model_path,
|
|
torch_dtype=torch.bfloat16,
|
|
trust_remote_code=True,
|
|
device_map="auto",
|
|
).eval()
|
|
|
|
subjects = sorted(
|
|
[
|
|
f.split("_test.csv")[0]
|
|
for f in os.listdir(os.path.join(args.data_dir, "test"))
|
|
if "_test.csv" in f
|
|
]
|
|
)
|
|
|
|
all_cors = []
|
|
num_requests = 0
|
|
total_latency = 0
|
|
|
|
for subject in tqdm(subjects[: args.nsub]):
|
|
dev_df = pd.read_csv(
|
|
os.path.join(args.data_dir, "dev", subject + "_dev.csv"), header=None
|
|
)[: args.ntrain]
|
|
test_df = pd.read_csv(
|
|
os.path.join(args.data_dir, "test", subject + "_test.csv"), header=None
|
|
)
|
|
|
|
k = args.ntrain
|
|
few_shot_examples = gen_prompt(dev_df, subject, k)
|
|
while len(tokenizer.encode(few_shot_examples)) > 1536:
|
|
k -= 1
|
|
if k < 0:
|
|
break
|
|
few_shot_examples = gen_prompt(dev_df, subject, k)
|
|
|
|
preds = []
|
|
labels = []
|
|
tic = time.perf_counter()
|
|
|
|
for i in range(test_df.shape[0]):
|
|
prompt_end = format_example(test_df, i, include_answer=False)
|
|
prompt = few_shot_examples + prompt_end
|
|
|
|
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
|
|
output_ids = model.generate(
|
|
input_ids,
|
|
max_new_tokens=1,
|
|
do_sample=False,
|
|
pad_token_id=tokenizer.eos_token_id,
|
|
)
|
|
|
|
output_str = tokenizer.decode(
|
|
output_ids[0][input_ids.shape[-1] :], skip_special_tokens=True
|
|
)
|
|
preds.append(output_str.strip()[0] if len(output_str.strip()) > 0 else "")
|
|
labels.append(test_df.iloc[i, test_df.shape[1] - 1])
|
|
|
|
latency = time.perf_counter() - tic
|
|
total_latency += latency
|
|
|
|
cors = [pred == label for pred, label in zip(preds, labels)]
|
|
all_cors.append(cors)
|
|
num_requests += len(test_df)
|
|
|
|
print(
|
|
f"Subject: {subject}, Accuracy: {np.mean(cors):.3f}, Latency: {latency:.3f}s"
|
|
)
|
|
|
|
weighted_acc = np.mean(np.concatenate(all_cors))
|
|
print(f"Total Latency: {total_latency:.3f}s")
|
|
print(f"Average Accuracy: {weighted_acc:.3f}")
|
|
|
|
if args.output:
|
|
with open(args.output, "a") as fout:
|
|
value = {
|
|
"task": "mmlu",
|
|
"backend": "hf",
|
|
"model": args.model_path,
|
|
"latency": round(total_latency, 3),
|
|
"accuracy": round(weighted_acc, 3),
|
|
"num_requests": num_requests,
|
|
"other": {
|
|
"nsub": args.nsub,
|
|
"ntrain": args.ntrain,
|
|
},
|
|
}
|
|
fout.write(json.dumps(value) + "\n")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--model-path", type=str, required=True)
|
|
parser.add_argument("--ntrain", type=int, default=5)
|
|
parser.add_argument("--data-dir", type=str, default="data")
|
|
parser.add_argument("--nsub", type=int, default=60)
|
|
parser.add_argument("--output", type=str, help="Output file path")
|
|
args = parser.parse_args()
|
|
main(args)
|