opr_impl.cpp 10.3 KB
Newer Older
1 2 3 4 5 6 7 8
/**
 * \file dnn/src/naive/winograd_filter_preprocess/opr_impl.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
9 10
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
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
 */

#include "src/naive/winograd_filter_preprocess/opr_impl.h"
#include "src/common/utils.h"
#include "src/common/winograd/winograd_helper.h"
#include "src/naive/handle.h"

#include "midout.h"
MIDOUT_DECL(megdnn_naive_winograd_filter_preprocess)

using namespace megdnn;
using namespace naive;

void WinogradFilterPreprocessImpl::exec(_megdnn_tensor_in src,
                                        _megdnn_tensor_out dst,
                                        _megdnn_workspace workspace) {
    check_exec(src.layout, dst.layout, workspace.size);

    //! nchw88 group conv
    size_t flt_start = 0;
    size_t pack_c_size = 1;
    size_t group = 1;
    //! group conv
    if (src.layout.ndim == 5) {
        flt_start = 1;
        group = src.layout[0];
        //! nchw88 dense conv
    } else if (src.layout.ndim == 6) {
        pack_c_size = src.layout[5];
        //! nchw88 group conv
    } else if (src.layout.ndim == 7) {
        flt_start = 1;
        group = src.layout[0];
        pack_c_size = src.layout[6];
    }
    size_t OC = src.layout[flt_start] * pack_c_size,
           IC = src.layout[flt_start + 1] * pack_c_size,
           FW = src.layout[flt_start + 3];

    size_t m = param().output_block_size;

    bool execed = false;
53 54 55 56 57 58 59 60 61 62

#define cb(_ctype, _dst_type, _input_filter_compute_type,                    \
           _output_compute_type, _format, rescale)                           \
    if (param().format == _format) {                                         \
        return winograd::StrategyHelper<                                     \
                _ctype, _dst_type, _input_filter_compute_type,               \
                _output_compute_type, param::ConvBias::Format::NCHW,         \
                _format>::filter(src_ptr, dst_ptr, workspace_ptr, OC, IC, 0, \
                                 OC, m, FW, interp_points, src.layout.dtype, \
                                 rescale);                                   \
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
    }

#define DISPATCH_FORMAT_MK4(_ctype, _dst_type, _input_filter_compute_type,  \
                            _output_compute_type, _rescale)                 \
    cb(_ctype, _dst_type, _input_filter_compute_type, _output_compute_type, \
       param::Winograd::Format::DEFAULT, _rescale);                         \
    cb(_ctype, _dst_type, _input_filter_compute_type, _output_compute_type, \
       param::Winograd::Format::MK4, _rescale);

#define DISPATCH_FORMAT_MK8(_ctype, _dst_type, _input_filter_compute_type,  \
                            _output_compute_type, _rescale)                 \
    cb(_ctype, _dst_type, _input_filter_compute_type, _output_compute_type, \
       param::Winograd::Format::DEFAULT, _rescale);                         \
    cb(_ctype, _dst_type, _input_filter_compute_type, _output_compute_type, \
       param::Winograd::Format::MK8, _rescale);

#define DISPATCH_KERNEL(_ctype, _dst_type, _input_filter_compute_type,     \
                        _output_compute_type, _kern, _rescale, ...)        \
    const _ctype* src_ptr = src.compatible_ptr<_ctype>();                  \
    _input_filter_compute_type* dst_ptr =                                  \
            dst.compatible_ptr<_input_filter_compute_type>();              \
    _input_filter_compute_type* workspace_ptr =                            \
            workspace.ptr<_input_filter_compute_type>();                   \
    MIDOUT_BEGIN(megdnn_naive_winograd_filter_preprocess, ##__VA_ARGS__) { \
        for (size_t g = 0; g < group; g++) {                               \
            auto run = [=]() {                                             \
                _kern(_ctype, _dst_type, _input_filter_compute_type,       \
                      _output_compute_type, _rescale);                     \
            };                                                             \
            MEGDNN_DISPATCH_CPU_KERN_OPR(run());                           \
            src_ptr += src.layout.stride[0];                               \
            dst_ptr += dst.layout.stride[0];                               \
        }                                                                  \
        execed = true;                                                     \
    }                                                                      \
    MIDOUT_END();

#define DISPATCH_DTYPE(_midout_tag)                                          \
    if (src.layout.dtype.enumv() == DTypeEnum::Float32) {                    \
        DISPATCH_KERNEL(dt_float32, dt_float32, dt_float32, dt_float32,      \
                        DISPATCH_FORMAT_MK4, 1.0f, _midout_tag, 0);          \
    }                                                                        \
    if (src.layout.dtype.enumv() == DTypeEnum::QuantizedS8) {                \
        DISPATCH_KERNEL(dt_int8, dt_int8, dt_int16, dt_int32,                \
                        DISPATCH_FORMAT_MK8, 2.0f, _midout_tag, 1);          \
    }                                                                        \
    MEGDNN_INC_FLOAT16(if (src.layout.dtype.enumv() == DTypeEnum::Float16) { \
        DISPATCH_KERNEL(dt_float16, dt_float16, dt_float16, dt_float16,      \
                        DISPATCH_FORMAT_MK8, 1.0f, _midout_tag, 2);          \
    })
113

114
    if (src.layout.ndim <= 5) {
115
        //! dispatch_dtype with consider layout and format.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        if (FW == 3) {
            if (m == 2) {
                std::vector<float> interp_points = {0, 1, -1};
                DISPATCH_DTYPE(0);
            } else if (m == 6) {
                std::vector<float> interp_points = {0, 1, -1, 2, -2, 0.5, -0.5};
                DISPATCH_DTYPE(1);
            }
        } else if (FW == 4) {
            if (m == 5) {
                std::vector<float> interp_points = {0, 0.5, -0.5, 1, -1, 2, -2};
                DISPATCH_DTYPE(2);
            }
        } else if (FW == 5) {
            if (m == 4) {
                std::vector<float> interp_points = {0, 1, -1, 0.5, -0.5, 2, -2};
                DISPATCH_DTYPE(3);
            }
        }
#undef cb
#undef DISPATCH_FORMAT_MK4
#undef DISPATCH_FORMAT_MK8
#undef DISPATCH_DTYPE
139
    } else {
140
        megdnn_assert(src.layout.ndim == 6 || src.layout.ndim == 7);
141 142 143 144 145 146 147 148 149
#define cb(_ctype, _dst_type, _input_filter_compute_type,                    \
           _output_compute_type, _format, rescale)                           \
    if (param().format == _format) {                                         \
        return winograd::StrategyHelper<                                     \
                _ctype, _dst_type, _input_filter_compute_type,               \
                _output_compute_type, param::ConvBias::Format::NCHW88,       \
                _format>::filter(src_ptr, dst_ptr, workspace_ptr, OC, IC, 0, \
                                 OC, m, FW, interp_points, src.layout.dtype, \
                                 rescale);                                   \
150 151 152 153 154 155 156 157 158 159 160 161
    }

#define DISPATCH_FORMAT_MK8(_ctype, _dst_type, _input_filter_compute_type,  \
                            _output_compute_type, _rescale)                 \
    cb(_ctype, _dst_type, _input_filter_compute_type, _output_compute_type, \
       param::Winograd::Format::MK8, _rescale);

#define DISPATCH_DTYPE(_midout_tag)                                     \
    if (src.layout.dtype.enumv() == DTypeEnum::Float32) {               \
        DISPATCH_KERNEL(dt_float32, dt_float32, dt_float32, dt_float32, \
                        DISPATCH_FORMAT_MK8, 1.0f, _midout_tag, 0);     \
    }
162 163 164 165 166 167 168 169 170 171
        if (pack_c_size == 8) {  //! NCHW88
            if (FW == 3) {
                if (m == 2) {
                    std::vector<float> interp_points = {0, 1, -1};
                    DISPATCH_DTYPE(4);
                } else if (m == 6) {
                    std::vector<float> interp_points = {0,  1,   -1,  2,
                                                        -2, 0.5, -0.5};
                    DISPATCH_DTYPE(5);
                }
172
            }
173 174 175
#undef cb
#undef DISPATCH_FORMAT_MK8
#undef DISPATCH_DTYPE
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
        else if (pack_c_size == 4) {  //! NCHW44
#define cb(_ctype, _dst_type, _input_filter_compute_type,                    \
           _output_compute_type, _format, rescale)                           \
    if (param().format == _format) {                                         \
        return winograd::StrategyHelper<                                     \
                _ctype, _dst_type, _input_filter_compute_type,               \
                _output_compute_type, param::ConvBias::Format::NCHW44,       \
                _format>::filter(src_ptr, dst_ptr, workspace_ptr, OC, IC, 0, \
                                 OC, m, FW, interp_points, src.layout.dtype, \
                                 rescale);                                   \
    }

#define DISPATCH_FORMAT_MK4(_ctype, _dst_type, _input_filter_compute_type,  \
                            _output_compute_type, _rescale)                 \
    cb(_ctype, _dst_type, _input_filter_compute_type, _output_compute_type, \
       param::Winograd::Format::MK4, _rescale);

#define DISPATCH_DTYPE(_midout_tag)                                     \
    if (src.layout.dtype.enumv() == DTypeEnum::Float32) {               \
        DISPATCH_KERNEL(dt_float32, dt_float32, dt_float32, dt_float32, \
                        DISPATCH_FORMAT_MK4, 1.0f, _midout_tag, 0);     \
    }
            if (FW == 3) {
                if (m == 2) {
                    std::vector<float> interp_points = {0, 1, -1};
                    DISPATCH_DTYPE(6);
                } else if (m == 6) {
                    std::vector<float> interp_points = {0,  1,   -1,  2,
                                                        -2, 0.5, -0.5};
                    DISPATCH_DTYPE(7);
                }
            }
209 210 211 212
#undef cb
#undef DISPATCH_FORMAT_MK8
#undef DISPATCH_KERNEL
#undef DISPATCH_DTYPE
213
        }
214
    }
215 216 217 218 219 220
    megdnn_assert(execed,
                  "Unsupport winograd filter preprocess. m: %zu src: %s", m,
                  src.layout.to_string().c_str());
}

// vim: syntax=cpp.doxygen