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

[RELAY] Added a AnnotatedRegion utility class #5030

Merged
merged 11 commits into from
Mar 26, 2020
Merged

Conversation

mbaret
Copy link
Contributor

@mbaret mbaret commented Mar 10, 2020

In many of the passes involved in graph partitioning, we need to extract and manipulate subgraphs - referred to here as 'regions'. This class simplifies the extraction of regions from a relay expression containing region begin and end annotations as well as providing utility functions to query these regions and merge them.

@mbaret mbaret changed the title [WIP] [RELAY] Added a SubgraphSet utility class [WIP] [RELAY] Added a AnnotatedRegion utility class Mar 23, 2020
@mbaret mbaret changed the title [WIP] [RELAY] Added a AnnotatedRegion utility class [RELAY] Added a AnnotatedRegion utility class Mar 23, 2020
@mbaret
Copy link
Contributor Author

mbaret commented Mar 23, 2020

cc @zhiics @comaniac @tqchen

@mbaret
Copy link
Contributor Author

mbaret commented Mar 23, 2020

Removed mentions of 'subgraph' and replaced this with the term 'AnnotatedRegion'.

Copy link
Contributor

@comaniac comaniac left a comment

Choose a reason for hiding this comment

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

Only miner comments. LGTM.
cc @masahi @soiferj who might be also interested in this PR.

python/tvm/relay/analysis/__init__.py Outdated Show resolved Hide resolved
src/relay/analysis/annotated_region_set.cc Outdated Show resolved Hide resolved
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.

Thanks for the effort. Some comments are left.

@tqchen I am okay with AnnotatedRegions, how do you think?

python/tvm/relay/analysis/__init__.py Outdated Show resolved Hide resolved
python/tvm/relay/analysis/annotated_regions.py Outdated Show resolved Hide resolved
class AnnotatedRegion;
class AnnotatedRegionSet;

class AnnotatedRegionNode : public Object {
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest we change this to a struct or just make members public since it only really just carries data. This getters seem to be redundant IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's written this way to more explicitly convey that Regions are read-only. Maybe there's a better pattern for this.

* \brief An object to hold the properties of a region as used by the
* AnnotatedRegionSet class. This should be considered read-only.
*/
class AnnotatedRegion : public ObjectRef {
Copy link
Member

Choose a reason for hiding this comment

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

BTW, AnnotatedRegion is more just for internal use, right? Likely, we don't need to register them to the object system as they will not need to be exposed to Python.

In this case, only need one class AnnotatedRegion to carry the data and AnnotatedRegionNode could be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is primarily to make this usable in python. If that's not required, we can remove this.

src/relay/analysis/annotated_region_set.h Outdated Show resolved Hide resolved
src/relay/analysis/annotated_region_set.h Show resolved Hide resolved
src/relay/analysis/annotated_region_set.h Outdated Show resolved Hide resolved
}
}

AnnotatedRegion AnnotatedRegionSetNode::MakeRegion() {
Copy link
Member

Choose a reason for hiding this comment

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

Likely we don't really need this helper. There is only one use of it. Inlining it to the caller with a comment clear enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think now I've moved the region_id counting logic into it (as per comaniac's suggestion) it's existence becomes justified.

src/relay/analysis/annotated_region_set.cc Outdated Show resolved Hide resolved
class AnnotatedRegionSet(Object):
"""Class to represent a relay expression split into regions."""

def __init__(self, expr, region_begin_op, region_end_op):
Copy link
Member

Choose a reason for hiding this comment

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

We probably want to make compile_begin and end as default to the region_begin_op and region_end_op

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this would unnecessarily couple it for compiler annotations (for instance, in future we may introduce the supported annotations).

ce_4 = compiler_end(O_3, 'test_target')
merged = relay.Function([data], ce_4)

region_set = relay.analysis.AnnotatedRegionSet(merged,
Copy link
Contributor

Choose a reason for hiding this comment

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

Correct me if I'm wrong but it seems like this pass should take a compiler as argument (test_target for example) and return regions annotated to that same compiler. Right now it seems to just look for all subgraphs regardless of their compiler. Maybe clearly documenting the intended use of this pass would help clear up this issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

This pass is not attempting to return regions for a specific compiler. Instead, it will return all regions in the graph for all compilers. However, I agree with you that the comment description should be improved to make it clear.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, is there then a way to tell which compiler each subgraph is annotated for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't deal with targets at all in this pass/class because in principle it should work for arbitrary begin/end annotations, not necessarily just compiler begin/end ones. All of the outs/ins are necessarily annotations, so one way you can query the target (and how I do it) is to look at any one of these annotations and check its 'compiler' attribute.

Copy link
Contributor

@manupak manupak Mar 25, 2020

Choose a reason for hiding this comment

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

@jwfromm To clarify further,

I think this feature provides a mechanism to obtain annotated regions (as a data structure -- a collection of nodes with ins and outs) that are bound (i.e. all the incoming and outgoing edges, to the region from the rest of the graph, are bound ) by annotation ops.

One important use case of it is to highlight regions that are off-loaded to different backend compilers. In which case, all the ins and outs (which are annotation ops) should have the 'compiler" attribute as @mbaret mentions in the previous comment.

However, it is not limited to backend compiler partitioning. I see this being useful in future region specific optimizations that a user might want to do without needing to pass additional objects (C++/Python); just through passing the IR modules with annotations. I am also using this in my recent PR.

In many of the passes involved in graph partitioning,
we need to extract and manipulate annotated regions.
This class simplifies the extraction of regions from a relay
expression containing region begin and end annotations
as well as providing utility functions to query these
regions and merge them.

Co-authored-by: Ramana Radhakrishnan  <ramana.radhakrishnan@arm.com>

Change-Id: Ia912fea0b99f64b6a7197aa6da2347e58f469fbb
Copy link
Contributor

@manupak manupak left a comment

Choose a reason for hiding this comment

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

I think this feature provides a mechanism to obtain annotated regions (as a data structure -- a collection of nodes with ins and outs) that are bound (i.e. all the incoming and outgoing edges, to the region from the rest of the graph, are bound ) by annotation ops. It's obviously useful for graph partitioning as it tends to use such an approach already.

@zhiics
Copy link
Member

zhiics commented Mar 25, 2020

@jwfromm could you PTAL?

@zhiics zhiics merged commit b5ec071 into apache:master Mar 26, 2020
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Apr 16, 2020
* [RELAY] Added an AnnotatedRegionSet utility class

In many of the passes involved in graph partitioning,
we need to extract and manipulate annotated regions.
This class simplifies the extraction of regions from a relay
expression containing region begin and end annotations
as well as providing utility functions to query these
regions and merge them.

Co-authored-by: Ramana Radhakrishnan  <ramana.radhakrishnan@arm.com>

Change-Id: Ia912fea0b99f64b6a7197aa6da2347e58f469fbb

* Rename fix

* Update MakeRegions

* Fix __init__

* Indentation

* Code style

* Remove 'Region' from docs

* Overload [] to get region

* Use src/dest for MergeRegions

* Simplify merge

* Tidy const loop vars
zhiics pushed a commit to neo-ai/tvm that referenced this pull request Apr 17, 2020
* [RELAY] Added an AnnotatedRegionSet utility class

In many of the passes involved in graph partitioning,
we need to extract and manipulate annotated regions.
This class simplifies the extraction of regions from a relay
expression containing region begin and end annotations
as well as providing utility functions to query these
regions and merge them.

Co-authored-by: Ramana Radhakrishnan  <ramana.radhakrishnan@arm.com>

Change-Id: Ia912fea0b99f64b6a7197aa6da2347e58f469fbb

* Rename fix

* Update MakeRegions

* Fix __init__

* Indentation

* Code style

* Remove 'Region' from docs

* Overload [] to get region

* Use src/dest for MergeRegions

* Simplify merge

* Tidy const loop vars
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.

5 participants