Skip to content

Commit 2afa97e

Browse files
authored
1.5-releasepost-custom-cpp-code-update
1 parent ac61f7a commit 2afa97e

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

_posts/2020-4-21-pytorch-1-dot-5-released-with-new-and-updated-apis.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ This release adds a new API, `torch.CutomClassHolder`, for binding custom C++ c
2929

3030
```python
3131
template <class T>
32-
struct Stack : torch::CustomClassHolder {
32+
struct MyStackClass : torch::CustomClassHolder {
3333
std::vector<T> stack_;
34-
Stack(std::vector<T> init) : stack_(std::move(init)) {}
34+
MyStackClass(std::vector<T> init) : stack_(std::move(init)) {}
3535

3636
void push(T x) {
3737
stack_.push_back(x);
@@ -44,21 +44,21 @@ struct Stack : torch::CustomClassHolder {
4444
};
4545

4646
static auto testStack =
47-
torch::class_<Stack<std::string>>("Stack")
47+
torch::class_<MyStackClass<std::string>>("myclasses", "MyStackClass")
4848
.def(torch::init<std::vector<std::string>>())
49-
.def("push", &Stack<std::string>::push)
50-
.def("pop", &Stack<std::string>::pop)
51-
.def("size", [](const c10::intrusive_ptr<Stack>& self) {
49+
.def("push", &MyStackClass<std::string>::push)
50+
.def("pop", &MyStackClass<std::string>::pop)
51+
.def("size", [](const c10::intrusive_ptr<MyStackClass>& self) {
5252
return self->stack_.size();
5353
});
5454
```
5555

5656
Which exposes a class you can use in Python and TorchScript like so:
5757

5858
```python
59-
@torch.jit.script
60-
def do_stacks(s : torch.classes.Stack):
61-
s2 = torch.classes.Stack(["hi", "mom"])
59+
@torch.jit.script
60+
def do_stacks(s : torch.classes.myclasses.MyStackClass):
61+
s2 = torch.classes.myclasses.MyStackClass(["hi", "mom"])
6262
print(s2.pop()) # "mom"
6363
s2.push("foobar")
6464
return s2 # ["hi", "foobar"]

0 commit comments

Comments
 (0)