Cannot run torchvision.transforms.functional.perspective on GPU

I am trying to perform a perspective transform, as folllows:

        B, C, H, W = reference_image.shape

        # Define source points (original corners of the target image)
        src_points = torch.tensor([
            [0, 0],  # Top-left
            [W - 1, 0],  # Top-right
            [W - 1, H - 1],  # Bottom-right
            [0, H - 1]  # Bottom-left
        ], dtype=torch.float32, device=reference_image.device)

        src_points = src_points.unsqueeze(0).repeat(B, 1, 1)  # (B, 4, 2)

        warped_images = []
        for i in range(B):
            warped = TF.perspective(
                target_image[i],
                src_points[i],
                predicted_points[i],
                interpolation=TF.InterpolationMode.BILINEAR,
                fill=0,
            )

where all target_image, src_points, predicted_points are definitely tensord in the GPUY (device is cuda). However when I run this, I get the error:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers-pro\pydevd_asyncio\pydevd_asyncio_utils.py", line 117, in _exec_async_code
    result = func()
             ^^^^^^
  File "<input>", line 1, in <module>
  File "C:\Users\AlbertoGomez\.conda\envs\torch\Lib\site-packages\torchvision\transforms\functional.py", line 741, in perspective
    coeffs = _get_perspective_coeffs(startpoints, endpoints)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\AlbertoGomez\.conda\envs\torch\Lib\site-packages\torchvision\transforms\functional.py", line 701, in _get_perspective_coeffs
    res = torch.linalg.lstsq(a_matrix, b_matrix, driver="gels").solution.to(torch.float32)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument b in method wrapper_CUDA_out_linalg_lstsq_out)

It seems that internally, the perspective transform crerates tensors in the cpu and there is no way to move them to cuda. For the record, I have verified that both torch and torchvision are installed with cuda support (cuda 12.8). My torch versions are:

torch.version
β€˜2.7.0+cu128’
torchvision.version
β€˜0.22.0+cu128’

Anybody knows what I am doing wrong or how can I run this on GPU? Thanks!

Thanks you for reporting this issue! I can reproduce it and have created a GitHub issue here.

1 Like