-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Conversation
Removed mentions of 'subgraph' and replaced this with the term 'AnnotatedRegion'. |
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.
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 the effort. Some comments are left.
@tqchen I am okay with AnnotatedRegions, how do you think?
class AnnotatedRegion; | ||
class AnnotatedRegionSet; | ||
|
||
class AnnotatedRegionNode : public Object { |
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.
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.
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'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 { |
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.
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.
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 is primarily to make this usable in python. If that's not required, we can remove this.
} | ||
} | ||
|
||
AnnotatedRegion AnnotatedRegionSetNode::MakeRegion() { |
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.
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.
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.
I think now I've moved the region_id counting logic into it (as per comaniac's suggestion) it's existence becomes justified.
class AnnotatedRegionSet(Object): | ||
"""Class to represent a relay expression split into regions.""" | ||
|
||
def __init__(self, expr, region_begin_op, region_end_op): |
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.
We probably want to make compile_begin and end as default to the region_begin_op and region_end_op
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.
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, |
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.
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.
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 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.
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.
I see, is there then a way to tell which compiler each subgraph is annotated for?
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.
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.
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.
@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
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.
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.
@jwfromm could you PTAL? |
* [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
* [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
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.