This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Relax][MS] Task extraction with proper weights (#129)
* [Relax][MS] Task extraction with proper weights (Hzfengsy#32) * Add a unit test * Update the deduplication mapping / Update the unit test * Update test for DummyDB reusing * Remove unnecessary args * Remove unused import
- Loading branch information
1 parent
1f75be5
commit 52480fd
Showing
4 changed files
with
178 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#include <tvm/meta_schedule/extracted_task.h> | ||
#include <tvm/relax/expr.h> | ||
#include <tvm/relax/expr_functor.h> | ||
#include <tvm/target/target.h> | ||
#include <tvm/tir/function.h> | ||
|
||
namespace tvm { | ||
namespace relax { | ||
namespace backend { | ||
|
||
using tvm::meta_schedule::ExtractedTask; | ||
|
||
/*! | ||
* \brief Extract the Meta-Schedule tuning task from a given IRModule. | ||
* \note | ||
* 1. The task extractor is responsible for task deduplication. The | ||
* deduplication is achieved by comparing structural hashes of PrimFuncs. | ||
* 2. For a PrimFunc, the weight of its corresponding task is the number | ||
* of times it called by op Call-TIR. Say in an IRModule there are three | ||
* PrimFuncs `fn1`, `fn2` and `fn3` sharing the same structural hash. | ||
* Suppose `fn1` is called by 5 Call-TIR ops among all Relax function, | ||
* `fn2` is called by 3 Call-TIR and `fn3` is called by 5 Call-TIR. | ||
* Then we will have a ExtractedTask for all three functions, whose weight | ||
* is 5 + 3 + 2 = 10. | ||
*/ | ||
class TaskExtractor : public ExprVisitor { | ||
public: | ||
static Array<ExtractedTask> ExtractTask(IRModule mod, Target target) { | ||
TaskExtractor extracor(mod, target); | ||
// We go through each Relax function in the module. | ||
for (const auto& kv : mod->functions) { | ||
if (const auto* func = kv.second.as<FunctionNode>()) { | ||
extracor(GetRef<Function>(func)); | ||
} | ||
} | ||
return std::move(extracor.tasks_); | ||
} | ||
|
||
private: | ||
explicit TaskExtractor(IRModule mod, Target target) | ||
: mod_(std::move(mod)), target_(std::move(target)) {} | ||
|
||
void VisitExpr_(const CallNode* call) final { | ||
static const Op& call_tir_op = Op::Get("relax.call_tir"); | ||
if (!call->op.same_as(call_tir_op)) { | ||
// Since the Relax function is of A-normal form, the arguments of this call cannot be another | ||
// Calls. And hence we do not need to recurse into this Call. | ||
return; | ||
} | ||
|
||
const GlobalVar& global_var = Downcast<GlobalVar>(call->args[0]); | ||
const tir::PrimFunc& func = Downcast<tir::PrimFunc>(mod_->Lookup(global_var)); | ||
|
||
auto it = func2task_.find(func); | ||
if (it != func2task_.end()) { | ||
it->second->weight += 1; | ||
return; | ||
} | ||
|
||
IRModule tir_mod({{global_var, func}}); | ||
ExtractedTask task(/*task_name=*/global_var->name_hint, // | ||
/*mod=*/tir_mod, // | ||
/*target=*/target_, // | ||
/*dispatched=*/{tir_mod}, // | ||
/*weight=*/1); | ||
tasks_.push_back(task); | ||
func2task_.emplace(func, task); | ||
} | ||
|
||
IRModule mod_; | ||
Target target_; | ||
Array<ExtractedTask> tasks_; | ||
std::unordered_map<tir::PrimFunc, ExtractedTask, StructuralHash, StructuralEqual> func2task_; | ||
}; | ||
|
||
TVM_REGISTER_GLOBAL("relax.backend.MetaScheduleExtractTask") | ||
.set_body_typed([](IRModule mod, Target target) { | ||
return TaskExtractor::ExtractTask(std::move(mod), std::move(target)); | ||
}); | ||
|
||
} // namespace backend | ||
} // namespace relax | ||
} // namespace tvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters