Writing a small Pytorch extension in C++


I’m trying to write a small extension to PyTorch in C++. I’ve tried to look at the examples provided and see that the syntax is quite similar to what the python syntax is.

Part - 1
Here is my code for the extension

#include <torch/extension.h>
#include <vector>
#include <iostream>

torch::Tensor compute_grad(torch::Tensor in, torch::Tensor out)
    std::cout << "Input is:" << in << "\n" << "Output is: " << out << "\n";
    auto b = out.sum();
    std::cout << "Out_sum:" << "\t" << b << std::endl;
    std::cout << __LINE__ << std::endl;
    return in.grad();

    m.def("compute_grad", &compute_grad, "Compute Gradient");

and I call it with

import torch
import DenseGrad

if __name__ == "__main__":
    x = (2 * torch.ones(2, 2)).requires_grad_()
    y = x ** 3
    print(DenseGrad.compute_grad(x, y))

For some reason, the backward() call is getting stuck and the only way to kill it is through top. Can someone tell me what I’m doing wrong?

I tried running your program with gdb, I’m getting a deadlock on the autograd execution engine. There are some posts related to this one, and a currently opened issue on PyTorch’s GitHub.

It seems to me that people have solved this by changing the environment, it could be related to PyTorch’s build and dependencies.

Thanks for taking the time to put it through gdb. From this, it seems to have been fixed a while ago, and I’m using conda to get the latest version of pytorch (1.1.0).

I’m unsure how to go about it though.

Hi there. @Haydnspass and me have created a simple example extension. You can find it here. Hope it helps