Conv2D source code

blade: <p>I was trying to look at the implementation of the 2D convolution on PyTorch. I found <a href="https://github.com/pytorch/pytorch/blob/e1cfac42b2b3ea96d3783c66c8d2e44244a40524/torch/nn/modules/conv.py#L316" rel="noopener nofollow ugc"><code>Conv2D</code> class</a>, which is using <a href="https://github.com/pytorch/pytorch/blob/e1cfac42b2b3ea96d3783c66c8d2e44244a40524/torch/nn/modules/conv.py#L459" rel="noopener nofollow ugc"><code>F.conv2</code></a>, but I can’t get to the source code for the actual implementation, except for these 2 lines:</p>
<pre><code class="lang-auto">@overload
def conv2d(input: Tensor, weight: Tensor, bias: Optional[Tensor]=None, stride: Union[_int, _size]=1, padding: Union[_int, _size]=0, dilation: Union[_int, _size]=1, groups: _int=1) -&gt; Tensor: ...
@overload
def conv2d(input: Tensor, weight: Tensor, bias: Optional[Tensor]=None, stride: Union[_int, _size]=1, padding: str="valid", dilation: Union[_int, _size]=1, groups: _int=1) -&gt; Tensor: ...
</code></pre>
<p>Would appreciate if someone can direct me to the right place. Thanks!</p>

Afekst: <p>Of course because PyTorch is open-source you can see it, but when using it within a python code you actually run a pre-compiled C++ code so it isn’t available.<br>
The actual implementation can be found on GitHub</p><aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/pytorch/blob/e1cfac42b2b3ea96d3783c66c8d2e44244a40524/aten/src/ATen/native/Convolution.cpp#L799">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/blob/e1cfac42b2b3ea96d3783c66c8d2e44244a40524/aten/src/ATen/native/Convolution.cpp#L799" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/e1cfac42b2b3ea96d3783c66c8d2e44244a40524/aten/src/ATen/native/Convolution.cpp#L799" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/e1cfac42b2b3ea96d3783c66c8d2e44244a40524/aten/src/ATen/native/Convolution.cpp#L799</a></h4>



    <pre class="onebox"><code class="lang-cpp">
      <ol class="start lines" start="789" style="counter-reset: li-counter 788 ;">
          <li>  std::tie(input, is_batched) = batchify(input_, /*num_spatial_dims=*/ 1, "conv1d");</li>
          <li>  Tensor output;</li>
          <li>  if (at::isComplexType(input_.scalar_type())) {</li>
          <li>    output = complex_convolution(input, weight, bias, stride, padding, dilation, {0}, groups);</li>
          <li>  } else {</li>
          <li>    output = at::convolution(input, weight, bias, stride, padding, dilation, false, {0}, groups);</li>
          <li>  }</li>
          <li>  return is_batched ? output : output.squeeze(0);</li>
          <li>}</li>
          <li>
          </li>
<li class="selected">at::Tensor conv2d(</li>
          <li>    const Tensor&amp; input_, const Tensor&amp; weight, const c10::optional&lt;Tensor&gt;&amp; bias_opt,</li>
          <li>    IntArrayRef stride, IntArrayRef padding, IntArrayRef dilation, int64_t groups) {</li>
          <li>  // See [Note: hacky wrapper removal for optional tensor]</li>
          <li>  c10::MaybeOwned&lt;Tensor&gt; bias_maybe_owned = at::borrow_from_optional_tensor(bias_opt);</li>
          <li>  const Tensor&amp; bias = *bias_maybe_owned;</li>
          <li>
          </li>
<li>  Tensor input;</li>
          <li>  bool is_batched;</li>
          <li>  std::tie(input, is_batched) = batchify(input_, /*num_spatial_dims=*/ 2, "conv2d");</li>
          <li>  Tensor output;</li>
      </ol>
    </code></pre>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>This is true for a lot of “basic” things in PyTorch.</p>