LLM Class#

class vllm.LLM(model: str, tokenizer: str | None = None, tokenizer_mode: str = 'auto', skip_tokenizer_init: bool = False, trust_remote_code: bool = False, tensor_parallel_size: int = 1, dtype: str = 'auto', quantization: str | None = None, revision: str | None = None, tokenizer_revision: str | None = None, seed: int = 0, gpu_memory_utilization: float = 0.9, swap_space: float = 4, cpu_offload_gb: float = 0, enforce_eager: bool | None = None, max_context_len_to_capture: int | None = None, max_seq_len_to_capture: int = 8192, disable_custom_all_reduce: bool = False, **kwargs)[source]#

An LLM for generating texts from given prompts and sampling parameters.

This class includes a tokenizer, a language model (possibly distributed across multiple GPUs), and GPU memory space allocated for intermediate states (aka KV cache). Given a batch of prompts and sampling parameters, this class generates texts from the model, using an intelligent batching mechanism and efficient memory management.

Parameters:
  • model – The name or path of a HuggingFace Transformers model.

  • tokenizer – The name or path of a HuggingFace Transformers tokenizer.

  • tokenizer_mode – The tokenizer mode. “auto” will use the fast tokenizer if available, and “slow” will always use the slow tokenizer.

  • skip_tokenizer_init – If true, skip initialization of tokenizer and detokenizer. Expect valid prompt_token_ids and None for prompt from the input.

  • trust_remote_code – Trust remote code (e.g., from HuggingFace) when downloading the model and tokenizer.

  • tensor_parallel_size – The number of GPUs to use for distributed execution with tensor parallelism.

  • dtype – The data type for the model weights and activations. Currently, we support float32, float16, and bfloat16. If auto, we use the torch_dtype attribute specified in the model config file. However, if the torch_dtype in the config is float32, we will use float16 instead.

  • quantization – The method used to quantize the model weights. Currently, we support “awq”, “gptq”, “squeezellm”, and “fp8” (experimental). If None, we first check the quantization_config attribute in the model config file. If that is None, we assume the model weights are not quantized and use dtype to determine the data type of the weights.

  • revision – The specific model version to use. It can be a branch name, a tag name, or a commit id.

  • tokenizer_revision – The specific tokenizer version to use. It can be a branch name, a tag name, or a commit id.

  • seed – The seed to initialize the random number generator for sampling.

  • gpu_memory_utilization – The ratio (between 0 and 1) of GPU memory to reserve for the model weights, activations, and KV cache. Higher values will increase the KV cache size and thus improve the model’s throughput. However, if the value is too high, it may cause out-of- memory (OOM) errors.

  • swap_space – The size (GiB) of CPU memory per GPU to use as swap space. This can be used for temporarily storing the states of the requests when their best_of sampling parameters are larger than 1. If all requests will have best_of=1, you can safely set this to 0. Otherwise, too small values may cause out-of-memory (OOM) errors.

  • cpu_offload_gb – The size (GiB) of CPU memory to use for offloading the model weights. This virtually increases the GPU memory space you can use to hold the model weights, at the cost of CPU-GPU data transfer for every forward pass.

  • enforce_eager – Whether to enforce eager execution. If True, we will disable CUDA graph and always execute the model in eager mode. If False, we will use CUDA graph and eager execution in hybrid.

  • max_context_len_to_capture – Maximum context len covered by CUDA graphs. When a sequence has context length larger than this, we fall back to eager mode (DEPRECATED. Use max_seq_len_to_capture instead).

  • max_seq_len_to_capture – Maximum sequence len covered by CUDA graphs. When a sequence has context length larger than this, we fall back to eager mode.

  • disable_custom_all_reduce – See ParallelConfig

  • **kwargs – Arguments for EngineArgs. (See Engine Arguments)

Note

This class is intended to be used for offline inference. For online serving, use the AsyncLLMEngine class instead.

DEPRECATE_LEGACY: ClassVar[bool] = False#

A flag to toggle whether to deprecate the legacy generate/encode API.

chat(messages: List[ChatCompletionSystemMessageParam | ChatCompletionUserMessageParam | ChatCompletionAssistantMessageParam | ChatCompletionToolMessageParam | ChatCompletionFunctionMessageParam | CustomChatCompletionMessageParam], sampling_params: SamplingParams | List[SamplingParams] | None = None, use_tqdm: bool = True, lora_request: LoRARequest | None = None, chat_template: str | None = None, add_generation_prompt: bool = True) List[RequestOutput][source]#

Generates responses for chat messages.

Converts the messages to prompts using the tokenizer and calls the generate() method to generate the responses.

Parameters:
  • messages – A list of messages to generate responses for. Each message is a list of dictionaries with ‘role’ and ‘content’ keys.

  • sampling_params – The sampling parameters for text generation. If None, we use the default sampling parameters. When it is a single value, it is applied to every prompt. When it is a list, the list must have the same length as the prompts and it is paired one by one with the prompt.

  • use_tqdm – Whether to use tqdm to display the progress bar.

  • lora_request – LoRA request to use for generation, if any.

  • chat_template – The template to use for structuring the chat. If not provided, the model’s default chat template will be used.

  • add_generation_prompt – If True, adds a generation template to each message.

Returns:

A list of RequestOutput objects containing the generated responses in the same order as the input messages.

encode(prompts: str, pooling_params: PoolingParams | Sequence[PoolingParams] | None = None, prompt_token_ids: List[int] | None = None, use_tqdm: bool = True, lora_request: List[LoRARequest] | LoRARequest | None = None) List[EmbeddingRequestOutput][source]#
encode(prompts: List[str], pooling_params: PoolingParams | Sequence[PoolingParams] | None = None, prompt_token_ids: List[List[int]] | None = None, use_tqdm: bool = True, lora_request: List[LoRARequest] | LoRARequest | None = None) List[EmbeddingRequestOutput]
encode(prompts: str | None = None, pooling_params: PoolingParams | Sequence[PoolingParams] | None = None, *, prompt_token_ids: List[int], use_tqdm: bool = True, lora_request: List[LoRARequest] | LoRARequest | None = None) List[EmbeddingRequestOutput]
encode(prompts: List[str] | None = None, pooling_params: PoolingParams | Sequence[PoolingParams] | None = None, *, prompt_token_ids: List[List[int]], use_tqdm: bool = True, lora_request: List[LoRARequest] | LoRARequest | None = None) List[EmbeddingRequestOutput]
encode(prompts: None, pooling_params: None, prompt_token_ids: List[int] | List[List[int]], use_tqdm: bool = True, lora_request: List[LoRARequest] | LoRARequest | None = None) List[EmbeddingRequestOutput]
encode(inputs: PromptInputs | Sequence[PromptInputs], /, *, pooling_params: PoolingParams | Sequence[PoolingParams] | None = None, use_tqdm: bool = True, lora_request: List[LoRARequest] | LoRARequest | None = None) List[EmbeddingRequestOutput]

Generates the completions for the input prompts.

This class automatically batches the given prompts, considering the memory constraint. For the best performance, put all of your prompts into a single list and pass it to this method.

Parameters:
  • inputs – The inputs to the LLM. You may pass a sequence of inputs for batch inference. See PromptInputs for more details about the format of each input.

  • pooling_params – The pooling parameters for pooling. If None, we use the default pooling parameters.

  • use_tqdm – Whether to use tqdm to display the progress bar.

  • lora_request – LoRA request to use for generation, if any.

  • prompt_adapter_request – Prompt Adapter request to use for generation, if any.

Returns:

A list of EmbeddingRequestOutput objects containing the generated embeddings in the same order as the input prompts.

Note

Using prompts and prompt_token_ids as keyword parameters is considered legacy and may be deprecated in the future. You should instead pass them via the inputs parameter.

generate(prompts: str, sampling_params: SamplingParams | List[SamplingParams] | None = None, prompt_token_ids: List[int] | None = None, use_tqdm: bool = True, lora_request: List[LoRARequest] | LoRARequest | None = None) List[RequestOutput][source]#
generate(prompts: List[str], sampling_params: SamplingParams | List[SamplingParams] | None = None, prompt_token_ids: List[List[int]] | None = None, use_tqdm: bool = True, lora_request: List[LoRARequest] | LoRARequest | None = None) List[RequestOutput]
generate(prompts: str | None = None, sampling_params: SamplingParams | List[SamplingParams] | None = None, *, prompt_token_ids: List[int], use_tqdm: bool = True, lora_request: List[LoRARequest] | LoRARequest | None = None) List[RequestOutput]
generate(prompts: List[str] | None = None, sampling_params: SamplingParams | List[SamplingParams] | None = None, *, prompt_token_ids: List[List[int]], use_tqdm: bool = True, lora_request: List[LoRARequest] | LoRARequest | None = None) List[RequestOutput]
generate(prompts: None, sampling_params: None, prompt_token_ids: List[int] | List[List[int]], use_tqdm: bool = True, lora_request: List[LoRARequest] | LoRARequest | None = None) List[RequestOutput]
generate(inputs: PromptInputs | Sequence[PromptInputs], /, *, sampling_params: SamplingParams | Sequence[SamplingParams] | None = None, use_tqdm: bool = True, lora_request: List[LoRARequest] | LoRARequest | None = None) List[RequestOutput]

Generates the completions for the input prompts.

This class automatically batches the given prompts, considering the memory constraint. For the best performance, put all of your prompts into a single list and pass it to this method.

Parameters:
  • inputs – A list of inputs to generate completions for.

  • sampling_params – The sampling parameters for text generation. If None, we use the default sampling parameters. When it is a single value, it is applied to every prompt. When it is a list, the list must have the same length as the prompts and it is paired one by one with the prompt.

  • use_tqdm – Whether to use tqdm to display the progress bar.

  • lora_request – LoRA request to use for generation, if any.

  • prompt_adapter_request – Prompt Adapter request to use for generation, if any.

Returns:

A list of RequestOutput objects containing the generated completions in the same order as the input prompts.

Note

Using prompts and prompt_token_ids as keyword parameters is considered legacy and may be deprecated in the future. You should instead pass them via the inputs parameter.