vLLM
安装
您需要安装 vllm 库以使用 vLLM 集成。请参见 安装部分 以获取在 CPU 或 ROCm 上安装 vLLM 的说明。要开始,您还可以运行:
加载模型
Outlines 支持通过 vLLM 的离线批量推理接口提供的模型。您可以使用以下方式加载模型:
或者:
import vllm
from outlines import models
llm = vllm.LLM("microsoft/Phi-3-mini-4k-instruct")
model = models.VLLM(llm)
模型从HuggingFace hub加载。
设备
vLLM的默认安装仅允许在GPU上加载模型。请参阅安装说明以在CPU上运行模型。
您可以传递任何您通常会传递给 vllm.LLM 的参数,作为关键字参数:
from outlines import models
model = models.vllm(
"microsoft/Phi-3-mini-4k-instruct",
trust_remote_code=True,
gpu_memory_utilization=0.7
)
主要参数:
| 参数 | 类型 | 描述 | 默认 |
|---|---|---|---|
tokenizer_mode |
str |
"auto"将使用快速分词器(如果可用),而"slow"将始终使用慢速分词器。 | auto |
trust_remote_code |
bool |
在下载模型和分词器时,信任远程代码。 | False |
tensor_parallel_size |
int |
用于张量并行执行的GPU数量。 | 1 |
dtype |
str |
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. |
auto |
quantization |
Optional[str] |
用于量化模型权重的方法。目前,我们支持 "awq"、"gptq" 和 "squeezellm"。如果为 None,我们首先检查模型配置文件中的 quantization_config 属性。如果该属性也为 None,我们假定模型权重未被量化,并使用 dtype 来确定权重的数据类型。 |
None |
revision |
Optional[str] |
要使用的具体模型版本。它可以是一个分支名称、一个标签名称或一个提交id。 | None |
tokenizer_revision |
Optional[str] |
要使用的特定分词器版本。它可以是分支名称、标签名称或提交 ID。 | None |
gpu_memory_utilization |
float |
保留给模型权重、激活和KV缓存的GPU内存比例(在0到1之间)。更高的值将会增加KV缓存大小,从而提高模型的吞吐量。然而,如果值过高,可能会导致内存不足(OOM)错误。 | 0.9 |
swap_space |
int |
每个GPU可用作交换空间的CPU内存大小(GiB)。当请求的best_of采样参数大于1时,可以用于临时存储请求的状态。如果所有请求的best_of=1,您可以安全地将其设置为0。否则,值过小可能会导致内存不足(OOM)错误。 |
4 |
enforce_eager |
bool |
是否强制执行急切模式。如果为 True,我们将禁用 CUDA 图并始终以急切模式执行模型。如果为 False,我们将使用 CUDA 图并在混合模式中进行急切执行。 | False |
enable_lora |
bool |
是否启用加载LoRA适配器 | False |
请查看vLLM 代码以获取所有可用参数的列表。
使用量化模型
vLLM支持AWQ、GPTQ和SqueezeLLM量化模型:
from outlines import models
model = models.vllm("TheBloke/Llama-2-7B-Chat-AWQ", quantization="awq")
model = models.vllm("TheBloke/Mistral-7B-Instruct-v0.2-GPTQ", quantization="gptq")
model = models.vllm("https://huggingface.co/squeeze-ai-lab/sq-llama-30b-w4-s5", quantization="squeezellm")
依赖关系
要使用AWQ模型,您需要安装autoawq库 pip install autoawq。
要使用GPTQ模型,您需要安装autoGTPQ和optimum库 pip install auto-gptq optimum。
多GPU使用
要使用vLLM进行多GPU推理,您需要在初始化模型时将tensor_parallel_size参数设置为可用的GPU数量。例如,要在2个GPU上运行推理:
from outlines import models
model = models.vllm(
"microsoft/Phi-3-mini-4k-instruct"
tensor_parallel_size=2
)
加载LoRA适配器
您可以加载LoRA适配器并在它们之间动态切换:
from outlines import models
model = models.vllm("facebook/opt-350m", enable_lora=True)
model.load_lora("ybelkaa/opt-350m-lora") # Load LoRA adapter
model.load_lora(None) # Unload LoRA adapter
生成文本
除了在 文本生成部分 中描述的参数,您还可以通过 sampling_params 关键字参数直接将 SamplingParams 的实例传递给任何生成器:
from vllm.sampling_params import SamplingParams
from outlines import models, generate
model = models.vllm("microsoft/Phi-3-mini-4k-instruct")
generator = generate.text(model)
params = SamplingParams(n=2, frequency_penalty=1., min_tokens=2)
answer = generator("A prompt", sampling_params=params)
这同样适用于使用 generate.regex、generate.json、generate.cfg、generate.format 和 generate.choice 构建的生成器。
注意
通过 SamplingParams 实例传递的值优先于生成器或采样器的其他参数。
SamplingParams 属性:
| 参数 | 类型 | 描述 | 默认值 |
|---|---|---|---|
n |
int |
给定提示要返回的输出序列的数量。 | 1 |
best_of |
Optional[int] |
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. |
None |
presence_penalty |
float |
一个浮动值,用于根据新生成的文本中是否出现过的标记来惩罚新标记。大于 0 的值鼓励模型使用新标记,而小于 0 的值则鼓励模型重复使用已有标记。 | 0.0 |
frequency_penalty |
float |
一个浮点数,根据生成文本中新词的频率对其进行惩罚。值 > 0 鼓励模型使用新词,而值 < 0 则鼓励模型重复使用词语。 | 0.0 |
repetition_penalty |
float |
浮动值,根据新令牌是否出现在提示和迄今生成的文本中对其进行惩罚。大于1的值鼓励模型使用新令牌,而小于1的值则鼓励模型重复令牌。 | 1.0 |
temperature |
float |
控制采样随机性的浮点数。较低的值使模型更具确定性,而较高的值使模型更随机。零表示贪婪采样。 | 1.0 |
top_p |
float |
浮动值,控制要考虑的顶级标记的累计概率。必须在 (0, 1] 之间。设置为 1 考虑所有标记。 | 1.0 |
top_k |
int |
控制要考虑的前几个标记的整数。设置为 -1 以考虑所有标记。 | -1 |
min_p |
float |
表示一个令牌被考虑的最小概率的浮动值,相对于最可能令牌的概率。必须在 [0, 1] 之间。设置为 0 以禁用此功能。 | 0.0 |
seed |
Optional[int] |
用于生成的随机种子。 | None |
use_beam_search |
bool |
是否使用束搜索而不是采样。 | False |
length_penalty |
float |
根据序列的长度对其进行惩罚的浮点数。用于束搜索。 | 1.0 |
early_stopping |
Union[bool, str] |
Controls the stopping condition for beam search. It accepts the following values: True, where the generation stops as soon as there are best_of complete candidates; False, where an heuristic is applied and the generation stops when is it very unlikely to find better candidates; "never", where the beam search procedure only stops when there cannot be better candidates (canonical beam search algorithm). |
False |
stop |
Optional[Union[str, List[str]]] |
生成时会在生成的字符串中停止的字符串列表。返回的输出将不包含停止字符串。 | None |
stop_token_ids |
Optional[List[int]] |
生成时停止生成的标记列表。如果生成的输出包含停止标记,则输出中将包含这些停止标记,除非停止标记是特殊标记。 | None |
include_stop_str_in_output |
bool |
是否在输出文本中包含停止字符串。默认为 False。 | False |
ignore_eos |
bool |
是否忽略EOS标记并在生成EOS标记后继续生成标记。 | False |
max_tokens |
int |
每个输出序列生成的最大令牌数。 | 16 |
min_tokens |
int |
生成每个输出序列的最小令牌数,直到可以生成EOS或stop_token_ids | 0 |
skip_special_tokens |
bool |
是否在输出中跳过特殊标记。 | True |
spaces_between_special_tokens |
bool |
是否在输出中在特殊标记之间添加空格。默认为 True。 | True |
流媒体
警告
离线 vLLM 集成不支持流式传输。
安装
默认情况下,vLLM库以预编译的C++和CUDA二进制文件安装,只能在GPU上运行:
中央处理器
您需要在系统上安装 gcc 编译器。然后您需要从源代码安装 vLLM。首先克隆代码库:
安装所需的Python包:
pip install --upgrade pip
pip install wheel packaging ninja setuptools>=49.4.0 numpy
pip install -v -r requirements-cpu.txt --extra-index-url https://download.pytorch.org/whl/cpu
最后运行:
有关更多详细信息、替代安装方法(Docker)和性能提示,请参阅vLLM 文档。
ROCm
您需要从源代码安装 vLLM。首先在 ROCm 上安装 Pytorch:
pip install torch==2.2.0.dev20231206+rocm5.7 --index-url https://download.pytorch.org/whl/nightly/rocm5.7 # tested version
然后您需要按照 这些说明 为 ROCm 安装 Flash Attention。接着,您可以安装 xformers=0.0.23 并应用所需的补丁以适应 ROCm 的 Flash Attention:
最后构建 vLLM:
cd vllm
pip install -U -r requirements-rocm.txt
python setup.py install # This may take 5-10 minutes.
有关替代安装方法(Docker),请参见 vLLM documentation。