tensor_tools.cpp 11.6 KB
Newer Older
1
2
3
4
5
6
7
// Copyright (C) 2015  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_TeNSOR_TOOLS_CPP_
#define DLIB_TeNSOR_TOOLS_CPP_

#include "tensor_tools.h"
#include "cpu_dlib.h"
Davis King's avatar
Davis King committed
8
#include "cuda_dlib.h"
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

namespace dlib { namespace tt
{

// ----------------------------------------------------------------------------------------

    void gemm (
        float beta,
        tensor& dest,
        float alpha,
        const tensor& lhs,
        bool trans_lhs,
        const tensor& rhs,
        bool trans_rhs
    )
    {
#ifdef DLIB_USE_CUDA
        cuda::gemm(beta, dest, alpha, lhs, trans_lhs, rhs, trans_rhs);
#else
        if (trans_lhs && trans_rhs)
            dest = alpha*trans(mat(lhs))*trans(mat(rhs)) + beta*mat(dest);
        if (!trans_lhs && trans_rhs)
            dest = alpha*mat(lhs)*trans(mat(rhs)) + beta*mat(dest);
        if (trans_lhs && !trans_rhs)
            dest = alpha*trans(mat(lhs))*mat(rhs) + beta*mat(dest);
        else
            dest = alpha*mat(lhs)*mat(rhs) + beta*mat(dest);
#endif
    }

// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------

    tensor_rand::
    tensor_rand(
        unsigned long long seed
    )
    {
        // TODO
    }

    void tensor_rand::
    fill_gaussian (
        tensor& data,
        float mean,
        float stddev
    )
    {
        DLIB_CASSERT(data.size()%2 == 0,"");
#ifdef DLIB_USE_CUDA
Davis King's avatar
Davis King committed
59
        rnd.fill_gaussian(data, mean, stddev);
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
#else
        for (auto& x : data) 
            x = rnd.get_random_gaussian()*stddev + mean;
#endif
    }

    void tensor_rand::
    fill_uniform (
        tensor& data
    )
    {
#ifdef DLIB_USE_CUDA
        rnd.fill_uniform(data);
#else
        for (auto& x : data) 
            x = rnd.get_random_float();
#endif
    }

// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------

    void multiply (
        tensor& dest,
        const tensor& src
    )
    {
        DLIB_CASSERT(have_same_dimensions(dest,src) == true,"");
#ifdef DLIB_USE_CUDA
89
        //cuda::multiply(dest, src);
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#else
        cpu::multiply(dest, src);
#endif

    }

// ----------------------------------------------------------------------------------------

    void affine_transform(
        resizable_tensor& dest,
        const tensor& src,
        const float A,
        const float B
    )
    {
#ifdef DLIB_USE_CUDA
106
        //cuda::affine_transform(dest,src,A,B);
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#else
        cpu::affine_transform(dest,src,A,B);
#endif
    }

// ----------------------------------------------------------------------------------------

    void affine_transform(
        resizable_tensor& dest,
        const tensor& src,
        const tensor& A,
        const tensor& B
    )
    {
#ifdef DLIB_USE_CUDA
122
        //cuda::affine_transform(dest,src,A,B);
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#else
        cpu::affine_transform(dest,src,A,B);
#endif
    }

// ----------------------------------------------------------------------------------------

    void batch_normalize (
        resizable_tensor& dest,
        resizable_tensor& means,
        resizable_tensor& vars,
        const tensor& src,
        const tensor& gamma, 
        const tensor& beta 
    )
    {
#ifdef DLIB_USE_CUDA
140
        //cuda::batch_normalize(dest,means,vars,src,gamma,beta);
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#else
        cpu::batch_normalize(dest,means,vars,src,gamma,beta);
#endif
    }

// ----------------------------------------------------------------------------------------

    void batch_normalize_gradient (
        const tensor& gradient_input,
        const tensor& means,
        const tensor& vars,
        const tensor& src,
        const tensor& gamma,
        tensor& src_grad,
        tensor& gamma_grad, 
        tensor& beta_grad 
    )
    {
#ifdef DLIB_USE_CUDA
160
        /*
161
162
        cuda::batch_normalize_gradient(gradient_input,means,vars,src,gamma,
                                       src_grad,gamma_grad,beta_grad);
163
        */
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#else
        cpu::batch_normalize_gradient(gradient_input,means,vars,src,gamma,
                                       src_grad,gamma_grad,beta_grad);
#endif
    }

// ----------------------------------------------------------------------------------------

    void batch_normalize_conv (
        resizable_tensor& dest,
        resizable_tensor& means,
        resizable_tensor& vars,
        const tensor& src,
        const tensor& gamma, 
        const tensor& beta 
    )
    {
#ifdef DLIB_USE_CUDA
182
        //cuda::batch_normalize_conv(dest,means,vars,src,gamma,beta);
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#else
        cpu::batch_normalize_conv(dest,means,vars,src,gamma,beta);
#endif
    }

// ----------------------------------------------------------------------------------------

    void batch_normalize_conv_gradient (
        const tensor& gradient_input,
        const tensor& means,
        const tensor& vars,
        const tensor& src,
        const tensor& gamma,
        tensor& src_grad,
        tensor& gamma_grad, 
        tensor& beta_grad 
    )
    {
#ifdef DLIB_USE_CUDA
202
        /*
203
204
        cuda::batch_normalize_conv_gradient(gradient_input,means,vars,src,gamma,
                                       src_grad,gamma_grad,beta_grad);
205
        */
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#else
        cpu::batch_normalize_conv_gradient(gradient_input,means,vars,src,gamma,
                                       src_grad,gamma_grad,beta_grad);
#endif
    }

// ----------------------------------------------------------------------------------------

    void threshold (
        tensor& data,
        float thresh
    )
    {
#ifdef DLIB_USE_CUDA
220
        //cuda::threshold(data,thresh);
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
#else
        cpu::threshold(data,thresh);
#endif
    }

// ----------------------------------------------------------------------------------------

    void add(
        float beta,
        tensor& dest,
        float alpha,
        const tensor& src
    )
    {
#ifdef DLIB_USE_CUDA
        cuda::add(beta,dest,alpha,src);
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

// ----------------------------------------------------------------------------------------

    void add_conv_bias_gradient (
        tensor& grad,
        const tensor& gradient_input
    )
    {
#ifdef DLIB_USE_CUDA
        cuda::add_conv_bias_gradient(grad,gradient_input);
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------

    tensor_conv::
    tensor_conv()
    {
    }

    void tensor_conv::
    clear(
    )
    {
#ifdef DLIB_USE_CUDA
        impl.clear();
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

    void tensor_conv::
    setup(
        const tensor& data,
        const tensor& filters,
        int stride_y,
        int stride_x
    )
    {
#ifdef DLIB_USE_CUDA
        impl.setup(data, filters, stride_y, stride_x);
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

    void tensor_conv::
    operator() (
        resizable_tensor& output,
        const tensor& data,
        const tensor& filters
    )
    {
#ifdef DLIB_USE_CUDA
        impl(output, data, filters);
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

    void tensor_conv::
    get_gradient_for_data (
        const tensor& gradient_input, 
        const tensor& filters,
        tensor& data_gradient
    )
    {
#ifdef DLIB_USE_CUDA
        impl.get_gradient_for_data(gradient_input, filters, data_gradient);
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

    void tensor_conv::
    get_gradient_for_filters (
        const tensor& gradient_input, 
        const tensor& data,
        tensor& filters_gradient
    )
    {
#ifdef DLIB_USE_CUDA
        impl.get_gradient_for_filters(gradient_input, data, filters_gradient);
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------

    max_pool::
    max_pool (
    )
    {
    }

    void max_pool::
    clear(
    )
    {
#ifdef DLIB_USE_CUDA
        impl.clear();
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

    void max_pool::
    setup(
        int window_height,
        int window_width,
        int stride_y,
        int stride_x
    )
    {
#ifdef DLIB_USE_CUDA
        impl.setup(window_height, window_width, stride_y, stride_x);
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

    void max_pool::
    operator() (
        resizable_tensor& dest,
        const tensor& src
    )
    {
#ifdef DLIB_USE_CUDA
        impl(dest, src);
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

    void max_pool::
    get_gradient(
        const tensor& gradient_input, 
        const tensor& dest,
        const tensor& src,
        tensor& grad 
    )
    {
#ifdef DLIB_USE_CUDA
        impl.get_gradient(gradient_input, dest, src, grad);
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------

    void softmax (
        resizable_tensor& dest,
        const tensor& src
    )
    {
#ifdef DLIB_USE_CUDA
        cuda::softmax(dest,src);
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

    void softmax_gradient (
        tensor& grad,
424
        const tensor& dest,
425
426
427
428
        const tensor& gradient_input
    )
    {
#ifdef DLIB_USE_CUDA
429
        cuda::softmax_gradient(grad, dest, gradient_input);
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
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

// ----------------------------------------------------------------------------------------

    void sigmoid (
        resizable_tensor& dest,
        const tensor& src
    )
    {
#ifdef DLIB_USE_CUDA
        cuda::sigmoid(dest,src);
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

    void sigmoid_gradient (
        tensor& grad,
        const tensor& dest,
        const tensor& gradient_input
    )
    {
#ifdef DLIB_USE_CUDA
458
        cuda::sigmoid_gradient(grad, dest, gradient_input);
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
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

// ----------------------------------------------------------------------------------------

    void relu (
        resizable_tensor& dest,
        const tensor& src
    )
    {
#ifdef DLIB_USE_CUDA
        cuda::relu(dest,src);
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

    void relu_gradient (
        tensor& grad,
        const tensor& dest,
        const tensor& gradient_input
    )
    {
#ifdef DLIB_USE_CUDA
487
        cuda::relu_gradient(grad, dest, gradient_input);
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
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

// ----------------------------------------------------------------------------------------

    void tanh (
        resizable_tensor& dest,
        const tensor& src
    )
    {
#ifdef DLIB_USE_CUDA
        cuda::tanh(dest,src);
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

    void tanh_gradient (
        tensor& grad,
        const tensor& dest,
        const tensor& gradient_input
    )
    {
#ifdef DLIB_USE_CUDA
516
        cuda::tanh_gradient(grad, dest, gradient_input);
517
518
519
520
521
522
523
524
525
526
527
528
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

// ----------------------------------------------------------------------------------------

}}

#endif // DLIB_TeNSOR_TOOLS_CPP_