How to get ctx.xx outside the custom autograd function?

Naruto-Sasuke: <p>When I implement custom autograd function.<br>
In the forward of class  <code>xxxFunction(torch.autograd.Function)</code>,<br>
I get <code>ctx.matches</code>, then I want to get the <code>ctx.matches</code> outside this function.<br>
I tried</p>
<pre><code class="lang-auto">@staticmethod
def get_matches(ctx):
    return ctx.matches
</code></pre>
<p>Then in the class <code>xxx(torch.nn.Module)</code>, I run <code>xxxFunction.get_matches()</code>.<br>
<code>get_matches() takes exactly 1 argument (0 given)</code>.</p>
<p>Thank you  in advance!</p>

Naruto-Sasuke: <p><a class="mention" href="/u/alband">@albanD</a> could you help me in this problem?</p>

albanD: <p>Hi,</p>
<p>You can access this from the <code>backward</code> method that gets the same <code>ctx</code> as input.<br>
I don’t think you can extract it in a reliable way though.<br>
Why do you need to do this? Can’t you just return it as another output of the <code>forward</code> method?</p>

cosmozhang1988: <p>I am facing the same issue. If I return it as another output of the <code>forward</code> method, how to adjust the <code>backward</code> method? <a class="mention" href="/u/alband">@albanD</a><br>
Many thanks!</p>

albanD: <p>The backward method should get and return None for everything that is not differentiable.</p>

Zhaoyi-Yan: <p>Here is an example:<br>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/Zhaoyi-Yan/Shift-Net_pytorch/blob/master/models/shift_net/InnerShiftTriple.py#L34" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/Zhaoyi-Yan/Shift-Net_pytorch/blob/master/models/shift_net/InnerShiftTriple.py#L34" target="_blank" rel="nofollow noopener">Zhaoyi-Yan/Shift-Net_pytorch/blob/master/models/shift_net/InnerShiftTriple.py#L34</a></h4>
<pre class="onebox"><code class="lang-py"><ol class="start lines" start="24" style="counter-reset: li-counter 23 ;">
<li>    cur_device = torch.cuda.current_device()</li>
<li>    self.cur_mask = self.mask_all[cur_device*cur_bsize:(cur_device+1)*cur_bsize, :, :, :]</li>
<li>
</li>
<li># If mask changes, then need to set cal_fix_flag true each iteration.</li>
<li>def forward(self, input):</li>
<li>    self.bz, self.c, self.h, self.w = input.size()</li>
<li>    self._split_mask(self.bz)</li>
<li>    self.flag = util.cal_flag_given_mask_thred(self.cur_mask, self.shift_sz, self.stride, self.mask_thred)</li>
<li>    final_out = InnerShiftTripleFunction.apply(input, self.shift_sz, self.stride, self.triple_weight, self.flag, self.show_flow)</li>
<li>    if self.show_flow:</li>
<li class="selected">        self.flow_srcs = InnerShiftTripleFunction.get_flow_src()</li>
<li>    return final_out</li>
<li>
</li>
<li>def get_flow(self):</li>
<li>    return self.flow_srcs</li>
<li>
</li>
<li>def set_flow_true(self):</li>
<li>    self.show_flow = True</li>
<li>
</li>
<li>def set_flow_false(self):</li>
<li>    self.show_flow = False</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>