I am learning python torch nowadays. There was a good example for newbies on youtube. (https://youtu.be/r9IqwpMR9TE?si=VYCbx3O-3BX9pPZx&t=887) It uses pytorch cuda library. It shows the time spent calculating via CPU and GPU respectively.
When i tried to run there is an error on calling a function. torch.cuda.synchronize()
Here is error code:
Traceback (most recent call last):
File "c:\Users\msaid\OneDrive - Bingöl Üniversitesi\Python\erMeydani\Vyeni\CUDAtest.py", line 25, in <module>
torch.cuda.synchronize()
File "C:\Program Files\Python311\Lib\site-packages\torch\cuda\__init__.py", line 687, in synchronize
with torch.cuda.device(device):
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\site-packages\torch\cuda\__init__.py", line 312, in __init__
self.idx = _get_device_index(device, optional=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\site-packages\torch\cuda\_utils.py", line 27, in _get_device_index
if isinstance(device, torch.device):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union
and here is my sample code:
import torch
torch.cuda.empty_cache()
if torch.cuda.is_available():
device=torch.device=("cuda")
else:
device=torch.device=("cpu")
print("using", device)
import time
matrix_size= 32*256
x=torch.randn(matrix_size, matrix_size)
y=torch.randn(matrix_size, matrix_size)
print("*******************CPU SPEED*********************")
start=time.time()
result=torch.matmul(x,y)
print(time.time()-start)
print("verify device", result.device)
x_gpu=x.to(device)
y_gpu=y.to(device)
torch.cuda.synchronize()
for i in range(3):
print("*******************GPU SPEED*********************")
start=time.time()
result_gpu=torch.matmul(x_gpu,y_gpu)
print(time.time()-start)
print("verify device", result_gpu.device)
I tried to call function with device
variable torch.cuda.synchronize(device)
, but same error again.
The problem is in these lines of code:
if torch.cuda.is_available():
device=torch.device=("cuda")
else:
device=torch.device=("cpu")
replace torch.device=("cuda")
by torch.device("cuda")
, likewise for cpu.