Convolution backward path for CUDA

username: Stonepia
cooked: <p>Hi,<br>
I want to write a convolution backward function , and it would call Pytorch’s cuda path for default. Basically it has the following definition:</p>
<pre><code class="lang-auto">std::tuple&lt;at::Tensor,at::Tensor,at::Tensor&gt;  
custom_convolution_backward(const at::Tensor &amp; grad_output, 
                    const at::Tensor &amp; input, 
                    const at::Tensor &amp; weight, 
                    at::IntArrayRef stride, 
                    at::IntArrayRef padding, 
                    at::IntArrayRef dilation,
                    bool transposed, 
                    at::IntArrayRef output_padding, 
                    int64_t groups, 
                    std::array&lt;bool,3&gt; output_mask);
</code></pre>
<p>I explore the source code in PyTorch and find that CUDA uses <a href="https://github.com/pytorch/pytorch/blob/6c5a1c50bf59ad917f82d4a4861243d3b2069fe1/aten/src/ATen/native/cuda/ConvolutionMM2d.cu#L7" rel="noopener nofollow ugc">slow_conv2d_backward_out_cuda</a>.</p>
<p>My question is :</p>
<ol>
<li>Is <code>slow_conv2d_backward_out_cuda</code> the right function I need to use?</li>
<li>The function has args of <code>finput</code> and <code>fgrad_input</code> <a href="https://github.com/pytorch/pytorch/blob/6c5a1c50bf59ad917f82d4a4861243d3b2069fe1/aten/src/ATen/native/cuda/ConvolutionMM2d.cu#L17-L18" rel="noopener nofollow ugc">finput</a>, what are these two? I can’t find docs nor comments on them.</li>
<li>Why can’t I find the convolution backward function in <a href="https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/Convolution.cpp" rel="noopener nofollow ugc">Convolution.cpp</a> ? Isn’t it should write the backward path as in cudnn ?</li>
</ol>

username: Stonepia
cooked: <p>I have figured this out and I may answer the question myself. Hope it will offer some help for coming visitors.</p>
<ol>
<li>
<p>The function that needs to be used is <a href="https://github.com/pytorch/pytorch/blob/6c5a1c50bf59ad917f82d4a4861243d3b2069fe1/aten/src/ATen/cuda/LegacyTHFunctionsCUDA.cpp#L3546" rel="noopener nofollow ugc">thnn_conv2d_backward</a>, the method to find this is to read the <a href="https://github.com/pytorch/pytorch/blob/a1db5b0f2bcd96f1e884ff63ebcf2b01f543a77a/tools/autograd/derivatives.yaml#L1446-L1447" rel="noopener nofollow ugc">derivatives.yaml</a>. This file will record the forward and backward function.<br>
{Note}  If you come to this and find that there is no THNN function, you may refer to this <a href="https://github.com/pytorch/pytorch/issues/24507" rel="noopener nofollow ugc">issue</a>, because the pytorch teams are migrating to newer ATen function. The ATen is modern implementation for it.</p>
</li>
<li>
<p>The finput and fgradinput are just two temporary buffer. Which is cols and ones if you refer to Convolution implementation papers. Sorry I forget the paper’s name so you may search by your self.<br>
The thing is, you don’t have to worry about these two, just pass empty tensor is enough, they serve as only a buffer. You may find the usage like</p>
<pre><code class="lang-auto">at::Tensor finput = at::empty({0},input.options()); 
at::Tensor fgrad_input = at::empty({0}, input.options());
auto kernel_size = weight.sizes().slice(2);
auto &amp;&amp;result = at::thnn_conv2d_backward(grad_output, input, weight,kernel_size , stride, padding, 
finput, fgrad_input, output_mask);
</code></pre>
</li>
<li>
<p>This could be find on derivatives.yaml as stated before.</p>
</li>
</ol>