About torch.autograd.Function

morphism: <p>Hello, everyone</p>
<p>I called RoIAlignFunction from RoIAlignAvg<br>
but there is an error</p>
<p>‘RoIAlignFunctionBackward’ object has no attribute ‘aligned_height’</p>
<p>How can I solve this problem???</p>
<p>Thank you in advance and have a nice day</p>
<p>##############<br>
class RoIAlignAvg(Module):<br>
def <strong>init</strong>(self, aligned_height, aligned_width, spatial_scale):<br>
super(RoIAlignAvg, self).<strong>init</strong>()<br>
self.align_function = RoIAlignFunction(aligned_height+1, aligned_width+1, spatial_scale)</p>
<pre><code>def forward(self, features, rois):
    x = self.align_function.apply(features, rois)
    return avg_pool2d(x, kernel_size=2, stride=1)
</code></pre>
<p>#############<br>
class RoIAlignFunction(Function):<br>
def <strong>init</strong>(self, aligned_height, aligned_width, spatial_scale):<br>
self.aligned_width = int(aligned_width)<br>
self.aligned_height = int(aligned_height)<br>
self.spatial_scale = float(spatial_scale)<br>
self.rois = None<br>
self.feature_size = None</p>
<pre><code>@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</code></pre>

tom: <aside class="quote no-group" data-username="morphism" data-post="1" data-topic="123397">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/morphism/48/28101_2.png" class="avatar"> morphism:</div>
<blockquote>
<p>‘RoIAlignFunctionBackward’ object has no attribute ‘aligned_height’</p>
</blockquote>
</aside>
<p>The thing to know here is that the first argument, which more commonly is called <code>ctx</code> in PyTorch examples because it is NOT a <code>self</code> object is of type <code>RoIAlignFunctionBackward</code>. The code tries to access <code>self.aligned_width</code> but never assigned it.</p>
<p>Best regards</p>
<p>Thomas</p>

morphism: <p>Dear Thomas<br>
Thank you for your explanation</p>
<p>But how should I modify the source above?</p>
<p>Should I write down like this??<br>
output = features.new(num_rois, num_channels, ctx.aligned_height, ctx.width).zero_()</p>

tom: <p>Well, the rename to ctx is a good idea, but really, you would need to find a source for your shape.<br>
For example TorchVision’s roi align-function takes some more parameters (<a href="https://github.com/pytorch/vision/blob/0013d9314cf1bd83eaf38c3ac6e0e9342fa99683/torchvision/csrc/ops/autograd/roi_align_kernel.cpp#L111-L127" class="inline-onebox" rel="noopener nofollow ugc">vision/roi_align_kernel.cpp at 0013d9314cf1bd83eaf38c3ac6e0e9342fa99683 · pytorch/vision · GitHub</a>), maybe the forward should, too, and then assign them to ctx members.</p>

morphism: <p>Thank you, Thomas.<br>
I will look at the github source you recommended.<br>
It will help me a lot.</p>
<p>Have a nice weekend and see you again.<br>
Take care</p>

tom: <p>There are also some projects adding more advances sparse functionality (eg. <a href="https://github.com/huggingface/pytorch_block_sparse" class="inline-onebox" rel="noopener nofollow ugc">GitHub - huggingface/pytorch_block_sparse: Fast Block Sparse Matrices for Pytorch</a> ) that might be worth checking out.</p>