docstr: Compute gradients for ROIAlignRotated with multiple bounding boxes on the GPU,
  and compare the result with ROIAlign
docstr_tok: ["Compute","gradients","for","ROIAlignRotated","with","multiple","bounding","boxes","on","the","GPU",",","and","compare","the","result","with","ROIAlign"]
code: def test_roi_align_rotated_gradient_cuda(self):
      """
      Compute gradients for ROIAlignRotated with multiple bounding boxes on the GPU,
      and compare the result with ROIAlign
      """
      # torch.manual_seed(123)
      dtype = torch.float64
      device = torch.device("cuda")
      pool_h, pool_w = (5, 5)
  
      roi_align = ROIAlign(
          output_size=(pool_h, pool_w),
          spatial_scale=1,
          sampling_ratio=2,
      ).to(device=device)
  
      roi_align_rotated = ROIAlignRotated(
          output_size=(pool_h, pool_w),
          spatial_scale=1,
          sampling_ratio=2,
      ).to(device=device)
  
      x = torch.rand(
          1, 1, 10, 10, dtype=dtype, device=device, requires_grad=True
      )
      # x_rotated = x.clone() won't work (will lead to grad_fun=CloneBackward)!
      x_rotated = Variable(x.data.clone(), requires_grad=True)
  
      # roi_rotated format is (batch index, x_center, y_center, width, height, angle)
      rois_rotated = torch.tensor(
          [
              [0, 4.5, 4.5, 9, 9, 0],
              [0, 2, 7, 4, 4, 0],
              [0, 7, 7, 4, 4, 0],
          ],
          dtype=dtype,
          device=device,
      )
  
      y_rotated = roi_align_rotated(x_rotated, rois_rotated)
      s_rotated = y_rotated.sum()
      s_rotated.backward()
  
      # roi format is (batch index, x1, y1, x2, y2)
      rois = torch.tensor(
          [[0, 0, 0, 9, 9], [0, 0, 5, 4, 9], [0, 5, 5, 9, 9]],
          dtype=dtype,
          device=device,
      )
  
      y = roi_align(x, rois)
      s = y.sum()
      s.backward()
  
      assert torch.allclose(
          x.grad, x_rotated.grad
      ), "gradients for ROIAlign and ROIAlignRotated mismatch on CUDA"
  
file_path: detectron2/tests/layers/test_roi_align_rotated.py
imports: import logging
  import unittest
  import cv2
  import torch
  from torch.autograd import Variable, gradcheck
  from detectron2.layers.roi_align import ROIAlign
  from detectron2.layers.roi_align_rotated import ROIAlignRotated
label_desc: Use image node_id 8 for calling the ROIAlignRotatedTest obj's underlying member method code with example usage: obj.test_roi_align_rotated_gradient_cuda() without return types