When the device doesn’t support double (eg. intel iGPUs with torch-directml), this function will throw RuntimError
:
def tensor_totype(t):
dtype = torch.float if t.is_mps else torch.double
return t.to(dtype=dtype)
Example:
>>> import torch
>>> import torch_directml
>>> device = torch_directml.device(1) # UHD Graphics 770
>>> x = torch.ones(5, device=device)
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\u7755376\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\_tensor.py", line 463, in __repr__
return torch._tensor_str._str(self, tensor_contents=tensor_contents)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\u7755376\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\_tensor_str.py", line 698, in _str
return _str_intern(self, tensor_contents=tensor_contents)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\u7755376\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\_tensor_str.py", line 618, in _str_intern
tensor_str = _tensor_str(self, indent)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\u7755376\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\_tensor_str.py", line 350, in _tensor_str
formatter = _Formatter(get_summarized_data(self) if summarize else self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\u7755376\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\_tensor_str.py", line 147, in __init__
nonzero_finite_abs = tensor_totype(nonzero_finite_vals.abs())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\u7755376\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\_tensor_str.py", line 119, in tensor_totype
return t.to(dtype=dtype)
^^^^^^^^^^^^^^^^^
RuntimeError: The GPU device does not support Double (Float64) operations!
Would there be any way to fix this?