Skip to main content
Version: Next

*在线运行 vLLM 入门教程:零基础分步指南

LLMEngine

*class***vllm.**LLMEngine**(*vllmconfig: VllmConfig***,***executor_class:**Type[ExecutorBase]*,*log_stats:*bool,*usage_context: UsageContext**=* *UsageContext.ENGINE_CONTEXT*,*stat_loggers:Dict[str, StatLoggerBase] |None =* *None*,*input_registry: InputRegistry**=* *INPUT_REGISTRY*,*mm_registry:MultiModalRegistry =* *MULTIMODAL_REGISTRY*,*use_cached_outputs:bool _=* *False***)**

[source]

一个接收请求并生成文本的 LLM 引擎。

这是 vLLM 引擎的主要类。它接收来自客户端的请求,并从 LLM 生成文本。它包括一个分词器、一个语言模型(可能分布在多个 GPU 上),以及为中间状态(即 KV 缓存)分配的 GPU 内存空间。该类利用迭代级调度和高效的内存管理来最大化服务吞吐量。

LLM 类包装了该类以进行离线批量推理,而 AsyncLLMEngine 类包装了该类以进行在线服务。

配置参数源自 EngineArgs。(参见引擎参数

参数:

  • model_config – 与 LLM 模型相关的配置。

  • cache_config – 与 KV 缓存内存管理相关的配置。

  • parallel_config – 与分布式执行相关的配置。

  • scheduler_config – 与请求调度器相关的配置。

  • device_config – 与设备相关的配置。

  • lora_config (可选) – 与多 LoRA 服务相关的配置。

  • speculative_config (可选) – 与推测解码相关的配置。

  • executor_class – 用于管理分布式执行的模型执行器类。

  • prompt_adapter_config (可选) – 与提示适配器服务相关的配置。

  • log_stats – 是否记录统计信息。

  • usage_context – 指定的入口点,用于使用信息收集。

DO_VALIDATE_OUTPUT***:ClassVar[bool] = False***

[source]

标志,用于切换是否验证请求输出的类型。

abort_request**(*requestid:**str|Iterable[str_]*)**→**None

[source]

中止具有给定 ID 的请求。

参数:

request_id – 要中止的请求的 ID(s)。

详细信息:

  • 请参阅 Scheduler 类中的 abort_seq_group()

示例

>>> # initialize engine and add a request with request_id
>>> # 初始化引擎并添加一个带有 request_id 的请求
>>> request_id = str(0)
>>> # abort the request
>>> # 中止请求
>>> engine.abort_request(request_id)

add_request**(*requestid:*str,*prompt: PromptType***,***params:**SamplingParams|*PoolingParams,*arrival_time:float|None =* *None*,*lora_request: LoRARequest |None =* *None*,*trace_headers: Mapping[str,str] |None =* *None*,*prompt_adapter_request: PromptAdapterRequest |None =* *None*,*priority:int _=* *0*)**→**None

[source]

add_request**(*requestid:*str,*******,*inputs: PromptType***,***params:**SamplingParams|*PoolingParams,*arrival_time:float|None =* *None*,*lora_request: LoRARequest |None =* *None*,*trace_headers: Mapping[str,str] |None =* *None*,*prompt_adapter_request: PromptAdapterRequest |None =* *None*,*priority:int _=* *0*)**→**None

将请求添加到引擎的请求池中。

请求被添加到请求池中,并将在调用 engine.step() 时由调度器处理。具体的调度策略由调度器决定。

参数:

  • request_id – 请求的唯一 ID。

  • prompt – 提供给 LLM 的提示。有关每个输入格式的更多详细信息,请参阅 PromptType

  • params – 采样或池化的参数。SamplingParams 用于文本生成。PoolingParams 用于池化。

  • arrival_time – 请求的到达时间。如果为 None,则使用当前的单调时间。

  • lora_request – 要添加的 LoRA 请求。

  • trace_headers – OpenTelemetry 跟踪头。

  • prompt_adapter_request – 要添加的提示适配器请求。

  • priority – 请求的优先级。仅适用于优先级调度。

详细信息:

  • 如果 arrival_time 为 None,则将其设置为当前时间。

  • 如果 prompt_token_ids 为 None,则将其设置为编码后的提示。

  • 创建 n 个 Sequence 对象。

  • 从 Sequence 列表中创建一个 SequenceGroup 对象。

  • 将 SequenceGroup 对象添加到调度器中。

示例

>>> # initialize engine
>>> # 初始化引擎
>>> engine = LLMEngine.from_engine_args(engine_args)
>>> # set request arguments
>>> # 设置请求参数
>>> example_prompt = "Who is the president of the United States?"
>>> sampling_params = SamplingParams(temperature=0.0)
>>> request_id = 0
>>>
>>> # add the request to the engine
>>> # 将请求添加到引擎
>>> engine.add_request(
>>> str(request_id),
>>> example_prompt,
>>> SamplingParams(temperature=0.0))
>>> # continue the request processing
>>> # 继续请求处理
>>> ...

do_log_stats**(*scheduleroutputs: SchedulerOutputs |**None =* *None*,*model_output:List[SamplerOutput] |None =* *None*,*finished_before:List[int] |None =* *None*,*skip:List[int] |None _=* *None*)**→**None

[source]

在没有活动请求时强制记录日志。

*classmethod***fromengine_args**(***engine_args: EngineArgs***,*usage_context: UsageContext**=* *UsageContext.ENGINE_CONTEXT*,***stat_loggers:**Dict[str, StatLoggerBase] |None _=* *None*)**→**LLMEngine

[source]

从引擎参数创建 LLM 引擎。

get_decoding_config**()**→ DecodingConfig

[source]

获取解码配置。

get_lora_config**()**→ LoRAConfig

[source]

获取 LoRA 配置。

get_model_config**()**→ ModelConfig

[source]

获取模型配置。

get_num_unfinished_requests**()**→int

[source]

获取未完成请求的数量。

get_parallel_config**()**→ ParallelConfig

[source]

获取并行配置。

get_scheduler_config**()**→ SchedulerConfig

[source]

获取调度器配置。

has_unfinished_requests**()**→bool

[source]

如果有未完成的请求,则返回 True。

has_unfinished_requests_for_virtual_engine**(*virtual_engine:*int)**→bool

[source]

如果虚拟引擎有未完成的请求,则返回 True。

reset_prefix_cache**()**→bool

[source]

重置所有设备的前缀缓存。

step**()**→List[RequestOutput | PoolingRequestOutput]

[source]

执行一次解码迭代并返回新生成的结果。

图片

step 函数的概述。

详细信息:

  • 步骤 1:调度要在下一次迭代中执行的序列以及要交换/复制出的 token 块。

    • 根据调度策略,序列可能会被抢占/重新排序。

    • 序列组(SG)指的是从同一提示生成的一组序列。

  • 步骤 2:调用分布式执行器来执行模型。

  • 步骤 3:处理模型输出。主要包括:

    • 解码相关输出。

    • 根据其采样参数(是否使用 beam_search)更新调度的序列组。

    • 释放已完成的序列组。

  • 最后,创建并返回新生成的结果。

示例

>>> # Please see the example/ folder for more detailed examples.
>>> # 请参阅 example/ 文件夹以获取更详细的示例。
>>>
>>> # initialize engine and request arguments
>>> # 初始化引擎和请求参数
>>> engine = LLMEngine.from_engine_args(engine_args)
>>> example_inputs = [(0, "What is LLM?",
>>> SamplingParams(temperature=0.0))]
>>>
>>> # Start the engine with an event loop
>>> # 启动引擎并进入事件循环
>>> while True:
>>> if example_inputs:
>>> req_id, prompt, sampling_params = example_inputs.pop(0)
>>> engine.add_request(str(req_id),prompt,sampling_params)
>>>
>>> # continue the request processing
>>> # 继续请求处理
>>> request_outputs = engine.step()
>>> for request_output in request_outputs:
>>> if request_output.finished:
>>> # return or show the request output
>>> # 返回或显示请求输出
>>>
>>> if not (engine.has_unfinished_requests() or example_inputs):
>>> break