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.

相关推荐
聆风吟º3 分钟前
CANN runtime 性能优化:异构计算下运行时组件的效率提升与资源利用策略
人工智能·深度学习·神经网络·cann
一山秋叶5 分钟前
带分数正则的一致性蒸馏
人工智能·深度学习
Aspect of twilight10 分钟前
Mind-Cube介绍
人工智能·深度学习
肾透侧视攻城狮24 分钟前
《Transformer模型PyTorch实现全攻略:架构拆解、代码示例与优化技巧》
深度学习·transformer·构建transformer模型·定义多头注意力模块·定义位置前馈网络·构建解/编码器模块·训练transformer模型
爱吃大芒果30 分钟前
CANN ops-nn 算子开发指南:NPU 端神经网络计算加速实战
人工智能·深度学习·神经网络
聆风吟º31 分钟前
CANN ops-nn 实战指南:异构计算场景中神经网络算子的调用、调优与扩展技巧
人工智能·深度学习·神经网络·cann
乾元39 分钟前
身份与访问:行为生物识别(按键习惯、移动轨迹)的 AI 建模
运维·网络·人工智能·深度学习·安全·自动化·安全架构
啊阿狸不会拉杆39 分钟前
《机器学习导论》第 7 章-聚类
数据结构·人工智能·python·算法·机器学习·数据挖掘·聚类
木非哲1 小时前
机器学习--从“三个臭皮匠”到 XGBoost:揭秘 Boosting 算法的“填坑”艺术
算法·机器学习·boosting
love you joyfully1 小时前
告别“人多力量大”误区:看AI团队如何通过奖励设计实现协作韧性
人工智能·深度学习·神经网络·多智能体