I edited you message with ``` to do a code block and make it easier to read 
It would be slightly different:
class dWdx(Function):
    @staticmethod
    def forward(ctx, x, p, t):
        np_x = x.detach().numpy() # The detach should not be needed here, will be in a future version of pytorch
        np_p = p.detach().numpy() # The detach should not be needed here, will be in a future version of pytorch
        np_t = t.detach().numpy() # The detach should not be needed here, will be in a future version of pytorch
        result = scipy_integrate(f, np_x, np_p, np_t) # I don't know what are the args
        result = torch.from_numpy(result)
        torch.save_for_backward(x, p, t, result)
        return result

    @once_differentiable # This is just because our backward cannot be auto-diffed
    @staticmethod
    def backward(ctx, grad_output):
        x, p, t, result = ctx.saved_tensors
        grad_x = XXX # Here you can use whatever you want because it is once_differentiable.
        grad_p = XXX
        grad_t = XXX
        return grad_x, grad_p, grad_t # Matching the input of forward