常见的推理框架:
- HuggingFace Transformers
- vLLM
- Text Generation Inference(TGI)
- TensorRT-LLM
- ollama
- DeepSpeed
- ...
--1 ollama 部署
--1-1 ollama 基本设置
Linux 模型默认存放位置: /usr/share/ollama/.ollama/models
--1-2 设置ollama服务外部可访问
- 停止ollama服务: systemctl stop ollama
- 修改ollama的service文件:/etc/systemd/system/ollama.service 新增两个Environment设置
Environment="OLLAMA_HOST=0.0.0.0"
Environment="OLLAMA_ORIGINS=*"
Description=Ollama Service
After=network-online.target
[Service]
ExecStart=/usr/local/bin/ollama serve
User=ollama
Group=ollama
Restart=always
RestartSec=3
Environment="PATH=/run/user/1000/fnm_multishells/3595978_1739449245215/bin:/home/uto/.local/share/fnm:/usr/local/cuda/bin:/opt/ros/galactic/bin:/home/uto/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/opt/uto/pilot/uto_map_sdk:/opt/uto/pilot/rviz2/bin:/opt/uto/pilot/rqt_bag/bin:/opt/uto/pilot/urdfdom/bin:/opt/uto/pilot/rqt_graph/bin:/opt/uto/pilot/rqt_gui/bin:/opt/uto/pilot/ros2cli/bin:/opt/uto/pilot/ament_uncrustify/bin:/opt/uto/pilot/uncrustify_vendor/bin:/opt/uto/pilot/pendulum_control/bin:/opt/uto/pilot/tlsf_cpp/bin:/opt/uto/pilot/rttest/bin:/opt/uto/pilot/rosidl_cli/bin:/opt/uto/pilot/launch_testing/bin:/opt/uto/pilot/cyclonedds/bin:/opt/uto/pilot/iceoryx_posh/bin:/opt/uto/pilot/fastrtps/bin:/opt/uto/pilot/foonathan_memory_vendor/bin:/opt/uto/pilot/ament_xmllint/bin:/opt/uto/pilot/ament_pyflakes/bin:/opt/uto/pilot/ament_pycodestyle/bin:/opt/uto/pilot/ament_pep257/bin:/opt/uto/pilot/ament_pclint/bin:/opt/uto/pilot/ament_mypy/bin:/opt/uto/pilot/ament_lint_cmake/bin:/opt/uto/pilot/ament_flake8/bin:/opt/uto/pilot/ament_copyright/bin:/opt/uto/pilot/ament_index_python/bin:/opt/uto/pilot/ament_cpplint/bin:/opt/uto/pilot/ament_cppcheck/bin:/opt/uto/pilot/ament_clang_tidy/bin:/opt/uto/pilot/ament_clang_format/bin:/opt/uto/pilot:/usr/local/cuda-11.4/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/uto/.fzf/bin"
Environment="OLLAMA_HOST=0.0.0.0"
Environment="OLLAMA_ORIGINS=*"
[Install]
WantedBy=default.target
- 重载daemon文件 systemctl daemon-reload
- 启动ollama服务 systemctl start ollama
- 外部访问使用这个url 进行访问 http://IP:11434/v1
--1-3 设置ollama并发
vim /etc/systemd/system/ollama.service
[Service]
Environment="OLLAMA_NUM_PARALLEL=4" #并行处理请求的数量
Environment="OLLAMA_MAX_LOADED_MODELS=4" #同时加载的模型数量
sudo systemctl daemon-reload
sudo systemctl restart ollama
--2 vllm 部署
vLLM 框架是一个高效的大语言模型推理和部署服务系统,具备以下特性:
- 高效的内存管理:通过 PagedAttention ,vLLM 实现了对 KV 缓存的高效管理,减少了内存浪费,优化了模型的运行效率。
- 高吞吐量:vLLM 支持异步处理和连续批处理请求,显著提高了模型推理的吞吐量,加速了文本生成和处理速度。
- 易用性:vLLM 与 HuggingFace 模型无缝集成,支持多种流行的大型语言模型,简化了模型部署和推理的过程。兼容 OpenAI 的 API 服务器。
- 分布式推理:框架支持在多 GPU 环境中进行分布式推理,通过模型并行策略和高效的数据通信,提升了处理大型模型的能力。
- 开源共享:vLLM 由于其开源的属性,拥有活跃的社区支持,这也便于开发者贡献和改进,共同推动技术发展。
--2-1 启动推理服务
vLLM 支持提供 OpenAI 格式的 API,启动命令如下:
modelpath=/models/Qwen1.5-1.8B-Chat
# 单卡
python3 -m vllm.entrypoints.openai.api_server \
--model $modelpath \
--served-model-name qwen \
--trust-remote-code
--2-2 离线推理
对于离线推理,我们可以直接使用 vLLM 库在 Python 代码中完成推理,以省去 API 请求耗时。
from vllm import LLM, SamplingParams
# Sample prompts.
prompts = [
"Hello, my name is",
"The president of the United States is",
"The capital of France is",
"The future of AI is",
]
# Create a sampling params object.
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
# Create an LLM.
llm = LLM(model="facebook/opt-125m")
# Generate texts from the prompts. The output is a list of RequestOutput objects
# that contain the prompt, generated text, and other information.
outputs = llm.generate(prompts, sampling_params)
# Print the outputs.
for output in outputs:
prompt = output.prompt
generated_text = output.outputs[0].text
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")