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.
- Loading branch information
Showing
3 changed files
with
66 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import numpy as np | ||
|
||
import tvm | ||
import topi | ||
import nnvm.symbol as sym | ||
import nnvm.compiler | ||
import nnvm.runtime | ||
|
||
USE_GPU=True | ||
|
||
def default_target(): | ||
if USE_GPU: | ||
return 'cuda' | ||
else: | ||
return 'llvm' | ||
|
||
def default_ctx(): | ||
if USE_GPU: | ||
return tvm.gpu(0) | ||
else: | ||
return tvm.cpu(0) | ||
|
||
def test_softmax(): | ||
x = sym.Variable("x") | ||
y = sym.softmax(x) | ||
dtype = "float32" | ||
dshape = (10, 1000) | ||
oshape = dshape | ||
graph, lib = nnvm.compiler.build(y, default_target(), {"x": dshape}) | ||
m = nnvm.runtime.create(graph, lib, default_ctx()) | ||
# get member functions | ||
set_input, run, get_output = m["set_input"], m["run"], m["get_output"] | ||
# set input | ||
data = tvm.nd.array(np.random.uniform(size=dshape).astype(dtype)) | ||
set_input("x", data) | ||
# execute | ||
run() | ||
# get outputs | ||
out = tvm.nd.empty(oshape, dtype) | ||
get_output(0, out) | ||
y_np = topi.testing.softmax_python(data.asnumpy()) | ||
np.testing.assert_allclose(out.asnumpy(), y_np, rtol=1e-5) | ||
|
||
|
||
if __name__ == "__main__": | ||
test_softmax() |
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