forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TOP][COMPILER] sum, min, max, transpose, fix dense (apache#28)
- Loading branch information
Showing
10 changed files
with
180 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
from . import tensor | ||
from . import nn | ||
from . import transform | ||
from . import reduction |
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,47 @@ | ||
# pylint: disable=invalid-name, unused-argument | ||
"""Reduction ops""" | ||
from __future__ import absolute_import | ||
|
||
import tvm | ||
import topi | ||
import topi.cuda | ||
from ..compiler import registry as reg | ||
from ..compiler import OpPattern | ||
|
||
def _schedule_reduce(_, outs, target): | ||
"""Generic schedule for reduce""" | ||
if target == "cuda": | ||
return topi.cuda.schedule_reduce(outs) | ||
assert target.startswith("llvm") | ||
s = tvm.create_schedule([x.op for x in outs]) | ||
x = outs[0] | ||
tvm.schedule.AutoInlineInjective(s) | ||
s[x].fuse(s[x].op.axis) | ||
return s | ||
|
||
_fschedule_reduce = tvm.convert(_schedule_reduce) | ||
|
||
def _compute_reduce(f): | ||
"""auxiliary function""" | ||
def _compute(attrs, inputs, out_info): | ||
axis = attrs.get_int_tuple("axis") | ||
keepdims = attrs.get_bool("keepdims") | ||
if axis: | ||
return f(inputs[0], axis=axis, keepdims=keepdims) | ||
return f(inputs[0], keepdims=keepdims) | ||
return _compute | ||
|
||
# sum | ||
reg.register_compute("sum", _compute_reduce(topi.sum)) | ||
reg.register_pattern("sum", OpPattern.COMM_REDUCE) | ||
reg.register_schedule("sum", _fschedule_reduce) | ||
|
||
# max | ||
reg.register_compute("max", _compute_reduce(topi.max)) | ||
reg.register_pattern("max", OpPattern.COMM_REDUCE) | ||
reg.register_schedule("max", _fschedule_reduce) | ||
|
||
# min | ||
reg.register_compute("min", _compute_reduce(topi.min)) | ||
reg.register_pattern("min", OpPattern.COMM_REDUCE) | ||
reg.register_schedule("min", _fschedule_reduce) |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import numpy as np | ||
import tvm | ||
import topi | ||
import nnvm.symbol as sym | ||
import nnvm.compiler | ||
import nnvm.runtime | ||
from nnvm.testing.config import test_ctx_list | ||
|
||
def verify_transpose(dshape, axes): | ||
x = sym.Variable("x") | ||
if axes: | ||
y = sym.transpose(x, axes=axes) | ||
else: | ||
y = sym.transpose(x) | ||
y = y + 1 | ||
dtype = "float32" | ||
for target, ctx in test_ctx_list(): | ||
graph, lib, _ = nnvm.compiler.build(y, target, {"x": dshape}) | ||
m = nnvm.runtime.create(graph, lib, ctx) | ||
# set input | ||
data = tvm.nd.array(np.random.uniform(size=dshape).astype(dtype)) | ||
m.run(x=data) | ||
out_np = np.transpose(data.asnumpy(), axes=axes) + 1 | ||
out = m.get_output(0, tvm.nd.empty(out_np.shape)) | ||
np.testing.assert_allclose(out.asnumpy(), out_np, atol=1e-5, rtol=1e-5) | ||
|
||
|
||
def verify_reduce(dshape, fnp, fsym, **kwargs): | ||
x = sym.Variable("x") | ||
y = fsym(x + 1, **kwargs) | ||
dtype = "float32" | ||
for target, ctx in test_ctx_list(): | ||
graph, lib, _ = nnvm.compiler.build(y, target, {"x": dshape}) | ||
m = nnvm.runtime.create(graph, lib, ctx) | ||
# set input | ||
data = np.random.uniform(size=dshape).astype(dtype) | ||
out_np = fnp(data + 1, **kwargs) | ||
m.run(x=data) | ||
out = m.get_output(0, tvm.nd.empty(out_np.shape)) | ||
np.testing.assert_allclose(out.asnumpy(), out_np, atol=1e-5, rtol=1e-5) | ||
|
||
|
||
def test_tranpose(): | ||
verify_transpose((2, 3, 4), (0, 2, 1)) | ||
verify_transpose((2, 3, 4), None) | ||
|
||
|
||
def test_reduce(): | ||
verify_reduce((2, 3, 4), np.max, sym.max, axis=1, keepdims=True) | ||
verify_reduce((4, 4, 3), np.min, sym.min, keepdims=True) | ||
verify_reduce((4, 4, 3), np.sum, sym.sum, axis=(0, 2)) | ||
|
||
|
||
if __name__ == "__main__": | ||
test_reduce() | ||
test_tranpose() |