How to verify your CUDA and pytorch

1.Test CUDA

This script will attempt to create a tensor on the GPU and perform a simple computation.

python 复制代码
import torch

def test_cuda():
    # Check CUDA availability
    if torch.cuda.is_available():
        print("CUDA is available on this device.")
        # Set device to CUDA
        device = torch.device("cuda")
        # Create a tensor and move it to CUDA
        x = torch.rand(10, 10).to(device)
        print("Successfully created a tensor on CUDA:", x)
        # Perform a simple computation
        y = x * x
        print("Output of the computation:", y)
    else:
        print("CUDA is not available. Check your installation and driver.")

if __name__ == "__main__":
    test_cuda()

Run the script: Use a terminal or command prompt to run the script with Python.

bash 复制代码
python test_cuda.py

The script should print whether CUDA is available, show the tensor created on the GPU, and display the output of a simple multiplication operation. If there are any errors during these steps, they will help pinpoint what might be wrong with your CUDA setup.

2.Check PyTorch Compatibility

python 复制代码
# test_torch.py
import torch
print(torch.__version__)
print(torch.cuda.is_available())

Run the script: Use a terminal or command prompt to run the script with Python.

bash 复制代码
python test_torch.py

If torch.cuda.is_available() returns True, then PyTorch is able to use CUDA.

相关推荐
默 语25 分钟前
HarmonyOS7 鼠标画板实战:onMouse、onTouch 与 Canvas 实现手绘画板(附完整案例)
pytorch·深度学习·计算机外设
会飞锦鲤2 小时前
基于Mask R-CNN 手机屏幕缺陷检测
pytorch·深度学习·计算机视觉·缺陷检测·实例分割·工业视觉·mask r-cnn
aifannihao4 小时前
AI 学习第 29 课:loss 不动先查梯度,177 的范数不是爆炸是累积
pytorch·深度学习·梯度·学习路线·训练监控
严肃小白兔8 小时前
第 19/24 期:循环神经网络及其变体的数学原理
人工智能·rnn·深度学习
隔窗听雨眠10 小时前
机器学习数据一致性的六个断点与工程化收口
机器学习·数据质量·数据工程
iiiiii1111 小时前
从最大熵到 SAC:Soft Q-Learning 与 Soft Actor-Critic 公式推导笔记
人工智能·笔记·算法·机器学习·强化学习
罗西的思考1 天前
【轻量化复刻大模型 / KIMI K3】MXFP4 / MXFP8 量化感知训练(QAT)& 演示
人工智能·算法·机器学习
zs1996_1 天前
推荐系统学习笔记
人工智能·深度学习·推荐算法
阿enna1 天前
从训练到部署:导出ONNX并检查YOLO26n的一致性
人工智能·深度学习·神经网络·yolo·目标检测·计算机视觉·cnn