pytorch/test/cpp_extensions/cpp_frontend_extension.cpp
Peter Goldsborough 0bf1383f0a Python <-> C++ Frontend inter-op (#13481)
Summary:
This PR enables C++ frontend modules to be bound into Python and added as submodules of Python modules. For this, I added lots of pybind11 bindings for the `torch::nn::Module` class, and modified the `torch.nn.Module` class in Python to have a new Metaclass that makes `isinstance(m, torch.nn.Module)` return true when `m` is a C++ frontend module. The methods and fields of C++ modules are bound in such a way that they work seamlessly as submodules of Python modules for most operations (one exception I know of: calling `.to()` ends up calling `.apply()` on each submodule with a Python lambda, which cannot be used in C++ -- this may require small changes on Python side).

I've added quite a bunch of tests to verify the bindings and equality with Python. I think I should also try out adding a C++ module as part of some large PyTorch module, like a WLM or something, and see if everything works smoothly.

The next step for inter-op across our system is ScriptModule <-> C++ Frontend Module inter-op. I think this will then also allow using C++ frontend modules from TorchScript.

apaszke zdevito

CC dzhulgakov
Pull Request resolved: https://github.com/pytorch/pytorch/pull/13481

Differential Revision: D12981996

Pulled By: goldsborough

fbshipit-source-id: 147370d3596ebb0e94c82cec92993a148fee50a7
2018-12-13 08:04:02 -08:00

50 lines
1.2 KiB
C++

#include <torch/extension.h>
#include <cstddef>
#include <string>
struct Net : torch::nn::Module {
Net(int64_t in, int64_t out) : fc(in, out) {
register_module("fc", fc);
buffer = register_buffer("buf", torch::eye(5));
}
torch::Tensor forward(torch::Tensor x) {
return fc->forward(x);
}
void set_bias(torch::Tensor bias) {
torch::NoGradGuard guard;
fc->bias.set_(bias);
}
torch::Tensor get_bias() const {
return fc->bias;
}
void add_new_parameter(const std::string& name, torch::Tensor tensor) {
register_parameter(name, tensor);
}
void add_new_buffer(const std::string& name, torch::Tensor tensor) {
register_buffer(name, tensor);
}
void add_new_submodule(const std::string& name) {
register_module(name, torch::nn::Linear(fc->options));
}
torch::nn::Linear fc;
torch::Tensor buffer;
};
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
torch::python::bind_module<Net>(m, "Net")
.def(py::init<int64_t, int64_t>())
.def("set_bias", &Net::set_bias)
.def("get_bias", &Net::get_bias)
.def("add_new_parameter", &Net::add_new_parameter)
.def("add_new_buffer", &Net::add_new_buffer)
.def("add_new_submodule", &Net::add_new_submodule);
}