Skip to content

[Metaschedule] New relay backend for meta schedule task extraction - #10578

Merged
masahi merged 19 commits into
apache:mainfrom
masahi:task-extraction-refactor
Mar 16, 2022
Merged

[Metaschedule] New relay backend for meta schedule task extraction#10578
masahi merged 19 commits into
apache:mainfrom
masahi:task-extraction-refactor

Conversation

@masahi

@masahimasahi commented Mar 11, 2022

Copy link
Copy Markdown
Member

Building on #10561, add a new interface for task extraction that doesn't require the whole VMCompiler.lower(). Extracted tasks are the same as ones from the existing flow. Currently only enabled for meta schedule, but I believe it is worth adapting for autotvm / ansor cc @comaniac@tkonolige

I'm currently working on a model where TE scheduling leads to an error during lowering, but TIR scheduling might work. I wanted to try the latter, but currently task extraction entails (for no reason) TE schedule application and lowering, which leads to the said error. This motivated this work, which aims to remove the unnecessary TE scheduling steps from the task extraction process.

Please review @junrushao1994@mbs-octoml@jroesch

@masahi
masahi marked this pull request as ready for review March 11, 2022 15:25
@github-actions
github-actionsBot requested a review from comaniacMarch 11, 2022 19:08
Comment threadpython/tvm/meta_schedule/integration.py
Comment threadsrc/relay/backend/task_extraction.cc Outdated
@junrushaojunrushao self-assigned this Mar 11, 2022

@tkonoligetkonolige left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I like not having to go through VMCompiler to get tasks. And I'd like to see this adapted to autoscheduler/autotvm. Another improvement would be to remove all metascheduler/auotscheduler depenednt code from te_compiler. te_compiler could return a list of tasks and the metascheduler/autoscheduler could handle it as they please. Maybe Collage will do this.

Comment threadpython/tvm/topi/x86/batch_matmul.py Outdated
Comment threadsrc/relay/backend/task_extraction.cc
@masahi

masahi commented Mar 11, 2022

Copy link
Copy Markdown
MemberAuthor

Another improvement would be to remove all metascheduler/auotscheduler depenednt code from te_compiler. te_compiler could return a list of tasks and the metascheduler/autoscheduler could handle it as they please

This is basically my goal of this PR, and already achieved for meta scheduler. There is no longer meta scheduler-specific code path for task extraction in te_compiler_cache.cc. Most of the code in task_extraction.cc, in particular

Array<ExtractedTask> ExtractTask(IRModule mod, Target target, Map<String, Constant> params) {
backend::BindParamsInModule(mod, params);
// is_vm=true for backward compatibility
Array<Pass> pass_seqs = relay::backend::GetPassPrefix(/*is_homogenous=*/true, /*is_vm=*/true);
pass_seqs.push_back(transform::FuseOps());
transform::Sequential seq(pass_seqs);
auto opt_mod = seq(std::move(mod));
Array<ExtractedTask> tasks;
std::unordered_set<tec::CCacheKey> cache_;
std::unordered_map<std::string, int> name_map;
PostOrderVisit(opt_mod->Lookup("main"), [target, &tasks, &cache_, &name_map](const Expr& exp) {
if (exp->IsInstance<FunctionNode>()) {
Function relay_func = Downcast<Function>(exp);
tec::CCacheKey cache_key(relay_func, target);
if (relay_func->HasNonzeroAttr(attr::kPrimitive) && cache_.find(cache_key) == cache_.end()) {
Array<te::Tensor> outputs;
std::string fused_name;
std::tie(outputs, fused_name) =
tec::LowerTECompute(relay_func, target, /*return_inputs*/ true);
...

are not specific to any tuners and thus can be shared. Different tuners can customize the behavior as they please, e.g. how to create "task" objects from lowered TE compute.

@junrushao

Copy link
Copy Markdown
Member

Will review next week!

@masahi
masahiforce-pushed the task-extraction-refactor branch from 840ed47 to 8475b18CompareMarch 13, 2022 21:35
*/
virtual Optional<ObjectRef> Query(runtime::String task_name, IRModule mod, Target target,
Optional<Array<IRModule>> dispatched) = 0;
virtual IRModule Query(runtime::String task_name, IRModule mod, Target target,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for refactoring this interface! On the other hand, the interface itself might be unnecessary any more after the refactoring :-) Perhaps we would love to refactor this completely some time after this PR is merged!

@masahimasahiMar 15, 2022

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

yeah, based on the current use, I want to change the interface to be tir::Schedule -> tir::Schedule or PrimFunc -> PrimFunc, and rename Query to ApplySchedule or something like that.

When we do need IRModule -> IRModule interface later, I think it's better to introduce another function. Rather than one interface that does everything.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like the idea of renaming to something more meaningful :-)

On the interface side, I would prefer IRModule -> IRModule transformation instead of limiting it to something less general. Imagine the case where we do subgraph-level BYOC, which we already supported in Builder/Runner, and adding the database support might be less tricky if we use the most general IRModule -> IRModule transformation

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

As I commented above, for other use cases that may or may not happen in the future, I think it's better to introduce another interface dedicated for it. BYOC, as currently implemented today, doesn't go through TE Compiler.

Comment threadpython/tvm/meta_schedule/integration.py Outdated
Comment threadsrc/meta_schedule/integration.cc Outdated
Comment threadsrc/meta_schedule/integration.cc Outdated
Comment threadsrc/relay/backend/te_compiler_cache.cc Outdated
Comment threadsrc/relay/backend/te_compiler_cache.cc Outdated
Comment threadsrc/relay/backend/te_compiler_cache.cc Outdated
Comment threadsrc/relay/backend/te_compiler_cache.cc Outdated
Comment threadsrc/relay/backend/task_extraction.cc Outdated

@junrushaojunrushao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall looks good to me! Only a few nitpicks :-)

@masahi
masahiforce-pushed the task-extraction-refactor branch 2 times, most recently from 5c8800c to 1497d65CompareMarch 15, 2022 04:17
commit 501fac6
Merge: 076fa33ce8c563
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 14:16:47 2022 +0900
New relay backend for meta schedule task extraction
commit ce8c563
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 14:12:30 2022 +0900
fix cpplint
commit dfa4fb0
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 14:09:11 2022 +0900
update expected op list in
test_meta_schedule_integration_extract_from_resnet to remove dep on Ansor
commit a98182e
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 13:56:35 2022 +0900
fixed test_meta_schedule_integration_apply_history_best
commit 40d52a1
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 13:50:43 2022 +0900
uniquefy task names
commit dfaf496
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 13:45:30 2022 +0900
dedup tasks
commit e49d500
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 12:59:45 2022 +0900
return reversed list
commit 74636be
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 12:39:58 2022 +0900
refactor
commit 99f1701
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 12:34:14 2022 +0900
clean up integration.cc and Query interface
commit 3f93a1e
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 11:54:57 2022 +0900
check in minor vnni-related change
commit af3e988
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 07:36:35 2022 +0900
Removed TaskExtraction node
commit 7b4d35e
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 05:42:56 2022 +0900
add doc to util functions
commit 3c5a318
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 05:27:53 2022 +0900
rename to task extraction
commit 57f2882
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 05:24:37 2022 +0900
fixed constant param bind
commit f099537
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 05:10:44 2022 +0900
remove unused stuff from python extract_tasks_from_relay
commit 4a5e4aa
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 05:10:30 2022 +0900
move BindParams function to cc file
commit efeccea
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 03:56:05 2022 +0900
refactor param binding
commit 109187f
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 02:21:58 2022 +0900
New relay backend for meta schedule task extraction
commit 6f01901
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 11:25:44 2022 +0900
fixed anchor impl selection
commit be6c258
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 10:57:02 2022 +0900
Forgot visiting arg in ScheduleBuilder CallNode vsit
commit 0c6d4a6
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 10:45:08 2022 +0900
add public, fix include path convention
commit 4cd3a16
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Thu Mar 10 18:43:15 2022 +0900
removed create_schedule stuff
commit eb1bc7e
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Thu Mar 10 18:13:42 2022 +0900
fixed merge conflict
commit 6e68fd9
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Thu Mar 10 14:27:34 2022 +0900
Decouple TE compute and schedule lowering in ScheduleBuilder
@masahi
masahiforce-pushed the task-extraction-refactor branch from 04b7ea1 to 40ee540CompareMarch 15, 2022 19:58
@masahi
masahi merged commit ce335c3 into apache:mainMar 16, 2022
pfk-beta pushed a commit to pfk-beta/tvm that referenced this pull request Apr 11, 2022
…pache#10578)
* New relay backend for meta schedule task extraction
commit 501fac6
Merge: 076fa33ce8c563
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 14:16:47 2022 +0900
New relay backend for meta schedule task extraction
commit ce8c563
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 14:12:30 2022 +0900
fix cpplint
commit dfa4fb0
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 14:09:11 2022 +0900
update expected op list in
test_meta_schedule_integration_extract_from_resnet to remove dep on Ansor
commit a98182e
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 13:56:35 2022 +0900
fixed test_meta_schedule_integration_apply_history_best
commit 40d52a1
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 13:50:43 2022 +0900
uniquefy task names
commit dfaf496
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 13:45:30 2022 +0900
dedup tasks
commit e49d500
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 12:59:45 2022 +0900
return reversed list
commit 74636be
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 12:39:58 2022 +0900
refactor
commit 99f1701
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 12:34:14 2022 +0900
clean up integration.cc and Query interface
commit 3f93a1e
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 11:54:57 2022 +0900
check in minor vnni-related change
commit af3e988
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 07:36:35 2022 +0900
Removed TaskExtraction node
commit 7b4d35e
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 05:42:56 2022 +0900
add doc to util functions
commit 3c5a318
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 05:27:53 2022 +0900
rename to task extraction
commit 57f2882
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 05:24:37 2022 +0900
fixed constant param bind
commit f099537
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 05:10:44 2022 +0900
remove unused stuff from python extract_tasks_from_relay
commit 4a5e4aa
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 05:10:30 2022 +0900
move BindParams function to cc file
commit efeccea
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 03:56:05 2022 +0900
refactor param binding
commit 109187f
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 02:21:58 2022 +0900
New relay backend for meta schedule task extraction
commit 6f01901
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 11:25:44 2022 +0900
fixed anchor impl selection
commit be6c258
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 10:57:02 2022 +0900
Forgot visiting arg in ScheduleBuilder CallNode vsit
commit 0c6d4a6
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Fri Mar 11 10:45:08 2022 +0900
add public, fix include path convention
commit 4cd3a16
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Thu Mar 10 18:43:15 2022 +0900
removed create_schedule stuff
commit eb1bc7e
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Thu Mar 10 18:13:42 2022 +0900
fixed merge conflict
commit 6e68fd9
Author: Masahiro Masuda <masahi129@gmail.com>
Date: Thu Mar 10 14:27:34 2022 +0900
Decouple TE compute and schedule lowering in ScheduleBuilder
* update integration.h doc
* remove unused import
* fix mypy check
* use_meta_schedule restored, now extracts the same task as Ansor
* python doc update
* unused import
* cache_ -> cache, suppres "Cannot find workdload" warning
* Update src/relay/backend/task_extraction.cc and te_compiler_cache.cc
Co-authored-by: Junru Shao <junrushao1994@gmail.com>
* removed unnecessary include
* fixed build
* drop relay.const on params
* updated comment in integration.cc
* update schedule_rule name to prepend "metaschedule"
* typo fix
* more nit change
* make the output of Query Optional
* update py doc
* remove TODO comment on parse_mod
Co-authored-by: Junru Shao <junrushao1994@gmail.com>
junrushao pushed a commit that referenced this pull request Jun 13, 2022
This PR adds `te.extern_primfunc` which provides the interface around TE ExternOp that allows a TVMScript defined schedulable TIR PrimFunc to be inlined into a TE compute graph. The result is that TIR can be used for compute definitions in Relay OpStrategies and, paired with meta-scheduler support in relay as introduced in #10578, these compute definitions can be scheduled and tuned as demonstrated in the attached tests. Prior to this, compute definitions were limited to those definable in TE only. As a consequence of this patch and ongoing improvements to TVMScript meta-programming (#11097), TOPI can be extended to include compute and scheduling functions targeting schedulable TIR uniformly.
Sign up for freeto 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.

4 participants

@masahi@junrushao@tkonolige@zxybazh