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

相关推荐
平和男人杨争争5 分钟前
机器学习2——贝叶斯理论下
人工智能·机器学习
静心问道6 分钟前
XLSR-Wav2Vec2:用于语音识别的无监督跨语言表示学习
人工智能·学习·语音识别
算家计算11 分钟前
5 秒预览物理世界,2 行代码启动生成——ComfyUI-Cosmos-Predict2 本地部署教程,重塑机器人训练范式!
人工智能·开源
摆烂工程师11 分钟前
国内如何安装和使用 Claude Code 教程 - Windows 用户篇
人工智能·ai编程·claude
云天徽上9 天前
【目标检测】图像处理基础:像素、分辨率与图像格式解析
图像处理·人工智能·目标检测·计算机视觉·数据可视化
Vertira9 天前
PyTorch中的permute, transpose, view, reshape和flatten函数详解(已解决)
人工智能·pytorch·python
heimeiyingwang9 天前
【深度学习加速探秘】Winograd 卷积算法:让计算效率 “飞” 起来
人工智能·深度学习·算法
lsd&xql9 天前
AI大模型(四)openAI应用实战
人工智能
飞哥数智坊9 天前
AI编程实战:使用Cursor,65分钟轻松打造番茄时钟应用
人工智能
匿名的魔术师9 天前
实验问题记录:PyTorch Tensor 也会出现 a = b 赋值后,修改 a 会影响 b 的情况
人工智能·pytorch·python