-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconv2d_example.py
More file actions
245 lines (200 loc) · 12.1 KB
/
conv2d_example.py
File metadata and controls
245 lines (200 loc) · 12.1 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# Conv2D example with hierarchical tiling and explicit double buffering (DaCe).
#
# Layouts:
# X: [H, W, Cin] (NHWC with implicit N=1)
# W: [KH, KW, Cin, Cout]
# Y: [Hout, Wout, Cout] (valid convolution)
#
# Memory hierarchy:
# - L3: Global memory (X, W, Y)
# - L2: Double-buffered tiles of X and W (ping-pong buffers)
# - L1: Double-buffered tiles of X, W, and Y partial sums
#
# Notes:
# - This is a readable reference implementation of the same buffering approach
# used in gemm_example.py, not a fully optimized conv kernel.
# - Double buffering is expressed structurally (distinct buffers + control flow),
# not via reassignment of Views (illegal in DaCe).
import dace
import numpy as np
from conv2d_tiling_solver import MemoryBudget, Conv2DShape, get_conv2d_tiling
# -----------------------------------------------------------------------------
# Problem definition (keep small while iterating)
# -----------------------------------------------------------------------------
H, W = 32, 32
Cin, Cout = 16, 32
KH, KW = 3, 3
stride_h, stride_w = 1, 1
Hout = (H - KH) // stride_h + 1
Wout = (W - KW) // stride_w + 1
dtype = dace.float32
dtype_bytes = 4
# -----------------------------------------------------------------------------
# Memory budgets (example values; tune for your target)
# -----------------------------------------------------------------------------
budgets = MemoryBudget(
L1_bytes=64 * 1024,
L2_bytes=512 * 1024,
L3_bytes=8 * 1024 * 1024,
)
# -----------------------------------------------------------------------------
# Query hierarchical tiling
# -----------------------------------------------------------------------------
shape = Conv2DShape(H=H, W=W, Cin=Cin, Cout=Cout, KH=KH, KW=KW,
stride_h=stride_h, stride_w=stride_w, Hout=Hout, Wout=Wout)
tile = get_conv2d_tiling(shape, budgets, dtype_bytes=dtype_bytes, double_buffering=True)
TH, TW, TIC, TOC = tile.TH, tile.TW, tile.TIC, tile.TOC
TH2, TW2, TIC2, TOC2 = tile.TH_L2, tile.TW_L2, tile.TIC_L2, tile.TOC_L2
TH3, TW3, TIC3, TOC3 = tile.TH_L3, tile.TW_L3, tile.TIC_L3, tile.TOC_L3
print("Chosen tiling:")
print(f" L1: TH={TH}, TW={TW}, TIC={TIC}, TOC={TOC}")
print(f" L2: TH={TH2}, TW={TW2}, TIC={TIC2}, TOC={TOC2}")
print(f" L3: TH={TH3}, TW={TW3}, TIC={TIC3}, TOC={TOC3}")
# Derived block counts (unit-step maps)
IC2_BLOCKS = Cin // TIC2
IC1_BLOCKS = TIC2 // TIC
# Derived spatial footprints for input tiles (constants; keep out of the DaCe program)
IH2 = (TH2 - 1) * stride_h + KH
IW2 = (TW2 - 1) * stride_w + KW
IH1 = (TH - 1) * stride_h + KH
IW1 = (TW - 1) * stride_w + KW
# -----------------------------------------------------------------------------
# DACE program
# -----------------------------------------------------------------------------
@dace.program
def conv2d(X: dtype[H, W, Cin],
Wt: dtype[KH, KW, Cin, Cout],
Y: dtype[Hout, Wout, Cout]):
for ho3, wo3, co3 in dace.map[0:Hout:TH3, 0:Wout:TW3, 0:Cout:TOC3]:
# L3 buffer for Y tile
Y_L3 = dace.define_local((TH3, TW3, TOC3), dtype, storage=dace.StorageType.CPU_Heap)
# Initialize Y_L3 from global Y
for oh, ow, oc in dace.map[0:TH3, 0:TW3, 0:TOC3]:
Y_L3[oh, ow, oc] = Y[ho3 + oh, wo3 + ow, co3 + oc]
for ho2, wo2, co2 in dace.map[ho3:ho3 + TH3:TH2, wo3:wo3 + TW3:TW2, co3:co3 + TOC3:TOC2]:
# L2 double buffers for input X and weights Wt, ping-pong over IC blocks
# Input tile spatial footprint for OHxOW output tile:
# IH = (OH-1)*stride + KH, IW = (OW-1)*stride + KW
X_L2_0 = dace.define_local((IH2, IW2, TIC2), dtype, storage=dace.StorageType.CPU_Heap)
X_L2_1 = dace.define_local((IH2, IW2, TIC2), dtype, storage=dace.StorageType.CPU_Heap)
W_L2_0 = dace.define_local((KH, KW, TIC2, TOC2), dtype, storage=dace.StorageType.CPU_Heap)
W_L2_1 = dace.define_local((KH, KW, TIC2, TOC2), dtype, storage=dace.StorageType.CPU_Heap)
for t in dace.map[0:IC2_BLOCKS]:
ci2 = t * TIC2
ping2 = t % 2
if ping2 == 0:
# Load X tile into L2 buffer 0
for ih, iw, ic in dace.map[0:IH2, 0:IW2, 0:TIC2]:
X_L2_0[ih, iw, ic] = X[(ho2 * stride_h) + ih, (wo2 * stride_w) + iw, ci2 + ic]
# Load W tile into L2 buffer 0
for kh, kw, ic, oc in dace.map[0:KH, 0:KW, 0:TIC2, 0:TOC2]:
W_L2_0[kh, kw, ic, oc] = Wt[kh, kw, ci2 + ic, co2 + oc]
# L1 blocking inside the L2 tile
for ho1, wo1, co1 in dace.map[0:TH2:TH, 0:TW2:TW, 0:TOC2:TOC]:
# L1 double buffers
X_L1_0 = dace.define_local((IH1, IW1, TIC), dtype, storage=dace.StorageType.CPU_Heap)
X_L1_1 = dace.define_local((IH1, IW1, TIC), dtype, storage=dace.StorageType.CPU_Heap)
W_L1_0 = dace.define_local((KH, KW, TIC, TOC), dtype, storage=dace.StorageType.CPU_Heap)
W_L1_1 = dace.define_local((KH, KW, TIC, TOC), dtype, storage=dace.StorageType.CPU_Heap)
# Y partial sums in L1 (double buffered)
Y_L1_0 = dace.define_local((TH, TW, TOC), dtype, storage=dace.StorageType.CPU_Heap)
Y_L1_1 = dace.define_local((TH, TW, TOC), dtype, storage=dace.StorageType.CPU_Heap)
for oh, ow, oc in dace.map[0:TH, 0:TW, 0:TOC]:
Y_L1_0[oh, ow, oc] = 0
Y_L1_1[oh, ow, oc] = 0
# Ping-pong over IC blocks inside TIC2
for tb in dace.map[0:IC1_BLOCKS]:
ci1 = tb * TIC
ping1 = tb % 2
if ping1 == 0:
# Load X/W into L1 buffer 0
for ih, iw, ic in dace.map[0:IH1, 0:IW1, 0:TIC]:
X_L1_0[ih, iw, ic] = X_L2_0[(ho1 * stride_h) + ih, (wo1 * stride_w) + iw, ci1 + ic]
for kh, kw, ic, oc in dace.map[0:KH, 0:KW, 0:TIC, 0:TOC]:
W_L1_0[kh, kw, ic, oc] = W_L2_0[kh, kw, ci1 + ic, co1 + oc]
# Compute/update into Y_L1_0
for oh, ow, oc, kh, kw, ic in dace.map[0:TH, 0:TW, 0:TOC, 0:KH, 0:KW, 0:TIC]:
ih = oh * stride_h + kh
iw = ow * stride_w + kw
Y_L1_0[oh, ow, oc] += X_L1_0[ih, iw, ic] * W_L1_0[kh, kw, ic, oc]
else:
# Load X/W into L1 buffer 1
for ih, iw, ic in dace.map[0:IH1, 0:IW1, 0:TIC]:
X_L1_1[ih, iw, ic] = X_L2_0[(ho1 * stride_h) + ih, (wo1 * stride_w) + iw, ci1 + ic]
for kh, kw, ic, oc in dace.map[0:KH, 0:KW, 0:TIC, 0:TOC]:
W_L1_1[kh, kw, ic, oc] = W_L2_0[kh, kw, ci1 + ic, co1 + oc]
# Compute/update into Y_L1_1
for oh, ow, oc, kh, kw, ic in dace.map[0:TH, 0:TW, 0:TOC, 0:KH, 0:KW, 0:TIC]:
ih = oh * stride_h + kh
iw = ow * stride_w + kw
Y_L1_1[oh, ow, oc] += X_L1_1[ih, iw, ic] * W_L1_1[kh, kw, ic, oc]
# Accumulate Y_L1 back to Y_L3
for oh, ow, oc in dace.map[0:TH, 0:TW, 0:TOC]:
Y_L3[(ho2 - ho3) + ho1 + oh, (wo2 - wo3) + wo1 + ow, (co2 - co3) + co1 + oc] += (
Y_L1_0[oh, ow, oc] + Y_L1_1[oh, ow, oc]
)
else:
# Load X tile into L2 buffer 1
for ih, iw, ic in dace.map[0:IH2, 0:IW2, 0:TIC2]:
X_L2_1[ih, iw, ic] = X[(ho2 * stride_h) + ih, (wo2 * stride_w) + iw, ci2 + ic]
# Load W tile into L2 buffer 1
for kh, kw, ic, oc in dace.map[0:KH, 0:KW, 0:TIC2, 0:TOC2]:
W_L2_1[kh, kw, ic, oc] = Wt[kh, kw, ci2 + ic, co2 + oc]
# L1 blocking inside the L2 tile
for ho1, wo1, co1 in dace.map[0:TH2:TH, 0:TW2:TW, 0:TOC2:TOC]:
# L1 double buffers
X_L1_0 = dace.define_local((IH1, IW1, TIC), dtype, storage=dace.StorageType.CPU_Heap)
X_L1_1 = dace.define_local((IH1, IW1, TIC), dtype, storage=dace.StorageType.CPU_Heap)
W_L1_0 = dace.define_local((KH, KW, TIC, TOC), dtype, storage=dace.StorageType.CPU_Heap)
W_L1_1 = dace.define_local((KH, KW, TIC, TOC), dtype, storage=dace.StorageType.CPU_Heap)
# Y partial sums in L1 (double buffered)
Y_L1_0 = dace.define_local((TH, TW, TOC), dtype, storage=dace.StorageType.CPU_Heap)
Y_L1_1 = dace.define_local((TH, TW, TOC), dtype, storage=dace.StorageType.CPU_Heap)
for oh, ow, oc in dace.map[0:TH, 0:TW, 0:TOC]:
Y_L1_0[oh, ow, oc] = 0
Y_L1_1[oh, ow, oc] = 0
for tb in dace.map[0:IC1_BLOCKS]:
ci1 = tb * TIC
ping1 = tb % 2
if ping1 == 0:
for ih, iw, ic in dace.map[0:IH1, 0:IW1, 0:TIC]:
X_L1_0[ih, iw, ic] = X_L2_1[(ho1 * stride_h) + ih, (wo1 * stride_w) + iw, ci1 + ic]
for kh, kw, ic, oc in dace.map[0:KH, 0:KW, 0:TIC, 0:TOC]:
W_L1_0[kh, kw, ic, oc] = W_L2_1[kh, kw, ci1 + ic, co1 + oc]
for oh, ow, oc, kh, kw, ic in dace.map[0:TH, 0:TW, 0:TOC, 0:KH, 0:KW, 0:TIC]:
ih = oh * stride_h + kh
iw = ow * stride_w + kw
Y_L1_0[oh, ow, oc] += X_L1_0[ih, iw, ic] * W_L1_0[kh, kw, ic, oc]
else:
for ih, iw, ic in dace.map[0:IH1, 0:IW1, 0:TIC]:
X_L1_1[ih, iw, ic] = X_L2_1[(ho1 * stride_h) + ih, (wo1 * stride_w) + iw, ci1 + ic]
for kh, kw, ic, oc in dace.map[0:KH, 0:KW, 0:TIC, 0:TOC]:
W_L1_1[kh, kw, ic, oc] = W_L2_1[kh, kw, ci1 + ic, co1 + oc]
for oh, ow, oc, kh, kw, ic in dace.map[0:TH, 0:TW, 0:TOC, 0:KH, 0:KW, 0:TIC]:
ih = oh * stride_h + kh
iw = ow * stride_w + kw
Y_L1_1[oh, ow, oc] += X_L1_1[ih, iw, ic] * W_L1_1[kh, kw, ic, oc]
for oh, ow, oc in dace.map[0:TH, 0:TW, 0:TOC]:
Y_L3[(ho2 - ho3) + ho1 + oh, (wo2 - wo3) + wo1 + ow, (co2 - co3) + co1 + oc] += (
Y_L1_0[oh, ow, oc] + Y_L1_1[oh, ow, oc]
)
# Store Y_L3 back to global Y
for oh, ow, oc in dace.map[0:TH3, 0:TW3, 0:TOC3]:
Y[ho3 + oh, wo3 + ow, co3 + oc] = Y_L3[oh, ow, oc]
if __name__ == "__main__":
X = np.random.rand(H, W, Cin).astype(np.float32)
Wt = np.random.rand(KH, KW, Cin, Cout).astype(np.float32)
Y = np.zeros((Hout, Wout, Cout), dtype=np.float32)
conv2d(X, Wt, Y)
# Reference (naive NumPy) for correctness
Y_ref = np.zeros_like(Y)
for oh in range(Hout):
for ow in range(Wout):
for oc in range(Cout):
acc = 0.0
for kh in range(KH):
for kw in range(KW):
for ic in range(Cin):
acc += X[oh * stride_h + kh, ow * stride_w + kw, ic] * Wt[kh, kw, ic, oc]
Y_ref[oh, ow, oc] = acc
print("Max error:", np.max(np.abs(Y - Y_ref)))