# How to find the source code of conv2d backward function

**URL:** <https://discuss.pytorch.org/t/how-to-find-the-source-code-of-conv2d-backward-function/19139>\
**Category:** vision\
**Created:** [June 4, 2018, 1:11am UTC](https://discuss.pytorch.org/t/how-to-find-the-source-code-of-conv2d-backward-function/19139 "2018-06-04T01:11:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Lee](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tony_lee/32/10577_2.png) [@Tony\_Lee](https://discuss.pytorch.org/u/Tony_Lee)\
**Post date:** [June 4, 2018, 1:11am UTC](https://discuss.pytorch.org/t/how-to-find-the-source-code-of-conv2d-backward-function/19139/1 "2018-06-04T01:11:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crcrpar](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/crcrpar/32/24766_2.png) [@crcrpar](https://discuss.pytorch.org/u/crcrpar)\
**Post date:** [June 4, 2018, 3:24am UTC](https://discuss.pytorch.org/t/how-to-find-the-source-code-of-conv2d-backward-function/19139/2 "2018-06-04T03:24:06Z")

</div>

Hi, @Tony_Lee

I think common operations/layers/functions/normalizations are defined in pytorch/pytorch/aten.

How about these 2?

> <https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/mkldnn/Conv.cpp>

  

> <https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/cudnn/Conv.cpp>

---

<div class="post-metadata">

**Author:** ![Tony\_Lee](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tony_lee/32/10577_2.png) [@Tony\_Lee](https://discuss.pytorch.org/u/Tony_Lee)\
**Post date:** [June 4, 2018, 8:32am UTC](https://discuss.pytorch.org/t/how-to-find-the-source-code-of-conv2d-backward-function/19139/3 "2018-06-04T08:32:44Z")

</div>

oh, I see, thanks !  
Are there any python interface that I can use to customize the grad\_weight and grad\_input?

---

<div class="post-metadata">

**Author:** ![crcrpar](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/crcrpar/32/24766_2.png) [@crcrpar](https://discuss.pytorch.org/u/crcrpar)\
**Post date:** [June 4, 2018, 8:59am UTC](https://discuss.pytorch.org/t/how-to-find-the-source-code-of-conv2d-backward-function/19139/4 "2018-06-04T08:59:49Z")

</div>

If you want to manipulate gradients directly, to `module.register_backward_hook` [doc](https://pytorch.org/docs/master/nn.html#torch.nn.Module.register_backward_hook) is one alternative.

Another alternative is to override `nn.Conv2d`.

---

<div class="post-metadata">

**Author:** ![Tony\_Lee](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tony_lee/32/10577_2.png) [@Tony\_Lee](https://discuss.pytorch.org/u/Tony_Lee)\
**Post date:** [June 4, 2018, 10:34am UTC](https://discuss.pytorch.org/t/how-to-find-the-source-code-of-conv2d-backward-function/19139/5 "2018-06-04T10:34:15Z")

</div>

yes,I have tried to define a conv2d layer, but it doesn’t work  
if you have done this thing . could you share me your demo code  
my code is like this, but it doesn’t work

class Conv2dF(Function):

```
@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
```

---

<div class="post-metadata">

**Author:** ![crcrpar](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/crcrpar/32/24766_2.png) [@crcrpar](https://discuss.pytorch.org/u/crcrpar)\
**Post date:** [June 4, 2018, 1:04pm UTC](https://discuss.pytorch.org/t/how-to-find-the-source-code-of-conv2d-backward-function/19139/6 "2018-06-04T13:04:28Z")

</div>

Sorry, but I’ve never experienced that thing…😔

---

<div class="post-metadata">

**Author:** ![Tony\_Lee](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tony_lee/32/10577_2.png) [@Tony\_Lee](https://discuss.pytorch.org/u/Tony_Lee)\
**Post date:** [June 4, 2018, 1:17pm UTC](https://discuss.pytorch.org/t/how-to-find-the-source-code-of-conv2d-backward-function/19139/7 "2018-06-04T13:17:29Z")

</div>

Anyway, thank you so much！

---

<div class="post-metadata">

**Author:** ![Waylon](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/w/dbc845/32.png) [@Waylon](https://discuss.pytorch.org/u/Waylon)\
**Post date:** [July 31, 2018, 4:57pm UTC](https://discuss.pytorch.org/t/how-to-find-the-source-code-of-conv2d-backward-function/19139/8 "2018-07-31T16:57:36Z")

</div>

Hey Tony, I want to do the same thing as you do, modifying the conv2d layer. Did you figure this out?

---

<div class="post-metadata">

**Author:** ![Tony\_Lee](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tony_lee/32/10577_2.png) [@Tony\_Lee](https://discuss.pytorch.org/u/Tony_Lee)\
**Post date:** [August 4, 2018, 7:53am UTC](https://discuss.pytorch.org/t/how-to-find-the-source-code-of-conv2d-backward-function/19139/9 "2018-08-04T07:53:40Z")

</div>

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
