~iliaplatone/spacedrone.eu/inova-sis-pack

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
// Copyright (C) 2018-2019 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "ext_list.hpp"
#include "ext_base.hpp"

#include <cmath>
#include <string>
#include <vector>
#include <cassert>
#include <algorithm>
#if defined(HAVE_AVX2) || defined(HAVE_AVX512F)
#include <immintrin.h>
#endif
#include "ie_parallel.hpp"

namespace InferenceEngine {
namespace Extensions {
namespace Cpu {

inline int div_up(const int a, const int b) {
    assert(b);
    return (a + b - 1) / b;
}

class MVNImpl: public ExtLayerBase {
public:
    explicit MVNImpl(const CNNLayer* layer) {
        try {
            if (layer->insData.size() != 1 || layer->outData.empty())
                THROW_IE_EXCEPTION << "Incorrect number of input/output edges!";

            across_channels = layer->GetParamAsBool("across_channels", false);
            normalize_variance = layer->GetParamAsBool("normalize_variance", false);
            eps = layer->GetParamAsFloat("eps");

#if defined(HAVE_AVX512F)
            auto blk_layout = ConfLayout::BLK16;
#else
            auto blk_layout = ConfLayout::BLK8;
#endif
            addConfig(layer, {{blk_layout, false, -1}}, {{blk_layout, false, 0}});
            addConfig(layer, {{ConfLayout::PLN, false, 0}}, {{ConfLayout::PLN, false, 0}});
        } catch (InferenceEngine::details::InferenceEngineException &ex) {
            errorMsg = ex.what();
        }
    }

    StatusCode execute(std::vector<Blob::Ptr>& inputs, std::vector<Blob::Ptr>& outputs,
                       ResponseDesc *resp) noexcept override {
        float* src_data = inputs[0]->buffer();
        float* dst_data = outputs[0]->buffer();

        if (inputs[0]->getTensorDesc().getLayout() == NCHW || inputs[0]->getTensorDesc().getLayout() == NCDHW) {
            mvn_pln(src_data, dst_data, inputs[0]->getTensorDesc().getDims());
        } else {
            mvn_blk(src_data, dst_data, inputs[0]->getTensorDesc().getDims());
        }

        return OK;
    }

private:
    void mvn_pln(const float* src_data, float* dst_data, const SizeVector& dims);
    void mvn_blk(const float* src_data, float* dst_data, const SizeVector& dims);

    bool across_channels = false;
    bool normalize_variance = true;
    float eps = 1e-9f;
};

void MVNImpl::mvn_pln(const float* src_data, float* dst_data, const SizeVector& dims) {
    size_t dims_size = dims.size();
    size_t N = (dims_size > 0) ? dims[0] : 1lu;
    size_t C = (dims_size > 1) ? dims[1] : 1lu;
    size_t D = (dims_size > 4) ? dims[dims_size - 3] : 1lu;
    size_t H = (dims_size > 3) ? dims[dims_size - 2] : 1lu;
    size_t W = (dims_size > 2) ? dims[dims_size - 1] : 1lu;

    size_t C1 = H * W;
    size_t C2 = C1 * D;
    size_t C3 = C2 * C;

    for (size_t b = 0lu; b < N; b++) {
        // Calculate mean value
        size_t cb = b * C3;
        if (across_channels) {
            double mean = 0.0;
            mean = parallel_sum(C, mean, [&](size_t c)->double {
                double mean_internal = 0.0;
                size_t cc = cb + c * C2;
                for (size_t d = 0lu; d < D; d++) {
                    size_t cd = cc + d * C1;
                    for (size_t h = 0lu; h < H; h++) {
                        size_t ch = cd + h * W;
                        for (size_t w = 0lu; w < W; w++) {
                            mean_internal += src_data[ch + w];
                        }
                    }
                }
                return mean_internal;
            });

            mean /= C3;
            parallel_for(C, [&](int c) {
                size_t cc = cb + c * C2;
                for (size_t d = 0lu; d < D; d++) {
                    size_t cd = cc + d * C1;
                    for (size_t h = 0lu; h < H; h++) {
                        size_t ch = cd + h * W;
                        for (size_t w = 0lu; w < W; w++) {
                            size_t cw = ch + w;
                            dst_data[cw] = src_data[cw] - static_cast<float>(mean);
                        }
                    }
                }
            });
        } else {
            parallel_for(C, [&](size_t c) {
                double mean = 0.f;
                size_t cc = cb + c * C2;
                for (size_t d = 0lu; d < D; d++) {
                    size_t cd = cc + d * C1;
                    for (size_t h = 0lu; h < H; h++) {
                        size_t ch = cd + h * W;
                        for (size_t w = 0lu; w < W; w++) {
                            mean += src_data[ch + w];
                        }
                    }
                }

                mean /= static_cast<double>(C2);

                for (size_t d = 0lu; d < D; d++) {
                    size_t cd = cc + d * C1;
                    for (size_t h = 0lu; h < H; h++) {
                        size_t ch = cd + h * W;
                        for (size_t w = 0lu; w < W; w++) {
                            size_t cw = ch + w;
                            dst_data[cw] = src_data[cw] - static_cast<float>(mean);
                        }
                    }
                }
            });
        }
    }

    if (normalize_variance) {
        for (size_t b = 0lu; b < N; b++) {
            // Calculate variances value
            size_t cb = b * C3;
            if (across_channels) {
                double variance = 0.0;
                variance = parallel_sum(C, variance, [&](size_t c)->double {
                    double variance_internal = 0.0;
                    size_t cc = cb + c * C2;
                    for (size_t d = 0lu; d < D; d++) {
                        size_t cd = cc + d * C1;
                        for (size_t h = 0lu; h < H; h++) {
                            size_t ch = cd + h * W;
                            for (size_t w = 0lu; w < W; w++) {
                                variance_internal += std::pow(dst_data[ch + w], 2);
                            }
                        }
                    }
                    return variance_internal;
                });

                variance /= C3;
                variance += eps;
                variance = std::pow(variance, 0.5f);
                parallel_for(C, [&](int c) {
                    size_t cc = cb + c * C2;
                    for (size_t d = 0lu; d < D; d++) {
                        size_t cd = cc + d * C1;
                        for (size_t h = 0lu; h < H; h++) {
                            size_t ch = cd + h * W;
                            for (size_t w = 0lu; w < W; w++) {
                                dst_data[ch + w] /= static_cast<float>(variance);
                            }
                        }
                    }
                });
            } else {
                parallel_for(C, [&](size_t c) {
                    double variance = 0.0;
                    size_t cc = cb + c * C2;
                    for (size_t d = 0lu; d < D; d++) {
                        size_t cd = cc + d * C1;
                        for (size_t h = 0lu; h < H; h++) {
                            size_t ch = cd + h * W;
                            for (size_t w = 0lu; w < W; w++) {
                                variance += std::pow(dst_data[ch + w], 2);
                            }
                        }
                    }

                    variance /= static_cast<double>(C2);
                    variance += eps;
                    variance = std::pow(variance, 0.5f);
                    for (size_t d = 0lu; d < D; d++) {
                        size_t cd = cc + d * C1;
                        for (size_t h = 0lu; h < H; h++) {
                            size_t ch = cd + h * W;
                            for (size_t w = 0lu; w < W; w++) {
                                dst_data[ch + w] /= static_cast<float>(variance);
                            }
                        }
                    }
                });
            }
        }
    }
}

void MVNImpl::mvn_blk(const float* src_data, float* dst_data, const SizeVector& dims) {
#if defined(HAVE_AVX512F)
    size_t blk_size = 16;
#else
    size_t blk_size = 8lu;
#endif

#if defined(HAVE_AVX512F)
    typedef __m512 vec_type;
#elif defined(HAVE_AVX2)
    typedef __m256 vec_type;
#endif
    size_t dims_size = dims.size();
    size_t N = (dims_size > 0) ? dims[0] : 1lu;
    size_t C = (dims_size > 1) ? dims[1] : 1lu;
    size_t D = (dims_size > 4) ? dims[dims_size - 3] : 1lu;
    size_t H = (dims_size > 3) ? dims[dims_size - 2] : 1lu;
    size_t W = (dims_size > 2) ? dims[dims_size - 1] : 1lu;

    int CB = div_up(static_cast<int>(C), static_cast<int>(blk_size));

    size_t C0 = W * blk_size;
    size_t C1 = C0 * H;
    size_t C2 = C1 * D;
    size_t C3 = C2 * CB;
    size_t C5 = C * D * H * W;

    if (normalize_variance) {
        for (size_t b = 0lu; b < N; b++) {
            size_t ccb = b * C3;
            if (across_channels) {
                double mean = 0.0;
                mean = parallel_sum3d(CB, D, H, mean, [&](size_t cb, size_t d, size_t h)->double {
                    size_t ccbd = ccb + cb * C2 + d * C1 + h * C0;
                    size_t min_cb = std::min(blk_size, C - cb * blk_size);
                    double mean_internal = 0.0;
                    for (size_t w = 0lu; w < W; w++) {
                        size_t cw = ccbd + w * blk_size;
                        for (size_t c = 0lu; c < min_cb; c++) {
                            mean_internal += src_data[cw + c];
                        }
                    }
                    return mean_internal;
                });

                mean /= static_cast<double>(C5);

                double variance = 0.0;
                variance = parallel_sum3d(CB, D, H, variance, [&](size_t cb, size_t d, size_t h)->double {
                    size_t ccbd = ccb + cb * C2 + d * C1 + h * C0;
                    double variance_internal = 0.0;
                    for (size_t w = 0lu, min_cb = std::min(blk_size, C - cb * blk_size); w < W; w++) {
                        size_t cw = ccbd + w * blk_size;
                        for (size_t c = 0lu; c < min_cb; c++) {
                            variance_internal += std::pow(static_cast<double>(src_data[cw + c]) - mean, 2);
                        }
                    }
                    return variance_internal;
                });

                variance /= static_cast<double>(C5);
                variance += eps;
                variance = std::pow(variance, 0.5f);

                parallel_for3d(CB, D, H, [&](size_t cb, size_t d, size_t h) {
                    size_t ccbd = ccb + cb * C2 + d * C1 + h * C0;
                    for (size_t w = 0lu, min_cb = std::min(blk_size, C - cb * blk_size); w < W; w++) {
                        size_t cw = ccbd + w * blk_size;
                        for (size_t c = 0lu; c < min_cb; c++) {
                            size_t src_offset = cw + c;

                            dst_data[src_offset] = static_cast<float>((static_cast<double>(src_data[src_offset]) - mean) / variance);
                        }
                    }
                });
            } else {
                parallel_for(CB, [&](size_t cb) {
                    size_t src_off = ccb + cb * C2;
#if defined(HAVE_AVX2) || defined(HAVE_AVX512F)
                    vec_type vmean = _mm_uni_setzero_ps();
                    for (size_t d = 0lu; d < D; d++) {
                        size_t cd = src_off + d * C1;
                        for (size_t h = 0lu; h < H; h++) {
                            size_t ch = cd + h * C0;
                            for (size_t w = 0lu; w < W; w++) {
                                vec_type vsrc = _mm_uni_loadu_ps(src_data + ch + w * blk_size);
                                vmean = _mm_uni_add_ps(vmean, vsrc);
                            }
                        }
                    }

                    vec_type vsize = _mm_uni_set1_ps(static_cast<float>(D * H * W));
                    vmean = _mm_uni_div_ps(vmean, vsize);

                    vec_type vvariance = _mm_uni_setzero_ps();
                    for (size_t d = 0lu; d < D; d++) {
                        size_t cd = src_off + d * C1;
                        for (size_t h = 0lu; h < H; h++) {
                            size_t ch = cd + h * C0;
                            for (size_t w = 0lu; w < W; w++) {
                                vec_type vsrc = _mm_uni_loadu_ps(src_data + ch + w * blk_size);
                                vsrc = _mm_uni_sub_ps(vsrc, vmean);
                                vvariance = _mm_uni_add_ps(vvariance, _mm_uni_mul_ps(vsrc, vsrc));
                            }
                        }
                    }
                    vvariance = _mm_uni_div_ps(vvariance, vsize);

                    vec_type veps = _mm_uni_set1_ps(eps);
                    vvariance = _mm_uni_add_ps(vvariance, veps);

                    vvariance = _mm_uni_sqrt_ps(vvariance);

                    for (size_t d = 0lu; d < D; d++) {
                        size_t cd = src_off + d * C1;
                        for (size_t h = 0lu; h < H; h++) {
                            size_t ch = cd + h * C0;
                            for (size_t w = 0lu; w < W; w++) {
                                size_t offset = ch + w * blk_size;
                                vec_type vsrc = _mm_uni_loadu_ps(src_data + offset);
                                vsrc = _mm_uni_sub_ps(vsrc, vmean);
                                _mm_uni_storeu_ps(dst_data + offset, _mm_uni_div_ps(vsrc, vvariance));
                            }
                        }
                    }
#else
                    size_t min_cb = std::min(blk_size, C - cb * blk_size);
                    for (size_t c = 0; c < min_cb; c++) {
                        size_t cc = src_off + c;

                        double mean = 0.0;
                        for (size_t d = 0; d < D; d++) {
                            size_t cd = cc + d * C1;
                            for (size_t h = 0; h < H; h++) {
                                size_t ch = cd + h * C0;
                                for (size_t w = 0; w < W; w++) {
                                    mean += src_data[ch + w * blk_size];
                                }
                            }
                        }

                        size_t C4 = D * H * W;
                        mean /= static_cast<double>(C4);

                        double variance = 0.0;
                        for (size_t d = 0lu; d < D; d++) {
                            size_t cd = cc + d * C1;
                            for (size_t h = 0lu; h < H; h++) {
                                size_t ch = cd + h * C0;
                                for (size_t w = 0lu; w < W; w++) {
                                    double value = static_cast<double>(src_data[ch + w * blk_size]) - mean;
                                    variance += std::pow(value, 2);
                                }
                            }
                        }

                        variance /= static_cast<double>(C4);
                        variance += eps;
                        variance = std::pow(variance, 0.5f);

                        for (size_t d = 0lu; d < D; d++) {
                            size_t cd = cc + d * C1;
                            for (size_t h = 0lu; h < H; h++) {
                                size_t ch = cd + h * C0;
                                for (size_t w = 0lu; w < W; w++) {
                                    size_t index = ch + w * blk_size;
                                    dst_data[index] = (src_data[index] - static_cast<float>(mean)) / static_cast<float>(variance);
                                }
                            }
                        }
                    }
#endif
                });
            }
        }
    } else {
        for (size_t b = 0; b < N; b++) {
            size_t ccb = b * C3;
            if (across_channels) {
                double mean = 0.0;
                mean = parallel_sum3d(CB, D, H, mean, [&](size_t cb, size_t d, size_t h)->double {
                    size_t ccbd = ccb + cb * C2 + d * C1 + h * C0;
                    double mean_internal = 0.f;
                    for (size_t w = 0lu, min_cb = std::min(blk_size, C - cb * blk_size); w < W; w++) {
                        size_t cw = ccbd + w * blk_size;
                        for (size_t c = 0lu; c < min_cb; c++) {
                            mean_internal += src_data[cw + c];
                        }
                    }
                    return mean_internal;
                });

                mean /= static_cast<double>(C5);

                parallel_for3d(CB, D, H, [&](size_t cb, size_t d, size_t h) {
                    size_t ccbd = ccb + cb * C2 + d * C1 + h * C0;
                    for (size_t w = 0lu, min_cb = std::min(blk_size, C - cb * blk_size); w < W; w++) {
                        size_t cw = ccbd + w * blk_size;
                        for (size_t c = 0lu; c < min_cb; c++) {
                            size_t src_offset = cw + c;

                            dst_data[src_offset] = src_data[src_offset] - static_cast<float>(mean);
                        }
                    }
                });
            } else {
                parallel_for(CB, [&](size_t cb) {
                    size_t src_off = ccb + cb * C2;
#if defined(HAVE_AVX2) || defined(HAVE_AVX512F)
                    vec_type vmean = _mm_uni_setzero_ps();
                    for (size_t d = 0lu; d < D; d++) {
                        size_t cd = src_off + d * C1;
                        for (size_t h = 0lu; h < H; h++) {
                            size_t ch = cd + h * C0;
                            for (size_t w = 0lu; w < W; w++) {
                                vec_type vsrc = _mm_uni_loadu_ps(src_data + ch + w * blk_size);
                                vmean = _mm_uni_add_ps(vmean, vsrc);
                            }
                        }
                    }

                    vec_type vsize = _mm_uni_set1_ps(static_cast<float>(D * H * W));
                    vmean = _mm_uni_div_ps(vmean, vsize);

                    for (size_t d = 0lu; d < D; d++) {
                        size_t cd = src_off + d * C1;
                        for (size_t h = 0lu; h < H; h++) {
                            size_t ch = cd + h * C0;
                            for (size_t w = 0lu; w < W; w++) {
                                size_t offset = ch + w * blk_size;
                                vec_type vsrc = _mm_uni_loadu_ps(src_data + offset);
                                _mm_uni_storeu_ps(dst_data + offset, _mm_uni_sub_ps(vsrc, vmean));
                            }
                        }
                    }
#else
                    size_t min_cb = std::min(blk_size, C - cb * blk_size);
                    for (size_t c = 0lu; c < min_cb; c++) {
                        size_t cc = src_off + c;
                        double mean = 0.0;
                        for (size_t d = 0lu; d < D; d++) {
                            size_t cd = cc + d * C1;
                            for (size_t h = 0lu; h < H; h++) {
                                size_t ch = cd + h * C0;
                                for (size_t w = 0lu; w < W; w++) {
                                    mean += src_data[ch + w * blk_size];
                                }
                            }
                        }

                        size_t C4 = D * H * W;
                        mean /= static_cast<double>(C4);

                        for (size_t d = 0lu; d < D; d++) {
                            size_t cd = cc + d * C1;
                            for (size_t h = 0lu; h < H; h++) {
                                size_t ch = cd + h * C0;
                                for (size_t w = 0lu; w < W; w++) {
                                    size_t index = ch + w * blk_size;
                                    dst_data[index] = src_data[index] - static_cast<float>(mean);
                                }
                            }
                        }
                    }
#endif
                });
            }
        }
    }
}

REG_FACTORY_FOR(ImplFactory<MVNImpl>, MVN);

}  // namespace Cpu
}  // namespace Extensions
}  // namespace InferenceEngine