-
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
[LANG] Support for Tuple Inputs of Reducer and ComputeOp #175
Merged
Merged
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5cf3323
Support for batch ComputeOp
ZihengJiang c75d50f
Support for batch ComputeOp
ZihengJiang 8a22418
Fix CrossThreadReduction
ZihengJiang efa79fb
Fix lint
ZihengJiang 8869899
Add UpdateArray, remove support for batch reduce
ZihengJiang 6703850
Tuple input support for reduce
ZihengJiang 50d20be
rfactor works with multiple reducer; support multiple reducers with d…
ZihengJiang 371a6c0
Small fix
ZihengJiang 7b3ba61
Small fix
ZihengJiang 2da9fd9
Merge branch 'master' of https://github.com/dmlc/tvm into dev
ZihengJiang 0fa0452
Change return type of rfactor to Array<Expr>
ZihengJiang 3739cb7
Fix lint
ZihengJiang 0e7258e
Improve
ZihengJiang 2c42e95
Add tutorial
ZihengJiang 8491288
Improve tutorial
ZihengJiang a3c596b
Improve tutorial
ZihengJiang 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
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 |
---|---|---|
|
@@ -174,10 +174,18 @@ def compute(shape, fcompute, name="compute"): | |
|
||
dim_var = [_IterVar((0, s), x, 0) for x, s in zip(arg_names, shape)] | ||
body = fcompute(*[v.var for v in dim_var]) | ||
if not isinstance(body, (list, tuple)): | ||
body = [body] | ||
body = convert(body) | ||
op_node = _api_internal._ComputeOp( | ||
name, dim_var, body) | ||
return op_node.output(0) | ||
outputs = [] | ||
num = op_node.num_outputs | ||
if num == 1: | ||
return op_node.output(0) | ||
for i in range(num): | ||
outputs.append(op_node.output(i)) | ||
return tuple(outputs) | ||
|
||
|
||
def scan(init, update, state_placeholder, inputs=None, name="scan"): | ||
|
@@ -525,18 +533,46 @@ def _reduce_directly(*args): | |
return res | ||
|
||
def _make_reduce(expr, axis, where=None): | ||
expr = convert(expr) | ||
dtype = expr.dtype | ||
code = fcombine.__code__ | ||
assert fcombine.__code__.co_argcount == 2 | ||
arg_vars = [var(name, dtype) for name in code.co_varnames] | ||
result = fcombine(*[v for v in arg_vars]) | ||
expr = convert(expr) | ||
if isinstance(expr, _collections.Array): | ||
size = len(expr) | ||
larr = [] | ||
rarr = [] | ||
dtypes = [] | ||
for i in range(size): | ||
dtype = expr[i].dtype | ||
dtypes.append(dtype) | ||
lname = code.co_varnames[0] + '_' + str(i) | ||
larr.append(var(lname, dtype)) | ||
rname = code.co_varnames[1] + '_' + str(i) | ||
rarr.append(var(rname, dtype)) | ||
lhs = convert(larr) | ||
rhs = convert(rarr) | ||
result = fcombine(lhs, rhs) | ||
id_elem = fidentity(*dtypes) | ||
else: | ||
assert isinstance(expr, _expr.Expr) | ||
size = 1 | ||
dtype = expr.dtype | ||
lvar = var(code.co_varnames[0], dtype) | ||
rvar = var(code.co_varnames[1], dtype) | ||
result = [fcombine(lvar, rvar)] | ||
id_elem = [fidentity(dtype)] | ||
lhs = convert([lvar]) | ||
rhs = convert([rvar]) | ||
expr = convert([expr]) | ||
result = convert(result) | ||
id_elem = fidentity(dtype) | ||
assert isinstance(id_elem, _expr.Expr) | ||
combiner = _make.CommReducer(arg_vars, result, id_elem) | ||
axis = axis if isinstance(axis, list) else [axis] | ||
return _make.Reduce(combiner, expr, axis, where) | ||
id_elem = convert(id_elem) | ||
combiner = _make.CommReducer(lhs, rhs, result, id_elem) | ||
axis = convert(axis if isinstance(axis, list) else [axis]) | ||
if where is None: | ||
where = convert(True) | ||
if size == 1: | ||
return _make.Reduce(combiner, expr, axis, where, 0) | ||
return [_make.Reduce(combiner, expr, axis, where, i) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change to tuple |
||
for i in range(size)] | ||
|
||
def reducer(expr, axis, where=None, *args): | ||
if isinstance(axis, (_schedule.IterVar, list)): | ||
|
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
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
#include <ir/IR.h> | ||
#include <ir/IRPrinter.h> | ||
#include <memory> | ||
#include "../pass/ir_util.h" | ||
|
||
namespace Halide { | ||
namespace Internal { | ||
|
@@ -25,23 +26,20 @@ void ExprNode<Reduce>::accept(IRVisitor *v, const Expr&) const { | |
TVM_STATIC_IR_FUNCTOR(IRPrinter, vtable) | ||
.set_dispatch<Reduce>([](const Reduce *op, IRPrinter *p) { | ||
p->stream << "reduce(combiner=" | ||
<< op->combiner | ||
<< ", "; | ||
p->print(op->source); | ||
<< op->combiner; | ||
p->stream << ", source=" << op->source; | ||
p->stream << ", axis=" << op->axis; | ||
if (!is_const(op->condition, 1)) { | ||
p->stream << ", where=" << op->condition; | ||
} | ||
p->stream << ", where=" << op->condition; | ||
p->stream << ", value_index=" << op->value_index; | ||
p->stream << ")"; | ||
}); | ||
|
||
TVM_STATIC_IR_FUNCTOR(IRPrinter, vtable) | ||
.set_dispatch<CommReducerNode>([](const CommReducerNode *op, IRPrinter *p) { | ||
p->stream << "comm_reducer(result=" | ||
<< op->result | ||
<< ", args=" << op->args | ||
<< ", identity_element=" | ||
<< op->identity_element | ||
p->stream << "comm_reducer(result=" << op->result | ||
<< ", lhs=" << op->lhs | ||
<< ", rhs=" << op->rhs | ||
<< ", identity_element=" << op->identity_element | ||
<< ")"; | ||
}); | ||
} // namespace Internal | ||
|
@@ -50,23 +48,34 @@ TVM_STATIC_IR_FUNCTOR(IRPrinter, vtable) | |
namespace tvm { | ||
namespace ir { | ||
|
||
CommReducer CommReducerNode::make(Array<Var> args, Expr result, Expr identity_element) { | ||
CommReducer CommReducerNode::make(Array<Var> lhs, | ||
Array<Var> rhs, | ||
Array<Expr> result, | ||
Array<Expr> identity_element) { | ||
auto node = std::make_shared<CommReducerNode>(); | ||
node->args = args; | ||
node->lhs = lhs; | ||
node->rhs = rhs; | ||
node->result = result; | ||
node->identity_element = identity_element; | ||
return CommReducer(node); | ||
} | ||
|
||
Expr CommReducerNode::operator()(Expr a, Expr b) const { | ||
Array<Expr> CommReducerNode::operator()(Array<Expr> a, Array<Expr> b) const { | ||
CHECK_EQ(a.size(), b.size()); | ||
CHECK_EQ(lhs.size(), a.size()); | ||
CHECK_EQ(rhs.size(), b.size()); | ||
Map<Var, Expr> value_map; | ||
value_map.Set(args[0], a); | ||
value_map.Set(args[1], b); | ||
return Substitute(result, value_map); | ||
for (size_t i = 0; i < a.size(); ++i) { | ||
value_map.Set(lhs[i], a[i]); | ||
value_map.Set(rhs[i], b[i]); | ||
} | ||
return UpdateArray(result, [&value_map] (const Expr& e) { | ||
return Substitute(e, value_map); | ||
}); | ||
} | ||
|
||
Expr Reduce::make(CommReducer combiner, Expr source, | ||
Array<IterVar> axis, Expr condition) { | ||
Expr Reduce::make(CommReducer combiner, Array<Expr> source, | ||
Array<IterVar> axis, Expr condition, int value_index) { | ||
for (size_t i = 0; i < axis.size(); ++i) { | ||
CHECK_EQ(axis[i]->iter_type, kCommReduce) | ||
<< "Can only take axis created by reduce_axis"; | ||
|
@@ -79,11 +88,12 @@ Expr Reduce::make(CommReducer combiner, Expr source, | |
for (size_t i = 0; i < axis.size(); ++i) { | ||
CHECK(axis[i].defined()); | ||
} | ||
n->type = source.type(); | ||
n->type = source[value_index].type(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if argument is passing by value, do std::move to save copy constructor |
||
n->combiner = combiner; | ||
n->source = source; | ||
n->axis = axis; | ||
n->condition = condition; | ||
n->value_index = value_index; | ||
return Expr(n); | ||
} | ||
|
||
|
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.
remove the default value, to be safe
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.
never mind, forget this comment