Tensor_totype issues with device doesn't support double

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?

@chn-lee-yumi , It is due to that the iGPU does not support double. Does the fix here mean XPU should not raise RuntimeError?

By the way, you can check if the device supports FP64 through get_device_capability.

import torch
xpu_cap = torch.xpu.get_device_capability()

# Take Intel(R) Data Center GPU Max 1550 as an example, its device capability is as follows:
# {
#    'architecture': 13136561920,
#    'gpu_eu_count': 512, 
#    'gpu_subslice_count': 64,
#    'has_atomic64': True,
#    'has_bfloat16_conversions': True,
#    'has_fp16': True, 
#    'has_fp64': True, 
#    'has_subgroup_2d_block_io': True, 
#    'has_subgroup_matrix_multiply_accumulate': True, 
#    'has_subgroup_matrix_multiply_accumulate_tensor_float32': False,
#    'max_compute_units': 512,
#    'max_num_sub_groups': 64,
#    'max_work_group_size': 1024, 
#    'name': 'Intel(R) Data Center GPU Max 1550',
#    'platform_name': 'Intel(R) oneAPI Unified Runtime over Level-Zero',
#    'sub_group_sizes': [16, 32], 
#    'total_memory': 68719476736, 
#    'type': 'gpu',
#    'vendor': 'Intel(R) Corporation',
#    'version': '12.60.7'
# }

if xpu_cap["has_fp64"]:
  # Do something