-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fpga_codegen_fix' of https://github.com/manuelburger/dace…
… into fpga_codegen_fix # Please enter a commit message to explain why this merge is necessary,
- Loading branch information
Showing
2 changed files
with
109 additions
and
0 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,69 @@ | ||
""" Tests for half-precision syntax quirks. """ | ||
|
||
import dace | ||
import numpy as np | ||
|
||
N = dace.symbol('N') | ||
|
||
|
||
def test_relu(): | ||
@dace.program | ||
def halftest(A: dace.float16[N]): | ||
out = np.ndarray([N], dace.float16) | ||
for i in dace.map[0:N]: | ||
with dace.tasklet: | ||
a << A[i] | ||
o >> out[i] | ||
o = a if a > dace.float16(0) else dace.float16(0) | ||
return out | ||
|
||
A = np.random.rand(20).astype(np.float16) | ||
sdfg = halftest.to_sdfg() | ||
sdfg.apply_gpu_transformations() | ||
out = sdfg(A=A, N=20) | ||
assert np.allclose(out, np.maximum(A, 0)) | ||
|
||
|
||
def test_relu_2(): | ||
@dace.program | ||
def halftest(A: dace.float16[N]): | ||
out = np.ndarray([N], dace.float16) | ||
for i in dace.map[0:N]: | ||
with dace.tasklet: | ||
a << A[i] | ||
o >> out[i] | ||
o = max(a, 0) | ||
return out | ||
|
||
A = np.random.rand(20).astype(np.float16) | ||
sdfg = halftest.to_sdfg() | ||
sdfg.apply_gpu_transformations() | ||
out = sdfg(A=A, N=20) | ||
assert np.allclose(out, np.maximum(A, 0)) | ||
|
||
|
||
def test_dropout(): | ||
@dace.program | ||
def halftest(A: dace.float16[N], mask: dace.int32[N]): | ||
out = np.ndarray([N], dace.float16) | ||
for i in dace.map[0:N]: | ||
with dace.tasklet: | ||
a << A[i] | ||
d << mask[i] | ||
o >> out[i] | ||
#o = a * dace.float16(d) | ||
o = a if d else dace.float16(0) | ||
return out | ||
|
||
A = np.random.rand(20).astype(np.float16) | ||
mask = np.random.randint(0, 2, size=[20]).astype(np.int32) | ||
sdfg = halftest.to_sdfg() | ||
sdfg.apply_gpu_transformations() | ||
out = sdfg(A=A, mask=mask, N=20) | ||
assert np.allclose(out, A * mask) | ||
|
||
|
||
if __name__ == '__main__': | ||
test_relu() | ||
test_relu_2() | ||
test_dropout() |