Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

     

Check PyTorch version, CPU and GPU(CUDA) in PyTorch

Buy Me a Coffee

*My post explains how to create and acceess a tensor.

__version__ can check PyTorch version as shown below. *__version__ can be used withtorch but not with a tensor:

importtorchtorch.__version__# 2.2.1+cu121
Enter fullscreen modeExit fullscreen mode

cpu.is_available(),cpu.device_count() orcpu.current_device() can check if CPU is available, getting a scalar as shown below:

*Memos:

  • cpu.is_available(),cpu.device_count() orcpu.current_device() can be used withtorch but not with a tensor.
  • cpu.device_count() can get the number of CPUs. *It always gets1:
  • cpu.current_device() can get the index of a currently selected CPU. *It always getscpu:
importtorchtorch.cpu.is_available()# Truetorch.cpu.device_count()# 1torch.cpu.current_device()# cpu
Enter fullscreen modeExit fullscreen mode

cuda.is_available() orcuda.device_count() can check if GPU(CUDA) is available, getting a scalar as shown below:

*Memos:

  • cuda.is_available() orcuda.device_count() can be used withtorch but not with a tensor.
  • cuda.device_count() can get the number of GPUs.
importtorchtorch.cuda.is_available()# Truetorch.cuda.device_count()# 1
Enter fullscreen modeExit fullscreen mode

In addition, you can usecuda.current_device(),cuda.get_device_name() orcuda.get_device_properties(), getting a scalar as shown below:

*Memos:

  • cuda.current_device(),cuda.get_device_name() orcuda.get_device_properties() can be used withtorch but not with a tensor.
  • cuda.current_device() can get the index of a currently selected GPU.
  • cuda.get_device_name() can get the name of a GPU.*Memos:
    • The 1st argument withtorch isdevice(Optional-Default:None-Type:(str,int ordevice().
    • If it'sNone,cuda.current_device() is used.
    • The number must be zero or positive.
    • Onlycuda can be set todevice.
    • My post explainsdevice().
  • cuda.get_device_properties() can get the properties of a GPU. *Memos:
    • The 1st argument withtorch isdevice(Required-Type:str,int ordevice()).
    • The number must be zero or positive.
    • Onlycuda can be set todevice.
    • My post explainsdevice().
torch.cuda.current_device()# 0torch.cuda.get_device_name()torch.cuda.get_device_name(device='cuda:0')torch.cuda.get_device_name(device='cuda')torch.cuda.get_device_name(device=0)torch.cuda.get_device_name(device=torch.device(device='cuda:0'))torch.cuda.get_device_name(device=torch.device(device='cuda'))torch.cuda.get_device_name(device=torch.device(device=0))torch.cuda.get_device_name(device=torch.device(type='cuda'))torch.cuda.get_device_name(device=torch.device(type='cuda',index=0))# Tesla T4torch.cuda.get_device_properties(device='cuda:0')torch.cuda.get_device_properties(device='cuda')torch.cuda.get_device_properties(device=0)torch.cuda.get_device_properties(device=torch.device(device='cuda:0'))torch.cuda.get_device_properties(device=torch.device(device='cuda'))torch.cuda.get_device_properties(device=torch.device(device=0))torch.cuda.get_device_name(device=torch.device(type='cuda'))torch.cuda.get_device_name(device=torch.device(type='cuda',index=0))# _CudaDeviceProperties(name='Tesla T4', major=7, minor=5, # total_memory=15102MB, multi_processor_count=40)
Enter fullscreen modeExit fullscreen mode

!nvidia-smi can get the information about GPUs as shown below:

!nvidia-smiWedMay1513:18:152024+---------------------------------------------------------------------------------------+|NVIDIA-SMI535.104.05DriverVersion:535.104.05CUDAVersion:12.2||-----------------------------------------+----------------------+----------------------+|GPUNamePersistence-M|Bus-IdDisp.A|VolatileUncorr.ECC||FanTempPerfPwr:Usage/Cap|Memory-Usage|GPU-UtilComputeM.||||MIGM.||=========================================+======================+======================||0TeslaT4Off|00000000:00:04.0Off|0||N/A56CP028W/70W|105MiB/15360MiB|0%Default||||N/A|+-----------------------------------------+----------------------+----------------------++---------------------------------------------------------------------------------------+|Processes:||GPUGICIPIDTypeProcessnameGPUMemory||IDIDUsage||=======================================================================================|+---------------------------------------------------------------------------------------+
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm a web developer.Buy Me a Coffee: ko-fi.com/superkaiSO: stackoverflow.com/users/3247006/super-kai-kazuya-itoX(Twitter): twitter.com/superkai_kazuyaFB: facebook.com/superkai.kazuya
  • Joined

More fromSuper Kai (Kazuya Ito)

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp