About torch.autograd.Function

Hello, everyone

I called RoIAlignFunction from RoIAlignAvg
but there is an error

‘RoIAlignFunctionBackward’ object has no attribute ‘aligned_height’

How can I solve this problem???

Thank you in advance and have a nice day

##############
class RoIAlignAvg(Module):
def init(self, aligned_height, aligned_width, spatial_scale):
super(RoIAlignAvg, self).init()
self.align_function = RoIAlignFunction(aligned_height+1, aligned_width+1, spatial_scale)

def forward(self, features, rois):
    x = self.align_function.apply(features, rois)
    return avg_pool2d(x, kernel_size=2, stride=1)

#############
class RoIAlignFunction(Function):
def init(self, aligned_height, aligned_width, spatial_scale):
self.aligned_width = int(aligned_width)
self.aligned_height = int(aligned_height)
self.spatial_scale = float(spatial_scale)
self.rois = None
self.feature_size = None

@staticmethod
def forward(self, features, rois):
    self.rois = rois
    self.feature_size = features.size()
    batch_size, num_channels, data_height, data_width = features.size()
    num_rois = rois.size(0)
   ######### error occurs here!!
    output = features.new(num_rois, num_channels, self.aligned_height, self.aligned_width).zero_()
    roi_align.roi_align_forward(self.aligned_height,self.aligned_width,self.spatial_scale, features,rois, output)
    return output

@staticmethod
def backward(self, grad_output):
    batch_size, num_channels, data_height, data_width = self.feature_size
    grad_input = self.rois.new(batch_size, num_channels, data_height,data_width).zero_()
    roi_align.roi_align_backward_cuda(self.aligned_height,self.aligned_width,self.spatial_scale,grad_output,self.rois, grad_input)
    return grad_input, None

The thing to know here is that the first argument, which more commonly is called ctx in PyTorch examples because it is NOT a self object is of type RoIAlignFunctionBackward. The code tries to access self.aligned_width but never assigned it.

Best regards

Thomas

Dear Thomas
Thank you for your explanation

But how should I modify the source above?

Should I write down like this??
output = features.new(num_rois, num_channels, ctx.aligned_height, ctx.width).zero_()

Well, the rename to ctx is a good idea, but really, you would need to find a source for your shape.
For example TorchVision’s roi align-function takes some more parameters (vision/roi_align_kernel.cpp at 0013d9314cf1bd83eaf38c3ac6e0e9342fa99683 · pytorch/vision · GitHub), maybe the forward should, too, and then assign them to ctx members.

Thank you, Thomas.
I will look at the github source you recommended.
It will help me a lot.

Have a nice weekend and see you again.
Take care

There are also some projects adding more advances sparse functionality (eg. GitHub - huggingface/pytorch_block_sparse: Fast Block Sparse Matrices for Pytorch ) that might be worth checking out.