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