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: int = 4, enforce_eager: bool = False, 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.

NOTE: This class is intended to be used for offline inference. For online serving, use the AsyncLLMEngine class instead. NOTE: For the comprehensive list of arguments, see EngineArgs.

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.

  • 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

encode(prompts: str | List[str] | None = None, pooling_params: PoolingParams | List[PoolingParams] | None = None, prompt_token_ids: List[List[int]] | None = None, use_tqdm: bool = True, lora_request: LoRARequest | None = None, multi_modal_data: MultiModalData | None = None) List[EmbeddingRequestOutput][source]#

Generates the completions for the input prompts.

NOTE: 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:
  • prompts – A list of prompts to generate completions for.

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

  • prompt_token_ids – A list of token IDs for the prompts. If None, we use the tokenizer to convert the prompts to token IDs.

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

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

  • multi_modal_data – Multi modal data.

Returns:

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

generate(prompts: str | List[str] | None = None, sampling_params: SamplingParams | List[SamplingParams] | None = None, prompt_token_ids: List[List[int]] | None = None, use_tqdm: bool = True, lora_request: LoRARequest | None = None, multi_modal_data: MultiModalData | None = None) List[RequestOutput][source]#

Generates the completions for the input prompts.

NOTE: 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:
  • prompts – A list of prompts 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.

  • prompt_token_ids – A list of token IDs for the prompts. If None, we use the tokenizer to convert the prompts to token IDs.

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

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

  • multi_modal_data – Multi modal data.

Returns:

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