LLMEngine#

class vllm.LLMEngine(vllm_config: 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(request_id: str | Iterable[str]) None[source]#

中止具有给定 ID 的请求。

参数:

request_id – 要中止的请求的 ID。

详情
  • 请参考 abort_seq_group(),来自类 Scheduler

示例

>>> # initialize engine and add a request with request_id
>>> request_id = str(0)
>>> # abort the request
>>> engine.abort_request(request_id)
add_request(request_id: 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(request_id: 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,则将其设置为编码后的提示。

  • 创建 nSequence 对象。

  • 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(scheduler_outputs: SchedulerOutputs | None = None, model_output: List[SamplerOutput] | None = None, finished_before: List[int] | None = None, skip: List[int] | None = None) None[source]#

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

classmethod from_engine_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]#

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

Overview of the step function

step 函数的概述。#

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

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

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

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

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

    • 解码相关的输出。

    • 根据计划序列组的采样参数(是否use_beam_search),使用模型输出更新它们。

    • 释放已完成的序列组。

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

示例

>>> # Please see the example/ folder for more detailed examples.
>>>
>>> # 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