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

相关推荐
缘华工业智维3 小时前
工业设备预测性维护:能源成本降低的“隐藏钥匙”?
大数据·网络·人工智能
于小汐在咯4 小时前
词根学习笔记 | Agri系列
笔记·学习
DooTask官方号4 小时前
跨语言协作新范式:阿里云Qwen-MT与DooTask的翻译技术突破
人工智能·ai·项目管理·机器翻译·dootask
霜绛4 小时前
Unity:Json笔记——Json文件格式、JsonUtlity序列化和反序列化
学习·unity·json·游戏引擎
凯禾瑞华养老实训室6 小时前
聚焦生活照护能力培育:老年生活照护实训室建设清单的模块设计与资源整合
大数据·人工智能·科技·ar·vr·智慧养老·智慧健康养老服务与管理
倔强青铜三6 小时前
苦练Python第64天:从零掌握多线程,threading模块全面指南
人工智能·python·面试
我命由我123456 小时前
Excel - Excel 列出一列中所有不重复数据
经验分享·学习·职场和发展·word·powerpoint·excel·职场发展
格林威6 小时前
偏振相机是否属于不同光谱相机的范围内
图像处理·人工智能·数码相机·计算机视觉·视觉检测·工业相机
璞致电子6 小时前
fpga开发板ZYNQ 璞致 PZ7010/7020 邮票孔核心板简介-ZYNQ7000系列小系统学习板
linux·嵌入式硬件·学习·fpga开发·fpga·fpga开发板·xilinx开发板
A-大程序员6 小时前
【pytorch】合并与分割
人工智能·pytorch·深度学习