opr_impl.cpp 10.5 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 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
#include "src/naive/norm/opr_impl.h"

#include "helper.h"
#include "src/common/utils.h"
#include "src/naive/handle.h"

namespace megdnn {
namespace naive {
using Mode = Norm::Mode;

template <>
void NormForwardImpl::dispatch_mode<Mode::NEG_INF_NORM>(
        _megdnn_tensor_in src, _megdnn_tensor_out dst, size_t A, size_t B, size_t C) {
#define CASE(dt)                                                                 \
    case DTypeTrait<dt>::enumv: {                                                \
        using ctype = DTypeTrait<dt>::ctype;                                     \
        const ctype* __restrict sptr = src.ptr<ctype>();                         \
        ctype* __restrict dptr = dst.ptr<ctype>();                               \
        std::function<ctype(size_t, size_t, size_t, size_t)> func;               \
        func = [&](size_t a, size_t c, size_t bl, size_t br) -> ctype {          \
            if (bl + 1 < br) {                                                   \
                size_t mid = bl + (br - bl) / 2;                                 \
                return Trait<ReduceForward::Mode::MIN, ctype>::apply(            \
                        func(a, c, bl, mid), func(a, c, mid, br));               \
            } else {                                                             \
                return Trait<ReduceForward::Mode::MIN, ctype>::visit(            \
                        sptr[a * B * C + bl * C + c]);                           \
            }                                                                    \
        };                                                                       \
        for (size_t a = 0; a < A; ++a)                                           \
            for (size_t c = 0; c < C; ++c) {                                     \
                dptr[a * C + c] = Trait<ReduceForward::Mode::MIN, ctype>::write( \
                        func(a, c, 0, B), B);                                    \
            }                                                                    \
        break;                                                                   \
    };

    switch (src.layout.dtype.enumv()) {
        CASE(::megdnn::dtype::Float32)
#if !MEGDNN_DISABLE_FLOAT16
        CASE(::megdnn::dtype::Float16)
#endif
        default:
            megdnn_assert_internal(false);
    }
#undef CASE
}

template <>
void NormForwardImpl::dispatch_mode<Mode::INF_NORM>(
        _megdnn_tensor_in src, _megdnn_tensor_out dst, size_t A, size_t B, size_t C) {
#define CASE(dt)                                                                 \
    case DTypeTrait<dt>::enumv: {                                                \
        using ctype = DTypeTrait<dt>::ctype;                                     \
        const ctype* __restrict sptr = src.ptr<ctype>();                         \
        ctype* __restrict dptr = dst.ptr<ctype>();                               \
        std::function<ctype(size_t, size_t, size_t, size_t)> func;               \
        func = [&](size_t a, size_t c, size_t bl, size_t br) -> ctype {          \
            if (bl + 1 < br) {                                                   \
                size_t mid = bl + (br - bl) / 2;                                 \
                return Trait<ReduceForward::Mode::MAX, ctype>::apply(            \
                        func(a, c, bl, mid), func(a, c, mid, br));               \
            } else {                                                             \
                return Trait<ReduceForward::Mode::MAX, ctype>::visit(            \
                        sptr[a * B * C + bl * C + c]);                           \
            }                                                                    \
        };                                                                       \
        for (size_t a = 0; a < A; ++a)                                           \
            for (size_t c = 0; c < C; ++c) {                                     \
                dptr[a * C + c] = Trait<ReduceForward::Mode::MAX, ctype>::write( \
                        func(a, c, 0, B), B);                                    \
            }                                                                    \
        break;                                                                   \
    };

    switch (src.layout.dtype.enumv()) {
        CASE(::megdnn::dtype::Float32)
#if !MEGDNN_DISABLE_FLOAT16
        CASE(::megdnn::dtype::Float16)
#endif
        default:
            megdnn_assert_internal(false);
    }
#undef CASE
}

template <>
void NormForwardImpl::dispatch_mode<Mode::P_NORM>(
        _megdnn_tensor_in src, _megdnn_tensor_out dst, size_t A, size_t B, size_t C) {
#define CASE(dt)                                                                     \
    case DTypeTrait<dt>::enumv: {                                                    \
        using ctype = DTypeTrait<dt>::ctype;                                         \
        const ctype* __restrict sptr = src.ptr<ctype>();                             \
        ctype* __restrict dptr = dst.ptr<ctype>();                                   \
        std::function<ctype(size_t, size_t, size_t, size_t)> func;                   \
        if (param().p - 0.f < 0.00001f) {                                            \
            func = [&](size_t a, size_t c, size_t bl, size_t br) -> ctype {          \
                if (bl + 1 < br) {                                                   \
                    size_t mid = bl + (br - bl) / 2;                                 \
                    return NormZeroOp<ctype>::apply(                                 \
                            func(a, c, bl, mid), func(a, c, mid, br));               \
                } else {                                                             \
                    return NormZeroOp<ctype>::visit(sptr[a * B * C + bl * C + c]);   \
                }                                                                    \
            };                                                                       \
            for (size_t a = 0; a < A; ++a) {                                         \
                for (size_t c = 0; c < C; ++c) {                                     \
                    dptr[a * C + c] = NormZeroOp<ctype>::write(func(a, c, 0, B), B); \
                }                                                                    \
            }                                                                        \
        } else {                                                                     \
            func = [&](size_t a, size_t c, size_t bl, size_t br) -> ctype {          \
                if (bl + 1 < br) {                                                   \
                    size_t mid = bl + (br - bl) / 2;                                 \
                    return NormOp<ctype>::apply(                                     \
                            func(a, c, bl, mid), func(a, c, mid, br));               \
                } else {                                                             \
                    return NormOp<ctype>::visit(                                     \
                            sptr[a * B * C + bl * C + c], param().p);                \
                }                                                                    \
            };                                                                       \
            for (size_t a = 0; a < A; ++a) {                                         \
                for (size_t c = 0; c < C; ++c) {                                     \
                    dptr[a * C + c] =                                                \
                            NormOp<ctype>::write(func(a, c, 0, B), B, param().p);    \
                }                                                                    \
            }                                                                        \
        }                                                                            \
        break;                                                                       \
    };

    switch (src.layout.dtype.enumv()) {
        CASE(::megdnn::dtype::Float32)
#if !MEGDNN_DISABLE_FLOAT16
        CASE(::megdnn::dtype::Float16)
#endif
        default:
            megdnn_assert_internal(false);
    }
#undef CASE
}

void NormForwardImpl::exec(
        _megdnn_tensor_in src, _megdnn_tensor_out dst, _megdnn_workspace workspace) {
    check_exec(src.layout, dst.layout, workspace.size);
    using namespace reduce;
    size_t A, B, C;
    reduce::get_ABC(src.layout, A, B, C, param().dim);
    auto make_tensor = [&](DType comp_dtype, _megdnn_tensor_inout tensor,
                           dt_byte*& workspace_ptr) {
        if (comp_dtype == tensor.layout.dtype)
            return tensor;
        auto layout = TensorLayout(tensor.layout, comp_dtype);
        TensorND new_tensor{workspace_ptr, layout};
        workspace_ptr += layout.span().dist_byte();
        return new_tensor;
    };
    auto typecvt = handle()->create_operator<TypeCvt>();

    auto copy_to = [&typecvt](const TensorND& from, const TensorND& to) {
        if (from.raw_ptr() != to.raw_ptr())
            typecvt->exec(from, to);
    };

    auto workspace_ptr = workspace.ptr<dt_byte>();

    auto new_src = make_tensor(src.layout.dtype, src, workspace_ptr);
    auto new_dst = make_tensor(dst.layout.dtype, dst, workspace_ptr);

#define CASE(mode)                                                                   \
    case mode: {                                                                     \
        copy_to(src, new_src);                                                       \
        ::megdnn::naive::HandleImpl* handlePtr = static_cast<HandleImpl*>(handle()); \
        MEGDNN_DISPATCH_CPU_KERN(                                                    \
                handlePtr, dispatch_mode<mode>(new_src, new_dst, A, B, C));          \
        copy_to(new_dst, dst);                                                       \
        break;                                                                       \
    };
    switch (param().mode) {
        CASE(Mode::P_NORM)
        CASE(Mode::INF_NORM)
        CASE(Mode::NEG_INF_NORM)
        default:
            megdnn_assert_internal(false);
    }
#undef CASE
}

size_t NormForwardImpl::get_workspace_in_bytes(
        const TensorLayout& src, const TensorLayout& dst) {
    MEGDNN_MARK_USED_VAR(src);
    MEGDNN_MARK_USED_VAR(dst);
    return 0;
}

}  // namespace naive
}  // namespace megdnn