/
box_blur.clj
419 lines (378 loc) · 18.3 KB
/
box_blur.clj
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
(ns box-blur
(:require [tvm-clj.api :as api]
[tech.opencv :as opencv]
[tech.datatype :as dtype]
[tech.compute.tvm :as tvm]
[tech.compute.verify.tensor :as vf]
[tech.compute :as compute]
[clojure.core.matrix :as m]
[tech.compute.tensor :as ct]
[tech.compute.tensor.defaults :as ct-defaults]
[tvm-clj.api-sugar :refer :all]
[tech.compute.tvm.cpu]
[tech.compute.tvm.gpu]
[tech.compute.tvm.tensor-math])
(:refer-clojure :exclude [+ - / * cast min max = rem])
(:import [org.bytedeco.javacpp opencv_imgproc]))
(set! *warn-on-reflection* true)
;;Note we aren't warning on boxed math. The small amounts of boxed
;;math that happen in this file are irrelevant for speed purposes; its
;;all compiled to the specific backend.
;;A 3x3 box blur. Using the api sugar liberally to make the code shorter
;;and more readable.
(defn box-blur-fn
[intermediate-datatype]
(let [in-height (tvar "height")
in-width (tvar "width")
in-channels (int 3)
input (placeholder [in-height in-width in-channels] "input"
:dtype :uint8)
clamp (fn [val val_min val_max]
(-> (min val val_max)
(max val_min)))
;;Read a clamped-to-edge value of the source image
read-element (fn [img y x c]
(img [(clamp y 0 (- in-height 1))
(clamp x 0 (- in-width 1))
c]))
padded-input (compute
[(+ in-height 2) (+ in-width 2) in-channels]
(lambda [y x c]
(-> (read-element input (- y 1) (- x 1) c)
(cast intermediate-datatype)))
"padded")
x-blur-vec (compute
[(+ in-height 2) in-width in-channels]
(lambda
[y x c]
(+ (padded-input [y x c])
(padded-input [y (+ x 1) c])
(padded-input [y (+ x 2) c])))
"x-blur")
y-blur-vec (compute
[in-height in-width in-channels]
(lambda [y x c]
(+ (x-blur-vec [y x c])
(x-blur-vec [(+ y 1) x c])
(x-blur-vec [(+ y 2) x c])))
"y-blur")
output (compute
[in-height in-width in-channels]
(lambda [y x c]
(-> (y-blur-vec [y x c])
(/ (const 9 intermediate-datatype))
(cast :uint8)))
"box-blur")]
{:input input
:padded padded-input
:x-blur x-blur-vec
:y-blur y-blur-vec
:box-blur output
:output output
:in-height in-height
:in-width in-width
:in-channels in-channels
:arglist [input output]}))
(defn box-blur-no-sugar
[intermediate-datatype]
(let [in-height (api/variable "height")
in-width (api/variable "width")
in-channels (api/const 3 :int32)
input (api/placeholder [in-height in-width in-channels] "input"
:dtype :uint8)
clamp (api/tvm-fn
[val val_min val_max]
(->
(api/min val val_max)
(api/max val_min)))
;;Read a clamped-to-edge value of the source image
read-element (api/tvm-fn
[img y x c]
(api/tget img [(clamp y 0 (api/sub in-height 1))
(clamp x 0 (api/sub in-width 1))
c]))
padded-input-op (api/compute
[(api/add in-height 2) (api/add in-width 2) in-channels]
(api/tvm-fn
[y x c]
(-> (read-element input (api/sub y 1) (api/sub x 1) c)
(api/cast intermediate-datatype)))
"padded")
padded-input (first (api/output-tensors padded-input-op))
;; We know that we will access this blur incrementing the y axis.
;; So we want to store the results of this computation such that
;; Y is the most rapidly changing (elements in Y are adjacent in memory).
;;
;; It would be idea if this could happen at the scheduling phase but
;; currently I do not believe it is possible.
x-blur-op (api/compute
[(api/add in-height 2) in-width in-channels]
(api/tvm-fn
[y x c]
(api/div
(->
(api/add (api/tget padded-input [y (api/add x 2) c])
(api/tget padded-input [y x c]))
(api/add (api/tget padded-input [y (api/add x 1) c])))
(api/const 3 intermediate-datatype)))
"x-blur")
x-blur-vec (first (api/output-tensors x-blur-op))
y-blur-op (api/compute
[in-height in-width in-channels]
(api/tvm-fn
[y x c]
(-> (api/div (-> (api/add
(api/tget x-blur-vec [y x c])
(api/tget x-blur-vec [(api/add y 1) x c]))
(api/add (api/tget x-blur-vec
[(api/add y 2) x c])))
(api/const 3 intermediate-datatype))))
"y-blur")
y-blur-vec (first (api/output-tensors y-blur-op))
final-cast (api/compute
[in-height in-width in-channels]
(api/tvm-fn
[y x c]
(-> (api/tget y-blur-vec [y x c])
(api/add (float 0.5))
(api/cast :uint8)))
"box-blur")
output (first (api/output-tensors final-cast))]
{:input input
:padded padded-input-op
:x-blur x-blur-vec
:y-blur y-blur-vec
:box-blur final-cast
:output output
:in-height in-height
:in-width in-width
:in-channels in-channels
:arglist [input output]}))
(defmacro simple-time
[& body]
`(let [report-time# (->> (repeatedly
10
#(let [start# (System/nanoTime)
result# (do ~@body)]
(- (System/nanoTime) start#)))
(map #(/ (double %)
100000.0))
sort)]
(apply format "top-3 times: %.2fms %.2fms %.2fms"
(take 3 report-time#))))
(defn time-schedule
[schedule-fn & {:keys [device-type algorithm-fn]
:or {device-type :cpu
;;Could also try no-sugar
algorithm-fn box-blur-fn}}]
(let [driver (tvm/driver device-type)]
;;Bind the default compute stream for the default device for this driver
;;Bind the default datatype to use if non are provided.
(vf/tensor-default-context
driver
:uint8
(let [src-img (opencv/load "test/data/test.jpg")
src-tensor (if (= :cpu device-type)
;;If we are a cpu device then we can use the opencv image
;;directly as a tensor.
src-img
;;Else we upload the image to the device returning a new
;;tensor that has a buffer on the device.
ct/clone-to-device)
;;opencv images implement tech.datatype.base/PPrototype.
;;We can create on like this one or we can clone exactly this one.
dst-img (dtype/from-prototype src-img)
;;A terse way of stating the if condition above. cond-> threads the first
;;argument through the clauses that are true and then returns the result.
dst-tensor (cond-> dst-img
(not= :cpu device-type)
ct/clone-to-device)
;;Call the algorithm-fn. This generates an AST that describes our algorithm
;;Then schedule it on the given device type.
{:keys [arglist schedule bind-map]} (-> (algorithm-fn :uint16)
(schedule-fn device-type))
;;Always helpful
_ (println (api/schedule->str schedule arglist "box_blur"))
;;Schedule the function and return a normal clojure function that will do the
;;thing.
box-blur (tvm/schedule->fn driver {:schedule schedule
:arglist arglist
:name :blox-blur
:bind-map (or bind-map {})})]
;;Note that on the cpu, the opencv image itself is used with no copy nor
;;transformation. TVM can operate directly on items that implement enough
;;protocols: (tech.compute.tvm.driver/acceptable-tvm-device-buffer?).
;;This is the 'warmup' run.
(box-blur src-tensor dst-tensor)
_ (when-not (= :cpu device-type)
;;If we aren't on the cpu then transfer the data back to the cpu.
(ct/assign! dst-img dst-tensor))
(let [time-result
(simple-time
(box-blur src-tensor dst-tensor)
;;We need to sync with the host in order to fairly time the algorithm.
;;Without this the algorithm continues to run in the background but the
;;save immediately below would write indeterminate results to the disk.
(compute/sync-with-host (ct-defaults/infer-stream {})))]
;;Write the result and off we go!
(opencv/save dst-img (format "result-%s.jpg" (name device-type)))
time-result)))))
(defn base-schedule
[item device-type]
(let [target (:box-blur item)
schedule (api/create-schedule [target])]
(assoc item :schedule schedule
:arglist [(:input item) target]
:output target)))
(defn parallel
[{:keys [box-blur y-blur x-blur] :as item} device-type]
(let [schedule (api/create-schedule [box-blur])
[y-axis x-axis chan-axis] (:axis box-blur)]
(api/stage-parallel (schedule box-blur) y-axis)
(assoc item :schedule schedule)))
(defn reorder
[{:keys [box-blur y-blur x-blur] :as item} device-type]
(let [schedule (api/create-schedule [box-blur])
[x-x-axis x-chan-axis x-y-axis] (:axis x-blur)
[y-axis x-axis chan-axis] (:axis box-blur)]
(api/stage-reorder (schedule x-blur) [x-y-axis x-x-axis x-chan-axis])
(assoc item :schedule schedule)))
(defn split-axis
[{:keys [box-blur y-blur x-blur] :as item} device-type]
(let [schedule (api/create-schedule [box-blur])
[x-x-axis x-chan-axis x-y-axis] (:axis x-blur)
[y-axis x-axis chan-axis] (:axis box-blur)]
(api/stage-split-axis (schedule box-blur) y-axis 16)
(assoc item :schedule schedule)))
(defn tiled-schedule
[{:keys [box-blur y-blur] :as item} device-type]
(let [schedule (api/create-schedule [box-blur])
[y-axis x-axis chan-axis] (:axis y-blur)
tile-axis
(api/stage-tile (schedule y-blur)
y-axis x-axis 16 3)]
(assoc item
:schedule schedule
:tile-axis tile-axis)))
(defn inline
[{:keys [padded x-blur y-blur box-blur] :as item} device-type]
(let [schedule (api/create-schedule [box-blur])]
(api/stage-inline (schedule padded))
(api/stage-inline (schedule x-blur))
(api/stage-inline (schedule y-blur))
(api/stage-parallel (schedule box-blur)
(api/stage-fuse (schedule box-blur)
(:axis box-blur)))
(assoc item :schedule schedule)))
(defn compute-at
[{:keys [padded x-blur y-blur box-blur] :as item} device-type]
(let [schedule (api/create-schedule [box-blur])
[y-axis x-axis chan-axis] (:axis box-blur)]
(api/stage-inline (schedule x-blur))
(api/stage-inline (schedule padded))
(api/stage-compute-at (schedule y-blur)
(schedule box-blur) y-axis)
(assoc item :schedule schedule)))
(defn all-the-toys
[{:keys [padded box-blur x-blur y-blur in-height in-width in-channels]
:as item} device-type]
(let [schedule (api/create-schedule [box-blur])
[x-blur-y-axis x-blur-x-axis x-blur-chan-axis] (:axis x-blur)
[y-axis x-axis chan-axis] (:axis box-blur)
[y-blur-y-axis y-blur-x-axis y-blur-chan-axis] (:axis y-blur)
[y-outer x-outer y-inner x-inner] (api/stage-tile
(schedule box-blur)
y-axis x-axis
8 8)
;;Here I had to ask some questions:
;;
;;https://discuss.tvm.ai/t/how-to-control-data-layout-of-intermediate-values/898/4
;;We want to calculate X in xyc order but store in xcy order as that is the
;;order it is accessed. This requires setting up a cache operation. First,
;;reorder X so that the computation happens in xcy order. Note that this will
;;make calculating X inefficient at this point due to the extremely out-of-order
;;access of the padded input:. It will make the y-blur more efficient, however.
_ (api/stage-reorder (schedule x-blur) [x-blur-x-axis x-blur-chan-axis
x-blur-y-axis])
{x-blur-cache :tensor
schedule :schedule} (api/schedule-cache-write schedule x-blur "local")
[cache-x-axis cache-chan-axis cache-y-axis] (:axis x-blur-cache)]
(api/stage-inline (schedule padded))
(api/stage-inline (schedule x-blur))
(api/stage-inline (schedule y-blur))
;;Reorder the cache stage to compute in y,x,c order. This means we will read input
;;in a sane order but write to the cache out of order. This is fine because we
;;aren't using those writes for a while meaning the memory stalls will be ignored
;;till much later. Since we are writing thread-local only the system may not even
;;write the cache back out to main memory saving a ton of time.
(api/stage-reorder (schedule x-blur-cache) [cache-y-axis cache-x-axis
cache-chan-axis])
;;If we travel down the y dimension in the tile then we do not have to precompute
;;as much
(if (= device-type :cpu)
(do
;;schedule the cache operation to happen
(api/stage-compute-at (schedule x-blur-cache)
(schedule box-blur) y-inner)
(api/stage-parallel (schedule box-blur) y-outer))
(let [gpu-thread-axis (api/stage-fuse (schedule box-blur)
[x-inner chan-axis])]
(api/stage-compute-at (schedule x-blur-cache)
(schedule box-blur) gpu-thread-axis)
;;We lose the cache here really but it does at least run and produce a correct
;;result. Really, on a per-block basis, you would want to build the cache as a
;;first step using all the threads but that leads to more sophistication than I
;;wanted in a demo. For homework, go through the gpu conv layer tvm tutorial
;;and then apply it here.
(api/stage-bind-gpu (schedule box-blur)
[(api/stage-fuse (schedule box-blur)
[y-outer x-outer y-inner])]
[gpu-thread-axis])))
(assoc item :schedule schedule)))
(defn all-the-toys-vertical-thread-cpu
[{:keys [padded box-blur x-blur y-blur in-height in-width in-channels]
:as item} device-type]
(let [schedule (api/create-schedule [box-blur])
[x-blur-y-axis x-blur-x-axis x-blur-chan-axis] (:axis x-blur)
[y-axis x-axis chan-axis] (:axis box-blur)
[y-blur-y-axis y-blur-x-axis y-blur-chan-axis] (:axis y-blur)
;;We want computations to run vertically down the image
[x-outer x-inner] (api/stage-split-axis (schedule box-blur) x-axis 16)
_ (api/stage-reorder (schedule x-blur) [x-blur-x-axis x-blur-chan-axis x-blur-y-axis])
{x-blur-cache :tensor
schedule :schedule} (api/schedule-cache-write schedule x-blur "local")
[cache-x-axis cache-chan-axis cache-y-axis] (:axis x-blur-cache)]
(api/stage-inline (schedule padded)) ;;inline into x-blur
(api/stage-inline (schedule x-blur)) ;;inline into x-blur-cache
(api/stage-inline (schedule y-blur)) ;;inline into box-blur
;;Reorder the cache stage to compute in y,x,c order. This means we will read input
;;in a sane order but write to the cache out of order. This is fine because we
;;aren't using those writes for a while meaning the memory stalls will be ignored
;;till much later. Since we are writing thread-local only the system may not even
;;write the cache back out to main memory saving a ton of time.
(api/stage-reorder (schedule x-blur-cache) [cache-y-axis cache-x-axis
cache-chan-axis])
(api/stage-reorder (schedule box-blur) [x-outer y-axis x-inner chan-axis])
(if (= device-type :cpu)
(do
;;schedule the cache operation to happen
(api/stage-compute-at (schedule x-blur-cache)
(schedule box-blur) y-axis)
(api/stage-parallel (schedule box-blur) x-outer)
(api/stage-unroll (schedule x-blur-cache) cache-chan-axis)
(api/stage-unroll (schedule box-blur) chan-axis))
(throw (ex-info "GPU version not implemented" {})))
(assoc item :schedule schedule)))
(defn time-opencv
[]
(let [src-img (opencv/load "test/data/test.jpg")
[src-height src-width src-chan] (m/shape src-img)
dst-img (opencv/new-mat src-height src-width src-chan
:dtype :uint8)]
;;warmup, load the images into cache
(opencv_imgproc/blur src-img dst-img (opencv/size 3 3))
(let [time-result
(simple-time
(opencv_imgproc/blur src-img dst-img (opencv/size 3 3)))]
(opencv/save dst-img "reference.jpg")
time-result)))