Newbie question: cannot use `save_for_backward`

sapo: <p>After having ridden the <a href="https://pytorch.org/docs/stable/notes/extending.html" rel="nofollow noopener">how to</a> about extending autograd, I’m trying to do a little experiment:</p>
<pre><code class="lang-python">from torch.autograd import Function
import torch

class MyCustomOp(Function):                            
                                                                
    @staticmethod                                               
    def forward(ctx, mat):                                      
        indices = mat.sum(2)                                    
        ctx.save_for_bacward(mat.shape, indices)                
        # just return something and save stuffs for later

        return indices                                          
                                                                
    @staticmethod                                               
    def backward(ctx, grad_indices):                            
        mat_shape, indices = ctx.saved_tensors                  
                                                                
        # just some random computation using saved tensors      
        grad_mat = indices[..., None].expand(mat_shape)         
        return grad_mat                                         

MyCustomOp.apply(torch.rand(10, 10, 10, 10))
</code></pre>
<p>However, I get: <code>AttributeError: 'MyCustomOpBackward' object has no attribute 'save_for_bacward'</code></p>
<p>What’s wrong?</p>
<p>Torch version <code>1.4.0</code>.</p>

albanD: <p>Hi,</p>
<p>You’re missing <code>k</code> in <code>save_for_backward</code> <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>Also keep in mind that you should use <code>save_for_backward()</code> <strong>only</strong> for input or output Tensors. Other intermediary Tensors or input/output of other type can just be saved in the ctx as <code>ctx.mat_shape = mat.shape</code> in your case.</p>

sapo: <aside class="quote no-group" data-username="albanD" data-post="2" data-topic="69367">
<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/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p>You’re missing <code>k</code> in <code>save_for_backward</code> <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside>
<p>I’m not getting it, sorry…</p>
<aside class="quote no-group" data-username="albanD" data-post="2" data-topic="69367">
<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/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p>Also keep in mind that you should use <code>save_for_backward()</code> <strong>only</strong> for input or output Tensors. Other intermediary Tensors or input/output of other type can just be saved in the ctx as <code>ctx.mat_shape = mat.shape</code> in your case.</p>
</blockquote>
</aside>
<p>Ah, good news, thanks for that!</p>

albanD: <p>In your code you use <code>save_for_bacward</code>, it should be <code>save_for_backward</code>.</p>

sapo: <p>Ah <img src="https://discuss.pytorch.org/images/emoji/apple/cowboy_hat_face.png?v=9" title=":cowboy_hat_face:" class="emoji" alt=":cowboy_hat_face:"></p>