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

相关推荐
Jinkxs1 分钟前
测试工程师的AI转型指南:从工具使用到测试策略重构
人工智能·重构
喜欢吃燃面6 分钟前
C++算法竞赛:位运算
开发语言·c++·学习·算法
传奇开心果编程7 分钟前
【传奇开心果系列】Flet框架实现的家庭记账本示例自定义模板
python·学习·ui·前端框架·自动化
别惹CC13 分钟前
Spring AI 进阶之路01:三步将 AI 整合进 Spring Boot
人工智能·spring boot·spring
stbomei2 小时前
当 AI 开始 “理解” 情感:情感计算技术正在改写人机交互规则
人工智能·人机交互
_Kayo_6 小时前
node.js 学习笔记3 HTTP
笔记·学习
Moshow郑锴7 小时前
人工智能中的(特征选择)数据过滤方法和包裹方法
人工智能
TY-20258 小时前
【CV 目标检测】Fast RCNN模型①——与R-CNN区别
人工智能·目标检测·目标跟踪·cnn
CareyWYR9 小时前
苹果芯片Mac使用Docker部署MinerU api服务
人工智能
失散139 小时前
自然语言处理——02 文本预处理(下)
人工智能·自然语言处理