forked from alibaba/MNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer_to_image.cl
455 lines (391 loc) · 19.2 KB
/
buffer_to_image.cl
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#define GLOBAL_SIZE_2_DIMS __private const int global_size_dim0, __private const int global_size_dim1,
#define DEAL_NON_UNIFORM_DIM2(input1, input2) \
if (input1 >= global_size_dim0 || input2 >= global_size_dim1) { \
return; \
}
__constant sampler_t SAMPLER = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;
__kernel void nc4hw4_buffer_to_image(GLOBAL_SIZE_2_DIMS __global const float *input_ptr, __private const int2 output_shape,
__private const int channel_up_4, __write_only image2d_t output) {
int image_width_idx = get_global_id(0);
int image_height_idx = get_global_id(1);
DEAL_NON_UNIFORM_DIM2(image_width_idx, image_height_idx);
const int batch_idx = image_height_idx / output_shape.x;
const int height_idx = image_height_idx % output_shape.x;
const int width_idx = image_width_idx % output_shape.y;
const int channel_block_idx = image_width_idx / output_shape.y;
int buffer_offset =
(((batch_idx * channel_up_4 + channel_block_idx) * output_shape.x + height_idx) * output_shape.y + width_idx) * 4;
float4 values = vload4(0, input_ptr + buffer_offset);
int2 coord = (int2)(image_width_idx, image_height_idx);
write_imagef(output, coord, values);
}
__kernel void image_to_nc4hw4_buffer(GLOBAL_SIZE_2_DIMS __global float *output, /* nchw */
__private const int2 output_shape,
__private const int channel_up_4,
__read_only image2d_t input_ptr) {
int image_width_idx = get_global_id(0);
int image_height_idx = get_global_id(1);
DEAL_NON_UNIFORM_DIM2(image_width_idx, image_height_idx);
const int batch_idx = image_height_idx / output_shape.x;
const int height_idx = image_height_idx % output_shape.x;
const int width_idx = image_width_idx % output_shape.y;
int channel_block_idx = image_width_idx / output_shape.y;
int buffer_offset =
(((batch_idx * channel_up_4 + channel_block_idx) * output_shape.x + height_idx) * output_shape.y + width_idx) * 4;
int2 coord = (int2)(image_width_idx, image_height_idx);
float4 values = read_imagef(input_ptr, SAMPLER, coord);
vstore4(values, 0, output + buffer_offset);
}
// convert kernel : from buffer(oihw) to image(oc/4 h w , ic oc4)
__kernel void conv2d_filter_buffer_to_image(GLOBAL_SIZE_2_DIMS __global const float *input_ptr,
__private const int output_channel, __private const int2 kernel_shape, __private const int ic_h_w_size,
__private const int height_width_size, __write_only image2d_t output) {
int image_width_idx = get_global_id(0); // ic
int image_height_idx = get_global_id(1); // oc/4 h w
DEAL_NON_UNIFORM_DIM2(image_width_idx, image_height_idx);
const int input_channel_4_idx = image_width_idx;
const int output_channel_4_idx = (image_height_idx / height_width_size) * 4;
const int height_width_idx = image_height_idx % height_width_size;
const int buffer_height_idx = height_width_idx / kernel_shape.y;
const int buffer_width_idx = height_width_idx % kernel_shape.y;
const int buffer_offset = output_channel_4_idx * ic_h_w_size + input_channel_4_idx * height_width_size +
buffer_height_idx * kernel_shape.y + buffer_width_idx;
float4 output_values = 0;
if (output_channel_4_idx < output_channel) {
const int remain_channel = output_channel - output_channel_4_idx;
if (remain_channel >= 4) {
int offset = buffer_offset;
output_values.x = *(input_ptr + offset);
offset = mad24(1, ic_h_w_size, offset);
output_values.y = *(input_ptr + offset);
offset += ic_h_w_size;
output_values.z = *(input_ptr + offset);
offset += ic_h_w_size;
output_values.w = *(input_ptr + offset);
} else if (remain_channel == 3) {
int offset = buffer_offset;
output_values.x = *(input_ptr + offset);
offset = mad24(1, ic_h_w_size, offset);
output_values.y = *(input_ptr + offset);
offset += ic_h_w_size;
output_values.z = *(input_ptr + offset);
} else if (remain_channel == 2) {
int offset = buffer_offset;
output_values.x = *(input_ptr + offset);
offset = mad24(1, ic_h_w_size, offset);
output_values.y = *(input_ptr + offset);
} else if (remain_channel == 1) {
int offset = buffer_offset;
output_values.x = *(input_ptr + offset);
}
}
write_imagef(output, (int2)(image_width_idx, image_height_idx), output_values);
}
// only for debug
// convert kernel : from image(oc/4 h w , ic oc4) to buffer(oihw)
__kernel void conv2d_filter_image_to_buffer(GLOBAL_SIZE_2_DIMS __global float *output_ptr,
__private const int output_channel, __private const int2 kernel_shape,
__private const int ic_h_w_size,
__private const int height_width_size, __read_only image2d_t input_ptr) {
int image_width_idx = get_global_id(0);
int image_height_idx = get_global_id(1);
DEAL_NON_UNIFORM_DIM2(image_width_idx, image_height_idx);
const int input_channel_4_idx = image_width_idx;
const int output_channel_4_idx = image_height_idx / height_width_size * 4;
const int height_width_idx = image_height_idx % height_width_size;
const int buffer_height_idx = height_width_idx / kernel_shape.y;
const int buffer_width_idx = height_width_idx % kernel_shape.y;
const int buffer_offset = output_channel_4_idx * ic_h_w_size + input_channel_4_idx * height_width_size +
buffer_height_idx * kernel_shape.y + buffer_width_idx;
if (output_channel_4_idx < output_channel) {
int2 coord = (int2)(image_width_idx, image_height_idx);
float4 values = read_imagef(input_ptr, SAMPLER, coord);
const int remain_channel = (output_channel - output_channel_4_idx);
if (remain_channel >= 4) {
int offset = buffer_offset;
output_ptr[offset] = values.x;
offset = mad24(1, ic_h_w_size, offset);
output_ptr[offset] = values.y;
offset += ic_h_w_size;
output_ptr[offset] = values.z;
offset += ic_h_w_size;
output_ptr[offset] = values.w;
} else if (remain_channel == 3) {
int offset = buffer_offset;
output_ptr[offset] = values.x;
offset = mad24(1, ic_h_w_size, offset);
output_ptr[offset] = values.y;
offset += ic_h_w_size;
output_ptr[offset] = values.z;
} else if (remain_channel == 2) {
int offset = buffer_offset;
output_ptr[offset] = values.x;
offset = mad24(1, ic_h_w_size, offset);
output_ptr[offset] = values.y;
} else if (remain_channel == 1) {
int offset = buffer_offset;
output_ptr[offset] = values.x;
}
}
}
// convert kernel from buffer(mihw) to image(ic/4, ic4 h w m)
// but now dw only support m == 1
__kernel void dw_filter_buffer_to_image(GLOBAL_SIZE_2_DIMS __global const float *input_ptr,
__private const int4 kernel_shape,
__private const int height_width_size, __write_only image2d_t output) {
const int image_width_idx = get_global_id(0);
const int image_height_idx = get_global_id(1);
DEAL_NON_UNIFORM_DIM2(image_width_idx, image_height_idx);
float4 output_values = 0;
if (kernel_shape.x == 1) {
const int input_channel_4_idx = image_height_idx * 4;
const int buffer_height_idx = image_width_idx / kernel_shape.w;
const int buffer_width_idx = image_width_idx % kernel_shape.w;
const int buffer_offset =
mad24(mad24(input_channel_4_idx, kernel_shape.z, buffer_height_idx), kernel_shape.w, buffer_width_idx);
const int remain_channel = kernel_shape.y - input_channel_4_idx;
if (input_channel_4_idx < kernel_shape.y) {
if (remain_channel >= 4) {
int offset = buffer_offset;
output_values.x = *(input_ptr + offset);
offset += height_width_size;
output_values.y = *(input_ptr + offset);
offset += height_width_size;
output_values.z = *(input_ptr + offset);
offset += height_width_size;
output_values.w = *(input_ptr + offset);
} else if (remain_channel == 3) {
int offset = buffer_offset;
output_values.x = *(input_ptr + offset);
offset += height_width_size;
output_values.y = *(input_ptr + offset);
offset += height_width_size;
output_values.z = *(input_ptr + offset);
} else if (remain_channel == 2) {
int offset = buffer_offset;
output_values.x = *(input_ptr + offset);
offset += height_width_size;
output_values.y = *(input_ptr + offset);
} else if (remain_channel == 1) {
int offset = buffer_offset;
output_values.x = *(input_ptr + offset);
}
}
}
write_imagef(output, (int2)(image_width_idx, image_height_idx), output_values);
}
// convert data from buffer(nhwc) to image(b h, ic/4 w ic4)
__kernel void nhwc_buffer_to_image(GLOBAL_SIZE_2_DIMS __global const float *input_ptr, __private const int height,
__private const int width, __private const int channels,
__write_only image2d_t output) {
int image_width_idx = get_global_id(0);
int image_height_idx = get_global_id(1);
DEAL_NON_UNIFORM_DIM2(image_width_idx, image_height_idx);
const int batch_idx = image_height_idx / height;
const int height_idx = image_height_idx % height;
const int width_idx = image_width_idx % width;
const int channel_4_idx = (image_width_idx / width) << 2;
const int buffer_offset = ((batch_idx * height + height_idx) * width + width_idx) * channels + channel_4_idx;
const int remain_channel = channels - channel_4_idx;
__global const float *input_current_ptr = input_ptr + buffer_offset;
float4 values = 0;
values = vload4(0, input_current_ptr);
if (remain_channel == 3) {
values.w = 0;
} else if (remain_channel == 2) {
values.z = 0;
values.w = 0;
} else if (remain_channel == 1) {
values.y = 0;
values.z = 0;
values.w = 0;
}
write_imagef(output, (int2)(image_width_idx, image_height_idx), values);
}
// convert data from buffer(nchw) to image(b h, ic/4 w ic4)
__kernel void nchw_buffer_to_image(GLOBAL_SIZE_2_DIMS __global const float *input_ptr, /* nchw */
__private const int height, __private const int width, __private const int channels,
__write_only image2d_t output) {
int image_width_idx = get_global_id(0);
int image_height_idx = get_global_id(1);
DEAL_NON_UNIFORM_DIM2(image_width_idx, image_height_idx);
const int batch_idx = image_height_idx / height;
const int height_idx = image_height_idx % height;
const int width_idx = image_width_idx % width;
const int channel_4_idx = image_width_idx / width << 2;
const int buffer_offset = ((batch_idx * channels + channel_4_idx) * height + height_idx) * width + width_idx;
const int remain_channel = channels - channel_4_idx;
const int height_width_size = height * width;
float4 output_values = 0;
if (remain_channel >= 4) {
int offset = buffer_offset;
output_values.x = *(input_ptr + offset);
offset += height_width_size;
output_values.y = *(input_ptr + offset);
offset += height_width_size;
output_values.z = *(input_ptr + offset);
offset += height_width_size;
output_values.w = *(input_ptr + offset);
} else if (remain_channel == 3) {
int offset = buffer_offset;
output_values.x = *(input_ptr + offset);
offset += height_width_size;
output_values.y = *(input_ptr + offset);
offset += height_width_size;
output_values.z = *(input_ptr + offset);
} else if (remain_channel == 2) {
int offset = buffer_offset;
output_values.x = *(input_ptr + offset);
offset += height_width_size;
output_values.y = *(input_ptr + offset);
} else if (remain_channel == 1) {
int offset = buffer_offset;
output_values.x = *(input_ptr + offset);
}
write_imagef(output, (int2)(image_width_idx, image_height_idx), output_values);
}
// only for debug
// convert data from image(b h, ic/4 w ic4) to buffer(nhwc)
__kernel void image_to_nhwc_buffer(GLOBAL_SIZE_2_DIMS __global float *output, /* nhwc */
__private const int height, __private const int width, __private const int channels,
__read_only image2d_t input_ptr) {
int image_width_idx = get_global_id(0);
int image_height_idx = get_global_id(1);
DEAL_NON_UNIFORM_DIM2(image_width_idx, image_height_idx);
const int batch_idx = image_height_idx / height;
const int height_idx = image_height_idx % height;
const int width_idx = image_width_idx % width;
const int channel_4_idx = (image_width_idx / width) << 2;
const int buffer_offset = ((batch_idx * height + height_idx) * width + width_idx) * channels + channel_4_idx;
int2 coord = (int2)(image_width_idx, image_height_idx);
float4 values = read_imagef(input_ptr, SAMPLER, coord);
const int remain_channel = channels - channel_4_idx;
if (remain_channel >= 4) {
vstore4(values, 0, output + buffer_offset);
} else if (remain_channel == 3) {
int offset = buffer_offset;
output[offset] = values.x;
offset++;
output[offset] = values.y;
offset++;
output[offset] = values.z;
} else if (remain_channel == 2) {
int offset = buffer_offset;
output[offset] = values.x;
offset++;
output[offset] = values.y;
} else if (remain_channel == 1) {
int offset = buffer_offset;
output[offset] = values.x;
}
}
// only for debug
// convert data from image(b h, ic/4 w ic4) to buffer(nhwc)
__kernel void image_to_nchw_buffer(GLOBAL_SIZE_2_DIMS __global float *output, /* nchw */
__private const int height, __private const int width, __private const int channels,
__read_only image2d_t input_ptr) {
int image_width_idx = get_global_id(0);
int image_height_idx = get_global_id(1);
DEAL_NON_UNIFORM_DIM2(image_width_idx, image_height_idx);
const int batch_idx = image_height_idx / height;
const int height_idx = image_height_idx % height;
const int width_idx = image_width_idx % width;
int channel_4_idx = (image_width_idx / width) * 4;
int buffer_offset = ((batch_idx * channels + channel_4_idx) * height + height_idx) * width + width_idx;
float4 values = read_imagef(input_ptr, SAMPLER, (int2)(image_width_idx, image_height_idx));
const int height_width_size = height * width;
const int remain_channel = channels - channel_4_idx;
if (remain_channel >= 4) {
int offset = buffer_offset;
output[offset] = values.x;
offset += height_width_size;
output[offset] = values.y;
offset += height_width_size;
output[offset] = values.z;
offset += height_width_size;
output[offset] = values.w;
} else if (remain_channel == 3) {
int offset = buffer_offset;
output[offset] = values.x;
offset += height_width_size;
output[offset] = values.y;
offset += height_width_size;
output[offset] = values.z;
} else if (remain_channel == 2) {
int offset = buffer_offset;
output[offset] = values.x;
offset += height_width_size;
output[offset] = values.y;
} else if (remain_channel == 1) {
int offset = buffer_offset;
output[offset] = values.x;
}
}
// convert arg as 4 alignment
__kernel void arg_buffer_to_image(GLOBAL_SIZE_2_DIMS __global const float *input_ptr, __private const int count,
__write_only image2d_t output) {
int image_width_idx = get_global_id(0);
int image_height_idx = get_global_id(1);
DEAL_NON_UNIFORM_DIM2(image_width_idx, image_height_idx);
const int buffer_4_offset = image_width_idx << 2;
const int remain = count - buffer_4_offset;
int offset = buffer_4_offset;
float4 values = 0;
if (remain >= 4) {
values = vload4(0, input_ptr + offset);
} else if (remain == 3) {
values.x = *(input_ptr + offset);
offset++;
values.y = *(input_ptr + offset);
offset++;
values.z = *(input_ptr + offset);
} else if (remain == 2) {
values.x = *(input_ptr + offset);
offset++;
values.y = *(input_ptr + offset);
} else if (remain == 1) {
values.x = *(input_ptr + offset);
}
write_imagef(output, (int2)(image_width_idx, image_height_idx), values);
}
// only for debug
__kernel void arg_image_to_buffer(GLOBAL_SIZE_2_DIMS __global float *output, __private const int count,
__read_only image2d_t input_ptr) {
int image_width_idx = get_global_id(0);
int image_height_idx = get_global_id(1);
DEAL_NON_UNIFORM_DIM2(image_width_idx, image_height_idx);
const int buffer_4_offset = image_width_idx << 2;
int2 coord = (int2)(image_width_idx, image_height_idx);
float4 values = read_imagef(input_ptr, SAMPLER, coord);
const int remain = count - buffer_4_offset;
if (remain < 4) {
switch (remain) {
case 3:
output[buffer_4_offset + 2] = values.s2;
case 2:
output[buffer_4_offset + 1] = values.s1;
case 1:
output[buffer_4_offset] = values.s0;
}
} else {
vstore4(values, 0, output + buffer_4_offset);
}
if (remain >= 4) {
vstore4(values, 0, output + buffer_4_offset);
} else if (remain == 3) {
int offset = buffer_4_offset;
output[offset] = values.x;
offset++;
output[offset] = values.y;
offset++;
output[offset] = values.z;
} else if (remain == 2) {
int offset = buffer_4_offset;
output[offset] = values.x;
offset++;
output[offset] = values.y;
} else if (remain == 1) {
int offset = buffer_4_offset;
output[offset] = values.x;
}
}