Skip to content
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

[SCHEDULE] add 'void AutoFuseEwise(Schedule sch)' #36

Merged
merged 8 commits into from
Feb 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion include/tvm/ir_pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ Array<LoweredFunc> SplitHostDevice(LoweredFunc func);
*/
LoweredFunc StorageSync(LoweredFunc stmt, std::string storage_scope);


} // namespace ir
} // namespace tvm

Expand Down
11 changes: 11 additions & 0 deletions include/tvm/schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ class Stage : public NodeRef {
IterVar* p_x_outer, IterVar* p_y_outer,
IterVar* p_x_inner, IterVar* p_y_inner,
Expr x_factor, Expr y_factor);
/*!
* \brief whether the stage has been scheduled.
* \return whether the stage has been scheduled.
*/
inline bool is_scheduled() const;

// declare container type
using ContainerType = StageNode;
};
Expand Down Expand Up @@ -353,6 +359,11 @@ inline StageNode* Stage::operator->() {
return static_cast<StageNode*>(node_.get());
}

inline bool Stage::is_scheduled() const {
const StageNode* n = operator->();
return !(n->relations.empty() && n->attach_type == kNone);
}

inline const ScheduleNode* Schedule::operator->() const {
return static_cast<const ScheduleNode*>(node_.get());
}
Expand Down
7 changes: 7 additions & 0 deletions include/tvm/schedule_pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ Map<IterVar, Range> InferBound(Schedule sch);
*/
Stmt ScheduleOps(Schedule s, Map<IterVar, Range> dom_map);

/*!
* \brief To automatically inline the element-wise operations.
*
* \param sch The schedule to be inlined.
*/
void AutoInlineElemWise(Schedule sch);

} // namespace schedule
} // namespace tvm
#endif // TVM_SCHEDULE_PASS_H_
2 changes: 1 addition & 1 deletion python/tvm/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def compute_root(self):
parent : Stage
The parent stage
"""
_api_internal._StageComputeInline(self)
_api_internal._StageComputeRoot(self)

def reorder(self, *args):
"""reorder the arguments in the specified order.
Expand Down
5 changes: 5 additions & 0 deletions src/api/api_schedule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
namespace tvm {
namespace schedule {

TVM_REGISTER_API(_schedule_AutoInlineElemWise)
.set_body([](TVMArgs args, TVMRetValue* ret) {
AutoInlineElemWise(args[0]);
});

#define REGISTER_SCHEDULE_PASS1(PassName) \
TVM_REGISTER_API(_schedule_## PassName) \
.set_body([](TVMArgs args, TVMRetValue *ret) { \
Expand Down
76 changes: 76 additions & 0 deletions src/schedule/auto_inline_elem_wise.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*!
* Copyright (c) 2016 by Contributors
* \file auto_inline_elem_wise.cc
*/
#include <tvm/schedule_pass.h>
#include <tvm/ir_visitor.h>

namespace tvm {
namespace ir {

class ElemWiseDetector : public IRVisitor {
public:
explicit ElemWiseDetector(Array<IterVar> axis) : axis_(axis) {}

void Visit(const NodeRef& e) final {
if (!is_elem_wise_) return;
IRVisitor::Visit(e);
}

void Visit_(const Call* op) final {
Array<Expr> axis = op->args;
if (axis_.size() != axis.size()) {
is_elem_wise_ = false;
return;
}

for (size_t i = 0; i < axis_.size(); ++i) {
// const Variable *v1 = axis_[i]->var.as<Variable>();
// const Variable *v2 = axis[i].as<Variable>();
if (!axis[i].same_as(axis_[i]->var)) {
// if (!(v1 && v2) || (v1 != v2)) {
is_elem_wise_ = false;
return;
}
}
IRVisitor::Visit_(op);
}

bool is_elem_wise_{true};

private:
Array<IterVar> axis_;
};


bool IsElemWise(const Operation& op) {
if (const ComputeOpNode* compute = op.as<ComputeOpNode>()) {
ElemWiseDetector v = ElemWiseDetector(compute->axis);
v.Visit(compute->body);
return v.is_elem_wise_;
}
return false;
}

} // namespace ir

namespace schedule {

void AutoInlineElemWise(Schedule sch) {
for (Stage s : sch->stages) {
if (!s.is_scheduled() && ir::IsElemWise(s->op)) {
bool is_root = false;
for (auto r : sch->roots) {
if (r == s->op) {
is_root = true;
break;
}
}
if (!is_root)
s.compute_inline();
}
}
}

} // namespace schedule
} // namespace tvm
16 changes: 16 additions & 0 deletions tests/python/unittest/test_schedule_schedule_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,24 @@ def test_schedule2():
stmt = tvm.schedule.ScheduleOps(s, bounds)
print(stmt)

def test_auto_inline():
m = tvm.Var('m')
n = tvm.Var('n')
A = tvm.placeholder((m, n), name='A')
B = tvm.placeholder((m, n), name='B')
C = tvm.placeholder((m, n), name='C')
T1 = tvm.compute((m, n), lambda i, j: A(i, j) * B(i, j), name='T1')
T2 = tvm.compute((m, n), lambda i, j: T1(i, j) + C(i, j), name='T2')

s = tvm.Schedule(T2.op)
tvm.schedule.AutoInlineElemWise(s)
bounds = tvm.schedule.InferBound(s)
stmt = tvm.schedule.ScheduleOps(s, bounds)
print(stmt)


if __name__ == "__main__":
test_schedule0()
test_schedule1()
test_schedule2()
test_auto_inline()