Offline API

最近更新时间: 2026-06-30 15:06:00

LLM

LLM 构造参数

class taco_llm.LLM(
    model: str,
    tokenizer: Optional[str] = None,
    tokenizer_mode: str = "auto",
    skip_tokenizer_init: bool = False,
    trust_remote_code: bool = False,
    tensor_parallel_size: int = 1,
    dtype: str = "auto",
    quantization: Optional[str] = None,
    revision: Optional[str] = None,
    tokenizer_revision: Optional[str] = None,
    seed: int = 0,
    gpu_memory_utilization: float = 0.9,
    swap_space: float = 4,
    cpu_offload_gb: float = 0,
    enforce_eager: Optional[bool] = None,
    max_context_len_to_capture: Optional[int] = None,
    max_seq_len_to_capture: int = 8192,
    disable_custom_all_reduce: bool = False,
    disable_async_output_proc: bool = False,
    **kwargs,
)
    """
    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.
    
    Args:
        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", 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 :class:taco_llm.EngineArgs.
    
    Note:
        This class is intended to be used for offline inference. For online
        serving, use the :class:taco_llm.AsyncLLMEngine class instead.
    """
    

TACO-LLM 支持离线和在线两种模式,这两种模式的参数配置是一致的。因此,除了上述明确提到的参数外,您还可以设置任意 TACO-LLM 在线模式支持的参数。完整的参数配置请参见 Online API 章节。

chat 接口

def chat(
    self,
    messages: List[ChatCompletionMessageParam],
    sampling_params: Optional[Union[SamplingParams,
                                    List[SamplingParams]]] = None,
    use_tqdm: bool = True,
    lora_request: Optional[LoRARequest] = None,
    chat_template: Optional[str] = None,
    add_generation_prompt: bool = True,
) -> List[RequestOutput]:
    """
    Generate responses for a chat conversation.

    The chat conversation is converted into a text prompt using the
    tokenizer and calls the :meth:`generate` method to generate the
    responses.

    Multi-modal inputs can be passed in the same way you would pass them
    to the OpenAI API.

    Args:
        messages: A single conversation represented as a list of messages.
            Each message is a dictionary 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.
    """

generate 接口

def generate(
    self,
    prompts: Union[Union[PromptInputs, Sequence[PromptInputs]],
                   Optional[Union[str, List[str]]]] = None,
    sampling_params: Optional[Union[SamplingParams,
                                    Sequence[SamplingParams]]] = None,
    prompt_token_ids: Optional[Union[List[int], List[List[int]]]] = None,
    use_tqdm: bool = True,
    lora_request: Optional[Union[List[LoRARequest], LoRARequest]] = None,
    prompt_adapter_request: Optional[PromptAdapterRequest] = None,
    guided_options_request: Optional[Union[LLMGuidedOptions,
                                           GuidedDecodingRequest]] = 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.

    Args:
        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.
    """