winograd_helper.cpp 16.3 KB
Newer Older
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
/**
 * \file dnn/src/common/winograd/winograd_helper.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
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

#include "src/common/winograd/winograd_helper.h"
#include "src/common/winograd/winograd_generator.h"
#include "src/naive/matrix_mul/matrix_mul_helper.h"

using namespace megdnn;
namespace {
template <typename ctype, typename otype, typename enable = void>
struct Getter {
    Getter(DType){};
    otype operator()(ctype item) { return item; }
};

template <typename ctype, typename otype>
struct Getter<ctype, otype,
              typename std::enable_if_t<std::is_same<ctype, uint8_t>::value>> {
    otype zp;
    Getter(DType dtype) {
        zp = dtype.param<dtype::Quantized8Asymm>().zero_point;
    }
    otype operator()(ctype item) { return static_cast<otype>(item) - zp; }
};

template <typename ctype, typename otype, typename enable = void>
struct OutputGetter {
    OutputGetter(DType){};
    otype operator()(float item) { return static_cast<otype>(item); }
};

template <typename ctype, typename otype>
struct OutputGetter<
        ctype, otype,
        typename std::enable_if_t<std::is_same<otype, int8_t>::value>> {
    DType dtype;
    OutputGetter(DType dtype) : dtype{dtype} {}
    otype operator()(float item) {
        return dtype.param<dtype::QuantizedS8>().quantize(item).as_int8();
    }
};

template <typename ctype, typename otype>
struct OutputGetter<
        ctype, otype,
        typename std::enable_if_t<std::is_same<otype, uint8_t>::value>> {
    DType dtype;
    OutputGetter(DType dtype) : dtype{dtype} {}
    otype operator()(float item) {
        return dtype.param<dtype::Quantized8Asymm>().quantize(item).as_uint8();
    }
};
}  // namespace

namespace megdnn {
namespace winograd {

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
constexpr size_t layout_pack_size(param::ConvBias::Format layout) {
    switch (layout) {
        case param::ConvBias::Format::NHWCD4:
            return 4;
        case param::ConvBias::Format::NCHW4:
            return 4;
        case param::ConvBias::Format::NCHW32:
            return 32;
        case param::ConvBias::Format::NCHW88:
        case param::ConvBias::Format::NCHW8:
            return 8;
        default:
            return 1;
    }
}

template <param::ConvBias::Format layout, param::MatrixMul::Format format>
struct FilterVisitor {
    size_t IC, OC;
    FilterVisitor(size_t OC, size_t IC) : IC(IC), OC(OC) {}
    size_t get(size_t r, size_t oc, size_t ic, size_t h, size_t w) {
        constexpr size_t input_pack_size = layout_pack_size(layout);
        size_t ocb_layout = oc / input_pack_size;
        size_t oc_layout = oc % input_pack_size;
        size_t icb_layout = ic / input_pack_size;
        size_t ic_layout = ic % input_pack_size;

        return (ocb_layout * (IC / input_pack_size) + icb_layout) * r * r *
                       input_pack_size * input_pack_size +
               ic_layout * input_pack_size + oc_layout +
               (h * r + w) * input_pack_size * input_pack_size;
    }
98

99 100 101
    size_t put(size_t alpha, size_t oc, size_t ic, size_t h, size_t w) {
        if (format == param::MatrixMul::Format::DEFAULT) {
            return (h * alpha + w) * OC * IC + ic * OC + oc;
102
        }
103 104 105 106 107 108 109 110 111 112 113 114 115 116
        size_t matmul_pack_size = MatrixMulForward::pack_size(format);
        size_t ocb = oc / matmul_pack_size;
        size_t oc_pack = oc % matmul_pack_size;
        size_t icb = ic / matmul_pack_size;
        size_t ic_pack = ic % matmul_pack_size;

        size_t OCB = OC / matmul_pack_size;
        size_t ICB = IC / matmul_pack_size;

        return (h * alpha + w) * OCB * ICB * matmul_pack_size *
                       matmul_pack_size +
               ocb * ICB * matmul_pack_size * matmul_pack_size +
               icb * matmul_pack_size * matmul_pack_size +
               ic_pack * matmul_pack_size + oc_pack;
117
    }
118
};
119

120 121 122 123
template <param::ConvBias::Format layout, param::MatrixMul::Format format>
struct InputVisitor {
    size_t IC;
    InputVisitor(size_t IC) : IC(IC) {}
124

125 126 127 128 129 130 131 132
    size_t get(size_t alpha, size_t ic, size_t IH, size_t IW, size_t ih,
               size_t iw) {
        constexpr size_t input_pack_size = layout_pack_size(layout);
        size_t icb_layout = ic / input_pack_size;
        size_t ic_layout = ic % input_pack_size;

        return (icb_layout * IH * IW + ih * IW + iw) * input_pack_size +
               ic_layout;
133 134
    }

135 136 137 138
    size_t put(size_t alpha, size_t ic, size_t nr_units_in_tile,
               size_t unit_idx, size_t h, size_t w) {
        if (format == param::MatrixMul::Format::DEFAULT) {
            return (h * alpha + w) * nr_units_in_tile * IC + unit_idx * IC + ic;
139
        }
140 141 142 143 144 145 146 147
        size_t matmul_pack_size = MatrixMulForward::pack_size(format);
        size_t icb = ic / matmul_pack_size;
        size_t ic_pack = ic % matmul_pack_size;
        size_t ICB = IC / matmul_pack_size;

        return (h * alpha + w) * ICB * nr_units_in_tile * matmul_pack_size +
               icb * nr_units_in_tile * matmul_pack_size +
               unit_idx * matmul_pack_size + ic_pack;
148 149 150
    }
};

151 152 153 154
template <param::ConvBias::Format layout, param::MatrixMul::Format format>
struct OutputVisitor {
    size_t OC;
    OutputVisitor(size_t OC) : OC(OC) {}
155

156 157 158 159 160
    size_t get(size_t alpha, size_t oc_index, size_t oc,
               size_t nr_units_in_tile, size_t unit_idx, size_t h, size_t w) {
        if (format == param::MatrixMul::Format::DEFAULT) {
            return (h * alpha + w) * nr_units_in_tile * OC + unit_idx * OC +
                   oc_index;
161
        }
162 163 164 165 166 167 168 169
        size_t matmul_pack_size = MatrixMulForward::pack_size(format);
        size_t ocb = oc_index / matmul_pack_size;
        size_t oc_pack = oc % matmul_pack_size;
        size_t OCB = OC / matmul_pack_size;

        return (h * alpha + w) * OCB * nr_units_in_tile * matmul_pack_size +
               ocb * nr_units_in_tile * matmul_pack_size +
               unit_idx * matmul_pack_size + oc_pack;
170 171
    }

172 173 174
    size_t put(size_t oc, size_t OH, size_t OW, size_t oh, size_t ow) {
        constexpr size_t input_pack_size = layout_pack_size(layout);
        size_t oc_layout = oc % input_pack_size;
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
        return (oc / input_pack_size * OH * OW + oh * OW + ow) *
                       input_pack_size +
               oc_layout;
    }
};

template <typename ctype, typename dst_type, typename input_filter_compute_type,
          typename output_compute_type, param::ConvBias::Format layout,
          param::MatrixMul::Format format>
void StrategyHelper<
        ctype, dst_type, input_filter_compute_type, output_compute_type, layout,
        format>::filter(const ctype* filter,
                        input_filter_compute_type* filter_transform_buf,
                        input_filter_compute_type* transform_mid_buf, size_t OC,
                        size_t IC, size_t oc_start, size_t oc_end, size_t m,
                        size_t r, const std::vector<float>& interp_points,
                        DType dtype, float rescale) {
    size_t alpha = m + r - 1;
    WinogradCoeff<input_filter_compute_type> winograd_coeff(m, r,
                                                            interp_points);
    input_filter_compute_type* mid_buf1 = transform_mid_buf;
    input_filter_compute_type* mid_buf2 = transform_mid_buf + alpha * alpha;
    Getter<ctype, input_filter_compute_type> getter(dtype);
    FilterVisitor<layout, format> filter_visitor(OC, IC);

    for (size_t oc = oc_start; oc < oc_end; oc++) {
        rep(ic, IC) {
            rep(i, r) rep(j, r) {
                mid_buf1[i * r + j] =
                        getter(filter[filter_visitor.get(r, oc, ic, i, j)]);
206
            }
207 208

            /* tmp = Matmul(G, src) */
209
            megdnn::naive::run_matrix_mul_tpl<input_filter_compute_type,
210
                                              input_filter_compute_type, false,
211
                                              false>(
212 213 214
                    winograd_coeff.G(rescale).data(), mid_buf1, mid_buf2, alpha,
                    r, r, r, r, r, dtype, dtype);
            /* dst = Matmul(tmp, G^T) */
215 216
            megdnn::naive::run_matrix_mul_tpl<input_filter_compute_type,
                                              input_filter_compute_type, false,
217 218 219 220
                                              true>(
                    mid_buf2, winograd_coeff.G(rescale).data(), mid_buf1, alpha,
                    alpha, r, r, r, alpha, dtype, dtype);

221
            rep(i, alpha) rep(j, alpha) {
222
                filter_transform_buf[filter_visitor.put(alpha, oc, ic, i, j)] =
223 224 225 226
                        mid_buf1[i * alpha + j];
            }
        }
    }
227
}
228

229 230 231 232 233 234 235 236 237 238
template <typename ctype, typename dst_type, typename input_filter_compute_type,
          typename output_compute_type, param::ConvBias::Format layout,
          param::MatrixMul::Format format>
void StrategyHelper<
        ctype, dst_type, input_filter_compute_type, output_compute_type, layout,
        format>::input(const ctype* input,
                       input_filter_compute_type* input_transform_buf,
                       input_filter_compute_type* transform_mid_buf,
                       int ih_start, int iw_start, size_t IH, size_t IW,
                       size_t IC, size_t unit_idx, size_t nr_units_in_tile,
239 240 241
                       size_t m, size_t r,
                       const std::vector<float>& interp_points, DType dtype,
                       float rescale) {
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    size_t alpha = m + r - 1;
    WinogradCoeff<input_filter_compute_type> winograd_coeff(m, r,
                                                            interp_points);
    input_filter_compute_type* mid_buf1 = transform_mid_buf;
    input_filter_compute_type* mid_buf2 = transform_mid_buf + alpha * alpha;
    Getter<ctype, input_filter_compute_type> getter(dtype);
    InputVisitor<layout, format> intput_visitor(IC);

    rep(ic, IC) {
        memset(mid_buf1, 0, alpha * alpha * sizeof(input_filter_compute_type));
        rep(i, alpha) rep(j, alpha) {
            int ih = ih_start + i;
            int iw = iw_start + j;
            if (ih >= 0 && ih < (int)IH && iw >= 0 && iw < (int)IW) {
                mid_buf1[i * alpha + j] = getter(
                        input[intput_visitor.get(alpha, ic, IH, IW, ih, iw)]);
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
        }

        megdnn::naive::run_matrix_mul_tpl<input_filter_compute_type,
                                          input_filter_compute_type, true,
                                          false>(
                winograd_coeff.B(rescale).data(), mid_buf1, mid_buf2, alpha,
                alpha, alpha, alpha, alpha, alpha, dtype, dtype);
        megdnn::naive::run_matrix_mul_tpl<input_filter_compute_type,
                                          input_filter_compute_type, false,
                                          false>(
                mid_buf2, winograd_coeff.B(rescale).data(), mid_buf1, alpha,
                alpha, alpha, alpha, alpha, alpha, dtype, dtype);

        rep(i, alpha) rep(j, alpha) {
            input_transform_buf[intput_visitor.put(alpha, ic, nr_units_in_tile,
                                                   unit_idx, i, j)] =
                    mid_buf1[i * alpha + j];
        }
    }
}

template <typename ctype, typename dst_type, typename input_filter_compute_type,
          typename output_compute_type, param::ConvBias::Format layout,
          param::MatrixMul::Format format>
void StrategyHelper<
        ctype, dst_type, input_filter_compute_type, output_compute_type, layout,
        format>::output(const output_compute_type* output_transform_buf,
                        const output_compute_type* bias, dst_type* output,
                        output_compute_type* transform_mid_buf, BiasMode bmode,
                        NonlineMode nonline_mode, size_t oh_start,
                        size_t ow_start, size_t OH, size_t OW, size_t oc_start,
                        size_t oc_end, size_t unit_idx, size_t nr_units_in_tile,
                        size_t m, size_t r,
                        const std::vector<float>& interp_points, DType dtype,
                        float input_filter_scale, float input_filter_rescale,
                        float rescale) {
    size_t alpha = m + r - 1;
    winograd::WinogradCoeff<output_compute_type> winograd_coeff(m, r,
                                                                interp_points);
    output_compute_type* mid_buf1 = transform_mid_buf;
    output_compute_type* mid_buf2 = transform_mid_buf + alpha * alpha;
    OutputGetter<output_compute_type, dst_type> getter(dtype);
    OutputVisitor<layout, format> output_visitor(oc_end - oc_start);

    for (size_t oc = oc_start; oc < oc_end; oc++) {
        /* gather */
        rep(i, alpha) rep(j, alpha) {
            mid_buf1[i * alpha + j] = output_transform_buf[output_visitor.get(
                    alpha, oc - oc_start, oc, nr_units_in_tile, unit_idx, i,
                    j)];
        }
        /* A[alpha*m] M[alpha*alpha] */
        megdnn::naive::run_matrix_mul_tpl<output_compute_type,
                                          output_compute_type, true, false>(
                winograd_coeff.A(rescale).data(), mid_buf1, mid_buf2, m, alpha,
                alpha, m, alpha, alpha, dtype, dtype);
        megdnn::naive::run_matrix_mul_tpl<output_compute_type,
                                          output_compute_type, false, false>(
                mid_buf2, winograd_coeff.A(rescale).data(), mid_buf1, m, m,
                alpha, alpha, m, m, dtype, dtype);

        rep(i, m) rep(j, m) {
            auto oh = oh_start + i;
            auto ow = ow_start + j;
            if (oh < OH && ow < OW) {
                float val = mid_buf1[i * m + j];
                if (bmode == BiasMode::BROADCAST_CHANNEL_BIAS) {
                    val += bias[oc] * input_filter_rescale *
                           input_filter_rescale;
                } else if (bmode == BiasMode::BIAS) {
                    val += bias[output_visitor.put(oc, OH, OW, oh, ow)] *
                           input_filter_rescale * input_filter_rescale;
331
                }
332 333 334 335 336 337 338 339 340 341 342 343 344
                val = val * input_filter_scale /
                      (input_filter_rescale * input_filter_rescale * rescale *
                       rescale);
                if (nonline_mode == NonlineMode::RELU) {
                    val = val > 0 ? val : 0;
                } else if (nonline_mode == NonlineMode::SIGMOID) {
                    val = 1.f / (expf(-val) + 1.f);
                } else if (nonline_mode == NonlineMode::H_SWISH) {
                    val = val * std::min(std::max(val + 3, 0.f), 6.f) / 6.f;
                } else {
                    megdnn_assert(nonline_mode == NonlineMode::IDENTITY);
                }
                output[output_visitor.put(oc, OH, OW, oh, ow)] = getter(val);
345 346 347 348 349
            }
        }
    }
};

350 351 352 353 354
#define INST(_ctype, _dst_type, _input_filter_compute_type,   \
             _output_compute_type)                            \
    template class StrategyHelper<_ctype, _dst_type,          \
                                  _input_filter_compute_type, \
                                  _output_compute_type>;
355 356 357 358 359 360 361 362

INST(float, float, float, float)
MEGDNN_INC_FLOAT16(INST(dt_float16, dt_float16, dt_float16, dt_float16))
INST(int8_t, int8_t, int16_t, int)
INST(uint8_t, uint8_t, int16_t, int)
#undef INST

#define INST(_ctype, _dst_type, _input_filter_compute_type, \
363
             _output_compute_type, layout)                  \
364 365
    template class StrategyHelper<                          \
            _ctype, _dst_type, _input_filter_compute_type,  \
366 367
            _output_compute_type, layout, param::MatrixMul::Format::MK4>;
INST(float, float, float, float, param::ConvBias::Format::NCHW)
368 369 370
#undef INST

#define INST(_ctype, _dst_type, _input_filter_compute_type, \
371
             _output_compute_type, layout)                  \
372 373
    template class StrategyHelper<                          \
            _ctype, _dst_type, _input_filter_compute_type,  \
374 375 376 377 378
            _output_compute_type, layout, param::MatrixMul::Format::MK8>;
INST(int8_t, int8_t, int16_t, int, param::ConvBias::Format::NCHW)
INST(float, float, float, float, param::ConvBias::Format::NCHW88)
MEGDNN_INC_FLOAT16(INST(dt_float16, dt_float16, dt_float16, dt_float16,
                        param::ConvBias::Format::NCHW))
379 380 381 382 383
#undef INST
}  // namespace winograd
}  // namespace megdnn

// vim: syntax=cpp.doxygen