# Differentiate twice through a custom pytorch extension using numpy

Hello all,

I’d like to differentiate twice through a PyTorch extension that I implemented manually using numpy and
scipy. My objective is to compute a jvp/rop of this block as done here: Rop.py · GitHub

Is this possible? Should I manually define additional derivatives, and where?

The example I am really interested in is a bit lengthy, below there is a similar one based on the
tutorial example Creating Extensions Using numpy and scipy — PyTorch Tutorials 1.9.0+cu102 documentation

``````import torch
import numpy as np
from scipy.signal import convolve2d, correlate2d
from torch.nn.modules.module import Module
from torch.nn.parameter import Parameter

class ScipyConv2dFunction(Function):
@staticmethod
def forward(ctx, input, filter, bias):
# detach so we can cast to NumPy
input, filter, bias = input.detach(), filter.detach(), bias.detach()
result = correlate2d(input.numpy(), filter.numpy(), mode='valid')
result += bias.numpy()
ctx.save_for_backward(input, filter, bias)

@staticmethod
input, filter, bias = ctx.saved_tensors
# the previous line can be expressed equivalently as:

class ScipyConv2d(Module):
def __init__(self, filter_width, filter_height):
super(ScipyConv2d, self).__init__()
self.filter = Parameter(torch.randn(filter_width, filter_height))
self.bias = Parameter(torch.randn(1, 1))

def forward(self, input):
return ScipyConv2dFunction.apply(input, self.filter, self.bias)

if __name__ == "__main__":
module = ScipyConv2d(3, 3)
input = torch.randn(10, 10)
output = module(input)

# cannot differentiate through tmp! No grad_fn defined
``````

The mechanic behind double backward is that the operations performed during the initial backward are recorded by autograd.

This means that since you use numpy in the backward of ScipyConv2dFunction, it won’t be able to double backward out of the box.

For this to work you can implement the backward of ScipyConv2dFunction as another custom function `ScipyConv2dFunctionBackward`.

Let me know if this example makes sense, or are there more details you’d like to know.

``````import torch

def cube_forward(x):
return x**3

return grad_out * 3 * x**2

return grad_out * 3 * x**2

@staticmethod
def forward(ctx, x):
ctx.save_for_backward(x)
return cube_forward(x)

@staticmethod
x, = ctx.saved_tensors

@staticmethod

@staticmethod