-
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] Add pass for getting calibration data from a relay module #5997
Merged
Merged
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
373a462
add simple pass to extract outputs
seanlatias 9da5b2e
complete pass that collects all function inputs/outputs
seanlatias 91d0f09
add analysis pass for collecting outputs
seanlatias 7c1e0ca
reorganize the files
seanlatias 08c8664
add the first test
seanlatias e2d1281
update test with tuples
seanlatias db0bf7d
clean up Python code
seanlatias 3e29023
Merge branch 'master' of https://github.com/apache/incubator-tvm into…
seanlatias 84ae166
merge with upstream
seanlatias 0c76cf4
clean up transform.py
seanlatias 4f13b0c
add comments for cpp files
seanlatias c4f2746
Merge branch 'master' of https://github.com/apache/incubator-tvm into…
seanlatias 22b10db
fix lint issues
seanlatias d188d4c
update submodules
seanlatias 81ac706
modify files according to the review
seanlatias 1bf93f9
fix style and typo
seanlatias 02b9294
fix lint error
seanlatias 52b6116
add checks for repeated function calls
seanlatias 604c907
fix lint error
seanlatias 031d79e
merge review comments
seanlatias f68ecba
small simplification
seanlatias bf24218
revise the code according to the review comments
seanlatias 9ccd88f
add username in TODO
seanlatias cfd1e20
use IRModule directly
seanlatias c1eb082
use better APIs according to the review
seanlatias 1cc0c45
apply comments from the reviewer
seanlatias 79fd577
retrigger ci
seanlatias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,217 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/*! | ||
* \file src/relay/analysis/get_calibration_data.cc | ||
* | ||
* \brief To get the calibration data, we need to perform two | ||
* steps. First, we need to prepare the module that generate | ||
seanlatias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* the tensor values (GetCalibrateModule). Second, we need to | ||
* generate the mapping between the values and the functions | ||
* (GetCalibrateOutputMap). | ||
*/ | ||
|
||
#include <tvm/relay/analysis.h> | ||
#include <tvm/relay/expr.h> | ||
#include <tvm/relay/expr_functor.h> | ||
|
||
namespace tvm { | ||
namespace relay { | ||
|
||
/*! | ||
* \brief This function returns a module that will be used by | ||
* the relay graph runtime for collecting the calibration data. | ||
* To do that, we first make all inputs and outputs of each | ||
* function into the final output (i.e., the final output is a | ||
* tuple of tensors). Then, we change the compiler attribute of | ||
* each function. Finally, we mark all function to be inlined. | ||
*/ | ||
|
||
class Collector : public ExprRewriter { | ||
public: | ||
explicit Collector(const IRModule& module) { glob_funcs_ = module->functions; } | ||
|
||
Expr Rewrite_(const CallNode* call, const Expr& post) final { | ||
// check if the function implementation is available | ||
// intrinsic functions are excluded for now | ||
if (call->op->IsInstance<GlobalVarNode>()) { | ||
auto var = Downcast<GlobalVar>(call->op); | ||
CHECK_GT(glob_funcs_.count(var), 0) << "Function " << var << " is not defined"; | ||
// we only handle functions with Compiler attribute set | ||
auto* fn = glob_funcs_[var].as<FunctionNode>(); | ||
auto func = GetRef<Function>(fn); | ||
seanlatias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (func->GetAttr<String>(attr::kCompiler)) { | ||
// collect all the inputs and outputs | ||
for (const auto& it : call->args) new_outputs_.push_back(it); | ||
new_outputs_.push_back(post); | ||
} | ||
} | ||
return post; | ||
} | ||
|
||
Array<Expr> GetNewOutputs() { return new_outputs_; } | ||
|
||
private: | ||
Map<GlobalVar, BaseFunc> glob_funcs_; | ||
seanlatias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Array<Expr> new_outputs_; | ||
}; | ||
|
||
Expr FlattenToTuple(const Array<Expr>& exprs, const IRModule& module) { | ||
seanlatias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto glob_funcs = module->functions; | ||
Array<Expr> fields; | ||
for (const auto& it : exprs) { | ||
bool is_tuple = false; | ||
if (auto* cn = it.as<CallNode>()) { | ||
if (cn->op.as<GlobalVarNode>()) { | ||
if (auto* fn = glob_funcs[Downcast<GlobalVar>(cn->op)].as<FunctionNode>()) { | ||
if (auto* tn = fn->body.as<TupleNode>()) { | ||
is_tuple = true; | ||
for (size_t i = 0; i < tn->fields.size(); i++) { | ||
fields.push_back(TupleGetItem(it, i)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if (!is_tuple) { | ||
fields.push_back(it); | ||
} | ||
} | ||
return Tuple(fields); | ||
} | ||
|
||
IRModule GetCalibrateModule(IRModule module) { | ||
auto glob_funcs = module->functions; | ||
// module is mutable, hence, we make a copy of it. | ||
module.CopyOnWrite(); | ||
for (const auto& pair : glob_funcs) { | ||
if (auto* fn = pair.second.as<FunctionNode>()) { | ||
auto func = GetRef<Function>(fn); | ||
// we only collect the outputs for main function | ||
if (pair.first->name_hint == "main") { | ||
Collector collector(module); | ||
PostOrderRewrite(func->body, &collector); | ||
auto new_outputs = collector.GetNewOutputs(); | ||
if (!new_outputs.empty()) { | ||
seanlatias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Expr tuple = FlattenToTuple(new_outputs, module); | ||
func = | ||
Function(func->params, tuple, tuple->checked_type_, func->type_params, func->attrs); | ||
module->Update(pair.first, func); | ||
} | ||
} | ||
} | ||
} | ||
// reset the attribute of functions for running graph runtime | ||
for (const auto& pair : glob_funcs) { | ||
if (auto* fn = pair.second.as<FunctionNode>()) { | ||
auto func = GetRef<Function>(fn); | ||
if (func->GetAttr<String>(attr::kCompiler)) { | ||
// we need to inline the functions in order to run grpah runtime | ||
func = WithAttr(std::move(func), attr::kInline, tvm::Integer(1)); | ||
// reset the compiler attribute to null for llvm execution | ||
func = WithAttr(std::move(func), attr::kCompiler, NullValue<ObjectRef>()); | ||
module->Update(pair.first, func); | ||
} | ||
} | ||
} | ||
return module; | ||
} | ||
|
||
/*! | ||
* \brief This function generates the output mapping between | ||
* the calibration data and each function. The key is a | ||
* GlobalVar that corresponds to each function and the value | ||
* is an array of integers. The size of the array is always | ||
* three. The first value is the offset the points to the start. | ||
* The second value is the number of inputs. The third value | ||
* is the number of outputs. | ||
*/ | ||
|
||
class OutputMapper : public ExprRewriter { | ||
public: | ||
OutputMapper(Map<GlobalVar, Array<Integer>>* output_map, const IRModule& module, size_t* offset) | ||
: output_map_(output_map), offset_(offset) { | ||
glob_funcs_ = module->functions; | ||
} | ||
|
||
Expr Rewrite_(const CallNode* call, const Expr& post) final { | ||
if (call->op->IsInstance<GlobalVarNode>()) { | ||
auto var = Downcast<GlobalVar>(call->op); | ||
CHECK_GT(glob_funcs_.count(var), 0) << "Function " << var << " is not defined"; | ||
CHECK_EQ(output_map_->count(var), 0) | ||
<< "Repeated function call " << var << " is not supported."; | ||
// we only handle functions with Compiler attribute set | ||
auto* fn = glob_funcs_[var].as<FunctionNode>(); | ||
auto func = GetRef<Function>(fn); | ||
seanlatias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (func->GetAttr<String>(attr::kCompiler)) { | ||
Array<Integer> info; | ||
// the first value is the offset | ||
info.push_back(Integer(*offset_)); | ||
// the second value is the number of inputs | ||
info.push_back(Integer(call->args.size())); | ||
// the third value is the number of outputs | ||
// we need to check if the output is a tuple | ||
size_t out_size = 1; | ||
auto* fn = glob_funcs_[var].as<FunctionNode>(); | ||
if (auto* tn = fn->body.as<TupleNode>()) { | ||
info.push_back(Integer(tn->fields.size())); | ||
out_size = tn->fields.size(); | ||
} else { | ||
info.push_back(Integer(1)); | ||
} | ||
output_map_->Set(var, info); | ||
// calculate the offset for the next function | ||
*offset_ = *offset_ + call->args.size() + out_size; | ||
} | ||
} | ||
return post; | ||
} | ||
|
||
private: | ||
Map<GlobalVar, Array<Integer>>* output_map_; | ||
Map<GlobalVar, BaseFunc> glob_funcs_; | ||
seanlatias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
size_t* offset_; | ||
}; | ||
|
||
Map<GlobalVar, Array<Integer>> GetCalibrateOutputMap(const IRModule& module) { | ||
Map<GlobalVar, Array<Integer>> output_map; | ||
size_t offset = 0; | ||
auto glob_funcs = module->functions; | ||
for (const auto& pair : glob_funcs) { | ||
if (auto* fn = pair.second.as<FunctionNode>()) { | ||
if (pair.first->name_hint == "main") { | ||
OutputMapper output_mapper(&output_map, module, &offset); | ||
auto func = GetRef<Function>(fn); | ||
PostOrderRewrite(func->body, &output_mapper); | ||
} | ||
} | ||
} | ||
|
||
return output_map; | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("relay.analysis.get_calibrate_module").set_body_typed([](IRModule mod) { | ||
return GetCalibrateModule(mod); | ||
}); | ||
|
||
TVM_REGISTER_GLOBAL("relay.analysis.get_calibrate_output_map") | ||
.set_body_typed([](const IRModule& mod) { return GetCalibrateOutputMap(mod); }); | ||
|
||
} // namespace relay | ||
} // namespace tvm |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 may need to think of the semantic for the module with control flows, which has to use VM instead of graph runtime.
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.
Per offline discussion, please mention that this pass only works for the graph without control flow.