-
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][BYOC] Add support for composite functions in BYOC #5261
Conversation
Currently, MergeComposite can only perform structural matches. This patch introduces the ability to specify a 'check' function alongside the pattern which can include custom logic to determine whether an extracted pattern should be merged. For example, if you only want to merge 'NHWC' convolutions, you can specify a 'check' function which queries the data_layout value of the extracted pattern (see the test). Change-Id: I9337ce39f10997051a286d888be38ed0d410d340
Run clang-format on merge_composite.cc Change-Id: I1736bff798cc6d93e57519b08ab3362869098779
This patch introduces support to annotate composite functions in the AnnotateTarget pass. In order for a composite function to be annotated, you should name it according to the style: {codegen}.{name} eg. dnnl.add_relu Change-Id: I74d6c0b506153d866f6d1feb203b32dad59f2871
cc @soiferj |
Partitioning looks good for me with a simple pattern, conv2d_bias_relu. I'm seeing the check fail in IsOp for composite functions however. Maybe for another PR. bool IsOp(const CallNode* call, const std::string& op_name) const {
const auto* op_node = call->op.as<OpNode>();
CHECK(op_node) << "Expects a single op.";
Op op = GetRef<Op>(op_node);
return op == Op::Get(op_name);
}
|
Using MergeComposite, AnnotateTarget and PartitionGraph, I get the following graph for conv + bias + relu pattern:
Is it possible to inline composite function
Otherwise I have to support function call in DNNL codegen. @zhiics @mbaret UPDATE: hmm if I inline the composite function, I lose the composite attribute and hence cannot detect fused call. Is supporting function call in the DNNL codegen a better option? |
Hi @masahi, I think it is best to leave this to the codegen. That way we have the option to handle a composite all at once at call node or invididually by just doing VisitExpr(func->body) when we encounter one. |
@trevor-m Yeah agree. I'm looking to support either function call or "manual inline" in the codegen. |
Ok got the DNNL conv + bias + relu working (masahi/tvm@byoc-composite...masahi:dnnl-composite). This is a reimplmentation of #4741 based on composite annotate support in this PR, rather than the manual approach. I think it is much cleaner. I can send a new PR as soon as this PR is merged. |
@alexbooth Thanks for pointing out. The purpose of the codegen_c is mainly for quick prototyping. Would you be interested to send a PR to add the support for composite function after this is merged? |
@masahi yeah, inlining the function you pointed out is a little tricky because it varies case by case. I intentionally made it a bit more conservative and left it for the external codegen to handle. In the long run, it would be more helpful if we make the passes more configurable. |
@zhiics Yes, I'll review and merge this today |
* [RELAY] Add 'check' functions to MergeComposite Currently, MergeComposite can only perform structural matches. This patch introduces the ability to specify a 'check' function alongside the pattern which can include custom logic to determine whether an extracted pattern should be merged. For example, if you only want to merge 'NHWC' convolutions, you can specify a 'check' function which queries the data_layout value of the extracted pattern (see the test). Change-Id: I9337ce39f10997051a286d888be38ed0d410d340 * [RELAY] Reformat merge_composite.cc Run clang-format on merge_composite.cc Change-Id: I1736bff798cc6d93e57519b08ab3362869098779 * [RELAY][BYOC] Support composite functions in AnnotateTarget This patch introduces support to annotate composite functions in the AnnotateTarget pass. In order for a composite function to be annotated, you should name it according to the style: {codegen}.{name} eg. dnnl.add_relu Change-Id: I74d6c0b506153d866f6d1feb203b32dad59f2871
* [RELAY] Add 'check' functions to MergeComposite Currently, MergeComposite can only perform structural matches. This patch introduces the ability to specify a 'check' function alongside the pattern which can include custom logic to determine whether an extracted pattern should be merged. For example, if you only want to merge 'NHWC' convolutions, you can specify a 'check' function which queries the data_layout value of the extracted pattern (see the test). Change-Id: I9337ce39f10997051a286d888be38ed0d410d340 * [RELAY] Reformat merge_composite.cc Run clang-format on merge_composite.cc Change-Id: I1736bff798cc6d93e57519b08ab3362869098779 * [RELAY][BYOC] Support composite functions in AnnotateTarget This patch introduces support to annotate composite functions in the AnnotateTarget pass. In order for a composite function to be annotated, you should name it according to the style: {codegen}.{name} eg. dnnl.add_relu Change-Id: I74d6c0b506153d866f6d1feb203b32dad59f2871
* [RELAY] Add 'check' functions to MergeComposite Currently, MergeComposite can only perform structural matches. This patch introduces the ability to specify a 'check' function alongside the pattern which can include custom logic to determine whether an extracted pattern should be merged. For example, if you only want to merge 'NHWC' convolutions, you can specify a 'check' function which queries the data_layout value of the extracted pattern (see the test). Change-Id: I9337ce39f10997051a286d888be38ed0d410d340 * [RELAY] Reformat merge_composite.cc Run clang-format on merge_composite.cc Change-Id: I1736bff798cc6d93e57519b08ab3362869098779 * [RELAY][BYOC] Support composite functions in AnnotateTarget This patch introduces support to annotate composite functions in the AnnotateTarget pass. In order for a composite function to be annotated, you should name it according to the style: {codegen}.{name} eg. dnnl.add_relu Change-Id: I74d6c0b506153d866f6d1feb203b32dad59f2871
This PR includes two changes to support composite functions as part of the BYOC flow. First, 'check' functions have been added to the MergeComposite pass. Currently you can only do structural pattern matches which don't test any attributes or tensor sizes. The check function accepts the matching expression and can implement arbitrary logic to test whether that match should be supported. See the new test for an example which queries convolution layout.
The other change enables the annotation of composite functions in the AnnotateTarget pass. For a composite function to be recognised, you must name it according to the convention:
{compiler}.{composite_name}
eg. dnnl.add_relu