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.

相关推荐
汤姆宝库13 分钟前
蒸馏的三层实现:logits级、序列级与数据级
人工智能·深度学习·机器学习
搬砖的前端1 小时前
AI工具集:Ollama 加载 MLX 混合量化模型 panic失败修正量化标签int4→int8
深度学习
硅谷秋水6 小时前
BAAI的PhysCapBench分析
人工智能·机器学习·机器人
硅谷秋水10 小时前
逆矩阵的Physis-v1.0
人工智能·机器学习·计算机视觉·语言模型·机器人
木头左12 小时前
基于LSTM神经网络的ETF价格突破点预测策略回测验证
深度学习·lstm·python量化·价格突破·回测验证
会议咨询15 小时前
2027年计算机工程、数据处理与机器学习国际会议(CDML 2027)
机器学习·数据处理·计算机工程
龙腾AI白云16 小时前
视觉运动控制与灵巧抓取:具身智能手眼协调的学习
大数据·数据库·人工智能·机器学习·pygame
罗西的思考1 天前
【轻量化复刻大模型 / KIMI K3】K3 PostTrain 后训练交互式演示
人工智能·算法·机器学习
电科_银尘1 天前
【大模型算法与工程 39】PPO:策略比率、裁剪目标与训练稳定性
人工智能·算法·机器学习