File: cpu_kernels.hh

package info (click to toggle)
python-thinc 9.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,896 kB
  • sloc: python: 17,122; javascript: 1,559; ansic: 342; makefile: 15; sh: 13
file content (482 lines) | stat: -rw-r--r-- 14,601 bytes parent folder | download | duplicates (2)
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
#ifndef CPU_KERNELS_HH
#define CPU_KERNELS_HH

#include <algorithm>
#include <cmath>
#include <cstring>
#include <stdexcept>
#include <string>
#include <type_traits>

// Ideally we'd use an alias declaration for a generic definition of
// *axpy. But Cython doesn't support alias declarations yet:
//
// https://github.com/cython/cython/issues/3272
//
// template <typename T>
// using axpy = void (*)(int N, T alpha, const T* X, int incX,
//                       T *Y, int incY);
//
// So, instead we'll do this the pre-C++11 way:

template <typename T>
struct axpy {
    typedef void (*ptr)(int N, T alpha, const T* X, int incX, T *Y, int incY);
};


// All elementwise functions, such as most activations, work in-place.


template <typename T, typename L>
struct argmax_result {
    T max;
    L max_idx;
};

template <typename T, typename L>
argmax_result<T, L> argmax(T const *arr, L len)
{
    static_assert(std::is_floating_point<T>::value,
        "Array should be floating point");
    static_assert(std::is_integral<L>::value, "Array length should be integral");

    argmax_result<T, L> r { arr[0],  0 };

    for (L i = 1; i < len; ++i) {
        if (arr[i] > r.max) {
            r.max = arr[i];
            r.max_idx = i;
        }
    }

    return r;
}

// The next two templates define argmax for a fixed number of elements.

template <typename T, typename L>
argmax_result<T, L> argmax(T a) {
    static_assert(std::is_floating_point<T>::value, "Argument should be floating point");
    argmax_result<T, L> acc { a, 0 };
    return acc;
}

template<typename T, typename L, typename... Args>
argmax_result<T, L> argmax(T a, Args... args) {
    static_assert(std::is_floating_point<T>::value, "Arguments should be floating point");

    auto acc = argmax<T, L>(args...);

    if (acc.max > a) {
        acc.max_idx += 1;
    } else {
        acc.max_idx = 0;
        acc.max = a;
    }

    return acc;
}


template <typename A, typename L>
void vec_add(A* X, const A* Y, A scale, L N)
{
    static_assert(std::is_floating_point<A>::value,
        "Array should be floating point");
    static_assert(std::is_integral<L>::value, "Array length should be integral");

    for (L i = 0; i < N; ++i)
        X[i] += scale * Y[i];
}

template <typename A, typename L>
void cpu_maxout(A* best__bo, L* which__bo, const A* cands__bop, L B, L O, L P)
{
    static_assert(std::is_floating_point<A>::value,
        "Array should be floating point");
    static_assert(std::is_integral<L>::value, "Array length should be integral");

    // For small inputs, we use an unrolled argmax.
    if (P == 2) {
        for (int i = 0; i < B * O; ++i) {
            A const *input = cands__bop + i * P;
            auto r = argmax<A, L>(input[0], input[1]);
            which__bo[i] = r.max_idx;
            best__bo[i] = r.max;
        }
    } else if (P == 3) {
        for (int i = 0; i < B * O; ++i) {
            A const *input = cands__bop + i * P;
            auto r = argmax<A, L>(input[0], input[1], input[2]);
            which__bo[i] = r.max_idx;
            best__bo[i] = r.max;
        }
    } else {
        for (int i = 0; i < B * O; ++i) {
            auto r = argmax<A, L>(cands__bop + i * P, P);
            which__bo[i] = r.max_idx;
            best__bo[i] = r.max;
        }
    }
}


template <typename A, typename L>
void cpu_backprop_maxout(A* dX__bop, const A* dX__bo, const L* which__bo,
    L B, L O, L P)
{
    static_assert(std::is_floating_point<A>::value,
        "Array should be floating point");
    static_assert(std::is_integral<L>::value, "Array length should be integral");

    for (L b = 0; b < B; ++b) {
        for (L o = 0; o < O; ++o) {
            if (*which__bo >= P) {
                throw std::out_of_range(std::string("index ") + std::to_string(*which__bo) + " is out of bounds for maxout with size " + std::to_string(P));
            }

            dX__bop[*which__bo] = *dX__bo;
            dX__bop += P;
            dX__bo += 1;
            which__bo += 1;
        }
    }
}

template <typename A, typename L>
void cpu_reduce_max(A* maxes__bo, L* which__bo, const A* X__to,
    const L* lengths__b, L B, L T, L O)
{
    static_assert(std::is_floating_point<A>::value,
        "Array should be floating point");
    static_assert(std::is_integral<L>::value, "Array length should be integral");

    for (const L* length = lengths__b; length < lengths__b + B; ++length) {
        if (*length <= 0)
            throw std::invalid_argument(std::string("all sequence lengths must be > 0, was: ") + std::to_string(*length));
        else if (*length > T) {
            throw std::out_of_range("lengths must sum up to the number of rows");
        }

        T -= *length;

        std::memcpy(maxes__bo, X__to, O * sizeof(*maxes__bo));
        X__to += O;
        for (L i = 1; i < *length; ++i) {
            for (L j = 0; j < O; ++j) {
                if (X__to[j] > maxes__bo[j]) {
                    maxes__bo[j] = X__to[j];
                    which__bo[j] = i;
                }
            }
            X__to += O;
        }

        maxes__bo += O;
        which__bo += O;
    }
}

template <typename A, typename L>
void cpu_backprop_reduce_max(A* dX__to, const A* d_maxes__bo, const L* which__bo,
    const L* lengths__b, L B, L T, L O)
{
    static_assert(std::is_floating_point<A>::value,
        "Array should be floating point");
    static_assert(std::is_integral<L>::value, "Array length should be integral");

    for (const L* length = lengths__b; length < lengths__b + B; ++length) {
        for (L i = 0; i < O; ++i) {
            L item = which__bo[i];
            if (item >= *length) {
                throw std::out_of_range(std::string("index ") + std::to_string(item) + " is out of bounds for maxout with length " + std::to_string(*length));
            }

            dX__to[item * O + i] = d_maxes__bo[i];
        }

        dX__to += *length * O;
        d_maxes__bo += O;
        which__bo += O;
    }
}

template <typename A, typename L>
void cpu_reduce_mean(A* means__bo, const A* X__to, const L* lengths__b,
    L B, L T, L O)
{
    static_assert(std::is_floating_point<A>::value,
        "Array should be floating point");
    static_assert(std::is_integral<L>::value, "Array length should be integral");

    for (const L* length = lengths__b; length < lengths__b + B; ++length) {
        if (*length < 0) {
            throw std::invalid_argument(std::string("all sequence lengths must be >= 0, was: ") + std::to_string(*length));
        }
        else if (length == 0) {
            means__bo += O;
            continue;
        }
        else if (*length > T) {
            throw std::out_of_range("lengths must sum up to the number of rows");
        }

        T -= *length;

        A scale = 1. / *length;
        for (L i = 0; i < *length; ++i) {
            vec_add(means__bo, X__to, scale, O);
            X__to += O;
        }

        means__bo += O;
    }
}

template <typename A, typename L>
void cpu_backprop_reduce_mean(A* dX__to, const A* d_means__bo, const L* lengths__b,
    L B, L T, L O)
{
    static_assert(std::is_floating_point<A>::value,
        "Array should be floating point");
    static_assert(std::is_integral<L>::value, "Array length should be integral");

    for (const L* length = lengths__b; length < lengths__b + B; ++length) {
        A scale = 1. / *length;
        for (L i = 0; i < *length; ++i) {
            vec_add(dX__to, d_means__bo, scale, O);
            dX__to += O;
        }

        d_means__bo += O;
    }
}

template <typename A, typename L>
void cpu_mish(A* Y, L N, A threshold)
{
    static_assert(std::is_floating_point<A>::value,
        "Array should be floating point");
    static_assert(std::is_integral<L>::value, "Array length should be integral");

    for (L i = 0; i < N; ++i) {
        if (Y[i] < threshold) {
            Y[i] *= std::tanh(std::log(1.0 + std::exp(Y[i])));
        }
    }
}

template <typename A, typename L>
void cpu_backprop_mish(A* dX, const A* X, L N, A threshold)
{
    static_assert(std::is_floating_point<A>::value,
        "Array should be floating point");
    static_assert(std::is_integral<L>::value, "Array length should be integral");

    for (L i = 0; i < N; ++i) {
        A x = X[i];

        if (x < threshold) {
            A exp_x = std::exp(x);
            A exp_2x = std::exp(2 * x);
            A exp_3x = std::exp(3 * x);
            A omega = (4. * (x + 1)) + (4 * exp_2x) + exp_3x + exp_x * (4. * x + 6);
            A delta = 2. * exp_x + exp_2x + 2.;
            dX[i] = dX[i] * ((exp_x * omega) / (delta * delta));
        }
    }
}

template <typename A, typename L>
void cpu_reduce_sum(A* sums__bo, const A* X__to, const L* lengths__b,
    L B, L T, L O)
{
    static_assert(std::is_floating_point<A>::value,
        "Array should be floating point");
    static_assert(std::is_integral<L>::value, "Array length should be integral");

    for (const L* length = lengths__b; length < lengths__b + B; ++length) {
        if (*length < 0) {
            throw std::invalid_argument(std::string("all sequence lengths must be >= 0, was: ") + std::to_string(*length));
        }
        else if (length == 0) {
            sums__bo += O;
            continue;
        }
        else if (*length > T) {
            throw std::out_of_range("lengths must sum up to the number of rows");
        }

        T -= *length;

        for (L i = 0; i < *length; ++i) {
            vec_add(sums__bo, X__to, static_cast<A>(1.0), O);
            X__to += O;
        }

        sums__bo += O;
    }
}

template <typename A, typename L>
void cpu_backprop_reduce_sum(A* dX__to, const A* d_sums__bo, const L* lengths__b,
    L B, L T, L O)
{
    static_assert(std::is_floating_point<A>::value,
        "Array should be floating point");
    static_assert(std::is_integral<L>::value, "Array length should be integral");

    for (const L* length = lengths__b; length < lengths__b + B; ++length) {
        for (L i = 0; i < *length; ++i) {
            vec_add(dX__to, d_sums__bo, static_cast<A>(1.0), O);
            dX__to += O;
        }

        d_sums__bo += O;
    }
}

template <typename A, typename L>
void cpu_relu(A* X, L N)
{
    static_assert(std::is_floating_point<A>::value,
        "Array should be floating point");
    static_assert(std::is_integral<L>::value, "Array length should be integral");

    for (L i = 0; i < N; ++i) {
        if (X[i] <= 0.0) {
            X[i] = 0.0;
        }
    }
}

template <typename A, typename L>
void seq2col(A* output, const A* X, const L* lengths, L nW, L B, L I, L nL)
{
    // Let's say nW is 1 (it usually is). Then we want to take:

    // 1a 1b 1c
    // 2a 2b 2c
    // 3a 3b 3c

    // And make

    // __ __ __ 1a 1b 1c 2a 2b 2c
    // 1a 1b 1c 2a 2b 2c 3a 3b 3c
    // 2a 2b 2c 3a 3b 3c __ __ __

    // Where __ is padding.

    // Now let's say nW is 2. Then we want to take:

    // 1a 1b 1c
    // 2a 2b 2c
    // 3a 3b 3c

    // And make

    // __ __ __ __ __ __ 1a 1b 1c 2a 2b 2c 3a 3b 3c
    // __ __ __ 1a 1b 1c 2a 2b 2c 3a 3b 3c __ __ __
    // 1a 1b 1c 2a 2b 2c 3a 3b 3c __ __ __ __ __ __

    // * x_start=-6, x_end=9 : (0-2) * 3, (0+2+1) * 3
    // * x_start=-3, x_end=13 : (1-2) * 3, (1+2+1) * 3
    // * x_start=0, x_end=16 : (2-2) * 3, (2+2+1) * 3

    // If lengths > 1, then the sequence lengths dictate
    // the boundaries/padding rather than the begin/end
    // of X.
    static_assert(std::is_floating_point<A>::value,
        "Array should be floating point");
    static_assert(std::is_integral<L>::value, "Array length should be integral");

    L nF = nW * 2 + 1;

    L seq_start = 0;
    for (L i = 0; i < nL; ++i) {
        // Calculate the bounds of the next sequence.
        L seq_end = seq_start + lengths[i];

        for (L j = seq_start; j < seq_end; ++j) {
            // Find the unconstrained window around b, which
            // may be out of the sequence bounds.
            L window_start = j - nW;
            L window_end = j + nW + 1;

            // Find the sequence-constrained window around b.
            L x_start = std::max(seq_start, window_start);
            L x_end = std::min(seq_end, window_end);
            L n_elems = x_end - x_start;

            L out_offset = x_start - window_start;

            std::memcpy(output + (j * nF * I) + (out_offset * I),
                X + (x_start * I),
                n_elems * I * sizeof(*output));
        }

        seq_start += lengths[i];
    }
}

template <typename A, typename L>
void backprop_seq2col(A* d_seqs, const A* d_cols, const L* lengths, L B, L I, L nW, L nL)
{
    // here's what we're doing, if we had 2d indexing.
    // for i in range(b):
    //     d_seq[i] += d_cols[i-2, 4]
    //     d_seq[i] += d_cols[i-1, 3]
    //     d_seq[i] += d_cols[i, 2]
    //     d_seq[i] += d_cols[i+1, 1]
    //     d_seq[i] += d_cols[i+2, 0]
    static_assert(std::is_floating_point<A>::value,
        "Array should be floating point");
    static_assert(std::is_integral<L>::value, "Array length should be integral");

    L nF = nW * 2 + 1;

    L seq_start = 0;
    for (L i = 0; i < nL; ++i) {
        // Calculate the bounds of the next sequence.
        L seq_end = seq_start + lengths[i];

        for (L j = seq_start; j < seq_end; ++j) {
            // Find the unconstrained window around b, which
            // may be out of the sequence bounds.
            L window_begin = j - nW;
            L window_end = j + nW + 1;

            // Find the sequence-constrained window around b.
            L d_seqs_begin = std::max(seq_start, window_begin);
            L d_seqs_end = std::min(seq_end, window_end);
            L n_elems = d_seqs_end - d_seqs_begin;

            // If the left window is cut short, we want to
            // start by the same amount in the output.
            L out_offset = d_seqs_begin - window_begin;

            vec_add(d_seqs + d_seqs_begin * I,
                d_cols + (j * nF * I) + (out_offset * I),
                static_cast<A>(1.), n_elems * I);
        }

        seq_start += lengths[i];
    }
}

template <typename F, typename I, typename L>
void cpu_gather_add(typename axpy<F>::ptr axpy, F* out_bo, const F* table_to, const I* indices_bk, L T, L O, L B, L K) {
     for (L b = 0; b < B; ++b) {
        for (L k = 0; k < K; ++k) {
            I idx = indices_bk[b * K + k];
            if (idx > T) {
                throw std::out_of_range("Embedding index out-of-bounds");
            }
            axpy(O, 1.0, table_to + idx * O, 1, out_bo + b * O, 1);
        }
    }
}


#endif // CPU_KERNELS_HH