helper.h 6.7 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 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
#pragma once
#include "megdnn/dtype.h"

#if MEGDNN_CC_HOST
#include "megdnn/basic_types.h"
#endif

namespace megdnn {
namespace device_reduce {

template <typename src_ctype, typename dst_ctype, typename wtype_>
struct NormOp;

template <>
struct NormOp<dt_float32, dt_float32, dt_float32> {
    typedef dt_float32 wtype;
    typedef dt_float32 src_ctype;
    typedef dt_float32 dst_ctype;
    typedef wtype p_type;
    const wtype INIT;

    src_ctype* src;
    dst_ctype* dst;
    const size_t B;
    const p_type p;

    MEGDNN_HOST MEGDNN_DEVICE wtype read(uint32_t idx) {
        return powf(fabsf(src[idx]), p);
    }
    MEGDNN_HOST MEGDNN_DEVICE void write(uint32_t idx, wtype val) {
        dst[idx] = powf(val, 1.f / p);
    }
    static MEGDNN_HOST MEGDNN_DEVICE wtype apply(wtype lhs, wtype rhs) {
        return lhs + rhs;
    }
    MEGDNN_HOST MEGDNN_DEVICE NormOp(src_ctype* src, dst_ctype* dst, size_t B, p_type p)
            : INIT(wtype(0)), src(src), dst(dst), B(B), p(static_cast<wtype>(p)) {}
};

#if !MEGDNN_DISABLE_FLOAT16
template <>
struct NormOp<dt_float16, dt_float16, dt_float16> {
    typedef dt_float16 wtype;
    typedef dt_float16 src_ctype;
    typedef dt_float16 dst_ctype;
    const wtype INIT;

    src_ctype* src;
    dst_ctype* dst;
    const size_t B;
    const wtype p;

    // HALF_FLOAT API has dispatch host and device.
    MEGDNN_HOST MEGDNN_DEVICE wtype read(uint32_t idx) {
        return half_float::detail::pow(half_float::detail::abs(src[idx]), p);
    }
    MEGDNN_HOST MEGDNN_DEVICE void write(uint32_t idx, wtype val) {
        dst[idx] = half_float::detail::pow(val, static_cast<wtype>(1.f) / p);
    }
    static MEGDNN_HOST MEGDNN_DEVICE wtype apply(wtype lhs, wtype rhs) {
        return lhs + rhs;
    }
    MEGDNN_HOST MEGDNN_DEVICE
    NormOp(src_ctype* src, dst_ctype* dst, size_t B, dt_float32 p)
            : INIT(wtype(0)), src(src), dst(dst), B(B), p(static_cast<wtype>(p)) {}
};
#endif

// TODO: 0Norm impl need understand reduceop
template <typename src_ctype, typename dst_ctype, typename wtype_>
struct NormZeroOp;

template <>
struct NormZeroOp<dt_float32, dt_float32, dt_float32> {
    typedef dt_float32 wtype;
    typedef dt_float32 src_ctype;
    typedef dt_float32 dst_ctype;
    const wtype INIT;

    src_ctype* src;
    dst_ctype* dst;
    const size_t B;
    const wtype epsilon = 0.00001f;

    MEGDNN_HOST MEGDNN_DEVICE wtype read(uint32_t idx) {
        return fabsf(src[idx] - 0.0f) <= epsilon ? 0.0f : 1.0f;
    }
    MEGDNN_HOST MEGDNN_DEVICE void write(uint32_t idx, wtype val) { dst[idx] = val; }

    static MEGDNN_HOST MEGDNN_DEVICE wtype apply(wtype lhs, wtype rhs) {
        return lhs + rhs;
    }
    MEGDNN_HOST MEGDNN_DEVICE NormZeroOp(src_ctype* src, dst_ctype* dst, size_t B)
            : INIT(wtype(0)), src(src), dst(dst), B(B) {}
};

#if !MEGDNN_DISABLE_FLOAT16
template <>
struct NormZeroOp<dt_float16, dt_float16, dt_float16> {
    typedef dt_float16 wtype;
    typedef dt_float16 src_ctype;
    typedef dt_float16 dst_ctype;
    const wtype INIT;

    src_ctype* src;
    dst_ctype* dst;
    const size_t B;
    const wtype epsilon = half_float::half(0.00001f);

    MEGDNN_HOST MEGDNN_DEVICE wtype read(uint32_t idx) {
        return half_float::detail::fabs(src[idx] - half_float::half()) <= epsilon
                     ? half_float::half(0.0f)
                     : half_float::half(1.0f);
    }
    MEGDNN_HOST MEGDNN_DEVICE void write(uint32_t idx, wtype val) { dst[idx] = val; }

    static MEGDNN_HOST MEGDNN_DEVICE wtype apply(wtype lhs, wtype rhs) {
        return lhs + rhs;
    }
    MEGDNN_HOST MEGDNN_DEVICE NormZeroOp(src_ctype* src, dst_ctype* dst, size_t B)
            : INIT(wtype(0)), src(src), dst(dst), B(B) {}
};
#endif

template <typename src_ctype, typename dst_ctype, typename wtype_>
struct NormOneOp;

template <>
struct NormOneOp<dt_float32, dt_float32, dt_float32> {
    typedef dt_float32 wtype;
    typedef dt_float32 src_ctype;
    typedef dt_float32 dst_ctype;
    const wtype INIT;

    src_ctype* src;
    dst_ctype* dst;
    const size_t B;

    MEGDNN_HOST MEGDNN_DEVICE wtype read(uint32_t idx) { return fabsf(src[idx]); }
    MEGDNN_HOST MEGDNN_DEVICE void write(uint32_t idx, wtype val) { dst[idx] = val; }

    static MEGDNN_HOST MEGDNN_DEVICE wtype apply(wtype lhs, wtype rhs) {
        return lhs + rhs;
    }
    MEGDNN_HOST MEGDNN_DEVICE NormOneOp(src_ctype* src, dst_ctype* dst, size_t B)
            : INIT(wtype(0)), src(src), dst(dst), B(B) {}
};

#if !MEGDNN_DISABLE_FLOAT16
template <>
struct NormOneOp<dt_float16, dt_float16, dt_float16> {
    typedef dt_float16 wtype;
    typedef dt_float16 src_ctype;
    typedef dt_float16 dst_ctype;
    const wtype INIT;

    src_ctype* src;
    dst_ctype* dst;
    const size_t B;

    MEGDNN_HOST MEGDNN_DEVICE wtype read(uint32_t idx) {
        return half_float::detail::abs(src[idx]);
    }
    MEGDNN_HOST MEGDNN_DEVICE void write(uint32_t idx, wtype val) { dst[idx] = val; }

    static MEGDNN_HOST MEGDNN_DEVICE wtype apply(wtype lhs, wtype rhs) {
        return lhs + rhs;
    }
    MEGDNN_HOST MEGDNN_DEVICE NormOneOp(src_ctype* src, dst_ctype* dst, size_t B)
            : INIT(wtype(0)), src(src), dst(dst), B(B) {}
};
#endif

template <typename src_ctype, typename dst_ctype, typename wtype_>
struct NormTwoOp;

template <>
struct NormTwoOp<dt_float32, dt_float32, dt_float32> {
    typedef dt_float32 wtype;
    typedef dt_float32 src_ctype;
    typedef dt_float32 dst_ctype;
    const wtype INIT;

    src_ctype* src;
    dst_ctype* dst;
    const size_t B;

    MEGDNN_HOST MEGDNN_DEVICE wtype read(uint32_t idx) { return src[idx] * src[idx]; }
    MEGDNN_HOST MEGDNN_DEVICE void write(uint32_t idx, wtype val) {
        dst[idx] = sqrtf(val);
    }

    static MEGDNN_HOST MEGDNN_DEVICE wtype apply(wtype lhs, wtype rhs) {
        return lhs + rhs;
    }
    MEGDNN_HOST MEGDNN_DEVICE NormTwoOp(src_ctype* src, dst_ctype* dst, size_t B)
            : INIT(wtype(0)), src(src), dst(dst), B(B) {}
};

#if !MEGDNN_DISABLE_FLOAT16
template <>
struct NormTwoOp<dt_float16, dt_float16, dt_float16> {
    typedef dt_float16 wtype;
    typedef dt_float16 src_ctype;
    typedef dt_float16 dst_ctype;
    const wtype INIT;

    src_ctype* src;
    dst_ctype* dst;
    const size_t B;

    MEGDNN_HOST MEGDNN_DEVICE wtype read(uint32_t idx) { return src[idx] * src[idx]; }
    MEGDNN_HOST MEGDNN_DEVICE void write(uint32_t idx, wtype val) {
        dst[idx] = half_float::detail::sqrt(val);
    }

    static MEGDNN_HOST MEGDNN_DEVICE wtype apply(wtype lhs, wtype rhs) {
        return lhs + rhs;
    }
    MEGDNN_HOST MEGDNN_DEVICE NormTwoOp(src_ctype* src, dst_ctype* dst, size_t B)
            : INIT(wtype(0)), src(src), dst(dst), B(B) {}
};
#endif

}  // namespace device_reduce
}  // namespace megdnn