更新时间:2025-11-19 GMT+08:00
分享

大语言模型推理参数自动寻优

大模型推理服务部署时,超参数的配置对模型性能影响很大。比如请求 batch size 、最大处理的 token 等会对服务的性能带来很大影响。较大的 batch size 可以一次执行多条请求提升并发吞吐,但是 batch 增大的同时也伴随计算量增加造成请求时延的降低。同时不同长度的输入,也应当采用不同的 batch size 策略。比如短文本可以采用较大的 batch size,长文本则需要减少 batch size 以避免 OOM。

acsprof 超参数寻优工具通过将性能SLO指标(如TTFT、TPOT、QPS等)与模型超参数之间建立函数关系,在指定超参数空间进行迭代寻找最佳性能,进而确定最优超参数组合。当前已针对 vllm 的PD 混部场景完成适配。

约束限制

当前版本不支持在单个容器内启动多个寻优进程

安装 acsprof 工具

acsprof 超参数寻优能力目前集成在 acs-service-profiler 工具中。昇腾云6.5.906及以后版本的配套镜像中默认已经安装acs_service_profiler-xxx-py3-none-any.whl工具包,无需单独安装。

可以通过如下命令查看是否已经安装acs_service_profiler工具。

$ pip show acs-service-profiler

如果需要安装acs-service-profiler工具,请参考以下步骤执行。

1. 获取acs-service-profiler工具的whl包。whl包存放在软件包AscendCloud-LLM-xxx.zip > llm_tools目录中。acs-service-profiler工具安装在具备python的环境即可。

2. 在已经安装python的环境中如windows机器(打开Windows PowerShell)或者linux服务器中,执行如下命令安装acs-service-profiler工具。(可选)

$ pip install llm_tools/acs_service_profiler-*-py3-none-any.whl

配置寻优参数

使用 acsprof 进行参数寻优前需要先指定三部分配置:寻优算法配置、服务启动配置、acs-bench 性能评测配置。配置内容写在config.toml中,并通过MODEL_EVAL_STATE_CONFIG_PATH 环境变量指定,命令如下:

export  MODEL_EVAL_STATE_CONFIG_PATH=config.toml

以下为 Qwen3-8B 的混部服务config.toml 配置文件:

# -------------------------寻优相关参数配置-----------------------------------------
n_particles = 5
iters = 10
ttft_penalty = 1
tpot_penalty = 1
success_rate_penalty = 5.0
ttft_slo = 2
tpot_slo = 0.05
service = "master"
sample_size = 1000

# -------------------------vllm相关配置--------------------------------------------
[vllm]
[vllm.command]
host = "localhost"
port = "8081"
model = "/Qwen3-8B"
served_model_name = "qwen3"
others = "--dtype=auto --tensor-parallel-size=1 --block-size=128 --max-model-len=32768 --trust-remote-code --enable-reasoning --reasoning-parser=deepseek_r1 --guided-decoding-backend=xgrammar --enable-auto-tool-choice --tool-call-parser=hermes --distributed-executor-backend=ray --load-format=dummy --enforce-eager"
[[vllm.target_field]]
name = "MAX_NUM_BATCHED_TOKENS"
config_position = "env"
min = 8192
max = 65536
dtype = "int"
value = 8192
[[vllm.target_field]]
name = "MAX_NUM_SEQS"
config_position = "env"
min = 32
max = 512
dtype = "int"
value = 64

# -------------------------acsbench相关配置-----------------------------------------
[acs_bench.command]
dataset_type = "custom"
input_path = "built_in_dataset"
input_length = 512
output_length = 128
num_requests = 80
provider = "providers.yaml"
concurrency = 3000
benchmark_save_path = "test_results"
others = ""

其中寻优相关参数可参考建议值, vllm 服务及 acs-bench 参数依据实际服务参数指定。具体参数说明参考寻优参数说明

寻优参数说明

表1 寻优参数配置说明

参数

含义

类型

取值说明

n_particles

寻优种子数

int

范围为1-1000,建议设为 5 ~ 10

iters

迭代轮次数

int

范围为1-1000,建议设为 5 ~ 10

ttft_penalty

全量时延惩罚系数

int

0或1,1表示开启,0关闭。ttft_penalty 和 tpot_penalty 不可同时取0,至少需要有一个为1

tpot_penalty

增量时延惩罚系数

int

0或1,1表示开启,0关闭。ttft_penalty 和 tpot_penalty 不可同时取0,至少需要有一个为1

success_rate_penalty

请求成功率惩罚系数

float

建议值5

ttft_slo

全量时延SLO约束

float

单位s,必须大于0,建议值1~2

tpot_slo

增量时延SLO约束

float

单位s,必须大于0,建议值 0.05s

service

多机启动标识

str

主机master,从机slave

sample_size

对原始数据集采样大小,用采样后的数据进行调优,可增加寻优效率

int

建议设为原数据集请求的1 / 3,要求不小于1000

表2 vllm服务配置说明

参数

含义

类型

取值说明

host

服务IP

str

127.0.0.1

port

服务端口

str

8000

model

模型路径

str

/home/qwen3

served_model_name

模型名称

str

qwen3

others

其它服务参数

str

--dtype=auto --tensor-parallel-size=1 --block-size=128

MAX_NUM_SEQS

单batch最大请求个数

区间值

建议 32-512

MAX_NUM_BATCHED_TOKENS

单batch最大处理 token 个数

区间值

建议 8192-65536

注意:

  1. 6.5.908 版本图模型ascend_turbo_graph 开启时 MAX_NUM_SEQS 需固定为 128,暂不支持参数调优.
  2. others 参数间使用空格分隔,参数内部不能留有空格。开启图 ascend_turbo_graph 模型下参考:
     others = "--additional-config={\"ascend_turbo_graph_config\":{\"enabled\":true},\"ascend_scheduler_config\":{\"enabled\":true,\"chunked_prefill_enabled\":false}}"

表3 acs-bench配置说明

参数

含义

类型

dataset_type

数据类型

str

input_path

输出数据集路径

str

input_length

输入长度

int

output_length

输出长度

int

num_requests

请求总数

int

provider

服务配置yaml文件路径

str

concurrency

最大并发数

int

benchmark_save_path

评测结果存放位置

str

others

其它参数

str

当前 acs-bench 配置支持并发和爬坡模型,爬坡模式配置方式可在 others 参数中增加参数,参考:

others="--backend openai-chat --epochs 3 --warmup 2 --use-climb --climb-mode linear --growth-rate 5 --init-concurrency 1 --growth-interval 1000" 

更多 acs-bench 评测命令参考:大语言模型推理性能测试

执行参数寻优命令

使用 acsprof auto_tune 命令启动参数调优

 acsprof auto_tune -e vllm -b acsbench

参数说明如下表:

参数名称

类型

参数功能

可选

是否必选

-e / --engine

String

The engine used for model evaluation. (default: None)

vllm

-b / --benchmark_policy

String

使用的 benchmark 评测工具

acsbench

查看寻优结果

寻优结果默认会存储在脚本执行当前目录的 result/store/xxx.csv 文件下,也可以使用环境变量 MODEL_EVAL_STATE_CUSTOM_OUTPUT 来控制输出路径,如:

 export MODEL_EVAL_STATE_CUSTOM_OUTPUT=result/store/ 

最终输出结果如下表格:

依照 fitness 列进行从大到小排序,找到最小的 fitness 值对应行的超参数即为最优超参数。

相关文档