高效部署大型语言模型:基于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。

相关推荐
Light6010 分钟前
MCP协议:CAD设计的“AI大脑”革命——从图纸到智能决策的全链路跃迁
人工智能·工业4.0·mcp协议·cad智能化·ai设计自动化
鹿鸣天涯10 分钟前
智能世界2035:探索未知,跃见未来
人工智能
火星技术15 分钟前
【 AI 智能换装开源】
人工智能
zyplayer-doc19 分钟前
目录支持批量操作,文档增加可见范围、锁定功能,PDF查看优化,zyplayer-doc 2.5.8 发布啦!
数据库·人工智能·pdf·编辑器·飞书·石墨文档
不羁的木木22 分钟前
【开源鸿蒙跨平台开发学习笔记】Day01:React Native 开发 HarmonyOS-环境搭建篇
学习·开源·harmonyos
@小红花29 分钟前
从零到精通 Hadoop 的系统学习文档
大数据·hadoop·学习
Dandelion____z29 分钟前
AI 驱动业务的致命风险:如何用架构设计守住安全底线?
java·大数据·人工智能·spring boot·aigc·jboltai
黑客思维者37 分钟前
为什么大语言模型需要海量训练数据?
人工智能·语言模型·自然语言处理
d111111111d1 小时前
W25Q60简介--SPI通信(笔记)
笔记·stm32·单片机·嵌入式硬件·学习
north_eagle1 小时前
缓解电动汽车里程焦虑:一个简单的AI模型如何预测港口可用性
人工智能