"vscode:/vscode.git/clone" did not exist on "f2167cb6bbb773f5b51f526dda69fbc0da01dcee"
tensor_tools.cpp 15 KB
Newer Older
1
2
3
4
5
6
// 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"
Davis King's avatar
Davis King committed
7
#include "../string.h"
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

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
27
28
29
30
31
32
33
34
35
36
37
        if (beta != 0)
        {
            if (trans_lhs && trans_rhs)
                dest = alpha*trans(mat(lhs))*trans(mat(rhs)) + beta*mat(dest);
            else if (!trans_lhs && trans_rhs)
                dest = alpha*mat(lhs)*trans(mat(rhs)) + beta*mat(dest);
            else 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);
        }
38
        else
39
40
41
42
43
44
45
46
47
48
        {
            if (trans_lhs && trans_rhs)
                dest = alpha*trans(mat(lhs))*trans(mat(rhs));
            else if (!trans_lhs && trans_rhs)
                dest = alpha*mat(lhs)*trans(mat(rhs));
            else if (trans_lhs && !trans_rhs)
                dest = alpha*trans(mat(lhs))*mat(rhs);
            else
                dest = alpha*mat(lhs)*mat(rhs);
        }
49
50
51
52
53
54
55
56
57
#endif
    }

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

    tensor_rand::
    tensor_rand(
        unsigned long long seed
Davis King's avatar
Davis King committed
58
59
60
61
62
63
    ) 
#ifdef DLIB_USE_CUDA
    :rnd(seed){}
#else
    {rnd.set_seed(cast_to_string(seed)); }
#endif
64
65
66
67
68
69
70
71
72
73

    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
74
        rnd.fill_gaussian(data, mean, stddev);
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#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
    }

// ----------------------------------------------------------------------------------------
95
96
97
98
99
100
101
102
// ----------------------------------------------------------------------------------------

    void multiply (
        tensor& dest,
        const tensor& src1,
        const tensor& src2
    )
    {
103
104
105
106
107
108
109
        DLIB_CASSERT(dest.k() == src1.k() && src1.k() == src2.k() &&
            dest.nr() == src1.nr() && src1.nr() == src2.nr() &&
            dest.nc() == src1.nc() && src1.nc() == src2.nc() ,"");
        const long MD = std::max(std::max(dest.num_samples(),src1.num_samples()),src2.num_samples());
        DLIB_CASSERT((dest.num_samples()==1 || dest.num_samples()==MD) &&
                    (src1.num_samples()==1 || src1.num_samples()==MD) &&
                    (src2.num_samples()==1 || src2.num_samples()==MD) ,"");
110
111
112
113
114
115
116
117
#ifdef DLIB_USE_CUDA
        cuda::multiply(dest, src1, src2);
#else
        cpu::multiply(dest, src1, src2);
#endif

    }

118
119
120
// ----------------------------------------------------------------------------------------

    void affine_transform(
121
        tensor& dest,
122
123
124
125
126
127
        const tensor& src,
        const float A,
        const float B
    )
    {
#ifdef DLIB_USE_CUDA
128
        cuda::affine_transform(dest,src,A,B);
129
130
131
132
133
#else
        cpu::affine_transform(dest,src,A,B);
#endif
    }

134
135
136
137
138
139
140
141
142
143
    void affine_transform(
        tensor& dest,
        const tensor& src1,
        const tensor& src2,
        const float A,
        const float B,
        const float C
    )
    {
#ifdef DLIB_USE_CUDA
144
        cuda::affine_transform(dest,src1,src2,A,B,C);
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#else
        cpu::affine_transform(dest,src1,src2,A,B,C);
#endif
    }

    void affine_transform(
        tensor& dest,
        const tensor& src1,
        const tensor& src2,
        const tensor& src3,
        const float A,
        const float B,
        const float C,
        const float D
    )
    {
#ifdef DLIB_USE_CUDA
162
        cuda::affine_transform(dest,src1,src2,src3,A,B,C,D);
163
164
165
166
167
#else
        cpu::affine_transform(dest,src1,src2,src3,A,B,C,D);
#endif
    }

168
169
170
// ----------------------------------------------------------------------------------------

    void affine_transform(
171
        tensor& dest,
172
173
174
175
176
177
        const tensor& src,
        const tensor& A,
        const tensor& B
    )
    {
#ifdef DLIB_USE_CUDA
178
        cuda::affine_transform(dest,src,A,B);
179
180
181
182
183
184
185
#else
        cpu::affine_transform(dest,src,A,B);
#endif
    }

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

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
    void batch_normalize_inference (
        resizable_tensor& dest,
        const tensor& src,
        const tensor& gamma, 
        const tensor& beta,
        const tensor& running_means,
        const tensor& running_invstds
    )
    {
#ifdef DLIB_USE_CUDA
        cuda::batch_normalize_inference(dest,src,gamma,beta,running_means,running_invstds);
#else
        cpu::batch_normalize_inference(dest,src,gamma,beta,running_means,running_invstds);
#endif
    }

202
203
204
205
    void batch_normalize (
        resizable_tensor& dest,
        resizable_tensor& means,
        resizable_tensor& vars,
206
207
208
        const double averaging_factor,
        resizable_tensor& running_means,
        resizable_tensor& running_invstds,
209
210
211
212
213
214
        const tensor& src,
        const tensor& gamma, 
        const tensor& beta 
    )
    {
#ifdef DLIB_USE_CUDA
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
        cuda::batch_normalize(dest,means,vars,averaging_factor,running_means,running_invstds,src,gamma,beta);
#else
        cpu::batch_normalize(dest,means,vars,averaging_factor,running_means,running_invstds,src,gamma,beta);
#endif
    }

    void batch_normalize_gradient (
            const tensor& gradient_input,
            const tensor& means,
            const tensor& invstds,
            const tensor& src,
            const tensor& gamma,
            tensor& src_grad,
            tensor& gamma_grad, 
            tensor& beta_grad 
    )
    {
             
#ifdef DLIB_USE_CUDA
        cuda::batch_normalize_gradient(gradient_input, means, invstds, src, gamma, src_grad, gamma_grad, beta_grad);
235
#else
236
        cpu::batch_normalize_gradient(gradient_input, means, invstds, src, gamma, src_grad, gamma_grad, beta_grad);
237
238
239
240
241
#endif
    }

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

242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    void batch_normalize_conv_inference (
        resizable_tensor& dest,
        const tensor& src,
        const tensor& gamma, 
        const tensor& beta,
        const tensor& running_means,
        const tensor& running_invstds
    )
    {
#ifdef DLIB_USE_CUDA
        cuda::batch_normalize_conv_inference(dest,src,gamma,beta,running_means,running_invstds);
#else
        cpu::batch_normalize_conv_inference(dest,src,gamma,beta,running_means,running_invstds);
#endif
    }

258
259
260
261
    void batch_normalize_conv (
        resizable_tensor& dest,
        resizable_tensor& means,
        resizable_tensor& vars,
262
263
264
        const double averaging_factor,
        resizable_tensor& running_means,
        resizable_tensor& running_invstds,
265
266
267
268
269
270
        const tensor& src,
        const tensor& gamma, 
        const tensor& beta 
    )
    {
#ifdef DLIB_USE_CUDA
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
        cuda::batch_normalize_conv(dest,means,vars,averaging_factor,running_means,running_invstds,src,gamma,beta);
#else
        cpu::batch_normalize_conv(dest,means,vars,averaging_factor,running_means,running_invstds,src,gamma,beta);
#endif
    }

    void batch_normalize_conv_gradient (
            const tensor& gradient_input,
            const tensor& means,
            const tensor& invstds,
            const tensor& src,
            const tensor& gamma,
            tensor& src_grad,
            tensor& gamma_grad, 
            tensor& beta_grad 
    )
    {
             
#ifdef DLIB_USE_CUDA
        cuda::batch_normalize_conv_gradient(gradient_input, means, invstds, src, gamma, src_grad, gamma_grad, beta_grad);
291
#else
292
        cpu::batch_normalize_conv_gradient(gradient_input, means, invstds, src, gamma, src_grad, gamma_grad, beta_grad);
293
294
295
296
297
298
299
300
301
302
303
#endif
    }

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

    void threshold (
        tensor& data,
        float thresh
    )
    {
#ifdef DLIB_USE_CUDA
304
        cuda::threshold(data,thresh);
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#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
322
        cpu::add(beta,dest,alpha,src);
323
324
325
326
327
#endif
    }

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

328
    void assign_conv_bias_gradient (
329
330
331
332
333
        tensor& grad,
        const tensor& gradient_input
    )
    {
#ifdef DLIB_USE_CUDA
334
        cuda::assign_conv_bias_gradient(grad,gradient_input);
335
336
337
338
339
340
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

341
342
// ----------------------------------------------------------------------------------------

343
    void assign_bias_gradient (
344
345
346
347
348
        tensor& grad,
        const tensor& gradient_input
    )
    {
#ifdef DLIB_USE_CUDA
349
        cuda::assign_bias_gradient(grad,gradient_input);
350
#else
351
        cpu::assign_bias_gradient(grad,gradient_input);
352
353
354
#endif
    }

355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------

    tensor_conv::
    tensor_conv()
    {
    }

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

    void tensor_conv::
376
377
    operator() (
        resizable_tensor& output,
378
379
380
381
382
383
384
        const tensor& data,
        const tensor& filters,
        int stride_y,
        int stride_x
    )
    {
#ifdef DLIB_USE_CUDA
385
        impl(output, data, filters, stride_y, stride_x);
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
#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
    }

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

425
426
    pooling::
    pooling (
427
428
429
430
    )
    {
    }

431
    void pooling::
432
433
434
435
436
437
438
439
440
441
442
    clear(
    )
    {
#ifdef DLIB_USE_CUDA
        impl.clear();
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

443
444
    void pooling::
    setup_max_pooling(
445
446
447
448
449
450
451
        int window_height,
        int window_width,
        int stride_y,
        int stride_x
    )
    {
#ifdef DLIB_USE_CUDA
452
        impl.setup_max_pooling(window_height, window_width, stride_y, stride_x);
453
454
455
456
457
458
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

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
    void pooling::
    setup_avg_pooling(
        int window_height,
        int window_width,
        int stride_y,
        int stride_x
    )
    {
#ifdef DLIB_USE_CUDA
        impl.setup_avg_pooling(window_height, window_width, stride_y, stride_x);
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

    bool pooling::
    does_max_pooling (
    ) const
    {
#ifdef DLIB_USE_CUDA
        return impl.does_max_pooling();
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

    void pooling::
488
489
490
491
492
493
494
495
496
497
498
499
500
    operator() (
        resizable_tensor& dest,
        const tensor& src
    )
    {
#ifdef DLIB_USE_CUDA
        impl(dest, src);
#else
        // TODO
        DLIB_CASSERT(false,"");
#endif
    }

501
    void pooling::
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
    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 (
521
        tensor& dest,
522
523
524
525
526
527
        const tensor& src
    )
    {
#ifdef DLIB_USE_CUDA
        cuda::softmax(dest,src);
#else
528
        cpu::softmax(dest,src);
529
530
531
532
533
#endif
    }

    void softmax_gradient (
        tensor& grad,
534
        const tensor& dest,
535
536
537
538
        const tensor& gradient_input
    )
    {
#ifdef DLIB_USE_CUDA
539
        cuda::softmax_gradient(grad, dest, gradient_input);
540
#else
541
        cpu::softmax_gradient(grad, dest, gradient_input);
542
543
544
545
546
547
#endif
    }

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

    void sigmoid (
548
        tensor& dest,
549
550
551
552
553
554
        const tensor& src
    )
    {
#ifdef DLIB_USE_CUDA
        cuda::sigmoid(dest,src);
#else
555
        cpu::sigmoid(dest,src);
556
557
558
559
560
561
562
563
564
565
#endif
    }

    void sigmoid_gradient (
        tensor& grad,
        const tensor& dest,
        const tensor& gradient_input
    )
    {
#ifdef DLIB_USE_CUDA
566
        cuda::sigmoid_gradient(grad, dest, gradient_input);
567
#else
568
        cpu::sigmoid_gradient(grad, dest, gradient_input);
569
570
571
572
573
574
#endif
    }

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

    void relu (
575
        tensor& dest,
576
577
578
579
580
581
        const tensor& src
    )
    {
#ifdef DLIB_USE_CUDA
        cuda::relu(dest,src);
#else
582
        cpu::relu(dest,src);
583
584
585
586
587
588
589
590
591
592
#endif
    }

    void relu_gradient (
        tensor& grad,
        const tensor& dest,
        const tensor& gradient_input
    )
    {
#ifdef DLIB_USE_CUDA
593
        cuda::relu_gradient(grad, dest, gradient_input);
594
#else
595
        cpu::relu_gradient(grad, dest, gradient_input);
596
597
598
599
600
601
#endif
    }

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

    void tanh (
602
        tensor& dest,
603
604
605
606
607
608
        const tensor& src
    )
    {
#ifdef DLIB_USE_CUDA
        cuda::tanh(dest,src);
#else
609
        cpu::tanh(dest,src);
610
611
612
613
614
615
616
617
618
619
#endif
    }

    void tanh_gradient (
        tensor& grad,
        const tensor& dest,
        const tensor& gradient_input
    )
    {
#ifdef DLIB_USE_CUDA
620
        cuda::tanh_gradient(grad, dest, gradient_input);
621
#else
622
        cpu::tanh_gradient(grad, dest, gradient_input);
623
624
625
626
627
628
629
630
631
#endif
    }

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

}}

#endif // DLIB_TeNSOR_TOOLS_CPP_