vLLM 介绍
vLLM 是一个高效的、可扩展的深度学习库,专门用于处理大规模语言模型(LLMs)。它旨在提供一种灵活且高效的方式来训练和推理大型语言模型,特别是在资源受限的环境中。
vLLM主要特点
-
高效的推理:
- vLLM 采用了优化的推理算法,能够在较低的内存占用下实现高效的推理。这使得用户能够在普通硬件上运行大型语言模型,而不需要高端的 GPU。
-
支持多种模型:
- vLLM 支持多种流行的预训练语言模型,包括 GPT、BERT、T5 等。用户可以方便地加载和使用这些模型。
-
灵活的架构:
- vLLM 提供了灵活的 API,允许用户根据自己的需求进行定制和扩展。用户可以轻松地集成自己的模型或修改现有模型的行为。
-
分布式训练:
- vLLM 支持分布式训练,允许用户在多个 GPU 或机器上并行训练模型。这对于处理大规模数据集和模型非常重要。
-
易于使用:
- vLLM 提供了简单易用的接口,用户可以快速上手,进行模型的训练和推理。文档和示例代码也使得新用户能够更快地理解和使用库的功能。
-
与 Hugging Face 兼容:
- vLLM 可以与 Hugging Face 的 Transformers 库无缝集成,用户可以利用 Hugging Face 提供的丰富模型和工具。
vLLM需要用到uvloop,而Windows不支持uvloop,所以这个只能在Linux等系统用了。
服务器模式实践
(本机未成功,kaggle未成功)
安装vLLM库
# Install vLLM from pip:
pip install vllm
有时运行的时候会报缺一些库,可以一并安装
pip install uvloop
下载并启动大模型服务
# Load and run the model:
vllm serve "HuggingFaceTB/SmolLM2-135M"
调用模型测试
# Call the server using curl:
curl -X POST "http://localhost:8000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "HuggingFaceTB/SmolLM2-135M",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'
交互模式实践
在Kaggle使用T4环境测试通过
先安装vLLM库
# Install vLLM from pip:
pip install vllm
notebook测试
from vllm import LLM, SamplingParams
# 推理数据以List[str]格式组织
prompts = [
"Hello, my name is",
"The president of the United States is",
"The capital of France is",
"The future of AI is",
]
prompts = [
"Hello, my name is",
"The president of the United States is",
"The capital of France is",
]
# 设置采样参数
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
# 加载模型
llm = LLM(model="facebook/opt-125m", dtype="half")
# llm = LLM("HuggingFaceTB/SmolLM2-135M")
# llm = LLM("HuggingFaceTB/SmolLM2-135M", dtype="half") # p100 和T4 7.5专用
# llm = LLM(model="facebook/opt-125m", device="cpu") # cpu专用
# llm = LLM("HuggingFaceTB/SmolLM2-135M", device="cpu") # 指定使用 CPU
# 执行推理
outputs = llm.generate(prompts, sampling_params)
# 输出推理结果
for output in outputs:
prompt = output.prompt
generated_text = output.outputs[0].text
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
输出:
INFO 03-18 02:12:10 model_runner.py:1434] Capturing cudagraphs for decoding. This may lead to unexpected consequences if the model is not static. To run the model in eager mode, set 'enforce_eager=True' or use '--enforce-eager' in the CLI. If out-of-memory error occurs during cudagraph capture, consider decreasing `gpu_memory_utilization` or switching to eager mode. You can also reduce the `max_num_seqs` as needed to decrease memory usage.
Capturing CUDA graph shapes: 100%|██████████| 35/35 [00:24<00:00, 1.43it/s]
INFO 03-18 02:12:34 model_runner.py:1562] Graph capturing finished in 24 secs, took 0.14 GiB
INFO 03-18 02:12:34 llm_engine.py:436] init engine (profile, create kv cache, warmup model) took 29.92 seconds
Processed prompts: 100%|██████████| 3/3 [00:00<00:00, 21.87it/s, est. speed input: 145.96 toks/s, output: 350.26 toks/s]
Prompt: 'Hello, my name is', Generated text: ' Joel, my dad is my friend and we are in a relationship. I am'
Prompt: 'The president of the United States is', Generated text: ' giving an update to the coronavirus pandemic after testing positive for the virus'
Prompt: 'The capital of France is', Generated text: ' now the capital of the French Republic. (AP Photo/Evan Vucci'
测试完成。
模型存盘问题
在使用 vLLM
从 Hugging Face 下载模型时,模型文件通常会被存储在本地的缓存目录中。具体的存储位置取决于你的操作系统和 Hugging Face 的配置。以下是一些常见的存储位置:
1. 默认缓存目录
Hugging Face Transformers 库默认将模型下载到以下目录:
-
Linux 和 macOS:
~/.cache/huggingface/transformers
Copy -
Windows:
C:\Users\<YourUsername>\.cache\huggingface\transformers
Copy
2. 自定义缓存目录
如果你在使用 Hugging Face 的库时设置了自定义的缓存目录(通过环境变量 TRANSFORMERS_CACHE
),那么模型将会下载到你指定的目录。例如:
export TRANSFORMERS_CACHE=/path/to/your/cache
Copy
3. 使用 vllm
时的模型路径
在使用 vllm
时,你可以指定模型的路径。如果你没有指定路径,vllm
会自动查找 Hugging Face 的缓存目录。
4. 检查下载的模型
你可以在上述目录中找到下载的模型文件,通常包括以下几种文件:
pytorch_model.bin
:模型权重文件。config.json
:模型配置文件。tokenizer.json
或其他与分词器相关的文件。
5. 使用示例
在使用 vllm
加载模型时,你可以直接使用模型的名称,vllm
会自动从缓存中加载模型。例如:
from vllm import VLLM
model = VLLM("model_name_or_path")
如果模型已经下载到缓存中,vllm
会直接使用缓存中的文件。
总结
Hugging Face 下载的模型通常存储在用户主目录下的 .cache/huggingface/transformers
目录中。可以根据需要自定义缓存目录,并在使用 vllm
时直接加载模型。
总结
cpu下没有测试成功。
在Kaggle T4环境下测试成功。
LMStudio等软件是支持使用vLLM的,Huggingface也是支持vLLM的。
本身使用vLLM的目的是为了从Huggingface下载模型
from vllm import LLM
llm = LLM(model="HuggingFaceTB/SmolLM2-135M", device="cpu")
但是没有成功。
调试
报错undefined symbol: cuTensorMapEncodeTiled
File "/home/skywalk/py310/lib/python3.10/site-packages/vllm/platforms/cuda.py", line 15, in <module>
import vllm._C # noqa
ImportError: /home/skywalk/py310/lib/python3.10/site-packages/vllm/_C.abi3.so: undefined symbol: cuTensorMapEncodeTiled
应该是cuda版本没弄好,卡是11.04 ,安装的是11.08
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.256.02 Driver Version: 475.14 CUDA Version: 11.4 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Quadro K4200 Off | 00000000:05:00.0 On | N/A |
|ERR! 0C P8 16W / 110W | 578MiB / 4096MiB | ERR! Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
+-----------------------------------------------------------------------------+
pip list |grep cuda
cupy-cuda12x 13.4.0
nvidia-cuda-cupti-cu11 11.8.87
nvidia-cuda-cupti-cu12 12.4.127
nvidia-cuda-nvrtc-cu11 11.8.89
nvidia-cuda-nvrtc-cu12 12.4.127
nvidia-cuda-runtime-cu11 11.8.89
nvidia-cuda-runtime-cu12 12.4.127
自己的显卡有问题,先搁置
后来是到Kaggle去做测试。
kaggle下测试报错CUDA error
RuntimeError: CUDA error: no kernel image is available for execution on the device CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect. For debugging consider passing CUDA_LAUNCH_BLOCKING=1 Compile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.
将P100换成T4
Kaggle T4碰到爆显存的问题
废了很大劲终于运行成功了,用习惯各大厂家的api调用,再用vLLM真的很不习惯,一不小心GPU显存就爆掉
经测试,4条prompt就会爆内存,3条就ok
prompts = [
"Hello, my name is",
"The president of the United States is",
"The capital of France is",
"The future of AI is",
]
prompts = [
"Hello, my name is",
"The president of the United States is",
"The capital of France is",
]
每次运行完都要重启清内存,感觉pytorch的显存控制没有飞桨做的好,在星河社区用飞桨感觉没这么容易爆显存。
尝试在本机cpu下载模型失败报错:No module named 'vllm._C'
使用命令
from vllm import LLM
llm = LLM(model="HuggingFaceTB/SmolLM2-135M", device="cpu")
报错信息:
import vllm._C # noqa
ModuleNotFoundError: No module named 'vllm._C'
问题先搁置,换用transformers下载吧。