[JIT] Change expect, cast on Type to return shared pointers, make isSubtypeOf accept TypePtr#9786
[JIT] Change expect, cast on Type to return shared pointers, make isSubtypeOf accept TypePtr#9786wanchaol wants to merge 2 commits intopytorch:masterfrom
Conversation
facebook-github-bot
left a comment
There was a problem hiding this comment.
@wanchaol has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
@pytorchbot retest this please |
torch/csrc/jit/type.h
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/type.h
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
apaszke
left a comment
There was a problem hiding this comment.
Looks good, but I'm not sure if using shared pointers everywhere is a good idea, compared to just using references. There are three issues I see:
- Equality can have surprising behavior, because comparing pointers doesn't use the
operator==of the underlying types, but only compares their memory location. This is ok in case of singleton types, but definitely not what we want forTensorType. It's very easy to forget about it, and the compiler won't catch this error for us. If we still want to pursue theshared_ptrpath, we might want to at least have a custom subclass of it, that overrides the equality semantics. - Every
XType::get()is an unnecessary refcount bump/decref, and we do those in plenty of places just to perform type checking. - They are nullable, which is usually not what you want.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/python_ir.cpp
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
We were already using type pointers before, this is just making their use consistent. There were a bunch of places where you ended up with a derived type but needed the shared pointer again. In the future, I think it would be better to make types behave like value types (e.g. like at::Tensor or IValue), meaning that equality etc. all just works and we can pass them around as Type objects. But this would be a more invasive change that we can't make while all our other PRs are in flight. |
|
@apaszke As of today in JIT, I am seeing everywhere we are using
|
|
@smessmer Can you review this again? You can just review the second commit where it addressed the enable_shared_from_this pitfall. |
facebook-github-bot
left a comment
There was a problem hiding this comment.
@wanchaol has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
| private: | ||
| ListType(TypePtr elem) | ||
| : Type(TypeKind::ListType), elem(elem) {} | ||
| static const TypeKind Kind = TypeKind::ListType; |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| } | ||
| bool isSubtypeOf(const Type& rhs) const override { | ||
| return *this == rhs || rhs.kind() == TypeKind::NumberType; | ||
| bool isSubtypeOf(const TypePtr rhs) const override { |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
…Of accept TypePtr (pytorch#9786) Summary: Follow up task of pytorch#9584. Commit 1: - change expect/cast to return shared pointers instead of raw pointer - isSubtypeOf accept TypePtr instead. Use `x->isSubtypeOf(NumberType::get())` rather than `x->isSubtypeOf(*NumberType::get())` Commit 2: - to address enable_shared_from_this pitfalls, we make the constructor private and expose the factory method to make sure user can only create it using our factory method. Pull Request resolved: pytorch#9786 Reviewed By: zdevito Differential Revision: D8980441 Pulled By: wanchaol fbshipit-source-id: e5c923fc57a701014310e77cf29985b43bb25364
…Of accept TypePtr (pytorch#9786) Summary: Follow up task of pytorch#9584. Commit 1: - change expect/cast to return shared pointers instead of raw pointer - isSubtypeOf accept TypePtr instead. Use `x->isSubtypeOf(NumberType::get())` rather than `x->isSubtypeOf(*NumberType::get())` Commit 2: - to address enable_shared_from_this pitfalls, we make the constructor private and expose the factory method to make sure user can only create it using our factory method. Pull Request resolved: pytorch#9786 Reviewed By: zdevito Differential Revision: D8980441 Pulled By: wanchaol fbshipit-source-id: e5c923fc57a701014310e77cf29985b43bb25364
Follow up task of #9584.
Commit 1:
x->isSubtypeOf(NumberType::get())rather thanx->isSubtypeOf(*NumberType::get())Commit 2: