mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 00:21:07 +01:00
Summary:
I have some test code in there as well, along with a script "test_libtorch" to run it. You'll need to modify `test_libtorch` to point to where you have `pytorch` built. I currently require that `pybind11` is included as a subdirectory of the test, but added it to the `.gitignore` to make this reviewable.
Currently, something like this works:
```cpp
struct Foo {
int x, y;
Foo(): x(2), y(5){}
Foo(int x_, int y_) : x(x_), y(y_) {}
void display() {
cout<<"x: "<<x<<' '<<"y: "<<y<<endl;
}
int64_t add(int64_t z) {
return (x+y)*z;
}
};
static auto test = torch::jit::class_<Foo>("Foo")
.def(torch::jit::init<int64_t, int64_t>())
.def("display", &Foo::display)
.def("add", &Foo::add)
.def("combine", &Foo::combine);
```
with
```py
torch.jit.script
def f(x):
val = torch._C.Foo(5, 3)
val.display()
print(val.add(3))
```
results in
```
x: 5 y: 3
24
```
Current issues:
- [x] The python class created by torchscript doesn't interactly properly with the surrounding code.
```
torch.jit.script
def f(x):
val = torch._C.Foo(5, 3)
return val
```
- [x] Doesn't properly take in non-pointer classes. Can't define this function signature in cpp (We don't want to support this I believe).
```cpp
void combine(Foo x) {
```
- [x] Has some issues with memory for blobs when constructing multiple objects (fix constant propagation pass to not treat capsules as the same object).
```py
torch.jit.script
def f(x):
val = torch._C.Foo(5, 3)
val2 = torch._C.Foo(100, 0)
val.display()
print(val.add(3))
```
- [ ] Can't define multiple constructors (need to define overload string. Currently not possible since we don't support overloaded methods).
- [x] `init` is a little bit different syntax than `pybind`. `.init<...>()` instead of `.def(py::init<>())`
- [x] I couldn't figure out how to add some files into the build so they'd be copied to the `include/` directories, so I symlinked them manually.
- [ ] Currently, the conversion from Python into Torchscript doesn't work.
- [ ] Torchbind also currently requires Python/Pybind dependency. Fixing this would probably involve some kind of macro to bind into Python when possible.
- [ ] We pass back into Python by value, currently. There's no way of passing by reference.
- [x] Currently can only register one method with the same type signature. This is because we create a `static auto opRegistry`, and the function is templated on the type signature.
Somewhat blocked on https://github.com/pytorch/pytorch/pull/21177. We currently use some structures that will be refactored by his PR (namely `return_type_to_ivalue` and `ivalue_to_arg_type`.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/21098
Differential Revision: D16634872
Pulled By: Chillee
fbshipit-source-id: 1408bb89ea649c27d560df59e2cf9920467fe1de
66 lines
1.7 KiB
C++
66 lines
1.7 KiB
C++
|
|
#include <cassert>
|
|
#include <climits>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
#include <iterator>
|
|
#include <list>
|
|
#include <torch/script.h>
|
|
#include <torch/custom_class.h>
|
|
#include <pybind11/pybind11.h>
|
|
|
|
using namespace std;
|
|
|
|
namespace py = pybind11;
|
|
|
|
struct Foo : torch::jit::CustomClassHolder {
|
|
int x, y;
|
|
Foo(): x(0), y(0){}
|
|
Foo(int x_, int y_) : x(x_), y(y_) {}
|
|
int64_t info() {
|
|
return this->x * this->y;
|
|
}
|
|
int64_t add(int64_t z) {
|
|
return (x+y)*z;
|
|
}
|
|
void increment(int64_t z) {
|
|
this->x+=z;
|
|
this->y+=z;
|
|
}
|
|
int64_t combine(c10::intrusive_ptr<Foo> b) {
|
|
return this->info() + b->info();
|
|
}
|
|
~Foo() {
|
|
// std::cout<<"Destroying object with values: "<<x<<' '<<y<<std::endl;
|
|
}
|
|
};
|
|
|
|
template <class T> struct Stack : torch::jit::CustomClassHolder {
|
|
std::vector<T> stack_;
|
|
Stack(std::vector<T> init): stack_(init.begin(), init.end()) {}
|
|
|
|
void push(T x) {
|
|
stack_.push_back(x);
|
|
}
|
|
T pop() {
|
|
auto val = stack_.back();
|
|
stack_.pop_back();
|
|
return val;
|
|
}
|
|
};
|
|
|
|
static auto test = torch::jit::class_<Foo>("Foo")
|
|
.def(torch::jit::init<int64_t, int64_t>())
|
|
// .def(torch::jit::init<>())
|
|
.def("info", &Foo::info)
|
|
.def("increment", &Foo::increment)
|
|
// .def("add", &Foo::add);
|
|
.def("combine", &Foo::combine)
|
|
;
|
|
|
|
static auto testStack = torch::jit::class_<Stack<std::string>>("StackString")
|
|
.def(torch::jit::init<std::vector<std::string>>())
|
|
.def("push", &Stack<std::string>::push)
|
|
.def("pop", &Stack<std::string>::pop)
|
|
;
|