forked from mvitez/thnets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thapi.c
627 lines (592 loc) · 17.9 KB
/
thapi.c
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "thnets.h"
static int lasterror;
static short TB_YUR[256], TB_YUB[256], TB_YUGU[256], TB_YUGV[256], TB_Y[256];
static unsigned char TB_SAT[1024 + 1024 + 256];
int th_debug, th_profile;
#ifdef CUDNN
int cuda_maphostmem;
#endif
#define BYTE2FLOAT 0.003921568f // 1/255
static void rgb2float(float *dst, const unsigned char *src, int width, int height, int srcstride, const float *mean, const float *std)
{
int c, i, j;
float std1[3];
std1[0] = 1 / std[0];
std1[1] = 1 / std[1];
std1[2] = 1 / std[2];
#pragma omp parallel for private(c, i, j)
for(c = 0; c < 3; c++)
for(i = 0; i < height; i++)
for(j = 0; j < width; j++)
dst[j + (i + c * height) * width] = (src[c + 3*j + srcstride*i] * BYTE2FLOAT - mean[c]) * std1[c];
}
static void bgr2float(float *dst, const unsigned char *src, int width, int height, int srcstride, const float *mean, const float *std)
{
int c, i, j;
float std1[3];
std1[0] = 1 / std[0];
std1[1] = 1 / std[1];
std1[2] = 1 / std[2];
#pragma omp parallel for private(c, i, j)
for(c = 0; c < 3; c++)
for(i = 0; i < height; i++)
for(j = 0; j < width; j++)
dst[j + (i + c * height) * width] = (src[2-c + 3*j + srcstride*i] * BYTE2FLOAT - mean[c]) * std1[c];
}
static void init_yuv2rgb()
{
int i;
/* calculate lookup table for yuv420p */
for (i = 0; i < 256; i++) {
TB_YUR[i] = 459 * (i-128) / 256;
TB_YUB[i] = 541 * (i-128) / 256;
TB_YUGU[i] = -137 * (i-128) / 256;
TB_YUGV[i] = - 55 * (i-128) / 256;
TB_Y[i] = (i-16) * 298 / 256;
}
for (i = 0; i < 1024; i++) {
TB_SAT[i] = 0;
TB_SAT[i + 1024 + 256] = 255;
}
for (i = 0; i < 256; i++)
TB_SAT[i + 1024] = i;
}
static void yuyv2fRGB(const unsigned char *frame, float *dst_float, int imgstride, int rowstride, int w, int h, const float *mean, const float *std)
{
int i, j, w2 = w / 2, c;
float std0 = 1/std[0];
float std1 = 1/std[1];
float std2 = 1/std[2];
#pragma omp parallel for private(c, i, j)
for(c = 0; c < 3; c++)
{
float *dst;
const unsigned char *src;
if(c == 0)
{
/* convert for R channel */
src = frame;
for (i = 0; i < h; i++) {
dst = dst_float + i * rowstride;
for (j = 0; j < w2; j++) {
*dst++ = (TB_SAT[ TB_Y[ src[0] ] + TB_YUR[ src[3] ] + 1024] * BYTE2FLOAT - mean[0]) * std0;
*dst++ = (TB_SAT[ TB_Y[ src[2] ] + TB_YUR[ src[3] ] + 1024] * BYTE2FLOAT - mean[0]) * std0;
src += 4;
}
}
} else if(c == 1)
{
/* convert for G channel */
src = frame;
for (i = 0; i < h; i++) {
dst = dst_float + i * rowstride + imgstride;
for (j = 0; j < w2; j++) {
*dst++ = (TB_SAT[ TB_Y[ src[0] ] + TB_YUGU[ src[1] ] + TB_YUGV[ src[3] ] + 1024] * BYTE2FLOAT - mean[1]) * std1;
*dst++ = (TB_SAT[ TB_Y[ src[2] ] + TB_YUGU[ src[1] ] + TB_YUGV[ src[3] ] + 1024] * BYTE2FLOAT - mean[1]) * std1;
src += 4;
}
}
} else if(c == 2)
{
/* convert for B channel */
src = frame;
for (i = 0; i < h; i++) {
dst = dst_float + i * rowstride + 2*imgstride;
for (j = 0; j < w2; j++) {
*dst++ = (TB_SAT[ TB_Y[ src[0] ] + TB_YUB[ src[1] ] + 1024] * BYTE2FLOAT - mean[2]) * std2;
*dst++ = (TB_SAT[ TB_Y[ src[2] ] + TB_YUB[ src[1] ] + 1024] * BYTE2FLOAT - mean[2]) * std2;
src += 4;
}
}
}
}
}
double th_seconds()
{
static double s;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
if(!s)
s = ts.tv_sec + ts.tv_nsec * 1e-9;
return ts.tv_sec + ts.tv_nsec * 1e-9 - s;
}
THFloatTensor *forward(struct network *net, THFloatTensor *in)
{
int i;
double t = 0, convtot = 0, convflops = 0;
#ifdef OPENCL
if(net->opencl == 1)
OpenCL_Build(net, in);
#endif
for(i = 0; i < net->nelem; i++)
{
if(th_profile)
t = th_seconds();
in = net->modules[i].updateOutput(&net->modules[i], in);
// You can remove these lines if you don't have problems with memory
// These lines free intermediate results
if(i > 0)
{
THFloatTensor_free(net->modules[i-1].output);
net->modules[i-1].output = THFloatTensor_new();
}
if(th_profile)
{
#ifdef OPENCL
if(net->opencl)
clFinish(cl_queue);
#endif
t = th_seconds() - t;
if(net->modules[i].type == MT_SpatialConvolutionMM ||
net->modules[i].type == MT_SpatialConvolutionVirtMM ||
net->modules[i].type == MT_SpatialConvolution)
{
double flops = 2.0 * THFloatTensor_nElement(in) * net->modules[i].SpatialConvolution.nInputPlane *
net->modules[i].SpatialConvolution.kW * net->modules[i].SpatialConvolution.kH;
printf("%f seconds for module %d, %f Gflops/s\n", t, i+1, flops * 1e-9 / t);
convtot += t;
convflops += flops;
} else printf("%f seconds for module %d\n", t, i+1);
}
if(th_debug > 1)
printf("%d) %d %d %ld %ld %ld %ld\n", i+1, net->modules[i].type, in->nDimension, in->size[0], in->size[1], in->size[2], in->size[3]);
}
if(th_profile)
printf("%f seconds for convolutions %f Gflops/s\n", convtot, convflops * 1e-9 / convtot);
return in;
}
THNETWORK *THLoadNetwork(const char *path)
{
char tmppath[255];
int i, longsize = 8;
THNETWORK *net = calloc(1, sizeof(*net));
sprintf(tmppath, "%s/model.net", path);
net->netobj = malloc(sizeof(*net->netobj));
lasterror = loadtorch(tmppath, net->netobj, longsize);
if(lasterror == ERR_CORRUPTED)
lasterror = loadtorch(tmppath, net->netobj, longsize = 4);
if(lasterror)
{
free(net->netobj);
free(net);
return 0;
}
if(th_debug)
printobject(net->netobj, 0);
net->net = Object2Network(net->netobj);
if(!net->net)
{
lasterror = ERR_WRONGOBJECT;
freeobject(net->netobj);
free(net->netobj);
free(net);
return 0;
}
net->std[0] = net->std[1] = net->std[2] = 1;
net->mean[0] = net->mean[1] = net->mean[2] = 0;
sprintf(tmppath, "%s/stat.t7", path);
net->statobj = malloc(sizeof(*net->statobj));
lasterror = loadtorch(tmppath, net->statobj, longsize);
if(!lasterror)
{
if(net->statobj->type != TYPE_TABLE || net->statobj->table->nelem != 2)
{
lasterror = ERR_WRONGOBJECT;
freenetwork(net->net);
freeobject(net->netobj);
free(net->netobj);
freeobject(net->statobj);
free(net->statobj);
free(net);
return 0;
}
for(i = 0; i < net->statobj->table->nelem; i++)
if(net->statobj->table->records[i].name.type == TYPE_STRING)
{
if(!strcmp(net->statobj->table->records[i].name.string.data, "mean"))
memcpy(net->mean, net->statobj->table->records[i].value.tensor->storage->data, sizeof(net->mean));
else if(!strcmp(net->statobj->table->records[i].name.string.data, "std"))
memcpy(net->std, net->statobj->table->records[i].value.tensor->storage->data, sizeof(net->std));
}
} else {
free(net->statobj);
net->statobj = 0;
}
THUseSpatialConvolutionMM(net, 2);
return net;
}
void THInit()
{
static int init;
if(init)
return;
init_yuv2rgb();
#ifndef USEBLAS
blas_init();
#endif
init = 1;
#if defined CUDNN && defined USECUDAHOSTALLOC
// cuda_maphostmem = 1 requires that memory was allocated with cudaHostAlloc
// cuda_maphostmem = 2 will work with malloc, but Tegra TX1 does not support cudaHostRegister with cudaHostRegisterMapped
struct cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, 0);
if(prop.canMapHostMemory)
{
errcheck(cudaSetDeviceFlags(cudaDeviceMapHost));
cuda_maphostmem = 1;
}
#endif
#ifdef OPENCL
thopencl_init();
#endif
}
int THProcessFloat(THNETWORK *network, float *data, int batchsize, int width, int height, float **result, int *outwidth, int *outheight)
{
int b, c, i;
THFloatTensor *t = THFloatTensor_new();
THFloatTensor *out;
t->nDimension = 4;
t->size[0] = batchsize;
t->size[1] = 3;
t->size[2] = height;
t->size[3] = width;
t->stride[0] = 3 * width * height;
t->stride[1] = width * height;
t->stride[2] = width;
t->stride[3] = 1;
t->storage = THFloatStorage_newwithbuffer((float *)data);
#pragma omp parallel for private(b, c, i)
for(b = 0; b < batchsize; b++)
for(c = 0; c < 3; c++)
for(i = 0; i < width*height; i++)
data[b * t->stride[0] + c * t->stride[1] + i] =
(data[b * t->stride[0] + c * t->stride[1] + i] - network->mean[c]) / network->std[c];
#ifdef CUDNN
if(network->net->cuda)
{
THFloatTensor *t2 = THCudaTensor_newFromFloatTensor(t);
out = forward(network->net, t2);
THFloatTensor_free(t2);
if(network->out)
THFloatTensor_free(network->out);
network->out = THFloatTensor_newFromCudaTensor(out);
out = network->out;
} else
#endif
#ifdef OPENCL
if(network->net->opencl)
{
THFloatTensor *t2 = THOpenCLTensor_newFromImageTensor(t);
out = forward(network->net, t2);
THFloatTensor_free(t2);
if(network->out)
THFloatTensor_free(network->out);
network->out = THFloatTensor_newFromOpenCLImageTensor(out);
out = network->out;
} else
#endif
out = forward(network->net, t);
THFloatTensor_free(t);
*result = out->storage->data;
if(out->nDimension >= 3)
{
*outwidth = out->size[out->nDimension - 1];
*outheight = out->size[out->nDimension - 2];
} else *outwidth = *outheight = 1;
return THFloatTensor_nElement(out);
}
int THProcessImages(THNETWORK *network, unsigned char **images, int batchsize, int width, int height, int stride, float **results, int *outwidth, int *outheight, int bgr)
{
int i;
THFloatTensor *out, *t = 0;
THFloatStorage *st;
#ifdef CUDNN
if(network->net->cuda)
{
#ifdef HAVEFP16
if(floattype == CUDNN_DATA_HALF)
{
st = THCudaStorage_new(batchsize * (width * height * 3));
for(i = 0; i < batchsize; i++)
cuda_rgb2half((unsigned short *)st->data + i * (width * height * 3), images[i], width, height, stride, network->mean, network->std, bgr);
} else
#endif
{
st = THCudaStorage_new(batchsize * width * height * 3);
for(i = 0; i < batchsize; i++)
cuda_rgb2float(st->data + i * width * height * 3, images[i], width, height, stride, network->mean, network->std, bgr);
}
} else
#endif
#ifdef OPENCL
if(network->net->opencl)
t = OpenCL_LoadImage(images[0], width, height, stride, network->mean, network->std, bgr);
else
#endif
{
st = THFloatStorage_new(batchsize * width * height * 3);
if(bgr)
#pragma omp parallel for if(batchsize>1) private(i)
for(i = 0; i < batchsize; i++)
bgr2float(st->data + i * width * height * 3, images[i], width, height, stride, network->mean, network->std);
else
#pragma omp parallel for if(batchsize>1) private(i)
for(i = 0; i < batchsize; i++)
rgb2float(st->data + i * width * height * 3, images[i], width, height, stride, network->mean, network->std);
}
if(!t)
{
t = THFloatTensor_new();
t->storage = st;
if(batchsize == 1)
{
t->nDimension = 3;
t->size[0] = 3;
t->size[1] = height;
t->size[2] = width;
t->stride[0] = width * height;
t->stride[1] = width;
t->stride[2] = 1;
} else {
t->nDimension = 4;
t->size[0] = batchsize;
t->size[1] = 3;
t->size[2] = height;
t->size[3] = width;
t->stride[0] = 3 * width * height;
t->stride[1] = width * height;
t->stride[2] = width;
t->stride[3] = 1;
}
}
#ifdef CUDNN
if(network->net->cuda)
{
out = forward(network->net, t);
if(network->out)
THFloatTensor_free(network->out);
#ifdef HAVEFP16
if(floattype == CUDNN_DATA_HALF)
network->out = THFloatTensor_newFromHalfCudaTensor(out);
else
#endif
network->out = THFloatTensor_newFromCudaTensor(out);
out = network->out;
} else
#endif
#ifdef OPENCL
if(network->net->opencl)
{
out = forward(network->net, t);
if(network->out)
THFloatTensor_free(network->out);
#ifdef HAVEFP16
if(cl_datasize == 2)
network->out = THFloatTensor_newFromHalfOpenCLImageTensor(out);
else
#endif
network->out = THFloatTensor_newFromOpenCLImageTensor(out);
out = network->out;
} else
#endif
out = forward(network->net, t);
THFloatTensor_free(t);
*results = out->storage->data;
if(out->nDimension >= 3)
{
*outwidth = out->size[out->nDimension - 1];
*outheight = out->size[out->nDimension - 2];
} else *outwidth = *outheight = 1;
return THFloatTensor_nElement(out);
}
int THProcessYUYV(THNETWORK *network, unsigned char *image, int width, int height, float **results, int *outwidth, int *outheight)
{
THFloatTensor *out;
THFloatStorage *st;
#ifdef CUDNN
if(network->net->cuda)
THError("This function is not supported with CUDNN");
#endif
#ifdef OPENCL
if(network->net->cuda)
THError("This function is not supported with OpenCL");
#endif
st = THFloatStorage_new(width * height * 3);
yuyv2fRGB(image, st->data, width*height, width, width, height, network->mean, network->std);
THFloatTensor *t = THFloatTensor_new();
t->storage = st;
t->nDimension = 3;
t->size[0] = 3;
t->size[1] = height;
t->size[2] = width;
t->stride[0] = width * height;
t->stride[1] = width;
t->stride[2] = 1;
out = forward(network->net, t);
THFloatTensor_free(t);
*results = out->storage->data;
if(out->nDimension >= 3)
{
*outwidth = out->size[out->nDimension - 1];
*outheight = out->size[out->nDimension - 2];
} else *outwidth = *outheight = 1;
return THFloatTensor_nElement(out);
}
void THFreeNetwork(THNETWORK *network)
{
freenetwork(network->net);
if(network->netobj)
{
freeobject(network->netobj);
free(network->netobj);
}
if(network->statobj)
{
freeobject(network->statobj);
free(network->statobj);
}
if(network->out)
THFloatTensor_free(network->out);
free(network);
}
int THLastError()
{
return lasterror;
}
void THMakeSpatial(THNETWORK *network)
{
int i, size = 231, nInputPlane = 3;
for(i = 0; i < network->net->nelem; i++)
{
if(network->net->modules[i].type == MT_View || network->net->modules[i].type == MT_Reshape)
{
THFloatTensor_free(network->net->modules[i].output);
memmove(network->net->modules+i, network->net->modules+i+1, sizeof(*network->net->modules) * (network->net->nelem - i - 1));
network->net->nelem--;
i--;
} else if(network->net->modules[i].type == MT_Linear)
{
THFloatTensor_free(network->net->modules[i].Linear.addBuffer);
network->net->modules[i].updateOutput = nn_SpatialConvolutionMM_updateOutput;
#ifndef USEBLAS
network->net->modules[i].type = MT_SpatialConvolutionVirtMM;
#else
network->net->modules[i].type = MT_SpatialConvolutionMM;
#endif
struct SpatialConvolution *c = &network->net->modules[i].SpatialConvolution;
c->finput = THFloatTensor_new();
c->padW = c->padH = 0;
c->dW = c->dH = 1;
c->kW = c->kH = size;
c->nInputPlane = nInputPlane;
nInputPlane = c->nOutputPlane = c->weight->size[0];
size = (size + 2*c->padW - c->kW) / c->dW + 1;
} else if(network->net->modules[i].type == MT_SpatialConvolution ||
network->net->modules[i].type == MT_SpatialConvolutionMM ||
network->net->modules[i].type == MT_SpatialConvolutionVirtMM)
{
struct SpatialConvolution *c = &network->net->modules[i].SpatialConvolution;
size = (size + 2*c->padW - c->kW) / c->dW + 1;
nInputPlane = network->net->modules[i].SpatialConvolution.nOutputPlane;
} else if(network->net->modules[i].type == MT_SpatialMaxPooling)
{
struct SpatialMaxPooling *c = &network->net->modules[i].SpatialMaxPooling;
if(c->ceil_mode)
size = (long)(ceil((float)(size - c->kH + 2*c->padH) / c->dH)) + 1;
else size = (long)(floor((float)(size - c->kH + 2*c->padH) / c->dH)) + 1;
} else if(network->net->modules[i].type == MT_SpatialZeroPadding)
{
struct SpatialZeroPadding *c = &network->net->modules[i].SpatialZeroPadding;
size += c->pad_l + c->pad_r;
}
}
}
int THUseSpatialConvolutionMM(THNETWORK *network, int mm_type)
{
int i;
int rc = 0;
for(i = 0; i < network->net->nelem; i++)
{
if(mm_type && network->net->modules[i].type == MT_SpatialConvolution)
{
struct SpatialConvolution *c = &network->net->modules[i].SpatialConvolution;
network->net->modules[i].type = MT_SpatialConvolutionMM;
network->net->modules[i].updateOutput = nn_SpatialConvolutionMM_updateOutput;
THFloatTensor_resize2d(c->weight, c->nOutputPlane, c->nInputPlane * c->kH * c->kW);
} else if(!mm_type && (network->net->modules[i].type == MT_SpatialConvolutionMM ||
network->net->modules[i].type == MT_SpatialConvolutionVirtMM))
{
struct SpatialConvolution *c = &network->net->modules[i].SpatialConvolution;
if(c->padW || c->padH)
{
rc = ERR_NOTIMPLEMENTED;
continue;
}
network->net->modules[i].type = MT_SpatialConvolution;
network->net->modules[i].updateOutput = nn_SpatialConvolution_updateOutput;
THFloatTensor_resize4d(c->weight, c->nOutputPlane, c->nInputPlane, c->kH, c->kW);
}
#ifndef USEBLAS
if(mm_type == 2 && network->net->modules[i].type == MT_SpatialConvolutionMM)
network->net->modules[i].type = MT_SpatialConvolutionVirtMM;
else if(mm_type == 1 && network->net->modules[i].type == MT_SpatialConvolutionVirtMM)
network->net->modules[i].type = MT_SpatialConvolutionMM;
#endif
}
return rc;
}
THNETWORK *THCreateCudaNetwork(THNETWORK *net)
{
#ifdef CUDNN
THNETWORK *nn = malloc(sizeof(*nn));
memcpy(nn, net, sizeof(*nn));
nn->netobj = 0;
nn->statobj = 0;
nn->net = THcudnn_ToCUDNN(net->net);
return nn;
#else
return 0;
#endif
}
int THCudaHalfFloat(int enable)
{
#if defined CUDNN && defined HAVEFP16
if(enable)
{
floattype = CUDNN_DATA_HALF;
} else floattype = CUDNN_DATA_FLOAT;
return 0;
#else
return ERR_NOTIMPLEMENTED;
#endif
}
int THOpenCLHalfFloat(int enable)
{
#if defined OPENCL && defined HAVEFP16
if(enable)
{
cl_datasize = 2;
} else cl_datasize = 4;
return 0;
#else
return ERR_NOTIMPLEMENTED;
#endif
}
THNETWORK *THCreateOpenCLNetwork(THNETWORK *net)
{
#ifdef OPENCL
THNETWORK *nn = malloc(sizeof(*nn));
memcpy(nn, net, sizeof(*nn));
nn->netobj = 0;
nn->statobj = 0;
nn->net = THOpenCL_ToOpenCL(net->net);
return nn;
#else
return 0;
#endif
}