How to find the source code of conv2d backward function

Tony_Lee: <p>I want to custom a conv2d layer, so I need to change the code of forward and backward function of this layer.<br>
but I can’t find where is the original backward function’s source code of  conb2d function in pytorch.</p>

crcrpar: <p>Hi, <a class="mention" href="/u/tony_lee">@Tony_Lee</a></p>
<p>I think common operations/layers/functions/normalizations are defined in pytorch/pytorch/aten.</p>
<p>How about these 2?</p>
<p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/mkldnn/Conv.cpp" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/mkldnn/Conv.cpp" target="_blank" rel="nofollow noopener">pytorch/pytorch/blob/master/aten/src/ATen/native/mkldnn/Conv.cpp</a></h4>
<pre><code class="lang-cpp">#include &lt;ATen/ATen.h&gt;
#include &lt;ATen/NativeFunctions.h&gt;
#include &lt;ATen/Config.h&gt;

#if !AT_MKLDNN_ENABLED()

namespace at { namespace native {

at::Tensor mkldnn_convolution(
    const at::Tensor&amp; input, const at::Tensor&amp; weight, const at::Tensor&amp; bias,
    IntList padding, IntList stride, IntList dilation) {
  throw std::runtime_error("mkldnn_convolution_forward: ATen not compiled with MKLDNN support");
}

at::Tensor mkldnn_convolution_backward_input(
    IntList input_size, const at::Tensor&amp; grad_output, const at::Tensor&amp; weight,
    IntList padding, IntList stride, IntList dilation, bool bias_defined) {
  throw std::runtime_error("mkldnn_convolution_backward_input: ATen not compiled with MKLDNN support");
}

</code></pre>

  This file has been truncated. <a href="https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/mkldnn/Conv.cpp" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<br>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/cudnn/Conv.cpp" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/cudnn/Conv.cpp" target="_blank" rel="nofollow noopener">pytorch/pytorch/blob/master/aten/src/ATen/native/cudnn/Conv.cpp</a></h4>
<pre><code class="lang-cpp">#include &lt;ATen/ATen.h&gt;
#include &lt;ATen/NativeFunctions.h&gt;
#include &lt;ATen/Config.h&gt;
#include &lt;ATen/cuda/CUDAConfig.h&gt;

#if !AT_CUDNN_ENABLED()

namespace at { namespace native {

// See Note [ATen preprocessor philosophy]

at::Tensor cudnn_convolution(
    const at::Tensor&amp; input, const at::Tensor&amp; weight, const at::Tensor&amp; bias /* optional */,
    IntList padding, IntList stride, IntList dilation,
    int64_t groups, bool benchmark, bool deterministic) {
  throw std::runtime_error("cudnn_convolution: ATen not compiled with cuDNN support");
}

at::Tensor cudnn_convolution_backward_input(
    IntList input_size, const at::Tensor&amp; grad_output, const at::Tensor&amp; weight,
</code></pre>

  This file has been truncated. <a href="https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/cudnn/Conv.cpp" target="_blank" rel="nofollow noopener">show original</a>

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

Tony_Lee: <p>oh, I see, thanks !<br>
Are there any python interface that I can use to customize the grad_weight and grad_input?</p>

crcrpar: <p>If you want to manipulate gradients directly, to <code>module.register_backward_hook</code> <a href="https://pytorch.org/docs/master/nn.html#torch.nn.Module.register_backward_hook" rel="nofollow noopener">doc</a> is one alternative.</p>
<p>Another alternative is to override <code>nn.Conv2d</code>.</p>

Tony_Lee: <p>yes,I have tried to define a conv2d layer, but it doesn’t work<br>
if you have done this thing . could you share me your demo code<br>
my code is like this, but it doesn’t work</p>
<p>class Conv2dF(Function):</p>
<pre><code>@staticmethod
def forward(cxt, input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1):
        
    cxt.save_for_backward(input, weight, bias)

    return F.conv2d(input, weight, bias, stride, padding, dilation, groups)


@staticmethod
def backward(cxt, grad_output):
    input, weight, bias = cxt.saved_variables
            
    grad_input = grad_weight= grad_bias = None

    if cxt.needs_input_grad[0]:
        grad_input = torch.nn.grad.conv2d_input(input.shape, weight, grad_output)
        
    if cxt.needs_input_grad[1]:
        grad_weight = torch.nn.grad.conv2d_weight(input, weight.shape, grad_output)
            
    if bias is not None and cxt.needs_input_grad[2]:
        grad_bias = grad_output.sum(0).squeeze(0)
    
    if bias is not None:
        return grad_input, grad_weight, grad_bias
    else:
        return grad_input, grad_weight</code></pre>

crcrpar: <p>Sorry, but I’ve never experienced that thing…<img src="https://discuss.pytorch.org/images/emoji/apple/pensive.png?v=5" title=":pensive:" class="emoji" alt=":pensive:"></p>

Tony_Lee: <p>Anyway, thank you so much！</p>

Waylon: <p>Hey Tony, I want to do the same thing as you do, modifying the conv2d layer. Did you figure this out?</p>

Tony_Lee: <p>yes ,I have solved it. I just want to define the weight , so I change the value of weight and send it into the conv2d function</p>