GPU版Pytorch安装

一、获取安装指令

pytorch指令

pytorch1

注意Note,需要python3.10及以上版本

这是笔者的指令,请根据你的cuda版本对应正确的指令

1
pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cu130

二、在Anaconda虚拟环境安装

在anaconda虚拟环境中执行命令

pytorch2

三、测试是否生效

运行解决为True,均生效。

pytorch3

四、压力测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import torch
import time

# 创建大规模矩阵(单精度浮点)
a = torch.randn(10000, 10000).cuda()
b = torch.randn(10000, 10000).cuda()

# 预热GPU
for _ in range(10):
c = torch.matmul(a, b)

# 计时
start = time.time()
for _ in range(100):
c = torch.matmul(a, b)
torch.cuda.synchronize()
end = time.time()

print(f"GPU计算时间: {end - start:.2f}秒")

笔者显卡为RTX 5070 Ti,运行时间大概为14.30秒