text: # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  import torch
  from torch import nn
  from torch.autograd import Function
  from torch.autograd.function import once_differentiable
  from torch.nn.modules.utils import _pair
  
  from DetectionHub import _C
  # we need this for the custom ops to exist
  import DetectionHub._custom_ops   # noqa: F401
  
  from DetectionHub.utils import amp
  
  class _ROIAlign(Function):
      @staticmethod
      def forward(ctx, input, roi, output_size, spatial_scale, sampling_ratio):
          ctx.save_for_backward(roi)
          ctx.output_size = _pair(output_size)
          ctx.spatial_scale = spatial_scale
          ctx.sampling_ratio = sampling_ratio
          ctx.input_shape = input.size()
          output = torch.ops.DetectionHub.roi_align_forward(
              input, roi, spatial_scale, output_size[0], output_size[1], sampling_ratio
          )
          return output
  
      @staticmethod
      @once_differentiable
      def backward(ctx, grad_output):
          rois, = ctx.saved_tensors
          output_size = ctx.output_size
          spatial_scale = ctx.spatial_scale
          sampling_ratio = ctx.sampling_ratio
          bs, ch, h, w = ctx.input_shape
          grad_input = _C.roi_align_backward(
              grad_output,
              rois,
              spatial_scale,
              output_size[0],
              output_size[1],
              bs,
              ch,
              h,
              w,
              sampling_ratio,
          )
          return grad_input, None, None, None, None
  
  
  roi_align = _ROIAlign.apply
  
  class ROIAlign(nn.Module):
      def __init__(self, output_size, spatial_scale, sampling_ratio):
          super(ROIAlign, self).__init__()
          self.output_size = _pair(output_size)
          self.spatial_scale = spatial_scale
          self.sampling_ratio = sampling_ratio
  
      @amp.float_function
      def forward(self, input, rois):
          if torch._C._get_tracing_state():  # we cannot currently trace through the autograd function
              return torch.ops.DetectionHub.roi_align_forward(
                  input, rois, self.spatial_scale, self.output_size[0], self.output_size[1], self.sampling_ratio)
  
          return roi_align(
              input, rois, self.output_size, self.spatial_scale, self.sampling_ratio
          )
  
      def __repr__(self):
          tmpstr = self.__class__.__name__ + "("
          tmpstr += "output_size=" + str(self.output_size)
          tmpstr += ", spatial_scale=" + str(self.spatial_scale)
          tmpstr += ", sampling_ratio=" + str(self.sampling_ratio)
          tmpstr += ")"
          return tmpstr
  