/
image.clj
344 lines (304 loc) · 14.2 KB
/
image.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
(ns tvm-clj.application.image
"Image resize algorithm showing somewhat nontrivial application
of TVM operators. In this case we have an algorithm which is a simple
average area color algorithm used for scaling images down. This reads a
rectangle in the source image and averages it for every destination pixel.
This is a namespace where you want to view the source :-)
```clojure
(def input-img (bufimg/load \"test/data/jen.jpg\"))
(def test-fn (-> (tvm-area-resize-algo-def)
(schedule-tvm-area)
(compile-scheduled-tvm-area)))
(def result (time (area-resize! input-img 512 test-fn)))
;;179 ms
(def jvm-result (time (area-resize! input-img 512 jvm-area-resize-fn!)))
;;5.7 seconds
```"
(:require [tech.v3.datatype :as dtype]
[tech.v3.tensor :as dtt]
[tech.v3.tensor.dimensions :as dims]
[tech.v3.libs.buffered-image :as bufimg]
[tvm-clj.ast :as ast]
[tvm-clj.ast.elemwise-op :as ast-op]
[tvm-clj.schedule :as schedule]
[tvm-clj.compiler :as compiler]
[tvm-clj.module :as module]
[tvm-clj.device :as device]
[primitive-math :as pmath]
[tech.v3.resource :as resource])
(:import [tech.v3.datatype NDBuffer ObjectReader]))
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)
(defn area-resize!
"Perform an area resize with a defined resize algorithm."
[input ^long new-width resize-fn]
(let [[^long height ^long width _nchan] (dtype/shape input)
ratio (double (/ new-width width))
new-height (Math/round (* height ratio))
output-img (bufimg/new-image new-height new-width
(bufimg/image-type input))]
(resize-fn (dtt/ensure-tensor input) (dtt/ensure-tensor output-img))
output-img))
(defn- clamp
^double [^double value ^double val_min ^double val_max]
(-> (min value val_max)
(max val_min)))
(defn- clamp-long
^long [^long value ^long val_min ^long val_max]
(-> (min value val_max)
(max val_min)))
(defn- src-coord
^long [^long dest-coord ^long kernel-idx ^long kernel-width ^double out-over-in]
(- (+ (Math/round (/ dest-coord out-over-in))
kernel-idx)
(quot kernel-width 2)))
(defn jvm-area-resize-algo
[input output-shape]
(let [[^long in-height ^long in-width n-chan] (dtype/shape input)
[^long out-height ^long out-width n-chan] output-shape
input (dtt/ensure-tensor input)
max-idx-x (dec in-width)
max-idx-y (dec in-height)
x-ratio (/ (double out-width) (double in-width))
y-ratio (/ (double out-height) (double in-height))
;;Size of the reduction rectangle in the X dimension
;;Size of the reduction rectangle in the Y dimension
reduce-kernel-width (/ 1.0 x-ratio)
reduce-kernel-height (/ 1.0 y-ratio)
divisor (* x-ratio y-ratio)
identity-value 0.0]
;;Define a tensor using an algorithm definition - a 'compute' tensor
(dtt/typed-compute-tensor
;;Datatype for result
:uint8
;;Output shape
[out-height out-width n-chan]
;;Argument names used in code block below
[y x c]
;;Tensor definition. This is compiled inline into a new reified tensor
;;type.
(-> (loop [k-idx-y 0
outer-sum identity-value]
(if (< k-idx-y reduce-kernel-height)
(recur (unchecked-inc k-idx-y)
(double
(loop [k-idx-x 0
inner-sum outer-sum]
(if (< k-idx-x reduce-kernel-width)
(let [src-coord-x (clamp-long
(src-coord x k-idx-x reduce-kernel-width x-ratio)
0
max-idx-x)
src-coord-y (clamp-long
(src-coord y k-idx-y reduce-kernel-height y-ratio)
0
max-idx-y)]
(recur (unchecked-inc k-idx-x)
(pmath/+ inner-sum (.ndReadDouble input
src-coord-y
src-coord-x
c))))
inner-sum))))
outer-sum))
(double)
(* divisor)
(clamp 0.0 255.0)
(unchecked-long)))))
(defn jvm-area-split-resize-algo
[input output-shape]
(let [[^long in-height ^long in-width n-chan] (dtype/shape input)
[^long out-height ^long out-width n-chan] output-shape
input (dtt/ensure-tensor input)
max-idx-x (dec in-width)
max-idx-y (dec in-height)
x-ratio (double (/ (double out-width) in-width))
y-ratio (double (/ (double out-height) in-height))
reduce-kernel-width (/ 1.0 x-ratio)
reduce-kernel-height (/ 1.0 y-ratio)
divisor (* x-ratio y-ratio)
identity-value 0.0
;;If we split the X,y calculations we can be more work and
;;memory efficient as we do not recalculate as many partial
;;summations
horiz-sum (dtt/typed-compute-tensor
;;datatype
:float64
;;shape
[in-height out-width n-chan]
;argument names
[y x c]
;;per-element read code block
(loop [k-idx-x 0
inner-sum identity-value]
(if (< k-idx-x reduce-kernel-width)
(let [src-coord-x (clamp-long
(src-coord x k-idx-x reduce-kernel-width x-ratio)
0
max-idx-x)
src-coord-y y]
(recur (unchecked-inc k-idx-x)
(pmath/+ inner-sum (.ndReadDouble input src-coord-y
src-coord-x c))))
inner-sum)))
;;Force the calculation to complete to a temporary
temp-img (dtt/clone horiz-sum)]
;;Return the defined but not executed result.
(dtt/typed-compute-tensor
;;datatype
:uint8
;;shape
[out-height out-width n-chan]
;;per element arg names
[y x c]
;;code block
(-> (loop [k-idx-y 0
outer-sum identity-value]
(if (< k-idx-y reduce-kernel-height)
(let [src-coord-x x
src-coord-y (clamp-long
(src-coord y k-idx-y reduce-kernel-height y-ratio)
0
max-idx-y)]
(recur (unchecked-inc k-idx-y)
(pmath/+ outer-sum (.ndReadDouble temp-img src-coord-y
src-coord-x c))))
outer-sum))
(double)
(* divisor)
(clamp 0.0 255.0)
(unchecked-long)))))
(defn jvm-area-resize-fn!
[jvm-resize-algo input output]
(dtype/copy! (jvm-resize-algo input (dtype/shape output))
output)
output)
(defn tvm-area-resize-algo
"Step 1 is to define the algorithm. This definition looks strikingly similar
to the definition above."
[n-channels device-type]
(let [n-chan (ast-op/const n-channels :int32)
in-width (ast/variable "in-width")
in-height (ast/variable "in-height")
out-width (ast/variable "out-width")
out-height (ast/variable "out-height")
input (ast/placeholder [in-height in-width n-chan] "input" :dtype :uint8)
max-idx-x (ast-op/- in-width (int 1))
max-idx-y (ast-op/- in-height (int 1))
x-ratio (ast-op// (ast-op/cast out-width :float32) (ast-op/cast in-width :float32))
y-ratio (ast-op// (ast-op/cast out-height :float32) (ast-op/cast in-height :float32))
;;Size of the reduction rectangle in the X dimension
;;Size of the reduction rectangle in the Y dimension
reduce-kernel-width (ast-op// (float 1.0) x-ratio)
reduce-kernel-height (ast-op// (float 1.0) y-ratio)
divisor (ast-op/* x-ratio y-ratio)
clamp-fn (fn [val val-min val-max]
(-> (ast-op/min val val-max)
(ast-op/max val-min)))
coord-fn (fn [dest-coord kernel-idx kernel-width out-over-in]
(-> (ast-op// (ast-op/cast dest-coord :float32) out-over-in)
(ast-op/+ (ast-op/cast kernel-idx :float32))
(ast-op/- (ast-op// kernel-width (float 2.0)))
(ast-op/cast :int32)))
partial-result (-> (ast/compute
[out-height out-width n-chan] "partial-result"
[y x c]
(ast/commutative-reduce
;;First arg is a commutative reducer.
[:+ :float32]
;;Next are the inner axis we will reduce over
[{:domain [0 reduce-kernel-height] :name "k-idx-y"}
{:domain [0 reduce-kernel-width] :name "k-idx-x"}]
;;Finally a function from reduction axes to every input
;;argument as defined by our reducer above.
[(fn [k-idx-y k-idx-x]
(-> (ast/tget input
[(-> (coord-fn y k-idx-y reduce-kernel-height y-ratio)
(clamp-fn (int 0) max-idx-y))
(-> (coord-fn x k-idx-x reduce-kernel-width x-ratio)
(clamp-fn (int 0) max-idx-x))
c])
;;perform operation in float32 space
(ast-op/cast :float32)))]))
(ast/first-output))
result (-> (ast/compute
[out-height out-width n-chan] "result"
[y x c]
(-> (ast/tget partial-result [y x c])
(ast-op/* divisor)
(clamp-fn (float 0) (float 255))
;;convert back to uint8 space
(ast-op/cast :uint8)))
(ast/first-output))
schedule (schedule/create-schedule result)
stage-map (:stage_map schedule)
partial-stage (stage-map (ast/->operation partial-result))
final-stage (stage-map (ast/->operation result))
[final-y final-x final-c] (get-in result [:op :axis])]
(if (= device-type :llvm)
(let [[final-y-outer final-x-outer final-y-inner final-x-inner]
(schedule/stage-tile final-stage
final-y
final-x
1, 16)]
(schedule/stage-compute-at partial-stage final-stage final-c)
(schedule/stage-parallel final-stage final-x-outer))
;;gpu schedule
(let [[final-y-outer final-x-outer final-y-inner final-x-inner]
(schedule/stage-tile final-stage
final-y
final-x
16, 16)
block-axis (schedule/stage-fuse final-stage [final-y-outer final-x-outer])
thread-axis (schedule/stage-fuse final-stage [final-y-inner final-x-inner])]
(schedule/stage-compute-at partial-stage final-stage final-c)
(schedule/stage-bind-gpu final-stage [block-axis] [thread-axis])))
{:arguments [input result]
:target device-type
:schedule schedule}))
(def tvm-fns
(memoize
(fn [n-chan device-type]
(let [tvm-fn (-> (tvm-area-resize-algo n-chan device-type)
(compiler/ir->fn (format "%s_area_resize"
(name device-type)
n-chan)))]
(fn [input output]
(resource/stack-resource-context
(let [cpu-input (dtt/ensure-native input)
cpu-output (dtt/native-tensor (dtype/shape output)
(dtype/elemwise-datatype output))
device-id 0
kernel-input (if (= device-type :llvm)
cpu-input
(device/cpu->device cpu-input device-type device-id
{:resource-type :auto}))
kernel-output (if (= device-type :llvm)
cpu-output
(device/device-tensor cpu-output
device-type
device-id))]
(tvm-fn kernel-input kernel-output)
(when (not= device-type :llvm)
(device/copy-tensor! kernel-output cpu-output nil)
(device/sync-with-host device-type 0))
(dtype/copy! cpu-output output))))))))
(comment
(do
(def input-img (bufimg/load "test/data/jen.jpg")))
(def jvm-result (time (area-resize! input-img 512 (partial jvm-area-resize-fn! jvm-area-resize-algo))))
;;1.8 seconds
(def jvm-split-result (time (area-resize!
input-img 512
(partial jvm-area-resize-fn!
jvm-area-split-resize-algo))))
;;1.6 seconds
(def tvm-cpu-fn (tvm-fns (last (dtype/shape input-img)) :llvm))
(def tvm-cpu-result (time (area-resize! input-img 512 tvm-cpu-fn)))
;;75ms
(def tvm-cuda-fn (tvm-fns (last (dtype/shape input-img)) :cuda))
(def cuda-result (time (area-resize! input-img 512 tvm-cuda-fn)))
;;88ms
(def tvm-opencl-fn (tvm-fns (last (dtype/shape input-img)) :opencl))
(def opencl-result (time (area-resize! input-img 512 tvm-opencl-fn)))
;;65ms
)