summaryrefslogtreecommitdiff
path: root/inference-engine/src/extension/ext_normalize.cpp
blob: 420e5046ecd1f8d946bbe2bbeafe04b87bb32da8 (plain)
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
// Copyright (C) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//

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

#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <immintrin.h>

namespace InferenceEngine {
namespace Extensions {
namespace Cpu {

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

            weights = std::dynamic_pointer_cast<TBlob<float>>(layer->blobs.at("weights"));
            if (!weights)
                THROW_IE_EXCEPTION << layer->name << " weights is empty!";
            across_spatial = static_cast<bool>(layer->GetParamAsInt("across_spatial"));
            channel_shared = static_cast<bool>(layer->GetParamAsInt("channel_shared"));
            eps = layer->GetParamAsFloat("eps");

            addConfig(layer, {{ConfLayout::PLN, false, 0}}, {{ConfLayout::PLN, false, 0}}, true);
        } catch (InferenceEngine::details::InferenceEngineException &ex) {
            errorMsg = ex.what();
        }
    }

#if defined(HAVE_SSE) || defined(HAVE_AVX2)
    float hsum_sse(__m128 v) {
        __m128 shuf = _mm_movehdup_ps(v);
        __m128 sum = _mm_add_ps(v, shuf);
        shuf = _mm_movehl_ps(shuf, sum);
        sum = _mm_add_ss(sum, shuf);

        return _mm_cvtss_f32(sum);
    }

#if defined(HAVE_AVX2)
    float hsum_avx2(__m256 v) {
        __m128 vlow = _mm256_castps256_ps128(v);
        __m128 vhigh = _mm256_extractf128_ps(v, 1);

        __m128 sum = _mm_add_ps(vlow, vhigh);

        return hsum_sse(sum);
    }
#endif
#endif

    StatusCode execute(std::vector<Blob::Ptr>& inputs, std::vector<Blob::Ptr>& outputs,
                       ResponseDesc *resp) noexcept override {
        if (inputs.size() != 1 || outputs.empty()) {
            if (resp) {
                std::string errorMsg = "Incorrect number of input or output edges!";
                errorMsg.copy(resp->msg, sizeof(resp->msg) - 1);
            }
            return GENERAL_ERROR;
        }
        const float* src = inputs[0]->buffer();
        const float* scl = weights->buffer();
        float* dst = outputs[0]->buffer();

        SizeVector dims = inputs[0]->getTensorDesc().getDims();

        const int N = static_cast<const int>(dims[0]);
        const int C = static_cast<int>(dims[1]);
        const int H = static_cast<int>(dims.size() > 2 ? dims[2] : 1);
        const int W = static_cast<int>(dims.size() > 3 ? dims[3] : 1);

        const int HW = H*W;
        const int CHW = C*HW;

        for (int n = 0; n < N; n++) {
            const float* psrc = src + n*C*H*W;
            float* pdst = dst + n*C*H*W;

            if (across_spatial) {
                float norm = eps;
                int i = 0;
#if defined(HAVE_AVX2)
                {
                    __m256 vsum = _mm256_setzero_ps();
                    for (; i <= C*H*W-8; i += 8) {
                        __m256 vsrc = _mm256_loadu_ps(psrc + i);
                        vsum = _mm256_fmadd_ps(vsrc, vsrc, vsum);
                    }
                    norm += hsum_avx2(vsum);
                }
#elif defined(HAVE_SSE)
                {
                    __m128 vsum = _mm_setzero_ps();
                    for (; i <= C*H*W-4; i += 4) {
                        __m128 vsrc = _mm_loadu_ps(psrc + i);
                        vsum = _mm_add_ps(_mm_mul_ps(vsrc, vsrc), vsum);
                    }
                    norm += hsum_sse(vsum);
                }
#endif
                for (; i < C*H*W; i++) {
                    norm += psrc[i]*psrc[i];
                }
                norm = 1.0f / std::sqrt(norm);

                for (int c = 0 ; c < C; c++) {
                    int hw = 0;
#if defined(HAVE_AVX2)
                    __m256 vnorm_avx = _mm256_set1_ps(norm);
                    __m256 vscl_avx = _mm256_set1_ps(channel_shared ? scl[0] : scl[c]);
                    vnorm_avx = _mm256_mul_ps(vnorm_avx, vscl_avx);

                    for ( ; hw <= H*W - 8; hw += 8) {
                        __m256 vsrc = _mm256_loadu_ps(psrc + c*H*W + hw);
                        _mm256_storeu_ps(pdst + c*H*W+hw, _mm256_mul_ps(vsrc, vnorm_avx));
                    }
#elif defined(HAVE_SSE)
                    __m128 vnorm_sse = _mm_set1_ps(norm);
                    __m128 vscl_sse = _mm_set1_ps(channel_shared ? scl[0] : scl[c]);
                    vnorm_sse = _mm_mul_ps(vnorm_sse, vscl_sse);

                    for ( ; hw <= H*W - 4; hw += 4) {
                        __m128 vsrc = _mm_loadu_ps(psrc + c*H*W + hw);
                        _mm_storeu_ps(pdst + c*H*W+hw, _mm_mul_ps(vsrc, vnorm_sse));
                    }
#endif
                    for ( ; hw < H*W; hw++) {
                        float s = channel_shared ? scl[0] : scl[c];
                        pdst[c*H*W+hw] = psrc[c*H*W+hw] * norm * s;
                    }
                }
            } else {
                int wh = 0;
#if defined(HAVE_AVX2)
                for (; wh <= W*H - 8; wh += 8) {
                    __m256 vnorm = _mm256_set1_ps(eps);
                    for (int c = 0; c < C; c++) {
                        const float* psrc_c = psrc + c*W*H;
                        __m256 vsrc = _mm256_loadu_ps(psrc_c + wh);
                        vnorm = _mm256_fmadd_ps(vsrc, vsrc, vnorm);
                    }
                    vnorm = _mm256_div_ps(_mm256_set1_ps(1.0f), _mm256_sqrt_ps(vnorm));

                    for (int c = 0; c < C; c++) {
                        const float* psrc_c = psrc + c*W*H;
                        float* pdst_c = pdst + c*W*H;

                        __m256 vscl = _mm256_set1_ps(channel_shared ? scl[0] : scl[c]);

                        __m256 vsrc = _mm256_loadu_ps(psrc_c + wh);
                        __m256 vdst = _mm256_mul_ps(vsrc, vnorm);
                        vdst = _mm256_mul_ps(vdst, vscl);

                        _mm256_storeu_ps(pdst_c + wh, vdst);
                    }
                }
#elif defined(HAVE_SSE)
                for (; wh <= W*H - 4; wh += 4) {
                    __m128 vnorm = _mm_set1_ps(eps);
                    for (int c = 0; c < C; c++) {
                        const float* psrc_c = psrc + c*W*H;
                        __m128 vsrc = _mm_loadu_ps(psrc_c + wh);

                        vnorm = _mm_add_ps(_mm_mul_ps(vsrc, vsrc), vnorm);
                    }

                    vnorm = _mm_div_ps(_mm_set1_ps(1.0f), _mm_sqrt_ps(vnorm));

                    for (int c = 0; c < C; c++) {
                        const float* psrc_c = psrc + c*W*H;
                              float* pdst_c = pdst + c*W*H;

                        __m128 vscl = _mm_set1_ps(channel_shared ? scl[0] : scl[c]);

                        __m128 vsrc = _mm_loadu_ps(psrc_c + wh);
                        __m128 vdst = _mm_mul_ps(vsrc, vnorm);
                        vdst = _mm_mul_ps(vdst, vscl);

                        _mm_storeu_ps(pdst_c + wh, vdst);
                    }
                }
#endif
                for (; wh < W*H; wh++) {
                    float norm = eps;
                    for (int c = 0; c < C; c++) {
                        const float* psrc_c = psrc + c*W*H;
                        norm += psrc_c[wh]*psrc_c[wh];
                    }

                    norm = 1.0f / std::sqrt(norm);

                    for (int c = 0; c < C; c++) {
                        const float* psrc_c = psrc + c*W*H;
                        float* pdst_c = pdst + c*W*H;

                        pdst_c[wh] = channel_shared ? (psrc_c[wh] * norm * scl[0]) : (psrc_c[wh] * norm * scl[c]);
                    }
                }
            }
        }
        return OK;
    }

private:
    TBlob<float>::Ptr weights;

    bool across_spatial = true;
    bool channel_shared = true;
    float eps = 1e-10;
};

class NormalizeShapeInfer : public IShapeInferImpl {
public:
    StatusCode inferShapes(const std::vector<SizeVector>& inShapes,
                           const std::map<std::string, std::string>& params,
                           const std::map<std::string, Blob::Ptr>& blobs,
                           std::vector<SizeVector>& outShapes,
                           ResponseDesc* resp) noexcept override {
        outShapes.push_back(inShapes[0]);
        return InferenceEngine::OK;
    }
};

REG_FACTORY_FOR(ImplFactory<NormalizeImpl>, Normalize);
REG_SHAPE_INFER_FOR_TYPE(NormalizeShapeInfer, Normalize);

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