Commit 89a88c57 by 雾雨魔理沙 Committed by Tianqi Chen

[Relay] Start porting pass to the pass manager (#3191)

parent 7e648417
...@@ -61,6 +61,7 @@ ...@@ -61,6 +61,7 @@
#include <tvm/relay/error.h> #include <tvm/relay/error.h>
#include <tvm/relay/expr.h> #include <tvm/relay/expr.h>
#include <tvm/relay/module.h> #include <tvm/relay/module.h>
#include <tvm/relay/op_attr_types.h>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
...@@ -198,7 +199,7 @@ class Pass; ...@@ -198,7 +199,7 @@ class Pass;
*/ */
class PassNode : public RelayNode { class PassNode : public RelayNode {
public: public:
/* /*!
* \brief Get the pass information/meta data. */ * \brief Get the pass information/meta data. */
virtual PassInfo Info() const = 0; virtual PassInfo Info() const = 0;
...@@ -300,12 +301,119 @@ Pass CreateModulePass( ...@@ -300,12 +301,119 @@ Pass CreateModulePass(
* *
* \return The created function pass. * \return The created function pass.
*/ */
Pass CreateFunctionPass( TVM_DLL Pass CreateFunctionPass(const runtime::TypedPackedFunc<
const runtime::TypedPackedFunc<Function(Function, PassContext)>& pass_func, Function(Function, Module, PassContext)>& pass_func,
int opt_level, int opt_level,
const std::string& name, const std::string& name,
const tvm::Array<tvm::Expr>& required); const tvm::Array<tvm::Expr>& required);
/*! \brief Remove expressions which does not effect the program result.
*
* It will remove let bindings which are not referenced,
* and inline let bindings that are only used once.
*
* For example, this pass should turn `let a = 1 in 2` into `2`,
* as the value of the expression does not depend on a.
*
* As another example, `let a = 1 in a` will be optimized into 1.
*
* \return the pass.
*/
TVM_DLL Pass DeadCodeElimination();
/*!
* \brief Fold constant expressions.
*
* \return The pass.
*/
TVM_DLL Pass FoldConstant();
/*!
* \brief Fuse operations into expr into seperate functions.
*
* \param fuse_opt_level Optimization level. If it is -1 it will be inferred from pass context.
*
* \return The pass.
*/
TVM_DLL Pass FuseOps(int fuse_opt_level = -1);
/*!
* \brief Apply rewrite rules to rewrite the expr in post DFS order.
*
* \param rewrite_map_attr_name The Op's attr name which corresponds to the rewrite
* rule function.
* \param fcontext Additional callback to provide context argument for each call node.
* \param fmulti_ref_trigger Transformation function to be called when
* an Expr consumed by multiple callers.
*
* \return The pass.
*/
TVM_DLL Pass ForwardRewrite(const std::string& rewrite_map_attr_name,
std::function<NodeRef(const Call&)> fcontext = nullptr,
std::function<Expr(const Expr&)>
fmulti_ref_trigger = nullptr);
/*!
* \brief Apply rewrite rules to rewrite the expr in post DFS order.
*
* \param rewrite_func The rewrite func that will apply to all operators.
* \param fcontext Additional callback to provide context argument for each call node.
* \param fmulti_ref_trigger Transformation function to be called when
* an Expr consumed by multiple callers.
*
* \return The pass.
*/
TVM_DLL Pass ForwardRewrite(const FForwardRewrite& rewrite_func,
std::function<NodeRef(const Call&)> fcontext = nullptr,
std::function<Expr(const Expr&)> fmulti_ref_trigger = nullptr);
/*!
* \brief Rewrite the annotated program.
*
* \param fallback_device The fallback device which is the default device for
* operators without annotation.
*
* \return The pass.
*/
TVM_DLL Pass RewriteAnnotatedOps(int fallback_device);
/*!
* \brief turn a dataflow graph into Administrative Normal Form, or A-Normal Form (ANF).
*
* It will turn an expression that is in a graph form (with sharing implicit),
* to an expression with explicit sharing (A-Normal Form).
*
* The scope of the root expression is the global scope.
*
* The scope of any non root expression is the least common ancestor of all it's scope.
*
* Values are ordered by post-DFS order in each scope.
*
* \return The pass.
*/
TVM_DLL Pass ToANormalForm();
/*!
* \brief Remove let binding and directly share via pointer instead.
*
* It will remove all let binding,
* and turn all of the variable bound by let into direct pointer reference.
*
* \return the expression in graph normal form.
*/
TVM_DLL Pass ToGraphNormalForm();
/*!
* \brief Aggressive constant propagation/constant folding/inlining.
*
* It will do as much computation in compile time as possible.
* It has two benefit: remove runtime overhead, and allow more optimization (typically fusion).
* As a side effect, code size will explode.
*
* \return the optimized expression.
*/
TVM_DLL Pass PartialEval();
} // namespace transform } // namespace transform
} // namespace relay } // namespace relay
} // namespace tvm } // namespace tvm
......
...@@ -151,5 +151,17 @@ Expr DeadCodeElimination(const Expr& e) { ...@@ -151,5 +151,17 @@ Expr DeadCodeElimination(const Expr& e) {
TVM_REGISTER_API("relay._ir_pass.dead_code_elimination") TVM_REGISTER_API("relay._ir_pass.dead_code_elimination")
.set_body_typed(DeadCodeElimination); .set_body_typed(DeadCodeElimination);
namespace transform {
Pass DeadCodeElimination() {
runtime::TypedPackedFunc<Function(Function, Module, PassContext)> pass_func =
[=](Function f, Module m, PassContext pc) {
return Downcast<Function>(DeadCodeElimination(f));
};
return CreateFunctionPass(pass_func, 1, "dead_code_elimination", {});
}
} // namespace transform
} // namespace relay } // namespace relay
} // namespace tvm } // namespace tvm
...@@ -550,6 +550,18 @@ TVM_REGISTER_API("relay._ir_pass.RewriteDeviceAnnotation") ...@@ -550,6 +550,18 @@ TVM_REGISTER_API("relay._ir_pass.RewriteDeviceAnnotation")
TVM_REGISTER_API("relay._ir_pass.CollectDeviceAnnotationOps") TVM_REGISTER_API("relay._ir_pass.CollectDeviceAnnotationOps")
.set_body_typed(CollectDeviceAnnotationOps); .set_body_typed(CollectDeviceAnnotationOps);
namespace transform {
Pass RewriteAnnotatedOps(int fallback_device) {
runtime::TypedPackedFunc<Function(Function, Module, PassContext)> pass_func =
[=](Function f, Module m, PassContext pc) {
return Downcast<Function>(RewriteAnnotatedOps(f, fallback_device));
};
return CreateFunctionPass(pass_func, 1, "rewrite_annotated_ops", {});
}
} // namespace transform
} // namespace relay } // namespace relay
} // namespace tvm } // namespace tvm
...@@ -215,5 +215,17 @@ Expr FoldConstant(const Expr& expr) { ...@@ -215,5 +215,17 @@ Expr FoldConstant(const Expr& expr) {
TVM_REGISTER_API("relay._ir_pass.FoldConstant") TVM_REGISTER_API("relay._ir_pass.FoldConstant")
.set_body_typed(FoldConstant); .set_body_typed(FoldConstant);
namespace transform {
Pass FoldConstant() {
runtime::TypedPackedFunc<Function(Function, Module, PassContext)> pass_func =
[=](Function f, Module m, PassContext pc) {
return Downcast<Function>(FoldConstant(f));
};
return CreateFunctionPass(pass_func, 1, "fold_constant", {});
}
} // namespace transform
} // namespace relay } // namespace relay
} // namespace tvm } // namespace tvm
...@@ -206,6 +206,37 @@ Expr ForwardRewrite(const Expr& expr, ...@@ -206,6 +206,37 @@ Expr ForwardRewrite(const Expr& expr,
return ForwardRewriter(&rewrite_func, fcontext, fmulti_ref_trigger).Rewrite(expr); return ForwardRewriter(&rewrite_func, fcontext, fmulti_ref_trigger).Rewrite(expr);
} }
namespace transform {
using std::function;
Pass ForwardRewrite(const std::string& rewrite_map_attr_name,
function<NodeRef(const Call&)> fcontext,
function<Expr(const Expr&)> fmulti_ref_trigger) {
runtime::TypedPackedFunc<Function(Function, Module, PassContext)> pass_func =
[=](Function f, Module m, PassContext pc) {
return Downcast<Function>(ForwardRewrite(f,
rewrite_map_attr_name,
fcontext,
fmulti_ref_trigger));
};
return CreateFunctionPass(pass_func, 1, "forward_rewrite", {});
}
Pass ForwardRewrite(const FForwardRewrite& rewrite_func,
function<NodeRef(const Call&)> fcontext,
function<Expr(const Expr&)> fmulti_ref_trigger) {
runtime::TypedPackedFunc<Function(Function, Module, PassContext)> pass_func =
[=](Function f, Module m, PassContext pc) {
return Downcast<Function>(ForwardRewrite(f,
rewrite_func,
fcontext,
fmulti_ref_trigger));
};
return CreateFunctionPass(pass_func, 1, "forward_rewrite", {});
}
} // namespace transform
} // namespace relay } // namespace relay
} // namespace tvm } // namespace tvm
...@@ -964,5 +964,19 @@ Expr FuseOps(const Expr& expr, int fuse_opt_level, const Module& module) { ...@@ -964,5 +964,19 @@ Expr FuseOps(const Expr& expr, int fuse_opt_level, const Module& module) {
TVM_REGISTER_API("relay._ir_pass.FuseOps") TVM_REGISTER_API("relay._ir_pass.FuseOps")
.set_body_typed(FuseOps); .set_body_typed(FuseOps);
namespace transform {
Pass FuseOps(int fuse_opt_level) {
runtime::TypedPackedFunc<Function(Function, Module, PassContext)> pass_func =
[=](Function f, Module m, PassContext pc) {
int opt_level = fuse_opt_level == -1 ? pc->opt_level : fuse_opt_level;
return Downcast<Function>(FuseOps(f, opt_level, m));
};
return CreateFunctionPass(pass_func, 1, "fuse_ops", {});
}
} // namespace transform
} // namespace relay } // namespace relay
} // namespace tvm } // namespace tvm
...@@ -801,5 +801,17 @@ TVM_REGISTER_API("relay._ir_pass.partial_evaluate") ...@@ -801,5 +801,17 @@ TVM_REGISTER_API("relay._ir_pass.partial_evaluate")
*ret = PartialEval(args[0]); *ret = PartialEval(args[0]);
}); });
namespace transform {
Pass PartialEval() {
runtime::TypedPackedFunc<Function(Function, Module, PassContext)> pass_func =
[=](Function f, Module m, PassContext pc) {
return Downcast<Function>(PartialEval(f));
};
return CreateFunctionPass(pass_func, 1, "partial_eval", {});
}
} // namespace transform
} // namespace relay } // namespace relay
} // namespace tvm } // namespace tvm
...@@ -201,7 +201,7 @@ class FunctionPassNode : public PassNode { ...@@ -201,7 +201,7 @@ class FunctionPassNode : public PassNode {
* `pass_func` and let it run on a given module. The same `pass_func` will * `pass_func` and let it run on a given module. The same `pass_func` will
* then be applied on each function in the module. * then be applied on each function in the module.
*/ */
runtime::TypedPackedFunc<Function(Function, PassContext)> pass_func; runtime::TypedPackedFunc<Function(Function, Module, PassContext)> pass_func;
FunctionPassNode() = default; FunctionPassNode() = default;
...@@ -225,7 +225,7 @@ class FunctionPassNode : public PassNode { ...@@ -225,7 +225,7 @@ class FunctionPassNode : public PassNode {
PassInfo Info() const { return pass_info; } PassInfo Info() const { return pass_info; }
TVM_DLL static FunctionPass make( TVM_DLL static FunctionPass make(
runtime::TypedPackedFunc<Function(Function, PassContext)> pass_func, runtime::TypedPackedFunc<Function(Function, Module, PassContext)> pass_func,
PassInfo pass_info); PassInfo pass_info);
static constexpr const char* _type_key = "relay.FunctionPass"; static constexpr const char* _type_key = "relay.FunctionPass";
...@@ -363,7 +363,7 @@ Module ModulePassNode::operator()(const Module& mod, ...@@ -363,7 +363,7 @@ Module ModulePassNode::operator()(const Module& mod,
} }
FunctionPass FunctionPassNode::make( FunctionPass FunctionPassNode::make(
runtime::TypedPackedFunc<Function(Function, PassContext)> pass_func, runtime::TypedPackedFunc<Function(Function, Module, PassContext)> pass_func,
PassInfo pass_info) { PassInfo pass_info) {
auto n = make_node<FunctionPassNode>(); auto n = make_node<FunctionPassNode>();
n->pass_func = std::move(pass_func); n->pass_func = std::move(pass_func);
...@@ -383,8 +383,7 @@ Module FunctionPassNode::operator()(const Module& mod, ...@@ -383,8 +383,7 @@ Module FunctionPassNode::operator()(const Module& mod,
// Execute the pass function and return a new module. // Execute the pass function and return a new module.
for (const auto& it : mod->functions) { for (const auto& it : mod->functions) {
auto updated_func = auto updated_func = SkipFunction(it.second) ? it.second : pass_func(it.second, mod, pass_ctx);
SkipFunction(it.second) ? it.second : pass_func(it.second, pass_ctx);
new_mod->Add(it.first, updated_func); new_mod->Add(it.first, updated_func);
} }
...@@ -501,7 +500,7 @@ Pass CreateModulePass( ...@@ -501,7 +500,7 @@ Pass CreateModulePass(
} }
Pass CreateFunctionPass( Pass CreateFunctionPass(
const runtime::TypedPackedFunc<Function(Function, PassContext)>& pass_func, const runtime::TypedPackedFunc<Function(Function, Module, PassContext)>& pass_func,
int opt_level, int opt_level,
const std::string& name, const std::string& name,
const tvm::Array<tvm::Expr>& required) { const tvm::Array<tvm::Expr>& required) {
...@@ -589,7 +588,7 @@ TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable) ...@@ -589,7 +588,7 @@ TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
tvm::IRPrinter* p) { tvm::IRPrinter* p) {
const PassInfoNode* seq_pn = node->Info().operator->(); const PassInfoNode* seq_pn = node->Info().operator->();
p->stream << "Run Sequential pass: " << seq_pn->name p->stream << "Run Sequential pass: " << seq_pn->name
<< " at the optimization level. " << seq_pn->opt_level; << " at the optimization level " << seq_pn->opt_level << ". ";
p->stream << "The passes will be executed are: ["; p->stream << "The passes will be executed are: [";
for (const auto& it : node->passes) { for (const auto& it : node->passes) {
const PassNode* pn = it.operator->(); const PassNode* pn = it.operator->();
......
...@@ -333,5 +333,17 @@ Expr ToANormalForm(const Expr& e, const Module& m) { ...@@ -333,5 +333,17 @@ Expr ToANormalForm(const Expr& e, const Module& m) {
TVM_REGISTER_API("relay._ir_pass.to_a_normal_form") TVM_REGISTER_API("relay._ir_pass.to_a_normal_form")
.set_body_typed(static_cast<Expr (*)(const Expr&, const Module&)>(ToANormalForm)); .set_body_typed(static_cast<Expr (*)(const Expr&, const Module&)>(ToANormalForm));
namespace transform {
Pass ToANormalForm() {
runtime::TypedPackedFunc<Function(Function, Module, PassContext)> pass_func =
[=](Function f, Module m, PassContext pc) {
return Downcast<Function>(ToANormalForm(f, m));
};
return CreateFunctionPass(pass_func, 1, "to_a_normal_form", {});
}
} // namespace transform
} // namespace relay } // namespace relay
} // namespace tvm } // namespace tvm
...@@ -79,5 +79,17 @@ Expr ToGraphNormalForm(const Expr& e) { ...@@ -79,5 +79,17 @@ Expr ToGraphNormalForm(const Expr& e) {
TVM_REGISTER_API("relay._ir_pass.to_graph_normal_form") TVM_REGISTER_API("relay._ir_pass.to_graph_normal_form")
.set_body_typed(ToGraphNormalForm); .set_body_typed(ToGraphNormalForm);
namespace transform {
Pass ToGraphNormalForm() {
runtime::TypedPackedFunc<Function(Function, Module, PassContext)> pass_func =
[=](Function f, Module m, PassContext pc) {
return Downcast<Function>(ToGraphNormalForm(f));
};
return CreateFunctionPass(pass_func, 1, "to_graph_normal_form", {});
}
} // namespace transform
} // namespace relay } // namespace relay
} // namespace tvm } // namespace tvm
...@@ -204,7 +204,7 @@ def test_function_pass(): ...@@ -204,7 +204,7 @@ def test_function_pass():
pass_ctx = None pass_ctx = None
@_transform.function_pass(opt_level=opt_level, name=pass_name) @_transform.function_pass(opt_level=opt_level, name=pass_name)
def transform(expr, ctx): def transform(expr, mod, ctx):
return opt_tester.transform(expr, ctx) return opt_tester.transform(expr, ctx)
def get_ref_log(): def get_ref_log():
...@@ -303,7 +303,7 @@ def test_sequential_pass(): ...@@ -303,7 +303,7 @@ def test_sequential_pass():
# Register a function pass. # Register a function pass.
@_transform.function_pass(opt_level=1) @_transform.function_pass(opt_level=1)
def func_transform(expr, ctx): def func_transform(expr, mod, ctx):
return opt_tester.transform(expr, ctx) return opt_tester.transform(expr, ctx)
function_pass = func_transform function_pass = func_transform
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment