-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bugfix] Fix visit_attrs error if its function pointer is equal to nullptr #8920
Conversation
include/tvm/node/reflection.h
Outdated
@@ -386,11 +386,13 @@ inline ReflectionVTable::Registry ReflectionVTable::Register() { | |||
|
|||
inline void ReflectionVTable::VisitAttrs(Object* self, AttrVisitor* visitor) const { | |||
uint32_t tindex = self->type_index(); | |||
if (tindex >= fvisit_attrs_.size() || fvisit_attrs_[tindex] == nullptr) { | |||
if (tindex >= fvisit_attrs_.size()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might cause some of the unintended behavior(since for certain object we might want VisitAttrs to exist and would like to detecting problem by seeing the error). Would be great if we can directly overload the getattr and dir on the python container object instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your valuable advice! I re-submit a new commit to fix this bug by overloading the __getattr__
and __dir__
function on the python container object. Since this commit only focuses on the front-end python container, I think it would not bring any unintended behaviors now.
3dadc43
to
70baa9f
Compare
CC: @comaniac |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Do we need a test for this?
I agree. So I send a new commit about adding a new test for |
@@ -34,6 +34,21 @@ def test_array_save_load_json(): | |||
assert a_loaded[1].value == 2 | |||
|
|||
|
|||
def test_dir_array(): | |||
a = tvm.runtime.convert([1, 2, 3]) | |||
dir(a) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to check its outputs instead of just letting it go. It could be at least assert dir(a)
.
try: | ||
getattr(a, "test_key") | ||
except AttributeError: | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are expecting an exception, please use
with pytest.raise(AttributeError):
getattr(a, "test_key")
In this particular case, it might be even simpler :
assert not hasattr(a, "test_key")
test_dir_array() | ||
test_dir_map() | ||
test_getattr_array() | ||
test_getattr_map() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind to help change to the pytest style?
if __name__ == "__main__":
pytest.main([__file__])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your valuable advice! I have rewritten these four test cases and changed the test file to the pytest style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you help review this new commit as it passes all checks :-D @comaniac
aa2f3d4
to
6d8cc1d
Compare
7d5418a
to
fea4c26
Compare
@comaniac mind taking another look? thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks.
Thanks @syang-ng @junrushao1994 @tqchen |
…llptr (apache#8920) * fix visit_attrs equals nullptr on python container object * add a test a for python container object about function dir and getattr * change test_ir_container.py to the pytest style * update the style to fix ci error * update the style of ir container to fix ci error
…llptr (apache#8920) * fix visit_attrs equals nullptr on python container object * add a test a for python container object about function dir and getattr * change test_ir_container.py to the pytest style * update the style to fix ci error * update the style of ir container to fix ci error
Fix the bug mentioned in https://discuss.tvm.apache.org/t/cannot-list-attrs-of-tvm-ir-array-via-dir/10924
Every time when we try to register a node, the size of
fvisit_attrs_
will increase, and there are no other operations to unregister these nodes, so iftindex < fvisit_attrs_.size()
is true, it means that the node has been registered.Therefore, I just use the
tindex >= fvisit_attrs_.size()
to determine whether it has been registered. And iffvisit_attrs_[tindex] != nullptr
exists, I will executefvisit_attrs_[tindex](self, visitor);
to get its attribute.@junrushao1994 @comaniac