高效部署大型语言模型:基于AMD GPU的文本生成推理

Efficient deployment of large language models with Text Generation Inference on AMD GPUs

2024年1月24日,由Douglas Jia撰写。

[文本生成推理(TGI)](https://huggingface.co/docs/text-generation-inference/index "文本生成推理(TGI)")\]是一个用于以无与伦比的效率部署和服务大型语言模型(LLM)的工具包。TGI专门为流行的开源LLM(如Llama、Falcon、StarCoder、BLOOM、GPT-NeoX和T5)进行了优化,其优化措施包括张量并行、使用服务器发送事件(SSE)进行的令牌流、连续批处理以及优化的transformers代码。其强大功能集包括量化、安全张量、水印(用于确定文本是否由语言模型生成)、logits变形器以及对自定义提示生成和微调的支持。 TGI是Hugging Chat、Open Assistant和\[nat.dev\]([http://nat.dev](http://nat.dev/ "http://nat.dev"))等项目的重要框架组件,这证明了其在生产环境中的卓越性能。 在本教程中,我们将展示如何在AMD GPU上使用TGI部署和服务LLM。本教程改编自\[[官方的Hugging Face教程](https://huggingface.co/docs/text-generation-inference/index "官方的Hugging Face教程")\],并结合了额外的见解以提供更全面的学习体验。我们向原教程的贡献表示感谢。 ### 使用 TGI 部署大型语言模型(LLMs) 要在启用了 ROCm 的 AMD GPU 上利用 TGI 框架,你可以选择使用官方 Docker 容器或从源代码构建 TGI。我们推荐使用 Docker 方法,因为它简化了设置过程并减少了软件兼容性问题。如果你更喜欢从源代码构建,可以在 \[[Hugging Face](https://huggingface.co/docs/text-generation-inference/installation "Hugging Face")\]找到详细的说明。 在你的运行 Linux 的机器上且已启用 ROCm 的 AMD GPU,运行以下命令在终端中部署 Docker 容器,并使用我们指定的模型 `tiiuae/falcon-7b-instruct`: model=tiiuae/falcon-7b-instruct volume=$PWD/data # share a volume with the Docker container to avoid downloading weights every run docker run --cap-add=SYS_PTRACE --security-opt seccomp=unconfined --device=/dev/kfd --device=/dev/dri --group-add video --ipc=host --shm-size 1g -p 8080:80 -v $volume:/data ghcr.io/huggingface/text-generation-inference:1.3-rocm --model-id $model 这些命令将构建一个带有指定模型的 TGI 服务器,准备好处理你的请求。有关支持的模型的全面列表,请参阅 \[[支持的模型](https://huggingface.co/docs/text-generation-inference/supported_models "支持的模型")\]。 如果模型的大小超过了单个 GPU 的容量,无法完全容纳,考虑在 `docker run` 命令中加入 `--num-shard n` 标志,其中 `n` 表示你可用 GPU 的数量。此标志会激活张量并行,有效地将模型分片,分布在所有可用的 GPU 上。 ### 查询部署在服务器上的大型语言模型(LLM) 在上一步中,您已设置了一个监听请求的服务器。现在,您可以打开一个新的终端与该服务器互动(在整个过程中请保持原服务器运行)。要查询服务器,您可以使用多种方法;我们演示两种常用的方法:Python的\`requests\`库和\`curl\`命令行工具。 #### Python `requests`库 通过在终端运行\`python3\`命令来启动一个Python会话。然后运行以下Python代码: ```python import requests headers = { "Content-Type": "application/json", } data = { 'inputs': 'What is the best way to learn Deep Learning?', 'parameters': { 'max_new_tokens': 200, 'temperature': 0.1, }, } response = requests.post('http://127.0.0.1:8080/generate', headers=headers, json=data) print(response.json()) ``` 输出: ```python {'generated_text': '\nThe best way to learn Deep Learning is through a combination of hands-on practice and structured learning. Some popular resources for learning Deep Learning include online courses, such as those offered by Coursera or edX, and textbooks such as "Deep Learning" by Goodfellow, Bengio, and Courville. Additionally, participating in online coding challenges and competitions can help reinforce your knowledge and improve your skills.'} ``` 您可以更改\`inputs\`字段以测试不同的提示信息。为了尝试不同的生成配置,您可以调整参数(如\`max_new_tokens\`和\`temperature\`)。要查看所有可调整的参数,请参阅\[[这个列表](https://huggingface.co/docs/transformers/main_classes/text_generation "这个列表")\]。 #### Curl命令行 在终端中,您可以使用以下Curl命令直接查询服务器: ```python curl 127.0.0.1:8080/generate \ -X POST \ -d '{"inputs":"What is the best way to learn Deep Learning?","parameters":{"max_new_tokens":200,"temperature":0.1}}' \ -H 'Content-Type: application/json' ``` 输出: ```python {"generated_text":"\nThe best way to learn Deep Learning is through a combination of hands-on practice and structured learning. Some popular ways to learn Deep Learning include taking online courses, attending workshops, and working on personal projects. It's also important to stay up-to-date on the latest research and developments in the field."} ``` 您可能会观察到两种方法之间的输出略有不同。这是因为我们使用了0.1的\`temperature\`值,这促进了文本生成的多样性。为了获得更加确定性的输出,您可以将\`temperature\`值增加到1。

相关推荐
这张生成的图像能检测吗1 分钟前
(论文速读)YOLA:学习照明不变特征的低光目标检测
图像处理·人工智能·目标检测·计算机视觉·低照度
Chunyyyen10 分钟前
【第二十周】自然语言处理的学习笔记05
笔记·学习·自然语言处理
ZPC821013 分钟前
opencv 获取图像中物体的坐标值
人工智能·python·算法·机器人
亚里随笔20 分钟前
AsyPPO_ 轻量级mini-critics如何提升大语言模型推理能力
人工智能·语言模型·自然语言处理·llm·agentic
coding_ksy26 分钟前
基于启发式的多模态风险分布越狱攻击,针对多模态大型语言模型(ICCV 2025) - 论文阅读和解析
人工智能·语言模型
笨鸟笃行27 分钟前
百日挑战——单词篇(第十一天)
学习
算家计算1 小时前
5年后手机和APP将成历史?马斯克最新预言背后:端云协同与AI操作系统的未来架构
人工智能·云计算·资讯
多恩Stone2 小时前
【3DV 进阶-5】3D生成中 Inductive Bias (归纳偏置)的技术路线图
人工智能·python·算法·3d·aigc
HaiLang_IT2 小时前
2026 人工智能与大数据专业毕业论文选题方向及题目示例(nlp/自然语言处理/图像处理)
大数据·人工智能·毕业设计选题