Skip to content

Conversation

@Lunderberg
Copy link
Contributor

Prior to this commit, the default DictAttrs for an IRModule, tir::PrimFunc, relax::Function, and relay::Function was a null value. At each callsite, the absence of a DictAttrs needed to be treated as equivalent to an empty DictAttrs. In C++, this typically was done using the foo->GetAttr helper function, but in Python it needed to be checked explicitly. That is, every callsite needed to check if func.attrs is not None and attr_name in func.attrs, rather than only checking if attr_name in func.attrs.

Since most functions would have at least one attribute to specify the global symbol, these bugs would often surface when working on unrelated changes.

This commit changes the default attribute dictionary from NullValue<DictAttrs>() to DictAttrs(). This avoids having two separate representations of an object without any attributes, and allows the if attr_name in func.attrs pattern in the Python API.

Copy link
Contributor

@quic-sanirudh quic-sanirudh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this change. I've run into this issue a couple times as well, so this makes a lot of sense.

Prior to this commit, the default `DictAttrs` for an `IRModule`,
`tir::PrimFunc`, `relax::Function`, and `relay::Function` was a null
value.  At each callsite, the absence of a `DictAttrs` needed to be
treated as equivalent to an empty `DictAttrs`.  In C++, this typically
was done using the `foo->GetAttr` helper function, but in Python it
needed to be checked explicitly.  That is, every callsite needed to
check `if func.attrs is not None and attr_name in func.attrs`, rather
than only checking `if attr_name in func.attrs`.

Since most functions would have at least one attribute to specify the
global symbol, these bugs would often surface when working on
unrelated changes.

This commit changes the default attribute dictionary from
`NullValue<DictAttrs>()` to `DictAttrs()`.  This avoids having two
separate representations of an object without any attributes, and
allows the `if attr_name in func.attrs` pattern in the Python API.
@Lunderberg Lunderberg force-pushed the ir_attrs_default_to_empty branch from 3e71d64 to edd5448 Compare March 22, 2024 00:13
@Lunderberg Lunderberg merged commit b2204ae into apache:main Mar 25, 2024
@Lunderberg Lunderberg deleted the ir_attrs_default_to_empty branch March 25, 2024 18:07
Lunderberg added a commit to Lunderberg/tvm that referenced this pull request Mar 25, 2024
Introduced in apache#16745, should be the
string `"Composite"`, not the bytes `b"Composite"`.
Lunderberg added a commit that referenced this pull request Mar 26, 2024
Introduced in #16745, should be the
string `"Composite"`, not the bytes `b"Composite"`.
thaisacs pushed a commit to thaisacs/tvm that referenced this pull request Apr 3, 2024
* [IR] Default to empty attributes, instead of NULL

Prior to this commit, the default `DictAttrs` for an `IRModule`,
`tir::PrimFunc`, `relax::Function`, and `relay::Function` was a null
value.  At each callsite, the absence of a `DictAttrs` needed to be
treated as equivalent to an empty `DictAttrs`.  In C++, this typically
was done using the `foo->GetAttr` helper function, but in Python it
needed to be checked explicitly.  That is, every callsite needed to
check `if func.attrs is not None and attr_name in func.attrs`, rather
than only checking `if attr_name in func.attrs`.

Since most functions would have at least one attribute to specify the
global symbol, these bugs would often surface when working on
unrelated changes.

This commit changes the default attribute dictionary from
`NullValue<DictAttrs>()` to `DictAttrs()`.  This avoids having two
separate representations of an object without any attributes, and
allows the `if attr_name in func.attrs` pattern in the Python API.

* Remove no-longer-needed checks on attrs being present

* Fix up unit tests

* More unit test fixes

* Undo erroneous find/replace

* A few more unit tests

* Provide `DictAttrs.get`
thaisacs pushed a commit to thaisacs/tvm that referenced this pull request Apr 3, 2024
Introduced in apache#16745, should be the
string `"Composite"`, not the bytes `b"Composite"`.
Lunderberg added a commit to Lunderberg/tvm that referenced this pull request Apr 3, 2024
A follow-up to apache#16745.  For Relax
functions produced in TVMScript, when `R.func_attrs` was not present,
the default was set to `None` instead of an empty dictionary.
Lunderberg added a commit that referenced this pull request Apr 5, 2024
A follow-up to #16745.  For Relax
functions produced in TVMScript, when `R.func_attrs` was not present,
the default was set to `None` instead of an empty dictionary.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants