opr_impl.cpp 10.5 KB
Newer Older
1 2 3 4
/**
 * \file dnn/src/common/elemwise_multi_type/opr_impl.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
5
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
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
 *
 * 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 <mutex>
#include "megdnn/oprs.h"
#include "src/common/utils.h"

#include "midout.h"
MIDOUT_DECL(megdnn_common_elemwise_multi_type)

using namespace megdnn;

using Mode = ElemwiseMultiType::Mode;
using ModeTrait = ElemwiseMultiType::ModeTrait;

namespace {
void check_dtype(const ModeTrait& trait, size_t i, const TensorLayout& src) {
    trait.check_inp[i](src.dtype);
}
}  // anonymous namespace

const ModeTrait& ModeTrait::from_mode(Mode mode) {
31
    static DNN_MUTEX mtx;
32 33
    static std::vector<ModeTrait> traits;

34
    MEGDNN_LOCK_GUARD(mtx);
35 36 37

    auto make_check_dtype_func = [](DType expected) {
        auto func = [expected](DType dtype) {
M
Megvii Engine Team 已提交
38 39 40
            megdnn_assert(
                    expected.enumv() == dtype.enumv(), "expected %s, but got %s",
                    expected.name(), dtype.name());
41 42 43 44 45 46 47 48 49 50 51 52 53 54
        };
        return func;
    };

    auto make_check_category = [](DTypeCategory expected) {
        auto func = [expected](DType dtype) {
            megdnn_assert(expected == dtype.category());
        };
        return func;
    };

    auto make_out_dtype_func = [](DType expected) {
        auto func = [expected](DType& dtype, bool check) {
            if (check) {
M
Megvii Engine Team 已提交
55 56 57
                megdnn_assert(
                        expected.enumv() == dtype.enumv(), "expected %s, but got %s",
                        expected.name(), dtype.name());
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
            } else {
                dtype = expected;
            }
        };
        return func;
    };

    auto make_out_category_func = [](DTypeCategory expected) {
        auto func = [expected](DType& dtype, bool) {
            megdnn_assert(expected == dtype.category());
        };
        return func;
    };

    if (traits.empty()) {
        traits.resize(Param::MODE_NR_MEMBER);
        auto init_fma3_int16x32x32x32 = [&](ModeTrait& dst, const char* name) {
            dst.arity = 3;
            dst.check_inp[0] = make_check_dtype_func(dtype::Int16());
            dst.check_inp[1] = make_check_dtype_func(dtype::Int32());
            dst.check_inp[2] = make_check_dtype_func(dtype::Int32());
            dst.check_out = make_out_dtype_func(dtype::Int32());
            dst.name = name;
        };
        auto init_fma3_iXxf32xf32xi8 = [&](ModeTrait& dst, const char* name) {
            dst.arity = 3;
            dst.check_inp[0] = make_check_category(DTypeCategory::INT);
            dst.check_inp[1] = make_check_dtype_func(dtype::Float32());
            dst.check_inp[2] = make_check_dtype_func(dtype::Float32());
            dst.check_out = make_out_dtype_func(dtype::Int8());
            dst.name = name;
        };
        auto init_rshrs_iXxi8xi8 = [&](ModeTrait& dst, const char* name) {
            dst.arity = 2;
            dst.check_inp[0] = make_check_category(DTypeCategory::INT);
            dst.check_inp[1] = make_check_dtype_func(dtype::Int8());
            dst.check_out = make_out_dtype_func(dtype::Int8());
            dst.name = name;
        };
        auto init_fuse_add_rmulh_rshr_int16x16x16x8 = [&](ModeTrait& dst,
                                                          const char* name) {
            // TODO: This is stupid, we should parameterize shift
            //                   offset, minv and maxv.
            dst.arity = 6;

            dst.check_inp[0] = make_check_dtype_func(dtype::Int16());
            dst.check_inp[1] = make_check_dtype_func(dtype::Int16());
            dst.check_inp[2] = make_check_dtype_func(dtype::Int16());
            dst.check_inp[3] = make_check_dtype_func(dtype::Int8());
            dst.check_inp[4] = make_check_dtype_func(dtype::Int8());
            dst.check_inp[5] = make_check_dtype_func(dtype::Int8());
            dst.check_out = make_out_dtype_func(dtype::Int8());
            dst.name = name;
        };
        auto init_fuse_add_rmulh_rshr_int32x32x32x8 = [&](ModeTrait& dst,
                                                          const char* name) {
            dst.arity = 6;
            dst.check_inp[0] = make_check_dtype_func(dtype::Int32());
            dst.check_inp[1] = make_check_dtype_func(dtype::Int32());
            dst.check_inp[2] = make_check_dtype_func(dtype::Int32());
            dst.check_inp[3] = make_check_dtype_func(dtype::Int8());
            dst.check_inp[4] = make_check_dtype_func(dtype::Int8());
            dst.check_inp[5] = make_check_dtype_func(dtype::Int8());
            dst.check_out = make_out_dtype_func(dtype::Int8());
            dst.name = name;
        };
        auto init_rshrs_iXxi8xi16 = [&](ModeTrait& dst, const char* name) {
            dst.arity = 2;
            dst.check_inp[0] = make_check_category(DTypeCategory::INT);
            dst.check_inp[1] = make_check_dtype_func(dtype::Int8());
            dst.check_out = make_out_dtype_func(dtype::Int16());
            dst.name = name;
        };

        auto init_quantized_unary_op = [&](ModeTrait& dst, const char* name) {
            dst.arity = 1;
            dst.check_inp[0] = make_check_category(DTypeCategory::QUANTIZED);
            dst.check_out = make_out_category_func(DTypeCategory::QUANTIZED);
            dst.name = name;
            dst.need_specify_out_dtype = true;
        };

        auto init_quantized_binary_op = [&](ModeTrait& dst, const char* name) {
            dst.arity = 2;
            dst.check_inp[0] = make_check_category(DTypeCategory::QUANTIZED);
            dst.check_inp[1] = make_check_category(DTypeCategory::QUANTIZED);
            dst.check_out = make_out_category_func(DTypeCategory::QUANTIZED);
            dst.name = name;
            dst.need_specify_out_dtype = true;
        };

        auto init_quantized_ternary_op = [&](ModeTrait& dst, const char* name) {
            dst.arity = 3;
            dst.check_inp[0] = make_check_category(DTypeCategory::QUANTIZED);
            dst.check_inp[1] = make_check_category(DTypeCategory::QUANTIZED);
            dst.check_inp[2] = make_check_category(DTypeCategory::QUANTIZED);
            dst.check_out = make_out_category_func(DTypeCategory::QUANTIZED);
            dst.name = name;
            dst.need_specify_out_dtype = true;
        };

#define SET(f, m)                                                         \
    MIDOUT_BEGIN(megdnn_common_elemwise_multi_type, midout_iv(Mode::m)) { \
M
Megvii Engine Team 已提交
161
        f(traits[static_cast<int>(Mode::m)], (#m));                       \
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
    }                                                                     \
    MIDOUT_END();
        SET(init_fma3_int16x32x32x32, FUSE_MUL_ADD3_INT16x32x32x32);
        SET(init_fma3_iXxf32xf32xi8, FUSE_MUL_ADD3_IXxF32xF32xI8);
        SET(init_rshrs_iXxi8xi8, ROUND_SHR_SATURATE_IXxI8xI8);
        SET(init_fuse_add_rmulh_rshr_int16x16x16x8,
            FUSE_ADD_RMULH_ROUND_SHR_SATURATE_INT16x16x16x8);
        SET(init_fuse_add_rmulh_rshr_int32x32x32x8,
            FUSE_ADD_RMULH_ROUND_SHR_SATURATE_INT32x32x32x8);
        SET(init_rshrs_iXxi8xi16, ROUND_SHR_SATURATE_IXxI8xI16);

        //! quantized opr, with specified dtype.
        //! dispatch elemwise mode internally
        SET(init_quantized_unary_op, QRELU);
        SET(init_quantized_unary_op, QABS);
        SET(init_quantized_unary_op, QACOS);
        SET(init_quantized_unary_op, QASIN);
        SET(init_quantized_unary_op, QCEIL);
        SET(init_quantized_unary_op, QCOS);
        SET(init_quantized_unary_op, QEXP);
        SET(init_quantized_unary_op, QEXPM1);
        SET(init_quantized_unary_op, QFLOOR);
        SET(init_quantized_unary_op, QLOG);
        SET(init_quantized_unary_op, QLOG1P);
        SET(init_quantized_unary_op, QNEGATE);
        SET(init_quantized_unary_op, QSIGMOID);
        SET(init_quantized_unary_op, QSIN);
        SET(init_quantized_unary_op, QTANH);
        SET(init_quantized_unary_op, QFAST_TANH);
        SET(init_quantized_unary_op, QROUND);
        SET(init_quantized_unary_op, QERF);
        SET(init_quantized_unary_op, QERFINV);
        SET(init_quantized_unary_op, QERFC);
        SET(init_quantized_unary_op, QERFCINV);
        SET(init_quantized_unary_op, QH_SWISH);

        SET(init_quantized_binary_op, QABS_GRAD);
        SET(init_quantized_binary_op, QADD);
        SET(init_quantized_binary_op, QFLOOR_DIV);
        SET(init_quantized_binary_op, QMAX);
        SET(init_quantized_binary_op, QMIN);
        SET(init_quantized_binary_op, QMOD);
        SET(init_quantized_binary_op, QMUL);
        SET(init_quantized_binary_op, QPOW);
        SET(init_quantized_binary_op, QSIGMOID_GRAD);
        SET(init_quantized_binary_op, QSUB);
        SET(init_quantized_binary_op, QSWITCH_GT0);
        SET(init_quantized_binary_op, QTANH_GRAD);
        SET(init_quantized_binary_op, QTRUE_DIV);
        SET(init_quantized_binary_op, QLOG_SUM_EXP);

        SET(init_quantized_binary_op, QLT);
        SET(init_quantized_binary_op, QLEQ);
        SET(init_quantized_binary_op, QEQ);

        SET(init_quantized_binary_op, QFUSE_ADD_RELU);
        SET(init_quantized_binary_op, QFUSE_ADD_SIGMOID);
        SET(init_quantized_binary_op, QFUSE_ADD_TANH);
        SET(init_quantized_binary_op, QFAST_TANH_GRAD);
        SET(init_quantized_binary_op, QATAN2);
        SET(init_quantized_binary_op, QH_SWISH_GRAD);
        SET(init_quantized_binary_op, QFUSE_ADD_H_SWISH);

        SET(init_quantized_ternary_op, QFUSE_MUL_ADD3);
        SET(init_quantized_ternary_op, QCOND_LEQ_MOV);
#undef SET
    }

    return traits.at(static_cast<int>(mode));
}

M
Megvii Engine Team 已提交
233
void ElemwiseMultiType::deduce_layout(const TensorLayoutArray& src, TensorLayout& dst) {
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
    auto trait = mode_trait();
    megdnn_assert(src.size() == trait.arity);
    for (size_t i = 0; i < trait.arity; ++i) {
        check_dtype(trait, i, src[i]);
    }
    TensorShapeArray src_shp;
    for (auto&& i : src)
        src_shp.push_back(i);
    Elemwise::deduce_shape(src_shp, dst);
    dst.init_contiguous_stride();
    trait.check_out(dst.dtype, false);
}

void ElemwiseMultiType::check_layout_and_broadcast(
        const TensorLayoutPtrArray& src, const TensorLayout& dst) {
    auto trait = mode_trait();
    megdnn_assert(src.size() == trait.arity);
    for (size_t i = 0; i < trait.arity; ++i) {
        check_dtype(trait, i, *src[i]);
        *src[i] = src[i]->broadcast(dst);
    }
    auto dtype = dst.dtype;
    trait.check_out(dtype, true);
    megdnn_assert(dst.is_contiguous());
}

// vim: syntax=cpp.doxygen