forked from cornell-zhang/heterocl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec_test.py
86 lines (77 loc) · 2.91 KB
/
vec_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import heterocl as hcl
import numpy as np
import time
hcl.init()
#-------------------------------------------------------------
# Auto-testbench 3D vector-scalar add
#-------------------------------------------------------------
def vec_auto_3D():
target = hcl.Platform.aws_f1
A = hcl.placeholder((2,3,4),'A')
B = hcl.compute(A.shape, lambda x,y,z: A[x,y,z], name="B")
C = hcl.compute(A.shape, lambda x,y,z: B[x,y,z]+1, name="C")
D = hcl.compute(A.shape, lambda x,y,z: C[x,y,z], name="D")
s = hcl.create_schedule([A,B,C,D])
s[C].vectorize(C.axis[2])
target.config(mode="hw_sim",compiler="vitis")
f = hcl.build(s,target)
np_A = np.random.randint(0, 10, size=(2,3,4))
hcl_A = hcl.asarray(np_A)
hcl_B = hcl.asarray(np.zeros(A.shape))
hcl_C = hcl.asarray(np.zeros(A.shape))
hcl_D = hcl.asarray(np.zeros(A.shape))
f(hcl_A, hcl_B, hcl_C, hcl_D)
#-------------------------------------------------------------
# 1D vector-scalar add
#-------------------------------------------------------------
def add_1D_vec():
A = hcl.placeholder((40,),'A')
B = hcl.compute(A.shape, lambda x: A[x]+1, name="B")
s = hcl.create_schedule([A,B])
s[B].vectorize(B.axis[0]) # [0] for x-axis
f = hcl.build(s, target = "vhls")
print(f)
#-------------------------------------------------------------
# 1D non-vectorized add
#-------------------------------------------------------------
def add_1D_not():
A = hcl.placeholder((40,),'A')
B = hcl.compute(A.shape, lambda x: A[x]+1, name="B")
s = hcl.create_schedule([A,B])
f = hcl.build(s, target = "vhls")
print(f)
#-------------------------------------------------------------
# 2D vector-scalar add
#-------------------------------------------------------------
def add_2D_vec():
A = hcl.placeholder((30, 20),'A')
B = hcl.compute(A.shape, lambda x,y: A[x,y]+1, name="B")
s = hcl.create_schedule([A,B])
s[B].vectorize(B.axis[1]) # [0] for x-axis, [1] for y-axis
f = hcl.build(s, target = "vhls")
print(f)
#-------------------------------------------------------------
# 2D non-vectorized add
#-------------------------------------------------------------
def add_2D_not():
A = hcl.placeholder((40, 35),'A')
B = hcl.compute(A.shape, lambda x,y: A[x,y]+1, name="B")
s = hcl.create_schedule([A,B])
f = hcl.build(s, target = "vhls")
print(f)
#-------------------------------------------------------------
# 3D vector-scalar add
#-------------------------------------------------------------
def add_3D_vec():
A = hcl.placeholder((2, 3, 4),'A')
B = hcl.compute(A.shape, lambda x,y,z: A[x,y,z]+1, name="B")
s = hcl.create_schedule([A,B])
s[B].vectorize(B.axis[2]) # [0] for x-axis, [1] for y-axis, [2] for z-axis
target = hcl.Platform.aws_f1
target.config(compiler="vitis", mode="sw_sim")
f = hcl.build(s, target=target)
np_A = np.random.randint(0, 10, size=(2,3,4))
hcl_A = hcl.asarray(np_A)
hcl_B = hcl.asarray(np.zeros(A.shape))
f(hcl_A, hcl_B)
add_3D_vec()