飞桨使用pytorch

本文档详细介绍了如何在飞桨环境中安装PyTorch,并验证其是否正确安装及CUDA兼容性。通过创建虚拟环境并安装特定版本的PyTorch来确保与CUDA版本匹配,最后通过简单的测试代码验证安装是否成功。

1. 环境安装

#!/bin/bash
# 不要直接运行该.sh脚本,建议手动逐行运行

# 进入持续化目录,这里安装的东西关机不会被清空
cd work
# 创建虚拟环境:torch_env
python -m venv torch_env
# 终端激活虚拟环境
# 如果要在vscode的UI界面使用自己的虚拟环境,按下ctrl+shift+p,输入python select
source torch_env/bin/activate
# 安装pytorch之前查看一下cuda驱动版本
nvidia-smi
# Tue Aug 27 11:28:56 2024       
# +-----------------------------------------------------------------------------+
# | NVIDIA-SMI 525.125.06   Driver Version: 525.125.06   CUDA Version: 12.0     |
# |-------------------------------+----------------------+----------------------+
# | GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
# | Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
# |                               |                      |               MIG M. |
# |===============================+======================+======================|
# |   0  Tesla V100-SXM2...  Off  | 00000000:04:00.0 Off |                    0 |
# | N/A   35C    P0    40W / 300W |      0MiB / 32768MiB |      0%      Default |
# |                               |                      |                  N/A |
# +-------------------------------+----------------------+----------------------+
                                                                               
# +-----------------------------------------------------------------------------+
# | Processes:                                                                  |
# |  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
# |        ID   ID                                                   Usage      |
# |=============================================================================|
# |  No running processes found                                                 |
# +-----------------------------------------------------------------------------+

# 如果嫌速度慢的话,可以在自己的电脑下载好.whl文件然后上传到飞桨中,然后直接安装whl文件
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
# (torch_env) aistudio@jupyter-9669401-8258477:~/work$ pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
# Looking in indexes: https://download.pytorch.org/whl/cu118, https://mirrors.aliyun.com/pypi/simple/
# Collecting torch
#   Downloading https://download.pytorch.org/whl/cu118/torch-2.4.0%2Bcu118-cp310-cp310-linux_x86_64.whl (857.7 MB)
#      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╺━━━━━━━━━━ 625.2/857.7 MB 8.0 MB/s eta 0:00:30

2. 环境测试代码

import torch

# 检查 PyTorch 版本
print("PyTorch version:", torch.__version__)

# 检查 CUDA 版本
print("CUDA version used by PyTorch:", torch.version.cuda)


# 检查是否有可用的 CUDA GPU
cuda_available = torch.cuda.is_available()
print("Is CUDA available?", cuda_available)

# 如果有可用的 CUDA GPU,输出 GPU 的数量和名称
if cuda_available:
    print("Number of GPUs available:", torch.cuda.device_count())
    print("GPU Name:", torch.cuda.get_device_name(0))

3. 测试代码的返回结果

PyTorch version: 2.4.0+cu118
CUDA version used by PyTorch: 11.8
Is CUDA available? True
Number of GPUs available: 1
GPU Name: Tesla V100-SXM2-32GB

4. 不用venv,用conda管理环境(补充)

conda安装不同版本python比较方便,已测试
在conda环境下pip install似乎有限制,经常会网络断开

# 创建环境到持续化路径
conda create --prefix ./work/test_env python=3.9
# 初始化conda(很重要的一步)
eval "$(conda shell.bash hook)"
# 激活环境
conda activate ./work/test_env

5. 项目地址

已经打包好环境的飞桨项目

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值