Skip to content
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

[REFACTOR] Streamline Function Attr interface. #5045

Merged
merged 4 commits into from
Mar 12, 2020
Merged

Conversation

tqchen
Copy link
Member

@tqchen tqchen commented Mar 11, 2020

There has been quite a few recent changes that depends heavily on
the function attr interface. This PR streamlines that interface by introducing
two APIs that covers most of the usages.

Thanks to @jroesch for discussing the API naming convention.

  • GetAttr which gets a typed object for a given key
    • HasNonzeroAttr is a quick helper that calls GetAttr to quickly check an attribute
  • WithAttr that creates a new function object with the given attr
    • The API comes with copy on write optimization to avoid multiple copies
    • We deliberately pick the prefix With(instead of Set) to indicate this
      function does not mutate the original input.

On the python side:

  • We allow read access via func.attrs (which is a DictAttr)
    e.g. func.attrs["__param__"]
  • func.with_attrs to create a new instance with updated attrs.

We also get rid of the small wrapper functions and make sure the API centered around
the GetAttr and HasNonzeroAttr interface.

This PR also changes the function construction to follow the new convention.

cc @jroesch @zhiics @mbaret

@tqchen
Copy link
Member Author

tqchen commented Mar 11, 2020

NOTE: I have not removed the UseDefaultCompiler member function, however, we should remove that in a followup PR by simply setting the default compiler to None and we could use GetAttr<StringImm>(attr::kCompiler).defined() to check whether the customization is defined.

StringImm should be changed to String once #4628 is merged

cc @comaniac @zhiics

Copy link
Member

@zhiics zhiics left a comment

Choose a reason for hiding this comment

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

LGTM. Only a few nitpicks.

class DictAttrs : public Attrs {
public:
/*!
* \brief Consruct a Attrs backed by DictAttrsNode.
Copy link
Member

Choose a reason for hiding this comment

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

s/Consruct/Construct

* \brief Base node of all functions.
*
* We support several variants of functions throughout the stack.
* All of the functions shares the same type system(via checked_type)
Copy link
Member

Choose a reason for hiding this comment

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

s/shares/share

* \code
*
* void HasNonzeroAttrExample(const BaseFunc& f) {
* if (f->HasNonzeroAttr(attr::Inline)) {
Copy link
Member

Choose a reason for hiding this comment

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

kInline

* \returns The new function with updated attributes.
*
* \note This function performs copy on write optimization for func.
* If we move an uniquely referenced func into WithAttr,
Copy link
Member

Choose a reason for hiding this comment

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

s/an/a

@tqchen
Copy link
Member Author

tqchen commented Mar 12, 2020

thanks @zhiics please take another look

Comment on lines 21 to 22
* \file tvm/ir/expr.h
* \brief Base expr nodes in TVM.
Copy link
Member

Choose a reason for hiding this comment

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

Update file & brief here

*/

/*!
* \file src/tvm/ir/function.cc
Copy link
Member

Choose a reason for hiding this comment

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

src/ir/function.cc

*/

/*!
* \file src/tvm/relay/ir/function.cc
Copy link
Member

Choose a reason for hiding this comment

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

same as above

Copy link
Member

@zhiics zhiics left a comment

Choose a reason for hiding this comment

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

@tqchen LGTM, please fix @siju-samuel's comments.

tqchen added 3 commits March 11, 2020 20:58
There has been quite a few recent changes that depends heavily on
the function attr interface. This PR streamlines that interface by introducing
two APIs that covers most of the usages.

- GetAttr which gets a typed object for a given key
  - HasNonzeroAttr is a quick helper that calls GetAttr to quickly check an attribute
- WithAttr that creates a new function object with the given attr
  - The API comes with copy on write optimization to avoid multiple copies
  - We deliberately pick the prefix With(instead of Set) to indicate this
    function does not mutate the original input.

On the python side:
- We allow read access via func.attrs (which is a DictAttr)
- func.with_attrs to create a new instance with updated attrs.

We also get rid of the small wrapper functions and make sure the API centered around
the GetAttr and HasNonzeroAttr interface.

This PR also changes the function construction to follow the new convention.
Copy link
Member

@siju-samuel siju-samuel left a comment

Choose a reason for hiding this comment

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

LGTM

@zhiics zhiics merged commit ec86d7f into apache:master Mar 12, 2020
@zhiics
Copy link
Member

zhiics commented Mar 12, 2020

Thanks @tqchen, @ZihengJiang, @siju-samuel

@tqchen tqchen deleted the attrs branch March 16, 2020 16:21
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Apr 16, 2020
* [REFACTOR] Streamline Function Attr interface.

There has been quite a few recent changes that depends heavily on
the function attr interface. This PR streamlines that interface by introducing
two APIs that covers most of the usages.

- GetAttr which gets a typed object for a given key
  - HasNonzeroAttr is a quick helper that calls GetAttr to quickly check an attribute
- WithAttr that creates a new function object with the given attr
  - The API comes with copy on write optimization to avoid multiple copies
  - We deliberately pick the prefix With(instead of Set) to indicate this
    function does not mutate the original input.

On the python side:
- We allow read access via func.attrs (which is a DictAttr)
- func.with_attrs to create a new instance with updated attrs.

We also get rid of the small wrapper functions and make sure the API centered around
the GetAttr and HasNonzeroAttr interface.

This PR also changes the function construction to follow the new convention.

* Address review comments

* Address review comments

* Fix doxygen path
zhiics pushed a commit to neo-ai/tvm that referenced this pull request Apr 17, 2020
* [REFACTOR] Streamline Function Attr interface.

There has been quite a few recent changes that depends heavily on
the function attr interface. This PR streamlines that interface by introducing
two APIs that covers most of the usages.

- GetAttr which gets a typed object for a given key
  - HasNonzeroAttr is a quick helper that calls GetAttr to quickly check an attribute
- WithAttr that creates a new function object with the given attr
  - The API comes with copy on write optimization to avoid multiple copies
  - We deliberately pick the prefix With(instead of Set) to indicate this
    function does not mutate the original input.

On the python side:
- We allow read access via func.attrs (which is a DictAttr)
- func.with_attrs to create a new instance with updated attrs.

We also get rid of the small wrapper functions and make sure the API centered around
the GetAttr and HasNonzeroAttr interface.

This PR also changes the function construction to follow the new convention.

* Address review comments

* Address review comments

* Fix doxygen path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants