昇腾平台LLM pytorch推理环境搭建

本文介绍如何在昇腾平台上搭建针对LLM模型的PyTorch推理环境,包括基础环境配置、PyTorch及依赖安装步骤,并提供验证方法及示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【昇腾】LLM pytorch推理环境搭建

1.基础环境准备

资源下载地址:https://www.hiascend.com/developer/download
需要下载:
驱动
固件
cann
pytorch
pytorch_npu

python和pytorch的对应关系

PyTorch版本Python版本
PyTorch1.11.0Python3.7.x(>=3.7.5), Python3.8.x, Python3.9.x, Python3.10.x
PyTorch2.0.1Python3.8.x, Python3.9.x, Python3.10.x
PyTorch2.1.0Python3.8.x, Python3.9.x, Python3.10.x

检查NPU是否正常在位可执行lspci | grep d802命令,如果服务器上有 N路NPU,回显N行含“d802”字段,则表示NPU正常在位。

1.1 驱动安装
sh Ascend-hdk-910b-npu-driver_23.0.rc3_linux-aarch64.run --full --install-for-all
sh Ascend-hdk-910b-npu-firmware_6.4.0.4.220.run

通过执行npu-smi info命令查看

1.2 cann安装
sh Ascend-cann-nnrt_7.0.RC1_linux-aarch64.run

安装成功确认
cat /usr/local/Ascend/ascend-toolkit/latest/aarch64-linux/ascend-toolkit_install.info

2. 安装pytorch

2.1 安装pytorch

aarch64:

pip install torch==2.1.0

2.2 安装依赖
pip3 install pyyaml
pip3 install setuptools
2.3 安装pytorch-npu
pip3 install torch-npu==2.1.0rc1

3.验证

3.1 设置环境变量
# Default path, change it if needed.
source /usr/local/Ascend/ascend-toolkit/set_env.sh

查看状态

python3 -c "import torch;import torch_npu;print(torch_npu.npu.is_available())"
#如果为false,则需要进行迁移

#自动映射cuda API到npu的代码
from torch_npu.contrib import transfer_to_npu
print(torch_npu.npu.is_available())
print(torch_npu.cuda.is_available())

#True
#True

3.2 代码验证
import torch
import torch_npu

x = torch.randn(1, 3, 224, 224).npu()

print(x.shape)
print(type(x))

4 llm示例

此处以chatglm3-6b为示例


import time

import torch
import torch_npu
from torch_npu import transfer_to_npu

from transformers import AutoTokenizer, AutoModel


model_dir = 'path/to/chatglm3-6b'

tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
t0 = time.time()
model = AutoModel.from_pretrained(model_dir, trust_remote_code=True).half().npu()
model = model.eval()
print("model load consume:", time.time() - t0)
#38s

response, history = model.chat(tokenizer, "你好", history=[])
print(response)

response, history = model.chat(tokenizer, "晚上睡不着应该怎么办", history=history)
print(response)

常见问题

  1. 2.3中pip直接安装失败
    下载源码进行安装
    git clone https://gitee.com/ascend/pytorch.git -b v2.1.0-5.0.rc3 --depth 1

  2. “ImportError: libhccl.so: cannot open shared object file: No such file or directory”
    设置环境变量,见3.1

  3. “npu error,erro code is 507008” fail to obtain the soc version
    查看当前用户是否有权限使用驱动
    当前用户下执行npu-smi info是否可以查看到gpu信息,如果不能,则表示驱动安装错误,需要在安装命令加上 --install-for-all

  4. npu()-cuda()迁移问题
    https://support.huaweicloud.com/bestpractice-modelarts/modelarts_10_2503.html


cann和pytorch的对应关系

CANN版本

支持的PyTorch版本

支持的Adapter版本

Github分支

AscendHub镜像版本/名称(链接)

CANN 7.0.RC1

2.1.0

2.1.0.rc1

v2.1.0-5.0.rc3

-

2.0.1

2.0.1

v2.0.1-5.0.rc3

-

1.11.0

1.11.0.post4

v1.11.0-5.0.rc3

-

CANN 6.3.RC3.1

1.11.0

1.11.0.post3

v1.11.0-5.0.rc2.2

-

CANN 6.3.RC3

1.11.0

1.11.0.post2

v1.11.0-5.0.rc2.1

-

CANN 6.3.RC2

2.0.1

2.0.1.rc1

v2.0.1-5.0.rc2

-

1.11.0

1.11.0.post1

v1.11.0-5.0.rc2

23.0.RC1-1.11.0

1.8.1

1.8.1.post2

v1.8.1-5.0.rc2

23.0.RC1-1.8.1

CANN 6.3.RC1

1.11.0

1.11.0

v1.11.0-5.0.rc1

-

1.8.1

1.8.1.post1

v1.8.1-5.0.rc1

-

CANN 6.0.1

1.5.0

1.5.0.post8

v1.5.0-3.0.0

22.0.0

1.8.1

1.8.1

v1.8.1-3.0.0

22.0.0-1.8.1

1.11.0

1.11.0.rc2(beta)

v1.11.0-3.0.0

-

CANN 6.0.RC1

1.5.0

1.5.0.post7

v1.5.0-3.0.rc3

22.0.RC3

1.8.1

1.8.1.rc3

v1.8.1-3.0.rc3

22.0.RC3-1.8.1

1.11.0

1.11.0.rc1(beta)

v1.11.0-3.0.rc3

-

CANN 5.1.RC2

1.5.0

1.5.0.post6

v1.5.0-3.0.rc2

22.0.RC2

1.8.1

1.8.1.rc2

v1.8.1-3.0.rc2

22.0.RC2-1.8.1

CANN 5.1.RC1

1.5.0

1.5.0.post5

v1.5.0-3.0.rc1

22.0.RC1

1.8.1

1.8.1.rc1

v1.8.1-3.0.rc1

-

CANN 5.0.4

1.5.0

1.5.0.post4

2.0.4.tr5

21.0.4

CANN 5.0.3

1.8.1

1.5.0.post3

2.0.3.tr5

21.0.3

CANN 5.0.2

1.5.0

1.5.0.post2

2.0.2.tr5

21.0.2

华为升腾平台支持运行 YOLOv5 目标检测模型,以下是一个简单的 YOLOv5 案例代码示例: ```python import numpy as np from PIL import Image from atlasutil import ai from atlasutil import camera # 加载模型 model = ai.Model("yolov5s.om") # 打开摄像头 camera.open() while True: # 获取摄像头采集的图像 image = camera.capture() # 图像预处理 image = Image.fromarray(np.uint8(image)) image = image.resize((640, 640)) image_data = np.array(image) # 图像推理 output = model.infer([image_data]) # 解析推理结果 output = output[0] boxes, confidences, class_ids = output['boxes'], output['confidences'], output['class_ids'] # 在图像上绘制检测框 for i in range(len(boxes)): box = boxes[i] confidence = confidences[i] class_id = class_ids[i] if confidence > 0.5: x, y, w, h = box left, top, right, bottom = int(x - w / 2), int(y - h / 2), int(x + w / 2), int(y + h / 2) cv2.rectangle(image_data, (left, top), (right, bottom), (0, 255, 0), 2) cv2.putText(image_data, f"{class_id}", (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) # 显示图像 cv2.imshow('Object Detection', image_data) if cv2.waitKey(1) & 0xFF == ord('q'): break # 关闭摄像头 camera.close() ``` 这段代码使用华为升腾平台的 YOLOv5 模型进行实时目标检测,并在图像上绘制检测框类别标签。请确保已安装好华为升腾平台的开发套件相关依赖库,并参考华为开发者社区的文档进行模型部署推理
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值