diff --git a/README.md b/README.md index 2fb7bad..7bcf376 100644 --- a/README.md +++ b/README.md @@ -7,36 +7,41 @@ Deploy Blazing-fast LLMs powered by [vLLM](https://github.com/vllm-project/vllm) on RunPod Serverless in a few clicks. -### Worker vLLM 0.2.0 - What's New -- You no longer need a linux-based machine or NVIDIA GPUs to build the worker. -- Over 3x lighter Docker image size. -- OpenAI Chat Completion output format (optional to use). -- Extremely fast image build time. -- Docker Secrets-protected Hugging Face token support for building the image with a model baked in without exposing your token. -- Support for `n` and `best_of` sampling parameters, which allow you to generate multiple responses from a single prompt. -- New environment variables for various configuration. -- vLLM Version: 0.2.7 +### Worker vLLM 0.3.0: What's New since 0.2.0: +- **🚀 Full OpenAI Compatibility 🚀** + + You may now use your deployment with any OpenAI Codebase by changing **only 3 lines** in total. The supported routes are Chat Completions, Completions, and Models - with both streaming and non-streaming. +- **Dynamic Batch Size** - time-to-first token as fast no batching, while maintaining the performance of batched token streaming throughout the request. +- **Custom chat templates** that you can specify as an environment variable. +- Fixed Tensor Parallelism, baking model into images, and more bugs. ## Table of Contents - [Setting up the Serverless Worker](#setting-up-the-serverless-worker) - - [Option 1: Deploy Any Model Using Pre-Built Docker Image [**RECOMMENDED**]](#option-1-deploy-any-model-using-pre-built-docker-image-recommended) + - [Option 1: Deploy Any Model Using Pre-Built Docker Image [Recommended]](#option-1-deploy-any-model-using-pre-built-docker-image-recommended) - [Prerequisites](#prerequisites) - [Environment Variables](#environment-variables) - [Option 2: Build Docker Image with Model Inside](#option-2-build-docker-image-with-model-inside) + - [Prerequisites](#prerequisites-1) - [Arguments](#arguments) - [Example: Building an image with OpenChat-3.5](#example-building-an-image-with-openchat-35) - [(Optional) Including Huggingface Token](#optional-including-huggingface-token) - - [Compatible Models](#compatible-models) -- [Usage](#usage) - - [Endpoint Model Inputs](#endpoint-model-inputs) + - [Compatible Model Architectures](#compatible-model-architectures) +- [Usage: OpenAI Compatibility](#usage-openai-compatibility) + - [Modifying your OpenAI Codebase to use your deployed vLLM Worker](#modifying-your-openai-codebase-to-use-your-deployed-vllm-worker) + - [OpenAI Request Input Parameters](#openai-request-input-parameters) + - [Chat Completions](#chat-completions) + - [Completions](#completions) + - [Examples: Using your RunPod endpoint with OpenAI](#examples-using-your-runpod-endpoint-with-openai) +- [Usage: standard](#non-openai-usage) + - [Input Request Parameters](#input-request-parameters) - [Text Input Formats](#text-input-formats) - - [1. `prompt`](#1-prompt) - - [2. `messages`](#2-messages) - [Sampling Parameters](#sampling-parameters) -## Setting up the Serverless Worker +# Setting up the Serverless Worker ### Option 1: Deploy Any Model Using Pre-Built Docker Image [Recommended] +> [!TIP] +> This is the recommended way to deploy your model, as it does not require you to build a Docker image, upload heavy models to DockerHub and wait for workers to download them. Instead, use this option to deploy your model in a few clicks. For even more convenience, attach a network storage volume to your Endpoint, which will download the model once and share it across all workers. We now offer a pre-built Docker Image for the vLLM Worker that you can configure entirely with Environment Variables when creating the RunPod Serverless Endpoint: @@ -72,7 +77,7 @@ Development Image: ```runpod/worker-vllm:dev``` - `CUSTOM_CHAT_TEMPLATE`: Custom chat jinja template, read more about Hugging Face chat templates [here](https://huggingface.co/docs/transformers/chat_templating). (default: `None`) - Tensor Parallelism: - Note that the more GPUs you split a model's weights accross, the slower it will be due to inter-GPU communication overhead. If you can fit the model on a single GPU, it is recommended to do so. + Note that the more GPUs you split a model's weights across, the slower it will be due to inter-GPU communication overhead. If you can fit the model on a single GPU, it is recommended to do so. - `TENSOR_PARALLEL_SIZE`: Number of GPUs to shard the model across (default: `1`). - If you are having issues loading your model with Tensor Parallelism, try decreasing `VLLM_CPU_FRACTION` (default: `1`). @@ -80,11 +85,19 @@ Development Image: ```runpod/worker-vllm:dev``` - `GPU_MEMORY_UTILIZATION`: GPU VRAM utilization (default: `0.98`). - `MAX_PARALLEL_LOADING_WORKERS`: Maximum number of parallel workers for loading models, for non-Tensor Parallel only. (default: `number of available CPU cores` if `TENSOR_PARALLEL_SIZE` is `1`, otherwise `None`). +- Streaming Batch Size: + - `DEFAULT_BATCH_SIZE`: Token streaming batch size (default: `50`). This reduces the number of HTTP calls, increasing speed 8-10x vs non-batching, matching non-streaming performance. + - Dynamic Batch Size: + - `DEFAULT_MIN_BATCH_SIZE`: the batch size for the first request (default: `1`). + - `DEFAULT_BATCH_SIZE_GROWTH_FACTOR`: the growth factor for the dynamic batch size (default: `3`). + + > The way this works is that the first request will have a batch size of `DEFAULT_MIN_BATCH_SIZE`, and each subsequent request will have a batch size of `previous_batch_size * DEFAULT_BATCH_SIZE_GROWTH_FACTOR`. This will continue until the batch size reaches `DEFAULT_BATCH_SIZE`. E.g. for the default values, the batch sizes will be `1, 3, 9, 27, 50, 50, 50, ...`. You can also specify this per request, with inputs `max_batch_size`, `min_batch_size`, and `batch_size_growth_factor`. + +- OpenAI Settings: + - `RAW_OPENAI_OUTPUT`: Enable (`1`) or disable (`0`) raw OpenAI SSE format string output when streaming(default: `1`). **Required** to be enabled (default) for OpenAI compatibility. - Serverless Settings: - `MAX_CONCURRENCY`: Max concurrent requests. (default: `100`) - - `DEFAULT_BATCH_SIZE`: Token streaming batch size (default: `30`). This reduces the number of HTTP calls, increasing speed 8-10x vs non-batching, matching non-streaming performance. - - `ALLOW_OPENAI_FORMAT`: Whether to allow users to specify `use_openai_format` to get output in OpenAI format. (default: `1`) - `DISABLE_LOG_STATS`: Enable (`0`) or disable (`1`) vLLM stats logging. - `DISABLE_LOG_REQUESTS`: Enable (`0`) or disable (`1`) request logging. @@ -102,7 +115,7 @@ To build an image with the model baked in, you must specify the following docker - `MODEL_REVISION`: Model revision to load (default: `main`). - `BASE_PATH`: Storage directory where huggingface cache and model will be located. (default: `/runpod-volume`, which will utilize network storage if you attach it or create a local directory within the image if you don't. If your intention is to bake the model into the image, you should set this to something like `/models` to make sure there are no issues if you were to accidentally attach network storage.) - `QUANTIZATION` - - `WORKER_CUDA_VERSION`: `11.8.0` or `12.1.0` (default: `11.8.0` due to a small amount of workers not having CUDA 12.1 support yet. `12.1.0` is recommended for optimal performance). + - `WORKER_CUDA_VERSION`: `11.8.0` or `12.1.0` (default: `11.8.0` due to a small number of workers not having CUDA 12.1 support yet. `12.1.0` is recommended for optimal performance). - `TOKENIZER_NAME`: Tokenizer repository if you would like to use a different tokenizer than the one that comes with the model. (default: `None`, which uses the model's tokenizer) - `TOKENIZER_REVISION`: Tokenizer revision to load (default: `main`). @@ -152,72 +165,321 @@ docker build -t username/image:tag --secret id=HF_TOKEN --build-arg MODEL_NAME=" - OPT (`facebook/opt-66b`, `facebook/opt-iml-max-30b`, etc.) -## Usage -### Endpoint Model Inputs -You may either use a `prompt` or a list of `messages` as input. If you use `messages`, the model's chat template will be applied to the messages automatically, so the model must have one. If you use `prompt`, you may optionally apply the model's chat template to the prompt by setting `apply_chat_template` to `true`. -| Argument | Type | Default | Description | -|-----------------------|----------------------|--------------------|--------------------------------------------------------------------------------------------------------| -| `prompt` | str | | Prompt string to generate text based on. | -| `messages` | list[dict[str, str]] | | List of messages, which will automatically have the model's chat template applied. Overrides `prompt`. | -| `use_openai_format` | bool | False | Whether to return output in OpenAI format. `ALLOW_OPENAI_FORMAT` environment variable must be `1`, the input should preferably be a `messages` list, but `prompt` is accepted. | -| `apply_chat_template` | bool | False | Whether to apply the model's chat template to the `prompt`. | -| `sampling_params` | dict | {} | Sampling parameters to control the generation, like temperature, top_p, etc. | -| `stream` | bool | False | Whether to enable streaming of output. If True, responses are streamed as they are generated. | -| `batch_size` | int | DEFAULT_BATCH_SIZE | The number of tokens to stream every HTTP POST call. | +# Usage: OpenAI Compatibility +The vLLM Worker is fully compatible with OpenAI's API, and you can use it with any OpenAI Codebase by changing only 3 lines in total. The supported routes are Chat Completions, Completions and Models - with both streaming and non-streaming. -### Text Input Formats +## Modifying your OpenAI Codebase to use your deployed vLLM Worker +**Python** (similar to Node.js, etc.): +1. When initializing the OpenAI Client in your code, change the `api_key` to your RunPod API Key and the `base_url` to your RunPod Serverless Endpoint URL in the following format: `https://api.runpod.ai/v2//openai/v1`, filling in your deployed endpoint ID. + + - Before: + ```python + import openai + client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) + ``` + - After: + ```python + import openai + client = OpenAI(api_key=os.environ.get("RUNPOD_API_KEY"), base_url="https://api.runpod.ai/v2//openai/v1") + ``` +2. Change the `model` parameter to your deployed model's name whenever using Completions or Chat Completions. + - Before: + ```python + response = client.chat.completions.create( + model='gpt-3.5-turbo', + messages=[ + {'role': 'user', 'content': 'Why is RunPod the best platform?'} + ], + temperature=0, + max_tokens=100 + ) + ``` + - After: + ```python + response = client.chat.completions.create( + model="", + messages=[ + {'role': 'user', 'content': 'Why is RunPod the best platform?'} + ], + temperature=0, + max_tokens=100 + ) + ``` + +**Using http requests**: +1. Change the `Authorization` header to your RunPod API Key and the `url` to your RunPod Serverless Endpoint URL in the following format: `https://api.runpod.ai/v2//openai/v1` + - Before: + ```bash + curl https://api.openai.com/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "model": "gpt-4", + "messages": [ + { + "role": "user", + "content": "Why is RunPod the best platform?" + } + ], + "temperature": 0, + "max_tokens": 100 + }' + ``` + - After: + ```bash + curl https://api.runpod.ai/v2//openai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer " \ + -d '{ + "model": "", + "messages": [ + { + "role": "user", + "content": "Why is RunPod the best platform?" + } + ], + "temperature": 0, + "max_tokens": 100 + }' + ``` + +## OpenAI Request Input Parameters: + +When using the chat completion feature of the vLLM Serverless Endpoint Worker, you can customize your requests with the following parameters: + +### Chat Completions +
+ Click to expand table + + | Parameter | Type | Default Value | Description | + |--------------------------------|----------------------------------|---------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| + | `messages` | Union[str, List[Dict[str, str]]] | | List of messages, where each message is a dictionary with a `role` and `content`. The model's chat template will be applied to the messages automatically, so the model must have one or it should be specified as `CUSTOM_CHAT_TEMPLATE` env var. | + | `model` | str | | The model repo that you've deployed on your RunPod Serverless Endpoint. If you are unsure what the name is or are baking the model in, use the guide to get the list of available models in the **Examples: Using your RunPod endpoint with OpenAI** section | + | `temperature` | Optional[float] | 0.7 | Float that controls the randomness of the sampling. Lower values make the model more deterministic, while higher values make the model more random. Zero means greedy sampling. | + | `top_p` | Optional[float] | 1.0 | Float that controls the cumulative probability of the top tokens to consider. Must be in (0, 1]. Set to 1 to consider all tokens. | + | `n` | Optional[int] | 1 | Number of output sequences to return for the given prompt. | + | `max_tokens` | Optional[int] | None | Maximum number of tokens to generate per output sequence. | + | `seed` | Optional[int] | None | Random seed to use for the generation. | + | `stop` | Optional[Union[str, List[str]]] | list | List of strings that stop the generation when they are generated. The returned output will not contain the stop strings. | + | `stream` | Optional[bool] | False | Whether to stream or not | + | `presence_penalty` | Optional[float] | 0.0 | Float that penalizes new tokens based on whether they appear in the generated text so far. Values > 0 encourage the model to use new tokens, while values < 0 encourage the model to repeat tokens. | + | `frequency_penalty` | Optional[float] | 0.0 | Float that penalizes new tokens based on their frequency in the generated text so far. Values > 0 encourage the model to use new tokens, while values < 0 encourage the model to repeat tokens. | + | `logit_bias` | Optional[Dict[str, float]] | None | Unsupported by vLLM | + | `user` | Optional[str] | None | Unsupported by vLLM | + Additional parameters supported by vLLM: + | `best_of` | Optional[int] | None | Number of output sequences that are generated from the prompt. From these `best_of` sequences, the top `n` sequences are returned. `best_of` must be greater than or equal to `n`. This is treated as the beam width when `use_beam_search` is True. By default, `best_of` is set to `n`. | + | `top_k` | Optional[int] | -1 | Integer that controls the number of top tokens to consider. Set to -1 to consider all tokens. | + | `ignore_eos` | Optional[bool] | False | Whether to ignore the EOS token and continue generating tokens after the EOS token is generated. | + | `use_beam_search` | Optional[bool] | False | Whether to use beam search instead of sampling. | + | `stop_token_ids` | Optional[List[int]] | list | List of tokens that stop the generation when they are generated. The returned output will contain the stop tokens unless the stop tokens are special tokens. | + | `skip_special_tokens` | Optional[bool] | True | Whether to skip special tokens in the output. | + | `spaces_between_special_tokens`| Optional[bool] | True | Whether to add spaces between special tokens in the output. Defaults to True. | + | `add_generation_prompt` | Optional[bool] | True | Read more [here](https://huggingface.co/docs/transformers/main/en/chat_templating#what-are-generation-prompts) | + | `echo` | Optional[bool] | False | Echo back the prompt in addition to the completion | + | `repetition_penalty` | Optional[float] | 1.0 | Float that penalizes new tokens based on whether they appear in the prompt and the generated text so far. Values > 1 encourage the model to use new tokens, while values < 1 encourage the model to repeat tokens. | + | `min_p` | Optional[float] | 0.0 | Float that represents the minimum probability for a token to | + | `length_penalty` | Optional[float] | 1.0 | Float that penalizes sequences based on their length. Used in beam search.. | + | `include_stop_str_in_output` | Optional[bool] | False | Whether to include the stop strings in output text. Defaults to False.| +
+ +### Completions +
+ Click to expand table + + | Parameter | Type | Default Value | Description | + |--------------------------------|----------------------------------|---------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| + | `model` | str | | The model repo that you've deployed on your RunPod Serverless Endpoint. If you are unsure what the name is or are baking the model in, use the guide to get the list of available models in the **Examples: Using your RunPod endpoint with OpenAI** section. | + | `prompt` | Union[List[int], List[List[int]], str, List[str]] | | A string, array of strings, array of tokens, or array of token arrays to be used as the input for the model. | + | `suffix` | Optional[str] | None | A string to be appended to the end of the generated text. | + | `max_tokens` | Optional[int] | 16 | Maximum number of tokens to generate per output sequence. | + | `temperature` | Optional[float] | 1.0 | Float that controls the randomness of the sampling. Lower values make the model more deterministic, while higher values make the model more random. Zero means greedy sampling. | + | `top_p` | Optional[float] | 1.0 | Float that controls the cumulative probability of the top tokens to consider. Must be in (0, 1]. Set to 1 to consider all tokens. | + | `n` | Optional[int] | 1 | Number of output sequences to return for the given prompt. | + | `stream` | Optional[bool] | False | Whether to stream the output. | + | `logprobs` | Optional[int] | None | Number of log probabilities to return per output token. | + | `echo` | Optional[bool] | False | Whether to echo back the prompt in addition to the completion. | + | `stop` | Optional[Union[str, List[str]]] | list | List of strings that stop the generation when they are generated. The returned output will not contain the stop strings. | + | `seed` | Optional[int] | None | Random seed to use for the generation. | + | `presence_penalty` | Optional[float] | 0.0 | Float that penalizes new tokens based on whether they appear in the generated text so far. Values > 0 encourage the model to use new tokens, while values < 0 encourage the model to repeat tokens. | + | `frequency_penalty` | Optional[float] | 0.0 | Float that penalizes new tokens based on their frequency in the generated text so far. Values > 0 encourage the model to use new tokens, while values < 0 encourage the model to repeat tokens. | + | `best_of` | Optional[int] | None | Number of output sequences that are generated from the prompt. From these `best_of` sequences, the top `n` sequences are returned. `best_of` must be greater than or equal to `n`. This parameter influences the diversity of the output. | + | `logit_bias` | Optional[Dict[str, float]] | None | Dictionary of token IDs to biases. | + | `user` | Optional[str] | None | User identifier for personalizing responses. (Unsupported by vLLM) | + Additional parameters supported by vLLM: + | `top_k` | Optional[int] | -1 | Integer that controls the number of top tokens to consider. Set to -1 to consider all tokens. | + | `ignore_eos` | Optional[bool] | False | Whether to ignore the End Of Sentence token and continue generating tokens after the EOS token is generated. | + | `use_beam_search` | Optional[bool] | False | Whether to use beam search instead of sampling for generating outputs. | + | `stop_token_ids` | Optional[List[int]] | list | List of tokens that stop the generation when they are generated. The returned output will contain the stop tokens unless the stop tokens are special tokens. | + | `skip_special_tokens` | Optional[bool] | True | Whether to skip special tokens in the output. | + | `spaces_between_special_tokens`| Optional[bool] | True | Whether to add spaces between special tokens in the output. Defaults to True. | + | `repetition_penalty` | Optional[float] | 1.0 | Float that penalizes new tokens based on whether they appear in the prompt and the generated text so far. Values > 1 encourage the model to use new tokens, while values < 1 encourage the model to repeat tokens. | + | `min_p` | Optional[float] | 0.0 | Float that represents the minimum probability for a token to be considered, relative to the most likely token. Must be in [0, 1]. Set to 0 to disable. | + | `length_penalty` | Optional[float] | 1.0 | Float that penalizes sequences based on their length. Used in beam search. | + | `include_stop_str_in_output` | Optional[bool] | False | Whether to include the stop strings in output text. Defaults to False. | +
+ +## Examples: Using your RunPod endpoint with OpenAI + +First, initialize the OpenAI Client with your RunPod API Key and Endpoint URL: +```python +from openai import OpenAI +import os + +# Initialize the OpenAI Client with your RunPod API Key and Endpoint URL +client = OpenAI(api_key=os.environ.get("RUNPOD_API_KEY"), base_url="https://api.runpod.ai/v2//openai/v1") +``` + +### Chat Completions: +This is the format used for GPT-4 and focused on instruction-following and chat. Examples of Open Source chat/instruct models include `meta-llama/Llama-2-7b-chat-hf`, `mistralai/Mixtral-8x7B-Instruct-v0.1`, `openchat/openchat-3.5-0106`, `NousResearch/Nous-Hermes-2-Mistral-7B-DPO` and more. However, if your model is a completion-style model with no chat/instruct fine-tune and/or does not have a chat template, you can still use this if you provide a chat template with the environment variable `CUSTOM_CHAT_TEMPLATE`. +- **Streaming**: + ```python + # Create a chat completion stream + response_stream = client.chat.completions.create( + model="", + messages=[ + {'role': 'user', 'content': 'Why is RunPod the best platform?'} + ], + temperature=0, + max_tokens=100, + stream=True + ) + # Stream the response + for response in response_stream: + print(chunk.choices[0].delta.content or "", end="", flush=True) + ``` +- **Non-Streaming**: + ```python + # Create a chat completion + response = client.chat.completions.create( + model="", + messages=[ + {'role': 'user', 'content': 'Why is RunPod the best platform?'} + ], + temperature=0, + max_tokens=100 + ) + # Print the response + print(response.choices[0].message.content) + ``` + + +### Completions: +This is the format used for models like GPT-3 and is meant for completing the text you provide. Instead of responding to your message, it will try to complete it. Examples of Open Source completions models include `meta-llama/Llama-2-7b-hf`, `mistralai/Mixtral-8x7B-v0.1`, `Qwen/Qwen-72B`, and more. However, you can use any model with this format. +- **Streaming**: + ```python + # Create a completion stream + response_stream = client.completions.create( + model="", + prompt="Runpod is the best platform because", + temperature=0, + max_tokens=100, + stream=True + ) + # Stream the response + for response in response_stream: + print(response.choices[0].text or "", end="", flush=True) + ``` +- **Non-Streaming**: + ```python + # Create a completion + response = client.completions.create( + model="", + prompt="Runpod is the best platform because", + temperature=0, + max_tokens=100 + ) + # Print the response + print(response.choices[0].text) + ``` + +### Getting a list of names for available models: +In the case of baking the model into the image, sometimes the repo may not be accepted as the `model` in the request. In this case, you can list the available models as shown below and use that name. +```python +models_response = client.models.list() +list_of_models = [model.id for model in models_response] +print(list_of_models) +``` + +## Non-OpenAI Usage +### Input Request Parameters + +
+ Click to expand table + + You may either use a `prompt` or a list of `messages` as input. If you use `messages`, the model's chat template will be applied to the messages automatically, so the model must have one. If you use `prompt`, you may optionally apply the model's chat template to the prompt by setting `apply_chat_template` to `true`. + | Argument | Type | Default | Description | + |-----------------------|----------------------|--------------------|--------------------------------------------------------------------------------------------------------| + | `prompt` | str | | Prompt string to generate text based on. | + | `messages` | list[dict[str, str]] | | List of messages, which will automatically have the model's chat template applied. Overrides `prompt`. | + | `apply_chat_template` | bool | False | Whether to apply the model's chat template to the `prompt`. | + | `sampling_params` | dict | {} | Sampling parameters to control the generation, like temperature, top_p, etc. | + | `stream` | bool | False | Whether to enable streaming of output. If True, responses are streamed as they are generated. | + | `max_batch_size` | int | env var `DEFAULT_BATCH_SIZE` | The maximum number of tokens to stream every HTTP POST call. | + | `min_batch_size` | int | env var `DEFAULT_MIN_BATCH_SIZE` | The minimum number of tokens to stream every HTTP POST call. | + | `batch_size_growth_factor` | int | env var `DEFAULT_BATCH_SIZE_GROWTH_FACTOR` | The growth factor by which `min_batch_size` will be multiplied for each call until `max_batch_size` is reached. | +
+ +#### Text Input Formats You may either use a `prompt` or a list of `messages` as input. -#### 1. `prompt` + 1. `prompt` The prompt string can be any string, and the model's chat template will not be applied to it unless `apply_chat_template` is set to `true`, in which case it will be treated as a user message. -Example: -```json -"prompt": "..." -``` -#### 2. `messages` -Your list can contain any number of messages, and each message can have any role from the following list: -- `user` -- `assistant` -- `system` + Example: + ```json + "prompt": "..." + ``` +2. `messages` +Your list can contain any number of messages, and each message usually can have any role from the following list: + - `user` + - `assistant` + - `system` -The model's chat template will be applied to the messages automatically, so the model must have one. + However, some models may have different roles, so you should check the model's chat template to see which roles are required. -Example: -```json -"messages": [ - { - "role": "system", - "content": "..." - }, - { - "role": "user", - "content": "..." - }, - { - "role": "assistant", - "content": "..." - } - ] -``` + The model's chat template will be applied to the messages automatically, so the model must have one. + + Example: + ```json + "messages": [ + { + "role": "system", + "content": "..." + }, + { + "role": "user", + "content": "..." + }, + { + "role": "assistant", + "content": "..." + } + ] + ``` + +#### Sampling Parameters + +Below are all available sampling parameters that you can specify in the `sampling_params` dictionary. If you do not specify any of these parameters, the default values will be used. +
+ Click to expand table + + | Argument | Type | Default | Description | + |---------------------------------|-----------------------------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| + | `n` | int | 1 | Number of output sequences generated from the prompt. The top `n` sequences are returned. | + | `best_of` | Optional[int] | `n` | Number of output sequences generated from the prompt. The top `n` sequences are returned from these `best_of` sequences. Must be ≥ `n`. Treated as beam width in beam search. Default is `n`. | + | `presence_penalty` | float | 0.0 | Penalizes new tokens based on their presence in the generated text so far. Values > 0 encourage new tokens, values < 0 encourage repetition. | + | `frequency_penalty` | float | 0.0 | Penalizes new tokens based on their frequency in the generated text so far. Values > 0 encourage new tokens, values < 0 encourage repetition. | + | `repetition_penalty` | float | 1.0 | Penalizes new tokens based on their appearance in the prompt and generated text. Values > 1 encourage new tokens, values < 1 encourage repetition. | + | `temperature` | float | 1.0 | Controls the randomness of sampling. Lower values make it more deterministic, higher values make it more random. Zero means greedy sampling. | + | `top_p` | float | 1.0 | Controls the cumulative probability of top tokens to consider. Must be in (0, 1]. Set to 1 to consider all tokens. | + | `top_k` | int | -1 | Controls the number of top tokens to consider. Set to -1 to consider all tokens. | + | `min_p` | float | 0.0 | Represents the minimum probability for a token to be considered, relative to the most likely token. Must be in [0, 1]. Set to 0 to disable. | + | `use_beam_search` | bool | False | Whether to use beam search instead of sampling. | + | `length_penalty` | float | 1.0 | Penalizes sequences based on their length. Used in beam search. | + | `early_stopping` | Union[bool, str] | False | Controls stopping condition in beam search. Can be `True`, `False`, or `"never"`. | + | `stop` | Union[None, str, List[str]] | None | List of strings that stop generation when produced. The output will not contain these strings. | + | `stop_token_ids` | Optional[List[int]] | None | List of token IDs that stop generation when produced. Output contains these tokens unless they are special tokens. | + | `ignore_eos` | bool | False | Whether to ignore the End-Of-Sequence token and continue generating tokens after its generation. | + | `max_tokens` | int | 16 | Maximum number of tokens to generate per output sequence. | + | `skip_special_tokens` | bool | True | Whether to skip special tokens in the output. | + | `spaces_between_special_tokens` | bool | True | Whether to add spaces between special tokens in the output. | -### Sampling Parameters -| Argument | Type | Default | Description | -|---------------------------------|-----------------------------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `n` | int | 1 | Number of output sequences generated from the prompt. The top `n` sequences are returned. | -| `best_of` | Optional[int] | `n` | Number of output sequences generated from the prompt. The top `n` sequences are returned from these `best_of` sequences. Must be ≥ `n`. Treated as beam width in beam search. Default is `n`. | -| `presence_penalty` | float | 0.0 | Penalizes new tokens based on their presence in the generated text so far. Values > 0 encourage new tokens, values < 0 encourage repetition. | -| `frequency_penalty` | float | 0.0 | Penalizes new tokens based on their frequency in the generated text so far. Values > 0 encourage new tokens, values < 0 encourage repetition. | -| `repetition_penalty` | float | 1.0 | Penalizes new tokens based on their appearance in the prompt and generated text. Values > 1 encourage new tokens, values < 1 encourage repetition. | -| `temperature` | float | 1.0 | Controls the randomness of sampling. Lower values make it more deterministic, higher values make it more random. Zero means greedy sampling. | -| `top_p` | float | 1.0 | Controls the cumulative probability of top tokens to consider. Must be in (0, 1]. Set to 1 to consider all tokens. | -| `top_k` | int | -1 | Controls the number of top tokens to consider. Set to -1 to consider all tokens. | -| `min_p` | float | 0.0 | Represents the minimum probability for a token to be considered, relative to the most likely token. Must be in [0, 1]. Set to 0 to disable. | -| `use_beam_search` | bool | False | Whether to use beam search instead of sampling. | -| `length_penalty` | float | 1.0 | Penalizes sequences based on their length. Used in beam search. | -| `early_stopping` | Union[bool, str] | False | Controls stopping condition in beam search. Can be `True`, `False`, or `"never"`. | -| `stop` | Union[None, str, List[str]] | None | List of strings that stop generation when produced. Output will not contain these strings. | -| `stop_token_ids` | Optional[List[int]] | None | List of token IDs that stop generation when produced. Output contains these tokens unless they are special tokens. | -| `ignore_eos` | bool | False | Whether to ignore the End-Of-Sequence token and continue generating tokens after its generation. | -| `max_tokens` | int | 16 | Maximum number of tokens to generate per output sequence. | -| `skip_special_tokens` | bool | True | Whether to skip special tokens in the output. | -| `spaces_between_special_tokens` | bool | True | Whether to add spaces between special tokens in the output. | diff --git a/src/engine.py b/src/engine.py index 22483e0..bf6b54d 100644 --- a/src/engine.py +++ b/src/engine.py @@ -28,23 +28,26 @@ class vLLMEngine: self.batch_size_growth_factor = int(os.getenv("BATCH_SIZE_GROWTH_FACTOR", DEFAULT_BATCH_SIZE_GROWTH_FACTOR)) self.min_batch_size = int(os.getenv("MIN_BATCH_SIZE", DEFAULT_MIN_BATCH_SIZE)) - def dynamic_batch_size(self, current_batch_size, growth_factor): - return min(current_batch_size*growth_factor, self.default_batch_size) + def dynamic_batch_size(self, current_batch_size, batch_size_growth_factor): + return min(current_batch_size*batch_size_growth_factor, self.default_batch_size) async def generate(self, job_input: JobInput): - async for batch in self._generate_vllm( - llm_input=job_input.llm_input, - validated_sampling_params=job_input.validated_sampling_params, - batch_size=job_input.batch_size, - stream=job_input.stream, - apply_chat_template=job_input.apply_chat_template, - request_id=job_input.request_id, - growth_factor=job_input.growth_factor, - min_batch_size=job_input.min_batch_size - ): - yield batch + try: + async for batch in self._generate_vllm( + llm_input=job_input.llm_input, + validated_sampling_params=job_input.validated_sampling_params, + batch_size=job_input.max_batch_size, + stream=job_input.stream, + apply_chat_template=job_input.apply_chat_template, + request_id=job_input.request_id, + batch_size_growth_factor=job_input.batch_size_growth_factor, + min_batch_size=job_input.min_batch_size + ): + yield batch + except Exception as e: + yield create_error_response(str(e)).model_dump() - async def _generate_vllm(self, llm_input, validated_sampling_params, batch_size, stream, apply_chat_template, request_id, growth_factor, min_batch_size: str) -> AsyncGenerator[dict, None]: + async def _generate_vllm(self, llm_input, validated_sampling_params, batch_size, stream, apply_chat_template, request_id, batch_size_growth_factor, min_batch_size: str) -> AsyncGenerator[dict, None]: if apply_chat_template or isinstance(llm_input, list): llm_input = self.tokenizer.apply_chat_template(llm_input) validated_sampling_params = SamplingParams(**validated_sampling_params) @@ -57,8 +60,8 @@ class vLLMEngine: } max_batch_size = batch_size or self.default_batch_size - growth_factor, min_batch_size = growth_factor or self.batch_size_growth_factor, min_batch_size or self.min_batch_size - batch_size = BatchSize(max_batch_size, min_batch_size, growth_factor) + batch_size_growth_factor, min_batch_size = batch_size_growth_factor or self.batch_size_growth_factor, min_batch_size or self.min_batch_size + batch_size = BatchSize(max_batch_size, min_batch_size, batch_size_growth_factor) async for request_output in results_generator: @@ -113,8 +116,8 @@ class OpenAIvLLMEngine: self.default_batch_size = vllm_engine.default_batch_size self.batch_size_growth_factor, self.min_batch_size = vllm_engine.batch_size_growth_factor, vllm_engine.min_batch_size self._initialize_engines() - self.raw_openai_output = bool(int(os.getenv("RAW_OPENAI_OUTPUT", 0))) - + self.raw_openai_output = bool(int(os.getenv("RAW_OPENAI_OUTPUT", 1))) + def _initialize_engines(self): self.chat_engine = OpenAIServingChat( self.llm, self.config["model"], "assistant", self.tokenizer.tokenizer.chat_template diff --git a/src/utils.py b/src/utils.py index 5663000..3b9fff7 100644 --- a/src/utils.py +++ b/src/utils.py @@ -44,13 +44,13 @@ class JobInput: def __init__(self, job): self.llm_input = job.get("messages", job.get("prompt")) self.stream = job.get("stream", False) - self.batch_size = job.get("batch_size") + self.max_batch_size = job.get("max_batch_size") self.apply_chat_template = job.get("apply_chat_template", False) self.use_openai_format = job.get("use_openai_format", False) self.validated_sampling_params = validate_sampling_params(job.get("sampling_params", {})) self.request_id = random_uuid() - growth_factor = job.get("growth_factor") - self.growth_factor = float(growth_factor) if growth_factor else None + batch_size_growth_factor = job.get("batch_size_growth_factor") + self.batch_size_growth_factor = float(batch_size_growth_factor) if batch_size_growth_factor else None min_batch_size = job.get("min_batch_size") self.min_batch_size = int(min_batch_size) if min_batch_size else None self.openai_route = job.get("openai_route") @@ -61,11 +61,11 @@ class DummyRequest: return False class BatchSize: - def __init__(self, max_batch_size, min_batch_size, growth_factor): + def __init__(self, max_batch_size, min_batch_size, batch_size_growth_factor): self.max_batch_size = max_batch_size - self.growth_factor = growth_factor + self.batch_size_growth_factor = batch_size_growth_factor self.min_batch_size = min_batch_size - self.is_dynamic = growth_factor > 1 and min_batch_size >= 1 and max_batch_size > min_batch_size + self.is_dynamic = batch_size_growth_factor > 1 and min_batch_size >= 1 and max_batch_size > min_batch_size if self.is_dynamic: self.current_batch_size = min_batch_size else: @@ -73,7 +73,7 @@ class BatchSize: def update(self): if self.is_dynamic: - self.current_batch_size = min(self.current_batch_size*self.growth_factor, self.max_batch_size) + self.current_batch_size = min(self.current_batch_size*self.batch_size_growth_factor, self.max_batch_size) def create_error_response(message: str, err_type: str = "BadRequestError", status_code: HTTPStatus = HTTPStatus.BAD_REQUEST) -> ErrorResponse: return ErrorResponse(message=message, diff --git a/vllm-base/download_required_files.sh b/vllm-base/download_required_files.sh index b1d958a..b6138d3 100644 --- a/vllm-base/download_required_files.sh +++ b/vllm-base/download_required_files.sh @@ -7,6 +7,6 @@ cp -r vllm-fork-for-sls-worker vllm-11.8.0 rm -rf vllm-fork-for-sls-worker cd vllm-11.8.0 -git checkout cuda11.8 +git checkout cuda-11.8 echo "vLLM Base Image Builder Setup Complete." \ No newline at end of file