softmax.h 4.22 KB
Newer Older
openvino-pushbot's avatar
openvino-pushbot committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright (C) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#define USE_FAST_EXP 0

#if USE_FAST_EXP
#include "fast_exp.h"
#else

#include "opt_exp.h"

#endif

#include <cmath>
#include "defs.h"
Alexey Suhov's avatar
Alexey Suhov committed
20 21
#include "ie_parallel.hpp"

openvino-pushbot's avatar
openvino-pushbot committed
22 23 24

static inline
void softmax_many_batches(const float *src_data, float *dst_data, int B, int C, int H, int W) {
Alexey Suhov's avatar
Alexey Suhov committed
25
    InferenceEngine::parallel_for(B * H * W, [&](size_t i) {
openvino-pushbot's avatar
openvino-pushbot committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
        const float *psrc = src_data + (i / (H * W)) * C * H * W - (i / (H * W)) * H * W;
        float *pdst = dst_data + (i / (H * W)) * C * H * W - (i / (H * W)) * H * W;

        float max = psrc[i];
        for (int c = 0; c < C; c++) {
            float val = psrc[c * H * W + i];
            if (val > max) max = val;
        }

        float expSum = 0;
        for (int c = 0; c < C; c++) {
            pdst[c * H * W + i] = exp(psrc[c * H * W + i] - max);
            expSum += pdst[c * H * W + i];
        }

        for (int c = 0; c < C; c++) {
            pdst[c * H * W + i] = pdst[c * H * W + i] / expSum;
        }
Alexey Suhov's avatar
Alexey Suhov committed
44
    });
openvino-pushbot's avatar
openvino-pushbot committed
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
}

static inline
void softmax_generic(const float *src_data, float *dst_data, int B, int C, int H, int W) {
    for (int b = 0; b < B; b++) {
#if defined(HAVE_AVX2)
        for (int i = 0; i <= H*W - 8; i += 8) {
            __m256 vmax = _mm256_loadu_ps(src_data + b*C*H*W + i);
            for (int c = 0; c < C; c++) {
                __m256 vval = _mm256_loadu_ps(src_data + b*C*H*W + c*H*W + i);
                __m256 vmask = _mm256_cmp_ps(vval, vmax, _CMP_GT_OS);
                vmax = _mm256_blendv_ps(vmax, vval, vmask);
            }

            __m256 vexpSum = _mm256_setzero_ps();
            for (int c = 0; c < C; c++) {
                __m256 vval = _mm256_loadu_ps(src_data + b*C*H*W + c*H*W + i);
#if USE_FAST_EXP
                __m256 vres = _avx_fast_exp_ps(_mm256_sub_ps(vval, vmax));
#else
                __m256 vres = _avx_opt_exp_ps(_mm256_sub_ps(vval, vmax));
#endif
                vexpSum = _mm256_add_ps(vexpSum, vres);
                _mm256_storeu_ps(dst_data + b*C*H*W + c*H*W + i, vres);
            }

            for (int c = 0; c < C; c++) {
                __m256 vval = _mm256_loadu_ps(dst_data + b*C*H*W + c*H*W + i);
                _mm256_storeu_ps(dst_data + b*C*H*W + c*H*W + i, _mm256_div_ps(vval, vexpSum));
            }
        }
#elif defined(HAVE_SSE)
        for (int i = 0; i <= H*W - 4; i += 4) {
            __m128 vmax = _mm_loadu_ps(src_data + b*C*H*W + i);
            for (int c = 0; c < C; c++) {
                __m128 vval = _mm_loadu_ps(src_data + b*C*H*W + c*H*W + i);
                __m128 vmask = _mm_cmpgt_ps(vval, vmax);
                vmax = _mm_blendv_ps(vmax, vval, vmask);
            }

            __m128 vexpSum = _mm_setzero_ps();
            for (int c = 0; c < C; c++) {
                __m128 vval = _mm_loadu_ps(src_data + b*C*H*W + c*H*W + i);
#if USE_FAST_EXP
                __m128 vres = _sse_fast_exp_ps(_mm_sub_ps(vval, vmax));
#else
                __m128 vres = _sse_opt_exp_ps(_mm_sub_ps(vval, vmax));
#endif
                vexpSum = _mm_add_ps(vexpSum, vres);
                _mm_storeu_ps(dst_data + b*C*H*W + c*H*W + i, vres);
            }

            for (int c = 0; c < C; c++) {
                __m128 vval = _mm_loadu_ps(dst_data + b*C*H*W + c*H*W + i);
                _mm_storeu_ps(dst_data + b*C*H*W + c*H*W + i, _mm_div_ps(vval, vexpSum));
            }
        }
#endif

#if defined(HAVE_AVX2)
        int start = (H*W / 8) * 8;
#elif defined(HAVE_SSE)
        int start = (H*W / 4) * 4;
#else
        int start = 0;
#endif
        for (int i = start; i < H * W; i++) {
            float max = src_data[b * C * H * W + i];
            for (int c = 0; c < C; c++) {
                float val = src_data[b * C * H * W + c * H * W + i];
                if (val > max) max = val;
            }

            float expSum = 0;
            for (int c = 0; c < C; c++) {
                dst_data[b * C * H * W + c * H * W + i] = exp(src_data[b * C * H * W + c * H * W + i] - max);
                expSum += dst_data[b * C * H * W + c * H * W + i];
            }

            for (int c = 0; c < C; c++) {
                dst_data[b * C * H * W + c * H * W + i] = dst_data[b * C * H * W + c * H * W + i] / expSum;
            }
        }
    }
}